I am following this tutorial from Vault about creating your own certificate authority. I’d like to separate the response (change the output to API call using cURL to see the response) into two distinct files, one file possessing the certificate
and issuing_ca
attributes, the other file containing the private_key
. The tutorial is using jq to parse JSON objects, but my unfamiliarity with jq isn’t helpful here, and most searches are returning info on how to merge JSON using jq.
I’ve tried running something like
vault write -format=json pki_int/issue/example-dot-com
common_name="test.example.com"
ttl="24h"
format=pem
jq -r '.data.certificate, .data.issuing_ca > test.cert.pem
jq -r '.data.private_key' > test.key.pem
or
vault write -format=json pki_int/issue/example-dot-com
common_name="test.example.com"
ttl="24h"
format=pem
| jq -r '.data.certificate, .data.issuing_ca > test.cert.pem
| jq -r '.data.private_key' > test.key.pem
but no dice.
3
Answers
It is not an issue with
jq
invocation, but the way the output files get written. Per your usage indicated, after writing the filetest.cert.pem
, the contents over the read end of the pipe (JSON output) is no longer available to extract theprivate_key
contents.To duplicate the contents over at the write end of pipe, use
tee
along with process substitution. The following should work on bash/zsh or ksh93 and not on POSIX bourne shell shSee this in action
and now observe the contents of both the files.
You could abuse
jq
‘s ability to write to standard error (version 1.6 or later) separately from standard output.There’s a general technique for this type of problem that is worth mentioning
because it has minimal prerequisites (just jq and awk), and because
it scales well with the number of files. Furthermore it is quite efficient in that only one invocation each of jq and awk is needed. The idea is to setup a pipeline of the form: jq … | awk …
There are many variants
of the technique but in the present case, the following would suffice:
Notice that this works even if the items of interest are strings with embedded tabs.