From 005bf9a0096a9dbe8d5ce50feb88240b0dbc29ab Mon Sep 17 00:00:00 2001 From: wankhede04 Date: Thu, 29 Oct 2026 18:39:30 +0530 Subject: [PATCH] caddytls: support network_proxy in the Caddyfile ACME issuer ACMEIssuer already has a network_proxy JSON field and the Caddyfile adapter already merges it, but UnmarshalCaddyfile rejected the subdirective, so a forward proxy could not be scoped to ACME requests from a Caddyfile. Parse it the same way reverse_proxy does, using the caddy.network_proxy module namespace, and document it in the syntax comment. Add a unit test and an adapt fixture. Signed-off-by: wankhede04 --- .../tls_acme_network_proxy.caddyfiletest | 53 ++++++++++++++++++ modules/caddytls/acmeissuer.go | 16 ++++++ modules/caddytls/acmeissuer_test.go | 56 +++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 caddytest/integration/caddyfile_adapt/tls_acme_network_proxy.caddyfiletest diff --git a/caddytest/integration/caddyfile_adapt/tls_acme_network_proxy.caddyfiletest b/caddytest/integration/caddyfile_adapt/tls_acme_network_proxy.caddyfiletest new file mode 100644 index 000000000..4e6c3fe38 --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/tls_acme_network_proxy.caddyfiletest @@ -0,0 +1,53 @@ +localhost + +tls { + issuer acme { + network_proxy url http://localhost:8080 + } +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "localhost" + ] + } + ], + "terminal": true + } + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "subjects": [ + "localhost" + ], + "issuers": [ + { + "module": "acme", + "network_proxy": { + "from": "url", + "url": "http://localhost:8080" + } + } + ] + } + ] + } + } + } +} diff --git a/modules/caddytls/acmeissuer.go b/modules/caddytls/acmeissuer.go index 396e765da..32135317a 100644 --- a/modules/caddytls/acmeissuer.go +++ b/modules/caddytls/acmeissuer.go @@ -448,7 +448,11 @@ func (iss *ACMEIssuer) generateZeroSSLEABCredentials(ctx context.Context, acct a // root_common_name // any_common_name // } +// network_proxy [] // } +// +// The network_proxy module (for example, "url " or "none") sets +// the forward proxy used for this issuer's requests to the ACME server. func (iss *ACMEIssuer) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { d.Next() // consume issuer name @@ -693,6 +697,18 @@ func (iss *ACMEIssuer) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { } iss.PreferredChains = chainPref + case "network_proxy": + if !d.NextArg() { + return d.ArgErr() + } + modStem := d.Val() + modID := "caddy.network_proxy." + modStem + unm, err := caddyfile.UnmarshalModule(d, modID) + if err != nil { + return err + } + iss.NetworkProxyRaw = caddyconfig.JSONModuleObject(unm, "from", modStem, nil) + default: return d.Errf("unrecognized ACME issuer property: %s", d.Val()) } diff --git a/modules/caddytls/acmeissuer_test.go b/modules/caddytls/acmeissuer_test.go index 55a74ede3..be5f0894b 100644 --- a/modules/caddytls/acmeissuer_test.go +++ b/modules/caddytls/acmeissuer_test.go @@ -13,6 +13,7 @@ import ( "time" "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" _ "github.com/caddyserver/caddy/v2/modules/internal/network" "github.com/caddyserver/certmagic" "github.com/mholt/acmez/v3/acme" @@ -129,3 +130,58 @@ func TestACMEIssuerNetworkProxyAfterRebuildCertMagic(t *testing.T) { t.Error("ACME request did not go through the configured network proxy") } } + +func TestACMEIssuerUnmarshalCaddyfileNetworkProxy(t *testing.T) { + for i, tc := range []struct { + name string + input string + wantRaw string + wantErr bool + }{ + { + name: "url", + input: "acme {\n\tnetwork_proxy url http://localhost:8080\n}", + wantRaw: `{"from":"url","url":"http://localhost:8080"}`, + }, + { + name: "url with placeholder", + input: "acme {\n\tnetwork_proxy url {env.ACME_PROXY}\n}", + wantRaw: `{"from":"url","url":"{env.ACME_PROXY}"}`, + }, + { + name: "none", + input: "acme {\n\tnetwork_proxy none\n}", + wantRaw: `{"from":"none"}`, + }, + { + name: "not set", + input: "acme {\n\temail admin@example.com\n}", + }, + { + name: "missing module name", + input: "acme {\n\tnetwork_proxy\n}", + wantErr: true, + }, + { + name: "unknown module name", + input: "acme {\n\tnetwork_proxy bogus\n}", + wantErr: true, + }, + } { + var iss ACMEIssuer + err := iss.UnmarshalCaddyfile(caddyfile.NewTestDispenser(tc.input)) + if tc.wantErr { + if err == nil { + t.Errorf("Test %d (%s): expected error but got none", i, tc.name) + } + continue + } + if err != nil { + t.Errorf("Test %d (%s): unexpected error: %v", i, tc.name, err) + continue + } + if got := string(iss.NetworkProxyRaw); got != tc.wantRaw { + t.Errorf("Test %d (%s): NetworkProxyRaw = %q, want %q", i, tc.name, got, tc.wantRaw) + } + } +}