abiosoft/caddy-exec

Command in route is not executed?

fd0 opened this issue · 7 comments

fd0 commented

Hi,

thanks a lot for writing this extension! I'd like to run git pull when a secret webhook URL is requested by GitHub. Unfortunately, I'm unable to get it working. I've built caddy according to the instructions:

$ xcaddy build v2.0.0 --output caddy --with github.com/abiosoft/caddy-exec
2020/05/17 21:39:31 [INFO] Temporary folder: /tmp/zsh-1000/buildenv_2020-05-17-2139.789508356
2020/05/17 21:39:31 [INFO] Writing main module: /tmp/zsh-1000/buildenv_2020-05-17-2139.789508356/main.go
2020/05/17 21:39:31 [INFO] Initializing Go module
2020/05/17 21:39:31 [INFO] exec (timeout=10s): /home/fd0/sdk/go1.14/bin/go mod init caddy
go: creating new go.mod: module caddy
2020/05/17 21:39:31 [INFO] Pinning versions
2020/05/17 21:39:31 [INFO] exec (timeout=0s): /home/fd0/sdk/go1.14/bin/go get -d -v github.com/caddyserver/caddy/v2@v2.0.0
2020/05/17 21:39:32 [INFO] exec (timeout=0s): /home/fd0/sdk/go1.14/bin/go get -d -v github.com/abiosoft/caddy-exec
go: github.com/abiosoft/caddy-exec upgrade => v0.0.0-20200514094724-d001b97b6396
2020/05/17 21:39:33 [INFO] Build environment ready
2020/05/17 21:39:33 [INFO] Building Caddy
2020/05/17 21:39:33 [INFO] exec (timeout=0s): /home/fd0/sdk/go1.14/bin/go build -o /home/fd0/src/xcaddy/cmd/xcaddy/caddy -ldflags -w -s -trimpath
2020/05/17 21:39:34 [INFO] Build complete: caddy
2020/05/17 21:39:34 [INFO] Cleaning up temporary folder: /tmp/zsh-1000/buildenv_2020-05-17-2139.789508356

./caddy version
v2.0.0 h1:pQSaIJGFluFvu8KDGDODV8u4/QRED/OPyIR+MWYYse8=

The Caddyfile contains the following:

{
    order exec first
}

http://localhost:3000

exec echo startup happening now {
    foreground
    startup
}

route /webhook {
    exec echo foo {
        foreground
    }
    respond "webhook"
}

I've verified (using strace) that the startup command is executed, but the command in the route is not, you can see the output for the startup command in the log:

$ caddy run
2020/05/17 19:41:58.639	INFO	using adjacent Caddyfile
2020/05/17 19:41:58.641	INFO	admin	admin endpoint started	{"address": "tcp/localhost:2019", "enforce_origin": false, "origins": ["localhost:2019", "[::1]:2019", "127.0.0.1:2019"]}
2020/05/17 21:41:58 [INFO][cache:0xc00025fcc0] Started certificate maintenance routine
2020/05/17 19:41:58.642	INFO	tls	cleaned up storage units
startup happening now
2020/05/17 19:41:58.643	INFO	http.handlers.exec.exit		{"command": ["echo", "startup", "happening", "now"], "duration": 0.000732318}
2020/05/17 19:41:58.643	INFO	autosaved config	{"file": "/home/fd0/.config/caddy/autosave.json"}
2020/05/17 19:41:58.643	INFO	serving initial configuration

Now I request the /webhook path:

$ curl -i http://localhost:3000/webhook
HTTP/1.1 200 OK
Server: Caddy
Date: Sun, 17 May 2020 19:42:15 GMT
Content-Length: 7

webhook

The command is not executed, verified by running caddy with strace like this:

$ strace -f -e execve caddy run

Here's the config for the Caddy instance:

$ caddy adapt | jq .
{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":3000"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "localhost"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "args": [
                            "startup",
                            "happening",
                            "now"
                          ],
                          "at": [
                            "startup"
                          ],
                          "command": "echo",
                          "foreground": true,
                          "handler": "exec"
                        }
                      ],
                      "match": [
                        {
                          "execnopmatch": {}
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "args": [
                                    "foo"
                                  ],
                                  "command": "echo",
                                  "foreground": true,
                                  "handler": "exec"
                                }
                              ],
                              "match": [
                                {
                                  "execnopmatch": {}
                                }
                              ]
                            },
                            {
                              "handle": [
                                {
                                  "body": "webhook",
                                  "handler": "static_response"
                                }
                              ]
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/webhook"
                          ]
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }
          ],
          "automatic_https": {
            "skip": [
              "localhost"
            ]
          }
        }
      }
    }
  }
}

Any idea what I'm doing wrong? Is this a bug?

I can't see anything wrong with your config. Let me try to reproduce this.

fd0 commented

Cool, thanks! Let me know if there's anything I can do to help!

@fd0 Try it now, should be fixed.

fd0 commented

Works now, thanks! :)

shameless plug, you probably need hmac if you're setting a secret in Github.

fd0 commented

Thanks for the hint! I don't now if I need that, maybe I'll just use a secret URL for the webhook :)

With the current version (06d4f72), the above Caddyfile results in the startup command being executed on every request, but the response for the request is {"status":"success"}:

$ curl http://localhost:3000/webhook
{"status":"success"}

What is the expected behaviour?