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 <rothberg@redhat.com>
  • Loading branch information
vrothberg committed Dec 5, 2020
1 parent 7589fa9 commit 2aaf057
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
15 changes: 8 additions & 7 deletions pkg/shortnames/shortnames.go
Expand Up @@ -225,9 +225,8 @@ func (c *PullCandidate) Record() error {
// Note that tags and digests are stripped from the specified name before
// looking up an alias. Stripped off tags and digests are later on appended to
// all candidates. If neither tag nor digest is specified, candidates are
// normalized with the "latest" tag. PullCandidates in the returned value may
// be empty if there is no matching alias and no unqualified-search registries
// are configured.
// normalized with the "latest" tag. An error is returned if there is no
// matching alias and no unqualified-search registries are configured.
//
// Note that callers *must* call `(PullCandidate).Record` after a returned
// item has been pulled successfully; this callback will record a new
Expand Down Expand Up @@ -312,6 +311,10 @@ func Resolve(ctx *types.SystemContext, name string) (*Resolved, error) {
if err != nil {
return nil, err
}
// Error out if there's no matching alias and no search registries.
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)
}
resolved.originDescription = usrConfig

for _, reg := range unqualifiedSearchRegistries {
Expand All @@ -331,10 +334,8 @@ func Resolve(ctx *types.SystemContext, name string) (*Resolved, error) {
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 we have only one candidate, there's no ambiguity.
if len(resolved.PullCandidates) == 1 {
return resolved, nil
}

Expand Down
15 changes: 7 additions & 8 deletions pkg/shortnames/shortnames_test.go
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 2aaf057

Please sign in to comment.