Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HASHICORP VAULT: How to read multiple items from a file and write them to Vault

I am able to write values to Vault from a json file :

# cat secrets.json 
{ "value": "bGktzwatc" }
{ "value": "AGktzwatB" }

While trying to create a new value by reading from json file, Vault is reading only the first value from the file :

# ./vault write secret/passwd1 @secrets.json
Success! Data written to: secret/passwd1
# ./vault read secret/passwd1
Key                 Value
---                 -----
refresh_interval    768h0m0s
value               bGktzwatc

Is it possible to read multiple values from a file and write to different keys through vault?

My Requirement is adding values to the multiple keys by reading from a file:

Key                 Value
---                 -----
refresh_interval    768h0m0s
value               bGktzwatc

Key                 Value
---                 -----
refresh_interval    768h0m0s
value               AGktzwatB
like image 624
Here_2_learn Avatar asked Apr 12 '17 07:04

Here_2_learn


2 Answers

AFAIK, you cannot so this, as vault write command expects key to be specified as part of the command.

When trying > vault write @data.json it looks like it doesn't matter what the file contains at all, as instead of wrong file format kind of error there is the general output about available parameters for command.


Maybe it will be helpful: you can specify more than one value for specific key:

# cat secrets.json 
{ "value1": "bGktzwatc", "value2": "AGktzwatB" }

then key will contains

Key                 Value
---                 -----
refresh_interval    768h0m0s
value1              bGktzwatc
value2              AGktzwatB
like image 167
Set Avatar answered Nov 12 '22 05:11

Set


If you are copying data from one key to another key via a file, this approach works for me:

vault read -format=json secret/mykey1 > file.json
cat file.json | jq '.data' | vault write secret/mykey2 -

Note the use of jq to bring the "data" sub-element to the top level first.

like image 4
Yuri Astrakhan Avatar answered Nov 12 '22 05:11

Yuri Astrakhan