-
-
Notifications
You must be signed in to change notification settings - Fork 504
feat: enable authenticated pulls #1580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -27,10 +27,15 @@ import ( | |||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||
| "io" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| "github.com/distribution/reference" | ||||||||||||||||||||||
| dockerconfig "github.com/docker/cli/cli/config" | ||||||||||||||||||||||
| dockerconfigfile "github.com/docker/cli/cli/config/configfile" | ||||||||||||||||||||||
| dockerconfigtypes "github.com/docker/cli/cli/config/types" | ||||||||||||||||||||||
| "github.com/docker/docker/api/types" | ||||||||||||||||||||||
| "github.com/docker/docker/api/types/container" | ||||||||||||||||||||||
| "github.com/docker/docker/api/types/filters" | ||||||||||||||||||||||
| dockerimage "github.com/docker/docker/api/types/image" | ||||||||||||||||||||||
| registrytypes "github.com/docker/docker/api/types/registry" | ||||||||||||||||||||||
| "github.com/docker/docker/client" | ||||||||||||||||||||||
| l "github.com/k3d-io/k3d/v5/pkg/logger" | ||||||||||||||||||||||
| k3d "github.com/k3d-io/k3d/v5/pkg/types" | ||||||||||||||||||||||
|
|
@@ -104,9 +109,41 @@ func removeContainer(ctx context.Context, ID string) error { | |||||||||||||||||||||
| return nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // resolveAuth gets registry authentication configuration for an image | ||||||||||||||||||||||
| func resolveAuth(image string) (authConfig registrytypes.AuthConfig, err error) { | ||||||||||||||||||||||
| var ref reference.Named | ||||||||||||||||||||||
| var config *dockerconfigfile.ConfigFile | ||||||||||||||||||||||
| var dockerAuthConfig dockerconfigtypes.AuthConfig | ||||||||||||||||||||||
| if ref, err = reference.ParseNormalizedNamed(image); err != nil { | ||||||||||||||||||||||
| return | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| authKey := reference.Domain(ref) | ||||||||||||||||||||||
| if authKey == "docker.io" || authKey == "index.docker.io" { | ||||||||||||||||||||||
| authKey = "https://index.docker.io/v1/" | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if config, err = dockerconfig.Load(dockerconfig.Dir()); err != nil { | ||||||||||||||||||||||
| return | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if dockerAuthConfig, err = config.GetAuthConfig(authKey); err != nil { | ||||||||||||||||||||||
| return | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| authConfig = registrytypes.AuthConfig(dockerAuthConfig) | ||||||||||||||||||||||
| return | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // pullImage pulls a container image and outputs progress if --verbose flag is set | ||||||||||||||||||||||
| func pullImage(ctx context.Context, docker client.APIClient, image string) error { | ||||||||||||||||||||||
| resp, err := docker.ImagePull(ctx, image, dockerimage.PullOptions{}) | ||||||||||||||||||||||
| authConfig, err := resolveAuth(image) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| l.Log().Warnf("Failed to get auth: %v", err) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| encoded, err := registrytypes.EncodeAuthConfig(authConfig) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| l.Log().Warnf("Failed to encode auth: %v", err) | ||||||||||||||||||||||
|
Comment on lines
+138
to
+142
|
||||||||||||||||||||||
| l.Log().Warnf("Failed to get auth: %v", err) | |
| } | |
| encoded, err := registrytypes.EncodeAuthConfig(authConfig) | |
| if err != nil { | |
| l.Log().Warnf("Failed to encode auth: %v", err) | |
| return fmt.Errorf("failed to get auth: %w", err) | |
| } | |
| encoded, err := registrytypes.EncodeAuthConfig(authConfig) | |
| if err != nil { | |
| return fmt.Errorf("failed to encode auth: %w", err) |
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.
[nitpick] The hardcoded registry URL transformation should be extracted into a constant or helper function for better maintainability. Consider defining
const DockerHubAuthKey = \"https://index.docker.io/v1/\"at the package level.