Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test covering .dockerconfigjson secrets #1335

Merged
merged 3 commits into from
Jul 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions pkg/authn/kubernetes/keychain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package kubernetes
import (
"context"
"crypto/md5"
"encoding/base64"
"encoding/json"
"fmt"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -351,3 +353,124 @@ func repo(t *testing.T, repository string) authn.Resource {
}
return repo
}

// TestDockerConfigJSON tests using secrets using the .dockerconfigjson form,
// like you might get from running:
// kubectl create secret docker-registry secret -n ns --docker-server="fake.registry.io" --docker-username="foo" --docker-password="bar"
func TestDockerConfigJSON(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":{"fake.registry.io":{"username":%q,"password":%q,"auth":%q}}}`,
username, password,
base64.StdEncoding.EncodeToString([]byte(username+":"+password))),
),
},
}})
if err != nil {
t.Fatalf("NewFromPullSecrets() = %v", err)
}

reg, err := name.NewRegistry("fake.registry.io", name.WeakValidation)
if err != nil {
t.Errorf("NewRegistry() = %v", err)
}

auth, err := kc.Resolve(reg)
if err != nil {
t.Errorf("Resolve(%v) = %v", reg, 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)
}
}

func TestKubernetesAuth(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)
}
})
}
}
}