mirror of
https://github.com/go-acme/lego.git
synced 2024-11-25 00:56:20 +02:00
Adding Hezner DNS provider (#1128)
This commit is contained in:
parent
5ab7335987
commit
e90786484a
22
README.md
22
README.md
@ -53,16 +53,16 @@ Detailed documentation is available [here](https://go-acme.github.io/lego/dns).
|
||||
| [DNSPod](https://go-acme.github.io/lego/dns/dnspod/) | [Domain Offensive (do.de)](https://go-acme.github.io/lego/dns/dode/) | [DreamHost](https://go-acme.github.io/lego/dns/dreamhost/) | [Duck DNS](https://go-acme.github.io/lego/dns/duckdns/) |
|
||||
| [Dyn](https://go-acme.github.io/lego/dns/dyn/) | [Dynu](https://go-acme.github.io/lego/dns/dynu/) | [EasyDNS](https://go-acme.github.io/lego/dns/easydns/) | [Exoscale](https://go-acme.github.io/lego/dns/exoscale/) |
|
||||
| [External program](https://go-acme.github.io/lego/dns/exec/) | [FastDNS](https://go-acme.github.io/lego/dns/fastdns/) | [Gandi Live DNS (v5)](https://go-acme.github.io/lego/dns/gandiv5/) | [Gandi](https://go-acme.github.io/lego/dns/gandi/) |
|
||||
| [Glesys](https://go-acme.github.io/lego/dns/glesys/) | [Go Daddy](https://go-acme.github.io/lego/dns/godaddy/) | [Google Cloud](https://go-acme.github.io/lego/dns/gcloud/) | [Hosting.de](https://go-acme.github.io/lego/dns/hostingde/) |
|
||||
| [HTTP request](https://go-acme.github.io/lego/dns/httpreq/) | [Internet Initiative Japan](https://go-acme.github.io/lego/dns/iij/) | [INWX](https://go-acme.github.io/lego/dns/inwx/) | [Joker](https://go-acme.github.io/lego/dns/joker/) |
|
||||
| [Joohoi's ACME-DNS](https://go-acme.github.io/lego/dns/acme-dns/) | [Linode (deprecated)](https://go-acme.github.io/lego/dns/linode/) | [Linode (v4)](https://go-acme.github.io/lego/dns/linodev4/) | [Liquid Web](https://go-acme.github.io/lego/dns/liquidweb/) |
|
||||
| [Manual](https://go-acme.github.io/lego/dns/manual/) | [MyDNS.jp](https://go-acme.github.io/lego/dns/mydnsjp/) | [MythicBeasts](https://go-acme.github.io/lego/dns/mythicbeasts/) | [Name.com](https://go-acme.github.io/lego/dns/namedotcom/) |
|
||||
| [Namecheap](https://go-acme.github.io/lego/dns/namecheap/) | [Namesilo](https://go-acme.github.io/lego/dns/namesilo/) | [Netcup](https://go-acme.github.io/lego/dns/netcup/) | [Netlify](https://go-acme.github.io/lego/dns/netlify/) |
|
||||
| [NIFCloud](https://go-acme.github.io/lego/dns/nifcloud/) | [NS1](https://go-acme.github.io/lego/dns/ns1/) | [Open Telekom Cloud](https://go-acme.github.io/lego/dns/otc/) | [Oracle Cloud](https://go-acme.github.io/lego/dns/oraclecloud/) |
|
||||
| [OVH](https://go-acme.github.io/lego/dns/ovh/) | [PowerDNS](https://go-acme.github.io/lego/dns/pdns/) | [Rackspace](https://go-acme.github.io/lego/dns/rackspace/) | [reg.ru](https://go-acme.github.io/lego/dns/regru/) |
|
||||
| [RFC2136](https://go-acme.github.io/lego/dns/rfc2136/) | [RimuHosting](https://go-acme.github.io/lego/dns/rimuhosting/) | [Sakura Cloud](https://go-acme.github.io/lego/dns/sakuracloud/) | [Scaleway](https://go-acme.github.io/lego/dns/scaleway/) |
|
||||
| [Selectel](https://go-acme.github.io/lego/dns/selectel/) | [Servercow](https://go-acme.github.io/lego/dns/servercow/) | [Stackpath](https://go-acme.github.io/lego/dns/stackpath/) | [TransIP](https://go-acme.github.io/lego/dns/transip/) |
|
||||
| [VegaDNS](https://go-acme.github.io/lego/dns/vegadns/) | [Versio.[nl/eu/uk]](https://go-acme.github.io/lego/dns/versio/) | [Vscale](https://go-acme.github.io/lego/dns/vscale/) | [Vultr](https://go-acme.github.io/lego/dns/vultr/) |
|
||||
| [Yandex](https://go-acme.github.io/lego/dns/yandex/) | [Zone.ee](https://go-acme.github.io/lego/dns/zoneee/) | [Zonomi](https://go-acme.github.io/lego/dns/zonomi/) | |
|
||||
| [Glesys](https://go-acme.github.io/lego/dns/glesys/) | [Go Daddy](https://go-acme.github.io/lego/dns/godaddy/) | [Google Cloud](https://go-acme.github.io/lego/dns/gcloud/) | [Hetzner](https://go-acme.github.io/lego/dns/hetzner/) |
|
||||
| [Hosting.de](https://go-acme.github.io/lego/dns/hostingde/) | [HTTP request](https://go-acme.github.io/lego/dns/httpreq/) | [Internet Initiative Japan](https://go-acme.github.io/lego/dns/iij/) | [INWX](https://go-acme.github.io/lego/dns/inwx/) |
|
||||
| [Joker](https://go-acme.github.io/lego/dns/joker/) | [Joohoi's ACME-DNS](https://go-acme.github.io/lego/dns/acme-dns/) | [Linode (deprecated)](https://go-acme.github.io/lego/dns/linode/) | [Linode (v4)](https://go-acme.github.io/lego/dns/linodev4/) |
|
||||
| [Liquid Web](https://go-acme.github.io/lego/dns/liquidweb/) | [Manual](https://go-acme.github.io/lego/dns/manual/) | [MyDNS.jp](https://go-acme.github.io/lego/dns/mydnsjp/) | [MythicBeasts](https://go-acme.github.io/lego/dns/mythicbeasts/) |
|
||||
| [Name.com](https://go-acme.github.io/lego/dns/namedotcom/) | [Namecheap](https://go-acme.github.io/lego/dns/namecheap/) | [Namesilo](https://go-acme.github.io/lego/dns/namesilo/) | [Netcup](https://go-acme.github.io/lego/dns/netcup/) |
|
||||
| [Netlify](https://go-acme.github.io/lego/dns/netlify/) | [NIFCloud](https://go-acme.github.io/lego/dns/nifcloud/) | [NS1](https://go-acme.github.io/lego/dns/ns1/) | [Open Telekom Cloud](https://go-acme.github.io/lego/dns/otc/) |
|
||||
| [Oracle Cloud](https://go-acme.github.io/lego/dns/oraclecloud/) | [OVH](https://go-acme.github.io/lego/dns/ovh/) | [PowerDNS](https://go-acme.github.io/lego/dns/pdns/) | [Rackspace](https://go-acme.github.io/lego/dns/rackspace/) |
|
||||
| [reg.ru](https://go-acme.github.io/lego/dns/regru/) | [RFC2136](https://go-acme.github.io/lego/dns/rfc2136/) | [RimuHosting](https://go-acme.github.io/lego/dns/rimuhosting/) | [Sakura Cloud](https://go-acme.github.io/lego/dns/sakuracloud/) |
|
||||
| [Scaleway](https://go-acme.github.io/lego/dns/scaleway/) | [Selectel](https://go-acme.github.io/lego/dns/selectel/) | [Servercow](https://go-acme.github.io/lego/dns/servercow/) | [Stackpath](https://go-acme.github.io/lego/dns/stackpath/) |
|
||||
| [TransIP](https://go-acme.github.io/lego/dns/transip/) | [VegaDNS](https://go-acme.github.io/lego/dns/vegadns/) | [Versio.[nl/eu/uk]](https://go-acme.github.io/lego/dns/versio/) | [Vscale](https://go-acme.github.io/lego/dns/vscale/) |
|
||||
| [Vultr](https://go-acme.github.io/lego/dns/vultr/) | [Yandex](https://go-acme.github.io/lego/dns/yandex/) | [Zone.ee](https://go-acme.github.io/lego/dns/zoneee/) | [Zonomi](https://go-acme.github.io/lego/dns/zonomi/) |
|
||||
|
||||
<!-- END DNS PROVIDERS LIST -->
|
||||
|
@ -48,6 +48,7 @@ func allDNSCodes() string {
|
||||
"gcloud",
|
||||
"glesys",
|
||||
"godaddy",
|
||||
"hetzner",
|
||||
"hostingde",
|
||||
"httpreq",
|
||||
"iij",
|
||||
@ -816,6 +817,27 @@ func displayDNSHelp(name string) error {
|
||||
ew.writeln()
|
||||
ew.writeln(`More information: https://go-acme.github.io/lego/dns/godaddy`)
|
||||
|
||||
case "hetzner":
|
||||
// generated from: providers/dns/hetzner/hetzner.toml
|
||||
ew.writeln(`Configuration for Hetzner.`)
|
||||
ew.writeln(`Code: 'hetzner'`)
|
||||
ew.writeln(`Since: 'v3.7.0'`)
|
||||
ew.writeln()
|
||||
|
||||
ew.writeln(`Credentials:`)
|
||||
ew.writeln(` - "HETZNER_API_KEY": API key`)
|
||||
ew.writeln()
|
||||
|
||||
ew.writeln(`Additional Configuration:`)
|
||||
ew.writeln(` - "HETZNER_HTTP_TIMEOUT": API request timeout`)
|
||||
ew.writeln(` - "HETZNER_POLLING_INTERVAL": Time between DNS propagation check`)
|
||||
ew.writeln(` - "HETZNER_PROPAGATION_TIMEOUT": Maximum waiting time for DNS propagation`)
|
||||
ew.writeln(` - "HETZNER_SEQUENCE_INTERVAL": Interval between iteration`)
|
||||
ew.writeln(` - "HETZNER_TTL": The TTL of the TXT record used for the DNS challenge`)
|
||||
|
||||
ew.writeln()
|
||||
ew.writeln(`More information: https://go-acme.github.io/lego/dns/hetzner`)
|
||||
|
||||
case "hostingde":
|
||||
// generated from: providers/dns/hostingde/hostingde.toml
|
||||
ew.writeln(`Configuration for Hosting.de.`)
|
||||
|
63
docs/content/dns/zz_gen_hetzner.md
Normal file
63
docs/content/dns/zz_gen_hetzner.md
Normal file
@ -0,0 +1,63 @@
|
||||
---
|
||||
title: "Hetzner"
|
||||
date: 2019-03-03T16:39:46+01:00
|
||||
draft: false
|
||||
slug: hetzner
|
||||
---
|
||||
|
||||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. -->
|
||||
<!-- providers/dns/hetzner/hetzner.toml -->
|
||||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. -->
|
||||
|
||||
Since: v3.7.0
|
||||
|
||||
Configuration for [Hetzner](https://hetzner.com).
|
||||
|
||||
|
||||
<!--more-->
|
||||
|
||||
- Code: `hetzner`
|
||||
|
||||
Here is an example bash command using the Hetzner provider:
|
||||
|
||||
```bash
|
||||
HETZNER_API_KEY=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx \
|
||||
lego --dns hetzner --domains my.domain.com --email my@email.com run
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
## Credentials
|
||||
|
||||
| Environment Variable Name | Description |
|
||||
|-----------------------|-------------|
|
||||
| `HETZNER_API_KEY` | API key |
|
||||
|
||||
The environment variable names can be suffixed by `_FILE` to reference a file instead of a value.
|
||||
More information [here](/lego/dns/#configuration-and-credentials).
|
||||
|
||||
|
||||
## Additional Configuration
|
||||
|
||||
| Environment Variable Name | Description |
|
||||
|--------------------------------|-------------|
|
||||
| `HETZNER_HTTP_TIMEOUT` | API request timeout |
|
||||
| `HETZNER_POLLING_INTERVAL` | Time between DNS propagation check |
|
||||
| `HETZNER_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation |
|
||||
| `HETZNER_SEQUENCE_INTERVAL` | Interval between iteration |
|
||||
| `HETZNER_TTL` | The TTL of the TXT record used for the DNS challenge |
|
||||
|
||||
The environment variable names can be suffixed by `_FILE` to reference a file instead of a value.
|
||||
More information [here](/lego/dns/#configuration-and-credentials).
|
||||
|
||||
|
||||
|
||||
|
||||
## More information
|
||||
|
||||
- [API documentation](https://dns.hetzner.com/api-docs)
|
||||
|
||||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. -->
|
||||
<!-- providers/dns/hetzner/hetzner.toml -->
|
||||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. -->
|
@ -39,6 +39,7 @@ import (
|
||||
"github.com/go-acme/lego/v3/providers/dns/gcloud"
|
||||
"github.com/go-acme/lego/v3/providers/dns/glesys"
|
||||
"github.com/go-acme/lego/v3/providers/dns/godaddy"
|
||||
"github.com/go-acme/lego/v3/providers/dns/hetzner"
|
||||
"github.com/go-acme/lego/v3/providers/dns/hostingde"
|
||||
"github.com/go-acme/lego/v3/providers/dns/httpreq"
|
||||
"github.com/go-acme/lego/v3/providers/dns/iij"
|
||||
@ -152,6 +153,8 @@ func NewDNSChallengeProviderByName(name string) (challenge.Provider, error) {
|
||||
return gcloud.NewDNSProvider()
|
||||
case "godaddy":
|
||||
return godaddy.NewDNSProvider()
|
||||
case "hetzner":
|
||||
return hetzner.NewDNSProvider()
|
||||
case "hostingde":
|
||||
return hostingde.NewDNSProvider()
|
||||
case "httpreq":
|
||||
|
172
providers/dns/hetzner/hetzner.go
Normal file
172
providers/dns/hetzner/hetzner.go
Normal file
@ -0,0 +1,172 @@
|
||||
// Package hetzner implements a DNS provider for solving the DNS-01 challenge using Hetzner DNS.
|
||||
package hetzner
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-acme/lego/v3/challenge/dns01"
|
||||
"github.com/go-acme/lego/v3/platform/config/env"
|
||||
"github.com/go-acme/lego/v3/providers/dns/hetzner/internal"
|
||||
)
|
||||
|
||||
const minTTL = 600
|
||||
|
||||
// Environment variables names.
|
||||
const (
|
||||
envNamespace = "HETZNER_"
|
||||
|
||||
EnvAPIKey = envNamespace + "API_KEY"
|
||||
|
||||
EnvTTL = envNamespace + "TTL"
|
||||
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
||||
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
||||
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
||||
)
|
||||
|
||||
// Config is used to configure the creation of the DNSProvider.
|
||||
type Config struct {
|
||||
APIKey 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, minTTL),
|
||||
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second),
|
||||
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// DNSProvider is an implementation of the challenge.Provider interface.
|
||||
type DNSProvider struct {
|
||||
config *Config
|
||||
client *internal.Client
|
||||
}
|
||||
|
||||
// NewDNSProvider returns a DNSProvider instance configured for hetzner.
|
||||
// Credentials must be passed in the environment variable: HETZNER_API_KEY.
|
||||
func NewDNSProvider() (*DNSProvider, error) {
|
||||
values, err := env.Get(EnvAPIKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("hetzner: %w", err)
|
||||
}
|
||||
|
||||
config := NewDefaultConfig()
|
||||
config.APIKey = values[EnvAPIKey]
|
||||
|
||||
return NewDNSProviderConfig(config)
|
||||
}
|
||||
|
||||
// NewDNSProviderConfig return a DNSProvider instance configured for hetzner.
|
||||
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("hetzner: the configuration of the DNS provider is nil")
|
||||
}
|
||||
|
||||
if config.APIKey == "" {
|
||||
return nil, errors.New("hetzner: credentials missing")
|
||||
}
|
||||
|
||||
if config.TTL < minTTL {
|
||||
return nil, fmt.Errorf("hetzner: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
|
||||
}
|
||||
|
||||
client := internal.NewClient(config.APIKey)
|
||||
|
||||
if config.HTTPClient != nil {
|
||||
client.HTTPClient = config.HTTPClient
|
||||
}
|
||||
|
||||
return &DNSProvider{config: config, client: client}, 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.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 {
|
||||
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
||||
|
||||
zone, err := d.getZone(fqdn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("hetzner: failed to find zone: fqdn=%s: %w", fqdn, err)
|
||||
}
|
||||
|
||||
zoneID, err := d.client.GetZoneID(zone)
|
||||
if err != nil {
|
||||
return fmt.Errorf("hetzner: %w", err)
|
||||
}
|
||||
|
||||
record := internal.DNSRecord{
|
||||
Type: "TXT",
|
||||
Name: d.extractRecordName(fqdn, domain),
|
||||
Value: value,
|
||||
TTL: d.config.TTL,
|
||||
ZoneID: zoneID,
|
||||
}
|
||||
|
||||
if err := d.client.CreateRecord(record); err != nil {
|
||||
return fmt.Errorf("hetzner: failed to add TXT record: fqdn=%s, zoneID=%s: %w", fqdn, zoneID, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CleanUp removes the TXT record matching the specified parameters.
|
||||
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
||||
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
||||
|
||||
zone, err := d.getZone(fqdn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("hetzner: failed to find zone: fqdn=%s: %w", fqdn, err)
|
||||
}
|
||||
|
||||
zoneID, err := d.client.GetZoneID(zone)
|
||||
if err != nil {
|
||||
return fmt.Errorf("hetzner: %w", err)
|
||||
}
|
||||
|
||||
recordName := d.extractRecordName(fqdn, domain)
|
||||
|
||||
record, err := d.client.GetTxtRecord(recordName, value, zoneID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("hetzner: %w", err)
|
||||
}
|
||||
|
||||
if err := d.client.DeleteRecord(record.ID); err != nil {
|
||||
return fmt.Errorf("hetzner: failed to delate TXT record: id=%s, name=%s: %w", record.ID, record.Name, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DNSProvider) extractRecordName(fqdn, domain string) string {
|
||||
name := dns01.UnFqdn(fqdn)
|
||||
if idx := strings.Index(name, "."+domain); idx != -1 {
|
||||
return name[:idx]
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (d *DNSProvider) getZone(fqdn string) (string, error) {
|
||||
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return dns01.UnFqdn(authZone), nil
|
||||
}
|
23
providers/dns/hetzner/hetzner.toml
Normal file
23
providers/dns/hetzner/hetzner.toml
Normal file
@ -0,0 +1,23 @@
|
||||
Name = "Hetzner"
|
||||
Description = ''''''
|
||||
URL = "https://hetzner.com"
|
||||
Code = "hetzner"
|
||||
Since = "v3.7.0"
|
||||
|
||||
Example = '''
|
||||
HETZNER_API_KEY=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx \
|
||||
lego --dns hetzner --domains my.domain.com --email my@email.com run
|
||||
'''
|
||||
|
||||
[Configuration]
|
||||
[Configuration.Credentials]
|
||||
HETZNER_API_KEY = "API key"
|
||||
[Configuration.Additional]
|
||||
HETZNER_POLLING_INTERVAL = "Time between DNS propagation check"
|
||||
HETZNER_PROPAGATION_TIMEOUT = "Maximum waiting time for DNS propagation"
|
||||
HETZNER_TTL = "The TTL of the TXT record used for the DNS challenge"
|
||||
HETZNER_HTTP_TIMEOUT = "API request timeout"
|
||||
HETZNER_SEQUENCE_INTERVAL = "Interval between iteration"
|
||||
|
||||
[Links]
|
||||
API = "https://dns.hetzner.com/api-docs"
|
125
providers/dns/hetzner/hetzner_test.go
Normal file
125
providers/dns/hetzner/hetzner_test.go
Normal file
@ -0,0 +1,125 @@
|
||||
package hetzner
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/go-acme/lego/v3/platform/tester"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const envDomain = envNamespace + "DOMAIN"
|
||||
|
||||
var envTest = tester.NewEnvTest(
|
||||
EnvAPIKey).
|
||||
WithDomain(envDomain)
|
||||
|
||||
func TestNewDNSProvider(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
envVars map[string]string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
desc: "success",
|
||||
envVars: map[string]string{
|
||||
EnvAPIKey: "123",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "missing credentials",
|
||||
envVars: map[string]string{
|
||||
EnvAPIKey: "",
|
||||
},
|
||||
expected: "hetzner: some credentials information are missing: HETZNER_API_KEY",
|
||||
},
|
||||
}
|
||||
|
||||
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 len(test.expected) == 0 {
|
||||
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
|
||||
apiKey string
|
||||
ttl int
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
desc: "success",
|
||||
ttl: minTTL,
|
||||
apiKey: "123",
|
||||
},
|
||||
{
|
||||
desc: "missing credentials",
|
||||
ttl: minTTL,
|
||||
expected: "hetzner: credentials missing",
|
||||
},
|
||||
{
|
||||
desc: "invalid TTL",
|
||||
apiKey: "123",
|
||||
ttl: 60,
|
||||
expected: "hetzner: invalid TTL, TTL (60) must be greater than 600",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
config := NewDefaultConfig()
|
||||
config.APIKey = test.apiKey
|
||||
config.TTL = test.ttl
|
||||
|
||||
p, err := NewDNSProviderConfig(config)
|
||||
|
||||
if len(test.expected) == 0 {
|
||||
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)
|
||||
}
|
197
providers/dns/hetzner/internal/client.go
Normal file
197
providers/dns/hetzner/internal/client.go
Normal file
@ -0,0 +1,197 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
)
|
||||
|
||||
// defaultBaseURL represents the API endpoint to call.
|
||||
const defaultBaseURL = "https://dns.hetzner.com"
|
||||
|
||||
const authHeader = "Auth-API-Token"
|
||||
|
||||
// Client the Hetzner client.
|
||||
type Client struct {
|
||||
HTTPClient *http.Client
|
||||
BaseURL string
|
||||
|
||||
apiKey string
|
||||
}
|
||||
|
||||
// NewClient Creates a new Hetzner client.
|
||||
func NewClient(apiKey string) *Client {
|
||||
return &Client{
|
||||
HTTPClient: http.DefaultClient,
|
||||
BaseURL: defaultBaseURL,
|
||||
apiKey: apiKey,
|
||||
}
|
||||
}
|
||||
|
||||
// GetTxtRecord gets a TXT record.
|
||||
func (c *Client) GetTxtRecord(name, value, zoneID string) (*DNSRecord, error) {
|
||||
records, err := c.getRecords(zoneID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, record := range records.Records {
|
||||
if record.Type == "TXT" && record.Name == name && record.Value == value {
|
||||
return &record, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("could not find record: zone ID: %s; Record: %s", zoneID, name)
|
||||
}
|
||||
|
||||
// https://dns.hetzner.com/api-docs#operation/GetRecords
|
||||
func (c *Client) getRecords(zoneID string) (*DNSRecords, error) {
|
||||
endpoint, err := c.createEndpoint("api", "v1", "records")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create endpoint: %w", err)
|
||||
}
|
||||
|
||||
query := endpoint.Query()
|
||||
query.Set("zone_id", zoneID)
|
||||
endpoint.RawQuery = query.Encode()
|
||||
|
||||
resp, err := c.do(http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
bodyBytes, _ := ioutil.ReadAll(resp.Body)
|
||||
return nil, fmt.Errorf("could not get records: zone ID: %s; Status: %s; Body: %s",
|
||||
zoneID, resp.Status, string(bodyBytes))
|
||||
}
|
||||
|
||||
records := &DNSRecords{}
|
||||
err = json.NewDecoder(resp.Body).Decode(records)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response body: %w", err)
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
// CreateRecord creates a DNS record.
|
||||
// https://dns.hetzner.com/api-docs#operation/CreateRecord
|
||||
func (c *Client) CreateRecord(record DNSRecord) error {
|
||||
body, err := json.Marshal(record)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
endpoint, err := c.createEndpoint("api", "v1", "records")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create endpoint: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.do(http.MethodPost, endpoint, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
bodyBytes, _ := ioutil.ReadAll(resp.Body)
|
||||
return fmt.Errorf("could not create record %s; Status: %s; Body: %s", string(body), resp.Status, string(bodyBytes))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteRecord deletes a DNS record.
|
||||
// https://dns.hetzner.com/api-docs#operation/DeleteRecord
|
||||
func (c *Client) DeleteRecord(recordID string) error {
|
||||
endpoint, err := c.createEndpoint("api", "v1", "records", recordID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create endpoint: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.do(http.MethodDelete, endpoint, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("could not delete record: %s; Status: %s", resp.Status, recordID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetZoneID gets the zone ID for a domain.
|
||||
func (c *Client) GetZoneID(domain string) (string, error) {
|
||||
zones, err := c.getZones()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, zone := range zones.Zones {
|
||||
if zone.Name == domain {
|
||||
return zone.ID, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("could not get zone for domain %s not found", domain)
|
||||
}
|
||||
|
||||
// https://dns.hetzner.com/api-docs#operation/GetZones
|
||||
func (c *Client) getZones() (*Zones, error) {
|
||||
endpoint, err := c.createEndpoint("api", "v1", "zones")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create endpoint: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.do(http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get zones: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("could not get zones: %s", resp.Status)
|
||||
}
|
||||
|
||||
zones := &Zones{}
|
||||
err = json.NewDecoder(resp.Body).Decode(zones)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response body: %w", err)
|
||||
}
|
||||
|
||||
return zones, nil
|
||||
}
|
||||
|
||||
func (c *Client) do(method string, endpoint fmt.Stringer, body io.Reader) (*http.Response, error) {
|
||||
req, err := http.NewRequest(method, endpoint.String(), body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set(authHeader, c.apiKey)
|
||||
|
||||
return c.HTTPClient.Do(req)
|
||||
}
|
||||
|
||||
func (c *Client) createEndpoint(parts ...string) (*url.URL, error) {
|
||||
baseURL, err := url.Parse(c.BaseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
endpoint, err := baseURL.Parse(path.Join(parts...))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return endpoint, nil
|
||||
}
|
175
providers/dns/hetzner/internal/client_test.go
Normal file
175
providers/dns/hetzner/internal/client_test.go
Normal file
@ -0,0 +1,175 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestClient_GetTxtRecord(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
server := httptest.NewServer(mux)
|
||||
|
||||
const zoneID = "zoneA"
|
||||
const apiKey = "myKeyA"
|
||||
|
||||
mux.HandleFunc("/api/v1/records", func(rw http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodGet {
|
||||
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
auth := req.Header.Get(authHeader)
|
||||
if auth != apiKey {
|
||||
http.Error(rw, fmt.Sprintf("invalid API key: %s", auth), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
zID := req.URL.Query().Get("zone_id")
|
||||
if zID != zoneID {
|
||||
http.Error(rw, fmt.Sprintf("invalid zone ID: %s", zID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := os.Open("./fixtures/get_txt_record.json")
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
_, err = io.Copy(rw, file)
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
client := NewClient(apiKey)
|
||||
client.BaseURL = server.URL
|
||||
|
||||
record, err := client.GetTxtRecord("test1", "txttxttxt", zoneID)
|
||||
require.NoError(t, err)
|
||||
|
||||
fmt.Println(record)
|
||||
}
|
||||
|
||||
func TestClient_CreateRecord(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
server := httptest.NewServer(mux)
|
||||
|
||||
const zoneID = "zoneA"
|
||||
const apiKey = "myKeyB"
|
||||
|
||||
mux.HandleFunc("/api/v1/records", func(rw http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodPost {
|
||||
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
auth := req.Header.Get(authHeader)
|
||||
if auth != apiKey {
|
||||
http.Error(rw, fmt.Sprintf("invalid API key: %s", auth), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := os.Open("./fixtures/create_txt_record.json")
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
_, err = io.Copy(rw, file)
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
client := NewClient(apiKey)
|
||||
client.BaseURL = server.URL
|
||||
|
||||
record := DNSRecord{
|
||||
Name: "test",
|
||||
Type: "TXT",
|
||||
Value: "txttxttxt",
|
||||
TTL: 600,
|
||||
ZoneID: zoneID,
|
||||
}
|
||||
|
||||
err := client.CreateRecord(record)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestClient_DeleteRecord(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
server := httptest.NewServer(mux)
|
||||
|
||||
const apiKey = "myKeyC"
|
||||
|
||||
mux.HandleFunc("/api/v1/records/recordID", func(rw http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodDelete {
|
||||
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
auth := req.Header.Get(authHeader)
|
||||
if auth != apiKey {
|
||||
http.Error(rw, fmt.Sprintf("invalid API key: %s", auth), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
client := NewClient(apiKey)
|
||||
client.BaseURL = server.URL
|
||||
|
||||
err := client.DeleteRecord("recordID")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestClient_GetZoneID(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
server := httptest.NewServer(mux)
|
||||
|
||||
const apiKey = "myKeyD"
|
||||
mux.HandleFunc("/api/v1/zones", func(rw http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodGet {
|
||||
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
auth := req.Header.Get(authHeader)
|
||||
if auth != apiKey {
|
||||
http.Error(rw, fmt.Sprintf("invalid API key: %s", auth), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := os.Open("./fixtures/get_zone_id.json")
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
_, err = io.Copy(rw, file)
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
client := NewClient(apiKey)
|
||||
client.BaseURL = server.URL
|
||||
|
||||
zoneID, err := client.GetZoneID("example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "zoneA", zoneID)
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
{
|
||||
"record": {
|
||||
"type": "A",
|
||||
"id": "string",
|
||||
"created": "2020-05-08T10:49:18Z",
|
||||
"modified": "2020-05-08T10:49:18Z",
|
||||
"zone_id": "string",
|
||||
"name": "string",
|
||||
"value": "string",
|
||||
"ttl": 0
|
||||
}
|
||||
}
|
32
providers/dns/hetzner/internal/fixtures/get_txt_record.json
Normal file
32
providers/dns/hetzner/internal/fixtures/get_txt_record.json
Normal file
@ -0,0 +1,32 @@
|
||||
{
|
||||
"records": [
|
||||
{
|
||||
"type": "A",
|
||||
"id": "1a",
|
||||
"created": "2020-05-08T10:49:18Z",
|
||||
"modified": "2020-05-08T10:49:18Z",
|
||||
"zone_id": "zoneA",
|
||||
"name": "test",
|
||||
"value": "10.10.10.10",
|
||||
"ttl": 600
|
||||
},
|
||||
{
|
||||
"type": "TXT",
|
||||
"id": "1b",
|
||||
"created": "2020-05-08T10:49:19Z",
|
||||
"modified": "2020-05-08T10:49:19Z",
|
||||
"zone_id": "zoneA",
|
||||
"name": "test1",
|
||||
"value": "txttxttxt",
|
||||
"ttl": 600
|
||||
}
|
||||
],
|
||||
"meta": {
|
||||
"pagination": {
|
||||
"page": 1,
|
||||
"per_page": 20,
|
||||
"last_page": 1,
|
||||
"total_entries": 2
|
||||
}
|
||||
}
|
||||
}
|
66
providers/dns/hetzner/internal/fixtures/get_zone_id.json
Normal file
66
providers/dns/hetzner/internal/fixtures/get_zone_id.json
Normal file
@ -0,0 +1,66 @@
|
||||
{
|
||||
"zones": [
|
||||
{
|
||||
"id": "zoneA",
|
||||
"created": "2020-05-08T10:49:18Z",
|
||||
"modified": "2020-05-08T10:49:18Z",
|
||||
"legacy_dns_host": "string",
|
||||
"legacy_ns": [
|
||||
"string"
|
||||
],
|
||||
"name": "example.com",
|
||||
"ns": [
|
||||
"string"
|
||||
],
|
||||
"owner": "string",
|
||||
"paused": true,
|
||||
"permission": "string",
|
||||
"project": "string",
|
||||
"registrar": "string",
|
||||
"status": "verified",
|
||||
"ttl": 0,
|
||||
"verified": "2020-05-08T10:49:18Z",
|
||||
"records_count": 0,
|
||||
"is_secondary_dns": true,
|
||||
"txt_verification": {
|
||||
"name": "string",
|
||||
"token": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "zoneB",
|
||||
"created": "2020-05-08T10:49:18Z",
|
||||
"modified": "2020-05-08T10:49:18Z",
|
||||
"legacy_dns_host": "string",
|
||||
"legacy_ns": [
|
||||
"string"
|
||||
],
|
||||
"name": "example.org",
|
||||
"ns": [
|
||||
"string"
|
||||
],
|
||||
"owner": "string",
|
||||
"paused": true,
|
||||
"permission": "string",
|
||||
"project": "string",
|
||||
"registrar": "string",
|
||||
"status": "verified",
|
||||
"ttl": 0,
|
||||
"verified": "2020-05-08T10:49:18Z",
|
||||
"records_count": 0,
|
||||
"is_secondary_dns": true,
|
||||
"txt_verification": {
|
||||
"name": "string",
|
||||
"token": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"meta": {
|
||||
"pagination": {
|
||||
"page": 1,
|
||||
"per_page": 1,
|
||||
"last_page": 1,
|
||||
"total_entries": 0
|
||||
}
|
||||
}
|
||||
}
|
28
providers/dns/hetzner/internal/model.go
Normal file
28
providers/dns/hetzner/internal/model.go
Normal file
@ -0,0 +1,28 @@
|
||||
package internal
|
||||
|
||||
// DNSRecord a DNS record.
|
||||
type DNSRecord struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Value string `json:"value"`
|
||||
Priority int `json:"priority,omitempty"`
|
||||
TTL int `json:"ttl,omitempty"`
|
||||
ZoneID string `json:"zone_id,omitempty"`
|
||||
}
|
||||
|
||||
// DNSRecords a set of DNS record.
|
||||
type DNSRecords struct {
|
||||
Records []DNSRecord `json:"records"`
|
||||
}
|
||||
|
||||
// Zone a DNS zone.
|
||||
type Zone struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Zones a set of DNS zones.
|
||||
type Zones struct {
|
||||
Zones []Zone `json:"zones"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user