Skip to content

Commit ee6d1c3

Browse files
authored
Merge pull request #233055 from anthonychu/20230331-secret-kv-references
[Container Apps] Add secrets key vault references
2 parents a0d3e0f + d9534ac commit ee6d1c3

File tree

1 file changed

+148
-8
lines changed

1 file changed

+148
-8
lines changed

articles/container-apps/manage-secrets.md

Lines changed: 148 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ services: container-apps
55
author: craigshoemaker
66
ms.service: container-apps
77
ms.topic: how-to
8-
ms.date: 09/29/2022
8+
ms.date: 04/06/2023
99
ms.author: cshoe
1010
ms.custom: event-tier1-build-2022, ignite-2022, devx-track-azurecli, devx-track-azurepowershell
1111
---
1212

1313
# Manage secrets in Azure Container Apps
1414

15-
Azure Container Apps allows your application to securely store sensitive configuration values. Once secrets are defined at the application level, secured values are available to container apps. Specifically, you can reference secured values inside scale rules. For information on using secrets with Dapr, refer to [Dapr integration](./dapr-overview.md)
15+
Azure Container Apps allows your application to securely store sensitive configuration values. Once secrets are defined at the application level, secured values are available to revisions in your container apps. Additionally, you can reference secured values inside scale rules. For information on using secrets with Dapr, refer to [Dapr integration](./dapr-overview.md).
1616

1717
- Secrets are scoped to an application, outside of any specific revision of an application.
1818
- Adding, removing, or changing secrets doesn't generate new revisions.
@@ -26,11 +26,29 @@ An updated or deleted secret doesn't automatically affect existing revisions in
2626

2727
Before you delete a secret, deploy a new revision that no longer references the old secret. Then deactivate all revisions that reference the secret.
2828

29-
> [!NOTE]
30-
> Container Apps doesn't support Azure Key Vault integration. Instead, enable managed identity in the container app and use the [Key Vault SDK](../key-vault/general/developers-guide.md) in your app to access secrets.
29+
## Defining secrets
3130

31+
Secrets are defined as a set of name/value pairs. The value of each secret is specified directly or as a reference to a secret stored in Azure Key Vault.
3232

33-
## Defining secrets
33+
### Store secret value in Container Apps
34+
35+
When you define secrets through the portal, or via different command line options.
36+
37+
# [Azure portal](#tab/azure-portal)
38+
39+
1. Go to your container app in the [Azure portal](https://portal.azure.com).
40+
41+
1. Under the *Settings* section, select **Secrets**.
42+
43+
1. Select **Add**.
44+
45+
1. In the *Add secret* context pane, enter the following information:
46+
47+
- **Name**: The name of the secret.
48+
- **Type**: Select **Container Apps Secret**.
49+
- **Value**: The value of the secret.
50+
51+
1. Select **Add**.
3452

3553
# [ARM template](#tab/arm-template)
3654

@@ -67,10 +85,10 @@ az containerapp create \
6785
--name queuereader \
6886
--environment "my-environment-name" \
6987
--image demos/queuereader:v1 \
70-
--secrets "queue-connection-string=$CONNECTION_STRING"
88+
--secrets "queue-connection-string=<CONNECTION_STRING>"
7189
```
7290

73-
Here, a connection string to a queue storage account is declared in the `--secrets` parameter. The value for `queue-connection-string` comes from an environment variable named `$CONNECTION_STRING`.
91+
Here, a connection string to a queue storage account is declared in the `--secrets` parameter. Replace `<CONNECTION_STRING>` with the value of your connection string.
7492

7593
# [PowerShell](#tab/powershell)
7694

@@ -97,14 +115,136 @@ Here, a connection string to a queue storage account is declared. The value for
97115

98116
---
99117

118+
### <a name="reference-secret-from-key-vault"></a>Reference secret from Key Vault (preview)
119+
120+
When you define a secret, you create a reference to a secret stored in Azure Key Vault. Container Apps automatically retrieves the secret value from Key Vault and makes it available as a secret in your container app.
121+
122+
To reference a secret from Key Vault, you must first enable managed identity in your container app and grant the identity access to the Key Vault secrets.
123+
124+
To enable managed identity in your container app, see [Managed identities](managed-identity.md).
125+
126+
To grant access to Key Vault secrets, [create an access policy](../key-vault/general/assign-access-policy.md) in Key Vault for the managed identity you created. Enable the "Get" secret permission on this policy.
127+
128+
# [Azure portal](#tab/azure-portal)
129+
130+
1. Go to your container app in the [Azure portal](https://portal.azure.com).
131+
132+
1. Under the *Settings* section, select **Identity**.
133+
134+
1. In the *System assigned* tab, select **On**.
135+
136+
1. Select **Save** to enable system-assigned managed identity.
137+
138+
1. Under the *Settings* section, select **Secrets**.
139+
140+
1. Select **Add**.
141+
142+
1. In the *Add secret* context pane, enter the following information:
143+
144+
- **Name**: The name of the secret.
145+
- **Type**: Select **Key Vault reference**.
146+
- **Key Vault secret URL**: The URI of your secret in Key Vault.
147+
- **Identity**: The identity to use to retrieve the secret from Key Vault.
148+
149+
1. Select **Add**.
150+
151+
# [ARM template](#tab/arm-template)
152+
153+
Secrets are defined at the application level in the `resources.properties.configuration.secrets` section.
154+
155+
```json
156+
"resources": [
157+
{
158+
...
159+
"properties": {
160+
"configuration": {
161+
"secrets": [
162+
{
163+
"name": "queue-connection-string",
164+
"keyVaultUrl": "<KEY-VAULT-SECRET-URI>",
165+
"identity": "System"
166+
}],
167+
}
168+
}
169+
}
170+
```
171+
172+
Here, a connection string to a queue storage account is declared in the `secrets` array. Its value is automatically retrieved from Key Vault using the specified identity. To use a user managed identity, replace `System` with the identity's resource ID.
173+
174+
Replace `<KEY-VAULT-SECRET-URI>` with the URI of your secret in Key Vault.
175+
176+
# [Azure CLI](#tab/azure-cli)
177+
178+
When you create a container app, secrets are defined using the `--secrets` parameter.
179+
180+
- The parameter accepts a space-delimited set of name/value pairs.
181+
- Each pair is delimited by an equals sign (`=`).
182+
- To specify a Key Vault reference, use the format `<SECRET_NAME>=keyvaultref:<KEY_VAULT_SECRET_URI>,identityref:<MANAGED_IDENTITY_ID>`. For example, `queue-connection-string=keyvaultref:https://mykeyvault.vault.azure.net/secrets/queuereader,identityref:/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/my-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-identity`.
183+
184+
```bash
185+
az containerapp create \
186+
--resource-group "my-resource-group" \
187+
--name queuereader \
188+
--environment "my-environment-name" \
189+
--image demos/queuereader:v1 \
190+
--user-assigned "<USER_ASSIGNED_IDENTITY_ID>" \
191+
--secrets "queue-connection-string=keyvaultref:<KEY_VAULT_SECRET_URI>,identityref:<USER_ASSIGNED_IDENTITY_ID>"
192+
```
193+
194+
Here, a connection string to a queue storage account is declared in the `--secrets` parameter. Replace `<KEY_VAULT_SECRET_URI>` with the URI of your secret in Key Vault. Replace `<USER_ASSIGNED_IDENTITY_ID>` with the resource ID of the user assigned identity. For system assigned identity, use `System` instead of the resource ID.
195+
196+
> [!NOTE]
197+
> The user assigned identity must have access to read the secret in Key Vault. System assigned identity can't be used with the create command because it's not available until after the container app is created.
198+
199+
# [PowerShell](#tab/powershell)
200+
201+
Secrets Key Vault references aren't supported in PowerShell.
202+
203+
---
204+
205+
#### Key Vault secret URI and secret rotation
206+
207+
The Key Vault secret URI must be in one of the following formats:
208+
209+
* `https://myvault.vault.azure.net/secrets/mysecret/ec96f02080254f109c51a1f14cdb1931`: Reference a specific version of a secret.
210+
* `https://myvault.vault.azure.net/secrets/mysecret`: Reference the latest version of a secret.
211+
212+
If a version isn't specified in the URI, then the app uses the latest version that exists in the key vault. When newer versions become available, the app automatically retrieves the latest version within 30 minutes. Any active revisions that reference the secret in an environment variable is automatically restarted to pick up the new value.
213+
214+
For full control of which version of a secret is used, specify the version in the URI.
215+
100216
## <a name="using-secrets"></a>Referencing secrets in environment variables
101217

102218
After declaring secrets at the application level as described in the [defining secrets](#defining-secrets) section, you can reference them in environment variables when you create a new revision in your container app. When an environment variable references a secret, its value is populated with the value defined in the secret.
103219

104-
## Example
220+
### Example
105221

106222
The following example shows an application that declares a connection string at the application level. This connection is referenced in a container environment variable and in a scale rule.
107223

224+
# [Azure portal](#tab/azure-portal)
225+
226+
After you've [defined a secret](#defining-secrets) in your container app, you can reference it in an environment variable when you create a new revision.
227+
228+
1. Go to your container app in the [Azure portal](https://portal.azure.com).
229+
230+
1. Open the *Revision management* page.
231+
232+
1. Select **Create new revision**.
233+
234+
1. In the *Create and deploy new revision* page, select a container.
235+
236+
1. In the *Environment variables* section, select **Add**.
237+
238+
1. Enter the following information:
239+
240+
- **Name**: The name of the environment variable.
241+
- **Source**: Select **Reference a secret**.
242+
- **Value**: Select the secret you want to reference.
243+
244+
1. Select **Save**.
245+
246+
1. Select **Create** to create the new revision.
247+
108248
# [ARM template](#tab/arm-template)
109249

110250
In this example, the application connection string is declared as `queue-connection-string` and becomes available elsewhere in the configuration sections.

0 commit comments

Comments
 (0)