Skip to content

Commit bc171b1

Browse files
committed
[ACA] Show how to use KeyVault to store/retrieve container registry password
1 parent e8f1bf2 commit bc171b1

File tree

1 file changed

+109
-13
lines changed

1 file changed

+109
-13
lines changed

articles/container-apps/get-started-existing-container-image.md

Lines changed: 109 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,123 @@ The example shown in this article demonstrates how to use a custom container ima
5151

5252
::: zone pivot="container-apps-private-registry"
5353

54-
# [Bash](#tab/bash)
54+
### Set environment variables
5555

56-
For details on how to provide values for any of these parameters to the `create` command, run `az containerapp create --help` or [visit the online reference](/cli/azure/containerapp#az-containerapp-create). To generate credentials for an Azure Container Registry, use [az acr credential show](/cli/azure/acr/credential#az-acr-credential-show).
56+
Replace the \<PLACEHOLDERS\> with your values. Your user principal name will typically be in the format of an email address (for example, `[email protected]`).
57+
58+
# [Bash](#tab/bash)
5759

5860
```bash
61+
KEY_VAULT_NAME=<KEY_VAULT_NAME>
62+
USER_PRINCIPAL_NAME=<USER_PRINCIPAL_NAME>
63+
SECRET_NAME=<SECRET_NAME>
5964
CONTAINER_IMAGE_NAME=<CONTAINER_IMAGE_NAME>
6065
REGISTRY_SERVER=<REGISTRY_SERVER>
6166
REGISTRY_USERNAME=<REGISTRY_USERNAME>
62-
REGISTRY_PASSWORD=<REGISTRY_PASSWORD>
6367
```
6468

65-
(Replace the \<placeholders\> with your values.)
69+
# [Azure PowerShell](#tab/azure-powershell)
70+
71+
```azurepowershell-interactive
72+
$KeyVaultName = "<KEY_VAULT_NAME>"
73+
$UserPrincipalName = "<USER_PRINCIPAL_NAME>"
74+
$SecretName = "<SECRET_NAME>"
75+
$ContainerImageName = "<CONTAINER_IMAGE_NAME>"
76+
$RegistryServer = "<REGISTRY_SERVER>"
77+
$RegistryUsername = "<REGISTRY_USERNAME>"
78+
```
79+
80+
---
81+
82+
### Create key vault
83+
84+
# [Bash](#tab/bash)
85+
86+
```bash
87+
az keyvault create --name $KEY_VAULT_NAME --resource-group $RESOURCE_GROUP
88+
```
89+
90+
# [Azure PowerShell](#tab/azure-powershell)
91+
92+
First make sure you have installed the [KeyVault](https://www.powershellgallery.com/packages/Az.KeyVault) module.
93+
94+
```azurepowershell-interactive
95+
Install-Module Az.KeyVault -Repository PSGallery -Force
96+
```
97+
98+
```azurepowershell-interactive
99+
New-AzKeyVault -Name "$KeyVaultName" -ResourceGroupName "$ResourceGroupName" -Location "$Location"
100+
```
101+
102+
---
103+
104+
### Give your user account permissions to manage secrets in the key vault
105+
106+
# [Bash](#tab/bash)
107+
108+
```bash
109+
KEY_VAULT_ID=$(az keyvault show --name $KEY_VAULT_NAME --query id --output tsv)
110+
az role assignment create --role "Key Vault Secrets Officer" --assignee "$USER_PRINCIPAL_NAME" --scope "$KEY_VAULT_ID"
111+
```
112+
113+
# [Azure PowerShell](#tab/azure-powershell)
114+
115+
```azurepowershell-interactive
116+
$KeyVault=Get-AzKeyVault -VaultName $KeyVaultName
117+
New-AzRoleAssignment -SignInName "$UserPrincipalName" -RoleDefinitionName "Key Vault Secrets Officer" -Scope "$KeyVault.ResourceID"
118+
119+
```
120+
121+
---
122+
123+
### Store registry password
124+
125+
Replace the \<PLACEHOLDERS\> with your values.
126+
127+
# [Bash](#tab/bash)
128+
129+
```bash
130+
az keyvault secret set --vault-name $KEY_VAULT_NAME --name $SECRET_NAME --value "<REGISTRY_PASSWORD>"
131+
```
132+
133+
# [Azure PowerShell](#tab/azure-powershell)
134+
135+
```azurepowershell-interactive
136+
$Secret = ConvertTo-SecureString -String "<REGISTRY_PASSWORD>" -AsPlainText -Force
137+
Set-AzKeyVaultSecret -VaultName "$KeyVaultName" -Name "$SecretName" -SecretValue "$Secret"
138+
```
139+
140+
---
141+
142+
### Retrieve registry password
143+
144+
# [Bash](#tab/bash)
145+
146+
```bash
147+
REGISTRY_PASSWORD=$(az keyvault secret show --name $SECRET_NAME --vault-name $KEY_VAULT_NAME --query value --output tsv)
148+
```
149+
150+
For more information, see
151+
- [Quickstart: Set and retrieve a secret from Azure Key Vault using Azure CLI](../key-vault/secrets/quick-create-cli)
152+
- [Manage Key Vault using the Azure CLI](../key-vault/general/manage-with-cli2.md)
153+
154+
# [Azure PowerShell](#tab/azure-powershell)
155+
156+
```azurepowershell-interactive
157+
$RegistryPassword = Get-AzKeyVaultSecret -VaultName "$KeyVaultName" -Name "$SecretName" -AsPlainText
158+
```
159+
160+
For more information, see
161+
- [Quickstart: Set and retrieve a secret from Azure Key Vault using PowerShell](../key-vault/secrets/quick-create-powershell)
162+
- [Use Azure Key Vault in automation](../../powershell/utility-modules/secretmanagement/how-to/using-azure-keyvault?view=ps-modules)
163+
164+
---
165+
166+
### Create container app
167+
168+
# [Bash](#tab/bash)
169+
170+
For details on how to provide values for any of these parameters to the `create` command, run `az containerapp create --help` or [visit the online reference](/cli/azure/containerapp#az-containerapp-create). To generate credentials for an Azure Container Registry, use [az acr credential show](/cli/azure/acr/credential#az-acr-credential-show).
66171

67172
```azurecli-interactive
68173
az containerapp create \
@@ -77,15 +182,6 @@ az containerapp create \
77182

78183
# [Azure PowerShell](#tab/azure-powershell)
79184

80-
```azurepowershell-interactive
81-
$ContainerImageName = "<CONTAINER_IMAGE_NAME>"
82-
$RegistryServer = "<REGISTRY_SERVER>"
83-
$RegistryUsername = "<REGISTRY_USERNAME>"
84-
$RegistryPassword = "<REGISTRY_PASSWORD>"
85-
```
86-
87-
(Replace the \<placeholders\> with your values.)
88-
89185
```azurepowershell-interactive
90186
$EnvId = (Get-AzContainerAppManagedEnv -ResourceGroupName $ResourceGroupName -EnvName $ContainerAppsEnvironment).Id
91187

0 commit comments

Comments
 (0)