Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libpod: Ensure that generated container names are random #15581

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions libpod/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -112,6 +113,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.
Expand Down
28 changes: 28 additions & 0 deletions libpod/runtime_test.go
Original file line number Diff line number Diff line change
@@ -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)
}