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

feat: add e2e test for docker auth with _json_key #132

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions e2e/deploy/vault/vault.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ spec:
data:
DOCKER_REPO_USER: dockerrepouser
DOCKER_REPO_PASSWORD: dockerrepopassword
DOCKER_REPO_JSON_KEY: |
_json_key: {
"type": "service_account",
"project_id": "test"
}
- type: kv
path: secret/data/mysql
data:
Expand Down
21 changes: 21 additions & 0 deletions e2e/test/secret-docker-json-key-vault.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: v1
kind: Secret
metadata:
name: test-secret-docker-json-key-vault
annotations:
secrets-webhook.security.bank-vaults.io/provider: "vault"
secrets-webhook.security.bank-vaults.io/vault-addr: "https://vault.default.svc.cluster.local:8200"
secrets-webhook.security.bank-vaults.io/vault-role: "default"
secrets-webhook.security.bank-vaults.io/vault-tls-secret: vault-tls
# secrets-webhook.security.bank-vaults.io/vault-skip-verify: "true"
secrets-webhook.security.bank-vaults.io/vault-path: "kubernetes"
type: kubernetes.io/dockerconfigjson
stringData:
.dockerconfigjson: |
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "dmF1bHQ6c2VjcmV0L2RhdGEvZG9ja2VycmVwbyNET0NLRVJfUkVQT19KU09OX0tFWQ=="
}
}
}
63 changes: 49 additions & 14 deletions e2e/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ import (
)

func TestSecretValueInjection(t *testing.T) {
type dockerAuth struct {
Username string `json:"username"`
Password string `json:"password"`
Auth string `json:"auth"`
}

type auths struct {
DockerAuth dockerAuth `json:"https://index.docker.io/v1/"`
}

type dockerconfig struct {
Auths auths `json:"auths"`
}
secretVault := applyResource(features.New("secret-vault"), "secret-vault.yaml").
Assess("object created", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
secrets := &v1.SecretList{
Expand All @@ -61,28 +74,50 @@ func TestSecretValueInjection(t *testing.T) {
err := cfg.Client().Resources(cfg.Namespace()).Get(ctx, "test-secret-vault", cfg.Namespace(), &secret)
require.NoError(t, err)

type v1 struct {
Username string `json:"username"`
Password string `json:"password"`
Auth string `json:"auth"`
}
var dockerconfigjson dockerconfig

type auths struct {
V1 v1 `json:"https://index.docker.io/v1/"`
}
err = json.Unmarshal(secret.Data[".dockerconfigjson"], &dockerconfigjson)
require.NoError(t, err)

dockerrepoauth := base64.StdEncoding.EncodeToString([]byte("dockerrepouser:dockerrepopassword"))
assert.Equal(t, "dockerrepouser", dockerconfigjson.Auths.DockerAuth.Username)
assert.Equal(t, "dockerrepopassword", dockerconfigjson.Auths.DockerAuth.Password)
assert.Equal(t, dockerrepoauth, dockerconfigjson.Auths.DockerAuth.Auth)
assert.Equal(t, "Inline: secretId AWS_ACCESS_KEY_ID", string(secret.Data["inline"]))

type dockerconfig struct {
Auths auths `json:"auths"`
return ctx
}).
Feature()

secretDockerJsonKey := applyResource(features.New("secret-docker-json-key-vault"), "secret-docker-json-key-vault.yaml").
Assess("object created", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
secrets := &v1.SecretList{
Items: []v1.Secret{
{
ObjectMeta: metav1.ObjectMeta{Name: "test-secret-docker-json-key-vault", Namespace: cfg.Namespace()},
},
},
}

// wait for the secret to become available
err := wait.For(conditions.New(cfg.Client().Resources()).ResourcesFound(secrets), wait.WithTimeout(defaultTimeout))
require.NoError(t, err)

return ctx
}).
Assess("secret values are injected", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
var secret v1.Secret

err := cfg.Client().Resources(cfg.Namespace()).Get(ctx, "test-secret-docker-json-key-vault", cfg.Namespace(), &secret)
require.NoError(t, err)

var dockerconfigjson dockerconfig

err = json.Unmarshal(secret.Data[".dockerconfigjson"], &dockerconfigjson)
require.NoError(t, err)

assert.Equal(t, "dockerrepouser", dockerconfigjson.Auths.V1.Username)
assert.Equal(t, "dockerrepopassword", dockerconfigjson.Auths.V1.Password)
assert.Equal(t, "Inline: secretId AWS_ACCESS_KEY_ID", string(secret.Data["inline"]))
dockerrepoauth := base64.StdEncoding.EncodeToString([]byte("_json_key: {\n \"type\": \"service_account\",\n \"project_id\": \"test\"\n}\n"))
assert.Equal(t, dockerrepoauth, dockerconfigjson.Auths.DockerAuth.Auth)

return ctx
}).
Expand Down Expand Up @@ -119,7 +154,7 @@ func TestSecretValueInjection(t *testing.T) {
}).
Feature()

testenv.Test(t, secretVault, configMapVault)
testenv.Test(t, secretVault, secretDockerJsonKey, configMapVault)
}

func TestPodMutation(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/bao/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func mutateDockerCreds(secret *corev1.Secret, dc *common.DockerCredentials, inje
return errors.Wrap(err, "retrieving data from bao failed")
}

assembled.Auths[key] = common.AssembleDockerAuthConfig(dcCreds, creds)
assembled.Auths[key] = common.AssembleDockerAuthConfig(dcCreds)
}
}

Expand Down
37 changes: 10 additions & 27 deletions pkg/provider/common/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,55 +48,38 @@ type DockerAuthConfig struct {
func AssembleCredentialData(authCreds map[string]string) (map[string]string, error) {
if username, ok := authCreds["username"]; ok {
if password, ok := authCreds["password"]; ok {
credentialData := map[string]string{
return map[string]string{
"username": username,
"password": password,
}

return credentialData, nil
}, nil
}
}

if auth, ok := authCreds["auth"]; ok {
credentialData := map[string]string{
return map[string]string{
"auth": auth,
}

return credentialData, nil
}, nil
}

return nil, fmt.Errorf("no valid credentials found")
}

// assembleDockerAuthConfig assembles the DockerAuthConfig from the retrieved data from Vault
func AssembleDockerAuthConfig(dcCreds map[string]string, creds DockerAuthConfig) DockerAuthConfig {
func AssembleDockerAuthConfig(dcCreds map[string]string) DockerAuthConfig {
if username, ok := dcCreds["username"]; ok {
if password, ok := dcCreds["password"]; ok {
auth := fmt.Sprintf("%s:%s", username, password)

dockerAuth := DockerAuthConfig{
Auth: base64.StdEncoding.EncodeToString([]byte(auth)),
return DockerAuthConfig{
Username: username,
Password: password,
Auth: base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))),
}

if creds.Username != "" && creds.Password != "" {
dockerAuth.Username = dcCreds["username"]
dockerAuth.Password = dcCreds["password"]
}

return dockerAuth
}
}

if auth, ok := dcCreds["auth"]; ok {
dockerAuth := DockerAuthConfig{
return DockerAuthConfig{
Auth: base64.StdEncoding.EncodeToString([]byte(auth)),
}

if creds.Auth != "" {
dockerAuth.Auth = auth
}

return dockerAuth
}

return DockerAuthConfig{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/vault/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func mutateDockerCreds(secret *corev1.Secret, dc *common.DockerCredentials, inje
return errors.Wrap(err, "retrieving data from vault failed")
}

assembled.Auths[key] = common.AssembleDockerAuthConfig(dcCreds, creds)
assembled.Auths[key] = common.AssembleDockerAuthConfig(dcCreds)
}
}

Expand Down
Loading