-
Notifications
You must be signed in to change notification settings - Fork 7
feat: support credential function #45
Changes from 8 commits
3c54665
a07546c
e9bc229
72b2cf1
518f6ca
9becb4c
0897940
c97288d
2a558c2
4a7bf46
086f9b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ func Login(ctx context.Context, store Store, reg *remote.Registry, cred auth.Cre | |
if err := regClone.Ping(ctx); err != nil { | ||
return fmt.Errorf("unable to ping the registry %s: %w", regClone.Reference.Registry, err) | ||
} | ||
hostname := mapHostname(regClone.Reference.Registry) | ||
hostname := mapStoreRegistryName(regClone.Reference.Registry) | ||
if err := store.Put(ctx, hostname, cred); err != nil { | ||
return fmt.Errorf("unable to store the credential for %s: %w", hostname, err) | ||
} | ||
|
@@ -57,18 +57,38 @@ 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 = mapHostname(registryName) | ||
registryName = mapStoreRegistryName(registryName) | ||
if err := store.Delete(ctx, registryName); err != nil { | ||
return fmt.Errorf("unable to delete the credential for %s: %w", registryName, err) | ||
} | ||
return nil | ||
} | ||
|
||
func mapHostname(hostname string) string { | ||
// The Docker CLI expects that the 'docker.io' credential | ||
// will be added under the key "https://index.docker.io/v1/" | ||
if hostname == "docker.io" { | ||
// 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) | ||
if reg == "" { | ||
return auth.EmptyCredential, nil | ||
} | ||
return store.Get(ctx, reg) | ||
} | ||
} | ||
|
||
// The Docker CLI expects that the 'docker.io' credential | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documentation starts with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have any reference so that other contributors can understand? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are private functions and don't need documentation. I moved the comments inside the function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added reference code link. |
||
// will be added under the key "https://index.docker.io/v1/" | ||
func mapStoreRegistryName(registry string) string { | ||
if registry == "docker.io" { | ||
return "https://index.docker.io/v1/" | ||
} | ||
return registry | ||
} | ||
|
||
// It is expected that the traffic targetting "registry-1.docker.io" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documentation starts with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have any reference so that other contributors can understand? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are private functions and don't need documentation. I moved the comments inside the function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added reference code link. |
||
// will be redirected to "https://index.docker.io/v1/" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or we don't need to make it a function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a function here makes the code clean. I changed the name to |
||
func mapAuthenticationRegistryName(target string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed. |
||
if target == "registry-1.docker.io" { | ||
return "https://index.docker.io/v1/" | ||
} | ||
return hostname | ||
return target | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Wondering if we should move this comment inside. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved them inside.