Skip to content

Commit

Permalink
WIP: add failing unit test based on Dave's findings
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh committed Jul 13, 2022
1 parent 8288e21 commit 8dbf92d
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions pkg/authn/kubernetes/keychain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,79 @@ func TestDockerConfigJSON(t *testing.T) {
t.Errorf("Resolve() = %v, want %v", got, want)
}
}

func TestDave(t *testing.T) {
// From https://github.com/knative/serving/issues/12761#issuecomment-1097441770
// All of these should work with K8s' docker auth parsing.
for k, ss := range map[string][]string{
"registry.gitlab.com/dprotaso/test/nginx": []string{
"registry.gitlab.com",
"http://registry.gitlab.com",
"https://registry.gitlab.com",
"registry.gitlab.com/dprotaso",
"http://registry.gitlab.com/dprotaso",
"https://registry.gitlab.com/dprotaso",
"registry.gitlab.com/dprotaso/test",
"http://registry.gitlab.com/dprotaso/test",
"https://registry.gitlab.com/dprotaso/test",
"registry.gitlab.com/dprotaso/test/nginx",
"http://registry.gitlab.com/dprotaso/test/nginx",
"https://registry.gitlab.com/dprotaso/test/nginx",
},
"dtestcontainer.azurecr.io/dave/nginx": []string{
"dtestcontainer.azurecr.io",
"http://dtestcontainer.azurecr.io",
"https://dtestcontainer.azurecr.io",
"dtestcontainer.azurecr.io/dave",
"http://dtestcontainer.azurecr.io/dave",
"https://dtestcontainer.azurecr.io/dave",
"dtestcontainer.azurecr.io/dave/nginx",
"http://dtestcontainer.azurecr.io/dave/nginx",
"https://dtestcontainer.azurecr.io/dave/nginx",
}} {
repo, err := name.NewRepository(k)
if err != nil {
t.Errorf("parsing %q: %v", k, err)
continue
}

for _, s := range ss {
t.Run(fmt.Sprintf("%s - %s", k, s), func(t *testing.T) {
username, password := "foo", "bar"
kc, err := NewFromPullSecrets(context.Background(), []corev1.Secret{{
ObjectMeta: metav1.ObjectMeta{
Name: "secret",
Namespace: "ns",
},
Type: corev1.SecretTypeDockerConfigJson,
Data: map[string][]byte{
corev1.DockerConfigJsonKey: []byte(
fmt.Sprintf(`{"auths":{%q:{"username":%q,"password":%q,"auth":%q}}}`,
s,
username, password,
base64.StdEncoding.EncodeToString([]byte(username+":"+password))),
),
},
}})
if err != nil {
t.Fatalf("NewFromPullSecrets() = %v", err)
}
auth, err := kc.Resolve(repo)
if err != nil {
t.Errorf("Resolve(%v) = %v", repo, err)
}
got, err := auth.Authorization()
if err != nil {
t.Errorf("Authorization() = %v", err)
}
want, err := (&authn.Basic{Username: username, Password: password}).Authorization()
if err != nil {
t.Errorf("Authorization() = %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Resolve() = %v, want %v", got, want)
}
})
}
}
}

0 comments on commit 8dbf92d

Please sign in to comment.