Saturday, December 5, 2026

Automating Let’s Encrypt Wildcard Certificates with OPNsense and TrueNAS

The cryptic post quantum notification made me realize I did not post this when I developed it last year. Enjoy!

Overview

Like many of you, I use Let’s Encrypt (LE) certificates to secure web services across my systems whenever possible. Because these certificates expire every 90 days—and are heading down to 45 days—I looked for a way to automate the renewal process. I didn't find an existing solution I liked, so I built my own. My goal was to manage all of my certificates centrally and push them out to the relevant systems. Generally, I use wildcard certificates for the subdomains in each of my environments, alongside a few specific host certificates.

My physical infrastructure has become fairly streamlined: I run a two-node compute cluster on Proxmox VE (migrated from a single VMware ESXi) paired with TrueNAS SCALE (formally CORE) for storage, linked over a dedicated 40Gbit DAC network. My firewall runs on its own dedicated hardware, and I also maintain other, simplified environments where storage is virtualized on a single PVE host.

To centralize certificate management, I chose to handle renewals directly on OPNsense. Its ACME client is feature-rich and natively supports DNS-01 verification with Cloudflare, where my domains are hosted. Since the firewall is already the most locked-down device on my network, keeping my Cloudflare API keys isolated there made the most sense. (I won't cover the initial ACME setup here, as Dustin over at Home Network Guy already has a great guide on it.)

The next step was distributing the renewed certificates and updating the downstream systems. Rather than running custom deployment scripts directly on my firewall, I configured OPNsense to push the certificates via SFTP to my TrueNAS SCALE box, which handles the heavy lifting of distributing them. (I may offload this job to a dedicated, general-purpose VM or container down the road.) Authentication between these systems is handled via SSH keys which I touch on briefly here;  though plenty of in-depth guides already exist out there.

Configuring the SFTP automation in OPNsense isn't entirely intuitive, so it took a bit of trial and error to get right. Once configured, you just attach the SFTP automation action directly to your Let's Encrypt certificate. I had the the renewal job set to run weekly but have changed it to daily, ensuring it easily catches the standard 2/3 lifetime renewal window. To setup:

On your OPNSense go to Services | ACME Client | Automation

Create a new automation and select Upload certificate via SFTP and configure as you see fit.


For the Host Key, via terminal on the OPNSense run this and paste the entire string returned

 #ssh-keyscan -t ecdsa mytruenas.mydomain.com

For Username, use root unless you create separate accounts for tasks. Be sure to give this account relevant write permissions to the Remote Path.

Put the OPNSense public key into /root/.ssh/authorized_keys (or the user) on your TrueNAS. Or via its GUI at Credentials | Backup Credentials | SSH Key Pairs. You can click Show Identity to get this. Note the entire string as its limited to SFTP use. 

Once complete, click Test Connection to validate, then Save.  

Finally, go to Services | ACME Client | Certificates | Certificates and add it to Automations for the certificate(s).


The main wrapper script compares fingerprints between two hosts—in my setup, it checks OPNsense against TrueNAS, though you can also configure it to check against a local file or another host. If the fingerprints match, the script exits gracefully. If they don't, it runs the update commands you’ve defined at the end of the wrapper script to push changes to your systems. I intentionally kept error handling and notifications to a minimum since I'm constantly swapping out the services I run.

I'll put all my scripts in my GitHub for this article and will add more there as I develop new ones. With the Qualcomm shenanigans around VMWARE, I have moved to ProxMox since starting this, along with Unifi and hardware changes. These scripts were done to be adaptable across environments and versions. If I engaged any AI, I noted it.  

Some systems of Note

Ubiquiti Unifi Controllers

Hands down Glenn R script to do the heavy lifting. I initially ran the VM controller on a VM using his install script which I discuss here, however I tinkered with a CloudKey controller for a while since I was trying out some of their cameras but have centralized on a different brand so am back at a container now for the network controller. You can find his cert update script here which updates all the different Unifi controllers since its done in the middleware. I do have a tiny wrapper that I use to scp the certs and call his script located on the controller.

TrueNAS CORE/SCALE

For this I use danb35 script locally on the TrueNAS as like the Unifi, its done in the middleware and not part of the OS. 

HPE iLO 

Not a fan of HPE so I sold these servers off. Doesn't appear to support import via CLI, only GUI but this option is promising. 

Dell iDrac

Still working on this one as I do not have a Dell PE at the moment. iDRAC7 and up support wildcards. Best I can find is that you have to use the GUI or racadm which is not available on FreeBSD that TrueNAS CORE is built on but TRUENAS SCALE will probably work since its LINUX based. Step 3 in this Dell doc shows how to do it with racadm. Along with another guide. 

VMWare ESXi

Since I formally had one compute host at each site I saw no reason to use Vcenter. This one is really easy and I have a script for it on GitHub.

Windows Server

You really should use Active Directory Certificate Services (ADCS) if you have a domain going as it will faciliate certs for all hosts in your domain and then some. However to see if it works with LE I have the firewall requesting host specific certs for my Windows Servers and have some scripts in GitHub that also sets RDP to use the LE cert. On any DC's a cert will light up secure communication from member computers, even if the cert expires later.

You can’t import an LE key.pem and fullchain.pem directly into the Windows Certificate Store as‑is, because Windows requires certificates with private keys to be in a single, combined container format. I use a simple OpenSSL command to generate this but I do want to wrap it.

 #openssl pkcs12 -export -inkey /path/to/certs/hostname/key.pem -in /path/to/certs/hostname/fullchain.pem -out /path/to/certs/saved/hostname.pfx -password pass:yourpasswordhere
 #chmod 644 /path/to/certs/saved/hostname.pfx

Then two other scripts to import and set SDP to it along with clearing out expired LE certs. I keep the Windows scripts in DFS and the pfx is on TrueNAS as its joined to the domain and I gave perms to the systems to access their cert. 

While I did this on my own more as an experiment, it has worked well, there is a fantastic option in POSHACME.

Mikrotik

I have a script but here are various sources on it.

  • https://github.com/dualmi/letsencrypt-routeros_acme.sh
  • https://github.com/smoix/letsencrypt-mikrotik-cert
  • https://me.sdnix.com/mikrotik-lets-encrypt-wildcard-ssl-certificate/


-Kevin Fason



Friday, April 4, 2025

Back to Basics: Updated USMT Wrapper Script

I recently transitioned to a new role where one of my responsibilities is modernizing EUC functions. A low-hanging fruit I noticed was around how Hardware Refresh is handled, specifically moving data. Due to the type of work that is performed, we cannot use current technologies such as OneDrive to simplify this process even further. Previously, the process involved manually copying user data to a USB stick, then transferring it to the new hardware—a method that's outdated and painfully slow. Enter USMT.

Over a decade ago, I created a USMT shell wrapper so updated this wrapper. Why not rewrite it in PowerShell, you might ask? Speed and simplicity. It was much quicker to update the existing script than to build a new one from scratch in PowerShell. Moreover, this presents a learning opportunity for some of my staff, as I plan to assign them the task of converting the script to PowerShell once we've agreed on the featureset.

What’s Changed?

Being over a decade old, I made several updates and enhancements based on the initial need. As Microsoft has not updated USMT MIG files (it is going away after all). I am leveraging EhlerTech's MIG files, which are outstanding. This update was focused on the USMT version (10.0.22621.1) within ADK 11 version 10.1.22621, as newer ADKs are a little buggy, but other versions should work fine, as its more about the Windows version. Here’s a breakdown of the major updates:

  • Removed XP detection.
  • Forced AMD64 for data capture (this can be toggled back if needed).
  • Added Execution Escalation detection.
  • Enabled the ability to exclude IT support staff escalated accounts from user captures.
  • Expanded store file detection from just the (destination) C drive to multiple drives, supporting USB storage, which can be faster than gigabit networking anymore.
  • Implemented detection for Windows 10 or 11 as the source, ensuring the correct MIG file is used during capture.

  • Introduced a switch to specify a specific sAMAccountName for data capture.

What’s Next?

While these updates mark significant progress, there are additional improvements I'd like to make in the future, particularly once we transition to PowerShell. Some ideas include:

  • Supporting ARM64 captures and destinations if needed.
  • Enhancing USMT store file detection to ignore mapped drives etc.
  • Add UNC error handling
  • Make DOMAIN a variable for easier sharing
  • Add OneDrive Detection
You can find this and the older USMT files located here on GitHub.


Saturday, February 15, 2025

Replace failed ZFS mirror drive in OPNSense

Welcome Back!

It's been a while since I last shared anything. I recently changed jobs and have been busy with that endeavor, but I hope to share more insights from this journey soon.

Encountering SMART Errors after OPNSense Upgrade

Upon upgrading one of my OPNSense instances, I noticed some errors upon restarting one of my drives, ada1. After further investigation, I came across some SMART errors. Although these errors were not enough to trigger a SMART failure, they were still concerning. Even manual short tests returned clean results. Here's what I found when running smartctl -a:

 ID# ATTRIBUTE_NAME          FLAG     VALUE WORST THRESH TYPE      UPDATED  WHEN_FAILED RAW_VALUE
...  
180 Erase_Fail_Count        0x0032   100   100   000    Old_age   Always       -       4185
...
195 Hardware_ECC_Recovered  0x0032   100   099   000    Old_age   Always       -       1676229109
...
SMART Error Log Version: 1
ATA Error Count: 4216 (device log contains only the most recent five errors)
...	
Error 4216 occurred at disk power-on lifetime: 32126 hours (1338 days + 14 hours)
  When the command that caused the error occurred, the device was in an unknown state.
  After command completion occurred, registers were:
  ER ST SC SN CL CH DH
  -- -- -- -- -- -- --
  00 00 00 00 00 00 00

  Commands leading to the command that caused the error were:
  CR FR SC SN CL CH DH DC   Powered_Up_Time  Command/Feature_Name
  -- -- -- -- -- -- -- --  ----------------  --------------------
  ec 00 00 00 00 00 00 00      00:00:09.590  IDENTIFY DEVICE
  ec 00 00 00 00 00 00 00      00:00:09.260  IDENTIFY DEVICE
  f5 00 00 00 00 00 00 00      00:00:09.250  SECURITY FREEZE LOCK
  ec 00 00 00 00 00 00 00      00:00:09.250  IDENTIFY DEVICE
  ec 00 00 00 00 00 00 00      00:00:09.250  IDENTIFY DEVICE
...

This drive is on its way out, so I need to replace it. This particular system is a retasked $30 Barracuda Load Balancer 340 that I upgraded with a new processor and memory. It works great for the use case. Unfortunately, it uses commodity hardware (an MSI customized mainboard), and its manual did not state it supports hotplug, so I had to bring it down to swap it out. Log into the console via your preferred method—I'm using SSH. The first task is to remove the failing drive from the ZFS pool after identifying it.

root@OPNsense:~ # zpool status

pool: zroot
 state: ONLINE
  scan: scan: scrub repaired 0B in 00:00:15 with 0 errors on Wed Feb  5 01:31:15 2025
config:
        NAME        STATE     READ WRITE CKSUM
        zroot       ONLINE       0     0     0
          mirror-0  ONLINE       0     0     0
            ada0p4  ONLINE       0     0     0
            ada1p4  ONLINE       0     0     0
errors: No known data errors 

Since ada1 is failing, the ZFS partition is ada1p4, so we will remove that partition.

 root@OPNsense:~ # zpool detach ada1p4 

Replacing the Drive

As this system does not support hotplug, I then shut it down and swapped the defective drive with a known good one of the same size or larger. The new drive should be clean without any partitions on it. However, in this case, as the second drive, OPNSense will boot back up from ada0, so it's not super important. Your results may vary. Once back up, log back into the console via your preferred method.

Verifying the Partitions

Firstly, we need to verify the partitions because copying the wrong ones could lead to trouble! If your new drive has a partition table this will show it.

root@OPNsense:~ # gpart show

=>       40  500118112  ada0  GPT  (238G)
         40     532480     1  efi  (260M)
     532520       1024     2  freebsd-boot  (512K)
     533544        984        - free -  (492K)
     534528   16777216     3  freebsd-swap  (8.0G)
   17311744  482805760     4  freebsd-zfs  (230G)
  500117504        648        - free -  (324K)

We have four partitions and the partition table to clone to the new disk. We use dd on partitions 1 and 2; however, partitions 3 and 4 are addressed via the relevant tools. Next, we need to turn off swap. Since both partitions are listed in /etc/fstab, we receive an error for the swap located on the now-missing disk.

root@OPNsense:~ # swapoff -a

swapoff: removing /dev/ada0p3 as swap device
swapoff: /dev/ada1p3: No such file or directory

Cloning Partitions

Now comes the potentially dangerous parts, so be VERY careful here. The source drive is ada0, and the new drive is ada1. We will clone the partition table from ada0 to ada1.

root@OPNsense:~ # gpart backup ada0 | gpart restore -F ada1

Next we clone partition 1:

root@OPNsense:~ # dd if=/dev/ada0p1 of=/dev/ada1p1
532480+0 records in
532480+0 records out
272629760 bytes transferred in 22.115694 secs (12327434 bytes/sec)

Then partition 2:

root@OPNsense:~ # dd if=/dev/ada0p2 of=/dev/ada1p2
1024+0 records in
1024+0 records out
524288 bytes transferred in 0.054477 secs (9623964 bytes/sec)

For the ZFS mirror, we use the zpool tool to attach it to the zroot pool as shown by zpool status above.

 root@OPNsense:~ # zpool attach zroot ada0p4 ada1p4

You can verify it’s back to an expected state via zpool status:

 root@OPNsense:~ # zpool status
  pool: zroot
 state: ONLINE
  scan: resilvered 2.29G in 00:00:10 with 0 errors on Sat Feb 15 10:14:39 2025
config:

        NAME        STATE     READ WRITE CKSUM
        zroot       ONLINE       0     0     0
          mirror-0  ONLINE       0     0     0
            ada0p4  ONLINE       0     0     0
            ada1p4  ONLINE       0     0     0

errors: No known data errors

Finally, turn swap back on, which will take care of the third partition.

 root@OPNsense:~ # root@OPNsense:~ # swapon -a
swapon: adding /dev/ada0p3 as swap device
swapon: adding /dev/ada1p3 as swap device

At this point, it would be a good idea to go to the GUI and navigate to System: Settings: Cron, and verify the SMART tasks are configured the way you want.

Along with any ZFS tasks. I only have a monthly scrub due to enabling autotrim per my config articles.