KOP Recipes - AWS Secrets Manager Access - Rafay Product Documentation

Access

In this part, you will


Step 1: Create Secret Provider Class

To take advantage of the Secrets Store CSI driver a SecretProviderClass custom resource will need to be created. This provides driver configurations and parameter specific details to the CSI driver. We are utilizing ASCP so will need to define the objects and type. In the example below we will retrieve secrets from AWS Secrets Manager and sync the defined secrets to K8s cluster secrets. Secrets are defined under the secretObjects section. Objects defined in the objects section will be mounted as files.

Create and Publish Workload

  apiVersion: secrets-store.csi.x-k8s.io/v1alpha1
  kind: SecretProviderClass
  metadata:
    name: nginx-deployment-aws-secrets
  spec:
    provider: aws
    secretObjects:                        # [OPTIONAL] SecretObject defines the desired state of synced K8s secret objects
    - data:
      - key: username                     # data field to populate
        objectName: username
      - key: password                     # data field to populate
        objectName: password
      secretName: complete-secret         # name of the Kubernetes Secret object
      type: Opaque
    parameters:
      objects: |
          - objectName: "MySecret"
            objectType: "secretsmanager"
            jmesPath:
                  - path: "username"
                    objectAlias: "username"
                  - path: "password"
                    objectAlias: "password"

In a minute the workload should be deployed.


Step 2: Deploy Application

We are using nginx in this example to pull the secret from AWS Secret Manager.

In this step, we will configure and deploy a Nginx workload to the EKS Cluster. We will use a K8s YAML manifest which will incorporate the IRSA for Nginx from the prior step. The following manifest will create a service and deployment and is configured to use the SecretProviderClass and IRSA we created in previous steps so no changes are needed.

Create and Publish Workload

wget https://raw.githubusercontent.com/aws/secrets-store-csi-driver-provider-aws/main/examples/ExampleDeployment.yaml

Below is an example of a sample application. Note the following:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      serviceAccountName: nginx-deployment-sa
      volumes:
      - name: secrets-store-inline
        csi:
          driver: secrets-store.csi.k8s.io
          readOnly: true
          volumeAttributes:
            secretProviderClass: "nginx-deployment-aws-secrets"
      containers:
      - name: nginx-deployment
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: secrets-store-inline
          mountPath: "/mnt/secrets-store"
          readOnly: true

In a minute the workload should be deployed.

Step 3: Access the secrets

# cat /mnt/secrets-store/MySecret

The secret we configured in AWS Secrets Manager will be displayed and can be used in your applications.

{"username":"rafay", "password":"Rafay$2021"}
kubectl get secrets -n nginx complete-secret

NAME              TYPE     DATA   AGE
complete-secret   Opaque   2      121m
kubectl get secrets -n secrets-manager complete-secret -o yaml
apiVersion: v1
data:
  password: UmFmYXkkMjAyMQ==
  username: cmFmYXk=
kind: Secret

K8s secrets are stored as Base64 encoded strings. You can use the base64 program to decode the strings.

echo UmFmYXkkMjAyMQ== | base64 --decode
Rafay

Congratulations! You can now pull and inject secrets from AWS Secrets Manager into your applications.

More information about Secret Store CSI driver and examples can be found here