'Select sub portion of sting received from aws secretesmanager
I have multiple related secret stings in aws-secreate manager, in the same secret. I am running a aws cli command to retrieve them aws secretesmanager get-secret-value --secret-id .... However the rustling secretstring looks like this.
"SecreteString": "{\"Test1 <apple>\":\"Secret1\",\"Test2 <banana>\":\"Secret2\"...SecretN\"}",
I am trying to find a method so I can have a variable wanted="Test3 <cherries>" and the script will provide me with the resulting pair Secret3 in this case. The real values do use the <> if it matters.
At this moment I have the following working, to some extent, without the variable
aws secretsmanager ... -query SecreteString| awk -F"${input)|~" '{print $2"~"}'|cut -d \, -f1
resulting in:
\":\"Secret1\"
\":\"SecretN\"}"~
Is there a cleaner way to do all of this?
Even if I continue to go down this path and clean up results I can't help but think there is a simpler way to get the data I need. without multiple cut,awk, or sed commands.
Solution 1:[1]
Try this, to do it with a single sed command:
aws secretsmanager get-secret-value --secret-id ... --query SecretString | sed -e 's/.*'"${input}"'\\":\\"\([[:alnum:]]*\)\\".*"/\1/'
Agreed its ugly and may be hard to read. But it does parse the output with a single unix command instead of multiple cut, awk and sed
% export input="Test1 <apple>"
% aws secretsmanager get-secret-value --secret-id ... --query SecretString | sed -e 's/.*'"${input}"'\\":\\"\([[:alnum:]]*\)\\".*"/\1/'
Secret1
% export input="Test3 <cherries>"
% aws secretsmanager get-secret-value --secret-id ... --query SecretString | sed -e 's/.*'"${input}"'\\":\\"\([[:alnum:]]*\)\\".*"/\1/'
Secret3
% export input="Test2 <banana>"
% aws secretsmanager get-secret-value --secret-id ... --query SecretString | sed -e 's/.*'"${input}"'\\":\\"\([[:alnum:]]*\)\\".*"/\1/'
Secret2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pankaj Saini |
