Skip to content

FR: Automatically configure Synology with tailscale cert in Settings : Security/Certificate #4674

Closed

Description

@fatih

What are you trying to do?

I want to secure my Synology NAS (model: DS920+) via HTTPS endpoint. I'm using Synology DSM version: DSM 7.0.1-42218 Update 3.

Synology allows users to specify their own certs. You can find more information here: https://kb.synology.com/en-nz/DSM/help/DSM/AdminCenter/connection_certificate?version=7

I successfully secured my NAS by using Tailscale certs. The steps I've done:

  1. Enable SSH on my Synology
  2. SSH into the machine and run sudo tailscale cert my-domain
  3. Copy the files somewhere I could download to my MacBook (that's needed unfortunately as Synology can't select a cert from its own disk)
  4. Go to the Security/Certificate section and add a new certificate
  5. After adding the certificate, make sure to click on Settings in the Security/Certificate tab to use the new certificate:

ScreenShot-2022-05-14-at-00 52 36@2x

The final result on the Security/Certificate tab should look like:

ScreenShot-2022-05-14-at-00 34 20@2x

After these steps, I could visit my Synology home page and could verify that HTTPS work out of the box:

ScreenShot-2022-05-14-at-00 36 36@2x

How should we solve this?

It would be dope if the Synology Tailscale application could automatically generate and add the certificate to the settings. After that, the users could go to the Security/Certificate tab and enable the certificate for the services they are interested in.

What is the impact of not solving this?

It's cumbersome to set up HTTPS for Synology NAS, see the above steps.

Anything else?

No response

Activity

  1. added
    fr
    on May 13, 2022
  2. added
    L2 Few
    P1 Nuisance
    T0 New feature
    OS-synology
    and removed on May 13, 2022
  3. durind commented on Aug 9, 2022

    @durind

    Hi,

    This could be done with the feature I requested in #5011 and for example with a script like https://github.com/looran/synoadm/blob/main/synoadm.sh

  4. changed the title [-]FR: Automatically configure Synology with `tailscale cert` output[/-] [+]FR: Automatically configure Synology with `tailscale cert` in Settings : Security/Certificate[/+] on Aug 28, 2022
  5. maxenceleduc92 commented on Oct 17, 2022

    @maxenceleduc92

    Hi,
    I think I got something that sucessfully updates tailscale certificates on Synology.

    #!/bin/sh
    
    tailscale cert xxxx.xxxx.ts.net
    openssl pkcs8 -topk8 -nocrypt -in xxxx.xxxx.ts.net.key -out p8file.pem
    
    cp xxxx.xxxx.ts.net.crt /usr/syno/etc/certificate/_archive/IUou7F/cert.pem
    cp xxxx.xxxx.ts.net.crt /usr/syno/etc/certificate/_archive/IUou7F/fullchain.pem
    cp p8file.pem /usr/syno/etc/certificate/_archive/IUou7F/privkey.pem
    
    /usr/syno/bin/synosystemctl restart nginx
    

    You only need to replace xxxx.xxxx.ts.net by your own domain. The IUou7F folder might be different for everyone as well. If your tailscale certificate is configured as default, you can check the folder running
    sudo cat /usr/syno/etc/certificate/_archive/DEFAULT

  6. taglia commented on Dec 4, 2022

    @taglia

    Hi,

    I ended here looking for a way to automatically replace the tailscale certificate on Synology. Thank you so much @maxenceleduc92 for your script! I run a slight variation of it as a daily task. I had only one problem with it: for some reason, restarting nginx on Synology triggers a chain reaction of restarts (my problem was with docker restarting, while my containers might be doing something important).

    To the best of my knowledge, reloading nginx is sufficient to switch to the new certificate, so I have replaced the last line with: /usr/syno/bin/synosystemctl reload nginx. This does not trigger a restart for Docker, Web Station, etc.

  7. benjamin-hg commented on Dec 29, 2022

    @benjamin-hg

    Thanks a lot, @maxenceleduc92!

    To safe people like me some time figuring out how to schedule this script:
    Use the Task Scheduler of DMS (https://kb.synology.com/en-global/DSM/help/DSM/AdminCenter/system_taskscheduler?version=7), choose "user-defined script" and user "root" (admin won't work!)

    You may also want to send an email in case the script fails (there's a checkbox for it)

    BTW I used the reload nginx variant of @taglia and altough my DSM listed the updated certificate, my browser did not want to show it unless I restarted my box. As I'm running the script on a daily basis and also restarting my NAS every day, this is not an issue with a 3 months certicate lifetime.

  8. pjdubya commented on Jan 1, 2023

    @pjdubya

    @maxenceleduc92 you are amazing. Thank you so much!

    Only thing I would suggest also doing at the end of the script is to delete the xxxx.xxxx.ts.net.crt, xxxx.xxxx.ts.net.key and p8file.pem files from the default directory that they get created in. It's probably pretty low risk but better to clean up to be sure. Here's a slight revision to the earlier script that'll do that for you.

    #!/bin/sh
    
    TEMPDIR=~/tailscale_cert_update
    mkdir $TEMPDIR
    tailscale cert --cert-file $TEMPDIR/xxxx.xxxx.ts.net.crt --key-file $TEMPDIR/xxxx.xxxx.ts.net.key xxxx.xxxx.ts.net
    openssl pkcs8 -topk8 -nocrypt -in $TEMPDIR/xxxx.xxxx.ts.net.key -out $TEMPDIR/p8file.pem
    
    cp $TEMPDIR/xxxx.xxxx.ts.net.crt /usr/syno/etc/certificate/_archive/IUou7F/cert.pem
    cp $TEMPDIR/xxxx.xxxx.ts.net.crt /usr/syno/etc/certificate/_archive/IUou7F/fullchain.pem
    cp $TEMPDIR/p8file.pem /usr/syno/etc/certificate/_archive/IUou7F/privkey.pem
    
    /usr/syno/bin/synosystemctl restart nginx
    
    rm -rf $TEMPDIR
    

    BTW has anyone figured out how to use tailscale certs with DSM 6? I get an error when adding the cert initially via the DSM 6 UI saying "DSM does not support ECC certificates" and I can't figure out a workaround. Is this really only possible in DSM 7?

  9. rderewianko commented on Jan 3, 2023

    @rderewianko

    I've edited the script above a bit (also because i'll end up using the ssl certificate within docker)

    #!/bin/sh
    
    TEMPDIR=~/tailscale_cert_update
    TS_DNS="<yourtsdnshere>"
    SYNO_ID=$(cat /usr/syno/etc/certificate/_archive/DEFAULT)
    mkdir $TEMPDIR
    
    
    # Configure certificate for Synology
    tailscale cert --cert-file $TEMPDIR/$TS_DNS.crt --key-file $TEMPDIR/$TS_DNS.key $TS_DNS
    openssl pkcs8 -topk8 -nocrypt -in $TEMPDIR/$TS_DNS.key -out $TEMPDIR/p8file.pem
    
    cp $TEMPDIR/$TS_DNS.crt /usr/syno/etc/certificate/_archive/$SYNO_ID/cert.pem
    cp $TEMPDIR/$TS_DNS.crt /usr/syno/etc/certificate/_archive/$SYNO_ID/fullchain.pem
    cp $TEMPDIR/p8file.pem /usr/syno/etc/certificate/_archive/$SYNO_ID/privkey.pem
    
    # Create dir for tailscale in ssl
    mkdir  -p /etc/ssl/tailscale
    cp $TEMPDIR/$TS_DNS.crt /etc/ssl/tailscale/
    cp $TEMPDIR/$TS_DNS.key /etc/ssl/tailscale/
    /usr/syno/bin/synosystemctl reload nginx
    
    #Clean up
    rm -rf $TEMPDIR 
    
  10. af3556 commented on Jul 1, 2023

    @af3556

    FYI Hyper Backup on DSM 7.2 fails to accept a Tailscale certificate (you get a highly uninformative and incorrect "network is not working" error message from the backup source; a packet capture on the Vault (destination) NAS shows it doing a TCP RST after a short exchange). Switching the Vault TLS certificate to use the Synology 'QuickConnect' TLS cert gets it going again. The Tailscale cert continues to function just fine for HTTP access (i.e. System Default).

    i.e. this seems to work:
    image

  11. 14 remaining items

  12. martijk commented on Mar 8, 2024

    @martijk

    I have found a CLI tool which can be used to invoke the Synology web API as an admin without credentials (it does have to be run as root though). I suspect that will be more future-proof than copying files around in the internal directories. My PR #10994 packages this all up into one tailscale CLI command for convenience, but as a workaround in the meantime, here's an adaptation of the script from @tux86 that uses the synowebapi command.

    #!/bin/bash
    
    # Exit if not running as root
    if [ "$EUID" -ne 0 ]; then
      echo "This script must be run with sudo."
      exit 1
    fi
    
    USER_HOME=$(eval echo ~$SUDO_USER)
    TEMPDIR="$USER_HOME/.tailscale_certs"
    TS_DNS=$(tailscale status --json | jq -r '.Self.DNSName | .[:-1]')
    SYNO_ID=$(cat /usr/syno/etc/certificate/_archive/DEFAULT)  # We will overwrite the default certificate
    
    # Prepare temporary cert directory
    rm -f "$TEMPDIR/$TS_DNS*.pem"
    mkdir -p "$TEMPDIR"
    
    # Generate Tailscale certificates
    tailscale cert --cert-file "$TEMPDIR/$TS_DNS.crt.pem" --key-file "$TEMPDIR/$TS_DNS.key.pem" "$TS_DNS"
    
    # Import the certificates via the Synology web API.
    /usr/syno/bin/synowebapi --exec api="SYNO.Core.Certificate" method=import version=1 \
      id="\"$SYNO_ID\"" desc="\"Tailnet Certificate\"" \
      cert_tmp="\"$TEMPDIR/$TS_DNS.crt.pem\"" \
      key_tmp="\"$TEMPDIR/$TS_DNS.key.pem\""
    
    # Restart the Synology web server
    /usr/syno/bin/synosystemctl restart nginx
    
    # Clean up temporary directory
    rm -rf "$TEMPDIR"

    With credit to https://github.com/zaxbux/syno-acme/blob/main/acme.sh/deploy/synology_dsm_local.sh for helping figure out the undocumented parameters to synowebapi

    This is really great, thanks for sharing!

    Wouldn't it suffice to reload nginx rather than restarting it? Also, I feel like synowebapi might be able to take care of activating the new certificate, including restarting/reloading services. It returns a JSON suggesting that it could at least restart httpd:

    {
       "data" : {
          "id" : "t7UVwb",
          "restart_httpd" : false
       },
       "httpd_restart" : false,
       "success" : true
    }

    I'll see if I can verify that.

  13. martijk commented on Mar 8, 2024

    @martijk

    I have found a CLI tool which can be used to invoke the Synology web API as an admin without credentials (it does have to be run as root though). I suspect that will be more future-proof than copying files around in the internal directories. My PR #10994 packages this all up into one tailscale CLI command for convenience, but as a workaround in the meantime, here's an adaptation of the script from @tux86 that uses the synowebapi command.

    #!/bin/bash
    
    # Exit if not running as root
    if [ "$EUID" -ne 0 ]; then
      echo "This script must be run with sudo."
      exit 1
    fi
    
    USER_HOME=$(eval echo ~$SUDO_USER)
    TEMPDIR="$USER_HOME/.tailscale_certs"
    TS_DNS=$(tailscale status --json | jq -r '.Self.DNSName | .[:-1]')
    SYNO_ID=$(cat /usr/syno/etc/certificate/_archive/DEFAULT)  # We will overwrite the default certificate
    
    # Prepare temporary cert directory
    rm -f "$TEMPDIR/$TS_DNS*.pem"
    mkdir -p "$TEMPDIR"
    
    # Generate Tailscale certificates
    tailscale cert --cert-file "$TEMPDIR/$TS_DNS.crt.pem" --key-file "$TEMPDIR/$TS_DNS.key.pem" "$TS_DNS"
    
    # Import the certificates via the Synology web API.
    /usr/syno/bin/synowebapi --exec api="SYNO.Core.Certificate" method=import version=1 \
      id="\"$SYNO_ID\"" desc="\"Tailnet Certificate\"" \
      cert_tmp="\"$TEMPDIR/$TS_DNS.crt.pem\"" \
      key_tmp="\"$TEMPDIR/$TS_DNS.key.pem\""
    
    # Restart the Synology web server
    /usr/syno/bin/synosystemctl restart nginx
    
    # Clean up temporary directory
    rm -rf "$TEMPDIR"

    With credit to https://github.com/zaxbux/syno-acme/blob/main/acme.sh/deploy/synology_dsm_local.sh for helping figure out the undocumented parameters to synowebapi

    This is really great, thanks for sharing!

    Wouldn't it suffice to reload nginx rather than restarting it? Also, I feel like synowebapi might be able to take care of activating the new certificate, including restarting/reloading services. It returns a JSON suggesting that it could at least restart httpd:

    {
       "data" : {
          "id" : "t7UVwb",
          "restart_httpd" : false
       },
       "httpd_restart" : false,
       "success" : true
    }

    I'll see if I can verify that.

    The value of restart_httpd depends on some factors unknown to me, but you don't need to specify anything, DSM will just auto-detect it. I've also been able to confirm that you don't need to restart anything manually, at least not for my use case. I've first replaced the certificate with a bogus one in the web GUI, which gave me a certificate error in my browser after refreshing the page, and then I ran the script to generate and import the correct certificate again without touching nginx, and it worked immediately. It returned this:

    {
       "data" : {
          "id" : "oFvj7Y",
          "restart_httpd" : true
       },
       "httpd_restart" : true,
       "success" : true
    }

    Note that the ID is different from the one in my previous comment, but that's because I used another device to test this.

    I use this modified version of the script to run a weekly task as root with the task scheduler:

    #!/bin/bash
    
    # Exit on error
    set -e
    
    # Exit if not running as root
    if [ "$EUID" -ne 0 ]; then
      echo "This script must be run as root." >&2
      exit 1
    fi
    
    # Prepare temporary cert directory
    readonly TEMPDIR=$(mktemp -d)
    trap "rm -rf -- '$TEMPDIR'" EXIT
    chmod 0700 "$TEMPDIR"
    
    readonly TS_DNS=$(tailscale status --json | jq -r '.Self.DNSName | .[:-1]')
    if [ -z "$TS_DNS" ]; then
      echo "Couldn't determine the Tailscale hostname." >&2
      exit 1
    fi
    
    readonly SYNO_ID=$(</usr/syno/etc/certificate/_archive/DEFAULT)  # We will overwrite the default certificate
    if [ -z "$SYNO_ID" ]; then
      echo "Unable to determine the default certificate ID." >&2
      exit 1
    fi
    
    # Generate Tailscale certificates
    tailscale cert --cert-file "$TEMPDIR/$TS_DNS.crt.pem" --key-file "$TEMPDIR/$TS_DNS.key.pem" "$TS_DNS"
    
    # Import the certificates via the Synology web API.
    /usr/syno/bin/synowebapi --exec api="SYNO.Core.Certificate" method=import version=1 \
      id="\"$SYNO_ID\"" desc="\"Tailnet Certificate\"" \
      cert_tmp="\"$TEMPDIR/$TS_DNS.crt.pem\"" \
      key_tmp="\"$TEMPDIR/$TS_DNS.key.pem\""
  14. added a commit that references this issue on Apr 5, 2024
    306bacc
  15. alexraskin commented on Jun 19, 2024

    @alexraskin
  16. added a commit that references this issue on Jul 31, 2024
    9ce9197
  17. sim642 commented on Aug 11, 2024

    @sim642

    Since it may not be entirely obvious to everyone, PR #10994 implemented an official command to do this, so custom scripts are no longer necessary.
    However, for some reason the new command is nowhere documented (except for the PR), so I wrote my own guide here: https://sim642.eu/blog/2024/08/11/tailscale-https-certificate-on-synology-nas/. Hopefully, it also gets mentioned in the official Tailscale documentation at some point.

  18. jdumas commented on Aug 11, 2024

    @jdumas

    Isn't it gonna take a while for the Tailscale synology package to be updated to support this magic command though?

  19. tux86 commented on Aug 11, 2024

    @tux86

    Since it may not be entirely obvious to everyone, PR #10994 implemented an official command to do this, so custom scripts are no longer necessary. However, for some reason the new command is nowhere documented (except for the PR), so I wrote my own guide here: https://sim642.eu/blog/2024/08/11/tailscale-https-certificate-on-synology-nas/. Hopefully, it also gets mentioned in the official Tailscale documentation at some point.

    it works ! thank you for sharing.

    I have tailscale v 1.70 installed on my Synology:
    image

    Command documentation (cli help) :
    image

    As detailed in @sim642 article, here are the instructions for automating certificate renewal.

    Step 1:

    image

    Step 2:

    image

    Step 3:

    image

    Step 4:

    Screenshot 2024-08-11 at 21 20 23

  20. flammable commented on Aug 11, 2024

    @flammable

    Confirming this works great on 1.70.0. Thanks for writing up the instructions!

  21. kurrik commented on Aug 11, 2024

    @kurrik

    Isn't it gonna take a while for the Tailscale synology package to be updated to support this magic command though?

    My Synology package manager said my 1.58.2 install was "up to date", but SSH'ing into my Synology and running sudo tailscale update upgraded it to 1.70.0, which was enough to make the rest of the instructions work.

  22. xiNeRoar commented on Aug 23, 2024

    @xiNeRoar

    Since it may not be entirely obvious to everyone, PR #10994 implemented an official command to do this, so custom scripts are no longer necessary. However, for some reason the new command is nowhere documented (except for the PR), so I wrote my own guide here: https://sim642.eu/blog/2024/08/11/tailscale-https-certificate-on-synology-nas/. Hopefully, it also gets mentioned in the official Tailscale documentation at some point.

    it works ! thank you for sharing.

    I have tailscale v 1.70 installed on my Synology: image

    Command documentation (cli help) : image

    As detailed in @sim642 article, here are the instructions for automating certificate renewal.

    Step 1:

    image

    Step 2:

    image

    Step 3:

    image

    Step 4:

    Screenshot 2024-08-11 at 21 20 23

    Will this apply https to the short domain as well? Or it only apply to the fqdn?

  23. sim642 commented on Aug 23, 2024

    @sim642

    Will this apply https to the short domain as well? Or it only apply to the fqdn?

    The general Tailscale HTTPS documentation answers this:

    You cannot obtain an HTTPS URL to navigate to a bare hostname, such as https://machine-name. If you obtain a TLS certificate for a node using MagicDNS, it will be accessible at both https://machine-name.tailNNNN.ts.net, using HTTPS, and also at http://machine-name, without HTTPS but using MagicDNS as a DNS nameserver.

  24. added a commit that references this issue on Aug 23, 2024
    9272964
  25. mback2k commented on Aug 28, 2024

    @mback2k

    There is currently also an issue with the certificate generated, please see: #12762

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    L2 FewOS-synologyP1 NuisanceT0 New featurefr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions