Azure Key Vault Provider for Secrets Store CSI Driver

repository·master·Indexed 19 days ago

https://github.com/azure/secrets-store-csi-driver-provider-azure

An Azure Key Vault provider for the Kubernetes Secrets Store CSI Driver that enables mounting Azure Key Vault secrets, keys, and certificates into Kubernetes pods as volumes. It includes support for Linux and Windows nodes, secret rotation, and Kubernetes secret syncing, with installation and configuration available via Helm.

Tokens
41.1K
Snippets
83
Records
119
Agent score
65%

What's inside secrets-store-csi-driver-provider-azure

  1. Overview of Azure Key Vault Provider for Secrets Store CSI Driver

    master

    The Azure Key Vault provider for the Secrets Store CSI Driver enables Kubernetes pods to access secret contents stored in an Azure Key Vault. It uses the Secrets Store CSI driver interface to mount these secrets directly into pods as volumes.

    Key Features

    • CSI Inline Volume Mounting: Mounts secrets, keys, and certificates to pods using a CSI Inline volume.
    • Multi-Object Volumes: Supports mounting multiple secrets store objects as a single volume.
    • Multi-Provider Support: Multiple providers can run in the same cluster simultaneously.
    • Pod Portability: Uses the SecretProviderClass Custom Resource Definition (CRD) to support pod portability.
    • Cross-Platform: Supports both Linux and Windows containers.
    • Kubernetes Secret Sync: Supports synchronizing mounted secrets with native Kubernetes Secrets.
    • Auto Rotation: Supports automatic rotation of secrets.
  2. What is Identity Binding and when to use it

    master

    Identity Binding is an authentication mode specifically for Azure Kubernetes Service (AKS).

    It is used to overcome the limitations of workload identity, specifically the 20 federated identity credential (FIC) cap per managed identity.

    In workload identity mode, every unique combination of namespace, service account, and cluster requires its own FIC. This means a single managed identity can only support up to 20 unique workloads across all clusters.

    Identity Binding advantages:

    • No per-workload FIC cap: A single FIC on the managed identity is sufficient regardless of how many clusters or workloads use it.
    • Simplified management: New workloads can share a managed identity without needing to create and manage individual FICs.
    • Cross-platform: Supported on both Windows and Linux workloads.
  3. Features of the Azure Key Vault Provider

    master

    The provider includes the following capabilities:

    • CSI Inline Volume Mounting: Mounts secrets, keys, and certificates to pods using a CSI Inline volume.
    • Multi-object Volumes: Supports mounting multiple secrets store objects as a single volume.
    • Multi-provider Support: Supports multiple secrets stores as providers; multiple providers can run in the same cluster simultaneously.
    • Pod Portability: Uses the SecretProviderClass Custom Resource Definition (CRD) to support pod portability.
    • Cross-Platform: Supports both Linux and Windows containers.
    • Kubernetes Secret Sync: Supports syncing mounted secrets with native Kubernetes Secrets.
    • Auto Rotation: Supports automatic rotation of secrets.
  4. Upgrade Azure Key Vault Provider to 0.0.9+ (gRPC compatibility)

    master

    The Azure Key Vault provider 0.0.9+ release is incompatible with Secrets Store CSI Driver versions older than v0.0.14.

    Newer versions use gRPC for communication between the driver and the provider instead of invoking the provider binary directly. To use the 0.0.9+ provider, the driver must be running version v0.0.14+ and must have the --grpc-supported-providers=azure flag enabled in its container arguments.

  5. How identity access modes work in Azure Key Vault Provider

    master

    The Azure Key Vault Provider supports six different modes for accessing a Key Vault instance, depending on your environment and security requirements:

    1. Identity Binding: RECOMMENDED for AKS. Uses a single Federated Identity Credential (FIC) on the managed identity instead of one per workload.
    2. Workload Identity: RECOMMENDED. Works on any Kubernetes cluster with an OIDC issuer.
    3. Service Principal: The only way to connect to Azure Key Vault from a non-Azure environment.
    4. Pod Identity: DEPRECATED.
    5. User-assigned Managed Identity
    6. System-assigned Managed Identity
  6. Sync multiple versions of a secret

    master

    You can sync previous versions of a secret using the objectVersionHistory property in the SecretProviderClass.

    • If objectVersionHistory is greater than 1, the provider syncs up to that many versions starting from the specified version.
    • The files are named using the pattern {objectAlias}/{versionIndex}, where versionIndex is a 0-based index starting with the latest version (e.g., SECRET_1/0 is the latest).
    • Permissions: The principal used to access Key Vault must have list permissions for secrets, keys, and certificates to use this feature.
    • Kubernetes Secret Sync: To sync a specific version to a Kubernetes secret, use the path {objectAlias}/{versionIndex} in the [secretObjects].[objectName] field.
  7. Getting started with Azure Key Vault Provider

    master

    To use the Azure Key Vault Provider, follow these high-level steps:

    1. Configure Identity and Access: Set up the necessary role assignments and access policies in Azure to allow the driver to access your Key Vault.
    2. Install the Provider: Deploy the provider to your cluster using Helm or YAML deployment files.
    3. Configure Usage: Follow the usage guide and review supported configurations to set up your SecretProviderClass.

    For a complete end-to-end example, refer to the standard walkthrough.

  8. Configure Workload Identity for Azure Key Vault access

    master

    Use Azure Workload Identity to allow your Kubernetes pods to access Azure Key Vault without managing long-lived secrets. This method uses federated identity credentials between your Kubernetes Service Account and an Azure AD Application or User-Assigned Managed Identity.

    Prerequisites

    • AKS Cluster: Version 1.21+ with OIDC Issuer enabled.
    • Secrets Store CSI Driver: v1.1.0 or higher.
    • Azure Key Vault Provider: v1.1.0 or higher.
    • Azure CLI: version 2.40.0 or higher.

    Setup Steps

    1. Create an Identity

    Either create an Azure AD Application (Service Principal) or a User-Assigned Managed Identity:

    Azure AD Application:

    export APPLICATION_NAME="<your application name>"
    az ad sp create-for-rbac --name "${APPLICATION_NAME}"
    export APPLICATION_CLIENT_ID=$(az ad sp list --display-name ${APPLICATION_NAME} --query '[0].appId' -otsv)

    User-Assigned Managed Identity:

    export RESOURCE_GROUP=<resource group name>
    export USER_ASSIGNED_IDENTITY_NAME="<your user-assigned managed identity name>"
    az identity create -g ${RESOURCE_GROUP} -n ${USER_ASSIGNED_IDENTITY_NAME}
    export USER_ASSIGNED_IDENTITY_CLIENT_ID=$(az identity show -g ${RESOURCE_GROUP} -n ${USER_ASSIGNED_IDENTITY_NAME} --query clientId -otsv)

    2. Grant Key Vault Permissions

    Assign the necessary permissions to the identity to access keys, secrets, or certificates:

    # Replace $KEYVAULT_NAME and the identity ID with your values
    az keyvault set-policy -n $KEYVAULT_NAME --key-permissions get --spn ${IDENTITY_CLIENT_ID}
    az keyvault set-policy -n $KEYVAULT_NAME --secret-permissions get --spn ${IDENTITY_CLIENT_ID}
    az keyvault set-policy -n $KEYVAULT_NAME --certificate-permissions get --spn ${IDENTITY_CLIENT_ID}

    3. Establish Federated Identity Credential

    First, retrieve your AKS OIDC issuer URL:

    export SERVICE_ACCOUNT_ISSUER=$(az aks show --resource-group <resource_group> --name <cluster_name> --query "oidcIssuerProfile.issuerUrl" -otsv)

    Then, create the federation based on your identity type:

    For Azure AD Application:

    export SERVICE_ACCOUNT_NAME=<name of the service account>
    export SERVICE_ACCOUNT_NAMESPACE=<namespace of the service account>
    export APPLICATION_OBJECT_ID="$(az ad app show --id ${APPLICATION_CLIENT_ID} --query id -otsv)"
    
    cat <<EOF > params.json
    {
      "name": "kubernetes-federated-credential",
      "issuer": "${SERVICE_ACCOUNT_ISSUER}",
      "subject": "system:serviceaccount:${SERVICE_ACCOUNT_NAMESPACE}:${SERVICE_ACCOUNT_NAME}",
      "description": "Kubernetes service account federated credential",
      "audiences": [
        "api://AzureADTokenExchange"
      ]
    }
    EOF
    
    az ad app federated-credential create --id "${APPLICATION_OBJECT_ID}" --parameters @params.json

    For User-Assigned Managed Identity:

    az identity federated-credential create \
      --name "kubernetes-federated-credential" \
      --identity-name "${USER_ASSIGNED_IDENTITY_NAME}" \
      --resource-group "${RESOURCE_GROUP}" \
      --issuer "${SERVICE_ACCOUNT_ISSUER}" \
      --subject "system:serviceaccount:${SERVICE_ACCOUNT_NAMESPACE}:${SERVICE_ACCOUNT_NAME}"

    4. Deploy SecretProviderClass

    In your SecretProviderClass, set usePodIdentity: "false" and provide the clientID of your identity.

  9. Configure Identity Binding to access Key Vault

    master

    To use Identity Binding to access Azure Key Vault on AKS, follow these three main steps:

    1. Create a managed identity and grant Key Vault access

    Create a user-assigned managed identity and grant it get permissions for keys, secrets, and certificates in your Key Vault.

    2. Create the identity binding and configure RBAC

    Refer to the AKS identity binding documentation to:

    1. Create an identity binding resource for your AKS cluster.
    2. Create the required Kubernetes RBAC (ClusterRole and ClusterRoleBinding) to authorize your service account to use the managed identity.

    3. Deploy SecretProviderClass and application

    Configure your SecretProviderClass with useAzureTokenProxy: "true" and provide the clientID of your managed identity. Ensure your Pod uses a service account that has the RBAC configured in step 2.

    Important Note for Manual Installations: If you are installing the Secrets Store CSI Driver using raw manifests instead of the Helm chart, you must ensure the CSIDriver resource includes api://AKSIdentityBinding in the tokenRequests field. The Helm chart includes this by default.

    ### 1. Create a managed identity and grant Key Vault access
    
    ```bash
    export RESOURCE_GROUP=<resource group name>
    export USER_ASSIGNED_IDENTITY_NAME="<your managed identity name>"
    export KEYVAULT_NAME="<your key vault name>"
    
    az identity create -g ${RESOURCE_GROUP} -n ${USER_ASSIGNED_IDENTITY_NAME}
    export USER_ASSIGNED_IDENTITY_CLIENT_ID=$(az identity show -g ${RESOURCE_GROUP} -n ${USER_ASSIGNED_IDENTITY_NAME} --query clientId -otsv)
    
    # Grant Key Vault access
    az keyvault set-policy -n $KEYVAULT_NAME --key-permissions get --spn ${USER_ASSIGNED_IDENTITY_CLIENT_ID}
    az keyvault set-policy -n $KEYVAULT_NAME --secret-permissions get --spn ${USER_ASSIGNED_IDENTITY_CLIENT_ID}
    az keyvault set-policy -n $KEYVAULT_NAME --certificate-permissions get --spn ${USER_ASSIGNED_IDENTITY_CLIENT_ID}
  10. Generate and import a TLS certificate to Azure Key Vault

    master

    To use TLS certificates with the NGINX Ingress Controller via the Azure Key Vault Provider, you must first generate a certificate and import it to Azure Key Vault in .pfx format.

    1. Generate a self-signed certificate using OpenSSL.
    2. Convert the certificate to PFX format.
    3. Import the PFX to Azure Key Vault using the Azure CLI.

    Note: When retrieving certificates and private keys from Azure Key Vault, you must use objectType: secret in your SecretProviderClass configuration.

    # 1. Generate TLS Cert
    export CERT_NAME=ingresscert
    openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
        -out ingress-tls.crt \
        -keyout ingress-tls.key \
        -subj "/CN=demo.test.com/O=ingress-tls"
    
    # 2. Convert to PFX and Import to Azure Key Vault
    export AKV_NAME="[YOUR AKV NAME]"
    openssl pkcs12 -export -in ingress-tls.crt -inkey ingress-tls.key  -out $CERT_NAME.pfx
    # (skip Password prompt)
    
    az keyvault certificate import --vault-name $AKV_NAME -n $CERT_NAME -f $CERT_NAME.pfx
  11. Deploy Azure Key Vault Provider via Helm

    master

    To install the Azure Key Vault Provider and the Secrets Store CSI Driver components, add the official Helm repository and run the install command.

    helm repo add csi-secrets-store-provider-azure https://azure.github.io/secrets-store-csi-driver-provider-azure/charts
    helm install csi csi-secrets-store-provider-azure/csi-secrets-store-provider-azure