2018-09-08 10:52:36 +00:00
|
|
|
// Package iij implements a DNS provider for solving the DNS-01 challenge using IIJ DNS.
|
2019-03-11 16:56:48 +00:00
|
|
|
package iij
|
2018-09-08 10:52:36 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-27 18:14:46 +00:00
|
|
|
"errors"
|
2018-09-08 10:52:36 +00:00
|
|
|
"fmt"
|
2018-09-15 17:07:24 +00:00
|
|
|
"strconv"
|
2018-09-08 10:52:36 +00:00
|
|
|
"strings"
|
|
|
|
"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"
|
2018-09-08 10:52:36 +00:00
|
|
|
"github.com/iij/doapi"
|
|
|
|
"github.com/iij/doapi/protocol"
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "IIJ_"
|
|
|
|
|
|
|
|
EnvAPIAccessKey = envNamespace + "API_ACCESS_KEY"
|
|
|
|
EnvAPISecretKey = envNamespace + "API_SECRET_KEY"
|
|
|
|
EnvDoServiceCode = envNamespace + "DO_SERVICE_CODE"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-08 10:52:36 +00:00
|
|
|
type Config struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
AccessKey string
|
|
|
|
SecretKey string
|
|
|
|
DoServiceCode string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 300),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 4*time.Second),
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2018-09-08 10:52:36 +00:00
|
|
|
type DNSProvider struct {
|
|
|
|
api *doapi.API
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for IIJ DNS.
|
2018-09-08 10:52:36 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvAPIAccessKey, EnvAPISecretKey, EnvDoServiceCode)
|
2018-09-08 10:52:36 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("iij: %w", err)
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.AccessKey = values[EnvAPIAccessKey]
|
|
|
|
config.SecretKey = values[EnvAPISecretKey]
|
|
|
|
config.DoServiceCode = values[EnvDoServiceCode]
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderConfig takes a given config
|
2020-05-08 17:35:25 +00:00
|
|
|
// and returns a custom configured DNSProvider instance.
|
2018-09-08 10:52:36 +00:00
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.SecretKey == "" || config.AccessKey == "" || config.DoServiceCode == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("iij: credentials missing")
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 10:52:36 +00:00
|
|
|
return &DNSProvider{
|
|
|
|
api: doapi.NewAPI(config.AccessKey, config.SecretKey),
|
|
|
|
config: config,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Present creates a TXT record using the specified parameters.
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
// TODO(ldez) replace domain by FQDN to follow CNAME.
|
|
|
|
err := d.addTxtRecord(domain, info.Value)
|
2018-09-20 21:18:13 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("iij: %w", err)
|
2018-09-20 21:18:13 +00:00
|
|
|
}
|
|
|
|
return nil
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2022-11-25 17:12:21 +00:00
|
|
|
// TODO(ldez) replace domain by FQDN to follow CNAME.
|
2023-03-07 08:39:05 +00:00
|
|
|
err := d.deleteTxtRecord(domain, info.Value)
|
2018-09-20 21:18:13 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("iij: %w", err)
|
2018-09-20 21:18:13 +00:00
|
|
|
}
|
|
|
|
return nil
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) addTxtRecord(domain, value string) error {
|
|
|
|
zones, err := d.listZones()
|
2018-09-08 10:52:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:12:21 +00:00
|
|
|
// TODO(ldez) replace domain by FQDN to follow CNAME.
|
2018-09-08 10:52:36 +00:00
|
|
|
owner, zone, err := splitDomain(domain, zones)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
request := protocol.RecordAdd{
|
2018-09-15 17:07:24 +00:00
|
|
|
DoServiceCode: d.config.DoServiceCode,
|
2018-09-08 10:52:36 +00:00
|
|
|
ZoneName: zone,
|
|
|
|
Owner: owner,
|
2018-09-15 17:07:24 +00:00
|
|
|
TTL: strconv.Itoa(d.config.TTL),
|
2018-09-08 10:52:36 +00:00
|
|
|
RecordType: "TXT",
|
|
|
|
RData: value,
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &protocol.RecordAddResponse{}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if err := doapi.Call(*d.api, request, response); err != nil {
|
2018-09-08 10:52:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
return d.commit()
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) deleteTxtRecord(domain, value string) error {
|
|
|
|
zones, err := d.listZones()
|
2018-09-08 10:52:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
owner, zone, err := splitDomain(domain, zones)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
id, err := d.findTxtRecord(owner, zone, value)
|
2018-09-08 10:52:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
request := protocol.RecordDelete{
|
2018-09-15 17:07:24 +00:00
|
|
|
DoServiceCode: d.config.DoServiceCode,
|
2018-09-08 10:52:36 +00:00
|
|
|
ZoneName: zone,
|
|
|
|
RecordID: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &protocol.RecordDeleteResponse{}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if err := doapi.Call(*d.api, request, response); err != nil {
|
2018-09-08 10:52:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
return d.commit()
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) commit() error {
|
2018-09-08 10:52:36 +00:00
|
|
|
request := protocol.Commit{
|
2018-09-15 17:07:24 +00:00
|
|
|
DoServiceCode: d.config.DoServiceCode,
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response := &protocol.CommitResponse{}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
return doapi.Call(*d.api, request, response)
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) findTxtRecord(owner, zone, value string) (string, error) {
|
2018-09-08 10:52:36 +00:00
|
|
|
request := protocol.RecordListGet{
|
2018-09-15 17:07:24 +00:00
|
|
|
DoServiceCode: d.config.DoServiceCode,
|
2018-09-08 10:52:36 +00:00
|
|
|
ZoneName: zone,
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &protocol.RecordListGetResponse{}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if err := doapi.Call(*d.api, request, response); err != nil {
|
2018-09-08 10:52:36 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var id string
|
|
|
|
|
|
|
|
for _, record := range response.RecordList {
|
|
|
|
if record.Owner == owner && record.RecordType == "TXT" && record.RData == "\""+value+"\"" {
|
|
|
|
id = record.Id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if id == "" {
|
|
|
|
return "", fmt.Errorf("%s record in %s not found", owner, zone)
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) listZones() ([]string, error) {
|
2018-09-08 10:52:36 +00:00
|
|
|
request := protocol.ZoneListGet{
|
2018-09-15 17:07:24 +00:00
|
|
|
DoServiceCode: d.config.DoServiceCode,
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response := &protocol.ZoneListGetResponse{}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if err := doapi.Call(*d.api, request, response); err != nil {
|
2018-09-08 10:52:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.ZoneList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitDomain(domain string, zones []string) (string, string, error) {
|
|
|
|
parts := strings.Split(strings.Trim(domain, "."), ".")
|
|
|
|
|
|
|
|
var owner string
|
|
|
|
var zone string
|
|
|
|
|
|
|
|
for i := 0; i < len(parts)-1; i++ {
|
|
|
|
zone = strings.Join(parts[i:], ".")
|
|
|
|
if zoneContains(zone, zones) {
|
|
|
|
baseOwner := strings.Join(parts[0:i], ".")
|
|
|
|
if len(baseOwner) > 0 {
|
|
|
|
baseOwner = "." + baseOwner
|
|
|
|
}
|
|
|
|
owner = "_acme-challenge" + baseOwner
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if owner == "" {
|
2018-09-08 10:52:36 +00:00
|
|
|
return "", "", fmt.Errorf("%s not found", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
return owner, zone, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func zoneContains(zone string, zones []string) bool {
|
|
|
|
for _, z := range zones {
|
|
|
|
if zone == z {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|