From 8b327005b3105a5a70c8ace5cfe7e1f83e148f7c Mon Sep 17 00:00:00 2001 From: Simon Merschjohann Date: Mon, 22 Dec 2025 03:50:29 +0100 Subject: [PATCH] Add DNS Provider for ISPConfig (DDNS Module) (#2760) Co-authored-by: Fernandez Ludovic --- README.md | 48 ++--- cmd/zz_gen_cmd_dnshelp.go | 24 ++- docs/content/dns/zz_gen_ispconfig.md | 6 +- docs/content/dns/zz_gen_ispconfigddns.md | 74 +++++++ docs/data/zz_cli_help.toml | 2 +- providers/dns/ispconfig/ispconfig.toml | 2 +- .../dns/ispconfigddns/internal/client.go | 111 ++++++++++ .../dns/ispconfigddns/internal/client_test.go | 83 ++++++++ providers/dns/ispconfigddns/internal/types.go | 9 + providers/dns/ispconfigddns/ispconfigddns.go | 145 +++++++++++++ .../dns/ispconfigddns/ispconfigddns.toml | 32 +++ .../dns/ispconfigddns/ispconfigddns_test.go | 193 ++++++++++++++++++ providers/dns/zz_gen_dns_providers.go | 3 + 13 files changed, 702 insertions(+), 30 deletions(-) create mode 100644 docs/content/dns/zz_gen_ispconfigddns.md create mode 100644 providers/dns/ispconfigddns/internal/client.go create mode 100644 providers/dns/ispconfigddns/internal/client_test.go create mode 100644 providers/dns/ispconfigddns/internal/types.go create mode 100644 providers/dns/ispconfigddns/ispconfigddns.go create mode 100644 providers/dns/ispconfigddns/ispconfigddns.toml create mode 100644 providers/dns/ispconfigddns/ispconfigddns_test.go diff --git a/README.md b/README.md index 5530028cb..5fa9c1ed1 100644 --- a/README.md +++ b/README.md @@ -169,120 +169,120 @@ Detailed documentation is available [here](https://go-acme.github.io/lego/dns). Ionos Cloud IPv64 - ISPConfig + ISPConfig 3 + ISPConfig 3 - Dynamic DNS (DDNS) Module iwantmyname (Deprecated) Joker - Joohoi's ACME-DNS + Joohoi's ACME-DNS KeyHelp Liara Lima-City - Linode (v4) + Linode (v4) Liquid Web Loopia LuaDNS - Mail-in-a-Box + Mail-in-a-Box ManageEngine CloudDNS Manual Metaname - Metaregistrar + Metaregistrar mijn.host Mittwald myaddr.{tools,dev,io} - MyDNS.jp + MyDNS.jp MythicBeasts Name.com Namecheap - Namesilo + Namesilo NearlyFreeSpeech.NET Neodigit Netcup - Netlify + Netlify Nicmanager NIFCloud Njalla - Nodion + Nodion NS1 Octenium Open Telekom Cloud - Oracle Cloud + Oracle Cloud OVH plesk.com Porkbun - PowerDNS + PowerDNS Rackspace Rain Yun/雨云 RcodeZero - reg.ru + reg.ru Regfish RFC2136 RimuHosting - RU CENTER + RU CENTER Sakura Cloud Scaleway Selectel - Selectel v2 + Selectel v2 SelfHost.(de|eu) Servercow Shellrent - Simply.com + Simply.com Sonic Spaceship Stackpath - Syse + Syse Technitium Tencent Cloud DNS Tencent EdgeOne - Timeweb Cloud + Timeweb Cloud TransIP UKFast SafeDNS Ultradns - United-Domains + United-Domains Variomedia VegaDNS Vercel - Versio.[nl|eu|uk] + Versio.[nl|eu|uk] VinylDNS Virtualname VK Cloud - Volcano Engine/火山引擎 + Volcano Engine/火山引擎 Vscale Vultr webnames.ca - webnames.ru + webnames.ru Websupport WEDOS West.cn/西部数码 - Yandex 360 + Yandex 360 Yandex Cloud Yandex PDD Zone.ee - ZoneEdit + ZoneEdit Zonomi - diff --git a/cmd/zz_gen_cmd_dnshelp.go b/cmd/zz_gen_cmd_dnshelp.go index ac0a52427..7677818c9 100644 --- a/cmd/zz_gen_cmd_dnshelp.go +++ b/cmd/zz_gen_cmd_dnshelp.go @@ -101,6 +101,7 @@ func allDNSCodes() string { "ionoscloud", "ipv64", "ispconfig", + "ispconfigddns", "iwantmyname", "joker", "keyhelp", @@ -2086,7 +2087,7 @@ func displayDNSHelp(w io.Writer, name string) error { case "ispconfig": // generated from: providers/dns/ispconfig/ispconfig.toml - ew.writeln(`Configuration for ISPConfig.`) + ew.writeln(`Configuration for ISPConfig 3.`) ew.writeln(`Code: 'ispconfig'`) ew.writeln(`Since: 'v4.31.0'`) ew.writeln() @@ -2107,6 +2108,27 @@ func displayDNSHelp(w io.Writer, name string) error { ew.writeln() ew.writeln(`More information: https://go-acme.github.io/lego/dns/ispconfig`) + case "ispconfigddns": + // generated from: providers/dns/ispconfigddns/ispconfigddns.toml + ew.writeln(`Configuration for ISPConfig 3 - Dynamic DNS (DDNS) Module.`) + ew.writeln(`Code: 'ispconfigddns'`) + ew.writeln(`Since: 'v4.31.0'`) + ew.writeln() + + ew.writeln(`Credentials:`) + ew.writeln(` - "ISPCONFIG_DDNS_SERVER_URL": API server URL (ex: https://panel.example.com:8080)`) + ew.writeln(` - "ISPCONFIG_DDNS_TOKEN": DDNS API token`) + ew.writeln() + + ew.writeln(`Additional Configuration:`) + ew.writeln(` - "ISPCONFIG_DDNS_HTTP_TIMEOUT": API request timeout in seconds (Default: 30)`) + ew.writeln(` - "ISPCONFIG_DDNS_POLLING_INTERVAL": Time between DNS propagation check in seconds (Default: 2)`) + ew.writeln(` - "ISPCONFIG_DDNS_PROPAGATION_TIMEOUT": Maximum waiting time for DNS propagation in seconds (Default: 60)`) + ew.writeln(` - "ISPCONFIG_DDNS_TTL": The TTL of the TXT record used for the DNS challenge in seconds (Default: 3600)`) + + ew.writeln() + ew.writeln(`More information: https://go-acme.github.io/lego/dns/ispconfigddns`) + case "iwantmyname": // generated from: providers/dns/iwantmyname/iwantmyname.toml ew.writeln(`Configuration for iwantmyname (Deprecated).`) diff --git a/docs/content/dns/zz_gen_ispconfig.md b/docs/content/dns/zz_gen_ispconfig.md index 96b08a8e0..bd3b375da 100644 --- a/docs/content/dns/zz_gen_ispconfig.md +++ b/docs/content/dns/zz_gen_ispconfig.md @@ -1,5 +1,5 @@ --- -title: "ISPConfig" +title: "ISPConfig 3" date: 2019-03-03T16:39:46+01:00 draft: false slug: ispconfig @@ -14,7 +14,7 @@ dnsprovider: -Configuration for [ISPConfig](https://www.ispconfig.org/). +Configuration for [ISPConfig 3](https://www.ispconfig.org/). @@ -23,7 +23,7 @@ Configuration for [ISPConfig](https://www.ispconfig.org/). - Since: v4.31.0 -Here is an example bash command using the ISPConfig provider: +Here is an example bash command using the ISPConfig 3 provider: ```bash ISPCONFIG_SERVER_URL="https://example.com:8080/remote/json.php" \ diff --git a/docs/content/dns/zz_gen_ispconfigddns.md b/docs/content/dns/zz_gen_ispconfigddns.md new file mode 100644 index 000000000..c59bddda4 --- /dev/null +++ b/docs/content/dns/zz_gen_ispconfigddns.md @@ -0,0 +1,74 @@ +--- +title: "ISPConfig 3 - Dynamic DNS (DDNS) Module" +date: 2019-03-03T16:39:46+01:00 +draft: false +slug: ispconfigddns +dnsprovider: + since: "v4.31.0" + code: "ispconfigddns" + url: "https://www.ispconfig.org/" +--- + + + + + + +Configuration for [ISPConfig 3 - Dynamic DNS (DDNS) Module](https://www.ispconfig.org/). + + + + +- Code: `ispconfigddns` +- Since: v4.31.0 + + +Here is an example bash command using the ISPConfig 3 - Dynamic DNS (DDNS) Module provider: + +```bash +ISPCONFIG_DDNS_SERVER_URL="https://panel.example.com:8080" \ +ISPCONFIG_DDNS_TOKEN=xxxxxx \ +lego --email you@example.com --dns ispconfigddns -d '*.example.com' -d example.com run +``` + + + + +## Credentials + +| Environment Variable Name | Description | +|-----------------------|-------------| +| `ISPCONFIG_DDNS_SERVER_URL` | API server URL (ex: https://panel.example.com:8080) | +| `ISPCONFIG_DDNS_TOKEN` | DDNS API token | + +The environment variable names can be suffixed by `_FILE` to reference a file instead of a value. +More information [here]({{% ref "dns#configuration-and-credentials" %}}). + + +## Additional Configuration + +| Environment Variable Name | Description | +|--------------------------------|-------------| +| `ISPCONFIG_DDNS_HTTP_TIMEOUT` | API request timeout in seconds (Default: 30) | +| `ISPCONFIG_DDNS_POLLING_INTERVAL` | Time between DNS propagation check in seconds (Default: 2) | +| `ISPCONFIG_DDNS_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation in seconds (Default: 60) | +| `ISPCONFIG_DDNS_TTL` | The TTL of the TXT record used for the DNS challenge in seconds (Default: 3600) | + +The environment variable names can be suffixed by `_FILE` to reference a file instead of a value. +More information [here]({{% ref "dns#configuration-and-credentials" %}}). + +ISPConfig DNS provider supports leveraging the [ISPConfig 3 Dynamic DNS (DDNS) Module](https://github.com/mhofer117/ispconfig-ddns-module). + +Requires the DDNS module described at https://www.ispconfig.org/ispconfig/download/ + +See https://www.howtoforge.com/community/threads/ispconfig-3-danymic-dns-ddns-module.87967/ for additional details. + + + +## More information + +- [API documentation](https://github.com/mhofer117/ispconfig-ddns-module/tree/master/lib/updater) + + + + diff --git a/docs/data/zz_cli_help.toml b/docs/data/zz_cli_help.toml index 7de6ed5a1..ae2369a9a 100644 --- a/docs/data/zz_cli_help.toml +++ b/docs/data/zz_cli_help.toml @@ -152,7 +152,7 @@ To display the documentation for a specific DNS provider, run: $ lego dnshelp -c code Supported DNS providers: - acme-dns, active24, alidns, aliesa, allinkl, anexia, arvancloud, auroradns, autodns, axelname, azion, azure, azuredns, baiducloud, beget, binarylane, bindman, bluecat, bookmyname, brandit, bunny, checkdomain, civo, clouddns, cloudflare, cloudns, cloudru, cloudxns, conoha, conohav3, constellix, corenetworks, cpanel, derak, desec, designate, digitalocean, directadmin, dnshomede, dnsimple, dnsmadeeasy, dnspod, dode, domeneshop, dreamhost, duckdns, dyn, dyndnsfree, dynu, easydns, edgecenter, edgedns, edgeone, efficientip, epik, exec, exoscale, f5xc, freemyip, gandi, gandiv5, gcloud, gcore, gigahostno, glesys, godaddy, googledomains, gravity, hetzner, hostingde, hostinger, hostingnl, hosttech, httpnet, httpreq, huaweicloud, hurricane, hyperone, ibmcloud, iij, iijdpf, infoblox, infomaniak, internetbs, inwx, ionos, ionoscloud, ipv64, ispconfig, iwantmyname, joker, keyhelp, liara, lightsail, limacity, linode, liquidweb, loopia, luadns, mailinabox, manageengine, manual, metaname, metaregistrar, mijnhost, mittwald, myaddr, mydnsjp, mythicbeasts, namecheap, namedotcom, namesilo, nearlyfreespeech, neodigit, netcup, netlify, nicmanager, nicru, nifcloud, njalla, nodion, ns1, octenium, oraclecloud, otc, ovh, pdns, plesk, porkbun, rackspace, rainyun, rcodezero, regfish, regru, rfc2136, rimuhosting, route53, safedns, sakuracloud, scaleway, selectel, selectelv2, selfhostde, servercow, shellrent, simply, sonic, spaceship, stackpath, syse, technitium, tencentcloud, timewebcloud, transip, ultradns, uniteddomains, variomedia, vegadns, vercel, versio, vinyldns, virtualname, vkcloud, volcengine, vscale, vultr, webnames, webnamesca, websupport, wedos, westcn, yandex, yandex360, yandexcloud, zoneedit, zoneee, zonomi + acme-dns, active24, alidns, aliesa, allinkl, anexia, arvancloud, auroradns, autodns, axelname, azion, azure, azuredns, baiducloud, beget, binarylane, bindman, bluecat, bookmyname, brandit, bunny, checkdomain, civo, clouddns, cloudflare, cloudns, cloudru, cloudxns, conoha, conohav3, constellix, corenetworks, cpanel, derak, desec, designate, digitalocean, directadmin, dnshomede, dnsimple, dnsmadeeasy, dnspod, dode, domeneshop, dreamhost, duckdns, dyn, dyndnsfree, dynu, easydns, edgecenter, edgedns, edgeone, efficientip, epik, exec, exoscale, f5xc, freemyip, gandi, gandiv5, gcloud, gcore, gigahostno, glesys, godaddy, googledomains, gravity, hetzner, hostingde, hostinger, hostingnl, hosttech, httpnet, httpreq, huaweicloud, hurricane, hyperone, ibmcloud, iij, iijdpf, infoblox, infomaniak, internetbs, inwx, ionos, ionoscloud, ipv64, ispconfig, ispconfigddns, iwantmyname, joker, keyhelp, liara, lightsail, limacity, linode, liquidweb, loopia, luadns, mailinabox, manageengine, manual, metaname, metaregistrar, mijnhost, mittwald, myaddr, mydnsjp, mythicbeasts, namecheap, namedotcom, namesilo, nearlyfreespeech, neodigit, netcup, netlify, nicmanager, nicru, nifcloud, njalla, nodion, ns1, octenium, oraclecloud, otc, ovh, pdns, plesk, porkbun, rackspace, rainyun, rcodezero, regfish, regru, rfc2136, rimuhosting, route53, safedns, sakuracloud, scaleway, selectel, selectelv2, selfhostde, servercow, shellrent, simply, sonic, spaceship, stackpath, syse, technitium, tencentcloud, timewebcloud, transip, ultradns, uniteddomains, variomedia, vegadns, vercel, versio, vinyldns, virtualname, vkcloud, volcengine, vscale, vultr, webnames, webnamesca, websupport, wedos, westcn, yandex, yandex360, yandexcloud, zoneedit, zoneee, zonomi More information: https://go-acme.github.io/lego/dns """ diff --git a/providers/dns/ispconfig/ispconfig.toml b/providers/dns/ispconfig/ispconfig.toml index a1cb89210..399544742 100644 --- a/providers/dns/ispconfig/ispconfig.toml +++ b/providers/dns/ispconfig/ispconfig.toml @@ -1,4 +1,4 @@ -Name = "ISPConfig" +Name = "ISPConfig 3" Description = '''''' URL = "https://www.ispconfig.org/" Code = "ispconfig" diff --git a/providers/dns/ispconfigddns/internal/client.go b/providers/dns/ispconfigddns/internal/client.go new file mode 100644 index 000000000..700b58f89 --- /dev/null +++ b/providers/dns/ispconfigddns/internal/client.go @@ -0,0 +1,111 @@ +package internal + +import ( + "context" + "fmt" + "io" + "net/http" + "net/url" + "time" + + "github.com/go-acme/lego/v4/providers/dns/internal/errutils" + "github.com/go-acme/lego/v4/providers/dns/internal/useragent" + querystring "github.com/google/go-querystring/query" +) + +const ( + addAction = "add" + deleteAction = "delete" +) + +type Client struct { + token string + serverURL string + + HTTPClient *http.Client +} + +func NewClient(serverURL, token string) (*Client, error) { + _, err := url.Parse(serverURL) + if err != nil { + return nil, fmt.Errorf("server URL: %w", err) + } + + return &Client{ + serverURL: serverURL, + token: token, + HTTPClient: &http.Client{Timeout: 10 * time.Second}, + }, nil +} + +func (c *Client) AddTXTRecord(ctx context.Context, zone, fqdn, content string) error { + return c.updateRecord(ctx, UpdateRecord{Action: addAction, Zone: zone, Type: "TXT", Record: fqdn, Data: content}) +} + +func (c *Client) DeleteTXTRecord(ctx context.Context, zone, fqdn, recordContent string) error { + return c.updateRecord(ctx, UpdateRecord{Action: deleteAction, Zone: zone, Type: "TXT", Record: fqdn, Data: recordContent}) +} + +func (c *Client) updateRecord(ctx context.Context, action UpdateRecord) error { + req, err := c.newRequest(ctx, action) + if err != nil { + return err + } + + return c.do(req) +} + +func (c *Client) do(req *http.Request) error { + useragent.SetHeader(req.Header) + + req.SetBasicAuth("anonymous", c.token) + + resp, err := c.HTTPClient.Do(req) + if err != nil { + return errutils.NewHTTPDoError(req, err) + } + + defer func() { _ = resp.Body.Close() }() + + // The endpoint uses the `DefaultDdnsResponseWriter`, + // and this writer uses HTTP status code to determine if the request was successful or not. + // - https://github.com/mhofer117/ispconfig-ddns-module/blob/8b011a5bb138881d9f13360a5c4fec10c0084613/lib/updater/DdnsUpdater.php#L53-L57 + // - https://github.com/mhofer117/ispconfig-ddns-module/blob/master/lib/updater/response/DefaultDdnsResponseWriter.php + if resp.StatusCode/100 != 2 { + raw, _ := io.ReadAll(resp.Body) + + return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw) + } + + return nil +} + +func (c *Client) newRequest(ctx context.Context, action UpdateRecord) (*http.Request, error) { + endpoint, err := url.Parse(c.serverURL) + if err != nil { + return nil, err + } + + endpoint = endpoint.JoinPath("ddns", "update.php") + + values, err := querystring.Values(action) + if err != nil { + return nil, err + } + + endpoint.RawQuery = values.Encode() + + method := http.MethodPost + if action.Action == deleteAction { + method = http.MethodDelete + } + + req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), nil) + if err != nil { + return nil, err + } + + req.Header.Set("Accept", "application/json") + + return req, nil +} diff --git a/providers/dns/ispconfigddns/internal/client_test.go b/providers/dns/ispconfigddns/internal/client_test.go new file mode 100644 index 000000000..774e5ee46 --- /dev/null +++ b/providers/dns/ispconfigddns/internal/client_test.go @@ -0,0 +1,83 @@ +package internal + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-acme/lego/v4/platform/tester/servermock" + "github.com/stretchr/testify/require" +) + +func setupClient(server *httptest.Server) (*Client, error) { + client, err := NewClient(server.URL, "secret") + if err != nil { + return nil, err + } + + client.HTTPClient = server.Client() + + return client, nil +} + +func TestClient_AddTXTRecord(t *testing.T) { + client := servermock.NewBuilder[*Client](setupClient). + Route("POST /ddns/update.php", + servermock.Noop(), + servermock.CheckHeader(). + WithBasicAuth("anonymous", "secret"), + servermock.CheckQueryParameter().Strict(). + With("action", "add"). + With("zone", "example.com"). + With("type", "TXT"). + With("record", "_acme-challenge.example.com."). + With("data", "token"), + ). + Build(t) + + err := client.AddTXTRecord(t.Context(), "example.com", "_acme-challenge.example.com.", "token") + require.NoError(t, err) +} + +func TestClient_AddTXTRecord_error(t *testing.T) { + client := servermock.NewBuilder[*Client](setupClient). + Route("POST /ddns/update.php", + servermock.RawStringResponse("Missing or invalid token."). + WithStatusCode(http.StatusUnauthorized), + ). + Build(t) + + err := client.AddTXTRecord(t.Context(), "example.com", "_acme-challenge.example.com.", "token") + require.EqualError(t, err, "unexpected status code: [status code: 401] body: Missing or invalid token.") +} + +func TestClient_DeleteTXTRecord(t *testing.T) { + client := servermock.NewBuilder[*Client](setupClient). + Route("DELETE /ddns/update.php", + servermock.Noop(), + servermock.CheckHeader(). + WithBasicAuth("anonymous", "secret"), + servermock.CheckQueryParameter().Strict(). + With("action", "delete"). + With("zone", "example.com"). + With("type", "TXT"). + With("record", "_acme-challenge.example.com."). + With("data", "token"), + ). + Build(t) + + err := client.DeleteTXTRecord(t.Context(), "example.com", "_acme-challenge.example.com.", "token") + require.NoError(t, err) +} + +func TestClient_DeleteTXTRecord_error(t *testing.T) { + client := servermock.NewBuilder[*Client](setupClient). + Route("DELETE /ddns/update.php", + servermock.RawStringResponse("Missing or invalid token."). + WithStatusCode(http.StatusUnauthorized), + ). + Build(t) + + err := client.DeleteTXTRecord(t.Context(), "example.com", "_acme-challenge.example.com.", "token") + require.EqualError(t, err, "unexpected status code: [status code: 401] body: Missing or invalid token.") +} diff --git a/providers/dns/ispconfigddns/internal/types.go b/providers/dns/ispconfigddns/internal/types.go new file mode 100644 index 000000000..278738108 --- /dev/null +++ b/providers/dns/ispconfigddns/internal/types.go @@ -0,0 +1,9 @@ +package internal + +type UpdateRecord struct { + Action string `url:"action,omitempty"` + Zone string `url:"zone,omitempty"` + Type string `url:"type,omitempty"` + Record string `url:"record,omitempty"` + Data string `url:"data,omitempty"` +} diff --git a/providers/dns/ispconfigddns/ispconfigddns.go b/providers/dns/ispconfigddns/ispconfigddns.go new file mode 100644 index 000000000..eab5d413f --- /dev/null +++ b/providers/dns/ispconfigddns/ispconfigddns.go @@ -0,0 +1,145 @@ +// Package ispconfigddns implements a DNS provider for solving the DNS-01 challenge using ISPConfig 3 Dynamic DNS (DDNS) Module. +package ispconfigddns + +import ( + "context" + "errors" + "fmt" + "net/http" + "time" + + "github.com/go-acme/lego/v4/challenge" + "github.com/go-acme/lego/v4/challenge/dns01" + "github.com/go-acme/lego/v4/platform/config/env" + "github.com/go-acme/lego/v4/providers/dns/internal/clientdebug" + "github.com/go-acme/lego/v4/providers/dns/ispconfigddns/internal" +) + +// Environment variables names. +const ( + envNamespace = "ISPCONFIG_DDNS_" + + EnvServerURL = envNamespace + "SERVER_URL" + EnvToken = envNamespace + "TOKEN" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + +var _ challenge.ProviderTimeout = (*DNSProvider)(nil) + +// Config is used to configure the creation of the DNSProvider. +type Config struct { + ServerURL string + Token string + + PropagationTimeout time.Duration + PollingInterval time.Duration + TTL int + HTTPClient *http.Client +} + +// NewDefaultConfig returns a default configuration for the DNSProvider. +func NewDefaultConfig() *Config { + return &Config{ + TTL: env.GetOrDefaultInt(EnvTTL, 3600), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), + HTTPClient: &http.Client{ + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), + }, + } +} + +// DNSProvider implements the challenge.Provider interface. +type DNSProvider struct { + config *Config + client *internal.Client +} + +// NewDNSProvider returns a DNSProvider instance configured for ISPConfig 3 Dynamic DNS (DDNS) Module. +func NewDNSProvider() (*DNSProvider, error) { + values, err := env.Get(EnvServerURL, EnvToken) + if err != nil { + return nil, fmt.Errorf("ispconfig (DDNS module): %w", err) + } + + config := NewDefaultConfig() + config.ServerURL = values[EnvServerURL] + config.Token = values[EnvToken] + + return NewDNSProviderConfig(config) +} + +// NewDNSProviderConfig return a DNSProvider instance configured for ISPConfig 3 Dynamic DNS (DDNS) Module. +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { + if config == nil { + return nil, errors.New("ispconfig (DDNS module): the configuration of the DNS provider is nil") + } + + if config.ServerURL == "" { + return nil, errors.New("ispconfig (DDNS module): missing server URL") + } + + if config.Token == "" { + return nil, errors.New("ispconfig (DDNS module): missing token") + } + + client, err := internal.NewClient(config.ServerURL, config.Token) + if err != nil { + return nil, fmt.Errorf("ispconfig (DDNS module): %w", err) + } + + if config.HTTPClient != nil { + client.HTTPClient = config.HTTPClient + } + + client.HTTPClient = clientdebug.Wrap(client.HTTPClient) + + return &DNSProvider{ + config: config, + client: client, + }, nil +} + +// Timeout returns the timeout and interval to use when checking for DNS propagation. +// Adjusting here to control checking compliance to spec. +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { + return d.config.PropagationTimeout, d.config.PollingInterval +} + +// Present creates a TXT record to fulfill the dns-01 challenge. +func (d *DNSProvider) Present(domain, token, keyAuth string) error { + info := dns01.GetChallengeInfo(domain, keyAuth) + + zone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) + if err != nil { + return fmt.Errorf("ispconfig (DDNS module): could not find zone for domain %q: %w", domain, err) + } + + err = d.client.AddTXTRecord(context.Background(), dns01.UnFqdn(zone), info.EffectiveFQDN, info.Value) + if err != nil { + return fmt.Errorf("ispconfig (DDNS module): add record: %w", err) + } + + return nil +} + +// CleanUp removes the TXT record matching the specified parameters. +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { + info := dns01.GetChallengeInfo(domain, keyAuth) + + zone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) + if err != nil { + return fmt.Errorf("ispconfig (DDNS module): could not find zone for domain %q: %w", domain, err) + } + + err = d.client.DeleteTXTRecord(context.Background(), dns01.UnFqdn(zone), info.EffectiveFQDN, info.Value) + if err != nil { + return fmt.Errorf("ispconfig (DDNS module): delete record: %w", err) + } + + return nil +} diff --git a/providers/dns/ispconfigddns/ispconfigddns.toml b/providers/dns/ispconfigddns/ispconfigddns.toml new file mode 100644 index 000000000..84e82904f --- /dev/null +++ b/providers/dns/ispconfigddns/ispconfigddns.toml @@ -0,0 +1,32 @@ +Name = "ISPConfig 3 - Dynamic DNS (DDNS) Module" +Description = '''''' +URL = "https://www.ispconfig.org/" +Code = "ispconfigddns" +Since = "v4.31.0" + +Example = ''' +ISPCONFIG_DDNS_SERVER_URL="https://panel.example.com:8080" \ +ISPCONFIG_DDNS_TOKEN=xxxxxx \ +lego --email you@example.com --dns ispconfigddns -d '*.example.com' -d example.com run +''' + +Additional = ''' +ISPConfig DNS provider supports leveraging the [ISPConfig 3 Dynamic DNS (DDNS) Module](https://github.com/mhofer117/ispconfig-ddns-module). + +Requires the DDNS module described at https://www.ispconfig.org/ispconfig/download/ + +See https://www.howtoforge.com/community/threads/ispconfig-3-danymic-dns-ddns-module.87967/ for additional details. +''' + +[Configuration] + [Configuration.Credentials] + ISPCONFIG_DDNS_SERVER_URL = "API server URL (ex: https://panel.example.com:8080)" + ISPCONFIG_DDNS_TOKEN = "DDNS API token" + [Configuration.Additional] + ISPCONFIG_DDNS_POLLING_INTERVAL = "Time between DNS propagation check in seconds (Default: 2)" + ISPCONFIG_DDNS_PROPAGATION_TIMEOUT = "Maximum waiting time for DNS propagation in seconds (Default: 60)" + ISPCONFIG_DDNS_TTL = "The TTL of the TXT record used for the DNS challenge in seconds (Default: 3600)" + ISPCONFIG_DDNS_HTTP_TIMEOUT = "API request timeout in seconds (Default: 30)" + +[Links] + API = "https://github.com/mhofer117/ispconfig-ddns-module/tree/master/lib/updater" diff --git a/providers/dns/ispconfigddns/ispconfigddns_test.go b/providers/dns/ispconfigddns/ispconfigddns_test.go new file mode 100644 index 000000000..58e7a8f54 --- /dev/null +++ b/providers/dns/ispconfigddns/ispconfigddns_test.go @@ -0,0 +1,193 @@ +package ispconfigddns + +import ( + "net/http/httptest" + "testing" + + "github.com/go-acme/lego/v4/platform/tester" + "github.com/go-acme/lego/v4/platform/tester/servermock" + "github.com/stretchr/testify/require" +) + +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvServerURL, EnvToken). + WithDomain(envDomain) + +func TestNewDNSProvider(t *testing.T) { + testCases := []struct { + desc string + envVars map[string]string + expected string + }{ + { + desc: "success", + envVars: map[string]string{ + EnvServerURL: "https://example.com", + EnvToken: "secret", + }, + }, + { + desc: "missing server URL", + envVars: map[string]string{ + EnvServerURL: "", + EnvToken: "secret", + }, + expected: "ispconfig (DDNS module): some credentials information are missing: ISPCONFIG_DDNS_SERVER_URL", + }, + { + desc: "missing token", + envVars: map[string]string{ + EnvServerURL: "https://example.com", + EnvToken: "", + }, + expected: "ispconfig (DDNS module): some credentials information are missing: ISPCONFIG_DDNS_TOKEN", + }, + { + desc: "missing credentials", + envVars: map[string]string{}, + expected: "ispconfig (DDNS module): some credentials information are missing: ISPCONFIG_DDNS_SERVER_URL,ISPCONFIG_DDNS_TOKEN", + }, + } + + for _, test := range testCases { + t.Run(test.desc, func(t *testing.T) { + defer envTest.RestoreEnv() + + envTest.ClearEnv() + + envTest.Apply(test.envVars) + + p, err := NewDNSProvider() + + if test.expected == "" { + require.NoError(t, err) + require.NotNil(t, p) + require.NotNil(t, p.config) + } else { + require.EqualError(t, err, test.expected) + } + }) + } +} + +func TestNewDNSProviderConfig(t *testing.T) { + testCases := []struct { + desc string + serverURL string + token string + expected string + }{ + { + desc: "success", + serverURL: "https://example.com", + token: "secret", + }, + { + desc: "missing server URL", + serverURL: "", + token: "secret", + expected: "ispconfig (DDNS module): missing server URL", + }, + { + desc: "missing token", + serverURL: "https://example.com", + token: "", + expected: "ispconfig (DDNS module): missing token", + }, + } + + for _, test := range testCases { + t.Run(test.desc, func(t *testing.T) { + config := NewDefaultConfig() + config.ServerURL = test.serverURL + config.Token = test.token + + p, err := NewDNSProviderConfig(config) + + if test.expected == "" { + require.NoError(t, err) + require.NotNil(t, p) + require.NotNil(t, p.config) + } else { + require.EqualError(t, err, test.expected) + } + }) + } +} + +func TestLivePresent(t *testing.T) { + if !envTest.IsLiveTest() { + t.Skip("skipping live test") + } + + envTest.RestoreEnv() + + provider, err := NewDNSProvider() + require.NoError(t, err) + + err = provider.Present(envTest.GetDomain(), "", "123d==") + require.NoError(t, err) +} + +func TestLiveCleanUp(t *testing.T) { + if !envTest.IsLiveTest() { + t.Skip("skipping live test") + } + + envTest.RestoreEnv() + + provider, err := NewDNSProvider() + require.NoError(t, err) + + err = provider.CleanUp(envTest.GetDomain(), "", "123d==") + require.NoError(t, err) +} + +func mockBuilder() *servermock.Builder[*DNSProvider] { + return servermock.NewBuilder(func(server *httptest.Server) (*DNSProvider, error) { + config := NewDefaultConfig() + config.HTTPClient = server.Client() + config.Token = "secret" + config.ServerURL = server.URL + + return NewDNSProviderConfig(config) + }, + servermock.CheckHeader(). + WithBasicAuth("anonymous", "secret"), + ) +} + +func TestDNSProvider_Present(t *testing.T) { + provider := mockBuilder(). + Route("POST /ddns/update.php", + servermock.DumpRequest(), + servermock.CheckQueryParameter().Strict(). + With("action", "add"). + With("zone", "example.com"). + With("type", "TXT"). + With("record", "_acme-challenge.example.com."). + With("data", "ADw2sEd82DUgXcQ9hNBZThJs7zVJkR5v9JeSbAb9mZY"), + ). + Build(t) + + err := provider.Present("example.com", "abc", "123d==") + require.NoError(t, err) +} + +func TestDNSProvider_CleanUp(t *testing.T) { + provider := mockBuilder(). + Route("DELETE /ddns/update.php", + servermock.DumpRequest(), + servermock.CheckQueryParameter().Strict(). + With("action", "delete"). + With("zone", "example.com"). + With("type", "TXT"). + With("record", "_acme-challenge.example.com."). + With("data", "ADw2sEd82DUgXcQ9hNBZThJs7zVJkR5v9JeSbAb9mZY"), + ). + Build(t) + + err := provider.CleanUp("example.com", "abc", "123d==") + require.NoError(t, err) +} diff --git a/providers/dns/zz_gen_dns_providers.go b/providers/dns/zz_gen_dns_providers.go index 3a17594e2..38155e164 100644 --- a/providers/dns/zz_gen_dns_providers.go +++ b/providers/dns/zz_gen_dns_providers.go @@ -95,6 +95,7 @@ import ( "github.com/go-acme/lego/v4/providers/dns/ionoscloud" "github.com/go-acme/lego/v4/providers/dns/ipv64" "github.com/go-acme/lego/v4/providers/dns/ispconfig" + "github.com/go-acme/lego/v4/providers/dns/ispconfigddns" "github.com/go-acme/lego/v4/providers/dns/iwantmyname" "github.com/go-acme/lego/v4/providers/dns/joker" "github.com/go-acme/lego/v4/providers/dns/keyhelp" @@ -366,6 +367,8 @@ func NewDNSChallengeProviderByName(name string) (challenge.Provider, error) { return ipv64.NewDNSProvider() case "ispconfig": return ispconfig.NewDNSProvider() + case "ispconfigddns": + return ispconfigddns.NewDNSProvider() case "iwantmyname": return iwantmyname.NewDNSProvider() case "joker":