From b79d820562eaddb8532b840021dab3d869930424 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Thu, 14 Mar 2024 15:39:53 -0700 Subject: [PATCH] chore: Update docker auth API (#892) --- internal/cmd/docker/push.go | 16 +--------------- internal/http/imagerunner.go | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/internal/cmd/docker/push.go b/internal/cmd/docker/push.go index 562ca942f..d41f8f379 100644 --- a/internal/cmd/docker/push.go +++ b/internal/cmd/docker/push.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "os" - "strings" "time" "github.com/docker/docker/api/types" @@ -50,11 +49,7 @@ func PushCommand() *cobra.Command { }, RunE: func(cmd *cobra.Command, args []string) error { image := args[0] - repo, err := extractRepo(image) - if err != nil { - return err - } - auth, err := imageRunnerService.RegistryLogin(context.Background(), repo) + auth, err := imageRunnerService.RegistryLogin(context.Background(), image) if err != nil { return fmt.Errorf("failed to fetch auth token: %v", err) } @@ -109,12 +104,3 @@ func pushDockerImage(imageName, username, password string, timeout time.Duration return nil } - -func extractRepo(input string) (string, error) { - // Example: us-west4-docker.pkg.dev/sauce-hto-p-jy6b/sauce-devx-team-sauce/ubuntu:experiment - items := strings.Split(input, "/") - if len(items) >= 3 { - return items[2], nil - } - return "", fmt.Errorf("unable to extract repo name from docker image") -} diff --git a/internal/http/imagerunner.go b/internal/http/imagerunner.go index 4564b36a7..94690b82b 100644 --- a/internal/http/imagerunner.go +++ b/internal/http/imagerunner.go @@ -36,6 +36,10 @@ type AuthToken struct { Password string `json:"password"` } +type AuthRequest struct { + RegistryURL string `json:"registry_url"` +} + type ContainersResp struct { Items []imagerunner.Runner `json:"content"` } @@ -436,21 +440,21 @@ func (c *ImageRunner) newServerError(status int, short string, body []byte) erro } func (c *ImageRunner) RegistryLogin(ctx context.Context, repo string) (AuthToken, error) { - url := fmt.Sprintf("%s/v1alpha1/hosted/container-registry/%s/authorization-token", c.URL, repo) + url := fmt.Sprintf("%s/v1alpha1/hosted/container-registry/authorization-token", c.URL) var authToken AuthToken - req, err := NewRequestWithContext(ctx, http.MethodPost, url, nil) - if err != nil { - return authToken, err + var b bytes.Buffer + if err := json.NewEncoder(&b).Encode(AuthRequest{RegistryURL: repo}); err != nil { + return AuthToken{}, err } - req.SetBasicAuth(c.Creds.Username, c.Creds.AccessKey) - - r, err := retryablehttp.FromRequest(req) + req, err := NewRetryableRequestWithContext(ctx, http.MethodPost, url, &b) if err != nil { return authToken, err } + req.SetBasicAuth(c.Creds.Username, c.Creds.AccessKey) + req.Header.Set("Content-Type", "application/json") - resp, err := c.Client.Do(r) + resp, err := c.Client.Do(req) if err != nil { return authToken, err }