-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
registry: repo URL and dockerconfig URL mismatch
Registry login option should verify that the obtained dockerconfig credentials are for the same host. When the helmrepo URL and the URL in docker auth config don't match, the docker config store returns an empty auth config, instead of failing. This results in accepting empty username and password. The HelmRepo would appear to be ready in such situation because the creds are empty, no login is attempted. But when a HelmChart tries to use the login options, it'd fail. Signed-off-by: Sunny <darkowlzz@protonmail.com>
- Loading branch information
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
Copyright 2022 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package registry | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestLoginOptionFromSecret(t *testing.T) { | ||
testURL := "oci://registry.example.com/foo/bar" | ||
testUser := "flux" | ||
testPassword := "somepassword" | ||
testDockerconfigjson := `{"auths":{"registry.example.com":{"username":"flux","password":"somepassword","auth":"Zmx1eDpzb21lcGFzc3dvcmQ="}}}` | ||
dockerconfigjsonKey := ".dockerconfigjson" | ||
|
||
tests := []struct { | ||
name string | ||
url string | ||
secretType corev1.SecretType | ||
secretData map[string][]byte | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "generic secret", | ||
url: testURL, | ||
secretType: corev1.SecretTypeOpaque, | ||
secretData: map[string][]byte{ | ||
"username": []byte(testUser), | ||
"password": []byte(testPassword), | ||
}, | ||
}, | ||
{ | ||
name: "generic secret without username", | ||
url: testURL, | ||
secretType: corev1.SecretTypeOpaque, | ||
secretData: map[string][]byte{ | ||
"password": []byte(testPassword), | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "generic secret without password", | ||
url: testURL, | ||
secretType: corev1.SecretTypeOpaque, | ||
secretData: map[string][]byte{ | ||
"username": []byte(testUser), | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "generic secret without username and password", | ||
url: testURL, | ||
}, | ||
{ | ||
name: "docker-registry secret", | ||
url: testURL, | ||
secretType: corev1.SecretTypeDockerConfigJson, | ||
secretData: map[string][]byte{ | ||
dockerconfigjsonKey: []byte(testDockerconfigjson), | ||
}, | ||
}, | ||
{ | ||
name: "docker-registry secret host mismatch", | ||
url: "oci://registry.gitlab.com", | ||
secretType: corev1.SecretTypeDockerConfigJson, | ||
secretData: map[string][]byte{ | ||
".dockerconfigjson": []byte(testDockerconfigjson), | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "docker-registry secret invalid host", | ||
url: "oci://registry .gitlab.com", | ||
secretType: corev1.SecretTypeDockerConfigJson, | ||
secretData: map[string][]byte{ | ||
".dockerconfigjson": []byte(testDockerconfigjson), | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "docker-registry secret invalid docker config", | ||
url: testURL, | ||
secretType: corev1.SecretTypeDockerConfigJson, | ||
secretData: map[string][]byte{ | ||
".dockerconfigjson": []byte("foo"), | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
secret := corev1.Secret{} | ||
secret.Name = "test-secret" | ||
secret.Data = tt.secretData | ||
secret.Type = tt.secretType | ||
|
||
_, err := LoginOptionFromSecret(tt.url, secret) | ||
g.Expect(err != nil).To(Equal(tt.wantErr)) | ||
}) | ||
} | ||
} |