|
| 1 | +# Pulumi Azure KMS Encryption |
| 2 | + |
| 3 | +Pulumi allows you to use Azure Keyvault encryption from your cloud provider to encrypt any secrets stored in the backend. |
| 4 | + |
| 5 | +This example shows how this might be done for Azure Keyvault. It creates a storage bucket with a single file that has a "secret" value. |
| 6 | + |
| 7 | +# Getting Started |
| 8 | + |
| 9 | +To use this example, perform the following steps. This examples assumes you have the pulumi-cli installed and the Azure CLI installed. |
| 10 | + |
| 11 | +You should also ensure: |
| 12 | + |
| 13 | + * You azure command line tool installed |
| 14 | + * You are logging in via the `az` command line tool. |
| 15 | + * You have created a resource-group |
| 16 | + * You must have the environment variable `AZURE_KEYVAULT_AUTH_VIA_CLI` set to `true` eg: `export AZURE_KEYVAULT_AUTH_VIA_CLI=true` |
| 17 | + |
| 18 | +## Create an Azure Keyvault Key |
| 19 | + |
| 20 | +```bash |
| 21 | +# First, create a keyvault |
| 22 | +az keyvault create -l westus -n pulumi --resource-group $RESOURCE_GROUP_NAME |
| 23 | + |
| 24 | +# Then, create a key |
| 25 | +az keyvault key create --name pulumi-secret --vault-name pulumi |
| 26 | + |
| 27 | +# Finally, set the relevant permissions on the keyvault |
| 28 | +az keyvault set-policy --name pulumi --object-id $YOUR_OBJECT_ID --key-permissions decrypt get create delete list update import backup restore recover |
| 29 | +``` |
| 30 | + |
| 31 | +_When creating your key, be sure to specify a permissions that restricts access to only those that need to use the key_ |
| 32 | + |
| 33 | +## Initialize your stack |
| 34 | + |
| 35 | +Initialize your stack with Pulumi and ensure you set the `--secrets-provider` flag: |
| 36 | + |
| 37 | +```bash |
| 38 | +# Using your vault and key name |
| 39 | +pulumi stack init $PULUMI_ORG_NAME/$PULUMI_STACK_NAME --secrets-provider="azurekeyvault://pulumi.vault.azure.net/keys/pulumi-secret" |
| 40 | +``` |
| 41 | + |
| 42 | +## Verify your stack settings |
| 43 | + |
| 44 | +If everything has worked as expected, you should be able to verify in your stack settings that the secretsprovider is set: |
| 45 | + |
| 46 | +```bash |
| 47 | +cat Pulumi.$PULUMI_STACK_NAME.yaml |
| 48 | +secretsprovider: azurekeyvault://pulumi.vault.azure.net/keys/pulumi-secret/b636b47f2b474b2a8de3526561eae81b |
| 49 | +encryptedkey: Q2U5a1ZuTWsxLXVWOFdhVEdfaGExdWR1SzhzTlVFMldhWGlxU3RJVVdUWFJBcmM4M1ZlYzZOVVlpU3J2dW1NX2RIelMwV1h4el9hSjFibjcwdjVXcEgxZVlFa2c1LTlGUTBwX2ZnamcyNXh0V2RnYXlKaUNWSzd0VmlhY0ZyT2NCNGJ2SG40NkE4OFR2d0NWVzVEOUZOaUpGNm03TTlLUEl4VC0tbG9fYUJSSUlrZDJuUmNxVTJ2cWxDUjYtdVJYYjJKUjFoTlRYYkNaaEVTUzY4dGtNajZNRXBOQ1k4OGc4d0RTeUVBVGhweEswbUVXc3RaaGUtdnpQdktVY2tFUGFCVkdOaHZHOU1SYU91RWJ6QVZnLUtVdExHYlFHd19vUU15T3I4d3ZvajdJQ0liS0QtUTNLY0h4Q0JsMGNjd1A5ZXNWRUNNQ0tQZGhPY1cySTJwU1BR |
| 50 | + |
| 51 | +``` |
| 52 | + |
| 53 | +## Set your configuration settings |
| 54 | + |
| 55 | +```bash |
| 56 | +pulumi config set azure:location westus |
| 57 | +# Set the bucketname & the secret contents |
| 58 | +pulumi config set bucketName pulumilbriggs |
| 59 | +pulumi config set --secret secretValue "correct-horse-battery-stable" |
| 60 | +``` |
| 61 | + |
| 62 | +## Create the stack |
| 63 | + |
| 64 | +```bash |
| 65 | +# This will create the stack without prompting, be aware! |
| 66 | +Previewing update (azure-keyvault): |
| 67 | + Type Name Plan |
| 68 | + pulumi:pulumi:Stack pulumi-azure-keyvault-azure-keyvault |
| 69 | + + ├─ azure:core:ResourceGroup resourceGroup create |
| 70 | + + ├─ azure:storage:Account storage create |
| 71 | + + ├─ azure:storage:Container container create |
| 72 | + + └─ azure:storage:Blob blob create |
| 73 | + |
| 74 | +Outputs: |
| 75 | + + connectionString: output<string> |
| 76 | + |
| 77 | +Resources: |
| 78 | + + 4 to create |
| 79 | + 1 unchanged |
| 80 | + |
| 81 | +Updating (azure-keyvault): |
| 82 | + Type Name Status |
| 83 | + pulumi:pulumi:Stack pulumi-azure-keyvault-azure-keyvault |
| 84 | + + ├─ azure:core:ResourceGroup resourceGroup created |
| 85 | + + ├─ azure:storage:Account storage created |
| 86 | + + ├─ azure:storage:Container container created |
| 87 | + + └─ azure:storage:Blob blob created |
| 88 | + |
| 89 | +Outputs: |
| 90 | + + connectionString: "DefaultEndpointsProtocol=https;AccountName=pulumilbriggs;AccountKey=Efa63L/xDstQgyvgsYHZqzl3oIlQA4scS4NeX/O1TeBI3mbwMcKxiHIkAGkwJj21EPzHebiuAUM09i7dVv3f/A==;EndpointSuffix=core.windows.net" |
| 91 | + |
| 92 | +Resources: |
| 93 | + + 4 created |
| 94 | + 1 unchanged |
| 95 | + |
| 96 | +Duration: 31s |
| 97 | + |
| 98 | +Permalink: https://app.pulumi.com/jaxxstorm/pulumi-azure-keyvault/azure-keyvault/updates/3 |
| 99 | +``` |
| 100 | + |
| 101 | +You'll notice the secret value is also omitted from the output! |
| 102 | + |
| 103 | +## Verify the encryption |
| 104 | + |
| 105 | +A quick way to verify if the encryption is using the Azure Keyvault key is to remove your application credentials temporarily: |
| 106 | + |
| 107 | +``` |
| 108 | +unset AZURE_KEYVAULT_AUTH_VIA_CLI |
| 109 | +``` |
0 commit comments