Caddy: v2: IDs in JSON doesn't work properly

Created on 27 Nov 2019  路  4Comments  路  Source: caddyserver/caddy

1. Which version of Caddy are you using (caddy -version)?

8de1a762273323608d9d9080c42057d1814070b6

2. What are you trying to do?

I'll try to use IDs in JSON to identify the objects in a servers 'routes' section.

Base configuration JSON:

{
  "admin": {
    "disabled": false,
    "listen": ":2019",
    "enforce_origin": false,
    "origins": []
  },
  "apps": {
    "http": {
      "http_port": 8080,
      "https_port": 8443,
      "servers": {
        "proxy": {
          "listen": [
            ":8080",
            ":8443"
          ],
          "routes": [
          ]
        }
      }
    },
    "tls": {
      "automation": {},
      "session_tickets": {}
    }
  }
}

Add first route using the API:

$ curl -X POST -H 'Content-Type: application/json; charset=utf-8' http://localhost:2019/config/apps/http/servers/proxy/routes/ -d @- << EOF
{
  "@id": "foo",
  "match": [
    {
      "host": [
        "foo.localhost"
      ]
    }
  ],
  "handle": [
    {
      "handler": "reverse_proxy",
      "upstreams": [
        {
          "dial": "test.example.com:80"
        }
      ]
    }
  ]
}
EOF

Add second route using the API:

$ curl -X POST -H 'Content-Type: application/json; charset=utf-8' http://localhost:2019/config/apps/http/servers/proxy/routes/ -d @- << EOF
{
  "@id": "bar",
  "match": [
    {
      "host": [
        "bar.localhost"
      ]
    }
  ],
  "handle": [
    {
      "handler": "reverse_proxy",
      "upstreams": [
        {
          "dial": "test.example.com:80"
        }
      ]
    }
  ]
}
EOF

Removing first route using the API:

curl -X DELETE http://localhost:2019/id/foo/

But it results in:

{"error":"unknown object ID 'foo'"}

3. What is your Caddyfile?

_All configurations are already shown in 2._

4. How did you run Caddy (give the full command and describe the execution environment)?

$ caddy run --config /etc/caddy/config.json

5. Please paste any relevant HTTP request(s) here.

_none_

6. What did you expect to see?

The expectation is, that I can remove the route element form the list by using it's ID.

7. What did you see instead (give full error messages and/or log)?

{"error":"unknown object ID 'foo'"}

It seems that only the last ID will be stored.

8. Why is this a bug, and how do you think this should be fixed?

It seems that only the last ID will be stored.

9. What are you doing to work around the problem in the meantime?

Currently, I'll fetch the whole routes list, search the entry I want to remove.

curl http://localhost:2019/config/apps/http/servers/proxy/routes/

Then I call delete using the index of the element.

curl -X DELETE http://localhost:2019/config/apps/http/servers/proxy/routes/1

10. Please link to any related issues, pull requests, and/or discussion.

_none_

bug

Most helpful comment

@mholt It works great 馃憤
Thanks for the fast fix.

All 4 comments

Ah, thanks for the report. Working on a fix...

@FrankGiesecke Thanks to your detailed report and very easy reproduce instructions, I was able to find and fix the bug pretty easily. We were wiping out previous ID's, oops.

Now we preserve them, so you'll see them in your GET requests for current config. We just strip them out of the JSON internally using a regex. :joy: It works though. There is one other way we could do it... by deleting the "@id" fields as we find and index them (like we were before), then JSON-encode, then re-add the "@id" fields into our stored internal representation (rawCfg). We just have to make sure the resulting JSON doesn't have them, but our internal representation does. I just opted for the regex cause it's a quicker fix and seems to work pretty well.

Can you build from source (v2 branch) and try again and let me know how it goes for you?

@mholt It works great 馃憤
Thanks for the fast fix.

It's because your issue was so well-written. I will probably refer to it as a model for others.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mschneider82 picture mschneider82  路  3Comments

whs picture whs  路  3Comments

muhammadmuzzammil1998 picture muhammadmuzzammil1998  路  3Comments

ericmdantas picture ericmdantas  路  3Comments

xfzka picture xfzka  路  3Comments