Issue
I'd like to replace a variable in a script template by a public and private certificate.
For example, I've generated a harbor.crt
public certificate and a harbor.key
private key with the following command:
sudo openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout /data/harbor.key -out /data/harbor.crt -subj "/CN=$LOCAL_IP" -addext "subjectAltName=IP:127.0.0.1,IP:$LOCAL_IP"
In a template script, I've the following variables I'd like to replace with the above files:
CFG_HARBOR_CRT="CRT" # Harbor registry certificate
CFG_HARBOR_KEY="KEY" # Harbor registry key
To replace those values, I've tried to do something like that:
HARBOR_CRT=`sudo cat /data/harbor.crt`
HARBOR_KEY=`sudo cat /data/harbor.key`
sudo sed -i "s/CFG_HARBOR_CRT\=\"[^\"]*\"/CFG_HARBOR_CRT\=\"$HARBOR_CRT\"/g" ./template-script.sh
sudo sed -i "s/CFG_HARBOR_KEY\=\"[^\"]*\"/CFG_HARBOR_KEY\=\"$HARBOR_KEY\"/g" ./template-script.sh
But both commands failed on: sed: -e expression #1, char 70: unterminated
s' command`
Is there a way to use sed
command with unescaped variables ?
Solution
Pulling out of comments to get better visibility ...
Consider running the files through base64
and embedding the result into the script, then on the other end run base64 -d
to decrypt the data and store in the target files.
Using base64
encoded data should eliminate most (all?) of the sed
headaches of dealing with special characters and/or trying to find a sed
script delimiter that's not in the data.
OP/Manitoba's reply comment:
That did the trick. I used HARBOR_CRT=$(sudo cat /data/harbor.crt | base64 -w 0)
to convert certificate to B64 and echo $CFG_HARBOR_CRT | base64 --decode
to decode.
Answered By - markp-fuso Answer Checked By - Katrina (WPSolving Volunteer)