-
-
Save catchdave/69854624a21ac75194706ec20ca61327 to your computer and use it in GitHub Desktop.
# MOVED to public repo: https://github.com/catchdave/ssl-certs/blob/main/replace_synology_ssl_certs.sh |
@telnetdoogie thank you for your quick reply.
I do have 2 certificates, one is my actual certificate. The other one is for quickconnect, but I can't delete that one. I also don't think it is the one causing problems.
But I think I see the problem. The certificate I'm using is RSA, but the old one from synology is RSA/ECC. The webserver is probally using ECC I guess, because the file ECC-cert.pem is still my old certificate. I'm just going to reset the certificates and readd them. But I have reached my request limit so I'll have to wait a bit.
@Daermegil RSA is fine; that's what I'm using too... I did see in my notes that I needed to manually import the LE certificates into DSM before the automation worked reliably.
When you're prompted for Private Key that's the privkey.pem
file. When you're prompted for the Certificate that's cert.pem
. When prompted for the Intermediate Certificate that's fullchain.pem
.
DSM does accept certs without adding the intermediate cert, but it causes some things to fail.
Try manually adding those and see if that works for you. Once the manual add works, the automation should also work.
I think from my memory, if you upload the wrong files (and it'll still appear to work sometimes) in the wrong inputs, it will actually rename things on the filesystem but when the automation works, it's now updating the wrong files. I'm not stating that right but I definitely had some weirdness early on because I wasn't providing the Intermediate cert; instead I was using fullchain.pem
as the Certificate file, which gave me issues with VPN, for example.
@telnetdoogie I added a RSA only certificate and replaced it using the script, everything seems to work brilliantly now!
Thanks for the help!
This appears to miss all "WebStation" pages for me.
I added
# Add WebStation directories
for webstation in /usr/local/etc/certificate/WebStation/*/; do
debug "Found WebStation dir: ${webstation}"
target_cert_dirs+=("${webstation}")
done
But those then weren't re"compiled" into the correct folders nginx uses, so I wrote
# somehow WebStation ends up not getting applied later, so lets manually do it (if it works later it will simply get overwritten)
for ws in /usr/syno/etc/www/certificate/WebStation_*/; do
info "Copying WebStation certificates to '$ws'"
ws_crt=$( grep -o "$ws"cert.conf -Pe '(?<=ssl_certificate\s)\s*.*(?=;$)' | xargs )
ws_key=$( grep -o "$ws"cert.conf -Pe '(?<=ssl_certificate_key\s)\s*.*(?=;$)' | xargs )
ln -f "${certs_src_dir}/"fullchain.pem "$ws_crt"
ln -f "${certs_src_dir}/"privkey.pem "$ws_key"
done
which appears to be sufficient. I still had to add nginx -s reload
to get changes to show up.
Is this really the only way to do this?
In the process I grepped through my /usr
and also saw my cert ended up in /usr/local/etc/certificate/LogCenter/pkg-LogCenter/
somehow, so it might be good to add that path as
target_cert_dirs=(
[...]
"/usr/local/etc/certificate/LogCenter/pkg-LogCenter/")
# Faster ngnix restart (if certs don't appear to be refreshing, change to synosystemctl
if ! /usr/syno/bin/synow3tool --gen-all && sudo systemctl reload nginx; then
warn "nginx failed to restart"
fi
The above lines should change to
if ! /usr/syno/bin/synow3tool --gen-all; then
warn "nginx failed to generate config"
fi
info "Restart nginx"
# Faster ngnix restart (if certs don't appear to be refreshing, change to systemctl restart)
systemctl reload nginx
Otherwise, systemctl reload nginx
will not be executed.
Otherwise,
systemctl reload nginx
will not be executed.
@xiaozhuai: I'm not sure I understand how the above is true (or why you want to separate out the two commands). The intent is that we do not restart if the certs did not generate correctly and instead issue a warning.
systemctl reload nginx
will be executed if and only if the synow3tool --gen-all
command is executed correctly, which is the intent of the original code.
Could you provide an example where this does not work as intended?
Could you provide an example where this does not work as intended?
@catchdave
That's odd.
I try this script on my machine last time and foud the second command never exec.
And I'm sure the /usr/syno/bin/synow3tool --gen-all
is ok.
Confirmed, the script is wrong. You can try below.
#!/usr/bin/env bash
if ! true && echo "Good"; then
echo "Not good"
fi
It prints nothing.
It should be
#!/usr/bin/env bash
if ! (true && echo "Good"); then
echo "Not good"
fi
And it prints Good
.
So the script should change to
# Faster ngnix restart (if certs don't appear to be refreshing, change to systemctl restart)
if ! (/usr/syno/bin/synow3tool --gen-all && systemctl reload nginx); then
warn "nginx failed to restart"
fi
Thanks --you are right, it should have parenthesis! I updated the script. Appreciate the suggestion, @xiaozhuai
Hello @catchdave. Nice script! I want to test it. But I have DS218+ where in all certs directories are not just 3 files which is copied by your script.
There are these structure:
cert.pem chain.pem fullchain.pem info privkey.pem root.pem short-chain.pem
I can create cert.pem chain.pem fullchain.pem privkey.pem
But I haven't idea about root.pem short-chain.pem.
Do you have same structure? Or is it something new? How to solve it? Thx for info.
@raven2cz my advice is to initially manually add your LE cert and make it default. Through the DSM interface, the mapping of LE generated files is:
Private Key ---------------> privkey.pem
Certificate ---------------> cert.pem
Intermediate Certificate --> fullchain.pem
Once that's done and the LE cert is now your default certificate, the script should work fine.
Great script. You may also be interested in my old scripts for managing certificates also for other apps like MailStation and co. Recently I added also synow3tool call to be able to reload nginx properly in DSM7. This script presents jsut a slightly different approach and was mainly developed for DSM6. If you have a reliable way of reloading services in the absence of /usr/local/libexec/certificate.d/$subscriber in DSM7 I would really appreciate
Hey everyone, it's been about a year since I started using this script.
I've never been able to get VPN working with the updated certs automatically (with the script).
The VPNCenter package does restart, and I also tried restarting OpenVPN manually, which also doesn't help. (/var/packages/VPNCenter/target/scripts/openvpn.sh restart)
It seems I always have to manually go over Control Panel --> Security --> Certificate, then with the Settings button I need to change the certificate for "VPN Server", save, and then change it back.
Only then the VPN Server's certificate is "updated".
Anyone here using this script to update VPN Center's certificates?
@footswitch VPN requires the intermediate certificate to be updated, you may have the wrong file names selected, see my comment a few back regarding starting out with manual setup before automation https://gist.github.com/catchdave/69854624a21ac75194706ec20ca61327?permalink_comment_id=4787628#gistcomment-4787628
Synology WILL accept using the fullchain.pem
file as the "Certificate" file, however this causes VPN to fail. You need to use fullchain.pem
only for the Intermediate Certificate, and cert.pem
for "Certificate"
...I only remember this because I ran into the same issue.
#!/bin/bash
# modified version of https://gist.github.com/catchdave/69854624a21ac75194706ec20ca61327
# from https://github.com/catchdave
#
# - Important:
# Before this script can run reliably, you must first manually import your LE certificates into DSM.
# Private Key ---------------> privkey.pem
# Certificate ---------------> cert.pem
# Intermediate Certificate --> fullchain.pem
#
# It's possible to initially import certs WITHOUT adding an Intermediate Cert, and while this works in most cases,
# it will cause OpenVPN on Synology to fail, as it requires the intermediate certs present in fullchain.pem
# You can also add fullchain.pem as the "Certificate" file, which works, but it's important to upload the correct files
# as above, so that the synology certificate sync tool will write the correct contents into the "info" files and associate
# the correct files with the "cert", "chain", and "key".
#
Hi everyone, somewhat related, but has anyone figured out how to do the same for Synology Routers (SRM 1.3)?
@telnetdoogie I'm coming from a win-acme automation, and a while back (a year ago) I was trying to automate with the wrong intermediate file (chain-only from win-acme) and getting all sorts of errors: https://gist.github.com/catchdave/69854624a21ac75194706ec20ca61327?permalink_comment_id=4530953#gistcomment-4530953
-- Then I realised what the right file was: https://gist.github.com/catchdave/69854624a21ac75194706ec20ca61327?permalink_comment_id=4541242#gistcomment-4541242
However, NOW if I try to upload the (full)chain file into the Synology UI, it gives me an error; I can only replace the certificate without error if I choose the chain-only file. So this has suddenly changed and I don't know why or how.
I'm not even talking about the automation itself, I just lost OpenVPN connectivity a couple of months ago and I had to sort this out manually via the UI, and this is what I found.
I'm at a loss here.
I just realized my OpenVPN was broken after cert updates too. OpenVPN copies certs on install to a totally different location, and makes some modifications. Rather than try to fix it I just uninstall and reinstall and the OpenVPN does its thing from the ssl certs and works again.
Switch to wireguard and wg-easy and save yourself the hassle :)
Hello, thank you for your script. I did some modifications to fork of your script, maybe you would like to update yours as well: https://gist.github.com/tfilo/940856c9ef91e6a28f0d310aa899bb2c/revisions
Most important changes:
- In my DSM ActiveBackup and SynologyDrive certs had different owner than root.
- Added missing LogCenter, ReplicationService and kmip
- Added for loop to add all WebStation folders as well
- /usr/syno/bin/synosystemctl restart changed to /usr/syno/bin/synosystemctl try-restart to restarts only active units
@tfilo if you take a look at the version I have you'll see that you don't actually need to manage those destination folders... through testing I found that you just have to replace the certs in the default folder, and /usr/syno/bin/synow3tool --gen-all
will do all of the necessary copying for you to the appropriate locations where DSM otherwise manages certs. No need to add webstation folders etc.
@telnetdoogie thanks for information, I will look at it more but for first try it replaced only 2 out of 19 certificates. All WebStation certs stayed same after /usr/syno/bin/synow3tool --gen-all. Seems like only /usr/syno/etc/certificate/ReverseProxy/, /usr/syno/etc/certificate/system/default/ got replaced with this command. I would like to figure out how synology GUI does it. You select certificates, click next and it changes all certificates at one step. There must be some synology script or utility to update all at once somewhere in system.
@tfilo I see now... that makes sense! I added a 'check' for these other folders in my script too but yeah it'd be super nice to know how it pulls these certs... I know with VPNCenter the certs are copied by DSM on startup of that service, so maybe WebStation does the same thing as well?
@tfilo Yes, I just tested this... my script will check the /usr/local/etc/certificate/
path for certs now as well and as you can see below, my WebStation certs were not matching... restarting WebStation without copying the certs into those locations will re-sync those certificates with the ones in the system default folder. Maybe there's a less intrusive way than a restart but for now that might still be easier than copying the certs to every possible location:
➜ sudo ./check_certs.sh /volume1/docker/certbot/etc_letsencrypt/live/{mydomain}/cert.pem
The following certificates do not match the current version:
- /usr/local/etc/certificate/ScsiTarget/pkg-scsi-plugin-server/cert.pem
- /usr/local/etc/certificate/WebStation/7e8b547e-bd85-4d0e-8cb5-74d09c38e9c3/cert.pem
➜ sudo /usr/syno/bin/synopkg restart WebStation
restart package [WebStation] successfully
➜ sudo ./check_certs.sh /volume1/docker/certbot/etc_letsencrypt/live/hobbs-family.com/cert.pem
The following certificates do not match the current version:
- /usr/local/etc/certificate/ScsiTarget/pkg-scsi-plugin-server/cert.pem
➜
In the case of the above... to sync those certs as well, rather than adding directories, I can just add
SERVICES_TO_RESTART=("pkg-scsi-plugin-server")
PACKAGES_TO_RESTART=("VPNCenter" "WebStation")
AND / OR
Add those folders to TARGET_FOLDERS
in the script.
To resolve the above two, for example, WebStation
re-syncs certs,
pkg-scsi-plugin-server
does not.
So to resolve the case above, minimally I just:
- Add
WebStation
toPACKAGES_TO_RESTART
- Add
pkg-scsi-plugin-server
TOSERVICES_TO_RESTART
- Add
/usr/local/etc/certificate/ScsiTarget/pkg-scsi-plugin-server/
to theTARGET_FOLDERS
That gets all certs synced.
Well thats interesting, it looks like in my case restarting of webstation didn’t update certificates, but maybe i was just too hurry and didn’t let enough time for sync to happen. I will try to investigate it more but not sure if i will find time to play around with it in next few weeks. But i will try to find some time at evenings. Definetly i will let you know with results.
I just made another update too to the script so that when checking for 'non matching' certs (including the new certificate path that you identified - thank you!!) it will output some of the metadata from the info
file if it exists as well. That can help identify Services that could be restarted as a potential "quick fix". So now the output would look like this for non-matching certs:
Warning: Some unmatched certs still exist, in the following locations:
======================================================================
- /usr/local/etc/certificate/WebStation/7e8b547e-bd85-4d0e-8cb5-74d09c38e9c3
- Service : 7e8b547e-bd85-4d0e-8cb5-74d09c38e9c3
- Subscriber : WebStation
- /usr/local/etc/certificate/WebStation/poop
- (info file not found in /usr/local/etc/certificate/WebStation/poop)
...check the script and add these folders to TARGET_FOLDERS for syncing"
or alternatively add appropriate packages to PACKAGES_TO_RESTART or SERVICES_TO_RESTART for those
services that auto-sync certs
- WebStation is an example of a package that will re-sync certs from the system default on restart
this script can then be run again with the '--force' parameter to retry"
I'm not sure how to 'merge' any of my stuff back into @catchdave 's original gist...
I actually found that sudo systemctl reload nginx
line that several commenters said they preferred due to being less intrusive to actually not work for me. I just realized this when all my certs got out of date and was only fixed when I did a "actual ngnix restart".
So my script actually has if ! /usr/syno/bin/synow3tool --gen-all && sudo /usr/syno/bin/synosystemctl restart nginx; then
which I have found to currently work for me.
Perhaps try changing it to restart instead of reload to see if that resolves the mis-matching cert issue. Easy enough to modify to add extra services if you think it needs to be.
It seems that this code overwrites the certificates of all ReverseProxies, is that correct?
# Add reverse proxy app directories
for proxy in /usr/syno/etc/certificate/ReverseProxy/*/; do
debug "Found proxy dir: ${proxy}"
target_cert_dirs+=("${proxy}")
done
It would be better to modify it to overwrite only those using the default certificate.
# Add reverse proxy app directories that using default certificate
for proxy in /usr/syno/etc/certificate/ReverseProxy/*/; do
if cmp -s "${certs_src_dir}/privkey.pem" "${proxy}/privkey.pem" ; then
debug "Found proxy dir: ${proxy}"
target_cert_dirs+=("${proxy}")
fi
done
And I have a question.
When I look at /etc/nginx/sites-enabled/server.ReverseProxy.conf
,
I see that it uses another certificate in a path with "www" added, like the following:
...
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name some.example.com ;
if ( $host !~ "(^some.example.com$)" ) { return 404; }
include /usr/syno/etc/www/certificate/ReverseProxy_f34f22f1-6cee-4707-8768-826a2e85d507/cert.conf*;
include /usr/syno/etc/security-profile/tls-profile/config/ReverseProxy_f34f22f1-6cee-4707-8768-826a2e85d507.conf*;
...
}
...
#> cat /usr/syno/etc/www/certificate/ReverseProxy_f34f22f1-6cee-4707-8768-826a2e85d507/cert.conf
ssl_certificate /usr/syno/etc/www/certificate/ReverseProxy_f34f22f1-6cee-4707-8768-826a2e85d507/f0c77d20-2385-470f-a90b-458aac52504f.pem;
ssl_certificate_key /usr/syno/etc/www/certificate/ReverseProxy_f34f22f1-6cee-4707-8768-826a2e85d507/0e5d3729-671e-4a2a-9b9d-a08d0cdba465.pem;
ssl_certificate /usr/syno/etc/www/certificate/ReverseProxy_f34f22f1-6cee-4707-8768-826a2e85d507/4ffbf2b5-0708-4462-b3f5-f61f72b8ed5b.pem;
ssl_certificate_key /usr/syno/etc/www/certificate/ReverseProxy_f34f22f1-6cee-4707-8768-826a2e85d507/3e1a6a19-a158-4a18-a052-3dc2e6fdce76.pem;
I’m not sure if only I have different paths like this or if they are automatically re-generated.
@bungabear the answer to both is maybe. Is something not working for you?
This script works for me and my reverse proxies—is there a bug or negative symptom you’re experiencing, or is the questions theoretical?
for the www—when you run the script, does your certs work when visiting your custom domains? If not, what is the specific issue?
@Daermegil check in the Certificates pane in DSM and see if you have one certificate set up, and make sure it's this one. It is possible to have multiples and assign a different one to DSM.