Skip to content

Commit

Permalink
Allow credential mapping from dockercfg for canonical ports
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Aug 23, 2017
1 parent 108739d commit c865c5a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
12 changes: 11 additions & 1 deletion pkg/image/importer/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (s *SecretCredentialStore) init() credentialprovider.DockerKeyring {

func basicCredentialsFromKeyring(keyring credentialprovider.DockerKeyring, target *url.URL) (string, string) {
// TODO: compare this logic to Docker authConfig in v2 configuration
value := target.Host + target.Path
value := target.String()

// Lookup(...) expects an image (not a URL path).
// The keyring strips /v1/ and /v2/ version prefixes,
Expand All @@ -184,6 +184,16 @@ func basicCredentialsFromKeyring(keyring credentialprovider.DockerKeyring, targe
glog.V(5).Infof("Being asked for %s, trying %s for legacy behavior", target, "docker.io")
return basicCredentialsFromKeyring(keyring, &url.URL{Host: "docker.io"})
}

// try removing the canonical ports for the given requests
if (strings.HasSuffix(target.Host, ":443") && target.Scheme == "https") ||
(strings.HasSuffix(target.Host, ":80") && target.Scheme == "http") {
host := strings.SplitN(target.Host, ":", 2)[0]
glog.V(5).Infof("Being asked for %s, trying %s without port", target, host)

return basicCredentialsFromKeyring(keyring, &url.URL{Scheme: target.Scheme, Host: host, Path: target.Path})
}

glog.V(5).Infof("Unable to find a secret to match %s (%s)", target, value)
return "", ""
}
Expand Down
50 changes: 48 additions & 2 deletions pkg/image/importer/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"reflect"
"testing"

_ "github.com/openshift/origin/pkg/api/install"
"k8s.io/apimachinery/pkg/runtime"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/credentialprovider"

_ "github.com/openshift/origin/pkg/api/install"
)

func TestCredentialsForSecrets(t *testing.T) {
Expand Down Expand Up @@ -59,3 +58,50 @@ func TestBasicCredentials(t *testing.T) {
t.Fatalf("unexpected response: %s %s", u, p)
}
}

func Test_basicCredentialsFromKeyring(t *testing.T) {
fn := func(host string, entry credentialprovider.DockerConfigEntry) credentialprovider.DockerKeyring {
k := &credentialprovider.BasicDockerKeyring{}
k.Add(map[string]credentialprovider.DockerConfigEntry{host: entry})
return k
}
def := credentialprovider.DockerConfigEntry{
Username: "local_user",
Password: "local_pass",
}
type args struct {
keyring credentialprovider.DockerKeyring
target *url.URL
}
tests := []struct {
name string
args args
user string
password string
}{
// TODO: Add test cases.
{name: "exact", args: args{keyring: fn("localhost", def), target: &url.URL{Host: "localhost"}}, user: def.Username, password: def.Password},
{name: "http scheme", args: args{keyring: fn("localhost", def), target: &url.URL{Scheme: "http", Host: "localhost"}}, user: def.Username, password: def.Password},
{name: "https scheme", args: args{keyring: fn("localhost", def), target: &url.URL{Scheme: "https", Host: "localhost"}}, user: def.Username, password: def.Password},
{name: "canonical https", args: args{keyring: fn("localhost", def), target: &url.URL{Scheme: "https", Host: "localhost:443"}}, user: def.Username, password: def.Password},
{name: "canonical http", args: args{keyring: fn("localhost", def), target: &url.URL{Scheme: "http", Host: "localhost:80"}}, user: def.Username, password: def.Password},
{name: "only https", args: args{keyring: fn("https://localhost", def), target: &url.URL{Host: "localhost"}}, user: def.Username, password: def.Password},
{name: "only https scheme", args: args{keyring: fn("https://localhost", def), target: &url.URL{Scheme: "https", Host: "localhost"}}, user: def.Username, password: def.Password},

{name: "https not canonical", args: args{keyring: fn("localhost", def), target: &url.URL{Scheme: "https", Host: "localhost:80"}}, user: "", password: ""},
{name: "http not canonical", args: args{keyring: fn("localhost", def), target: &url.URL{Scheme: "http", Host: "localhost:443"}}, user: "", password: ""},
{name: "mismatched scheme", args: args{keyring: fn("https://localhost", def), target: &url.URL{Scheme: "http", Host: "localhost"}}, user: "", password: ""},
{name: "mismatched scheme - http", args: args{keyring: fn("http://localhost", def), target: &url.URL{Scheme: "https", Host: "localhost"}}, user: "", password: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
user, password := basicCredentialsFromKeyring(tt.args.keyring, tt.args.target)
if user != tt.user {
t.Errorf("basicCredentialsFromKeyring() user = %v, user %v", user, tt.user)
}
if password != tt.password {
t.Errorf("basicCredentialsFromKeyring() password = %v, user %v", password, tt.password)
}
})
}
}

0 comments on commit c865c5a

Please sign in to comment.