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

Commit

Permalink
resolved comments, added doc and rearranged existing comments
Browse files Browse the repository at this point in the history
Signed-off-by: root <[email protected]>
  • Loading branch information
root authored and root committed Jun 19, 2023
1 parent 7e7e54f commit 4e86073
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 := MapRegistryNameDockerIo(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 = MapRegistryNameDockerIo(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 = MapRegistryNameRegistry1DockerIo(reg)
reg = ServerAddressFromHostname(reg)
if reg == "" {
return auth.EmptyCredential, nil
}
return store.Get(ctx, reg)
}
}

func MapRegistryNameDockerIo(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.0-beta.2/registry/config.go#L25-L48
func ServerAddressFromRegistry(registry string) string {
if registry == "docker.io" {
return "https://index.docker.io/v1/"
}
return registry
}

func MapRegistryNameRegistry1DockerIo(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.0-beta.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 := MapRegistryNameDockerIo(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 4e86073

Please sign in to comment.