aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Rabson <dfr@rabson.org>2022-08-31 18:02:38 +0100
committerMatthew Heon <mheon@redhat.com>2022-09-06 15:08:10 -0400
commit85f3c2783cb6807032b779bc2827dd79dbcad3e4 (patch)
treed6d0391ee98629c97aacbf4e024f862aa92a0d3f
parent48a02aa1c02ddeffd16b3c19ccf9aa23c482e82a (diff)
downloadpodman-85f3c2783cb6807032b779bc2827dd79dbcad3e4.tar.gz
podman-85f3c2783cb6807032b779bc2827dd79dbcad3e4.tar.bz2
podman-85f3c2783cb6807032b779bc2827dd79dbcad3e4.zip
libpod: Ensure that generated container names are random
Fixes #15569. Signed-off-by: Doug Rabson <dfr@rabson.org>
-rw-r--r--libpod/runtime.go8
-rw-r--r--libpod/runtime_test.go28
2 files changed, 36 insertions, 0 deletions
diff --git a/libpod/runtime.go b/libpod/runtime.go
index ea4b34954..a0a7ef61f 100644
--- a/libpod/runtime.go
+++ b/libpod/runtime.go
@@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
+ "math/rand"
"os"
"os/exec"
"path/filepath"
@@ -127,6 +128,13 @@ type Runtime struct {
secretsManager *secrets.SecretsManager
}
+func init() {
+ // generateName calls namesgenerator.GetRandomName which the
+ // global RNG from math/rand. Seed it here to make sure we
+ // don't get the same name every time.
+ rand.Seed(time.Now().UnixNano())
+}
+
// SetXdgDirs ensures the XDG_RUNTIME_DIR env and XDG_CONFIG_HOME variables are set.
// containers/image uses XDG_RUNTIME_DIR to locate the auth file, XDG_CONFIG_HOME is
// use for the containers.conf configuration file.
diff --git a/libpod/runtime_test.go b/libpod/runtime_test.go
new file mode 100644
index 000000000..2e16c7fcd
--- /dev/null
+++ b/libpod/runtime_test.go
@@ -0,0 +1,28 @@
+package libpod
+
+import (
+ "math/rand"
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func Test_generateName(t *testing.T) {
+ state, path, _, err := getEmptyBoltState()
+ assert.NoError(t, err)
+ defer os.RemoveAll(path)
+ defer state.Close()
+
+ r := &Runtime{
+ state: state,
+ }
+
+ // Test that (*Runtime).generateName returns different names
+ // if called twice, even if the global RNG has the default
+ // seed.
+ n1, _ := r.generateName()
+ rand.Seed(1)
+ n2, _ := r.generateName()
+ assert.NotEqual(t, n1, n2)
+}