Skip to content

Reading secrets from Key Vault

.NET services read application secrets and certificates from Azure Key Vault at runtime. The shared library KalmarEnergi.Core.KeyVault builds the Key Vault clients, so credential selection and vault resolution live in one place.

For the policy on where secrets belong, see Secrets management.


flowchart LR
    Service[.NET service] -->|IConfiguration| Factory[AzureKeyVaultClientFactory]
    Factory -->|ClientID or ClientSecret set| SP[Service principal credential]
    Factory -->|neither set| Default[DefaultAzureCredential]
    SP --> Vault[(Azure Key Vault)]
    Default --> Vault
    Vault -->|secret or certificate| Service
  1. The service passes its configuration to AzureKeyVaultClientFactory.
  2. The factory picks a credential:
    • Service principal (ClientSecretCredential) when Azure:ClientID or Azure:ClientSecret is set. Tenant, client ID and client secret must then all be present, otherwise the service fails when it creates the client.
    • DefaultAzureCredential when both are empty. Locally this is your az login; in the cluster it is whatever Azure identity is bound to the pod.
  3. The factory creates a client for the vault in Azure:KeyVaultUri and reads the named secret or certificate.

MethodUse it for
CreateCredential(configuration)Getting the credential only, for another Azure SDK client
CreateSecretClient(configuration, defaultVaultUri)A reusable SecretClient. Falls back to defaultVaultUri when Azure:KeyVaultUri is not set
GetSecretAsync(secretName, configuration, cancellationToken)Reading one secret value
GetCertificateAsync(certificateName, configuration, cancellationToken)Downloading a certificate including its private key

GetSecretAsync and GetCertificateAsync create a new client on every call. Call them once (for example at startup or when a singleton is created) and keep the result, rather than calling them per request.


SettingEnvironment variableWhere to set itRequired
Azure:KeyVaultUriAzure__KeyVaultUriappsettings.jsonYes. Only CreateSecretClient accepts a default
Azure:TenantIDAzure__TenantIDKubernetes secret / environmentWhen using the service principal
Azure:ClientIDAzure__ClientIDKubernetes secret / environmentWhen using the service principal
Azure:ClientSecretAzure__ClientSecretKubernetes secret onlyWhen using the service principal

In addition, each integration reads the name of the secret or certificate it needs from its own setting or constant (for example Services:BankID:CertificateName). Names are configuration; values stay in Key Vault.

The identity used (service principal, pod identity or your own account) needs read access to secrets in the vault, and read access to certificates if the service downloads certificates. Downloading a certificate with its private key also requires read access to secrets.


ServiceIntegrationReadsNotes
CustomerPortalApiIntegrationReachMeeReachMee client credentialsUses CreateSecretClient with the default vault
IdentityApiIntegrationBankIDBankID relying party certificate and BankID CAUses GetCertificateAsync and GetSecretAsync
Consumers of IntegrationCreditsafeIntegrationCreditsafeCreditsafe credentialsRead once when the service is created
MeterControlApi—BankID certificatesBuilds its own client and always requires the service principal. Candidate for moving to the shared library

  1. Run az login with an account that has read access to the vault.
  2. Leave Azure:TenantID, Azure:ClientID and Azure:ClientSecret empty so DefaultAzureCredential is used.
  3. Make sure Azure:KeyVaultUri points at the vault you need.

The same restart applies when a secret value in Key Vault is rotated, for example an external system’s password.


Use Key Vault for every application secret

Section titled “Use Key Vault for every application secret”

All new and existing services that need a secret should read it from Key Vault through KalmarEnergi.Core.KeyVault.

When building something new

  • Store the secret in Key Vault and reference it by name from configuration or a constant.
  • Read it through AzureKeyVaultClientFactory instead of creating your own SecretClient, CertificateClient or credential.
  • Hide Key Vault behind a small provider interface, so tests can supply fake values without a vault.

When working in an existing service

If a service keeps a secret in appsettings*.json, plain-text values in deployment manifests, files source code, or builds its own Key Vault client, move it to Key Vault and the shared library when you are working in that area.