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

275 lines
7.9 KiB
Go
Raw Normal View History

2019-01-07 17:30:06 +00:00
// Package azure implements a DNS provider for solving the DNS-01 challenge using azure DNS.
2017-02-07 21:33:23 +00:00
// Azure doesn't like trailing dots on domain names, most of the acme code does.
package azure
import (
2018-04-09 16:28:03 +00:00
"context"
2018-07-03 10:44:04 +00:00
"errors"
2017-02-07 21:33:23 +00:00
"fmt"
2018-10-23 09:18:02 +00:00
"io/ioutil"
2018-09-17 13:16:03 +00:00
"net/http"
2017-04-07 09:53:39 +00:00
"strings"
2018-04-09 16:28:03 +00:00
"time"
2017-04-07 09:53:39 +00:00
2018-04-09 16:28:03 +00:00
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-09-01/dns"
2017-10-31 09:42:03 +00:00
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
2017-02-07 21:33:23 +00:00
"github.com/Azure/go-autorest/autorest/azure"
2018-10-23 09:18:02 +00:00
"github.com/Azure/go-autorest/autorest/azure/auth"
2017-02-07 21:33:23 +00:00
"github.com/Azure/go-autorest/autorest/to"
2019-01-07 17:30:06 +00:00
"github.com/xenolf/lego/challenge/dns01"
2018-07-03 10:44:04 +00:00
"github.com/xenolf/lego/platform/config/env"
2017-02-07 21:33:23 +00:00
)
2018-10-23 09:18:02 +00:00
const defaultMetadataEndpoint = "http://169.254.169.254"
2018-09-17 13:16:03 +00:00
// Config is used to configure the creation of the DNSProvider
type Config struct {
2018-10-23 09:18:02 +00:00
// optional if using instance metadata service
ClientID string
ClientSecret string
TenantID string
SubscriptionID string
ResourceGroup string
MetadataEndpoint string
2018-09-17 13:16:03 +00:00
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
HTTPClient *http.Client
}
// NewDefaultConfig returns a default configuration for the DNSProvider
func NewDefaultConfig() *Config {
return &Config{
TTL: env.GetOrDefaultInt("AZURE_TTL", 60),
PropagationTimeout: env.GetOrDefaultSecond("AZURE_PROPAGATION_TIMEOUT", 2*time.Minute),
PollingInterval: env.GetOrDefaultSecond("AZURE_POLLING_INTERVAL", 2*time.Second),
2018-10-23 09:18:02 +00:00
MetadataEndpoint: env.GetOrFile("AZURE_METADATA_ENDPOINT"),
2018-09-17 13:16:03 +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 {
2018-10-23 09:18:02 +00:00
config *Config
authorizer autorest.Authorizer
2017-02-07 21:33:23 +00:00
}
// NewDNSProvider returns a DNSProvider instance configured for azure.
2018-10-23 09:18:02 +00:00
// Credentials can be passed in the environment variables:
// AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID, AZURE_RESOURCE_GROUP
// If the credentials are _not_ set via the environment,
// then it will attempt to get a bearer token via the instance metadata service.
// see: https://github.com/Azure/go-autorest/blob/v10.14.0/autorest/azure/auth/auth.go#L38-L42
2017-02-07 21:33:23 +00:00
func NewDNSProvider() (*DNSProvider, error) {
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
2018-10-23 09:18:02 +00:00
config.SubscriptionID = env.GetOrFile("AZURE_SUBSCRIPTION_ID")
config.ResourceGroup = env.GetOrFile("AZURE_RESOURCE_GROUP")
2018-09-17 13:16:03 +00:00
return NewDNSProviderConfig(config)
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
// NewDNSProviderConfig return a DNSProvider instance configured for Azure.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("azure: the configuration of the DNS provider is nil")
2017-02-07 21:33:23 +00:00
}
2018-10-23 09:18:02 +00:00
if config.HTTPClient == nil {
config.HTTPClient = http.DefaultClient
}
authorizer, err := getAuthorizer(config)
if err != nil {
return nil, err
}
if config.SubscriptionID == "" {
subsID, err := getMetadata(config, "subscriptionId")
if err != nil {
return nil, fmt.Errorf("azure: %v", err)
}
if subsID == "" {
return nil, errors.New("azure: SubscriptionID is missing")
}
config.SubscriptionID = subsID
}
if config.ResourceGroup == "" {
resGroup, err := getMetadata(config, "resourceGroupName")
if err != nil {
return nil, fmt.Errorf("azure: %v", err)
}
if resGroup == "" {
return nil, errors.New("azure: ResourceGroup is missing")
}
config.ResourceGroup = resGroup
2018-09-17 13:16:03 +00:00
}
2018-10-23 09:18:02 +00:00
return &DNSProvider{config: config, authorizer: authorizer}, nil
2017-02-07 21:33:23 +00:00
}
2019-01-07 17:30:06 +00:00
// Timeout returns the timeout and interval to use when checking for DNS propagation.
// Adjusting here to cope with spikes in propagation times.
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
2018-09-17 13:16:03 +00:00
return d.config.PropagationTimeout, d.config.PollingInterval
2017-02-07 21:33:23 +00:00
}
2018-10-10 14:28:04 +00:00
// Present creates a TXT record to fulfill the dns-01 challenge
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2018-09-17 13:16:03 +00:00
ctx := context.Background()
2019-01-07 17:30:06 +00:00
fqdn, value := dns01.GetRecord(domain, keyAuth)
2018-09-17 13:16:03 +00:00
zone, err := d.getHostedZoneID(ctx, fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("azure: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
rsc := dns.NewRecordSetsClient(d.config.SubscriptionID)
2018-10-23 09:18:02 +00:00
rsc.Authorizer = d.authorizer
2017-10-31 09:42:03 +00:00
2019-01-07 17:30:06 +00:00
relative := toRelativeRecord(fqdn, dns01.ToFqdn(zone))
// Get existing record set
rset, err := rsc.Get(ctx, d.config.ResourceGroup, zone, relative, dns.TXT)
if err != nil {
detailedError, ok := err.(autorest.DetailedError)
if !ok || detailedError.StatusCode != http.StatusNotFound {
return fmt.Errorf("azure: %v", err)
}
}
// Construct unique TXT records using map
uniqRecords := map[string]struct{}{value: {}}
if rset.RecordSetProperties != nil && rset.TxtRecords != nil {
for _, txtRecord := range *rset.TxtRecords {
// Assume Value doesn't contain multiple strings
if txtRecord.Value != nil && len(*txtRecord.Value) > 0 {
uniqRecords[(*txtRecord.Value)[0]] = struct{}{}
}
}
}
var txtRecords []dns.TxtRecord
for txt := range uniqRecords {
txtRecords = append(txtRecords, dns.TxtRecord{Value: &[]string{txt}})
}
2017-02-07 21:33:23 +00:00
rec := dns.RecordSet{
Name: &relative,
RecordSetProperties: &dns.RecordSetProperties{
2018-09-17 13:16:03 +00:00
TTL: to.Int64Ptr(int64(d.config.TTL)),
2019-01-07 17:30:06 +00:00
TxtRecords: &txtRecords,
2017-02-07 21:33:23 +00:00
},
}
2018-09-17 13:16:03 +00:00
_, err = rsc.CreateOrUpdate(ctx, d.config.ResourceGroup, zone, relative, dns.TXT, rec, "", "")
2018-09-21 16:38:02 +00:00
if err != nil {
return fmt.Errorf("azure: %v", err)
}
return nil
2017-02-07 21:33:23 +00:00
}
// CleanUp removes the TXT record matching the specified parameters
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2018-09-17 13:16:03 +00:00
ctx := context.Background()
2019-01-07 17:30:06 +00:00
fqdn, _ := dns01.GetRecord(domain, keyAuth)
2017-02-07 21:33:23 +00:00
2018-09-17 13:16:03 +00:00
zone, err := d.getHostedZoneID(ctx, fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("azure: %v", err)
2017-02-07 21:33:23 +00:00
}
2019-01-07 17:30:06 +00:00
relative := toRelativeRecord(fqdn, dns01.ToFqdn(zone))
2018-09-17 13:16:03 +00:00
rsc := dns.NewRecordSetsClient(d.config.SubscriptionID)
2018-10-23 09:18:02 +00:00
rsc.Authorizer = d.authorizer
2018-05-31 07:30:04 +00:00
2018-09-17 13:16:03 +00:00
_, err = rsc.Delete(ctx, d.config.ResourceGroup, zone, relative, dns.TXT, "")
2018-09-21 16:38:02 +00:00
if err != nil {
return fmt.Errorf("azure: %v", err)
}
return nil
2017-02-07 21:33:23 +00:00
}
// Checks that azure has a zone for this domain name.
2018-09-17 13:16:03 +00:00
func (d *DNSProvider) getHostedZoneID(ctx context.Context, fqdn string) (string, error) {
2019-01-07 17:30:06 +00:00
authZone, err := dns01.FindZoneByFqdn(fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
return "", err
}
2018-09-17 13:16:03 +00:00
dc := dns.NewZonesClient(d.config.SubscriptionID)
2018-10-23 09:18:02 +00:00
dc.Authorizer = d.authorizer
2017-10-31 09:42:03 +00:00
2019-01-07 17:30:06 +00:00
zone, err := dc.Get(ctx, d.config.ResourceGroup, dns01.UnFqdn(authZone))
2017-02-07 21:33:23 +00:00
if err != nil {
return "", err
}
// zone.Name shouldn't have a trailing dot(.)
return to.String(zone.Name), nil
}
2018-09-17 13:16:03 +00:00
// Returns the relative record to the domain
func toRelativeRecord(domain, zone string) string {
2019-01-07 17:30:06 +00:00
return dns01.UnFqdn(strings.TrimSuffix(domain, zone))
2017-02-07 21:33:23 +00:00
}
2018-10-23 09:18:02 +00:00
func getAuthorizer(config *Config) (autorest.Authorizer, error) {
if config.ClientID != "" && config.ClientSecret != "" && config.TenantID != "" {
oauthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, config.TenantID)
if err != nil {
return nil, err
}
spt, err := adal.NewServicePrincipalToken(*oauthConfig, config.ClientID, config.ClientSecret, azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
return nil, err
}
spt.SetSender(config.HTTPClient)
return autorest.NewBearerAuthorizer(spt), nil
}
return auth.NewAuthorizerFromEnvironment()
}
// Fetches metadata from environment or he instance metadata service
// borrowed from https://github.com/Microsoft/azureimds/blob/master/imdssample.go
func getMetadata(config *Config, field string) (string, error) {
metadataEndpoint := config.MetadataEndpoint
if len(metadataEndpoint) == 0 {
metadataEndpoint = defaultMetadataEndpoint
}
resource := fmt.Sprintf("%s/metadata/instance/compute/%s", metadataEndpoint, field)
req, err := http.NewRequest(http.MethodGet, resource, nil)
if err != nil {
return "", err
}
req.Header.Add("Metadata", "True")
q := req.URL.Query()
q.Add("format", "text")
q.Add("api-version", "2017-12-01")
req.URL.RawQuery = q.Encode()
resp, err := config.HTTPClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
2019-01-07 17:30:06 +00:00
return string(respBody), nil
2018-10-23 09:18:02 +00:00
}