2016-03-17 20:59:15 +00:00
|
|
|
// Package dyn implements a DNS provider for solving the DNS-01 challenge
|
|
|
|
// using Dyn Managed DNS.
|
2016-03-18 02:30:21 +00:00
|
|
|
package dyn
|
2016-02-07 07:10:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2016-03-19 03:20:58 +00:00
|
|
|
"time"
|
2016-03-18 02:30:21 +00:00
|
|
|
|
|
|
|
"github.com/xenolf/lego/acme"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2016-02-07 07:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var dynBaseURL = "https://api.dynect.net/REST"
|
|
|
|
|
2016-03-19 20:14:23 +00:00
|
|
|
type dynResponse struct {
|
2016-02-07 07:10:36 +00:00
|
|
|
// One of 'success', 'failure', or 'incomplete'
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
|
|
|
// The structure containing the actual results of the request
|
|
|
|
Data json.RawMessage `json:"data"`
|
|
|
|
|
|
|
|
// The ID of the job that was created in response to a request.
|
2016-03-19 20:14:23 +00:00
|
|
|
JobID int `json:"job_id"`
|
2016-02-07 07:10:36 +00:00
|
|
|
|
|
|
|
// A list of zero or more messages
|
|
|
|
Messages json.RawMessage `json:"msgs"`
|
|
|
|
}
|
|
|
|
|
2016-03-18 02:30:21 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface that uses
|
2016-02-07 07:10:36 +00:00
|
|
|
// Dyn's Managed DNS API to manage TXT records for a domain.
|
2016-03-18 02:30:21 +00:00
|
|
|
type DNSProvider struct {
|
2016-02-07 07:10:36 +00:00
|
|
|
customerName string
|
|
|
|
userName string
|
|
|
|
password string
|
|
|
|
token string
|
|
|
|
}
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Dyn DNS.
|
|
|
|
// Credentials must be passed in the environment variables: DYN_CUSTOMER_NAME,
|
|
|
|
// DYN_USER_NAME and DYN_PASSWORD.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-06-11 15:32:50 +00:00
|
|
|
values, err := env.Get("DYN_CUSTOMER_NAME", "DYN_USER_NAME", "DYN_PASSWORD")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("DynDNS: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewDNSProviderCredentials(values["DYN_CUSTOMER_NAME"], values["DYN_USER_NAME"], values["DYN_PASSWORD"])
|
2016-03-17 20:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderCredentials uses the supplied credentials to return a
|
|
|
|
// DNSProvider instance configured for Dyn DNS.
|
|
|
|
func NewDNSProviderCredentials(customerName, userName, password string) (*DNSProvider, error) {
|
|
|
|
if customerName == "" || userName == "" || password == "" {
|
|
|
|
return nil, fmt.Errorf("DynDNS credentials missing")
|
|
|
|
}
|
|
|
|
|
2016-03-18 02:30:21 +00:00
|
|
|
return &DNSProvider{
|
2016-02-07 07:10:36 +00:00
|
|
|
customerName: customerName,
|
|
|
|
userName: userName,
|
|
|
|
password: password,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-03-19 20:14:23 +00:00
|
|
|
func (d *DNSProvider) sendRequest(method, resource string, payload interface{}) (*dynResponse, error) {
|
2016-02-07 07:10:36 +00:00
|
|
|
url := fmt.Sprintf("%s/%s", dynBaseURL, resource)
|
|
|
|
|
|
|
|
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("Auth-Token", d.token)
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
2016-03-19 03:20:58 +00:00
|
|
|
resp, err := client.Do(req)
|
2016-02-07 07:10:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-03-19 16:41:57 +00:00
|
|
|
if resp.StatusCode >= 500 {
|
2016-02-07 07:10:36 +00:00
|
|
|
return nil, fmt.Errorf("Dyn API request failed with HTTP status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2016-03-19 20:14:23 +00:00
|
|
|
var dynRes dynResponse
|
2016-02-07 07:10:36 +00:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&dynRes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-19 16:41:57 +00:00
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, fmt.Errorf("Dyn API request failed with HTTP status code %d: %s", resp.StatusCode, dynRes.Messages)
|
|
|
|
} else if resp.StatusCode == 307 {
|
|
|
|
// TODO add support for HTTP 307 response and long running jobs
|
|
|
|
return nil, fmt.Errorf("Dyn API request returned HTTP 307. This is currently unsupported")
|
|
|
|
}
|
|
|
|
|
2016-02-07 07:10:36 +00:00
|
|
|
if dynRes.Status == "failure" {
|
|
|
|
// TODO add better error handling
|
|
|
|
return nil, fmt.Errorf("Dyn API request failed: %s", dynRes.Messages)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dynRes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts a new Dyn API Session. Authenticates using customerName, userName,
|
|
|
|
// password and receives a token to be used in for subsequent requests.
|
2016-03-18 02:30:21 +00:00
|
|
|
func (d *DNSProvider) login() error {
|
2016-02-07 07:10:36 +00:00
|
|
|
type creds struct {
|
|
|
|
Customer string `json:"customer_name"`
|
|
|
|
User string `json:"user_name"`
|
|
|
|
Pass string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type session struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := &creds{Customer: d.customerName, User: d.userName, Pass: d.password}
|
|
|
|
dynRes, err := d.sendRequest("POST", "Session", payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var s session
|
|
|
|
err = json.Unmarshal(dynRes.Data, &s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.token = s.Token
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroys Dyn Session
|
2016-03-18 02:30:21 +00:00
|
|
|
func (d *DNSProvider) logout() error {
|
2016-02-07 07:10:36 +00:00
|
|
|
if len(d.token) == 0 {
|
|
|
|
// nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("%s/Session", dynBaseURL)
|
|
|
|
req, err := http.NewRequest("DELETE", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Auth-Token", d.token)
|
2016-03-19 03:20:58 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
2016-03-19 03:20:58 +00:00
|
|
|
resp, err := client.Do(req)
|
2016-02-07 07:10:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("Dyn API request failed to delete session with HTTP status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.token = ""
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters
|
2016-03-18 02:30:21 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2016-04-27 02:36:48 +00:00
|
|
|
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
2016-02-07 07:10:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-27 02:36:48 +00:00
|
|
|
err = d.login()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-07 07:10:36 +00:00
|
|
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"rdata": map[string]string{
|
|
|
|
"txtdata": value,
|
|
|
|
},
|
|
|
|
"ttl": strconv.Itoa(ttl),
|
|
|
|
}
|
|
|
|
|
2016-04-27 02:36:48 +00:00
|
|
|
resource := fmt.Sprintf("TXTRecord/%s/%s/", authZone, fqdn)
|
2016-02-07 07:10:36 +00:00
|
|
|
_, err = d.sendRequest("POST", resource, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-27 02:36:48 +00:00
|
|
|
err = d.publish(authZone, "Added TXT record for ACME dns-01 challenge using lego client")
|
2016-02-07 07:10:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
return d.logout()
|
2016-02-07 07:10:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:36:48 +00:00
|
|
|
func (d *DNSProvider) publish(zone, notes string) error {
|
2016-02-07 07:10:36 +00:00
|
|
|
type publish struct {
|
|
|
|
Publish bool `json:"publish"`
|
|
|
|
Notes string `json:"notes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
pub := &publish{Publish: true, Notes: notes}
|
2016-04-27 02:36:48 +00:00
|
|
|
resource := fmt.Sprintf("Zone/%s/", zone)
|
2016-02-07 07:10:36 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
_, err := d.sendRequest("PUT", resource, pub)
|
|
|
|
return err
|
2016-02-07 07:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
2016-03-18 02:30:21 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-04-27 02:36:48 +00:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
2016-02-07 07:10:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-27 02:36:48 +00:00
|
|
|
err = d.login()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-07 07:10:36 +00:00
|
|
|
|
2016-04-27 02:36:48 +00:00
|
|
|
resource := fmt.Sprintf("TXTRecord/%s/%s/", authZone, fqdn)
|
2016-02-07 07:10:36 +00:00
|
|
|
url := fmt.Sprintf("%s/%s", dynBaseURL, resource)
|
|
|
|
req, err := http.NewRequest("DELETE", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Auth-Token", d.token)
|
2016-03-19 03:20:58 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
2016-03-19 03:20:58 +00:00
|
|
|
resp, err := client.Do(req)
|
2016-02-07 07:10:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("Dyn API request failed to delete TXT record HTTP status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2016-04-27 02:36:48 +00:00
|
|
|
err = d.publish(authZone, "Removed TXT record for ACME dns-01 challenge using lego client")
|
2016-02-07 07:10:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
return d.logout()
|
2016-02-07 07:10:36 +00:00
|
|
|
}
|