Skip to content

A KeyVaultSecret keyword passes a Key Vault secret to a deployment template parameter

Context and Problem Statement

A DeployIfNotExists deployment passes values to its template's parameters. ARM accepts two shapes for each one: { "value": ... }, which Deployment -Parameters writes (ADR-0031), and a Key Vault reference, { "reference": { "keyVault": { "id": ... }, "secretName": ..., "secretVersion": ... } }, which makes Resource Manager read the secret when it runs the deployment, so that the secret is never in the policy. The Azure Landing Zones Library's Deploy-Windows-DomainJoin passes the domain user name and password this way, with the vault and secret names from policy parameters (ADR-0042). How does a Bylaw policy write a Key Vault reference?

Decision Drivers

  • The secret's location can come from policy parameters, as in the ALZ policy.
  • A Key Vault reference only means something to a deployment, so it can't be used where Bylaw compares or sets values.
  • It reads like the other values in -Parameters: Param, Arm and literals.
  • ConvertTo-Bylaw can write it back, so that such definitions convert and are proven (ADR-0039).

Considered Options

  • A KeyVaultSecret keyword, valid only as a value in Deployment -Parameters
  • A hashtable literal, @{ reference = @{ ... } }, passed through as written
  • A -SecureParameters parameter on Deployment

Decision Outcome

Chosen option: "A KeyVaultSecret keyword, valid only as a value in Deployment -Parameters", because it names what the value is, its parts can be Param or Arm, and it can be rejected anywhere else.

Deployment -TemplateFile ./templates/domain-join.json -Parameters @{
    vmName         = (Arm "[field('name')]")
    domainPassword = (KeyVaultSecret -VaultId (Param KeyVaultResourceId) -SecretName (Param DomainPasswordSecret))
}
  • -VaultId (the vault's resource ID) and -SecretName are required, and -SecretVersion is optional, as in ARM. Each is a string, Param or Arm (BYL1062).
  • It is emitted as { "reference": { "keyVault": { "id" }, "secretName", "secretVersion"? } }, in the order ARM's documentation gives it, with Param written as [parameters('...')].
  • It isn't a BylawValue in the IR, so it can't be compared with, set by Modify or Append, or passed to an initiative member. Anywhere other than Deployment -Parameters it is an error (BYL1063), and inside a hashtable or array literal it is the usual BYL1020.
  • The template parameter it is passed to is checked as any other (BYL3093, BYL3094), and parameters used in its parts must be declared (BYL3020) and count as used.
  • Bylaw doesn't require the template parameter to be securestring: ARM accepts a reference for other types, and the ALZ policy passes the user name to a String parameter.
  • ConvertTo-Bylaw writes a reference whose parts are strings as (KeyVaultSecret ...); other shapes are still reported (BYL6002).

Consequences

  • Good, because the ALZ definition that needed it converts and is proven (ADR-0042).
  • Good, because a Key Vault reference can't be misused as a value elsewhere.
  • Neutral, because whether the deployment can read the secret depends on the vault (it must allow template deployment) and the assignment's managed identity, which Bylaw can't check.
  • Bad, because it is one more keyword to learn, for a feature few policies need.

Confirmation

Unit tests cover the keyword, its errors, the emitted shape and the checks on its parts. The ConvertTo-Bylaw tests and the ALZ baseline cover conversion.

Pros and Cons of the Options

A hashtable literal, passed through as written

  • Good, because it needs no new keyword.
  • Bad, because a hashtable can't contain Param or Arm (BYL1020), so the vault and secret couldn't come from policy parameters without an exception to that rule.
  • Bad, because a misspelled key would be passed through to the deployment.

A -SecureParameters parameter on Deployment

  • Good, because it keeps secrets apart from the other values.
  • Bad, because one template parameter's value would be split across two Deployment parameters, and a name could be given in both.

More Information