2017-10-31 09:42:03 +00:00
|
|
|
// Package otc implements a DNS provider for solving the DNS-01 challenge
|
|
|
|
// using Open Telekom Cloud Managed DNS.
|
|
|
|
package otc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-09-17 13:16:03 +00:00
|
|
|
"errors"
|
2017-10-31 09:42:03 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2018-09-17 13:16:03 +00:00
|
|
|
"net"
|
2017-10-31 09:42:03 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2018-05-31 07:30:04 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2018-07-03 10:44:04 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2017-10-31 09:42:03 +00:00
|
|
|
)
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
const defaultIdentityEndpoint = "https://iam.eu-de.otc.t-systems.com:443/v3/auth/tokens"
|
|
|
|
|
|
|
|
// minTTL 300 is otc minimum value for ttl
|
|
|
|
const minTTL = 300
|
|
|
|
|
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
IdentityEndpoint string
|
|
|
|
DomainName string
|
|
|
|
ProjectName string
|
|
|
|
UserName string
|
|
|
|
Password string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
IdentityEndpoint: env.GetOrDefaultString("OTC_IDENTITY_ENDPOINT", defaultIdentityEndpoint),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("OTC_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("OTC_POLLING_INTERVAL", acme.DefaultPollingInterval),
|
|
|
|
TTL: env.GetOrDefaultInt("OTC_TTL", minTTL),
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond("OTC_HTTP_TIMEOUT", 10*time.Second),
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).DialContext,
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
|
|
|
|
// Workaround for keep alive bug in otc api
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:30:04 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface that uses
|
2017-10-31 09:42:03 +00:00
|
|
|
// OTC's Managed DNS API to manage TXT records for a domain.
|
|
|
|
type DNSProvider struct {
|
2018-09-17 13:16:03 +00:00
|
|
|
config *Config
|
|
|
|
baseURL string
|
|
|
|
token string
|
2017-10-31 09:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for OTC DNS.
|
|
|
|
// Credentials must be passed in the environment variables: OTC_USER_NAME,
|
|
|
|
// OTC_DOMAIN_NAME, OTC_PASSWORD OTC_PROJECT_NAME and OTC_IDENTITY_ENDPOINT.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-07-03 10:44:04 +00:00
|
|
|
values, err := env.Get("OTC_DOMAIN_NAME", "OTC_USER_NAME", "OTC_PASSWORD", "OTC_PROJECT_NAME")
|
|
|
|
if err != nil {
|
2018-09-17 13:16:03 +00:00
|
|
|
return nil, fmt.Errorf("otc: %v", err)
|
2018-07-03 10:44:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.DomainName = values["OTC_DOMAIN_NAME"]
|
|
|
|
config.UserName = values["OTC_USER_NAME"]
|
|
|
|
config.Password = values["OTC_PASSWORD"]
|
|
|
|
config.ProjectName = values["OTC_PROJECT_NAME"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2017-10-31 09:42:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for OTC DNS.
|
|
|
|
// Deprecated
|
2017-10-31 09:42:03 +00:00
|
|
|
func NewDNSProviderCredentials(domainName, userName, password, projectName, identityEndpoint string) (*DNSProvider, error) {
|
2018-09-17 13:16:03 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.IdentityEndpoint = identityEndpoint
|
|
|
|
config.DomainName = domainName
|
|
|
|
config.UserName = userName
|
|
|
|
config.Password = password
|
|
|
|
config.ProjectName = projectName
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for OTC DNS.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("otc: the configuration of the DNS provider is nil")
|
2017-10-31 09:42:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
if config.DomainName == "" || config.UserName == "" || config.Password == "" || config.ProjectName == "" {
|
|
|
|
return nil, fmt.Errorf("otc: credentials missing")
|
2017-10-31 09:42:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 14:28:04 +00:00
|
|
|
if config.TTL < minTTL {
|
|
|
|
return nil, fmt.Errorf("otc: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
if config.IdentityEndpoint == "" {
|
|
|
|
config.IdentityEndpoint = defaultIdentityEndpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{config: config}, nil
|
2017-10-31 09:42:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// Present creates a TXT record using the specified parameters
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("otc: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.login()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("otc: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
zoneID, err := d.getZoneID(authZone)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("otc: unable to get zone: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resource := fmt.Sprintf("zones/%s/recordsets", zoneID)
|
|
|
|
|
|
|
|
r1 := &recordset{
|
|
|
|
Name: fqdn,
|
|
|
|
Description: "Added TXT record for ACME dns-01 challenge using lego client",
|
|
|
|
Type: "TXT",
|
|
|
|
TTL: d.config.TTL,
|
|
|
|
Records: []string{fmt.Sprintf("\"%s\"", value)},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.sendRequest(http.MethodPost, resource, r1)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("otc: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("otc: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.login()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("otc: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
zoneID, err := d.getZoneID(authZone)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("otc: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
recordID, err := d.getRecordSetID(zoneID, fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("otc: unable go get record %s for zone %s: %s", fqdn, domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.deleteRecordSet(zoneID, recordID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("otc: %v", 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 (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendRequest send request
|
|
|
|
func (d *DNSProvider) sendRequest(method, resource string, payload interface{}) (io.Reader, error) {
|
|
|
|
url := fmt.Sprintf("%s/%s", d.baseURL, resource)
|
2017-10-31 09:42:03 +00:00
|
|
|
|
|
|
|
body, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, url, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
if len(d.token) > 0 {
|
|
|
|
req.Header.Set("X-Auth-Token", d.token)
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
2017-10-31 09:42:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, fmt.Errorf("OTC API request %s failed with HTTP status code %d", url, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
body1, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes.NewReader(body1), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) loginRequest() error {
|
|
|
|
userResp := userResponse{
|
2018-09-17 13:16:03 +00:00
|
|
|
Name: d.config.UserName,
|
|
|
|
Password: d.config.Password,
|
2017-10-31 09:42:03 +00:00
|
|
|
Domain: nameResponse{
|
2018-09-17 13:16:03 +00:00
|
|
|
Name: d.config.DomainName,
|
2017-10-31 09:42:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
loginResp := loginResponse{
|
|
|
|
Auth: authResponse{
|
|
|
|
Identity: identityResponse{
|
|
|
|
Methods: []string{"password"},
|
|
|
|
Password: passwordResponse{
|
|
|
|
User: userResp,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Scope: scopeResponse{
|
|
|
|
Project: nameResponse{
|
2018-09-17 13:16:03 +00:00
|
|
|
Name: d.config.ProjectName,
|
2017-10-31 09:42:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(loginResp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-17 13:16:03 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, d.config.IdentityEndpoint, bytes.NewReader(body))
|
2017-10-31 09:42:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
client := &http.Client{Timeout: d.config.HTTPClient.Timeout}
|
2017-10-31 09:42:03 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return fmt.Errorf("OTC API request failed with HTTP status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.token = resp.Header.Get("X-Subject-Token")
|
|
|
|
|
|
|
|
if d.token == "" {
|
|
|
|
return fmt.Errorf("unable to get auth token")
|
|
|
|
}
|
|
|
|
|
|
|
|
var endpointResp endpointResponse
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&endpointResp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range endpointResp.Token.Catalog {
|
|
|
|
if v.Type == "dns" {
|
|
|
|
for _, endpoint := range v.Endpoints {
|
2018-09-17 13:16:03 +00:00
|
|
|
d.baseURL = fmt.Sprintf("%s/v2", endpoint.URL)
|
2017-10-31 09:42:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
if d.baseURL == "" {
|
2017-10-31 09:42:03 +00:00
|
|
|
return fmt.Errorf("unable to get dns endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts a new OTC API Session. Authenticates using userName, password
|
|
|
|
// and receives a token to be used in for subsequent requests.
|
|
|
|
func (d *DNSProvider) login() error {
|
2018-05-31 07:30:04 +00:00
|
|
|
return d.loginRequest()
|
2017-10-31 09:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) getZoneID(zone string) (string, error) {
|
|
|
|
resource := fmt.Sprintf("zones?name=%s", zone)
|
2018-09-17 13:16:03 +00:00
|
|
|
resp, err := d.sendRequest(http.MethodGet, resource, nil)
|
2017-10-31 09:42:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var zonesRes zonesResponse
|
|
|
|
err = json.NewDecoder(resp).Decode(&zonesRes)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(zonesRes.Zones) < 1 {
|
|
|
|
return "", fmt.Errorf("zone %s not found", zone)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(zonesRes.Zones) > 1 {
|
|
|
|
return "", fmt.Errorf("to many zones found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if zonesRes.Zones[0].ID == "" {
|
|
|
|
return "", fmt.Errorf("id not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return zonesRes.Zones[0].ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) getRecordSetID(zoneID string, fqdn string) (string, error) {
|
|
|
|
resource := fmt.Sprintf("zones/%s/recordsets?type=TXT&name=%s", zoneID, fqdn)
|
2018-09-17 13:16:03 +00:00
|
|
|
resp, err := d.sendRequest(http.MethodGet, resource, nil)
|
2017-10-31 09:42:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var recordSetsRes recordSetsResponse
|
|
|
|
err = json.NewDecoder(resp).Decode(&recordSetsRes)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(recordSetsRes.RecordSets) < 1 {
|
|
|
|
return "", fmt.Errorf("record not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(recordSetsRes.RecordSets) > 1 {
|
|
|
|
return "", fmt.Errorf("to many records found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if recordSetsRes.RecordSets[0].ID == "" {
|
|
|
|
return "", fmt.Errorf("id not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return recordSetsRes.RecordSets[0].ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) deleteRecordSet(zoneID, recordID string) error {
|
|
|
|
resource := fmt.Sprintf("zones/%s/recordsets/%s", zoneID, recordID)
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
_, err := d.sendRequest(http.MethodDelete, resource, nil)
|
2018-05-31 07:30:04 +00:00
|
|
|
return err
|
2017-10-31 09:42:03 +00:00
|
|
|
}
|