|
| 1 | +// Module is included in the following assemblies: |
| 2 | +// |
| 3 | +// * securing_openshift_gitops/managing-secrets-securely-using-sscsid-with-gitops.adoc |
| 4 | + |
| 5 | +:_mod-docs-content-type: PROCEDURE |
| 6 | +[id="gitops-configuring-gitops-managed-resources-to-use-vault-mounted-secrets_{context}"] |
| 7 | += Configuring GitOps managed resources to use Vault-mounted secrets |
| 8 | + |
| 9 | +Securely inject secrets from HashiCorp Vault into GitOps-managed Kubernetes workloads using the Secrets Store CSI driver and Vault provider. The secrets are mounted as files in the pod's filesystem, allowing applications to access the data without storing it in Kubernetes Secret objects. |
| 10 | + |
| 11 | +.Procedure |
| 12 | + |
| 13 | +. Creating the `SecretProviderClass`. |
| 14 | + |
| 15 | +.. Create a `SecretProviderClass` resource in the application's manifest directory for example, `environments/dev/apps/demo-app/manifest/secretProviderClass.yaml`. This resource defines how the Secrets Store CSI driver retrieves secrets from Vault. |
| 16 | ++ |
| 17 | +.Example `vault-secret-provider-app.yaml` file |
| 18 | +[source,yaml] |
| 19 | +---- |
| 20 | +apiVersion: secrets-store.csi.x-k8s.io/v1 |
| 21 | + kind: SecretProviderClass |
| 22 | + metadata: |
| 23 | + name: demo-app-creds |
| 24 | + namespace: demo-app |
| 25 | + spec: |
| 26 | + provider: vault #<1> |
| 27 | + parameters: |
| 28 | + vaultAddress: http://vault.vault-csi-provider:8200 # <name>.<namespace>:port #<2> |
| 29 | + roleName: app #<3> |
| 30 | + objects: | #<4> |
| 31 | + - objectName: "demoAppUsername" |
| 32 | + secretPath: "secret/demo/config" |
| 33 | + secretKey: "username" |
| 34 | + - objectName: "demoAppPassword" |
| 35 | + secretPath: "secret/demo/config" |
| 36 | + secretKey: "password" |
| 37 | +---- |
| 38 | +<1> `<provider: vault>` - Specifies the name of the HashiCorp Vault. |
| 39 | +<2> `<vaultAddress>` - Specifies the network address of the Vault server. Adjust this based on your Vault setup, such as, in-cluster service or an external URL. |
| 40 | +<3> `<roleName>` - Specifies the Vault Kubernetes authentication role used by the application Service Account. |
| 41 | +Describes an array that defines which secrets to retrieve and how to map them to file names. |
| 42 | +<4> `<objects>` - Specifies an array that defines which secrets to retrieve and how to map them to file names. The `secretPath` for KV v2 includes `/data/`. |
| 43 | + |
| 44 | +. Create an Application, such as, `ServiceAccount`. |
| 45 | + |
| 46 | +.. Create a Kubernetes `ServiceAccount` for the application workload. The `ServiceAccount` name must match the `bound_service_account_names` value defined in the Vault Kubernetes authentication role. Store the manifest in the GitOps repository, for example, `environments/dev/apps/demo-app/manifest/serviceAccount.yaml`. |
| 47 | ++ |
| 48 | +[source,yaml] |
| 49 | +---- |
| 50 | +apiVersion: v1 |
| 51 | +kind: ServiceAccount |
| 52 | +metadata: |
| 53 | + name: demo-app-sa |
| 54 | + namespace: demo-app |
| 55 | +---- |
| 56 | + |
| 57 | +. Create the Application deployment: |
| 58 | + |
| 59 | +.. Modify the application's deployment to use the designated `ServiceAccount` and mount secrets using the CSI volume. Store the updated manifest in the GitOps repository, for example, `environments/dev/apps/demo-app/manifest/deployment.yaml`: |
| 60 | ++ |
| 61 | +.Example `deployment.yaml` file |
| 62 | +[source,yaml] |
| 63 | +---- |
| 64 | +apiVersion: apps/v1 |
| 65 | +kind: Deployment |
| 66 | +metadata: |
| 67 | + name: app |
| 68 | + namespace: demo-app |
| 69 | + labels: |
| 70 | + app: demo |
| 71 | +spec: |
| 72 | + replicas: 1 |
| 73 | + selector: |
| 74 | + matchLabels: |
| 75 | + app: demo |
| 76 | + template: |
| 77 | + metadata: |
| 78 | + labels: |
| 79 | + app: demo |
| 80 | + spec: |
| 81 | + serviceAccountName: demo-app-sa # <1> |
| 82 | + containers: |
| 83 | + - name: app |
| 84 | + image: nginxinc/nginx-unprivileged:latest |
| 85 | + volumeMounts: # <2> |
| 86 | + - name: vault-secrets |
| 87 | + mountPath: /mnt/secrets-store |
| 88 | + readOnly: true |
| 89 | + volumes: # <3> |
| 90 | + - name: vault-secrets |
| 91 | + csi: |
| 92 | + driver: secrets-store.csi.k8s.io |
| 93 | + readOnly: true |
| 94 | + volumeAttributes: |
| 95 | + secretProviderClass: demo-app-creds |
| 96 | +---- |
| 97 | +<1> `serviceAccountName` - Assigns the Kubernetes `ServiceAccount` name, for example, `demo-app-sa`, used by the application pod. This `ServiceAccount` is fundamental for authenticating with HashiCorp Vault, as it is linked to a Vault role that grants permissions to retrieve the necessary secrets. |
| 98 | +<2> `volumeMounts` - Mounts the vault-secrets volume into the container at the `/mnt/secrets-store` directory. |
| 99 | +<3> `volumes` - Defines the vault-secrets volume using the `secrets-store.csi.k8s.io` driver and references the `demo-app-creds` `SecretProviderClass`. |
| 100 | + |
| 101 | +. Define the Argo CD application for the workload: |
| 102 | + |
| 103 | +.. Define an Argo CD application resource to deploy application components such as `ServiceAccount`, `SecretProviderClass`, and `Deployment` from the GitOps repository. Store the Argo CD manifest in a directory location, such as, `environments/dev/apps/demo-app/argocd/demo-app.yaml`. |
| 104 | ++ |
| 105 | +.Example `demo-app.yaml` file |
| 106 | +[source,yaml] |
| 107 | +---- |
| 108 | +apiVersion: argoproj.io/v1alpha1 |
| 109 | +kind: Application |
| 110 | +metadata: |
| 111 | + name: demo-app |
| 112 | + namespace: openshift-gitops |
| 113 | +spec: |
| 114 | + project: default |
| 115 | + source: |
| 116 | + repoURL: https://your-git-repo-url.git |
| 117 | + targetRevision: HEAD |
| 118 | + path: environments/dev/apps/demo-app/manifest |
| 119 | + destination: |
| 120 | + server: https://kubernetes.default.svc |
| 121 | + namespace: demo-app |
| 122 | + syncPolicy: |
| 123 | + automated: |
| 124 | + prune: true |
| 125 | + selfHeal: true |
| 126 | + syncOptions: |
| 127 | + - CreateNamespace=true |
| 128 | +---- |
0 commit comments