2016-11-07 07:37:57 +00:00
|
|
|
// Package exoscale implements a DNS provider for solving the DNS-01 challenge
|
|
|
|
// using exoscale DNS.
|
|
|
|
package exoscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2017-09-13 21:40:58 +00:00
|
|
|
"github.com/exoscale/egoscale"
|
2016-11-07 07:37:57 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2016-11-07 07:37:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
|
|
|
|
type DNSProvider struct {
|
|
|
|
client *egoscale.Client
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// NewDNSProvider Credentials must be passed in the environment variables:
|
2016-11-07 07:37:57 +00:00
|
|
|
// EXOSCALE_API_KEY, EXOSCALE_API_SECRET, EXOSCALE_ENDPOINT.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-06-11 15:32:50 +00:00
|
|
|
values, err := env.Get("EXOSCALE_API_KEY", "EXOSCALE_API_SECRET")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Exoscale: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-11-07 07:37:57 +00:00
|
|
|
endpoint := os.Getenv("EXOSCALE_ENDPOINT")
|
2018-06-11 15:32:50 +00:00
|
|
|
return NewDNSProviderClient(values["EXOSCALE_API_KEY"], values["EXOSCALE_API_SECRET"], endpoint)
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// NewDNSProviderClient Uses the supplied parameters to return a DNSProvider instance
|
2016-11-07 07:37:57 +00:00
|
|
|
// configured for Exoscale.
|
|
|
|
func NewDNSProviderClient(key, secret, endpoint string) (*DNSProvider, error) {
|
|
|
|
if key == "" || secret == "" {
|
|
|
|
return nil, fmt.Errorf("Exoscale credentials missing")
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-11-07 07:37:57 +00:00
|
|
|
if endpoint == "" {
|
|
|
|
endpoint = "https://api.exoscale.ch/dns"
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{
|
|
|
|
client: egoscale.NewClient(endpoint, key, secret),
|
|
|
|
}, 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 {
|
2016-11-07 07:37:57 +00:00
|
|
|
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
|
2018-06-21 17:06:16 +00:00
|
|
|
zone, recordName, err := d.FindZoneAndRecordName(fqdn, domain)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
recordID, err := d.FindExistingRecordID(zone, recordName)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
record := egoscale.DNSRecord{
|
|
|
|
Name: recordName,
|
2018-01-15 20:58:24 +00:00
|
|
|
TTL: ttl,
|
2016-11-07 07:37:57 +00:00
|
|
|
Content: value,
|
|
|
|
RecordType: "TXT",
|
|
|
|
}
|
|
|
|
|
2018-01-15 20:58:24 +00:00
|
|
|
if recordID == 0 {
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err := d.client.CreateRecord(zone, record)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("Error while creating DNS record: " + err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-15 20:58:24 +00:00
|
|
|
record.ID = recordID
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err := d.client.UpdateRecord(zone, record)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("Error while updating DNS record: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the record matching the specified parameters.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-11-07 07:37:57 +00:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
2018-06-21 17:06:16 +00:00
|
|
|
zone, recordName, err := d.FindZoneAndRecordName(fqdn, domain)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
recordID, err := d.FindExistingRecordID(zone, recordName)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-15 20:58:24 +00:00
|
|
|
if recordID != 0 {
|
2018-06-21 17:06:16 +00:00
|
|
|
err = d.client.DeleteRecord(zone, recordID)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("Error while deleting DNS record: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// FindExistingRecordID Query Exoscale to find an existing record for this name.
|
2016-11-07 07:37:57 +00:00
|
|
|
// Returns nil if no record could be found
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) FindExistingRecordID(zone, recordName string) (int64, error) {
|
|
|
|
records, err := d.client.GetRecords(zone)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, errors.New("Error while retrievening DNS records: " + err.Error())
|
|
|
|
}
|
2018-01-15 20:58:24 +00:00
|
|
|
for _, record := range records {
|
|
|
|
if record.Name == recordName {
|
|
|
|
return record.ID, nil
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// FindZoneAndRecordName Extract DNS zone and DNS entry name
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) FindZoneAndRecordName(fqdn, domain string) (string, string, error) {
|
2016-11-07 07:37:57 +00:00
|
|
|
zone, err := acme.FindZoneByFqdn(acme.ToFqdn(domain), acme.RecursiveNameservers)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
zone = acme.UnFqdn(zone)
|
|
|
|
name := acme.UnFqdn(fqdn)
|
|
|
|
name = name[:len(name)-len("."+zone)]
|
|
|
|
|
|
|
|
return zone, name, nil
|
|
|
|
}
|