GithubHelp home page GithubHelp logo

caddy-events-exec's People

Contributors

mholt avatar pmaene avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

caddy-events-exec's Issues

How to exec echo command?

I'm trying to execute something outside of caddy running in a docker container.
The original purpose is to copy certificate to another directory on cert_obtained. But the directory isn't accessable for caddy inside the container. I did something similar with another container and a named pipe.
The idea is to echo some to a named pipe in a directory shared with the host. On the host there is script monitoring the named pipe and executing the real command.

I added this to my Caddyfile for testing, where /data/host.pipe is a regular file.

events {
	on tls_get_certificate exec /bin/sh -c 'echo caddy_cert {event.data.client_hello} >> /data/host.pipe'
}

Caddy gives me this error. Can you please help to get that echo command in the correct format?
{"level":"error","ts":"2024/05/02 14:27:25","logger":"events.handlers.exec","msg":"background command failed","error":"exit status 2"}

Using exec echo foo > bar just echoes exactly that to containers console and ignores redirection.

Event hook doesn't appear to run on event.

caddy --version
v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=

Caddy built with: xcaddy build --with github.com/mholt/caddy-events-exec

Caddyfile section:

{
    #debug
	events {
		on cert_obtained exec /usr/local/sbin/on_cert_update.sh {event.data.certificate_path}
		#on cached_managed_cert exec /usr/local/sbin/on_cert_update.sh {event.data.certificate_path}
	}
}

As you can see above, I changed the event to something I can easily trigger with a simple restart to avoid having to keep renewing certs. The result is the same. In the debug output, I can see the event itself, but no mention of the hook being run.

Script permissions:
-rwxr-xr-x 1 root root 613 Apr 18 08:49 /usr/local/sbin/on_cert_update.sh

caddy --list-modules
admin.api.load
admin.api.metrics
<snip>
tls.stek.standard

  Standard modules: 106

events.handlers.exec

  Non-standard modules: 1

  Unknown modules: 0

Example Debug output:

Apr 25 08:43:37 testsrv caddy[3547213]: {"level":"debug","ts":1713386617.0993721,"logger":"events","msg":"event","name":"cert_obtained","id":"09894d47-6749-4f57-9e52-4657d4351e37","origin":"tls","data":{"certificate_path":"certificates/acme-v02.api.letsencrypt.org-directory/www.redacted.co.nz/www.redacted.co.nz.crt","identifier":"www.redacted.co.nz","issuer":"acme-v02.api.letsencrypt.org-directory","metadata_path":"certificates/acme-v02.api.letsencrypt.org-directory/www.redacted.co.nz/www.redacted.co.nz.json","private_key_path":"certificates/acme-v02.api.letsencrypt.org-directory/www.redacted.co.nz/www.redacted.co.nz.key","renewal":false,"storage_path":"certificates/acme-v02.api.letsencrypt.org-directory/www.redacted.co.nz"}}
Apr 25 08:43:37 testsrv caddy[3547213]: {"level":"info","ts":1713386617.0995224,"logger":"tls.obtain","msg":"releasing lock","identifier":"www.redacted.co.nz"}
Apr 25 08:43:37 testsrv caddy[3547213]: {"level":"debug","ts":1713386617.1007128,"logger":"tls","msg":"loading managed certificate","domain":"www.redacted.co.nz","expiration":1721159016,"issuer_key":"acme-v02.api.letsencrypt.org-directory","storage":"FileStorage:/var/lib/caddy/.local/share/caddy"}
Apr 25 08:43:37 testsrv caddy[3547213]: {"level":"debug","ts":1713386617.9157176,"logger":"tls.cache","msg":"added certificate to cache","subjects":["www.redacted.co.nz"],"expiration":1721159016,"managed":true,"issuer_key":"acme-v02.api.letsencrypt.org-directory","hash":"6ee5b56001c43c16dbbe5374939f813e551af5a8d15c7c2174e7ac623c8f27b6","cache_size":17,"cache_capacity":10000}

I've googled a lot, and found one other person having a similar fault, but they had the event name incorrect. As you can see, I have it correct. Any ideas at all on how to debug this further would be appreciated as I am well and truly stuck!

Thanks.

xcaddy build getting stuck

I'm having trouble building using xcaddy build with this module.
xcaddy build by itself completes successfully. xcaddy build with a different module (caddy-security) completes successfully. But I can't get xcaddy build --with github.com/mholt/caddy-events-exec to complete successfully.
It always seems to get stuck on some dependancy. Here is the output. It got stuck so I had to eventually just ctrl+c to stop it:

xcaddy build --with github.com/mholt/caddy-events-exec
2023/02/20 22:11:21 [INFO] Temporary folder: /tmp/buildenv_2023-02-20-2211.3715409004
2023/02/20 22:11:21 [INFO] Writing main module: /tmp/buildenv_2023-02-20-2211.3715409004/main.go
package main

import (
	caddycmd "github.com/caddyserver/caddy/v2/cmd"

	// plug in Caddy modules here
	_ "github.com/caddyserver/caddy/v2/modules/standard"
	_ "github.com/mholt/caddy-events-exec"
)

func main() {
	caddycmd.Main()
}
2023/02/20 22:11:21 [INFO] Initializing Go module
2023/02/20 22:11:21 [INFO] exec (timeout=10s): /usr/bin/go mod init caddy 
go: creating new go.mod: module caddy
go: to add module requirements and sums:
	go mod tidy
2023/02/20 22:11:21 [INFO] Pinning versions
2023/02/20 22:11:21 [INFO] exec (timeout=0s): /usr/bin/go get -d -v github.com/caddyserver/caddy/v2 
go: downloading github.com/caddyserver/caddy/v2 v2.6.4
go: downloading github.com/caddyserver/caddy v1.0.5
go: downloading github.com/caddyserver/certmagic v0.17.2
go: downloading github.com/google/uuid v1.3.0
go: downloading github.com/prometheus/client_golang v1.14.0
go: downloading github.com/quic-go/quic-go v0.32.0
go: downloading go.uber.org/zap v1.24.0
go: downloading golang.org/x/sys v0.5.0
go: downloading golang.org/x/term v0.5.0
go: downloading github.com/klauspost/cpuid/v2 v2.2.3
go: downloading github.com/libdns/libdns v0.2.1
go: downloading github.com/mholt/acmez v1.1.0
go: downloading github.com/miekg/dns v1.1.50
go: downloading golang.org/x/crypto v0.5.0
go: downloading golang.org/x/net v0.7.0
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/cespare/xxhash/v2 v2.1.2
go: downloading github.com/cespare/xxhash v1.1.0
go: downloading github.com/golang/protobuf v1.5.2
go: downloading github.com/prometheus/client_model v0.3.0
go: downloading github.com/prometheus/common v0.37.0
go: downloading github.com/prometheus/procfs v0.8.0
go: downloading google.golang.org/protobuf v1.28.1
go: downloading go.uber.org/atomic v1.9.0
go: downloading go.uber.org/multierr v1.6.0
go: downloading github.com/quic-go/qpack v0.4.0
go: downloading github.com/golang/mock v1.6.0
go: downloading github.com/onsi/ginkgo/v2 v2.2.0
go: downloading golang.org/x/tools v0.2.0
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
go: downloading github.com/quic-go/qtls-go1-18 v0.2.0
go: downloading github.com/quic-go/qtls-go1-19 v0.2.0
go: downloading github.com/quic-go/qtls-go1-20 v0.1.0
go: downloading golang.org/x/exp v0.0.0-20221205204356-47842c84f3db
go: downloading golang.org/x/text v0.7.0
go: downloading golang.org/x/mod v0.6.0
go: downloading github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0
go: downloading github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38
go: added github.com/beorn7/perks v1.0.1
go: added github.com/caddyserver/caddy/v2 v2.6.4
go: added github.com/caddyserver/certmagic v0.17.2
go: added github.com/cespare/xxhash/v2 v2.1.2
go: added github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0
go: added github.com/golang/mock v1.6.0
go: added github.com/golang/protobuf v1.5.2
go: added github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38
go: added github.com/google/uuid v1.3.0
go: added github.com/klauspost/cpuid/v2 v2.2.3
go: added github.com/libdns/libdns v0.2.1
go: added github.com/matttproud/golang_protobuf_extensions v1.0.1
go: added github.com/mholt/acmez v1.1.0
go: added github.com/miekg/dns v1.1.50
go: added github.com/onsi/ginkgo/v2 v2.2.0
go: added github.com/prometheus/client_golang v1.14.0
go: added github.com/prometheus/client_model v0.3.0
go: added github.com/prometheus/common v0.37.0
go: added github.com/prometheus/procfs v0.8.0
go: added github.com/quic-go/qpack v0.4.0
go: added github.com/quic-go/qtls-go1-18 v0.2.0
go: added github.com/quic-go/qtls-go1-19 v0.2.0
go: added github.com/quic-go/qtls-go1-20 v0.1.0
go: added github.com/quic-go/quic-go v0.32.0
go: added go.uber.org/atomic v1.9.0
go: added go.uber.org/multierr v1.6.0
go: added go.uber.org/zap v1.24.0
go: added golang.org/x/crypto v0.5.0
go: added golang.org/x/exp v0.0.0-20221205204356-47842c84f3db
go: added golang.org/x/mod v0.6.0
go: added golang.org/x/net v0.7.0
go: added golang.org/x/sys v0.5.0
go: added golang.org/x/term v0.5.0
go: added golang.org/x/text v0.7.0
go: added golang.org/x/tools v0.2.0
go: added google.golang.org/protobuf v1.28.1
2023/02/20 22:11:26 [INFO] exec (timeout=0s): /usr/bin/go get -d -v github.com/mholt/caddy-events-exec github.com/caddyserver/caddy/v2 
go: downloading github.com/mholt/caddy-events-exec v0.0.0-20221013172934-88290c6b74c4
go: added github.com/mholt/caddy-events-exec v0.0.0-20221013172934-88290c6b74c4
2023/02/20 22:11:27 [INFO] exec (timeout=0s): /usr/bin/go get -d -v  
go: downloading github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b
go: downloading github.com/spf13/cobra v1.6.1
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/inconshreveable/mousetrap v1.0.1
go: downloading github.com/cpuguy83/go-md2man/v2 v2.0.2
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading github.com/smallstep/certificates v0.23.2
go: downloading github.com/smallstep/truststore v0.12.1
go: downloading go.step.sm/crypto v0.23.2
go: downloading github.com/go-chi/chi v4.1.2+incompatible
go: downloading github.com/smallstep/nosql v0.5.0
go: downloading github.com/tailscale/tscert v0.0.0-20230124224810-c6dc1f4049b2
go: downloading github.com/dustin/go-humanize v1.0.1
go: downloading gopkg.in/natefinch/lumberjack.v2 v2.2.1
go: downloading github.com/google/cel-go v0.13.0
go: downloading google.golang.org/genproto v0.0.0-20230202175211-008b39050e57
go: downloading golang.org/x/sync v0.1.0
go: downloading github.com/klauspost/compress v1.15.15
go: downloading github.com/BurntSushi/toml v1.2.1
go: downloading github.com/Masterminds/sprig/v3 v3.2.3
go: downloading github.com/alecthomas/chroma/v2 v2.5.0
go: downloading github.com/yuin/goldmark v1.5.4
go: downloading github.com/yuin/goldmark-highlighting/v2 v2.0.0-20220924101305-151362477c87
go: downloading go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.39.0
go: downloading go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.4.0
go: downloading go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.4.0
go: downloading go.opentelemetry.io/otel v1.13.0
go: downloading go.opentelemetry.io/otel/sdk v1.13.0
go: downloading github.com/russross/blackfriday/v2 v2.1.0
go: downloading howett.net/plist v1.0.0
go: downloading github.com/pkg/errors v0.9.1
go: downloading github.com/fxamacker/cbor/v2 v2.4.0
go: downloading github.com/slackhq/nebula v1.6.1
go: downloading go.step.sm/linkedca v0.19.0
go: downloading go.step.sm/cli-utils v0.7.5
go: downloading google.golang.org/grpc v1.52.3
go: downloading gopkg.in/square/go-jose.v2 v2.6.0
go: downloading github.com/antlr/antlr4/runtime/Go/antlr v1.4.10
go: downloading github.com/stoewer/go-strcase v1.2.0
go: downloading github.com/Masterminds/goutils v1.1.1
go: downloading github.com/Masterminds/semver/v3 v3.2.0
go: downloading github.com/huandu/xstrings v1.3.3
go: downloading github.com/imdario/mergo v0.3.12
go: downloading github.com/mitchellh/copystructure v1.2.0
go: downloading github.com/shopspring/decimal v1.2.0
go: downloading github.com/spf13/cast v1.4.1
go: downloading github.com/felixge/httpsnoop v1.0.3
go: downloading go.opentelemetry.io/otel/metric v0.36.0
go: downloading go.opentelemetry.io/otel/trace v1.13.0
go: downloading go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.4.0
go: downloading go.opentelemetry.io/proto/otlp v0.12.0
go: downloading filippo.io/edwards25519 v1.0.0
go: downloading github.com/x448/float16 v0.8.4
go: downloading github.com/rs/xid v1.4.0
go: downloading github.com/sirupsen/logrus v1.9.0
go: downloading github.com/micromdm/scep/v2 v2.1.0
go: downloading go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352
go: downloading github.com/urfave/cli v1.22.12
go: downloading github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
go: downloading github.com/manifoldco/promptui v0.9.0
go: downloading github.com/Microsoft/go-winio v0.6.0
go: downloading github.com/mitchellh/go-ps v1.0.0
go: downloading github.com/dgraph-io/badger v1.6.2
go: downloading github.com/dgraph-io/badger/v2 v2.2007.4
go: downloading go.etcd.io/bbolt v1.3.6
go: downloading github.com/go-sql-driver/mysql v1.6.0
go: downloading github.com/jackc/pgx/v4 v4.17.2
go: downloading github.com/mitchellh/reflectwalk v1.0.2
go: downloading github.com/dlclark/regexp2 v1.7.0
go: downloading github.com/go-logr/logr v1.2.3
go: downloading github.com/cenkalti/backoff/v4 v4.1.2
go: downloading github.com/go-logr/stdr v1.2.2
go: downloading github.com/grpc-ecosystem/grpc-gateway v1.16.0
go: downloading github.com/go-kit/kit v0.10.0
go: downloading github.com/dgraph-io/ristretto v0.1.0
go: downloading github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13
go: downloading github.com/jackc/pgconn v1.13.0
go: downloading github.com/jackc/pgtype v1.12.0
go: downloading github.com/jackc/pgio v1.0.0
go: downloading github.com/jackc/pgproto3/v2 v2.3.1
go: downloading github.com/go-logfmt/logfmt v0.5.1
go: downloading github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96
go: downloading github.com/golang/snappy v0.0.4
go: downloading github.com/jackc/chunkreader/v2 v2.0.1
go: downloading github.com/jackc/pgpassfile v1.0.0
go: downloading github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b
go: downloading github.com/golang/glog v1.0.0
go: downloading github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
go: downloading github.com/shurcooL/sanitized_anchor_name v1.0.0
go: downloading github.com/mattn/go-colorable v0.1.8
go: downloading github.com/mattn/go-isatty v0.0.13
^C2023/02/20 22:13:52 [INFO] SIGINT: Shutting down
2023/02/20 22:13:52 [FATAL] context canceled

"context canceled" when trying to run echo via cmd.exe on windows

I know this is early days but I tried it out on windows using caddyfile, and as far as I can tell that forces me to run in background mode.

{
    events {
        on cached_managed_cert exec cmd.exe /c echo
    }
    debug
}



my.domain.dev {
	handle {
		respond "Site not served from here"
	}
}

When I run this i get

2022/10/12 12:49:36.115 DEBUG   events  event   {"name": "cached_managed_cert", "id": "4e95e5ce-51f9-4c36-83cd-344476eb0cbe", "origin": "tls", "data": {"sans":["my.domain.dev"]}}
2022/10/12 12:49:36.122 INFO    tls     finished cleaning storage units
2022/10/12 12:49:36.130 ERROR   events.handlers.exec    background command failed       {"error": "context canceled"}

How do I avoid getting the context canceled using caddyfile syntax?

If I do a similar thing, but without running in a goroutine I get something usable.

// main.go
package main

import (
	"context"
	"fmt"
	"os"
	"os/exec"
	"time"
)

func main() {
	fmt.Println("exec called")
	ctx := context.Background()
	var cancel context.CancelFunc
	ctx, cancel = context.WithTimeout(ctx, 5*time.Second)
	cm := exec.CommandContext(ctx, "cmd.exe", "/C", "echo")
	defer cancel()

	cm.Stdout = os.Stdout
	cm.Stderr = os.Stderr

	if err := cm.Run(); err != nil {
		fmt.Printf("command failed: %v", err)
	}
}
go run ./
exec called
ECHO is on.

Config Question

I have this in my global config
events {
on cert_obtained exec /usr/local/bin/myscript
}

Since this is in the global config, this will be triggered when ANY cert is obtained, right? What if I only want it triggered when a cert for a specific domain is obtained? I tried putting it in a specific site block, but I got an error saying unrecognized directive: events.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.