2022-09-01 23:05:43 +02:00
|
|
|
// Package yandexcloud implements a DNS provider for solving the DNS-01 challenge using Yandex Cloud.
|
|
|
|
package yandexcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-02-09 22:55:43 +02:00
|
|
|
"slices"
|
2022-09-01 23:05:43 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
|
|
|
ycdns "github.com/yandex-cloud/go-genproto/yandex/cloud/dns/v1"
|
|
|
|
ycsdk "github.com/yandex-cloud/go-sdk"
|
|
|
|
"github.com/yandex-cloud/go-sdk/iamkey"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "YANDEX_CLOUD_"
|
|
|
|
|
|
|
|
EnvIamToken = envNamespace + "IAM_TOKEN"
|
|
|
|
EnvFolderID = envNamespace + "FOLDER_ID"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
|
|
|
type Config struct {
|
|
|
|
IamToken string
|
|
|
|
FolderID string
|
|
|
|
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2023-05-05 09:49:38 +02:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 60),
|
2022-09-01 23:05:43 +02:00
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
|
|
|
type DNSProvider struct {
|
|
|
|
client *ycsdk.SDK
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Yandex Cloud.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
values, err := env.Get(EnvIamToken, EnvFolderID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("yandexcloud: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.IamToken = values[EnvIamToken]
|
|
|
|
config.FolderID = values[EnvFolderID]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Yandex Cloud.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("yandexcloud: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.IamToken == "" {
|
2024-02-08 04:16:48 +02:00
|
|
|
return nil, errors.New("yandexcloud: some credentials information are missing IAM token")
|
2022-09-01 23:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.FolderID == "" {
|
2024-02-08 04:16:48 +02:00
|
|
|
return nil, errors.New("yandexcloud: some credentials information are missing folder id")
|
2022-09-01 23:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
creds, err := decodeCredentials(config.IamToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("yandexcloud: iam token is malformed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := ycsdk.Build(context.Background(), ycsdk.Config{Credentials: creds})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("yandexcloud: unable to build yandex cloud sdk")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{
|
|
|
|
client: client,
|
|
|
|
config: config,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
|
|
|
func (r *DNSProvider) Present(domain, _, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2022-09-01 23:05:43 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2022-09-01 23:05:43 +02:00
|
|
|
if err != nil {
|
2024-02-11 15:37:09 +02:00
|
|
|
return fmt.Errorf("yandexcloud: could not find zone for domain %q: %w", domain, err)
|
2022-09-01 23:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
zones, err := r.getZones(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("yandexcloud: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var zoneID string
|
|
|
|
|
|
|
|
for _, zone := range zones {
|
|
|
|
if zone.GetZone() == authZone {
|
|
|
|
zoneID = zone.GetId()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if zoneID == "" {
|
|
|
|
return fmt.Errorf("yandexcloud: cant find dns zone %s in yandex cloud", authZone)
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
2022-11-28 19:53:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("yandexcloud: %w", err)
|
|
|
|
}
|
2022-09-01 23:05:43 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
err = r.upsertRecordSetData(ctx, zoneID, subDomain, info.Value)
|
2022-09-01 23:05:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("yandexcloud: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (r *DNSProvider) CleanUp(domain, _, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2022-09-01 23:05:43 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2022-09-01 23:05:43 +02:00
|
|
|
if err != nil {
|
2024-02-11 15:37:09 +02:00
|
|
|
return fmt.Errorf("yandexcloud: could not find zone for domain %q: %w", domain, err)
|
2022-09-01 23:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
zones, err := r.getZones(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("yandexcloud: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var zoneID string
|
|
|
|
|
|
|
|
for _, zone := range zones {
|
|
|
|
if zone.GetZone() == authZone {
|
|
|
|
zoneID = zone.GetId()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if zoneID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
2022-11-28 19:53:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("yandexcloud: %w", err)
|
|
|
|
}
|
2022-09-01 23:05:43 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
err = r.removeRecordSetData(ctx, zoneID, subDomain, info.Value)
|
2022-09-01 23:05:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("yandexcloud: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
|
|
|
func (r *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return r.config.PropagationTimeout, r.config.PollingInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
// getZones retrieves available zones from yandex cloud.
|
|
|
|
func (r *DNSProvider) getZones(ctx context.Context) ([]*ycdns.DnsZone, error) {
|
|
|
|
list := &ycdns.ListDnsZonesRequest{
|
|
|
|
FolderId: r.config.FolderID,
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := r.client.DNS().DnsZone().List(ctx, list)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("unable to fetch dns zones")
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:08:50 +02:00
|
|
|
return response.GetDnsZones(), nil
|
2022-09-01 23:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DNSProvider) upsertRecordSetData(ctx context.Context, zoneID, name, value string) error {
|
|
|
|
get := &ycdns.GetDnsZoneRecordSetRequest{
|
|
|
|
DnsZoneId: zoneID,
|
|
|
|
Name: name,
|
|
|
|
Type: "TXT",
|
|
|
|
}
|
|
|
|
|
|
|
|
exist, err := r.client.DNS().DnsZone().GetRecordSet(ctx, get)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "RecordSet not found") {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
record := &ycdns.RecordSet{
|
|
|
|
Name: name,
|
|
|
|
Type: "TXT",
|
|
|
|
Ttl: int64(r.config.TTL),
|
|
|
|
Data: []string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
var deletions []*ycdns.RecordSet
|
|
|
|
if exist != nil {
|
2023-10-31 15:08:50 +02:00
|
|
|
record.SetData(append(record.GetData(), exist.GetData()...))
|
2022-09-01 23:05:43 +02:00
|
|
|
deletions = append(deletions, exist)
|
|
|
|
}
|
|
|
|
|
|
|
|
appended := appendRecordSetData(record, value)
|
|
|
|
if !appended {
|
|
|
|
// The value already present in RecordSet, nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
update := &ycdns.UpdateRecordSetsRequest{
|
|
|
|
DnsZoneId: zoneID,
|
|
|
|
Deletions: deletions,
|
|
|
|
Additions: []*ycdns.RecordSet{record},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.client.DNS().DnsZone().UpdateRecordSets(ctx, update)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DNSProvider) removeRecordSetData(ctx context.Context, zoneID, name, value string) error {
|
|
|
|
get := &ycdns.GetDnsZoneRecordSetRequest{
|
|
|
|
DnsZoneId: zoneID,
|
|
|
|
Name: name,
|
|
|
|
Type: "TXT",
|
|
|
|
}
|
|
|
|
|
|
|
|
previousRecord, err := r.client.DNS().DnsZone().GetRecordSet(ctx, get)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "RecordSet not found") {
|
|
|
|
// RecordSet is not present, nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var additions []*ycdns.RecordSet
|
|
|
|
|
2023-10-31 15:08:50 +02:00
|
|
|
if len(previousRecord.GetData()) > 1 {
|
2022-09-01 23:05:43 +02:00
|
|
|
// RecordSet is not empty we should update it
|
|
|
|
record := &ycdns.RecordSet{
|
|
|
|
Name: name,
|
|
|
|
Type: "TXT",
|
|
|
|
Ttl: int64(r.config.TTL),
|
|
|
|
Data: []string{},
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:08:50 +02:00
|
|
|
for _, data := range previousRecord.GetData() {
|
2022-09-01 23:05:43 +02:00
|
|
|
if data != value {
|
2023-10-31 15:08:50 +02:00
|
|
|
record.SetData(append(record.GetData(), data))
|
2022-09-01 23:05:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
additions = append(additions, record)
|
|
|
|
}
|
|
|
|
|
|
|
|
update := &ycdns.UpdateRecordSetsRequest{
|
|
|
|
DnsZoneId: zoneID,
|
|
|
|
Deletions: []*ycdns.RecordSet{previousRecord},
|
|
|
|
Additions: additions,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.client.DNS().DnsZone().UpdateRecordSets(ctx, update)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeCredentials converts base64 encoded json of iam token to struct.
|
|
|
|
func decodeCredentials(accountB64 string) (ycsdk.Credentials, error) {
|
|
|
|
account, err := base64.StdEncoding.DecodeString(accountB64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key := &iamkey.Key{}
|
|
|
|
err = json.Unmarshal(account, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ycsdk.ServiceAccountKey(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendRecordSetData(record *ycdns.RecordSet, value string) bool {
|
2024-02-09 22:55:43 +02:00
|
|
|
if slices.Contains(record.GetData(), value) {
|
|
|
|
return false
|
2022-09-01 23:05:43 +02:00
|
|
|
}
|
|
|
|
|
2023-10-31 15:08:50 +02:00
|
|
|
record.SetData(append(record.GetData(), value))
|
2022-09-01 23:05:43 +02:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|