Skip to content

Commit

Permalink
Fix pull authorization in a ninja way
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanilves committed Oct 8, 2017
1 parent 0cc7eb0 commit c4905f3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/base64"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -124,6 +125,16 @@ func getAuthorization(t auth.TokenResponse) string {
return t.Method() + " " + t.Token()
}

func getPullAuth(username, password string) string {
if username == "" && password == "" {
return ""
}

jsonString := fmt.Sprintf("{ \"username\": \"%s\", \"password\": \"%s\" }", username, password)

return base64.StdEncoding.EncodeToString([]byte(jsonString))
}

func main() {
o := options{}

Expand All @@ -132,7 +143,7 @@ func main() {
os.Exit(1)
}
if o.Version {
println(getVersion())
fmt.Printf("VERSION: %s", getVersion())
os.Exit(0)
}
if len(o.Positional.Repositories) == 0 {
Expand All @@ -152,6 +163,8 @@ func main() {
repoCount := len(o.Positional.Repositories)
pullCount := 0

pullAuths := make(map[string]string)

type tagResult struct {
Tags []*tag.Tag
Repo string
Expand All @@ -176,6 +189,8 @@ func main() {
suicide(err)
}

pullAuths[repoLocalName] = getPullAuth(username, password)

tresp, err := auth.NewToken(registryName, repoRegistryName, username, password)
if err != nil {
suicide(err)
Expand Down Expand Up @@ -248,7 +263,7 @@ func main() {
ref := repo + ":" + tg.GetName()

fmt.Printf("PULLING %s\n", ref)
err := local.Pull(ref)
err := local.Pull(ref, pullAuths[repo])
if err != nil {
suicide(err)
}
Expand Down
9 changes: 7 additions & 2 deletions tag/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,18 @@ func FormatRepoName(repository, registry string) string {
}

// Pull pulls Docker image specified locally
func Pull(ref string) error {
func Pull(ref, auth string) error {
cli, err := newClient()
if err != nil {
return err
}

resp, err := cli.ImagePull(context.Background(), ref, types.ImagePullOptions{})
pullOptions := types.ImagePullOptions{RegistryAuth: auth}
if auth == "" {
pullOptions = types.ImagePullOptions{}
}

resp, err := cli.ImagePull(context.Background(), ref, pullOptions)
if err != nil {
return err
}
Expand Down

0 comments on commit c4905f3

Please sign in to comment.