Skip to content

Commit

Permalink
chore: Update docker auth API (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianfeng92 authored Mar 14, 2024
1 parent 4a29661 commit b79d820
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
16 changes: 1 addition & 15 deletions internal/cmd/docker/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"os"
"strings"
"time"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
20 changes: 12 additions & 8 deletions internal/http/imagerunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b79d820

Please sign in to comment.