2023-07-29 12:59:24 +02:00
|
|
|
// Package httpreq implements a DNS provider for solving the DNS-01 challenge through an HTTP server.
|
2018-11-08 19:51:13 +02:00
|
|
|
package httpreq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 09:49:38 +02:00
|
|
|
"context"
|
2018-11-08 19:51:13 +02:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2023-05-05 09:49:38 +02:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2018-11-08 19:51:13 +02:00
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "HTTPREQ_"
|
|
|
|
|
|
|
|
EnvEndpoint = envNamespace + "ENDPOINT"
|
|
|
|
EnvMode = envNamespace + "MODE"
|
|
|
|
EnvUsername = envNamespace + "USERNAME"
|
|
|
|
EnvPassword = envNamespace + "PASSWORD"
|
|
|
|
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2018-11-08 19:51:13 +02:00
|
|
|
type message struct {
|
|
|
|
FQDN string `json:"fqdn"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type messageRaw struct {
|
|
|
|
Domain string `json:"domain"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
KeyAuth string `json:"keyAuth"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-11-08 19:51:13 +02:00
|
|
|
type Config struct {
|
|
|
|
Endpoint *url.URL
|
|
|
|
Mode string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-11-08 19:51:13 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-12 00:51:10 +02:00
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2018-11-08 19:51:13 +02:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-12 00:51:10 +02:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
2018-11-08 19:51:13 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2018-11-08 19:51:13 +02:00
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-12 00:51:10 +02:00
|
|
|
values, err := env.Get(EnvEndpoint)
|
2018-11-08 19:51:13 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("httpreq: %w", err)
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
endpoint, err := url.Parse(values[EnvEndpoint])
|
2018-11-08 19:51:13 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("httpreq: %w", err)
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
2020-03-12 00:51:10 +02:00
|
|
|
config.Mode = env.GetOrFile(EnvMode)
|
|
|
|
config.Username = env.GetOrFile(EnvUsername)
|
|
|
|
config.Password = env.GetOrFile(EnvPassword)
|
2018-11-08 19:51:13 +02:00
|
|
|
config.Endpoint = endpoint
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
2019-08-20 18:40:41 +02:00
|
|
|
// NewDNSProviderConfig return a DNSProvider.
|
2018-11-08 19:51:13 +02:00
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("httpreq: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Endpoint == nil {
|
|
|
|
return nil, errors.New("httpreq: the endpoint is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{config: config}, 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
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2018-11-08 19:51:13 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-05-05 09:49:38 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-11-08 19:51:13 +02:00
|
|
|
if d.config.Mode == "RAW" {
|
|
|
|
msg := &messageRaw{
|
|
|
|
Domain: domain,
|
|
|
|
Token: token,
|
|
|
|
KeyAuth: keyAuth,
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err := d.doPost(ctx, "/present", msg)
|
2018-11-08 19:51:13 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("httpreq: %w", err)
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-11-08 19:51:13 +02:00
|
|
|
msg := &message{
|
2023-03-07 10:39:05 +02:00
|
|
|
FQDN: info.EffectiveFQDN,
|
|
|
|
Value: info.Value,
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err := d.doPost(ctx, "/present", msg)
|
2018-11-08 19:51:13 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("httpreq: %w", err)
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-11-08 19:51:13 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-05-05 09:49:38 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-11-08 19:51:13 +02:00
|
|
|
if d.config.Mode == "RAW" {
|
|
|
|
msg := &messageRaw{
|
|
|
|
Domain: domain,
|
|
|
|
Token: token,
|
|
|
|
KeyAuth: keyAuth,
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err := d.doPost(ctx, "/cleanup", msg)
|
2018-11-08 19:51:13 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("httpreq: %w", err)
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-11-08 19:51:13 +02:00
|
|
|
msg := &message{
|
2023-03-07 10:39:05 +02:00
|
|
|
FQDN: info.EffectiveFQDN,
|
|
|
|
Value: info.Value,
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err := d.doPost(ctx, "/cleanup", msg)
|
2018-11-08 19:51:13 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("httpreq: %w", err)
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
func (d *DNSProvider) doPost(ctx context.Context, uri string, msg any) error {
|
|
|
|
reqBody := new(bytes.Buffer)
|
2018-11-08 19:51:13 +02:00
|
|
|
err := json.NewEncoder(reqBody).Encode(msg)
|
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return fmt.Errorf("failed to create request JSON body: %w", err)
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
|
2023-02-09 18:19:58 +02:00
|
|
|
endpoint := d.config.Endpoint.JoinPath(uri)
|
2018-11-08 19:51:13 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), reqBody)
|
2018-11-08 19:51:13 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return fmt.Errorf("unable to create request: %w", err)
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req.Header.Set("Accept", "application/json")
|
2018-11-08 19:51:13 +02:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
if d.config.Username != "" && d.config.Password != "" {
|
2018-11-08 19:51:13 +02:00
|
|
|
req.SetBasicAuth(d.config.Username, d.config.Password)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
2018-11-08 19:51:13 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
2018-11-08 19:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|