1
0
mirror of https://github.com/containers/image.git synced 2025-04-18 19:44:05 +03:00

Return an iterator from authKeyLookupOrder

... so that we don't have to allocate a single-use array

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač 2024-08-23 06:03:46 +02:00
parent 2f17a838a6
commit b1d309cce2
2 changed files with 22 additions and 19 deletions

View File

@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io/fs"
"iter"
"maps"
"os"
"os/exec"
@ -818,8 +819,7 @@ func findCredentialsInFile(key, registry string, path authPath) (types.DockerAut
//
// Repo or namespace keys are only supported as exact matches. For registry
// keys we prefer exact matches as well.
keys := authKeyLookupOrder(key, registry, path.legacyFormat)
for _, key := range keys {
for key := range authKeyLookupOrder(key, registry, path.legacyFormat) {
if val, exists := fileContents.AuthConfigs[key]; exists {
return decodeDockerAuth(path.path, key, val)
}
@ -854,23 +854,25 @@ func findCredentialsInFile(key, registry string, path authPath) (types.DockerAut
// - quay.io/repo/ns
// - quay.io/repo
// - quay.io
func authKeyLookupOrder(key, registry string, legacyFormat bool) []string {
if legacyFormat {
return []string{registry}
}
var res []string
for {
res = append(res, key)
lastSlash := strings.LastIndex(key, "/")
if lastSlash == -1 {
break
func authKeyLookupOrder(key, registry string, legacyFormat bool) iter.Seq[string] {
return func(yield func(string) bool) {
if legacyFormat {
_ = yield(registry) // We stop in any case
return
}
key = key[:lastSlash]
}
return res
for {
if !yield(key) {
return
}
lastSlash := strings.LastIndex(key, "/")
if lastSlash == -1 {
break
}
key = key[:lastSlash]
}
}
}
// decodeDockerAuth decodes the username and password from conf,

View File

@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
@ -693,10 +694,10 @@ func TestAuthKeyLookupOrder(t *testing.T) {
} else {
registry = tc.input
}
result := authKeyLookupOrder(tc.input, registry, false)
result := slices.Collect(authKeyLookupOrder(tc.input, registry, false))
assert.Equal(t, tc.expected, result, tc.name)
result = authKeyLookupOrder(tc.input, registry, true)
result = slices.Collect(authKeyLookupOrder(tc.input, registry, true))
assert.Equal(t, []string{registry}, result, tc.name)
}
}