2020-06-27 15:07:19 +02:00
|
|
|
// Package oraclecloud implements a DNS provider for solving the DNS-01 challenge using Oracle Cloud DNS.
|
2019-03-11 18:56:48 +02:00
|
|
|
package oraclecloud
|
2019-02-26 16:41:02 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2019-02-26 16:41:02 +02:00
|
|
|
"github.com/oracle/oci-go-sdk/common"
|
|
|
|
"github.com/oracle/oci-go-sdk/dns"
|
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "OCI_"
|
|
|
|
|
|
|
|
EnvCompartmentOCID = envNamespace + "COMPARTMENT_OCID"
|
|
|
|
envPrivKey = envNamespace + "PRIVKEY"
|
|
|
|
EnvPrivKeyFile = envPrivKey + "_FILE"
|
|
|
|
EnvPrivKeyPass = envPrivKey + "_PASS"
|
|
|
|
EnvTenancyOCID = envNamespace + "TENANCY_OCID"
|
|
|
|
EnvUserOCID = envNamespace + "USER_OCID"
|
|
|
|
EnvPubKeyFingerprint = envNamespace + "PUBKEY_FINGERPRINT"
|
|
|
|
EnvRegion = envNamespace + "REGION"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2019-02-26 16:41:02 +02:00
|
|
|
type Config struct {
|
|
|
|
CompartmentID string
|
|
|
|
OCIConfigProvider common.ConfigurationProvider
|
|
|
|
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.
|
2019-02-26 16:41:02 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-12 00:51:10 +02:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2019-02-26 16:41:02 +02:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-12 00:51:10 +02:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 60*time.Second),
|
2019-02-26 16:41:02 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2019-02-26 16:41:02 +02:00
|
|
|
type DNSProvider struct {
|
|
|
|
client *dns.DnsClient
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for OracleCloud.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-12 00:51:10 +02:00
|
|
|
values, err := env.Get(envPrivKey, EnvTenancyOCID, EnvUserOCID, EnvPubKeyFingerprint, EnvRegion, EnvCompartmentOCID)
|
2019-02-26 16:41:02 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("oraclecloud: %w", err)
|
2019-02-26 16:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
2020-03-12 00:51:10 +02:00
|
|
|
config.CompartmentID = values[EnvCompartmentOCID]
|
2019-02-26 16:41:02 +02:00
|
|
|
config.OCIConfigProvider = newConfigProvider(values)
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for OracleCloud.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("oraclecloud: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.CompartmentID == "" {
|
|
|
|
return nil, errors.New("oraclecloud: CompartmentID is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.OCIConfigProvider == nil {
|
|
|
|
return nil, errors.New("oraclecloud: OCIConfigProvider is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := dns.NewDnsClientWithConfigurationProvider(config.OCIConfigProvider)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("oraclecloud: %w", err)
|
2019-02-26 16:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.HTTPClient != nil {
|
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{client: &client, config: config}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2019-02-26 16:41:02 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2019-02-26 16:41:02 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
zoneNameOrID, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
|
|
|
if err != nil {
|
2024-02-11 15:37:09 +02:00
|
|
|
return fmt.Errorf("oraclecloud: could not find zone for domain %q: %w", domain, err)
|
2020-03-11 12:59:08 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 16:41:02 +02:00
|
|
|
// generate request to dns.PatchDomainRecordsRequest
|
|
|
|
recordOperation := dns.RecordOperation{
|
2023-03-07 10:39:05 +02:00
|
|
|
Domain: common.String(dns01.UnFqdn(info.EffectiveFQDN)),
|
|
|
|
Rdata: common.String(info.Value),
|
2019-02-26 16:41:02 +02:00
|
|
|
Rtype: common.String("TXT"),
|
2019-02-28 09:19:42 +02:00
|
|
|
Ttl: common.Int(d.config.TTL),
|
2019-02-26 16:41:02 +02:00
|
|
|
IsProtected: common.Bool(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
request := dns.PatchDomainRecordsRequest{
|
|
|
|
CompartmentId: common.String(d.config.CompartmentID),
|
2020-03-11 12:59:08 +02:00
|
|
|
ZoneNameOrId: common.String(zoneNameOrID),
|
2023-03-07 10:39:05 +02:00
|
|
|
Domain: common.String(dns01.UnFqdn(info.EffectiveFQDN)),
|
2019-02-26 16:41:02 +02:00
|
|
|
PatchDomainRecordsDetails: dns.PatchDomainRecordsDetails{
|
|
|
|
Items: []dns.RecordOperation{recordOperation},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
_, err = d.client.PatchDomainRecords(context.Background(), request)
|
2019-02-26 16:41:02 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("oraclecloud: %w", err)
|
2019-02-26 16:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2019-02-26 16:41:02 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2019-02-26 16:41:02 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
zoneNameOrID, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
|
|
|
if err != nil {
|
2024-02-11 15:37:09 +02:00
|
|
|
return fmt.Errorf("oraclecloud: could not find zone for domain %q: %w", domain, err)
|
2020-03-11 12:59:08 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 16:41:02 +02:00
|
|
|
// search to TXT record's hash to delete
|
|
|
|
getRequest := dns.GetDomainRecordsRequest{
|
2020-03-11 12:59:08 +02:00
|
|
|
ZoneNameOrId: common.String(zoneNameOrID),
|
2023-03-07 10:39:05 +02:00
|
|
|
Domain: common.String(dns01.UnFqdn(info.EffectiveFQDN)),
|
2019-02-26 16:41:02 +02:00
|
|
|
CompartmentId: common.String(d.config.CompartmentID),
|
|
|
|
Rtype: common.String("TXT"),
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
domainRecords, err := d.client.GetDomainRecords(ctx, getRequest)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("oraclecloud: %w", err)
|
2019-02-26 16:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if *domainRecords.OpcTotalItems == 0 {
|
2023-07-29 12:59:24 +02:00
|
|
|
return errors.New("oraclecloud: no record to clean up")
|
2019-02-26 16:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var deleteHash *string
|
|
|
|
for _, record := range domainRecords.RecordCollection.Items {
|
2023-03-07 10:39:05 +02:00
|
|
|
if record.Rdata != nil && *record.Rdata == `"`+info.Value+`"` {
|
2019-02-26 16:41:02 +02:00
|
|
|
deleteHash = record.RecordHash
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if deleteHash == nil {
|
2023-07-29 12:59:24 +02:00
|
|
|
return errors.New("oraclecloud: no record to clean up")
|
2019-02-26 16:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
recordOperation := dns.RecordOperation{
|
|
|
|
RecordHash: deleteHash,
|
|
|
|
Operation: dns.RecordOperationOperationRemove,
|
|
|
|
}
|
|
|
|
|
|
|
|
patchRequest := dns.PatchDomainRecordsRequest{
|
2020-03-11 12:59:08 +02:00
|
|
|
ZoneNameOrId: common.String(zoneNameOrID),
|
2023-03-07 10:39:05 +02:00
|
|
|
Domain: common.String(dns01.UnFqdn(info.EffectiveFQDN)),
|
2019-02-26 16:41:02 +02:00
|
|
|
PatchDomainRecordsDetails: dns.PatchDomainRecordsDetails{
|
|
|
|
Items: []dns.RecordOperation{recordOperation},
|
|
|
|
},
|
|
|
|
CompartmentId: common.String(d.config.CompartmentID),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.client.PatchDomainRecords(ctx, patchRequest)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("oraclecloud: %w", err)
|
2019-02-26 16:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-28 09:19:42 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|