API - change config conditionally only if it has not changed

Is it possible to change the configuration via the API only if the configuration has not been changed? For example, using the If-Modified-Since or If-Match HTTP header.

I want to change the configuration using an API from another application. But if I send, for example, a PATCH request to the /config/apps/http/servers/default/routes/0/handle/2/root address and someone else adds a new route to the beginning, I will change another item than I wanted. If I could load the configuration along with the last-modified timestamp or the Etag and then condition the configuration change (POST / PUT / PATCH /config call) by using the If-Modified-Since or If-Match headers to verify that there was no configuration change in the meantime, it would prevent unintentionally overwrite another part of the configuration. I would only need a global config timestamp or an ETag. It is not necessary to deal with each configuration node separately.

I know I can use @id, but that means automatically generating unique identifiers and having that identifier stored somewhere.

1. Caddy version (caddy version):

v2.4.6

2. How I run Caddy:

caddy run

a. System environment:

Windows 10

b. Command:

curl --location --request PATCH 'http://localhost:2019/config/apps/http/servers/default/routes/0/handle/0/root' \
--header 'If-Unmodified-Since: Wed, 21 Oct 2015 07:28:00 GMT' \
--header 'Content-Type: application/json' \
--data-raw '"C:\\dwnld"'

c. Service/unit/compose file:

Windows amd64 EXE file downloaded from Download Caddy with Standard features and without extra modules

d. My complete Caddyfile or JSON config:

{
    "apps": {
        "http": {
            "servers": {
                "default": {
                    "automatic_https": {
                        "disable": true,
                        "disable_redirects": true
                    },
                    "listen": [
                        "localhost:8080"
                    ],
                    "routes": [
                        {
                            "handle": [
                                {
                                    "browse": {},
                                    "handler": "file_server",
                                    "root": "C:\\work"
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
}

3. The problem I’m having:

Caddy should respond with 412 Precondition Failed error when configuration was modified since the specified date. It responds with 200 OK actually and overwrites bad part of the configuration (if input matches the expected structure).

4. Error messages and/or full log output:

5. What I already tried:

Use If-Modified-Since HTTP header. But Caddy doesn’t send a Last-Modified header, so it was a blind try.

6. Links to relevant resources:

1 Like

You should probably open a feature request for this on Github. I don’t think anything like this is implemented yet.

2 Likes

Whenever Caddy is told to load a config that is byte-for-byte the same as the current config, it will no-op by default (unless reload is forced explicitly).

I think the use case for If headers is valid, and would make a good feature request. If you could suggest this on GitHub like Francis also said, that would be great! Thanks

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.