Migrating secrets between two Vault instances

Featured image for sharing metadata for article

I've got a number of secrets I need to move across two Vault instances, which could be done manually, but I wanted to try and simplify with a lil' bit of automation.

As a starting point, I asked GPT-4o (via Copilot) which set me on the right track of being able to vault kv put ... @file.json, if the file.json is correctly set up.

With this in mind, I then wrote the following script:

SOURCE_VAULT_ADDR=https://vault-one.example.com
SOURCE_VAULT_PREFIX=kv/shared/all-applications

TARGET_VAULT_ADDR=https://vault-one.example.localhost
TARGET_VAULT_PREFIX=kv/some/other/path

for secret in "admin_creds" "docker_registry" "vendor_api_key"; do
	out="$(mktemp)"
	# NOTE that if you want to use a source Vault with a non-KV engine, you'll need to amend this
	env VAULT_ADDR="$SOURCE_VAULT_ADDR" VAULT_TOKEN="$SOURCE_VAULT_TOKEN" vault kv get -format=json -field=data "$SOURCE_VAULT_PREFIX/$secret" > "$out"

	# NOTE that if you want to use a target Vault with a non-KV engine, you'll need to amend this
	env VAULT_ADDR="$TARGET_VAULT_ADDR" VAULT_TOKEN="$TARGET_VAULT_TOKEN" vault kv put "$TARGET_VAULT_PREFIX/$secret" @"$out"

	# and make sure we delete the plaintext secrets
	shred "$out"
	rm "$out"
done

This would then allow being run like so:

env SOURCE_VAULT_TOKEN=hvs.xxxx TARGET_VAULT_TOKEN=hvs.xxxx ./mv-secrets.sh

This then writes them into the new locations πŸš€

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #vault.

πŸ€– Content in this blog post (prose or code snippets) includes code derived from the following LLMs:

  • gpt:4o

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.