Skip to content

Commit 5aa1707

Browse files
committed
fixup! copy newRNGSeed() from images to get stable rng numbers
1 parent e52bdf7 commit 5aa1707

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

bib/cmd/bootc-image-builder/image.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math"
88
"math/big"
99
"math/rand"
10+
"os"
1011
"slices"
1112
"strconv"
1213
"strings"
@@ -667,13 +668,23 @@ func getDistroAndRunner(osRelease osinfo.OSRelease) (manifest.Distro, runner.Run
667668
return manifest.DISTRO_NULL, &runner.Linux{}, nil
668669
}
669670

671+
// XXX: copied from images:internal/cmdutil/rand.go
672+
const RNG_SEED_ENV_KEY = "OSBUILD_TESTING_RNG_SEED"
673+
670674
func createRand() *rand.Rand {
671-
seed, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64))
675+
if envSeedStr := os.Getenv(RNG_SEED_ENV_KEY); envSeedStr != "" {
676+
envSeedInt, err := strconv.ParseInt(envSeedStr, 10, 64)
677+
if err != nil {
678+
panic(fmt.Errorf("failed to parse %s: %s", RNG_SEED_ENV_KEY, err))
679+
}
680+
fmt.Fprintf(os.Stderr, "TEST MODE: using rng seed %d\n", envSeedInt)
681+
return rand.New(rand.NewSource(envSeedInt))
682+
}
683+
randSeed, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64))
672684
if err != nil {
673-
panic("Cannot generate an RNG seed.")
685+
panic(fmt.Errorf("failed to generate random seed: %s", err))
674686
}
675-
676687
// math/rand is good enough in this case
677688
/* #nosec G404 */
678-
return rand.New(rand.NewSource(seed.Int64()))
689+
return rand.New(rand.NewSource(randSeed.Int64()))
679690
}

0 commit comments

Comments
 (0)