@@ -4,7 +4,9 @@ package liquidweb
44import (
55 "errors"
66 "fmt"
7+ "sort"
78 "strconv"
9+ "strings"
810 "sync"
911 "time"
1012
@@ -14,7 +16,7 @@ import (
1416 "github.com/liquidweb/liquidweb-go/network"
1517)
1618
17- const defaultBaseURL = "https://api.stormondemand .com"
19+ const defaultBaseURL = "https://api.liquidweb .com"
1820
1921// Environment variables names.
2022const (
@@ -45,15 +47,13 @@ type Config struct {
4547
4648// NewDefaultConfig returns a default configuration for the DNSProvider.
4749func NewDefaultConfig () * Config {
48- config := & Config {
50+ return & Config {
4951 BaseURL : defaultBaseURL ,
5052 TTL : env .GetOrDefaultInt (EnvTTL , 300 ),
5153 PropagationTimeout : env .GetOrDefaultSecond (EnvPropagationTimeout , 2 * time .Minute ),
5254 PollingInterval : env .GetOrDefaultSecond (EnvPollingInterval , 2 * time .Second ),
5355 HTTPTimeout : env .GetOrDefaultSecond (EnvHTTPTimeout , 1 * time .Minute ),
5456 }
55-
56- return config
5757}
5858
5959// DNSProvider implements the challenge.Provider interface.
@@ -66,7 +66,7 @@ type DNSProvider struct {
6666
6767// NewDNSProvider returns a DNSProvider instance configured for Liquid Web.
6868func NewDNSProvider () (* DNSProvider , error ) {
69- values , err := env .Get (EnvUsername , EnvPassword , EnvZone )
69+ values , err := env .Get (EnvUsername , EnvPassword )
7070 if err != nil {
7171 return nil , fmt .Errorf ("liquidweb: %w" , err )
7272 }
@@ -75,7 +75,7 @@ func NewDNSProvider() (*DNSProvider, error) {
7575 config .BaseURL = env .GetOrFile (EnvURL )
7676 config .Username = values [EnvUsername ]
7777 config .Password = values [EnvPassword ]
78- config .Zone = values [ EnvZone ]
78+ config .Zone = env . GetOrDefaultString ( EnvZone , "" )
7979
8080 return NewDNSProviderConfig (config )
8181}
@@ -90,19 +90,6 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
9090 config .BaseURL = defaultBaseURL
9191 }
9292
93- if config .Zone == "" {
94- return nil , errors .New ("liquidweb: zone is missing" )
95- }
96-
97- if config .Username == "" {
98- return nil , errors .New ("liquidweb: username is missing" )
99- }
100-
101- if config .Password == "" {
102- return nil , errors .New ("liquidweb: password is missing" )
103- }
104-
105- // Initialize LW client.
10693 client , err := lw .NewAPI (config .Username , config .Password , config .BaseURL , int (config .HTTPTimeout .Seconds ()))
10794 if err != nil {
10895 return nil , fmt .Errorf ("liquidweb: could not create Liquid Web API client: %w" , err )
@@ -133,6 +120,15 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
133120 TTL : d .config .TTL ,
134121 }
135122
123+ if params .Zone == "" {
124+ bestZone , err := d .findZone (params .Name )
125+ if err != nil {
126+ return fmt .Errorf ("liquidweb: %w" , err )
127+ }
128+
129+ params .Zone = bestZone
130+ }
131+
136132 dnsEntry , err := d .client .NetworkDNS .Create (params )
137133 if err != nil {
138134 return fmt .Errorf ("liquidweb: could not create TXT record: %w" , err )
@@ -167,3 +163,31 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
167163
168164 return nil
169165}
166+
167+ func (d * DNSProvider ) findZone (domain string ) (string , error ) {
168+ zones , err := d .client .NetworkDNSZone .ListAll ()
169+ if err != nil {
170+ return "" , fmt .Errorf ("failed to retrieve zones for account: %w" , err )
171+ }
172+
173+ // filter the zones on the account to only ones that match
174+ var zs []network.DNSZone
175+ for _ , item := range zones .Items {
176+ if strings .HasSuffix (domain , item .Name ) {
177+ zs = append (zs , item )
178+ }
179+ }
180+
181+ if len (zs ) < 1 {
182+ return "" , fmt .Errorf ("no valid zone in account for certificate '%s'" , domain )
183+ }
184+
185+ // powerdns _only_ looks for records on the longest matching subdomain zone aka,
186+ // for test.sub.example.com if sub.example.com exists,
187+ // it will look there it will not look atexample.com even if it also exists
188+ sort .Slice (zs , func (i , j int ) bool {
189+ return len (zs [i ].Name ) > len (zs [j ].Name )
190+ })
191+
192+ return zs [0 ].Name , nil
193+ }
0 commit comments