Skip to content

Commit

Permalink
shortnames: error if there's no alias and no search registries
Browse files Browse the repository at this point in the history
When a short-name could not be resolved to an alias and if there's no
unqualified-search registry, throw an error.

Reported-in: github.com/containers/podman/issues/8573
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Dec 3, 2020
1 parent 4f9be6b commit 0813d58
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
9 changes: 7 additions & 2 deletions pkg/shortnames/shortnames.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,21 +328,26 @@ func Resolve(ctx *types.SystemContext, name string) (*Resolved, error) {
// If we're running in disabled, return the candidates without
// prompting (and without recording).
if mode == types.ShortNameModeDisabled {
if len(unqualifiedSearchRegistries) == 0 {
return nil, errors.Errorf("short-name %q did not resolve to an alias and no unqualified-search registries are defined in %q", name, usrConfig)
}
return resolved, nil
}

// If we have only one candidate, there's no ambiguity. In case of an
// empty candidate slices, callers can implement custom logic or raise
// an error.
if len(resolved.PullCandidates) <= 1 {
if len(resolved.PullCandidates) == 1 {
return resolved, nil
}

// If we don't have a TTY, act according to the mode.
if !terminal.IsTerminal(int(os.Stdout.Fd())) || !terminal.IsTerminal(int(os.Stdin.Fd())) {
switch mode {
case types.ShortNameModePermissive:
// Permissive falls back to using all candidates.
if len(unqualifiedSearchRegistries) == 0 {
return nil, errors.Errorf("short-name %q did not resolve to an alias and no unqualified-search registries are defined in %q", name, usrConfig)
}
return resolved, nil
case types.ShortNameModeEnforcing:
// Enforcing errors out without a prompt.
Expand Down
15 changes: 7 additions & 8 deletions pkg/shortnames/shortnames_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,11 @@ func TestResolve(t *testing.T) {
assert.False(t, resolved.PullCandidates[0].record)
}

// Non-existent should return an empty slice as no search registries
// are configured in the config.
// Non-existent should return an error as no search registries are
// configured in the config.
resolved, err := Resolve(sys, "dontexist")
require.NoError(t, err)
require.NotNil(t, resolved)
require.Len(t, resolved.PullCandidates, 0)
require.Error(t, err)
require.Nil(t, resolved)

// An empty name is not valid.
resolved, err = Resolve(sys, "")
Expand Down Expand Up @@ -354,23 +353,23 @@ func TestResolveWithVaryingShortNameModes(t *testing.T) {
{"testdata/one-reg.conf", types.ShortNameModePermissive, "repo/image", false, 1},
{"testdata/two-reg.conf", types.ShortNameModePermissive, "repo/image", false, 1},
// Permisive + no match -> search (no tty)
{"testdata/no-reg.conf", types.ShortNameModePermissive, "donotexist", false, 0},
{"testdata/no-reg.conf", types.ShortNameModePermissive, "donotexist", true, 0},
{"testdata/one-reg.conf", types.ShortNameModePermissive, "donotexist", false, 1},
{"testdata/two-reg.conf", types.ShortNameModePermissive, "donotexist", false, 2},
// Disabled + match -> return alias
{"testdata/no-reg.conf", types.ShortNameModeDisabled, "repo/image", false, 1},
{"testdata/one-reg.conf", types.ShortNameModeDisabled, "repo/image", false, 1},
{"testdata/two-reg.conf", types.ShortNameModeDisabled, "repo/image", false, 1},
// Disabled + no match -> search
{"testdata/no-reg.conf", types.ShortNameModeDisabled, "donotexist", false, 0},
{"testdata/no-reg.conf", types.ShortNameModeDisabled, "donotexist", true, 0},
{"testdata/one-reg.conf", types.ShortNameModeDisabled, "donotexist", false, 1},
{"testdata/two-reg.conf", types.ShortNameModeDisabled, "donotexist", false, 2},
// Enforcing + match -> return alias
{"testdata/no-reg.conf", types.ShortNameModeEnforcing, "repo/image", false, 1},
{"testdata/one-reg.conf", types.ShortNameModeEnforcing, "repo/image", false, 1},
{"testdata/two-reg.conf", types.ShortNameModeEnforcing, "repo/image", false, 1},
// Enforcing + no match -> error if search regs > 1 and no tty
{"testdata/no-reg.conf", types.ShortNameModeEnforcing, "donotexist", false, 0},
{"testdata/no-reg.conf", types.ShortNameModeEnforcing, "donotexist", true, 0},
{"testdata/one-reg.conf", types.ShortNameModeEnforcing, "donotexist", false, 1},
{"testdata/two-reg.conf", types.ShortNameModeEnforcing, "donotexist", true, 0},
}
Expand Down

0 comments on commit 0813d58

Please sign in to comment.