2018-12-06 23:50:17 +02:00
|
|
|
// Package azure implements a DNS provider for solving the DNS-01 challenge using azure DNS.
|
2016-11-02 16:33:57 +02:00
|
|
|
// Azure doesn't like trailing dots on domain names, most of the acme code does.
|
2019-03-11 18:56:48 +02:00
|
|
|
package azure
|
2016-11-02 16:33:57 +02:00
|
|
|
|
|
|
|
import (
|
2018-06-11 17:32:50 +02:00
|
|
|
"errors"
|
2016-11-02 16:33:57 +02:00
|
|
|
"fmt"
|
2021-08-25 11:44:11 +02:00
|
|
|
"io"
|
2018-09-15 19:07:24 +02:00
|
|
|
"net/http"
|
2023-05-05 09:49:38 +02:00
|
|
|
"net/url"
|
2016-11-02 16:33:57 +02:00
|
|
|
"time"
|
|
|
|
|
2017-05-03 16:53:59 +02:00
|
|
|
"github.com/Azure/go-autorest/autorest"
|
2020-08-09 14:17:49 +02:00
|
|
|
aazure "github.com/Azure/go-autorest/autorest/azure"
|
2018-10-23 10:03:31 +02:00
|
|
|
"github.com/Azure/go-autorest/autorest/azure/auth"
|
2022-01-17 11:05:59 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge"
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2023-05-05 09:49:38 +02:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2016-11-02 16:33:57 +02:00
|
|
|
)
|
|
|
|
|
2018-10-23 10:03:31 +02:00
|
|
|
const defaultMetadataEndpoint = "http://169.254.169.254"
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "AZURE_"
|
|
|
|
|
2020-08-09 14:17:49 +02:00
|
|
|
EnvEnvironment = envNamespace + "ENVIRONMENT"
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvMetadataEndpoint = envNamespace + "METADATA_ENDPOINT"
|
|
|
|
EnvSubscriptionID = envNamespace + "SUBSCRIPTION_ID"
|
|
|
|
EnvResourceGroup = envNamespace + "RESOURCE_GROUP"
|
|
|
|
EnvTenantID = envNamespace + "TENANT_ID"
|
|
|
|
EnvClientID = envNamespace + "CLIENT_ID"
|
|
|
|
EnvClientSecret = envNamespace + "CLIENT_SECRET"
|
2021-08-07 13:03:14 +02:00
|
|
|
EnvZoneName = envNamespace + "ZONE_NAME"
|
2022-01-17 11:05:59 +02:00
|
|
|
EnvPrivateZone = envNamespace + "PRIVATE_ZONE"
|
2020-03-12 00:51:10 +02:00
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
type Config struct {
|
2024-07-21 15:06:01 +02:00
|
|
|
ZoneName string
|
|
|
|
|
2018-10-23 10:03:31 +02:00
|
|
|
// optional if using instance metadata service
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
TenantID string
|
|
|
|
|
|
|
|
SubscriptionID string
|
|
|
|
ResourceGroup string
|
2022-01-17 11:05:59 +02:00
|
|
|
PrivateZone bool
|
2018-10-23 10:03:31 +02:00
|
|
|
|
2020-08-09 14:17:49 +02:00
|
|
|
MetadataEndpoint string
|
|
|
|
ResourceManagerEndpoint string
|
|
|
|
ActiveDirectoryEndpoint string
|
2018-10-23 10:03:31 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2024-07-21 15:06:01 +02:00
|
|
|
ZoneName: env.GetOrFile(EnvZoneName),
|
2020-08-09 14:17:49 +02:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 60),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
|
|
|
|
MetadataEndpoint: env.GetOrFile(EnvMetadataEndpoint),
|
|
|
|
ResourceManagerEndpoint: aazure.PublicCloud.ResourceManagerEndpoint,
|
|
|
|
ActiveDirectoryEndpoint: aazure.PublicCloud.ActiveDirectoryEndpoint,
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2016-11-02 16:33:57 +02:00
|
|
|
type DNSProvider struct {
|
2022-01-17 11:05:59 +02:00
|
|
|
provider challenge.ProviderTimeout
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for azure.
|
2018-10-23 10:03:31 +02:00
|
|
|
// Credentials can be passed in the environment variables:
|
2020-08-09 14:17:49 +02:00
|
|
|
// AZURE_ENVIRONMENT, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET,
|
|
|
|
// AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID, AZURE_RESOURCE_GROUP
|
2018-10-23 10:03:31 +02:00
|
|
|
// 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
|
2023-07-12 20:02:06 +02:00
|
|
|
// Deprecated: use azuredns instead.
|
2016-11-02 16:33:57 +02:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
2020-08-09 14:17:49 +02:00
|
|
|
|
|
|
|
environmentName := env.GetOrFile(EnvEnvironment)
|
|
|
|
if environmentName != "" {
|
|
|
|
var environment aazure.Environment
|
|
|
|
switch environmentName {
|
|
|
|
case "china":
|
|
|
|
environment = aazure.ChinaCloud
|
|
|
|
case "german":
|
|
|
|
environment = aazure.GermanCloud
|
|
|
|
case "public":
|
|
|
|
environment = aazure.PublicCloud
|
|
|
|
case "usgovernment":
|
|
|
|
environment = aazure.USGovernmentCloud
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("azure: unknown environment %s", environmentName)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.ResourceManagerEndpoint = environment.ResourceManagerEndpoint
|
|
|
|
config.ActiveDirectoryEndpoint = environment.ActiveDirectoryEndpoint
|
|
|
|
}
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
config.SubscriptionID = env.GetOrFile(EnvSubscriptionID)
|
|
|
|
config.ResourceGroup = env.GetOrFile(EnvResourceGroup)
|
2020-03-13 01:08:58 +02:00
|
|
|
config.ClientSecret = env.GetOrFile(EnvClientSecret)
|
|
|
|
config.ClientID = env.GetOrFile(EnvClientID)
|
|
|
|
config.TenantID = env.GetOrFile(EnvTenantID)
|
2022-01-17 11:05:59 +02:00
|
|
|
config.PrivateZone = env.GetOrDefaultBool(EnvPrivateZone, false)
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Azure.
|
2023-07-12 20:02:06 +02:00
|
|
|
// Deprecated: use azuredns instead.
|
2018-09-15 19:07:24 +02:00
|
|
|
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 16:33:57 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 10:03:31 +02:00
|
|
|
if config.HTTPClient == nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
config.HTTPClient = &http.Client{Timeout: 5 * time.Second}
|
2018-10-23 10:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
authorizer, err := getAuthorizer(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.SubscriptionID == "" {
|
|
|
|
subsID, err := getMetadata(config, "subscriptionId")
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("azure: %w", err)
|
2018-10-23 10:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if subsID == "" {
|
|
|
|
return nil, errors.New("azure: SubscriptionID is missing")
|
|
|
|
}
|
|
|
|
config.SubscriptionID = subsID
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ResourceGroup == "" {
|
|
|
|
resGroup, err := getMetadata(config, "resourceGroupName")
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("azure: %w", err)
|
2018-10-23 10:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if resGroup == "" {
|
|
|
|
return nil, errors.New("azure: ResourceGroup is missing")
|
|
|
|
}
|
|
|
|
config.ResourceGroup = resGroup
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 11:05:59 +02:00
|
|
|
if config.PrivateZone {
|
|
|
|
return &DNSProvider{provider: &dnsProviderPrivate{config: config, authorizer: authorizer}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{provider: &dnsProviderPublic{config: config, authorizer: authorizer}}, nil
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02: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 19:06:16 +02:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2022-01-17 11:05:59 +02:00
|
|
|
return d.provider.Timeout()
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2022-01-17 11:05:59 +02:00
|
|
|
return d.provider.Present(domain, token, keyAuth)
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2022-01-17 11:05:59 +02:00
|
|
|
return d.provider.CleanUp(domain, token, keyAuth)
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 10:03:31 +02:00
|
|
|
func getAuthorizer(config *Config) (autorest.Authorizer, error) {
|
|
|
|
if config.ClientID != "" && config.ClientSecret != "" && config.TenantID != "" {
|
2020-08-09 14:17:49 +02:00
|
|
|
credentialsConfig := auth.ClientCredentialsConfig{
|
|
|
|
ClientID: config.ClientID,
|
|
|
|
ClientSecret: config.ClientSecret,
|
|
|
|
TenantID: config.TenantID,
|
|
|
|
Resource: config.ResourceManagerEndpoint,
|
|
|
|
AADEndpoint: config.ActiveDirectoryEndpoint,
|
|
|
|
}
|
2018-10-23 10:03:31 +02:00
|
|
|
|
2020-04-17 18:54:59 +02:00
|
|
|
spToken, err := credentialsConfig.ServicePrincipalToken()
|
2018-10-23 10:03:31 +02:00
|
|
|
if err != nil {
|
2020-10-27 13:01:05 +02:00
|
|
|
return nil, fmt.Errorf("failed to get oauth token from client credentials: %w", err)
|
2018-10-23 10:03:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 18:54:59 +02:00
|
|
|
spToken.SetSender(config.HTTPClient)
|
2018-10-23 10:03:31 +02:00
|
|
|
|
2020-04-17 18:54:59 +02:00
|
|
|
return autorest.NewBearerAuthorizer(spToken), nil
|
|
|
|
}
|
2020-08-09 14:17:49 +02:00
|
|
|
|
2018-10-23 10:03:31 +02:00
|
|
|
return auth.NewAuthorizerFromEnvironment()
|
|
|
|
}
|
|
|
|
|
2023-07-29 12:59:24 +02:00
|
|
|
// Fetches metadata from environment or the instance metadata service.
|
2018-10-23 10:03:31 +02:00
|
|
|
// borrowed from https://github.com/Microsoft/azureimds/blob/master/imdssample.go
|
|
|
|
func getMetadata(config *Config, field string) (string, error) {
|
|
|
|
metadataEndpoint := config.MetadataEndpoint
|
2021-03-04 21:16:59 +02:00
|
|
|
if metadataEndpoint == "" {
|
2018-10-23 10:03:31 +02:00
|
|
|
metadataEndpoint = defaultMetadataEndpoint
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
endpoint, err := url.JoinPath(metadataEndpoint, "metadata", "instance", "compute", field)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
2018-10-23 10:03:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:51:55 +02:00
|
|
|
req.Header.Set("Metadata", "True")
|
2018-10-23 10:03:31 +02:00
|
|
|
|
|
|
|
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 {
|
2023-05-05 09:49:38 +02:00
|
|
|
return "", errutils.NewHTTPDoError(req, err)
|
2018-10-23 10:03:31 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2018-10-23 10:03:31 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return "", errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2018-10-23 10:03:31 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
return string(raw), nil
|
2018-10-23 10:03:31 +02:00
|
|
|
}
|