Skip to content

Commit

Permalink
Merge pull request #70 from ivanilves/omg-fix-auth-pull
Browse files Browse the repository at this point in the history
Fix authenticated pull requests
  • Loading branch information
ivanilves authored Oct 8, 2017
2 parents 134a2f5 + 510e126 commit 0b354d3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
38 changes: 13 additions & 25 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/docker/docker"
version = "1.13.1"
name = "github.com/moby/moby"
version = "v17.03.2-ce"

[[constraint]]
name = "github.com/jessevdk/go-flags"
Expand Down
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
11 changes: 8 additions & 3 deletions tag/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// This "Moby" thing does not work for me...
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/moby/moby/client"

"github.com/tv42/httpunix"
"golang.org/x/net/context"
Expand Down 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 0b354d3

Please sign in to comment.