2016-11-02 14:33:57 +00:00
|
|
|
// Package azure implements a DNS provider for solving the DNS-01
|
|
|
|
// challenge using azure DNS.
|
|
|
|
// Azure doesn't like trailing dots on domain names, most of the acme code does.
|
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2018-02-14 20:28:02 +00:00
|
|
|
"context"
|
2018-06-11 15:32:50 +00:00
|
|
|
"errors"
|
2016-11-02 14:33:57 +00:00
|
|
|
"fmt"
|
2018-09-15 17:07:24 +00:00
|
|
|
"net/http"
|
2018-05-30 17:53:04 +00:00
|
|
|
"strings"
|
2016-11-02 14:33:57 +00:00
|
|
|
"time"
|
|
|
|
|
2018-02-14 20:28:02 +00:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-09-01/dns"
|
2017-05-03 14:53:59 +00:00
|
|
|
"github.com/Azure/go-autorest/autorest"
|
|
|
|
"github.com/Azure/go-autorest/autorest/adal"
|
2016-11-02 14:33:57 +00:00
|
|
|
"github.com/Azure/go-autorest/autorest/azure"
|
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
|
|
|
"github.com/xenolf/lego/acme"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2016-11-02 14:33:57 +00:00
|
|
|
)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
SubscriptionID string
|
|
|
|
TenantID string
|
|
|
|
ResourceGroup string
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:33:57 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface
|
|
|
|
type DNSProvider struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for azure.
|
|
|
|
// Credentials must be passed in the environment variables: AZURE_CLIENT_ID,
|
2017-11-14 00:14:38 +00:00
|
|
|
// AZURE_CLIENT_SECRET, AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID, AZURE_RESOURCE_GROUP
|
2016-11-02 14:33:57 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-06-11 15:32:50 +00:00
|
|
|
values, err := env.Get("AZURE_CLIENT_ID", "AZURE_CLIENT_SECRET", "AZURE_SUBSCRIPTION_ID", "AZURE_TENANT_ID", "AZURE_RESOURCE_GROUP")
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("azure: %v", err)
|
2018-06-11 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.ClientID = values["AZURE_CLIENT_ID"]
|
|
|
|
config.ClientSecret = values["AZURE_CLIENT_SECRET"]
|
|
|
|
config.SubscriptionID = values["AZURE_SUBSCRIPTION_ID"]
|
|
|
|
config.TenantID = values["AZURE_TENANT_ID"]
|
|
|
|
config.ResourceGroup = values["AZURE_RESOURCE_GROUP"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for azure.
|
|
|
|
// Deprecated
|
2018-05-30 17:53:04 +00:00
|
|
|
func NewDNSProviderCredentials(clientID, clientSecret, subscriptionID, tenantID, resourceGroup string) (*DNSProvider, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.ClientID = clientID
|
|
|
|
config.ClientSecret = clientSecret
|
|
|
|
config.SubscriptionID = subscriptionID
|
|
|
|
config.TenantID = tenantID
|
|
|
|
config.ResourceGroup = resourceGroup
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.ClientID == "" || config.ClientSecret == "" || config.SubscriptionID == "" || config.TenantID == "" || config.ResourceGroup == "" {
|
|
|
|
return nil, errors.New("azure: some credentials information are missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{config: config}, nil
|
2016-11-02 14:33:57 +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-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-09-15 17:07:24 +00:00
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-15 17:07:24 +00:00
|
|
|
ctx := context.Background()
|
2016-11-02 14:33:57 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
zone, err := d.getHostedZoneID(ctx, fqdn)
|
2016-11-02 14:33:57 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("azure: %v", err)
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
rsc := dns.NewRecordSetsClient(d.config.SubscriptionID)
|
|
|
|
spt, err := d.newServicePrincipalToken(azure.PublicCloud.ResourceManagerEndpoint)
|
2018-05-30 17:53:04 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("azure: %v", err)
|
2018-05-30 17:53:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 14:53:59 +00:00
|
|
|
rsc.Authorizer = autorest.NewBearerAuthorizer(spt)
|
|
|
|
|
2016-11-02 14:33:57 +00:00
|
|
|
relative := toRelativeRecord(fqdn, acme.ToFqdn(zone))
|
|
|
|
rec := dns.RecordSet{
|
|
|
|
Name: &relative,
|
2016-11-30 22:05:55 +00:00
|
|
|
RecordSetProperties: &dns.RecordSetProperties{
|
2018-09-15 17:07:24 +00:00
|
|
|
TTL: to.Int64Ptr(int64(d.config.TTL)),
|
2018-05-30 17:53:04 +00:00
|
|
|
TxtRecords: &[]dns.TxtRecord{{Value: &[]string{value}}},
|
2016-11-02 14:33:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
_, err = rsc.CreateOrUpdate(ctx, d.config.ResourceGroup, zone, relative, dns.TXT, rec, "", "")
|
2018-09-20 21:18:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("azure: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-15 17:07:24 +00:00
|
|
|
ctx := context.Background()
|
2016-11-02 14:33:57 +00:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
zone, err := d.getHostedZoneID(ctx, fqdn)
|
2016-11-02 14:33:57 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("azure: %v", err)
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
relative := toRelativeRecord(fqdn, acme.ToFqdn(zone))
|
2018-09-15 17:07:24 +00:00
|
|
|
rsc := dns.NewRecordSetsClient(d.config.SubscriptionID)
|
|
|
|
spt, err := d.newServicePrincipalToken(azure.PublicCloud.ResourceManagerEndpoint)
|
2016-11-02 14:33:57 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("azure: %v", err)
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
rsc.Authorizer = autorest.NewBearerAuthorizer(spt)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
_, err = rsc.Delete(ctx, d.config.ResourceGroup, zone, relative, dns.TXT, "")
|
2018-09-20 21:18:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("azure: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks that azure has a zone for this domain name.
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) getHostedZoneID(ctx context.Context, fqdn string) (string, error) {
|
2016-11-02 14:33:57 +00:00
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we want to to Azure and get the zone.
|
2018-09-15 17:07:24 +00:00
|
|
|
spt, err := d.newServicePrincipalToken(azure.PublicCloud.ResourceManagerEndpoint)
|
2018-05-30 17:53:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-25 19:57:58 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
dc := dns.NewZonesClient(d.config.SubscriptionID)
|
2017-09-25 19:57:58 +00:00
|
|
|
dc.Authorizer = autorest.NewBearerAuthorizer(spt)
|
2017-05-03 14:53:59 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
zone, err := dc.Get(ctx, d.config.ResourceGroup, acme.UnFqdn(authZone))
|
2016-11-02 14:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// zone.Name shouldn't have a trailing dot(.)
|
|
|
|
return to.String(zone.Name), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServicePrincipalTokenFromCredentials creates a new ServicePrincipalToken using values of the
|
|
|
|
// passed credentials map.
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) newServicePrincipalToken(scope string) (*adal.ServicePrincipalToken, error) {
|
|
|
|
oauthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, d.config.TenantID)
|
2016-11-02 14:33:57 +00:00
|
|
|
if err != nil {
|
2018-05-30 17:53:04 +00:00
|
|
|
return nil, err
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
return adal.NewServicePrincipalToken(*oauthConfig, d.config.ClientID, d.config.ClientSecret, scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the relative record to the domain
|
|
|
|
func toRelativeRecord(domain, zone string) string {
|
|
|
|
return acme.UnFqdn(strings.TrimSuffix(domain, zone))
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|