Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add acme-proxy provider #708

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions providers/dns/acmeproxy/acmeproxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Package acmeproxy implements a DNS provider for solving the DNS-01 challenge using acme-proxy.
package acmeproxy

import (
"bytes"
"errors"
"fmt"
"net/http"
"time"

"github.com/xenolf/lego/platform/config/env"
)

// Config is used to configure the creation of the DNSProvider
type Config struct {
URL string
Provider string
PropagationTimeout time.Duration
PollingInterval time.Duration
}

// NewDefaultConfig returns a default configuration for the DNSProvider
func NewDefaultConfig() *Config {
return &Config{
PropagationTimeout: env.GetOrDefaultSecond("ACMEPROXY_PROPAGATION_TIMEOUT", 10*time.Minute),
PollingInterval: env.GetOrDefaultSecond("ACMEPROXY_POLLING_INTERVAL", 10*time.Second),
}
}

// DNSProvider describes a provider for acme-proxy
type DNSProvider struct {
client *http.Client
config *Config
}

// NewDNSProvider returns a DNSProvider instance configured for acme-proxy.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get("ACMEPROXY_URL", "ACMEPROXY_PROVIDER")
if err != nil {
return nil, fmt.Errorf("acmeproxy: %v", err)
}

config := NewDefaultConfig()
config.URL = values["ACMEPROXY_URL"]
config.Provider = values["ACMEPROXY_PROVIDER"]
return NewDNSProviderConfig(config)
}

// NewDNSProviderConfig return a DNSProvider instance configured for acme-proxy.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("acmeproxy: the configuration of the DNS provider is nil")
}

client := &http.Client{}

return &DNSProvider{client: client, 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
}

// Present creates a TXT record to fulfill the dns-01 challenge
func (d *DNSProvider) Present(domain, token, keyAuth string) error {

var jsonStr = []byte(`{"provider":"` + d.config.Provider + `","action":"present","domain": "` + domain + `", "keyauth": "` + keyAuth + `"}`)
req, err := http.NewRequest("POST", d.config.URL, bytes.NewBuffer(jsonStr))
if err != nil {
return fmt.Errorf("acmeproxy: error for %s in Cleanup: %v", domain, err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := d.client.Do(req)
if err != nil {
return fmt.Errorf("acmeproxy: error for %s in Present: %v", domain, err)
}
defer resp.Body.Close()

return nil
}

// CleanUp removes the TXT record matching the specified parameters
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {

var jsonStr = []byte(`{"provider":"` + d.config.Provider + `","action":"cleanup","domain": "` + domain + `", "keyauth": "` + keyAuth + `"}`)
req, err := http.NewRequest("POST", d.config.URL, bytes.NewBuffer(jsonStr))
if err != nil {
return fmt.Errorf("acmeproxy: error for %s in Cleanup: %v", domain, err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := d.client.Do(req)
if err != nil {
return fmt.Errorf("acmeproxy: error for %s in Cleanup: %v", domain, err)
}
defer resp.Body.Close()

return nil
}
3 changes: 3 additions & 0 deletions providers/dns/dns_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/xenolf/lego/acme"
"github.com/xenolf/lego/providers/dns/acmedns"
"github.com/xenolf/lego/providers/dns/acmeproxy"
"github.com/xenolf/lego/providers/dns/alidns"
"github.com/xenolf/lego/providers/dns/auroradns"
"github.com/xenolf/lego/providers/dns/azure"
Expand Down Expand Up @@ -59,6 +60,8 @@ func NewDNSChallengeProviderByName(name string) (acme.ChallengeProvider, error)
switch name {
case "acme-dns":
return acmedns.NewDNSProvider()
case "acme-proxy":
return acmeproxy.NewDNSProvider()
case "alidns":
return alidns.NewDNSProvider()
case "azure":
Expand Down