Issue with JSON API and load end-point

1. Caddy version (caddy version):

v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

2. How I run Caddy:

systemd

a. System environment:

Ubuntu 18.04

b. Command:

n/a

c. Service/unit/compose file:

/na

d. My complete Caddyfile or JSON config:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "automatic_https": {
            "disable": true
          },
          "listen": [
            ":80"
          ],
          "routes": [
            {
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "group": "group0",
                      "handle": [
                        {
                          "handler": "vars",
                          "root": "/opt/ivt/photography"
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/files/*"
                          ]
                        }
                      ]
                    },
                    {
                      "group": "group0",
                      "handle": [
                        {
                          "handler": "vars",
                          "root": "/opt/ivt/apps/6.0.0/packages/client/spa"
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "rewrite",
                                  "uri_substring": [
                                    {
                                      "find": "/console_socket",
                                      "replace": "/console/socket.io"
                                    }
                                  ]
                                }
                              ]
                            },
                            {
                              "handle": [
                                {
                                  "handler": "reverse_proxy",
                                  "headers": {
                                    "request": {
                                      "set": {
                                        "Access-Control-Allow-Credentials": [
                                          "true"
                                        ],
                                        "Access-Control-Allow-Headers": [
                                          "Cache-Control,Content-Type"
                                        ],
                                        "Access-Control-Allow-Origin": [
                                          "*"
                                        ],
                                        "X-Forwarded-Proto": [
                                          "{http.request.scheme}"
                                        ],
                                        "X-Real-Ip": [
                                          "{http.request.remote.host}"
                                        ]
                                      }
                                    }
                                  },
                                  "upstreams": [
                                    {
                                      "dial": "localhost:3001"
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/console_socket/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "rewrite",
                                  "uri_substring": [
                                    {
                                      "find": "/web_app_socket",
                                      "replace": "/web/socket.io"
                                    }
                                  ]
                                }
                              ]
                            },
                            {
                              "handle": [
                                {
                                  "handler": "reverse_proxy",
                                  "headers": {
                                    "request": {
                                      "set": {
                                        "Access-Control-Allow-Credentials": [
                                          "true"
                                        ],
                                        "Access-Control-Allow-Headers": [
                                          "Cache-Control,Content-Type"
                                        ],
                                        "Access-Control-Allow-Origin": [
                                          "*"
                                        ],
                                        "X-Forwarded-Proto": [
                                          "{http.request.scheme}"
                                        ],
                                        "X-Real-Ip": [
                                          "{http.request.remote.host}"
                                        ]
                                      }
                                    }
                                  },
                                  "upstreams": [
                                    {
                                      "dial": "localhost:3001"
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/web_app_socket/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "rewrite",
                                  "uri_substring": [
                                    {
                                      "find": "/liveview",
                                      "replace": "/socket.io"
                                    }
                                  ]
                                }
                              ]
                            },
                            {
                              "handle": [
                                {
                                  "handler": "reverse_proxy",
                                  "headers": {
                                    "request": {
                                      "set": {
                                        "Access-Control-Allow-Credentials": [
                                          "true"
                                        ],
                                        "Access-Control-Allow-Headers": [
                                          "Cache-Control,Content-Type"
                                        ],
                                        "Access-Control-Allow-Origin": [
                                          "*"
                                        ],
                                        "X-Forwarded-Proto": [
                                          "{http.request.scheme}"
                                        ],
                                        "X-Real-Ip": [
                                          "{http.request.remote.host}"
                                        ]
                                      }
                                    }
                                  },
                                  "upstreams": [
                                    {
                                      "dial": "localhost:3004"
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/liveview/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "rewrite",
                                  "uri_substring": [
                                    {
                                      "find": "/weather",
                                      "replace": "/socket.io"
                                    }
                                  ]
                                }
                              ]
                            },
                            {
                              "handle": [
                                {
                                  "handler": "reverse_proxy",
                                  "headers": {
                                    "request": {
                                      "set": {
                                        "Access-Control-Allow-Credentials": [
                                          "true"
                                        ],
                                        "Access-Control-Allow-Headers": [
                                          "Cache-Control,Content-Type"
                                        ],
                                        "Access-Control-Allow-Origin": [
                                          "*"
                                        ],
                                        "X-Forwarded-Proto": [
                                          "{http.request.scheme}"
                                        ],
                                        "X-Real-Ip": [
                                          "{http.request.remote.host}"
                                        ]
                                      }
                                    }
                                  },
                                  "upstreams": [
                                    {
                                      "dial": "localhost:3010"
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/weather/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "rewrite",
                                  "uri_substring": [
                                    {
                                      "find": "/archive",
                                      "replace": "/socket.io"
                                    }
                                  ]
                                }
                              ]
                            },
                            {
                              "handle": [
                                {
                                  "handler": "reverse_proxy",
                                  "headers": {
                                    "request": {
                                      "set": {
                                        "Access-Control-Allow-Credentials": [
                                          "true"
                                        ],
                                        "Access-Control-Allow-Headers": [
                                          "Cache-Control,Content-Type"
                                        ],
                                        "Access-Control-Allow-Origin": [
                                          "*"
                                        ],
                                        "X-Forwarded-Proto": [
                                          "{http.request.scheme}"
                                        ],
                                        "X-Real-Ip": [
                                          "{http.request.remote.host}"
                                        ]
                                      }
                                    }
                                  },
                                  "upstreams": [
                                    {
                                      "dial": "localhost:3003"
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/archive/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "rewrite",
                                  "uri_substring": [
                                    {
                                      "find": "/alarms",
                                      "replace": "/socket.io"
                                    }
                                  ]
                                }
                              ]
                            },
                            {
                              "handle": [
                                {
                                  "handler": "reverse_proxy",
                                  "headers": {
                                    "request": {
                                      "set": {
                                        "Access-Control-Allow-Credentials": [
                                          "true"
                                        ],
                                        "Access-Control-Allow-Headers": [
                                          "Cache-Control,Content-Type"
                                        ],
                                        "Access-Control-Allow-Origin": [
                                          "*"
                                        ],
                                        "X-Forwarded-Proto": [
                                          "{http.request.scheme}"
                                        ],
                                        "X-Real-Ip": [
                                          "{http.request.remote.host}"
                                        ]
                                      }
                                    }
                                  },
                                  "upstreams": [
                                    {
                                      "dial": "localhost:3002"
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/alarms/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "rewrite",
                                  "uri_substring": [
                                    {
                                      "find": "/ptz",
                                      "replace": "/socket.io"
                                    }
                                  ]
                                }
                              ]
                            },
                            {
                              "handle": [
                                {
                                  "handler": "reverse_proxy",
                                  "headers": {
                                    "request": {
                                      "set": {
                                        "Access-Control-Allow-Credentials": [
                                          "true"
                                        ],
                                        "Access-Control-Allow-Headers": [
                                          "Cache-Control,Content-Type"
                                        ],
                                        "Access-Control-Allow-Origin": [
                                          "*"
                                        ],
                                        "X-Forwarded-Proto": [
                                          "{http.request.scheme}"
                                        ],
                                        "X-Real-Ip": [
                                          "{http.request.remote.host}"
                                        ]
                                      }
                                    }
                                  },
                                  "upstreams": [
                                    {
                                      "dial": "localhost:3006"
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/ptz/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "reverse_proxy",
                                  "headers": {
                                    "request": {
                                      "set": {
                                        "Access-Control-Allow-Credentials": [
                                          "true"
                                        ],
                                        "Access-Control-Allow-Headers": [
                                          "Cache-Control,Content-Type"
                                        ],
                                        "Access-Control-Allow-Origin": [
                                          "*"
                                        ],
                                        "X-Forwarded-Proto": [
                                          "{http.request.scheme}"
                                        ],
                                        "X-Real-Ip": [
                                          "{http.request.remote.host}"
                                        ]
                                      }
                                    }
                                  },
                                  "upstreams": [
                                    {
                                      "dial": "localhost:3001"
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/api/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "file_server",
                          "hide": [
                            "Caddyfile"
                          ]
                        }
                      ]
                    }
                  ]
                }
              ],
              "match": [
                {
                  "host": [
                    "localhost"
                  ]
                }
              ],
              "terminal": true
            }
          ]
        }
      }
    },
    "tls": {
      "automation": {
        "policies": [
          {
            "issuer": {
              "module": "internal"
            }
          }
        ]
      }
    }
  },
  "logging": {
    "logs": {
      "default": {
        "level": "DEBUG"
      }
    }
  }
}

3. The problem I’m having:

I am programmatically adding reverse_proxies to the json and sending the whole file using the load end-point.
What I am seeing, is if the command is repeated, then instead of 4 proxies being added, it befomes 8. The next time it is 12. It’s as if Caddy is merging what I send, instead of starting “fresh”.
If I sent the default json, with no added proxies, then the currently added proxies remain.

4. Error messages and/or full log output:

if I run this after a reset I can see the proxies remain:

curl localhost:2019/config/ | grep dcam-dev

5. What I already tried:

I have tried playing around with caching to see if that changes anything, but none work:
‘Cache-Control’: ‘must-revalidate’
‘Cache-Control’: ‘no-store’
‘Cache-Control’: ‘no-cache’

What this do tho, was force caddy into not thinking the cache had changed.

Before adding ‘Cache-Control’ command, I was getting this:

Aug 21 09:26:16 FLEX-5 caddy[1037]: {"level":"info","ts":1598023576.419586,"logger":"admin.api","msg":"received request","method":"POST","host":"localhost:2019","uri":"/load","remote_addr":"127.0.0.1:54164","headers":{"Accept":["application/json, text/plain, */*"],"Connection":["close"],"Content-Length":["9050"],"Content-Type":["application/json"],"User-Agent":["axios/0.19.2"]}}
Aug 21 09:26:16 FLEX-5 caddy[1037]: {"level":"info","ts":1598023576.4207368,"logger":"admin.api","msg":"config is unchanged"}
Aug 21 09:26:16 FLEX-5 caddy[1037]: {"level":"info","ts":1598023576.4207559,"logger":"admin.api","msg":"load complete"}

But with the cache command, I now get this:

Aug 21 10:30:15 FLEX-5 caddy[1037]: {"level":"info","ts":1598027415.6441956,"logger":"admin.api","msg":"received request","method":"POST","host":"localhost:2019","uri":"/load","remote_addr":"127.0.0.1:59392","headers":{"Accept":["application/json, text/plain, */*"],"Cache-Control":["must-revalidate"],"Connection":["close"],"Content-Length":["6868"],"Content-Type":["application/json"],"User-Agent":["axios/0.19.2"]}}
Aug 21 10:30:15 FLEX-5 caddy[1037]: {"level":"info","ts":1598027415.64678,"logger":"admin","msg":"admin endpoint started","address":"tcp/localhost:2019","enforce_origin":false,"origins":["localhost:2019","[::1]:2019","127.0.0.1:2019"]}
Aug 21 10:30:15 FLEX-5 caddy[1037]: 2020/08/21 10:30:15 [INFO][cache:0xc000de3440] Started certificate maintenance routine
Aug 21 10:30:15 FLEX-5 caddy[1037]: {"level":"debug","ts":1598027415.6508875,"logger":"http","msg":"starting server loop","address":"[::]:80","http3":false,"tls":false}
Aug 21 10:30:15 FLEX-5 caddy[1037]: {"level":"warn","ts":1598027415.6512709,"logger":"pki.ca.local","msg":"installing root certificate (you might be prompted for password)","path":"storage:pki/authorities/local/root.crt"}
Aug 21 10:30:15 FLEX-5 caddy[1037]: 2020/08/21 10:30:15 not NSS security databases found
Aug 21 10:30:15 FLEX-5 caddy[1037]: 2020/08/21 10:30:15 define JAVA_HOME environment variable to use the Java trust
Aug 21 10:30:15 FLEX-5 sudo[4692]: pam_unix(sudo:auth): conversation failed
Aug 21 10:30:15 FLEX-5 sudo[4692]: pam_unix(sudo:auth): auth could not identify password for [caddy]
Aug 21 10:30:15 FLEX-5 caddy[1037]: {"level":"error","ts":1598027415.655627,"logger":"pki.ca.local","msg":"failed to install root certificate","error":"failed to execute sudo: exit status 1","certificate_file":"storage:pki/authorities/local/root.crt"}
Aug 21 10:30:15 FLEX-5 caddy[1037]: 2020/08/21 10:30:15 [INFO][cache:0xc000ca4240] Stopped certificate maintenance routine
Aug 21 10:30:15 FLEX-5 caddy[1037]: {"level":"info","ts":1598027415.6559749,"msg":"autosaved config","file":"/var/lib/caddy/.config/caddy/autosave.json"}
Aug 21 10:30:15 FLEX-5 caddy[1037]: {"level":"info","ts":1598027415.655991,"logger":"admin.api","msg":"load complete"}
Aug 21 10:30:16 FLEX-5 caddy[1037]: {"level":"info","ts":1598027416.1492498,"logger":"admin","msg":"stopped previous server"}

But, the contents of Caddy are unchanged when I do this:

curl localhost:2019/config/ | grep dcam-dev

or, the data is merged in some way (unexpected)

After adjusting our default caddy json file, I send it with axios like this:

function putConfig (config) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: 'http://localhost:2019/load',
      headers: {
        'Content-Type': 'application/json',
        'Cache-Control': 'must-revalidate'
      },
      data: config
    })
      .then(results => {
        console.log(results)
        resolve(results.data)
      })
      .catch(error => {
        console.log(error)
        reject(error.message)
      })
  })
}

response back is 200

6. Links to relevant resources:

ok, embarrassed to say, but the merging was my issue because I didn’t do a deep copy on our default json file, so it was being modified each time. :frowning:

I am still having some other issues, but will address those next week. Taking a 4-day weekend.

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