traefik/vendor/github.com/xenolf/lego/providers/dns/exoscale/exoscale.go

129 lines
3.3 KiB
Go
Raw Normal View History

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