mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-17 13:29:35 +00:00
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 <wankhedevijay04@gmail.com>
This commit is contained in:
parent
32bba15459
commit
005bf9a009
3 changed files with 125 additions and 0 deletions
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -448,7 +448,11 @@ func (iss *ACMEIssuer) generateZeroSSLEABCredentials(ctx context.Context, acct a
|
|||
// root_common_name <common_names...>
|
||||
// any_common_name <common_names...>
|
||||
// }
|
||||
// network_proxy <module_name> [<options>]
|
||||
// }
|
||||
//
|
||||
// The network_proxy module (for example, "url <proxy_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())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue