2018-03-31 14:33:48 +00:00
|
|
|
// Package glesys implements a DNS provider for solving the DNS-01
|
|
|
|
// challenge using GleSYS api.
|
|
|
|
package glesys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
2018-03-31 14:33:48 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/xenolf/lego/acme"
|
2018-05-30 17:53:04 +00:00
|
|
|
"github.com/xenolf/lego/log"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2018-03-31 14:33:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GleSYS API reference: https://github.com/GleSYS/API/wiki/API-Documentation
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
const (
|
|
|
|
// defaultBaseURL is the GleSYS API endpoint used by Present and CleanUp.
|
|
|
|
defaultBaseURL = "https://api.glesys.com/domain"
|
|
|
|
minTTL = 60
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
APIUser string
|
|
|
|
APIKey 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{
|
|
|
|
TTL: env.GetOrDefaultInt("GLESYS_TTL", minTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("GLESYS_PROPAGATION_TIMEOUT", 20*time.Minute),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("GLESYS_POLLING_INTERVAL", 20*time.Second),
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond("GLESYS_HTTP_TIMEOUT", 10*time.Second),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-03-31 14:33:48 +00:00
|
|
|
|
|
|
|
// DNSProvider is an implementation of the
|
|
|
|
// acme.ChallengeProviderTimeout interface that uses GleSYS
|
|
|
|
// API to manage TXT records for a domain.
|
|
|
|
type DNSProvider struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2018-03-31 14:33:48 +00:00
|
|
|
activeRecords map[string]int
|
|
|
|
inProgressMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for GleSYS.
|
2018-09-15 17:07:24 +00:00
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// GLESYS_API_USER and GLESYS_API_KEY.
|
2018-03-31 14:33:48 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-06-11 15:32:50 +00:00
|
|
|
values, err := env.Get("GLESYS_API_USER", "GLESYS_API_KEY")
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("glesys: %v", err)
|
2018-06-11 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIUser = values["GLESYS_API_USER"]
|
|
|
|
config.APIKey = values["GLESYS_API_KEY"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2018-03-31 14:33:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for GleSYS.
|
|
|
|
// Deprecated
|
2018-03-31 14:33:48 +00:00
|
|
|
func NewDNSProviderCredentials(apiUser string, apiKey string) (*DNSProvider, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIUser = apiUser
|
|
|
|
config.APIKey = apiKey
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for GleSYS.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("glesys: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.APIUser == "" || config.APIKey == "" {
|
|
|
|
return nil, fmt.Errorf("glesys: incomplete credentials provided")
|
2018-03-31 14:33:48 +00:00
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
|
2018-03-31 14:33:48 +00:00
|
|
|
return &DNSProvider{
|
|
|
|
activeRecords: make(map[string]int),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-09-15 17:07:24 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
|
|
|
if d.config.TTL < minTTL {
|
|
|
|
d.config.TTL = minTTL // 60 is GleSYS minimum value for ttl
|
2018-03-31 14:33:48 +00:00
|
|
|
}
|
|
|
|
// find authZone
|
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("glesys: findZoneByFqdn failure: %v", err)
|
2018-03-31 14:33:48 +00:00
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-03-31 14:33:48 +00:00
|
|
|
// determine name of TXT record
|
|
|
|
if !strings.HasSuffix(
|
|
|
|
strings.ToLower(fqdn), strings.ToLower("."+authZone)) {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("glesys: unexpected authZone %s for fqdn %s", authZone, fqdn)
|
2018-03-31 14:33:48 +00:00
|
|
|
}
|
|
|
|
name := fqdn[:len(fqdn)-len("."+authZone)]
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-03-31 14:33:48 +00:00
|
|
|
// acquire lock and check there is not a challenge already in
|
|
|
|
// progress for this value of authZone
|
|
|
|
d.inProgressMu.Lock()
|
|
|
|
defer d.inProgressMu.Unlock()
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-03-31 14:33:48 +00:00
|
|
|
// add TXT record into authZone
|
2018-09-15 17:07:24 +00:00
|
|
|
recordID, err := d.addTXTRecord(domain, acme.UnFqdn(authZone), name, value, d.config.TTL)
|
2018-03-31 14:33:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-03-31 14:33:48 +00:00
|
|
|
// save data necessary for CleanUp
|
2018-05-30 17:53:04 +00:00
|
|
|
d.activeRecords[fqdn] = recordID
|
2018-03-31 14:33:48 +00:00
|
|
|
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)
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-03-31 14:33:48 +00:00
|
|
|
// acquire lock and retrieve authZone
|
|
|
|
d.inProgressMu.Lock()
|
|
|
|
defer d.inProgressMu.Unlock()
|
|
|
|
if _, ok := d.activeRecords[fqdn]; !ok {
|
|
|
|
// if there is no cleanup information then just return
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
|
|
|
|
recordID := d.activeRecords[fqdn]
|
2018-03-31 14:33:48 +00:00
|
|
|
delete(d.activeRecords, fqdn)
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-03-31 14:33:48 +00:00
|
|
|
// delete TXT record from authZone
|
2018-05-30 17:53:04 +00:00
|
|
|
return d.deleteTXTRecord(domain, recordID)
|
2018-03-31 14:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns the values (20*time.Minute, 20*time.Second) which
|
|
|
|
// are used by the acme package as timeout and check interval values
|
|
|
|
// when checking for DNS record propagation with GleSYS.
|
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-09-15 17:07:24 +00:00
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2018-03-31 14:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) sendRequest(method string, resource string, payload interface{}) (*responseStruct, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
url := fmt.Sprintf("%s/%s", defaultBaseURL, resource)
|
2018-03-31 14:33:48 +00:00
|
|
|
|
|
|
|
body, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-03-31 14:33:48 +00:00
|
|
|
req, err := http.NewRequest(method, url, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-03-31 14:33:48 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2018-09-15 17:07:24 +00:00
|
|
|
req.SetBasicAuth(d.config.APIUser, d.config.APIKey)
|
2018-03-31 14:33:48 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
2018-03-31 14:33:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode >= 400 {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("request failed with HTTP status code %d", resp.StatusCode)
|
2018-03-31 14:33:48 +00:00
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-03-31 14:33:48 +00:00
|
|
|
var response responseStruct
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
|
|
|
|
|
|
return &response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// functions to perform API actions
|
|
|
|
|
|
|
|
func (d *DNSProvider) addTXTRecord(fqdn string, domain string, name string, value string, ttl int) (int, error) {
|
2018-06-21 17:06:16 +00:00
|
|
|
response, err := d.sendRequest(http.MethodPost, "addrecord", addRecordRequest{
|
|
|
|
DomainName: domain,
|
2018-05-30 17:53:04 +00:00
|
|
|
Host: name,
|
|
|
|
Type: "TXT",
|
|
|
|
Data: value,
|
|
|
|
TTL: ttl,
|
2018-03-31 14:33:48 +00:00
|
|
|
})
|
2018-06-21 17:06:16 +00:00
|
|
|
|
|
|
|
if response != nil && response.Response.Status.Code == http.StatusOK {
|
2018-09-15 17:07:24 +00:00
|
|
|
log.Infof("[%s]: Successfully created record id %d", fqdn, response.Response.Record.RecordID)
|
2018-06-21 17:06:16 +00:00
|
|
|
return response.Response.Record.RecordID, nil
|
2018-03-31 14:33:48 +00:00
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) deleteTXTRecord(fqdn string, recordid int) error {
|
2018-06-21 17:06:16 +00:00
|
|
|
response, err := d.sendRequest(http.MethodPost, "deleterecord", deleteRecordRequest{
|
|
|
|
RecordID: recordid,
|
2018-03-31 14:33:48 +00:00
|
|
|
})
|
|
|
|
if response != nil && response.Response.Status.Code == 200 {
|
2018-09-15 17:07:24 +00:00
|
|
|
log.Infof("[%s]: Successfully deleted record id %d", fqdn, recordid)
|
2018-03-31 14:33:48 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|