forked from TrueCloudLab/lego
DNSimpleProvider: implement Present/CleanUp
This commit is contained in:
parent
6a3297e36f
commit
08516614dd
2 changed files with 122 additions and 0 deletions
|
@ -3,6 +3,7 @@ package acme
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/weppos/go-dnsimple/dnsimple"
|
"github.com/weppos/go-dnsimple/dnsimple"
|
||||||
)
|
)
|
||||||
|
@ -32,14 +33,102 @@ func NewDNSProviderDNSimple(dnsimpleEmail, dnsimpleApiKey string) (*DNSProviderD
|
||||||
|
|
||||||
// Present creates a TXT record to fulfil the dns-01 challenge.
|
// Present creates a TXT record to fulfil the dns-01 challenge.
|
||||||
func (c *DNSProviderDNSimple) Present(domain, token, keyAuth string) error {
|
func (c *DNSProviderDNSimple) Present(domain, token, keyAuth string) error {
|
||||||
|
fqdn, value, ttl := DNS01Record(domain, keyAuth)
|
||||||
|
|
||||||
|
zoneID, err := c.getHostedZoneID(domain)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
recordAttributes := c.newTxtRecord(domain, fqdn, value, ttl)
|
||||||
|
_, _, err = c.client.Domains.CreateRecord(zoneID, *recordAttributes)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("DNSimple API call failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CleanUp removes the TXT record matching the specified parameters.
|
// CleanUp removes the TXT record matching the specified parameters.
|
||||||
func (c *DNSProviderDNSimple) CleanUp(domain, token, keyAuth string) error {
|
func (c *DNSProviderDNSimple) CleanUp(domain, token, keyAuth string) error {
|
||||||
|
fqdn, _, _ := DNS01Record(domain, keyAuth)
|
||||||
|
|
||||||
|
records, err := c.findTxtRecords(domain, fqdn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rec := range records {
|
||||||
|
_, err := c.client.Domains.DeleteRecord(rec.DomainId, rec.Id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *DNSProviderDNSimple) getHostedZoneID(domain string) (string, error) {
|
||||||
|
domains, _, err := c.client.Domains.List()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("DNSimple API call failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var hostedDomain dnsimple.Domain
|
||||||
|
for _, d := range domains {
|
||||||
|
if strings.HasSuffix(domain, d.Name) {
|
||||||
|
if len(d.Name) > len(hostedDomain.Name) {
|
||||||
|
hostedDomain = d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if hostedDomain.Id == 0 {
|
||||||
|
return "", fmt.Errorf("No matching DNSimple domain found for domain %s", domain)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%v", hostedDomain.Id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *DNSProviderDNSimple) findTxtRecords(domain, fqdn string) ([]*dnsimple.Record, error) {
|
||||||
|
zoneID, err := c.getHostedZoneID(domain)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var records []*dnsimple.Record
|
||||||
|
result, _, err := c.client.Domains.ListRecords(zoneID, "", "TXT")
|
||||||
|
if err != nil {
|
||||||
|
return records, fmt.Errorf("DNSimple API call has failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
recordName := c.extractRecordName(fqdn, domain)
|
||||||
|
for _, record := range result {
|
||||||
|
if record.Name == recordName {
|
||||||
|
records = append(records, &record)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return records, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *DNSProviderDNSimple) newTxtRecord(domain, fqdn, value string, ttl int) *dnsimple.Record {
|
||||||
|
name := c.extractRecordName(fqdn, domain)
|
||||||
|
|
||||||
|
return &dnsimple.Record{
|
||||||
|
Type: "TXT",
|
||||||
|
Name: name,
|
||||||
|
Content: value,
|
||||||
|
TTL: ttl,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *DNSProviderDNSimple) extractRecordName(fqdn, domain string) string {
|
||||||
|
name := unFqdn(fqdn)
|
||||||
|
if idx := strings.Index(name, "."+domain); idx != -1 {
|
||||||
|
return name[:idx]
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
func dnsimpleEnvAuth() (email, apiKey string) {
|
func dnsimpleEnvAuth() (email, apiKey string) {
|
||||||
email = os.Getenv("DNSIMPLE_EMAIL")
|
email = os.Getenv("DNSIMPLE_EMAIL")
|
||||||
apiKey = os.Getenv("DNSIMPLE_API_KEY")
|
apiKey = os.Getenv("DNSIMPLE_API_KEY")
|
||||||
|
|
|
@ -3,18 +3,25 @@ package acme
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
dnsimpleLiveTest bool
|
||||||
dnsimpleEmail string
|
dnsimpleEmail string
|
||||||
dnsimpleAPIKey string
|
dnsimpleAPIKey string
|
||||||
|
dnsimpleDomain string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dnsimpleEmail = os.Getenv("DNSIMPLE_EMAIL")
|
dnsimpleEmail = os.Getenv("DNSIMPLE_EMAIL")
|
||||||
dnsimpleAPIKey = os.Getenv("DNSIMPLE_API_KEY")
|
dnsimpleAPIKey = os.Getenv("DNSIMPLE_API_KEY")
|
||||||
|
dnsimpleDomain = os.Getenv("DNSIMPLE_DOMAIN")
|
||||||
|
if len(dnsimpleEmail) > 0 && len(dnsimpleAPIKey) > 0 && len(dnsimpleDomain) > 0 {
|
||||||
|
dnsimpleLiveTest = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func restoreDNSimpleEnv() {
|
func restoreDNSimpleEnv() {
|
||||||
|
@ -44,3 +51,29 @@ func TestNewDNSProviderDNSimpleMissingCredErr(t *testing.T) {
|
||||||
assert.EqualError(t, err, "DNSimple credentials missing")
|
assert.EqualError(t, err, "DNSimple credentials missing")
|
||||||
restoreDNSimpleEnv()
|
restoreDNSimpleEnv()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLiveDNSimplePresent(t *testing.T) {
|
||||||
|
if !dnsimpleLiveTest {
|
||||||
|
t.Skip("skipping live test")
|
||||||
|
}
|
||||||
|
|
||||||
|
provider, err := NewDNSProviderDNSimple(dnsimpleEmail, dnsimpleAPIKey)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
err = provider.Present(dnsimpleDomain, "", "123d==")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveDNSimpleCleanUp(t *testing.T) {
|
||||||
|
if !dnsimpleLiveTest {
|
||||||
|
t.Skip("skipping live test")
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 1)
|
||||||
|
|
||||||
|
provider, err := NewDNSProviderDNSimple(cflareEmail, cflareAPIKey)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
err = provider.CleanUp(dnsimpleDomain, "", "123d==")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue