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