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

Commit

Permalink
feat: expose registry name mapping methods (#76)
Browse files Browse the repository at this point in the history
Resolves #72

Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 authored Jun 19, 2023
1 parent bf5244c commit 6d14c42
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 13 additions & 11 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Login(ctx context.Context, store Store, reg *remote.Registry, cred auth.Cre
if err := regClone.Ping(ctx); err != nil {
return fmt.Errorf("failed to validate the credential for %s: %w", regClone.Reference.Registry, err)
}
hostname := mapStoreRegistryName(regClone.Reference.Registry)
hostname := ServerAddressFromRegistry(regClone.Reference.Registry)
if err := store.Put(ctx, hostname, cred); err != nil {
return fmt.Errorf("failed to store the credential for %s: %w", hostname, err)
}
Expand All @@ -61,7 +61,7 @@ func Login(ctx context.Context, store Store, reg *remote.Registry, cred auth.Cre

// Logout provides the logout functionality given the registry name.
func Logout(ctx context.Context, store Store, registryName string) error {
registryName = mapStoreRegistryName(registryName)
registryName = ServerAddressFromRegistry(registryName)
if err := store.Delete(ctx, registryName); err != nil {
return fmt.Errorf("failed to delete the credential for %s: %w", registryName, err)
}
Expand All @@ -71,28 +71,30 @@ func Logout(ctx context.Context, store Store, registryName string) error {
// Credential returns a Credential() function that can be used by auth.Client.
func Credential(store Store) func(context.Context, string) (auth.Credential, error) {
return func(ctx context.Context, reg string) (auth.Credential, error) {
reg = mapAuthenticationRegistryName(reg)
reg = ServerAddressFromHostname(reg)
if reg == "" {
return auth.EmptyCredential, nil
}
return store.Get(ctx, reg)
}
}

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

func mapAuthenticationRegistryName(hostname string) string {
// It is expected that the traffic targetting "registry-1.docker.io"
// will be redirected to "https://index.docker.io/v1/"
// See: https://github.com/moby/moby/blob/v24.0.0-beta.2/registry/config.go#L25-L48
// ServerAddressFromHostname maps a hostname to a server address, which is used as
// a key for credentials store. It is expected that the traffic targetting the
// host "registry-1.docker.io" will be redirected to "https://index.docker.io/v1/".
// See: https://github.com/moby/moby/blob/v24.0.2/registry/config.go#L25-L48
func ServerAddressFromHostname(hostname string) string {
if hostname == "registry-1.docker.io" {
return "https://index.docker.io/v1/"
}
Expand Down
2 changes: 1 addition & 1 deletion registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func Test_mapHostname(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := mapStoreRegistryName(tt.host); got != tt.want {
if got := ServerAddressFromRegistry(tt.host); got != tt.want {
t.Errorf("mapHostname() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 6d14c42

Please sign in to comment.