caddy: No matching certificate to load: decoding certificate metadata: invalid character '}'

Caddy v2.8.4 started printing an error for one domain:

Jul 27 12:06:59 caddy-proxy-sbg7 caddy[1208]: {"level":"info","ts":1722074819.7958932,"logger":"tls.on_demand","msg":"obtaining new certificate","remote_ip":"77.141.97.142","remote_port":"49984","server_name":"server.example.com"}
Jul 27 12:06:59 caddy-proxy-sbg7 caddy[1208]: {"level":"error","ts":1722074819.8085268,"logger":"tls.on_demand","msg":"loading newly-obtained certificate from storage","remote_ip":"77.141.97.142","remote_port":"49984","server_name":"server.example.com","error":"no matching certificate to load for server.example.com: decoding certificate metadata: invalid character '}' after top-level value"}
{
        "sans": [
                "server.example.com"
        ],
        "issuer_data": {
                "url": "https://acme-v02.api.letsencrypt.org/acme/cert/042fb47e52c51ab43af43257f1ba5e8",
                "ca": "https://acme-v02.api.letsencrypt.org/directory",
                "renewal_info": {
                        "suggestedWindow": {
                                "start": "2024-09-23T06:58:45.333333334Z",
                                "end": "2024-09-25T06:58:45.333333334Z"
                        },
                        "_uniqueIdentifier": "kydGmAOpUWiOmNbEI.BC-0flLFGrQ69DL_idV_G6Xo",
                        "_retryAfter": "2024-07-27T09:40:01.93861755+02:00",
                        "_selectedTime": "2024-09-24T03:22:43Z"
                }
        }
}}

Removing the extra } at the bottom of the file /caddy/certificates/acme-v02.api.letsencrypt.org-directory/server.example.com/server.example.com.json fixes the error.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 7
  • Comments: 31 (21 by maintainers)

Most upvoted comments

Can confirm 2.9.0 fixed it 👍

Shortly after new years. One more beta before then I think.

Same issue here. I’m running a Caddy instance on a single machine with certs stored on a ext4 partition.

Here is a one line version of @aa-shalin script that fixes this issue as well:

$ grep -l "}}" **/*.json | xargs -I {} sed -i 's/}}/}/g' {}

@mholt Do you have any estimates for the release of version 2.9.0?

I wrote this script to find all malformed JSON files

# Directory containing the subdirectories
base_dir="/data/caddy/certificates/acme-v02.api.letsencrypt.org-directory"

# Output file to record JSON files ending with "}}\n}" or "}}"
output_file="valid_json_files.txt"

# Check if output file already exists and remove it to start fresh
if [ -f "$output_file" ]; then
    rm "$output_file"
fi

# Navigate through each subdirectory
for dir in "$base_dir"/*; do
    if [ -d "$dir" ]; then
        # Loop through each JSON file in the directory
        for json_file in "$dir"/*.json; do
            if [ -f "$json_file" ]; then
                # Use awk to check if the file ends with "}}\n}"
                last_chars=$(tail -n 2 "$json_file" | awk '/}}/{if(getline == 1 && /^}$/) print "yes";}')
                if [[ "$last_chars" == "yes" ]]; then
                    # Record the file path in the output file
                    echo "$json_file" >> "$output_file"
                fi
                if tail -c 2 "$json_file" | grep -q "}}"; then
                    # Record the file path in the output file
                    echo "$json_file" >> "$output_file"
                fi
            fi
        done
    fi
done

echo "Script execution completed. Check '$output_file' for results."

Will be hard to say if it fixes anything soon, because caddy was installed and runing for 2y with this setup without any issues 🤔 Nevertheless, I’ve built our 2 servers with latest certmagic@master and report back if this happens again. Feel free to close this issue for now.

@bryanus

And do I need to wipe all my certs first to have them reissued? TIA.

you do not need to wipe all of your certs. I assume that on error, the certs will refetch? maybe @mholt can confirm though .

it may be good to go through the certs and delete any of them that are malformed json.

Could someone remind me on how to perform an upgrade to the 2.9.0-beta.3?

depends on how you are running, but you just need to switch to running the new binary / install the new package from https://github.com/caddyserver/caddy/releases/tag/v2.9.0-beta.3 and restart.

@bryanus you can use 2.9.0-beta.3 right now

I have faced this issue too in this morning after upgrading Caddy to v2.8.4 and OS got rebooted. That Caddy has multiple sites but one site had showed this log messages continuously after upgrading.

I solved this by removing the site’s entire certificate cache dir (such as mv /var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/mysite.example.com{,_bak} in Ubuntu) and restarted Caddy. But I think it’s OK just to fix broken JSON.

Upgrading won’t fix the broken storage, it’ll just fix further breakages of the storage. You could use the beta, it’s pretty stable, just missing a few things we wanted to fit into the final release.

that said, i would seriously recommend just using redis with https://github.com/pberkel/caddy-storage-redis or https://github.com/ss098/certmagic-s3 with juicefs s3 gateway, or writing your own cert-magic plugin for s3 for any production settings.

I understand, but we prefer not adding plugins to caddy. In our env, JuiceFS handles terabytes of data without any issue since 2y now, we prefere keep it as our filesystem abstraction layer and rely on Caddy certmagic filesystem.

We’ll keep an aye on https://github.com/caddyserver/certmagic/pull/300 and provide feedback. 👍

this looks like one file is written on top of another without clearing the first once.

if read is called after fsync is called after truncate and after write but before flush/close, this could happen I think? the new cert would have to be one character smaller than the old cert. (is this possible? are any fields variable length?).

but it’s reading the old file size, so maybe something else happened at write time. regardless, something happened during file write, maybe also during a read.

but yes, my guess is the temp file approach fixes this @mholt

I doubt go’s json encoder would do this.