2019-03-11 16:56:48 +00:00
|
|
|
package rackspace
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/challenge/dns01"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// APIKeyCredentials API credential.
|
2018-09-15 17:07:24 +00:00
|
|
|
type APIKeyCredentials struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
APIKey string `json:"apiKey"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Auth auth credentials.
|
2018-09-15 17:07:24 +00:00
|
|
|
type Auth struct {
|
|
|
|
APIKeyCredentials `json:"RAX-KSKEY:apiKeyCredentials"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// AuthData Auth data.
|
2018-09-15 17:07:24 +00:00
|
|
|
type AuthData struct {
|
|
|
|
Auth `json:"auth"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Identity Identity.
|
2018-09-15 17:07:24 +00:00
|
|
|
type Identity struct {
|
2018-10-12 17:29:18 +00:00
|
|
|
Access Access `json:"access"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Access Access.
|
2018-10-12 17:29:18 +00:00
|
|
|
type Access struct {
|
|
|
|
ServiceCatalog []ServiceCatalog `json:"serviceCatalog"`
|
|
|
|
Token Token `json:"token"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Token Token.
|
2018-10-12 17:29:18 +00:00
|
|
|
type Token struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// ServiceCatalog ServiceCatalog.
|
2018-10-12 17:29:18 +00:00
|
|
|
type ServiceCatalog struct {
|
|
|
|
Endpoints []Endpoint `json:"endpoints"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Endpoint Endpoint.
|
2018-10-12 17:29:18 +00:00
|
|
|
type Endpoint struct {
|
|
|
|
PublicURL string `json:"publicURL"`
|
|
|
|
TenantID string `json:"tenantId"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// ZoneSearchResponse represents the response when querying Rackspace DNS zones.
|
2018-10-12 17:29:18 +00:00
|
|
|
type ZoneSearchResponse struct {
|
|
|
|
TotalEntries int `json:"totalEntries"`
|
|
|
|
HostedZones []HostedZone `json:"domains"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// HostedZone HostedZone.
|
2018-10-12 17:29:18 +00:00
|
|
|
type HostedZone struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Records is the list of records sent/received from the DNS API.
|
2018-09-15 17:07:24 +00:00
|
|
|
type Records struct {
|
|
|
|
Record []Record `json:"records"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Record represents a Rackspace DNS record.
|
2018-09-15 17:07:24 +00:00
|
|
|
type Record struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
// getHostedZoneID performs a lookup to get the DNS zone which needs
|
2020-05-08 17:35:25 +00:00
|
|
|
// modifying for a given FQDN.
|
2018-12-06 21:50:17 +00:00
|
|
|
func (d *DNSProvider) getHostedZoneID(fqdn string) (int, error) {
|
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := d.makeRequest(http.MethodGet, fmt.Sprintf("/domains?name=%s", dns01.UnFqdn(authZone)), nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var zoneSearchResponse ZoneSearchResponse
|
|
|
|
err = json.Unmarshal(result, &zoneSearchResponse)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If nothing was returned, or for whatever reason more than 1 was returned (the search uses exact match, so should not occur)
|
|
|
|
if zoneSearchResponse.TotalEntries != 1 {
|
|
|
|
return 0, fmt.Errorf("found %d zones for %s in Rackspace for domain %s", zoneSearchResponse.TotalEntries, authZone, fqdn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return zoneSearchResponse.HostedZones[0].ID, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// findTxtRecord searches a DNS zone for a TXT record with a specific name.
|
2018-12-06 21:50:17 +00:00
|
|
|
func (d *DNSProvider) findTxtRecord(fqdn string, zoneID int) (*Record, error) {
|
|
|
|
result, err := d.makeRequest(http.MethodGet, fmt.Sprintf("/domains/%d/records?type=TXT&name=%s", zoneID, dns01.UnFqdn(fqdn)), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var records Records
|
|
|
|
err = json.Unmarshal(result, &records)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(records.Record) {
|
|
|
|
case 1:
|
|
|
|
case 0:
|
|
|
|
return nil, fmt.Errorf("no TXT record found for %s", fqdn)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("more than 1 TXT record found for %s", fqdn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &records.Record[0], nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// makeRequest is a wrapper function used for making DNS API requests.
|
2018-12-06 21:50:17 +00:00
|
|
|
func (d *DNSProvider) makeRequest(method, uri string, body io.Reader) (json.RawMessage, error) {
|
|
|
|
url := d.cloudDNSEndpoint + uri
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, url, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("X-Auth-Token", d.token)
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("error querying DNS API: %w", err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted {
|
|
|
|
return nil, fmt.Errorf("request failed for %s %s. Response code: %d", method, url, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var r json.RawMessage
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("JSON decode failed for %s %s. Response code: %d", method, url, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func login(config *Config) (*Identity, error) {
|
|
|
|
authData := AuthData{
|
|
|
|
Auth: Auth{
|
|
|
|
APIKeyCredentials: APIKeyCredentials{
|
|
|
|
Username: config.APIUser,
|
|
|
|
APIKey: config.APIKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(authData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, config.BaseURL, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := config.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("error querying Identity API: %w", err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("authentication failed: response code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var identity Identity
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &identity, nil
|
|
|
|
}
|