Skip to content
This repository was archived by the owner on Apr 7, 2024. It is now read-only.

Commit 6d14c42

Browse files
feat: expose registry name mapping methods (#76)
Resolves #72 Signed-off-by: Xiaoxuan Wang <wangxiaoxuan119@gmail.com>
1 parent bf5244c commit 6d14c42

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

registry.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func Login(ctx context.Context, store Store, reg *remote.Registry, cred auth.Cre
5252
if err := regClone.Ping(ctx); err != nil {
5353
return fmt.Errorf("failed to validate the credential for %s: %w", regClone.Reference.Registry, err)
5454
}
55-
hostname := mapStoreRegistryName(regClone.Reference.Registry)
55+
hostname := ServerAddressFromRegistry(regClone.Reference.Registry)
5656
if err := store.Put(ctx, hostname, cred); err != nil {
5757
return fmt.Errorf("failed to store the credential for %s: %w", hostname, err)
5858
}
@@ -61,7 +61,7 @@ func Login(ctx context.Context, store Store, reg *remote.Registry, cred auth.Cre
6161

6262
// Logout provides the logout functionality given the registry name.
6363
func Logout(ctx context.Context, store Store, registryName string) error {
64-
registryName = mapStoreRegistryName(registryName)
64+
registryName = ServerAddressFromRegistry(registryName)
6565
if err := store.Delete(ctx, registryName); err != nil {
6666
return fmt.Errorf("failed to delete the credential for %s: %w", registryName, err)
6767
}
@@ -71,28 +71,30 @@ func Logout(ctx context.Context, store Store, registryName string) error {
7171
// Credential returns a Credential() function that can be used by auth.Client.
7272
func Credential(store Store) func(context.Context, string) (auth.Credential, error) {
7373
return func(ctx context.Context, reg string) (auth.Credential, error) {
74-
reg = mapAuthenticationRegistryName(reg)
74+
reg = ServerAddressFromHostname(reg)
7575
if reg == "" {
7676
return auth.EmptyCredential, nil
7777
}
7878
return store.Get(ctx, reg)
7979
}
8080
}
8181

82-
func mapStoreRegistryName(registry string) string {
83-
// The Docker CLI expects that the 'docker.io' credential
84-
// will be added under the key "https://index.docker.io/v1/"
85-
// See: https://github.com/moby/moby/blob/v24.0.0-beta.2/registry/config.go#L25-L48
82+
// ServerAddressFromRegistry maps a registry to a server address, which is used as
83+
// a key for credentials store. The Docker CLI expects that the credentials of
84+
// the registry 'docker.io' will be added under the key "https://index.docker.io/v1/".
85+
// See: https://github.com/moby/moby/blob/v24.0.2/registry/config.go#L25-L48
86+
func ServerAddressFromRegistry(registry string) string {
8687
if registry == "docker.io" {
8788
return "https://index.docker.io/v1/"
8889
}
8990
return registry
9091
}
9192

92-
func mapAuthenticationRegistryName(hostname string) string {
93-
// It is expected that the traffic targetting "registry-1.docker.io"
94-
// will be redirected to "https://index.docker.io/v1/"
95-
// See: https://github.com/moby/moby/blob/v24.0.0-beta.2/registry/config.go#L25-L48
93+
// ServerAddressFromHostname maps a hostname to a server address, which is used as
94+
// a key for credentials store. It is expected that the traffic targetting the
95+
// host "registry-1.docker.io" will be redirected to "https://index.docker.io/v1/".
96+
// See: https://github.com/moby/moby/blob/v24.0.2/registry/config.go#L25-L48
97+
func ServerAddressFromHostname(hostname string) string {
9698
if hostname == "registry-1.docker.io" {
9799
return "https://index.docker.io/v1/"
98100
}

registry_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func Test_mapHostname(t *testing.T) {
190190
}
191191
for _, tt := range tests {
192192
t.Run(tt.name, func(t *testing.T) {
193-
if got := mapStoreRegistryName(tt.host); got != tt.want {
193+
if got := ServerAddressFromRegistry(tt.host); got != tt.want {
194194
t.Errorf("mapHostname() = %v, want %v", got, tt.want)
195195
}
196196
})

0 commit comments

Comments
 (0)