|
| 1 | +# Vault on GCE Example |
| 2 | + |
| 3 | +**Figure 1.** *diagram of Google Cloud resources* |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Create the Cloud KMS KeyRing for asset encryption: |
| 8 | + |
| 9 | +Cloud KMS is used to encrypt assets like the Vault unseal keys and TLS certificates so they can be securely stored in a Cloud Storage bucket. |
| 10 | + |
| 11 | +Create the key ring and encryption key: |
| 12 | + |
| 13 | +``` |
| 14 | +gcloud kms keyrings create vault --location global |
| 15 | +
|
| 16 | +gcloud kms keys create vault-init --location global --keyring vault --purpose encryption |
| 17 | +``` |
| 18 | + |
| 19 | +## Set up the environment |
| 20 | + |
| 21 | +Enable the following Google Cloud APIs before continuing: |
| 22 | + |
| 23 | +- Google Compute Engine API |
| 24 | +- Google Cloud Storage |
| 25 | +- Google Cloud Key Management Service (KMS) API |
| 26 | +- Google Identity and Access Management (IAM) API |
| 27 | + |
| 28 | +``` |
| 29 | +gcloud auth application-default login |
| 30 | +export GOOGLE_PROJECT=$(gcloud config get-value project) |
| 31 | +``` |
| 32 | + |
| 33 | +Add the project ID, bucket name and KeyRing name to the `terraform.tfvars` file: |
| 34 | + |
| 35 | +``` |
| 36 | +export GOOGLE_PROJECT=$(gcloud config get-value project) |
| 37 | +cat - > terraform.tfvars <<EOF |
| 38 | +project_id = "${GOOGLE_PROJECT}" |
| 39 | +storage_bucket = "${GOOGLE_PROJECT}-vault" |
| 40 | +kms_keyring_name = "vault" |
| 41 | +EOF |
| 42 | +``` |
| 43 | + |
| 44 | +## Deploy Vault |
| 45 | + |
| 46 | +``` |
| 47 | +terraform init |
| 48 | +terraform plan |
| 49 | +terraform apply |
| 50 | +``` |
| 51 | + |
| 52 | +After a few minutes, the Vault instance will be ready. |
| 53 | + |
| 54 | +## SSH Into Vault Instnace |
| 55 | + |
| 56 | +Use SSH to connect to the Vault instance: |
| 57 | + |
| 58 | +``` |
| 59 | +gcloud compute ssh $(gcloud compute instances list --limit=1 --filter=name~vault- --uri) -- sudo bash |
| 60 | +``` |
| 61 | + |
| 62 | +> Note: the remainder of the commands will be run from within this SSH session. |
| 63 | +
|
| 64 | +## Initialize Vault |
| 65 | + |
| 66 | +Obtain the unseal keys from Cloud Storage and decrypt them using Cloud KMS: |
| 67 | + |
| 68 | +```shell |
| 69 | +export GOOGLE_PROJECT=$(gcloud config get-value project) |
| 70 | +gcloud kms decrypt \ |
| 71 | + --location=global \ |
| 72 | + --keyring=vault \ |
| 73 | + --key=vault-init \ |
| 74 | + --plaintext-file=/dev/stdout \ |
| 75 | + --ciphertext-file=<(gsutil cat gs://${GOOGLE_PROJECT}-vault-assets/vault_unseal_keys.txt.encrypted) |
| 76 | +``` |
| 77 | + |
| 78 | +The output will look like the following: |
| 79 | + |
| 80 | +``` |
| 81 | +Unseal Key 1: oO1UNH4TPVZRFuGWUa9D0eciJ2LMMgi2PYxm/bLL/lt0 |
| 82 | +Unseal Key 2: +4q3O9LT46p22uTcDTYZyIVvVt+mxhB8OQ87vZFc3pkp |
| 83 | +Unseal Key 3: tFnuYrDD1Xgkec3wFXhk93wIjEfq3kCOD34i16MkE+pl |
| 84 | +Unseal Key 4: DFQhkl344Z+jpwr9L/looYjNYPAh8/UKGF5fXAO2Vj0W |
| 85 | +Unseal Key 5: XOQVAZCKt6njWcF6IAP19ER1WnRqhH5MllyvcywBLtaw |
| 86 | +Initial Root Token: 8d9b6907-0386-c422-cad8-624ceba2d0ae |
| 87 | +``` |
| 88 | + |
| 89 | +Unseal Vault |
| 90 | + |
| 91 | +``` |
| 92 | +vault unseal |
| 93 | +``` |
| 94 | + |
| 95 | +> Run the command above at least 3 times, providing a different unseal key when prompted to unseal Vault. |
| 96 | +
|
| 97 | +Verify Vault is unsealed: |
| 98 | + |
| 99 | +``` |
| 100 | +vault status |
| 101 | +``` |
| 102 | + |
| 103 | +Authenticate to Vault as root: |
| 104 | + |
| 105 | +``` |
| 106 | +vault auth ROOT_TOKEN |
| 107 | +``` |
| 108 | + |
| 109 | +## Configure GCP Auth Backend |
| 110 | + |
| 111 | +Enable GCP auth backend: |
| 112 | + |
| 113 | +``` |
| 114 | +vault auth-enable gcp |
| 115 | +``` |
| 116 | + |
| 117 | +Configure GCP backend: |
| 118 | + |
| 119 | +``` |
| 120 | +vault write auth/gcp/config credentials=@/etc/vault/gcp_credentials.json |
| 121 | +``` |
| 122 | + |
| 123 | +## Create a Vault role and login with signed JWT |
| 124 | + |
| 125 | +Create a Vault role named `dev-role`: |
| 126 | + |
| 127 | +``` |
| 128 | +GOOGLE_PROJECT=$(gcloud config get-value project) |
| 129 | +vault write auth/gcp/role/dev-role \ |
| 130 | + type="iam" \ |
| 131 | + project_id="${GOOGLE_PROJECT}" \ |
| 132 | + policies="default" \ |
| 133 | + service_accounts="vault-admin@${GOOGLE_PROJECT}.iam.gserviceaccount.com" |
| 134 | +``` |
| 135 | + |
| 136 | +Get a signed JWT for the `dev-role`: |
| 137 | + |
| 138 | +``` |
| 139 | +GOOGLE_PROJECT=$(gcloud config get-value project) |
| 140 | +SERVICE_ACCOUNT=vault-admin@${GOOGLE_PROJECT}.iam.gserviceaccount.com |
| 141 | +cat - > login_request.json <<EOF |
| 142 | +{ |
| 143 | + "aud": "vault/dev-role", |
| 144 | + "sub": "${SERVICE_ACCOUNT}", |
| 145 | + "exp": $((EXP=$(date +%s)+600)) |
| 146 | +} |
| 147 | +EOF |
| 148 | +``` |
| 149 | + |
| 150 | +``` |
| 151 | +JWT_TOKEN=$(gcloud beta iam service-accounts sign-jwt login_request.json signed_jwt.json --iam-account=${SERVICE_ACCOUNT} && cat signed_jwt.json) |
| 152 | +``` |
| 153 | + |
| 154 | +Login to Vault with the signed JWT: |
| 155 | + |
| 156 | +``` |
| 157 | +curl -s ${VAULT_ADDR}/v1/auth/gcp/login -d '{"role": "dev-role", "jwt": "'${JWT_TOKEN}'"}' | jq -r '.auth.client_token' > ~/.vault-token |
| 158 | +``` |
| 159 | + |
| 160 | +Test access by writing and reading a value to the cubbyhole |
| 161 | + |
| 162 | +``` |
| 163 | +vault write /cubbyhole/hello value=world |
| 164 | +vault read /cubbyhole/hello |
| 165 | +``` |
| 166 | + |
| 167 | +Expected output: |
| 168 | + |
| 169 | +``` |
| 170 | +Key Value |
| 171 | +--- ----- |
| 172 | +value world |
| 173 | +``` |
0 commit comments