diff --git a/pkg/inject/secrets-provider_test.go b/pkg/inject/secrets-provider_test.go index a6b0555..a3e9264 100644 --- a/pkg/inject/secrets-provider_test.go +++ b/pkg/inject/secrets-provider_test.go @@ -44,6 +44,30 @@ func TestSecretsProviderSidecarInjection(t *testing.T) { "conjurSslCertificate": "-----BEGIN CERTIFICATE-----tVw0ZnjsOV2ZeIBRalX/72RplPzkmWKAw==\n-----END CERTIFICATE-----\n", }, }, + { + description: "SecretsProvider k8s_secrets sidecar - no conjur-secrets volume injected", + annotatedPodTemplateSpecPath: "./testdata/secrets-provider-k8s-annotated-pod.json", + expectedInjectedPodTemplateSpecPath: "./testdata/secrets-provider-k8s-mutated-pod.json", + env: map[string]string{ + "CONJUR_ACCOUNT": "myConjurAccount", + "CONJUR_APPLIANCE_URL": "https://conjur-oss.conjur-oss.svc.cluster.local", + "CONJUR_AUTHENTICATOR_ID": "my-authenticator-id", + "CONJUR_AUTHN_URL": "https://conjur-oss.conjur-oss.svc.cluster.local/authn-k8s/my-authenticator-id", + "CONJUR_SSL_CERTIFICATE": "-----BEGIN CERTIFICATE-----tVw0ZnjsOV2ZeIBRalX/72RplPzkmWKAw==\n-----END CERTIFICATE-----\n", + }, + }, + { + description: "SecretsProvider k8s_secrets init - no conjur-secrets volume injected", + annotatedPodTemplateSpecPath: "./testdata/secrets-provider-k8s-init-annotated-pod.json", + expectedInjectedPodTemplateSpecPath: "./testdata/secrets-provider-k8s-init-mutated-pod.json", + env: map[string]string{ + "CONJUR_ACCOUNT": "myConjurAccount", + "CONJUR_APPLIANCE_URL": "https://conjur-oss.conjur-oss.svc.cluster.local", + "CONJUR_AUTHENTICATOR_ID": "my-authenticator-id", + "CONJUR_AUTHN_URL": "https://conjur-oss.conjur-oss.svc.cluster.local/authn-k8s/my-authenticator-id", + "CONJUR_SSL_CERTIFICATE": "-----BEGIN CERTIFICATE-----tVw0ZnjsOV2ZeIBRalX/72RplPzkmWKAw==\n-----END CERTIFICATE-----\n", + }, + }, } for _, tc := range testCases { t.Run(tc.description, func(t *testing.T) { diff --git a/pkg/inject/server.go b/pkg/inject/server.go index a06ea79..bb4951e 100644 --- a/pkg/inject/server.go +++ b/pkg/inject/server.go @@ -302,18 +302,21 @@ func HandleAdmissionRequest( containerVolumeMounts := ContainerVolumeMounts{} for _, receiveContainerName := range conjurInjectVolume { - containerVolumeMounts[receiveContainerName] = []corev1.VolumeMount{ + mounts := []corev1.VolumeMount{ { Name: "conjur-status", ReadOnly: false, MountPath: "/conjur/status", }, - { + } + if secretsDestination == "file" { + mounts = append(mounts, corev1.VolumeMount{ Name: "conjur-secrets", ReadOnly: false, MountPath: "/conjur/secrets", - }, + }) } + containerVolumeMounts[receiveContainerName] = mounts } sidecarConfig.ContainerVolumeMounts = containerVolumeMounts default: diff --git a/pkg/inject/testdata/secrets-provider-k8s-annotated-pod.json b/pkg/inject/testdata/secrets-provider-k8s-annotated-pod.json new file mode 100644 index 0000000..1bc1bcd --- /dev/null +++ b/pkg/inject/testdata/secrets-provider-k8s-annotated-pod.json @@ -0,0 +1,42 @@ +{ + "metadata": { + "generateName": "nginx-deployment-6c54bd5869-", + "labels": { + "app": "nginx", + "pod-template-hash": "2710681425" + }, + "annotations": { + "conjur.org/inject": "true", + "conjur.org/inject-type": "secrets-provider", + "conjur.org/container-name" : "secrets-provider-name", + "conjur.org/container-mode": "sidecar", + "conjur.org/secrets-destination": "k8s_secrets", + "conjur.org/conjur-inject-volumes": "nginx-1", + "my-company": "my-project" + } + }, + "spec": { + "volumes": [ + { + "name": "default-token-tq5lq", + "secret": { + "secretName": "default-token-tq5lq" + } + } + ], + "containers": [ + { + "name": "nginx-1", + "image": "nginx:1.7.9", + "volumeMounts": [ + { + "name": "default-token-tq5lq", + "readOnly": true, + "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount" + } + ] + } + ] + } +} + diff --git a/pkg/inject/testdata/secrets-provider-k8s-init-annotated-pod.json b/pkg/inject/testdata/secrets-provider-k8s-init-annotated-pod.json new file mode 100644 index 0000000..3cffafe --- /dev/null +++ b/pkg/inject/testdata/secrets-provider-k8s-init-annotated-pod.json @@ -0,0 +1,42 @@ +{ + "metadata": { + "generateName": "nginx-deployment-6c54bd5869-", + "labels": { + "app": "nginx", + "pod-template-hash": "2710681425" + }, + "annotations": { + "conjur.org/inject": "true", + "conjur.org/inject-type": "secrets-provider", + "conjur.org/container-name" : "secrets-provider-name", + "conjur.org/container-mode": "init", + "conjur.org/secrets-destination": "k8s_secrets", + "conjur.org/conjur-inject-volumes": "nginx-1", + "my-company": "my-project" + } + }, + "spec": { + "volumes": [ + { + "name": "default-token-tq5lq", + "secret": { + "secretName": "default-token-tq5lq" + } + } + ], + "containers": [ + { + "name": "nginx-1", + "image": "nginx:1.7.9", + "volumeMounts": [ + { + "name": "default-token-tq5lq", + "readOnly": true, + "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount" + } + ] + } + ] + } +} + diff --git a/pkg/inject/testdata/secrets-provider-k8s-init-mutated-pod.json b/pkg/inject/testdata/secrets-provider-k8s-init-mutated-pod.json new file mode 100644 index 0000000..62b3ebc --- /dev/null +++ b/pkg/inject/testdata/secrets-provider-k8s-init-mutated-pod.json @@ -0,0 +1,119 @@ +{ + "metadata": { + "generateName": "nginx-deployment-6c54bd5869-", + "labels": { + "app": "nginx", + "pod-template-hash": "2710681425" + }, + "annotations": { + "conjur.org/container-mode": "init", + "conjur.org/secrets-destination": "k8s_secrets", + "my-company": "my-project", + "conjur.org/status": "injected" + } + }, + "spec": { + "volumes": [ + { + "name": "default-token-tq5lq", + "secret": { + "secretName": "default-token-tq5lq" + } + }, + { + "name": "podinfo", + "downwardAPI": { + "items": [ + { + "path": "annotations", + "fieldRef": { + "fieldPath": "metadata.annotations" + } + } + ] + } + }, + { + "name": "conjur-status", + "emptyDir": { + "medium": "Memory" + } + } + ], + "containers": [ + { + "name": "nginx-1", + "image": "nginx:1.7.9", + "volumeMounts": [ + { + "name": "default-token-tq5lq", + "readOnly": true, + "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount" + }, + { + "name": "conjur-status", + "mountPath": "/conjur/status" + } + ] + } + ], + "initContainers": [ + { + "name": "secrets-provider-name", + "image": "secrets-provider-image", + "resources": {}, + "imagePullPolicy": "Always", + "env": [ + { + "name": "MY_POD_NAME", + "valueFrom": { + "fieldRef": { + "fieldPath": "metadata.name" + } + } + }, + { + "name": "MY_POD_NAMESPACE", + "valueFrom": { + "fieldRef": { + "fieldPath": "metadata.namespace" + } + } + }, + { + "name": "CONJUR_ACCOUNT", + "value": "myConjurAccount" + }, + { + "name": "CONJUR_APPLIANCE_URL", + "value": "https://conjur-oss.conjur-oss.svc.cluster.local" + }, + { + "name": "CONJUR_AUTHENTICATOR_ID", + "value": "my-authenticator-id" + }, + { + "name": "CONJUR_AUTHN_URL", + "value": "https://conjur-oss.conjur-oss.svc.cluster.local/authn-k8s/my-authenticator-id" + }, + { + "name": "CONJUR_SSL_CERTIFICATE", + "value": "-----BEGIN CERTIFICATE-----tVw0ZnjsOV2ZeIBRalX/72RplPzkmWKAw==\n-----END CERTIFICATE-----\n" + } + ], + "volumeMounts": [ + { + "name": "podinfo", + "readOnly": true, + "mountPath": "/conjur/podinfo" + }, + { + "name": "conjur-status", + "mountPath": "/conjur/status" + } + ] + } + ] + } +} + diff --git a/pkg/inject/testdata/secrets-provider-k8s-mutated-pod.json b/pkg/inject/testdata/secrets-provider-k8s-mutated-pod.json new file mode 100644 index 0000000..91dbd18 --- /dev/null +++ b/pkg/inject/testdata/secrets-provider-k8s-mutated-pod.json @@ -0,0 +1,117 @@ +{ + "metadata": { + "generateName": "nginx-deployment-6c54bd5869-", + "labels": { + "app": "nginx", + "pod-template-hash": "2710681425" + }, + "annotations": { + "conjur.org/container-mode": "sidecar", + "conjur.org/secrets-destination": "k8s_secrets", + "my-company": "my-project", + "conjur.org/status": "injected" + } + }, + "spec": { + "volumes": [ + { + "name": "default-token-tq5lq", + "secret": { + "secretName": "default-token-tq5lq" + } + }, + { + "name": "podinfo", + "downwardAPI": { + "items": [ + { + "path": "annotations", + "fieldRef": { + "fieldPath": "metadata.annotations" + } + } + ] + } + }, + { + "name": "conjur-status", + "emptyDir": { + "medium": "Memory" + } + } + ], + "containers": [ + { + "name": "nginx-1", + "image": "nginx:1.7.9", + "volumeMounts": [ + { + "name": "default-token-tq5lq", + "readOnly": true, + "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount" + }, + { + "name": "conjur-status", + "mountPath": "/conjur/status" + } + ] + }, + { + "name": "secrets-provider-name", + "image": "secrets-provider-image", + "resources": {}, + "imagePullPolicy": "Always", + "env": [ + { + "name": "MY_POD_NAME", + "valueFrom": { + "fieldRef": { + "fieldPath": "metadata.name" + } + } + }, + { + "name": "MY_POD_NAMESPACE", + "valueFrom": { + "fieldRef": { + "fieldPath": "metadata.namespace" + } + } + }, + { + "name": "CONJUR_ACCOUNT", + "value": "myConjurAccount" + }, + { + "name": "CONJUR_APPLIANCE_URL", + "value": "https://conjur-oss.conjur-oss.svc.cluster.local" + }, + { + "name": "CONJUR_AUTHENTICATOR_ID", + "value": "my-authenticator-id" + }, + { + "name": "CONJUR_AUTHN_URL", + "value": "https://conjur-oss.conjur-oss.svc.cluster.local/authn-k8s/my-authenticator-id" + }, + { + "name": "CONJUR_SSL_CERTIFICATE", + "value": "-----BEGIN CERTIFICATE-----tVw0ZnjsOV2ZeIBRalX/72RplPzkmWKAw==\n-----END CERTIFICATE-----\n" + } + ], + "volumeMounts": [ + { + "name": "podinfo", + "readOnly": true, + "mountPath": "/conjur/podinfo" + }, + { + "name": "conjur-status", + "mountPath": "/conjur/status" + } + ] + } + ] + } +} +