Skip to content

Commit d4fdb48

Browse files
committed
m6 powershell
1 parent 35259e7 commit d4fdb48

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

m6/1-enable-transit-engine.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# We're going to enable the transit engine and then use it to encrypt some data
2+
3+
# Make sure your Vault server container is up and running
4+
docker container ls
5+
6+
# Let's set our vault env variables
7+
$env:VAULT_ADDR="https://127.0.0.1:8200"
8+
$env:VAULT_SKIP_VERIFY="true"
9+
10+
# Make sure vault isn't sealed
11+
vault status
12+
vault login
13+
14+
# Now let's enable the transit engine at the default path
15+
vault secrets enable transit
16+
17+
# Is there anything to configure here? Let's run path-help to check
18+
vault path-help transit/
19+
20+
# You can configure the caching strategy, which defaults to unlimited
21+
22+
# Let's do some encryption work!
23+
24+
# First we'll create an encryption key to use
25+
vault write -force transit/keys/ccid
26+
27+
# Let's check out some info about the key
28+
vault list transit/keys
29+
vault read transit/keys/ccid
30+
31+
# If you want to know more about these fields, use our friend path-help
32+
vault path-help transit/keys/ccid/config

m6/2-encrypt-data.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Now that we have a key, let's encrypt some plaintext
2+
$plaintext=[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("4444123412341234"))
3+
vault write transit/encrypt/ccid plaintext=$plaintext
4+
5+
# We should capture that in JSON so it's easier to use
6+
$json_data = vault write transit/encrypt/ccid plaintext=$plaintext -format=json | ConvertFrom-Json
7+
8+
# If we want to decrypt the data we need to submit the ciphertext with the version of the key
9+
# And we need to refer to the correct key
10+
vault write transit/decrypt/ccid ciphertext=$($json_data.data.ciphertext)
11+
12+
# That value is still base64 encoded. Let's decode it.
13+
$decrypt_json = vault write transit/decrypt/ccid ciphertext=$($json_data.data.ciphertext) -format=json | ConvertFrom-Json
14+
$decoded = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($decrypt_json.data.plaintext))
15+
Write-Output $decoded

m6/3-rotate-keys.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# What if we want to rotate the encryption keys?
2+
vault write -force transit/keys/ccid/rotate
3+
4+
# Let's get info about the key now
5+
vault read transit/keys/ccid
6+
7+
# We can still decrypt data using the old key
8+
vault write transit/decrypt/ccid ciphertext=$($json_data.data.ciphertext)
9+
10+
# If we try and encrypt a new piece of data, it will use the new key
11+
$plaintext=[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("4444123412341234"))
12+
vault write transit/encrypt/ccid plaintext=$plaintext
13+
14+
# Let's rotate the key a few more times
15+
vault write -force transit/keys/ccid/rotate
16+
17+
# And we'll set the minimum version availble to 2
18+
vault write transit/keys/ccid/config min_decryption_version=2
19+
20+
# If we try and decrypt our old ciphertext now, we'll get an error
21+
vault write transit/decrypt/ccid ciphertext=$($json_data.data.ciphertext)
22+
23+
# The key is still there
24+
vault read transit/keys/ccid
25+
26+
# If we want to purge an older key, we need to set the min encryption version
27+
# and then we can trim the keys
28+
vault write transit/keys/ccid/config min_encryption_version=2
29+
vault write transit/keys/ccid/trim min_available_version=2
30+
31+
# Now the key version 1 is gone and you can't get it back
32+
vault read transit/keys/ccid

m7/1-start-vault-agent.sh

Whitespace-only changes.

0 commit comments

Comments
 (0)