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-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
|
|
|
)
|
|
|
|
|
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface
|
|
|
|
type DNSProvider struct {
|
2018-05-30 17:53:04 +00:00
|
|
|
clientID string
|
2016-11-02 14:33:57 +00:00
|
|
|
clientSecret string
|
2018-05-30 17:53:04 +00:00
|
|
|
subscriptionID string
|
|
|
|
tenantID string
|
2016-11-02 14:33:57 +00:00
|
|
|
resourceGroup string
|
2018-05-30 17:53:04 +00:00
|
|
|
context context.Context
|
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 {
|
|
|
|
return nil, fmt.Errorf("Azure: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewDNSProviderCredentials(
|
|
|
|
values["AZURE_CLIENT_ID"],
|
|
|
|
values["AZURE_CLIENT_SECRET"],
|
|
|
|
values["AZURE_SUBSCRIPTION_ID"],
|
|
|
|
values["AZURE_TENANT_ID"],
|
|
|
|
values["AZURE_RESOURCE_GROUP"],
|
|
|
|
)
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderCredentials uses the supplied credentials to return a
|
|
|
|
// DNSProvider instance configured for azure.
|
2018-05-30 17:53:04 +00:00
|
|
|
func NewDNSProviderCredentials(clientID, clientSecret, subscriptionID, tenantID, resourceGroup string) (*DNSProvider, error) {
|
|
|
|
if clientID == "" || clientSecret == "" || subscriptionID == "" || tenantID == "" || resourceGroup == "" {
|
2018-06-11 15:32:50 +00:00
|
|
|
return nil, errors.New("Azure: some credentials information are missing")
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{
|
2018-05-30 17:53:04 +00:00
|
|
|
clientID: clientID,
|
2016-11-02 14:33:57 +00:00
|
|
|
clientSecret: clientSecret,
|
2018-05-30 17:53:04 +00:00
|
|
|
subscriptionID: subscriptionID,
|
|
|
|
tenantID: tenantID,
|
2016-11-02 14:33:57 +00:00
|
|
|
resourceGroup: resourceGroup,
|
2018-02-14 20:28:02 +00:00
|
|
|
// TODO: A timeout can be added here for cancellation purposes.
|
2018-04-25 15:12:41 +00:00
|
|
|
context: context.Background(),
|
2016-11-02 14:33:57 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS
|
|
|
|
// propagation. Adjusting here to cope with spikes in propagation times.
|
|
|
|
func (c *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return 120 * time.Second, 2 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record to fulfil the dns-01 challenge
|
|
|
|
func (c *DNSProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
zone, err := c.getHostedZoneID(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
rsc := dns.NewRecordSetsClient(c.subscriptionID)
|
2017-05-03 14:53:59 +00:00
|
|
|
spt, err := c.newServicePrincipalTokenFromCredentials(azure.PublicCloud.ResourceManagerEndpoint)
|
2018-05-30 17:53:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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{
|
2016-11-02 14:33:57 +00:00
|
|
|
TTL: to.Int64Ptr(60),
|
2018-05-30 17:53:04 +00:00
|
|
|
TxtRecords: &[]dns.TxtRecord{{Value: &[]string{value}}},
|
2016-11-02 14:33:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
_, err = rsc.CreateOrUpdate(c.context, c.resourceGroup, zone, relative, dns.TXT, rec, "", "")
|
|
|
|
return err
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the relative record to the domain
|
|
|
|
func toRelativeRecord(domain, zone string) string {
|
|
|
|
return acme.UnFqdn(strings.TrimSuffix(domain, zone))
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
|
|
|
func (c *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
|
|
|
zone, err := c.getHostedZoneID(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
relative := toRelativeRecord(fqdn, acme.ToFqdn(zone))
|
2018-05-30 17:53:04 +00:00
|
|
|
rsc := dns.NewRecordSetsClient(c.subscriptionID)
|
2017-05-03 14:53:59 +00:00
|
|
|
spt, err := c.newServicePrincipalTokenFromCredentials(azure.PublicCloud.ResourceManagerEndpoint)
|
2016-11-02 14:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
rsc.Authorizer = autorest.NewBearerAuthorizer(spt)
|
|
|
|
|
|
|
|
_, err = rsc.Delete(c.context, c.resourceGroup, zone, relative, dns.TXT, "")
|
|
|
|
return err
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks that azure has a zone for this domain name.
|
|
|
|
func (c *DNSProvider) getHostedZoneID(fqdn string) (string, error) {
|
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we want to to Azure and get the zone.
|
2017-05-03 14:53:59 +00:00
|
|
|
spt, err := c.newServicePrincipalTokenFromCredentials(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-05-30 17:53:04 +00:00
|
|
|
dc := dns.NewZonesClient(c.subscriptionID)
|
2017-09-25 19:57:58 +00:00
|
|
|
dc.Authorizer = autorest.NewBearerAuthorizer(spt)
|
2017-05-03 14:53:59 +00:00
|
|
|
|
2018-02-14 20:28:02 +00:00
|
|
|
zone, err := dc.Get(c.context, c.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.
|
2017-05-03 14:53:59 +00:00
|
|
|
func (c *DNSProvider) newServicePrincipalTokenFromCredentials(scope string) (*adal.ServicePrincipalToken, error) {
|
2018-05-30 17:53:04 +00:00
|
|
|
oauthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, c.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-05-30 17:53:04 +00:00
|
|
|
return adal.NewServicePrincipalToken(*oauthConfig, c.clientID, c.clientSecret, scope)
|
2016-11-02 14:33:57 +00:00
|
|
|
}
|