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.
How it works
Section titled “How it works”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
- The service passes its configuration to
AzureKeyVaultClientFactory. - The factory picks a credential:
- Service principal (
ClientSecretCredential) whenAzure:ClientIDorAzure:ClientSecretis set. Tenant, client ID and client secret must then all be present, otherwise the service fails when it creates the client. DefaultAzureCredentialwhen both are empty. Locally this is youraz login; in the cluster it is whatever Azure identity is bound to the pod.
- Service principal (
- The factory creates a client for the vault in
Azure:KeyVaultUriand reads the named secret or certificate.
Shared library
Section titled “Shared library”| Method | Use 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.
Required configuration
Section titled “Required configuration”| Setting | Environment variable | Where to set it | Required |
|---|---|---|---|
Azure:KeyVaultUri | Azure__KeyVaultUri | appsettings.json | Yes. Only CreateSecretClient accepts a default |
Azure:TenantID | Azure__TenantID | Kubernetes secret / environment | When using the service principal |
Azure:ClientID | Azure__ClientID | Kubernetes secret / environment | When using the service principal |
Azure:ClientSecret | Azure__ClientSecret | Kubernetes secret only | When 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.
Current consumers
Section titled “Current consumers”| Service | Integration | Reads | Notes |
|---|---|---|---|
| CustomerPortalApi | IntegrationReachMee | ReachMee client credentials | Uses CreateSecretClient with the default vault |
| IdentityApi | IntegrationBankID | BankID relying party certificate and BankID CA | Uses GetCertificateAsync and GetSecretAsync |
| Consumers of IntegrationCreditsafe | IntegrationCreditsafe | Creditsafe credentials | Read once when the service is created |
| MeterControlApi | — | BankID certificates | Builds its own client and always requires the service principal. Candidate for moving to the shared library |
Local development
Section titled “Local development”- Run
az loginwith an account that has read access to the vault. - Leave
Azure:TenantID,Azure:ClientIDandAzure:ClientSecretempty soDefaultAzureCredentialis used. - Make sure
Azure:KeyVaultUripoints at the vault you need.
Rotating the client secret
Section titled “Rotating the client secret”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
AzureKeyVaultClientFactoryinstead of creating your ownSecretClient,CertificateClientor 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.