Skip to content

Commit

Permalink
hostingde: autodetection of the zone name.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed May 14, 2021
1 parent 6872149 commit 9508192
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
57 changes: 37 additions & 20 deletions providers/dns/hostingde/hostingde.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ type DNSProvider struct {
// Credentials must be passed in the environment variables:
// HOSTINGDE_ZONE_NAME and HOSTINGDE_API_KEY.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvAPIKey, EnvZoneName)
values, err := env.Get(EnvAPIKey)
if err != nil {
return nil, fmt.Errorf("hostingde: %w", err)
}

config := NewDefaultConfig()
config.APIKey = values[EnvAPIKey]
config.ZoneName = values[EnvZoneName]
config.ZoneName = env.GetOrFile(EnvZoneName)

return NewDNSProviderConfig(config)
}
Expand All @@ -80,10 +80,6 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
return nil, errors.New("hostingde: API key missing")
}

if config.ZoneName == "" {
return nil, errors.New("hostingde: Zone Name missing")
}

return &DNSProvider{
config: config,
recordIDs: make(map[string]string),
Expand All @@ -100,22 +96,24 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
fqdn, value := dns01.GetRecord(domain, keyAuth)

zoneName, err := d.getZoneName(fqdn)
if err != nil {
return fmt.Errorf("hostingde: could not determine zone for domain %q: %w", domain, err)
}

// get the ZoneConfig for that domain
zonesFind := ZoneConfigsFindRequest{
Filter: Filter{
Field: "zoneName",
Value: d.config.ZoneName,
},
Limit: 1,
Page: 1,
Filter: Filter{Field: "zoneName", Value: zoneName},
Limit: 1,
Page: 1,
}
zonesFind.AuthToken = d.config.APIKey

zoneConfig, err := d.getZone(zonesFind)
if err != nil {
return fmt.Errorf("hostingde: %w", err)
}
zoneConfig.Name = d.config.ZoneName
zoneConfig.Name = zoneName

rec := []DNSRecord{{
Type: "TXT",
Expand Down Expand Up @@ -154,6 +152,11 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
fqdn, value := dns01.GetRecord(domain, keyAuth)

zoneName, err := d.getZoneName(fqdn)
if err != nil {
return fmt.Errorf("hostingde: could not determine zone for domain %q: %w", domain, err)
}

rec := []DNSRecord{{
Type: "TXT",
Name: dns01.UnFqdn(fqdn),
Expand All @@ -162,20 +165,17 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {

// get the ZoneConfig for that domain
zonesFind := ZoneConfigsFindRequest{
Filter: Filter{
Field: "zoneName",
Value: d.config.ZoneName,
},
Limit: 1,
Page: 1,
Filter: Filter{Field: "zoneName", Value: zoneName},
Limit: 1,
Page: 1,
}
zonesFind.AuthToken = d.config.APIKey

zoneConfig, err := d.getZone(zonesFind)
if err != nil {
return fmt.Errorf("hostingde: %w", err)
}
zoneConfig.Name = d.config.ZoneName
zoneConfig.Name = zoneName

req := ZoneUpdateRequest{
ZoneConfig: *zoneConfig,
Expand All @@ -194,3 +194,20 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
}
return nil
}

func (d *DNSProvider) getZoneName(fqdn string) (string, error) {
if d.config.ZoneName != "" {
return d.config.ZoneName, nil
}

zoneName, err := dns01.FindZoneByFqdn(fqdn)
if err != nil {
return "", err
}

if zoneName == "" {
return "", errors.New("empty zone name")
}

return dns01.UnFqdn(zoneName), nil
}
3 changes: 1 addition & 2 deletions providers/dns/hostingde/hostingde.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ Since = "v1.1.0"

Example = '''
HOSTINGDE_API_KEY=xxxxxxxx \
HOSTINGDE_ZONE_NAME=yyyyy \
lego -email myemail@example.com --dns hostingde --domains my.example.org -run
'''

[Configuration]
[Configuration.Credentials]
HOSTINGDE_API_KEY = "API key"
HOSTINGDE_ZONE_NAME = "Zone name in ACE format"
[Configuration.Additional]
HOSTINGDE_ZONE_NAME = "Zone name in ACE format"
HOSTINGDE_POLLING_INTERVAL = "Time between DNS propagation check"
HOSTINGDE_PROPAGATION_TIMEOUT = "Maximum waiting time for DNS propagation"
HOSTINGDE_TTL = "The TTL of the TXT record used for the DNS challenge"
Expand Down
19 changes: 3 additions & 16 deletions providers/dns/hostingde/hostingde_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewDNSProvider(t *testing.T) {
desc: "success",
envVars: map[string]string{
EnvAPIKey: "123",
EnvZoneName: "456",
EnvZoneName: "example.org",
},
},
{
Expand All @@ -34,7 +34,7 @@ func TestNewDNSProvider(t *testing.T) {
EnvAPIKey: "",
EnvZoneName: "",
},
expected: "hostingde: some credentials information are missing: HOSTINGDE_API_KEY,HOSTINGDE_ZONE_NAME",
expected: "hostingde: some credentials information are missing: HOSTINGDE_API_KEY",
},
{
desc: "missing access key",
Expand All @@ -44,14 +44,6 @@ func TestNewDNSProvider(t *testing.T) {
},
expected: "hostingde: some credentials information are missing: HOSTINGDE_API_KEY",
},
{
desc: "missing zone name",
envVars: map[string]string{
EnvAPIKey: "123",
EnvZoneName: "",
},
expected: "hostingde: some credentials information are missing: HOSTINGDE_ZONE_NAME",
},
}

for _, test := range testCases {
Expand Down Expand Up @@ -85,7 +77,7 @@ func TestNewDNSProviderConfig(t *testing.T) {
{
desc: "success",
apiKey: "123",
zoneName: "456",
zoneName: "example.org",
},
{
desc: "missing credentials",
Expand All @@ -96,11 +88,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
zoneName: "456",
expected: "hostingde: API key missing",
},
{
desc: "missing zone name",
apiKey: "123",
expected: "hostingde: Zone Name missing",
},
}

for _, test := range testCases {
Expand Down

0 comments on commit 9508192

Please sign in to comment.