Search code examples
amazon-web-servicesjqkubectl

How to add quotes to jq output to create a secret to kubectl secret


kubectl -n aqua123 create secret generic aquadocker1234 --from-env-file=<(
    aws secretsmanager get-secret-value --secret-id aqua_secret |
    jq -r '.SecretString' | jq -r 'to_entries | .[] | '.key' + "=" + '.value'')

The output of the jq script is in the form of [email protected]= password1234. The Kubernates Secret is not liking the @ sign in output.I want the output like '[email protected]'= 'password1234' (with quotes).

How can I modify my jq code to get the desired output?


Solution

  • You're using single quotes for your filter, but don't do anything to escape the inner single quotes. You need to do proper escaping to have those single quotes. Within a single quoted string, you would have to close the single quotes, switch to double quotes to add the single quote, then change back. i.e., '"'"'

    ... jq -r '.SecretString | to_entries[] | "'"'"'\(.key)'"'"'='"'"'\(.value)'"'"'"'
    

    If you find yourself needing single quotes frequently, it might be beneficial for you to define some functions in your .jq file.

    def squot: "'";
    def squot($val): "\(squot)\($val|gsub(squot; "\\\(squot)"))\(squot)";
    

    With this, your filter now becomes:

    ... jq -r '.SecretString | to_entries[] | "\(squot(.key))=\(squot(.value))"'