From b15e3b50ffd2d164574a985079317d9f2390c330 Mon Sep 17 00:00:00 2001 From: Paul Greenberg Date: Sat, 12 Mar 2022 12:56:34 -0500 Subject: [PATCH] add routeless config handling Resolves: #10 --- README.md | 9 +++++++++ assets/config/Caddyfile | 4 ++-- assets/config/post_cmd_exec/Caddyfile | 4 ++-- assets/config/routeless/Caddyfile | 28 +++++++++++++++++++++++++++ assets/config/ssh/Caddyfile | 10 +++++----- assets/config/webhook/Caddyfile | 4 ++-- caddyfile.go | 20 ++++++++++++++++--- pkg/service/endpoint.go | 2 ++ 8 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 assets/config/routeless/Caddyfile diff --git a/README.md b/README.md index 963a199..e115b95 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Configuration examples: * [Private or public repo over SSH with key-based authentication](./assets/config/ssh/Caddyfile) * [Repo with Webhooks](./assets/config/webhook/Caddyfile) * [Repo with post pull execution scripts](./assets/config/post_cmd_exec/Caddyfile) +* [Routeless config](./assets/config/routeless/Caddyfile) For example, the following configuration sets up a definition for `authp.github.io` repo. The request to `authp.myfiosgateway.com/update/authp.github.io` trigger @@ -49,11 +50,19 @@ repo. The request to `authp.myfiosgateway.com/update/authp.github.io` trigger base_dir /tmp url https://github.com/authp/authp.github.io.git branch gh-pages + post pull exec { + name Pager + command /usr/bin/echo + args "pulled authp.github.io repo" + } } } } authp.myfiosgateway.com { + route /version* { + respond * "1.0.0" 200 + } route /update/authp.github.io { git update repo authp.github.io } diff --git a/assets/config/Caddyfile b/assets/config/Caddyfile index 25589e3..c76d11e 100644 --- a/assets/config/Caddyfile +++ b/assets/config/Caddyfile @@ -6,7 +6,7 @@ git { repo authp.github.io { - base_dir ./tmp + base_dir {$HOME}/tmp url https://github.com/authp/authp.github.io.git branch gh-pages } @@ -22,7 +22,7 @@ } route { file_server { - root ./tmp/authp.github.io + root {$HOME}/tmp/authp.github.io } } } diff --git a/assets/config/post_cmd_exec/Caddyfile b/assets/config/post_cmd_exec/Caddyfile index b4a6e3a..003fa3c 100644 --- a/assets/config/post_cmd_exec/Caddyfile +++ b/assets/config/post_cmd_exec/Caddyfile @@ -6,7 +6,7 @@ git { repo authp.github.io { - base_dir ./tmp + base_dir {$HOME}/tmp url https://github.com/authp/authp.github.io.git branch gh-pages post pull exec { @@ -27,7 +27,7 @@ } route { file_server { - root ./tmp/authp.github.io + root {$HOME}/tmp/authp.github.io } } } diff --git a/assets/config/routeless/Caddyfile b/assets/config/routeless/Caddyfile new file mode 100644 index 0000000..e34c459 --- /dev/null +++ b/assets/config/routeless/Caddyfile @@ -0,0 +1,28 @@ +{ + debug + local_certs + http_port 8080 + https_port 8443 + + order git before respond + + git { + repo authp.github.io { + base_dir {$HOME}/tmp/authp/gitcfg/ + url https://github.com/authp/authp.github.io.git + branch gh-pages + post pull exec { + name Pager + command /usr/bin/echo + args "pulled authp.github.io repo" + } + } + } +} + +127.0.0.1, localhost { + respond /version* "1.0.0" 200 + git /update/authp.github.io update repo authp.github.io + root * {$HOME}/tmp/authp/gitcfg/authp.github.io + file_server +} diff --git a/assets/config/ssh/Caddyfile b/assets/config/ssh/Caddyfile index 6354f87..5c13af9 100644 --- a/assets/config/ssh/Caddyfile +++ b/assets/config/ssh/Caddyfile @@ -6,11 +6,11 @@ git { repo authp.github.io { - base_dir ./tmp/ssh + base_dir {$HOME}/tmp/ssh url git@github.com:authp/authp.github.io.git - # auth key ~/.ssh/id_rsa passphrase {env.MY_SSH_KEY_PASSPHRASE} - # auth key ~/.ssh/id_rsa passphrase {env.MY_SSH_KEY_PASSPHRASE} no_strict_host_key_check - auth key ~/.ssh/id_rsa + # auth key {$HOME}/.ssh/id_rsa passphrase {env.MY_SSH_KEY_PASSPHRASE} + # auth key {$HOME}/.ssh/id_rsa passphrase {env.MY_SSH_KEY_PASSPHRASE} no_strict_host_key_check + auth key {$HOME}/.ssh/id_rsa branch gh-pages } } @@ -25,7 +25,7 @@ } route { file_server { - root ./tmp/ssh/authp.github.io + root {$HOME}/tmp/ssh/authp.github.io } } } diff --git a/assets/config/webhook/Caddyfile b/assets/config/webhook/Caddyfile index c684636..b639d91 100644 --- a/assets/config/webhook/Caddyfile +++ b/assets/config/webhook/Caddyfile @@ -6,7 +6,7 @@ git { repo authp.github.io { - base_dir ./tmp + base_dir {$HOME}/tmp url https://github.com/authp/authp.github.io.git # webhook Github X-Hub-Signature-256 {env.MY_GITHUB_WEBHOOK_KEY} webhook Gitlab X-Gitlab-Token barbaz @@ -24,7 +24,7 @@ } route { file_server { - root ./tmp/authp.github.io + root {$HOME}/tmp/authp.github.io } } } diff --git a/caddyfile.go b/caddyfile.go index 62368fa..79821e2 100644 --- a/caddyfile.go +++ b/caddyfile.go @@ -208,11 +208,25 @@ func parseCaddyfileHandlerConfig(h httpcaddyfile.Helper) (*service.Endpoint, err for h.Next() { args := h.RemainingArgs() + strArgs := strings.Join(args, " ") + if !strings.Contains(strArgs, "update repo ") { + return nil, h.Errf("unsupported config: git %s", strArgs) + } switch { - case strings.HasPrefix(strings.Join(args, " "), "update repo "): + case args[0] == "update" && args[1] == "repo": + if len(args) != 3 { + return nil, h.Errf("malformed config: git %s", strArgs) + } + endpoint.Path = "*" endpoint.RepositoryName = args[2] + case args[1] == "update" && args[2] == "repo": + if len(args) != 4 { + return nil, h.Errf("malformed config: git %s", strArgs) + } + endpoint.Path = args[0] + endpoint.RepositoryName = args[3] default: - return nil, h.Errf("unsupported config: git %s", strings.Join(args, " ")) + return nil, h.Errf("malformed config: git %s", strArgs) } } @@ -227,7 +241,7 @@ func getRouteFromParseCaddyfileHandlerConfig(h httpcaddyfile.Helper) ([]httpcadd return nil, err } pathMatcher := caddy.ModuleMap{ - "path": h.JSON(caddyhttp.MatchPath{"*"}), + "path": h.JSON(caddyhttp.MatchPath{endpoint.Path}), } route := caddyhttp.Route{ HandlersRaw: []json.RawMessage{ diff --git a/pkg/service/endpoint.go b/pkg/service/endpoint.go index e069f05..ee08c1c 100644 --- a/pkg/service/endpoint.go +++ b/pkg/service/endpoint.go @@ -27,6 +27,7 @@ import ( type Endpoint struct { mu sync.Mutex Name string `json:"-"` + Path string `json:"path,omitempty" xml:"path,omitempty" yaml:"path,omitempty"` RepositoryName string logger *zap.Logger startedAt time.Time @@ -45,6 +46,7 @@ func (m *Endpoint) Provision() error { m.logger.Info( "provisioned plugin instance", zap.String("instance_name", m.Name), + zap.String("path", m.Path), zap.Time("started_at", m.startedAt), ) return nil