I'm working on automating the certificate renewal and installation in my web apps. This particular one is qBittorrent. It stores the SSL certificate/private key in a config file, like so
...
WebUI\AuthSubnetWhitelistEnabled=false
WebUI\HTTPS\Certificate=@ByteArray(-----BEGIN CERTIFICATE-----\n0GCSqGSIb3DQEBCwUA\nMEoxCzAQaih7W\n-----END CERTIFICATE-----)
WebUI\HTTPS\Enabled=true
WebUI\HTTPS\Key="@ByteArray(-----BEGIN PRIVATE KEY-----\nFAASdoONpV\nBvX+hUcjAne0PkZV9d7NG1QvipW+MPa4N12i+aRmwK\nhACATS21MMnBFTRGSfzwLy4=\n-----END PRIVATE KEY-----)"
WebUI\LocalHostAuth=true
...
I need to be able to parse the file and replace the certificate and private key strings. After spending a few days on this, I got close
sed -i -E "s/(WebUI\\\\HTTPS\\\\Certificate=@ByteArray\().+(\))/\1foo\2/g" /home/qbtuser/.config/qBittorrent/qBittorrent.conf
This works and replaces the certificate string with foo
. However, when I read the actual cert from a file and substitute it, it fails:
cert=$(<$certPath)
sed -i -E "s/(WebUI\\\\HTTPS\\\\Certificate=@ByteArray\().+(\))/\1${cert}\2/g" /home/qbtuser/.config/qBittorrent/qBittorrent.conf
sed: -e expression #1, char 78: unterminated `s' command
Apparently it doesn't like newlines in the certificate file. If I set the cert
to one-line value like foo
, it works. But multiline doesn't work.
How do I make this work?