mirror of
https://github.com/go-acme/lego
synced 2026-03-14 22:45:48 +01:00
Add DNS provider for Virtualname (#2748)
This commit is contained in:
parent
9e2dffe8d2
commit
e54598536b
19 changed files with 619 additions and 177 deletions
10
README.md
10
README.md
|
|
@ -256,28 +256,28 @@ Detailed documentation is available [here](https://go-acme.github.io/lego/dns).
|
|||
</tr><tr>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/versio/">Versio.[nl|eu|uk]</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/vinyldns/">VinylDNS</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/virtualname/">Virtualname</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/vkcloud/">VK Cloud</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/volcengine/">Volcano Engine/火山引擎</a></td>
|
||||
</tr><tr>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/volcengine/">Volcano Engine/火山引擎</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/vscale/">Vscale</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/vultr/">Vultr</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/webnamesca/">webnames.ca</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/webnames/">webnames.ru</a></td>
|
||||
</tr><tr>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/webnames/">webnames.ru</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/websupport/">Websupport</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/wedos/">WEDOS</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/westcn/">West.cn/西部数码</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/yandex360/">Yandex 360</a></td>
|
||||
</tr><tr>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/yandex360/">Yandex 360</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/yandexcloud/">Yandex Cloud</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/yandex/">Yandex PDD</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/zoneee/">Zone.ee</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/zoneedit/">ZoneEdit</a></td>
|
||||
</tr><tr>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/zoneedit/">ZoneEdit</a></td>
|
||||
<td><a href="https://go-acme.github.io/lego/dns/zonomi/">Zonomi</a></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr></table>
|
||||
|
||||
<!-- END DNS PROVIDERS LIST -->
|
||||
|
|
|
|||
21
cmd/zz_gen_cmd_dnshelp.go
generated
21
cmd/zz_gen_cmd_dnshelp.go
generated
|
|
@ -170,6 +170,7 @@ func allDNSCodes() string {
|
|||
"vercel",
|
||||
"versio",
|
||||
"vinyldns",
|
||||
"virtualname",
|
||||
"vkcloud",
|
||||
"volcengine",
|
||||
"vscale",
|
||||
|
|
@ -3588,6 +3589,26 @@ func displayDNSHelp(w io.Writer, name string) error {
|
|||
ew.writeln()
|
||||
ew.writeln(`More information: https://go-acme.github.io/lego/dns/vinyldns`)
|
||||
|
||||
case "virtualname":
|
||||
// generated from: providers/dns/virtualname/virtualname.toml
|
||||
ew.writeln(`Configuration for Virtualname.`)
|
||||
ew.writeln(`Code: 'virtualname'`)
|
||||
ew.writeln(`Since: 'v4.30.0'`)
|
||||
ew.writeln()
|
||||
|
||||
ew.writeln(`Credentials:`)
|
||||
ew.writeln(` - "VIRTUALNAME_TOKEN": API token`)
|
||||
ew.writeln()
|
||||
|
||||
ew.writeln(`Additional Configuration:`)
|
||||
ew.writeln(` - "VIRTUALNAME_HTTP_TIMEOUT": API request timeout in seconds (Default: 30)`)
|
||||
ew.writeln(` - "VIRTUALNAME_POLLING_INTERVAL": Time between DNS propagation check in seconds (Default: 10)`)
|
||||
ew.writeln(` - "VIRTUALNAME_PROPAGATION_TIMEOUT": Maximum waiting time for DNS propagation in seconds (Default: 300)`)
|
||||
ew.writeln(` - "VIRTUALNAME_TTL": The TTL of the TXT record used for the DNS challenge in seconds (Default: 120)`)
|
||||
|
||||
ew.writeln()
|
||||
ew.writeln(`More information: https://go-acme.github.io/lego/dns/virtualname`)
|
||||
|
||||
case "vkcloud":
|
||||
// generated from: providers/dns/vkcloud/vkcloud.toml
|
||||
ew.writeln(`Configuration for VK Cloud.`)
|
||||
|
|
|
|||
67
docs/content/dns/zz_gen_virtualname.md
generated
Normal file
67
docs/content/dns/zz_gen_virtualname.md
generated
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
---
|
||||
title: "Virtualname"
|
||||
date: 2019-03-03T16:39:46+01:00
|
||||
draft: false
|
||||
slug: virtualname
|
||||
dnsprovider:
|
||||
since: "v4.30.0"
|
||||
code: "virtualname"
|
||||
url: "https://www.virtualname.es/"
|
||||
---
|
||||
|
||||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. -->
|
||||
<!-- providers/dns/virtualname/virtualname.toml -->
|
||||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. -->
|
||||
|
||||
|
||||
Configuration for [Virtualname](https://www.virtualname.es/).
|
||||
|
||||
|
||||
<!--more-->
|
||||
|
||||
- Code: `virtualname`
|
||||
- Since: v4.30.0
|
||||
|
||||
|
||||
Here is an example bash command using the Virtualname provider:
|
||||
|
||||
```bash
|
||||
VIRTUALNAME_TOKEN=xxxxxx \
|
||||
lego --email you@example.com --dns virtualname -d '*.example.com' -d example.com run
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
## Credentials
|
||||
|
||||
| Environment Variable Name | Description |
|
||||
|-----------------------|-------------|
|
||||
| `VIRTUALNAME_TOKEN` | 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 |
|
||||
|--------------------------------|-------------|
|
||||
| `VIRTUALNAME_HTTP_TIMEOUT` | API request timeout in seconds (Default: 30) |
|
||||
| `VIRTUALNAME_POLLING_INTERVAL` | Time between DNS propagation check in seconds (Default: 10) |
|
||||
| `VIRTUALNAME_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation in seconds (Default: 300) |
|
||||
| `VIRTUALNAME_TTL` | The TTL of the TXT record used for the DNS challenge in seconds (Default: 120) |
|
||||
|
||||
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" %}}).
|
||||
|
||||
|
||||
|
||||
|
||||
## More information
|
||||
|
||||
- [API documentation](https://developers.virtualname.net/#dns)
|
||||
|
||||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. -->
|
||||
<!-- providers/dns/virtualname/virtualname.toml -->
|
||||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. -->
|
||||
2
docs/data/zz_cli_help.toml
generated
2
docs/data/zz_cli_help.toml
generated
|
|
@ -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, hosttech, httpnet, httpreq, huaweicloud, hurricane, hyperone, ibmcloud, iij, iijdpf, infoblox, infomaniak, internetbs, inwx, ionos, ipv64, 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, 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, hosttech, httpnet, httpreq, huaweicloud, hurricane, hyperone, ibmcloud, iij, iijdpf, infoblox, infomaniak, internetbs, inwx, ionos, ipv64, 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
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -16,10 +16,10 @@ import (
|
|||
"github.com/go-acme/lego/v4/providers/dns/internal/useragent"
|
||||
)
|
||||
|
||||
// DefaultBaseURL is the default API endpoint.
|
||||
const DefaultBaseURL = "https://api.neodigit.net/v1"
|
||||
// defaultBaseURL is the default API endpoint.
|
||||
const defaultBaseURL = "https://api.neodigit.net/v1"
|
||||
|
||||
// Client is a Neodigit API client.
|
||||
// Client is a Tecnocrática API client.
|
||||
type Client struct {
|
||||
token string
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ func NewClient(token string) (*Client, error) {
|
|||
return nil, errors.New("credentials missing: token")
|
||||
}
|
||||
|
||||
baseURL, err := url.Parse(DefaultBaseURL)
|
||||
baseURL, err := url.Parse(defaultBaseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
165
providers/dns/internal/tecnocratica/provider.go
Normal file
165
providers/dns/internal/tecnocratica/provider.go
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
// Package tecnocratica implements a DNS provider for solving the DNS-01 challenge using Tecnocrática.
|
||||
package tecnocratica
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-acme/lego/v4/challenge"
|
||||
"github.com/go-acme/lego/v4/challenge/dns01"
|
||||
"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
|
||||
"github.com/go-acme/lego/v4/providers/dns/internal/tecnocratica/internal"
|
||||
)
|
||||
|
||||
var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
|
||||
|
||||
// Config is used to configure the creation of the DNSProvider.
|
||||
type Config struct {
|
||||
Token string
|
||||
|
||||
PropagationTimeout time.Duration
|
||||
PollingInterval time.Duration
|
||||
TTL int
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
// DNSProvider implements the challenge.Provider interface.
|
||||
type DNSProvider struct {
|
||||
config *Config
|
||||
client *internal.Client
|
||||
|
||||
zoneIDs map[string]int
|
||||
recordIDs map[string]int
|
||||
recordIDsMu sync.Mutex
|
||||
}
|
||||
|
||||
// NewDNSProviderConfig return a DNSProvider instance configured for Tecnocrática.
|
||||
func NewDNSProviderConfig(config *Config, baseURL string) (*DNSProvider, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("the configuration of the DNS provider is nil")
|
||||
}
|
||||
|
||||
if config.Token == "" {
|
||||
return nil, errors.New("missing credentials")
|
||||
}
|
||||
|
||||
client, err := internal.NewClient(config.Token)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create client: %w", err)
|
||||
}
|
||||
|
||||
if config.HTTPClient != nil {
|
||||
client.HTTPClient = config.HTTPClient
|
||||
}
|
||||
|
||||
if baseURL != "" {
|
||||
client.BaseURL, err = url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
|
||||
|
||||
return &DNSProvider{
|
||||
config: config,
|
||||
client: client,
|
||||
zoneIDs: make(map[string]int),
|
||||
recordIDs: make(map[string]int),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
||||
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
||||
return d.config.PropagationTimeout, d.config.PollingInterval
|
||||
}
|
||||
|
||||
// Present creates a TXT record using the specified parameters.
|
||||
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
info := dns01.GetChallengeInfo(domain, keyAuth)
|
||||
|
||||
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not find zone for domain %q: %w", domain, err)
|
||||
}
|
||||
|
||||
authZone = dns01.UnFqdn(authZone)
|
||||
|
||||
zone, err := d.findZone(ctx, authZone)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
record := internal.Record{
|
||||
Name: subDomain,
|
||||
Type: "TXT",
|
||||
Content: info.Value,
|
||||
TTL: d.config.TTL,
|
||||
}
|
||||
|
||||
newRecord, err := d.client.CreateRecord(ctx, zone.ID, record)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create record: %w", err)
|
||||
}
|
||||
|
||||
d.recordIDsMu.Lock()
|
||||
d.zoneIDs[token] = zone.ID
|
||||
d.recordIDs[token] = newRecord.ID
|
||||
d.recordIDsMu.Unlock()
|
||||
|
||||
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)
|
||||
|
||||
d.recordIDsMu.Lock()
|
||||
zoneID, zoneOK := d.zoneIDs[token]
|
||||
recordID, recordOK := d.recordIDs[token]
|
||||
d.recordIDsMu.Unlock()
|
||||
|
||||
if !zoneOK || !recordOK {
|
||||
return fmt.Errorf("unknown record ID or zone ID for '%s' '%s'", info.EffectiveFQDN, token)
|
||||
}
|
||||
|
||||
err := d.client.DeleteRecord(context.Background(), zoneID, recordID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete record: fqdn=%s, zoneID=%d, recordID=%d: %w",
|
||||
info.EffectiveFQDN, zoneID, recordID, err)
|
||||
}
|
||||
|
||||
d.recordIDsMu.Lock()
|
||||
delete(d.zoneIDs, token)
|
||||
delete(d.recordIDs, token)
|
||||
d.recordIDsMu.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DNSProvider) findZone(ctx context.Context, zoneName string) (*internal.Zone, error) {
|
||||
zones, err := d.client.GetZones(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get zones: %w", err)
|
||||
}
|
||||
|
||||
for _, zone := range zones {
|
||||
if zone.Name == zoneName || zone.HumanName == zoneName {
|
||||
return &zone, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("zone not found: %s", zoneName)
|
||||
}
|
||||
99
providers/dns/internal/tecnocratica/provider_test.go
Normal file
99
providers/dns/internal/tecnocratica/provider_test.go
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
package tecnocratica
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-acme/lego/v4/platform/tester/servermock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNewDNSProviderConfig(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
token string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
desc: "success",
|
||||
token: "secret",
|
||||
},
|
||||
{
|
||||
desc: "missing token",
|
||||
expected: "missing credentials",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
config := &Config{}
|
||||
config.Token = test.token
|
||||
|
||||
p, err := NewDNSProviderConfig(config, "")
|
||||
|
||||
if test.expected == "" {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, p)
|
||||
require.NotNil(t, p.config)
|
||||
require.NotNil(t, p.client)
|
||||
} else {
|
||||
require.EqualError(t, err, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func mockBuilder() *servermock.Builder[*DNSProvider] {
|
||||
return servermock.NewBuilder(
|
||||
func(server *httptest.Server) (*DNSProvider, error) {
|
||||
config := &Config{
|
||||
Token: "secret",
|
||||
PropagationTimeout: 10 * time.Second,
|
||||
PollingInterval: 1 * time.Second,
|
||||
TTL: 120,
|
||||
HTTPClient: server.Client(),
|
||||
}
|
||||
|
||||
p, err := NewDNSProviderConfig(config, server.URL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return p, nil
|
||||
},
|
||||
servermock.CheckHeader().WithJSONHeaders().
|
||||
With("X-TCpanel-Token", "secret"),
|
||||
)
|
||||
}
|
||||
|
||||
func TestDNSProvider_Present(t *testing.T) {
|
||||
provider := mockBuilder().
|
||||
Route("GET /dns/zones",
|
||||
servermock.ResponseFromInternal("get_zones.json")).
|
||||
Route("POST /dns/zones/6/records",
|
||||
servermock.ResponseFromInternal("create_record.json").
|
||||
WithStatusCode(http.StatusCreated),
|
||||
servermock.CheckRequestJSONBodyFromInternal("create_record-request.json")).
|
||||
Build(t)
|
||||
|
||||
err := provider.Present("example.com", "abc", "123d==")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDNSProvider_CleanUp(t *testing.T) {
|
||||
provider := mockBuilder().
|
||||
Route("DELETE /dns/zones/456/records/123",
|
||||
servermock.Noop().
|
||||
WithStatusCode(http.StatusNoContent)).
|
||||
Build(t)
|
||||
|
||||
token := "abc"
|
||||
|
||||
provider.recordIDs[token] = 123
|
||||
provider.zoneIDs[token] = 456
|
||||
|
||||
err := provider.CleanUp("example.com", token, "123d==")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
|
@ -2,18 +2,15 @@
|
|||
package neodigit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
"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/neodigit/internal"
|
||||
"github.com/go-acme/lego/v4/providers/dns/internal/tecnocratica"
|
||||
)
|
||||
|
||||
// Environment variables names.
|
||||
|
|
@ -31,14 +28,7 @@ const (
|
|||
var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
|
||||
|
||||
// Config is used to configure the creation of the DNSProvider.
|
||||
type Config struct {
|
||||
Token string
|
||||
|
||||
PropagationTimeout time.Duration
|
||||
PollingInterval time.Duration
|
||||
TTL int
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
type Config = tecnocratica.Config
|
||||
|
||||
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
||||
func NewDefaultConfig() *Config {
|
||||
|
|
@ -54,12 +44,7 @@ func NewDefaultConfig() *Config {
|
|||
|
||||
// DNSProvider implements the challenge.Provider interface.
|
||||
type DNSProvider struct {
|
||||
config *Config
|
||||
client *internal.Client
|
||||
|
||||
zoneIDs map[string]int
|
||||
recordIDs map[string]int
|
||||
recordIDsMu sync.Mutex
|
||||
prv challenge.ProviderTimeout
|
||||
}
|
||||
|
||||
// NewDNSProvider returns a DNSProvider instance configured for Neodigit.
|
||||
|
|
@ -81,115 +66,36 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|||
return nil, errors.New("neodigit: the configuration of the DNS provider is nil")
|
||||
}
|
||||
|
||||
if config.Token == "" {
|
||||
return nil, errors.New("neodigit: missing credentials")
|
||||
}
|
||||
|
||||
client, err := internal.NewClient(config.Token)
|
||||
provider, err := tecnocratica.NewDNSProviderConfig(config, "")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("neodigit: create client: %w", err)
|
||||
return nil, fmt.Errorf("neodigit: %w", err)
|
||||
}
|
||||
|
||||
if config.HTTPClient != nil {
|
||||
client.HTTPClient = config.HTTPClient
|
||||
}
|
||||
|
||||
client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
|
||||
|
||||
return &DNSProvider{
|
||||
config: config,
|
||||
client: client,
|
||||
zoneIDs: make(map[string]int),
|
||||
recordIDs: make(map[string]int),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
||||
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
||||
return d.config.PropagationTimeout, d.config.PollingInterval
|
||||
return &DNSProvider{prv: provider}, nil
|
||||
}
|
||||
|
||||
// Present creates a TXT record using the specified parameters.
|
||||
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
info := dns01.GetChallengeInfo(domain, keyAuth)
|
||||
|
||||
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
||||
if err != nil {
|
||||
return fmt.Errorf("neodigit: could not find zone for domain %q: %w", domain, err)
|
||||
}
|
||||
|
||||
authZone = dns01.UnFqdn(authZone)
|
||||
|
||||
zone, err := d.findZone(ctx, authZone)
|
||||
err := d.prv.Present(domain, token, keyAuth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("neodigit: %w", err)
|
||||
}
|
||||
|
||||
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
||||
if err != nil {
|
||||
return fmt.Errorf("neodigit: %w", err)
|
||||
}
|
||||
|
||||
record := internal.Record{
|
||||
Name: subDomain,
|
||||
Type: "TXT",
|
||||
Content: info.Value,
|
||||
TTL: d.config.TTL,
|
||||
}
|
||||
|
||||
newRecord, err := d.client.CreateRecord(ctx, zone.ID, record)
|
||||
if err != nil {
|
||||
return fmt.Errorf("neodigit: create record: %w", err)
|
||||
}
|
||||
|
||||
d.recordIDsMu.Lock()
|
||||
d.zoneIDs[token] = zone.ID
|
||||
d.recordIDs[token] = newRecord.ID
|
||||
d.recordIDsMu.Unlock()
|
||||
|
||||
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)
|
||||
|
||||
d.recordIDsMu.Lock()
|
||||
zoneID, zoneOK := d.zoneIDs[token]
|
||||
recordID, recordOK := d.recordIDs[token]
|
||||
d.recordIDsMu.Unlock()
|
||||
|
||||
if !zoneOK || !recordOK {
|
||||
return fmt.Errorf("neodigit: unknown record ID or zone ID for '%s' '%s'", info.EffectiveFQDN, token)
|
||||
}
|
||||
|
||||
err := d.client.DeleteRecord(context.Background(), zoneID, recordID)
|
||||
err := d.prv.CleanUp(domain, token, keyAuth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("neodigit: delete record: fqdn=%s, zoneID=%d, recordID=%d: %w",
|
||||
info.EffectiveFQDN, zoneID, recordID, err)
|
||||
return fmt.Errorf("neodigit: %w", err)
|
||||
}
|
||||
|
||||
d.recordIDsMu.Lock()
|
||||
delete(d.zoneIDs, token)
|
||||
delete(d.recordIDs, token)
|
||||
d.recordIDsMu.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DNSProvider) findZone(ctx context.Context, zoneName string) (*internal.Zone, error) {
|
||||
zones, err := d.client.GetZones(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get zones: %w", err)
|
||||
}
|
||||
|
||||
for _, zone := range zones {
|
||||
if zone.Name == zoneName || zone.HumanName == zoneName {
|
||||
return &zone, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("zone not found: %s", zoneName)
|
||||
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
||||
// Adjusting here to cope with spikes in propagation times.
|
||||
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
||||
return d.prv.Timeout()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,9 @@
|
|||
package neodigit
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/go-acme/lego/v4/platform/tester"
|
||||
"github.com/go-acme/lego/v4/platform/tester/servermock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
|
|
@ -49,8 +45,7 @@ func TestNewDNSProvider(t *testing.T) {
|
|||
if test.expected == "" {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, p)
|
||||
require.NotNil(t, p.config)
|
||||
require.NotNil(t, p.client)
|
||||
require.NotNil(t, p.prv)
|
||||
} else {
|
||||
require.EqualError(t, err, test.expected)
|
||||
}
|
||||
|
|
@ -84,8 +79,7 @@ func TestNewDNSProviderConfig(t *testing.T) {
|
|||
if test.expected == "" {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, p)
|
||||
require.NotNil(t, p.config)
|
||||
require.NotNil(t, p.client)
|
||||
require.NotNil(t, p.prv)
|
||||
} else {
|
||||
require.EqualError(t, err, test.expected)
|
||||
}
|
||||
|
|
@ -120,55 +114,3 @@ func TestLiveCleanUp(t *testing.T) {
|
|||
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.Token = "secret"
|
||||
config.HTTPClient = server.Client()
|
||||
|
||||
p, err := NewDNSProviderConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p.client.BaseURL, _ = url.Parse(server.URL)
|
||||
|
||||
return p, nil
|
||||
},
|
||||
servermock.CheckHeader().WithJSONHeaders().
|
||||
With("X-TCpanel-Token", "secret"),
|
||||
)
|
||||
}
|
||||
|
||||
func TestDNSProvider_Present(t *testing.T) {
|
||||
provider := mockBuilder().
|
||||
Route("GET /dns/zones",
|
||||
servermock.ResponseFromInternal("get_zones.json")).
|
||||
Route("POST /dns/zones/6/records",
|
||||
servermock.ResponseFromInternal("create_record.json").
|
||||
WithStatusCode(http.StatusCreated),
|
||||
servermock.CheckRequestJSONBodyFromInternal("create_record-request.json")).
|
||||
Build(t)
|
||||
|
||||
err := provider.Present("example.com", "abc", "123d==")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDNSProvider_CleanUp(t *testing.T) {
|
||||
provider := mockBuilder().
|
||||
Route("DELETE /dns/zones/456/records/123",
|
||||
servermock.Noop().
|
||||
WithStatusCode(http.StatusNoContent)).
|
||||
Build(t)
|
||||
|
||||
token := "abc"
|
||||
|
||||
provider.recordIDs[token] = 123
|
||||
provider.zoneIDs[token] = 456
|
||||
|
||||
err := provider.CleanUp("example.com", token, "123d==")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
|
|
|||
101
providers/dns/virtualname/virtualname.go
Normal file
101
providers/dns/virtualname/virtualname.go
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
// Package virtualname implements a DNS provider for solving the DNS-01 challenge using Virtualname DNS.
|
||||
package virtualname
|
||||
|
||||
import (
|
||||
"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/tecnocratica"
|
||||
)
|
||||
|
||||
// Environment variables names.
|
||||
const (
|
||||
envNamespace = "VIRTUALNAME_"
|
||||
|
||||
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 = tecnocratica.Config
|
||||
|
||||
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
||||
func NewDefaultConfig() *Config {
|
||||
return &Config{
|
||||
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
||||
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 5*time.Minute),
|
||||
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second),
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// DNSProvider implements the challenge.Provider interface.
|
||||
type DNSProvider struct {
|
||||
prv challenge.ProviderTimeout
|
||||
}
|
||||
|
||||
// NewDNSProvider returns a DNSProvider instance configured for Virtualname.
|
||||
func NewDNSProvider() (*DNSProvider, error) {
|
||||
values, err := env.Get(EnvToken)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("virtualname: %w", err)
|
||||
}
|
||||
|
||||
config := NewDefaultConfig()
|
||||
config.Token = values[EnvToken]
|
||||
|
||||
return NewDNSProviderConfig(config)
|
||||
}
|
||||
|
||||
// NewDNSProviderConfig return a DNSProvider instance configured for Virtualname.
|
||||
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("virtualname: the configuration of the DNS provider is nil")
|
||||
}
|
||||
|
||||
provider, err := tecnocratica.NewDNSProviderConfig(config, "https://api.virtualname.net/v1")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("virtualname: %w", err)
|
||||
}
|
||||
|
||||
return &DNSProvider{prv: provider}, nil
|
||||
}
|
||||
|
||||
// Present creates a TXT record using the specified parameters.
|
||||
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
||||
err := d.prv.Present(domain, token, keyAuth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("virtualname: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CleanUp removes the TXT record matching the specified parameters.
|
||||
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
||||
err := d.prv.CleanUp(domain, token, keyAuth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("virtualname: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
||||
// Adjusting here to cope with spikes in propagation times.
|
||||
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
||||
return d.prv.Timeout()
|
||||
}
|
||||
22
providers/dns/virtualname/virtualname.toml
Normal file
22
providers/dns/virtualname/virtualname.toml
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
Name = "Virtualname"
|
||||
Description = ''''''
|
||||
URL = "https://www.virtualname.es/"
|
||||
Code = "virtualname"
|
||||
Since = "v4.30.0"
|
||||
|
||||
Example = '''
|
||||
VIRTUALNAME_TOKEN=xxxxxx \
|
||||
lego --email you@example.com --dns virtualname -d '*.example.com' -d example.com run
|
||||
'''
|
||||
|
||||
[Configuration]
|
||||
[Configuration.Credentials]
|
||||
VIRTUALNAME_TOKEN = "API token"
|
||||
[Configuration.Additional]
|
||||
VIRTUALNAME_POLLING_INTERVAL = "Time between DNS propagation check in seconds (Default: 10)"
|
||||
VIRTUALNAME_PROPAGATION_TIMEOUT = "Maximum waiting time for DNS propagation in seconds (Default: 300)"
|
||||
VIRTUALNAME_TTL = "The TTL of the TXT record used for the DNS challenge in seconds (Default: 120)"
|
||||
VIRTUALNAME_HTTP_TIMEOUT = "API request timeout in seconds (Default: 30)"
|
||||
|
||||
[Links]
|
||||
API = "https://developers.virtualname.net/#dns"
|
||||
116
providers/dns/virtualname/virtualname_test.go
Normal file
116
providers/dns/virtualname/virtualname_test.go
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
package virtualname
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/go-acme/lego/v4/platform/tester"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const envDomain = envNamespace + "DOMAIN"
|
||||
|
||||
var envTest = tester.NewEnvTest(EnvToken).WithDomain(envDomain)
|
||||
|
||||
func TestNewDNSProvider(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
envVars map[string]string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
desc: "success",
|
||||
envVars: map[string]string{
|
||||
EnvToken: "secret",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "missing credentials: token",
|
||||
envVars: map[string]string{
|
||||
EnvToken: "",
|
||||
},
|
||||
expected: "virtualname: some credentials information are missing: VIRTUALNAME_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.prv)
|
||||
} else {
|
||||
require.EqualError(t, err, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDNSProviderConfig(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
token string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
desc: "success",
|
||||
token: "secret",
|
||||
},
|
||||
{
|
||||
desc: "missing token",
|
||||
expected: "virtualname: missing credentials",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
config := NewDefaultConfig()
|
||||
config.Token = test.token
|
||||
|
||||
p, err := NewDNSProviderConfig(config)
|
||||
|
||||
if test.expected == "" {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, p)
|
||||
require.NotNil(t, p.prv)
|
||||
} 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)
|
||||
}
|
||||
3
providers/dns/zz_gen_dns_providers.go
generated
3
providers/dns/zz_gen_dns_providers.go
generated
|
|
@ -164,6 +164,7 @@ import (
|
|||
"github.com/go-acme/lego/v4/providers/dns/vercel"
|
||||
"github.com/go-acme/lego/v4/providers/dns/versio"
|
||||
"github.com/go-acme/lego/v4/providers/dns/vinyldns"
|
||||
"github.com/go-acme/lego/v4/providers/dns/virtualname"
|
||||
"github.com/go-acme/lego/v4/providers/dns/vkcloud"
|
||||
"github.com/go-acme/lego/v4/providers/dns/volcengine"
|
||||
"github.com/go-acme/lego/v4/providers/dns/vscale"
|
||||
|
|
@ -500,6 +501,8 @@ func NewDNSChallengeProviderByName(name string) (challenge.Provider, error) {
|
|||
return versio.NewDNSProvider()
|
||||
case "vinyldns":
|
||||
return vinyldns.NewDNSProvider()
|
||||
case "virtualname":
|
||||
return virtualname.NewDNSProvider()
|
||||
case "vkcloud":
|
||||
return vkcloud.NewDNSProvider()
|
||||
case "volcengine":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue