2018-03-14 17:43:09 +00:00
|
|
|
// Package namedotcom implements a DNS provider for solving the DNS-01 challenge
|
|
|
|
// using Name.com's DNS service.
|
|
|
|
package namedotcom
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/namedotcom/go/namecom"
|
|
|
|
"github.com/xenolf/lego/acme"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2018-03-14 17:43:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
|
|
|
|
type DNSProvider struct {
|
|
|
|
client *namecom.NameCom
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for namedotcom.
|
|
|
|
// Credentials must be passed in the environment variables: NAMECOM_USERNAME and NAMECOM_API_TOKEN
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-06-11 15:32:50 +00:00
|
|
|
values, err := env.Get("NAMECOM_USERNAME", "NAMECOM_API_TOKEN")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Name.com: %v", err)
|
|
|
|
}
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
server := os.Getenv("NAMECOM_SERVER")
|
|
|
|
return NewDNSProviderCredentials(values["NAMECOM_USERNAME"], values["NAMECOM_API_TOKEN"], server)
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderCredentials uses the supplied credentials to return a
|
|
|
|
// DNSProvider instance configured for namedotcom.
|
|
|
|
func NewDNSProviderCredentials(username, apiToken, server string) (*DNSProvider, error) {
|
|
|
|
if username == "" {
|
|
|
|
return nil, fmt.Errorf("Name.com Username is required")
|
|
|
|
}
|
|
|
|
if apiToken == "" {
|
|
|
|
return nil, fmt.Errorf("Name.com API token is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := namecom.New(username, apiToken)
|
|
|
|
|
|
|
|
if server != "" {
|
|
|
|
client.Server = server
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{client: client}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record to fulfil the dns-01 challenge.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-03-14 17:43:09 +00:00
|
|
|
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
|
|
|
request := &namecom.Record{
|
|
|
|
DomainName: domain,
|
2018-06-21 17:06:16 +00:00
|
|
|
Host: d.extractRecordName(fqdn, domain),
|
2018-03-14 17:43:09 +00:00
|
|
|
Type: "TXT",
|
|
|
|
TTL: uint32(ttl),
|
|
|
|
Answer: value,
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err := d.client.CreateRecord(request)
|
2018-03-14 17:43:09 +00:00
|
|
|
if err != nil {
|
2018-06-11 15:32:50 +00:00
|
|
|
return fmt.Errorf("Name.com API call failed: %v", err)
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2018-03-14 17:43:09 +00:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
records, err := d.getRecords(domain)
|
2018-03-14 17:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rec := range records {
|
|
|
|
if rec.Fqdn == fqdn && rec.Type == "TXT" {
|
|
|
|
request := &namecom.DeleteRecordRequest{
|
|
|
|
DomainName: domain,
|
|
|
|
ID: rec.ID,
|
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err := d.client.DeleteRecord(request)
|
2018-03-14 17:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) getRecords(domain string) ([]*namecom.Record, error) {
|
2018-03-14 17:43:09 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
records []*namecom.Record
|
|
|
|
response *namecom.ListRecordsResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
request := &namecom.ListRecordsRequest{
|
|
|
|
DomainName: domain,
|
|
|
|
Page: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
for request.Page > 0 {
|
2018-06-21 17:06:16 +00:00
|
|
|
response, err = d.client.ListRecords(request)
|
2018-03-14 17:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records = append(records, response.Records...)
|
|
|
|
request.Page = response.NextPage
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) extractRecordName(fqdn, domain string) string {
|
2018-03-14 17:43:09 +00:00
|
|
|
name := acme.UnFqdn(fqdn)
|
|
|
|
if idx := strings.Index(name, "."+domain); idx != -1 {
|
|
|
|
return name[:idx]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|