1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-13 21:55:28 +02:00
lego/providers/dns/exoscale/exoscale.go

185 lines
5.1 KiB
Go
Raw Normal View History

// Package exoscale implements a DNS provider for solving the DNS-01 challenge using exoscale DNS.
2019-03-11 18:56:48 +02:00
package exoscale
2016-11-07 09:37:57 +02:00
import (
2019-02-18 11:49:41 +02:00
"context"
2016-11-07 09:37:57 +02:00
"errors"
"fmt"
"net/http"
"time"
2016-11-07 09:37:57 +02:00
"github.com/exoscale/egoscale"
"github.com/go-acme/lego/v3/challenge/dns01"
"github.com/go-acme/lego/v3/platform/config/env"
2016-11-07 09:37:57 +02:00
)
2019-02-18 11:49:41 +02:00
const defaultBaseURL = "https://api.exoscale.com/dns"
// Config is used to configure the creation of the DNSProvider
type Config struct {
APIKey string
APISecret string
Endpoint string
HTTPClient *http.Client
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
}
// NewDefaultConfig returns a default configuration for the DNSProvider
func NewDefaultConfig() *Config {
return &Config{
TTL: env.GetOrDefaultInt("EXOSCALE_TTL", dns01.DefaultTTL),
PropagationTimeout: env.GetOrDefaultSecond("EXOSCALE_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond("EXOSCALE_POLLING_INTERVAL", dns01.DefaultPollingInterval),
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond("EXOSCALE_HTTP_TIMEOUT", 0),
},
}
}
2016-11-07 09:37:57 +02:00
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
type DNSProvider struct {
config *Config
2016-11-07 09:37:57 +02:00
client *egoscale.Client
}
2018-05-30 19:53:04 +02:00
// NewDNSProvider Credentials must be passed in the environment variables:
2016-11-07 09:37:57 +02:00
// EXOSCALE_API_KEY, EXOSCALE_API_SECRET, EXOSCALE_ENDPOINT.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get("EXOSCALE_API_KEY", "EXOSCALE_API_SECRET")
if err != nil {
return nil, fmt.Errorf("exoscale: %v", err)
}
config := NewDefaultConfig()
config.APIKey = values["EXOSCALE_API_KEY"]
config.APISecret = values["EXOSCALE_API_SECRET"]
config.Endpoint = env.GetOrFile("EXOSCALE_ENDPOINT")
return NewDNSProviderConfig(config)
2016-11-07 09:37:57 +02:00
}
// NewDNSProviderConfig return a DNSProvider instance configured for Exoscale.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("the configuration of the DNS provider is nil")
}
if config.APIKey == "" || config.APISecret == "" {
return nil, fmt.Errorf("exoscale: credentials missing")
2016-11-07 09:37:57 +02:00
}
if config.Endpoint == "" {
config.Endpoint = defaultBaseURL
2016-11-07 09:37:57 +02:00
}
client := egoscale.NewClient(config.Endpoint, config.APIKey, config.APISecret)
client.HTTPClient = config.HTTPClient
return &DNSProvider{client: client, config: config}, nil
2016-11-07 09:37:57 +02:00
}
// Present creates a TXT record to fulfill the dns-01 challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2019-02-18 11:49:41 +02:00
ctx := context.Background()
fqdn, value := dns01.GetRecord(domain, keyAuth)
zone, recordName, err := d.FindZoneAndRecordName(fqdn, domain)
2016-11-07 09:37:57 +02:00
if err != nil {
return err
}
recordID, err := d.FindExistingRecordID(zone, recordName)
2016-11-07 09:37:57 +02:00
if err != nil {
return err
}
if recordID == 0 {
record := egoscale.DNSRecord{
Name: recordName,
TTL: d.config.TTL,
Content: value,
RecordType: "TXT",
}
2019-02-18 11:49:41 +02:00
_, err := d.client.CreateRecord(ctx, zone, record)
2016-11-07 09:37:57 +02:00
if err != nil {
return errors.New("Error while creating DNS record: " + err.Error())
}
} else {
record := egoscale.UpdateDNSRecord{
ID: recordID,
Name: recordName,
TTL: d.config.TTL,
Content: value,
RecordType: "TXT",
}
2019-02-18 11:49:41 +02:00
_, err := d.client.UpdateRecord(ctx, zone, record)
2016-11-07 09:37:57 +02:00
if err != nil {
return errors.New("Error while updating DNS record: " + err.Error())
}
}
return nil
}
// CleanUp removes the record matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2019-02-18 11:49:41 +02:00
ctx := context.Background()
fqdn, _ := dns01.GetRecord(domain, keyAuth)
zone, recordName, err := d.FindZoneAndRecordName(fqdn, domain)
2016-11-07 09:37:57 +02:00
if err != nil {
return err
}
recordID, err := d.FindExistingRecordID(zone, recordName)
2016-11-07 09:37:57 +02:00
if err != nil {
return err
}
if recordID != 0 {
2019-02-18 11:49:41 +02:00
err = d.client.DeleteRecord(ctx, zone, recordID)
2016-11-07 09:37:57 +02:00
if err != nil {
return errors.New("Error while deleting DNS record: " + err.Error())
}
}
return 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
}
2018-05-30 19:53:04 +02:00
// FindExistingRecordID Query Exoscale to find an existing record for this name.
2016-11-07 09:37:57 +02:00
// Returns nil if no record could be found
func (d *DNSProvider) FindExistingRecordID(zone, recordName string) (int64, error) {
2019-02-18 11:49:41 +02:00
ctx := context.Background()
records, err := d.client.GetRecords(ctx, zone)
2016-11-07 09:37:57 +02: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
2016-11-07 09:37:57 +02:00
}
}
return 0, nil
}
2018-05-30 19:53:04 +02:00
// FindZoneAndRecordName Extract DNS zone and DNS entry name
func (d *DNSProvider) FindZoneAndRecordName(fqdn, domain string) (string, string, error) {
zone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(domain))
2016-11-07 09:37:57 +02:00
if err != nil {
return "", "", err
}
zone = dns01.UnFqdn(zone)
name := dns01.UnFqdn(fqdn)
2016-11-07 09:37:57 +02:00
name = name[:len(name)-len("."+zone)]
return zone, name, nil
}