-
Notifications
You must be signed in to change notification settings - Fork 162
test: refactor pull secret helper functions and update go-doc #6188
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,107 @@ | ||
| // Copyright 2025 Microsoft Corporation | ||
| // | ||
| // 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 framework | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| // RegistryAuth represents authentication credentials for a single container | ||
| // image registry. It models one entry inside the "auths" map of a | ||
| // kubernetes.io/dockerconfigjson Secret. The Auth field is a base64 encoding | ||
| // of "username:password"; Username and Email are optional metadata. | ||
| // | ||
| // See https://kubernetes.io/docs/concepts/configuration/secret/#docker-config-secrets | ||
| type RegistryAuth struct { | ||
| Username string `json:"username,omitempty"` | ||
| Email string `json:"email,omitempty"` | ||
| Auth string `json:"auth"` | ||
| } | ||
|
|
||
| // DockerConfigJSON is the root structure stored under the .dockerconfigjson | ||
| // key of a kubernetes.io/dockerconfigjson Secret. Auths maps registry | ||
| // hostnames (e.g. "quay.io", "registry.redhat.io") to their credentials. | ||
| // | ||
| // See https://kubernetes.io/docs/concepts/configuration/secret/#docker-config-secrets | ||
| type DockerConfigJSON struct { | ||
| Auths map[string]RegistryAuth `json:"auths"` | ||
| } | ||
|
|
||
| // CreateTestDockerConfigSecret builds a corev1.Secret of type | ||
| // kubernetes.io/dockerconfigjson containing credentials for a single registry. | ||
| // It returns both the Secret and the RegistryAuth it constructed, so callers | ||
| // can pass the auth data directly to verifiers without recomputing it. | ||
| // The returned Secret is suitable for use as the HCCO "additional-pull-secret" | ||
| // in kube-system, which HCCO merges into the cluster's global pull secret. | ||
| // | ||
| // See https://hypershift.pages.dev/how-to/aws/global-pull-secret/ | ||
| func CreateTestDockerConfigSecret(host, username, password, email, secretName, namespace string) (*corev1.Secret, RegistryAuth, error) { | ||
| registryAuth := RegistryAuth{ | ||
| Email: email, | ||
| Auth: base64.StdEncoding.EncodeToString([]byte(username + ":" + password)), | ||
| } | ||
|
|
||
| dockerConfig := DockerConfigJSON{ | ||
| Auths: map[string]RegistryAuth{ | ||
| host: registryAuth, | ||
| }, | ||
| } | ||
|
|
||
| dockerConfigJSON, err := json.Marshal(dockerConfig) | ||
| if err != nil { | ||
| return nil, RegistryAuth{}, fmt.Errorf("failed to marshal docker config: %w", err) | ||
| } | ||
|
|
||
| return &corev1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: secretName, | ||
| Namespace: namespace, | ||
| }, | ||
| Type: corev1.SecretTypeDockerConfigJson, | ||
| Data: map[string][]byte{ | ||
| corev1.DockerConfigJsonKey: dockerConfigJSON, | ||
| }, | ||
| }, registryAuth, nil | ||
| } | ||
|
|
||
| // AddRegistryAuthToSecret adds or replaces a registry entry in an existing | ||
| // dockerconfigjson Secret. It unmarshals the Secret's current .dockerconfigjson | ||
| // data, inserts (or overwrites) the entry for host, and marshals the result | ||
| // back into the Secret's Data field. The caller is responsible for applying the | ||
| // updated Secret to the cluster (e.g. via a Kubernetes Update call). | ||
| func AddRegistryAuthToSecret(secret *corev1.Secret, host string, registryAuth RegistryAuth) error { | ||
| var config DockerConfigJSON | ||
| if err := json.Unmarshal(secret.Data[corev1.DockerConfigJsonKey], &config); err != nil { | ||
| return fmt.Errorf("failed to unmarshal pull secret: %w", err) | ||
| } | ||
|
|
||
| if config.Auths == nil { | ||
| config.Auths = make(map[string]RegistryAuth) | ||
| } | ||
| config.Auths[host] = registryAuth | ||
|
|
||
| updated, err := json.Marshal(config) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal updated docker config: %w", err) | ||
| } | ||
|
|
||
| secret.Data[corev1.DockerConfigJsonKey] = updated | ||
| return nil | ||
| } | ||
This file contains hidden or 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,134 @@ | ||
| // Copyright 2026 Microsoft Corporation | ||
| // | ||
| // 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 framework | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| func TestCreateTestDockerConfigSecret(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| secret, registryAuth, err := CreateTestDockerConfigSecret( | ||
| "registry.example.com", | ||
| "user", | ||
| "pass", | ||
| "user@example.com", | ||
| "my-secret", | ||
| "my-namespace", | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, "my-secret", secret.Name) | ||
| assert.Equal(t, "my-namespace", secret.Namespace) | ||
| assert.Equal(t, corev1.SecretTypeDockerConfigJson, secret.Type) | ||
|
|
||
| expectedAuth := base64.StdEncoding.EncodeToString([]byte("user:pass")) | ||
| assert.Equal(t, expectedAuth, registryAuth.Auth) | ||
| assert.Equal(t, "user@example.com", registryAuth.Email) | ||
|
|
||
| var config DockerConfigJSON | ||
| require.NoError(t, json.Unmarshal(secret.Data[corev1.DockerConfigJsonKey], &config)) | ||
|
|
||
| hostAuth, exists := config.Auths["registry.example.com"] | ||
| assert.True(t, exists, "expected registry.example.com in auths") | ||
| assert.Equal(t, registryAuth, hostAuth, "returned RegistryAuth must match what is in the Secret") | ||
| } | ||
|
|
||
| func TestAddRegistryAuthToSecret(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| secret, originalAuth, err := CreateTestDockerConfigSecret( | ||
| "original.example.com", | ||
| "user1", | ||
| "pass1", | ||
| "user1@example.com", | ||
| "test-secret", | ||
| "default", | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| newAuth := RegistryAuth{ | ||
| Auth: base64.StdEncoding.EncodeToString([]byte("user2:pass2")), | ||
| Email: "user2@example.com", | ||
| } | ||
| err = AddRegistryAuthToSecret(secret, "new.example.com", newAuth) | ||
| require.NoError(t, err) | ||
|
|
||
| var config DockerConfigJSON | ||
| require.NoError(t, json.Unmarshal(secret.Data[corev1.DockerConfigJsonKey], &config)) | ||
|
|
||
| assert.Contains(t, config.Auths, "original.example.com", "original entry must be preserved") | ||
| assert.Equal(t, originalAuth, config.Auths["original.example.com"]) | ||
| assert.Contains(t, config.Auths, "new.example.com", "new entry must be present") | ||
| assert.Equal(t, newAuth, config.Auths["new.example.com"]) | ||
| } | ||
|
|
||
| func TestAddRegistryAuthToSecret_OverwritesExisting(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| secret, _, err := CreateTestDockerConfigSecret( | ||
| "registry.example.com", | ||
| "user", | ||
| "pass", | ||
| "old@example.com", | ||
| "test-secret", | ||
| "default", | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| updatedAuth := RegistryAuth{ | ||
| Auth: base64.StdEncoding.EncodeToString([]byte("newuser:newpass")), | ||
| Email: "new@example.com", | ||
| } | ||
| err = AddRegistryAuthToSecret(secret, "registry.example.com", updatedAuth) | ||
| require.NoError(t, err) | ||
|
|
||
| var config DockerConfigJSON | ||
| require.NoError(t, json.Unmarshal(secret.Data[corev1.DockerConfigJsonKey], &config)) | ||
|
|
||
| assert.Len(t, config.Auths, 1) | ||
| assert.Equal(t, updatedAuth, config.Auths["registry.example.com"]) | ||
| } | ||
|
|
||
| func TestAddRegistryAuthToSecret_NilAuth(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| secret := &corev1.Secret{ | ||
| Data: map[string][]byte{ | ||
| corev1.DockerConfigJsonKey: []byte(`{}`), | ||
| }, | ||
| } | ||
|
|
||
| newAuth := RegistryAuth{ | ||
| Auth: base64.StdEncoding.EncodeToString([]byte("user:pass")), | ||
| Email: "user@example.com", | ||
| } | ||
| err := AddRegistryAuthToSecret(secret, "registry.example.com", newAuth) | ||
| require.NoError(t, err) | ||
|
|
||
| var config DockerConfigJSON | ||
| require.NoError(t, json.Unmarshal(secret.Data[corev1.DockerConfigJsonKey], &config)) | ||
|
|
||
| assert.Len(t, config.Auths, 1) | ||
| assert.Equal(t, newAuth, config.Auths["registry.example.com"]) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.