Skip to content

Commit c5a95c4

Browse files
authored
cpanel: remove custom DNS call (#2102)
1 parent 83ff393 commit c5a95c4

File tree

6 files changed

+74
-188
lines changed

6 files changed

+74
-188
lines changed

cmd/zz_gen_cmd_dnshelp.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,6 @@ func displayDNSHelp(w io.Writer, name string) error {
621621

622622
ew.writeln(`Credentials:`)
623623
ew.writeln(` - "CPANEL_BASE_URL": API server URL`)
624-
ew.writeln(` - "CPANEL_NAMESERVER": Nameserver`)
625624
ew.writeln(` - "CPANEL_TOKEN": API token`)
626625
ew.writeln(` - "CPANEL_USERNAME": username`)
627626
ew.writeln()

docs/content/dns/zz_gen_cpanel.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Here is an example bash command using the CPanel/WHM provider:
3131
CPANEL_USERNAME = "yyyy"
3232
CPANEL_TOKEN = "xxxx"
3333
CPANEL_BASE_URL = "https://example.com:2083" \
34-
CPANEL_NAMESERVER = "ns1.example.com:53" \
3534
lego --email [email protected] --dns cpanel --domains my.example.org run
3635

3736
## WHM
@@ -40,7 +39,6 @@ CPANEL_MODE = whm
4039
CPANEL_USERNAME = "yyyy"
4140
CPANEL_TOKEN = "xxxx"
4241
CPANEL_BASE_URL = "https://example.com:2087" \
43-
CPANEL_NAMESERVER = "ns1.example.com:53" \
4442
lego --email [email protected] --dns cpanel --domains my.example.org run
4543
```
4644

@@ -52,7 +50,6 @@ lego --email [email protected] --dns cpanel --domains my.example.org run
5250
| Environment Variable Name | Description |
5351
|-----------------------|-------------|
5452
| `CPANEL_BASE_URL` | API server URL |
55-
| `CPANEL_NAMESERVER` | Nameserver |
5653
| `CPANEL_TOKEN` | API token |
5754
| `CPANEL_USERNAME` | username |
5855

providers/dns/cpanel/cpanel.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import (
2121
const (
2222
envNamespace = "CPANEL_"
2323

24-
EnvMode = envNamespace + "MODE"
25-
EnvUsername = envNamespace + "USERNAME"
26-
EnvToken = envNamespace + "TOKEN"
27-
EnvBaseURL = envNamespace + "BASE_URL"
28-
EnvNameserver = envNamespace + "NAMESERVER"
24+
EnvMode = envNamespace + "MODE"
25+
EnvUsername = envNamespace + "USERNAME"
26+
EnvToken = envNamespace + "TOKEN"
27+
EnvBaseURL = envNamespace + "BASE_URL"
2928

3029
EnvTTL = envNamespace + "TTL"
3130
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
@@ -46,7 +45,6 @@ type Config struct {
4645
Username string
4746
Token string
4847
BaseURL string
49-
Nameserver string
5048
TTL int
5149
PropagationTimeout time.Duration
5250
PollingInterval time.Duration
@@ -58,7 +56,7 @@ func NewDefaultConfig() *Config {
5856
return &Config{
5957
Mode: env.GetOrDefaultString(EnvMode, "cpanel"),
6058
TTL: env.GetOrDefaultInt(EnvTTL, 300),
61-
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
59+
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
6260
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
6361
HTTPClient: &http.Client{
6462
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
@@ -68,16 +66,15 @@ func NewDefaultConfig() *Config {
6866

6967
// DNSProvider implements the challenge.Provider interface.
7068
type DNSProvider struct {
71-
config *Config
72-
client apiClient
73-
dnsClient *shared.DNSClient
69+
config *Config
70+
client apiClient
7471
}
7572

7673
// NewDNSProvider returns a DNSProvider instance configured for CPanel.
7774
// Credentials must be passed in the environment variables:
7875
// CPANEL_USERNAME, CPANEL_TOKEN, CPANEL_BASE_URL, CPANEL_NAMESERVER.
7976
func NewDNSProvider() (*DNSProvider, error) {
80-
values, err := env.Get(EnvUsername, EnvToken, EnvBaseURL, EnvNameserver)
77+
values, err := env.Get(EnvUsername, EnvToken, EnvBaseURL)
8178
if err != nil {
8279
return nil, fmt.Errorf("cpanel: %w", err)
8380
}
@@ -86,7 +83,6 @@ func NewDNSProvider() (*DNSProvider, error) {
8683
config.Username = values[EnvUsername]
8784
config.Token = values[EnvToken]
8885
config.BaseURL = values[EnvBaseURL]
89-
config.Nameserver = values[EnvNameserver]
9086

9187
return NewDNSProviderConfig(config)
9288
}
@@ -101,7 +97,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
10197
return nil, errors.New("cpanel: some credentials information are missing")
10298
}
10399

104-
if config.BaseURL == "" || config.Nameserver == "" {
100+
if config.BaseURL == "" {
105101
return nil, errors.New("cpanel: server information are missing")
106102
}
107103

@@ -111,9 +107,8 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
111107
}
112108

113109
return &DNSProvider{
114-
config: config,
115-
client: client,
116-
dnsClient: shared.NewDNSClient(10 * time.Second),
110+
config: config,
111+
client: client,
117112
}, nil
118113
}
119114

@@ -128,21 +123,19 @@ func (d *DNSProvider) Present(domain, _, keyAuth string) error {
128123
ctx := context.Background()
129124
info := dns01.GetChallengeInfo(domain, keyAuth)
130125

131-
effectiveDomain := strings.TrimPrefix(info.EffectiveFQDN, "_acme-challenge.")
132-
133-
soa, err := d.dnsClient.SOACall(effectiveDomain, d.config.Nameserver)
126+
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
134127
if err != nil {
135-
return fmt.Errorf("cpanel[mode=%s]: could not find SOA for domain %q (%s) in %s: %w", d.config.Mode, domain, info.EffectiveFQDN, d.config.Nameserver, err)
128+
return fmt.Errorf("arvancloud: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
136129
}
137130

138-
zone := dns01.UnFqdn(soa.Hdr.Name)
131+
zone := dns01.UnFqdn(authZone)
139132

140133
zoneInfo, err := d.client.FetchZoneInformation(ctx, zone)
141134
if err != nil {
142135
return fmt.Errorf("cpanel[mode=%s]: fetch zone information: %w", d.config.Mode, err)
143136
}
144137

145-
serial, err := getZoneSerial(soa.Hdr.Name, zoneInfo)
138+
serial, err := getZoneSerial(authZone, zoneInfo)
146139
if err != nil {
147140
return fmt.Errorf("cpanel[mode=%s]: get zone serial: %w", d.config.Mode, err)
148141
}
@@ -204,19 +197,19 @@ func (d *DNSProvider) CleanUp(domain, _, keyAuth string) error {
204197
ctx := context.Background()
205198
info := dns01.GetChallengeInfo(domain, keyAuth)
206199

207-
soa, err := d.dnsClient.SOACall(strings.TrimPrefix(info.EffectiveFQDN, "_acme-challenge."), d.config.Nameserver)
200+
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
208201
if err != nil {
209-
return fmt.Errorf("cpanel[mode=%s]: could not find SOA for domain %q (%s) in %s: %w", d.config.Mode, domain, info.EffectiveFQDN, d.config.Nameserver, err)
202+
return fmt.Errorf("arvancloud: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
210203
}
211204

212-
zone := dns01.UnFqdn(soa.Hdr.Name)
205+
zone := dns01.UnFqdn(authZone)
213206

214207
zoneInfo, err := d.client.FetchZoneInformation(ctx, zone)
215208
if err != nil {
216209
return fmt.Errorf("cpanel[mode=%s]: fetch zone information: %w", d.config.Mode, err)
217210
}
218211

219-
serial, err := getZoneSerial(soa.Hdr.Name, zoneInfo)
212+
serial, err := getZoneSerial(authZone, zoneInfo)
220213
if err != nil {
221214
return fmt.Errorf("cpanel[mode=%s]: get zone serial: %w", d.config.Mode, err)
222215
}

providers/dns/cpanel/cpanel.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Example = '''
1010
CPANEL_USERNAME = "yyyy"
1111
CPANEL_TOKEN = "xxxx"
1212
CPANEL_BASE_URL = "https://example.com:2083" \
13-
CPANEL_NAMESERVER = "ns1.example.com:53" \
1413
lego --email [email protected] --dns cpanel --domains my.example.org run
1514
1615
## WHM
@@ -19,7 +18,6 @@ CPANEL_MODE = whm
1918
CPANEL_USERNAME = "yyyy"
2019
CPANEL_TOKEN = "xxxx"
2120
CPANEL_BASE_URL = "https://example.com:2087" \
22-
CPANEL_NAMESERVER = "ns1.example.com:53" \
2321
lego --email [email protected] --dns cpanel --domains my.example.org run
2422
'''
2523

@@ -28,7 +26,6 @@ lego --email [email protected] --dns cpanel --domains my.example.org run
2826
CPANEL_USERNAME = "username"
2927
CPANEL_TOKEN = "API token"
3028
CPANEL_BASE_URL = "API server URL"
31-
CPANEL_NAMESERVER = "Nameserver"
3229
[Configuration.Additional]
3330
CPANEL_MODE = "use cpanel API or WHM API (Default: cpanel)"
3431
CPANEL_POLLING_INTERVAL = "Time between DNS propagation check"

providers/dns/cpanel/cpanel_test.go

Lines changed: 55 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ var envTest = tester.NewEnvTest(
1616
EnvMode,
1717
EnvUsername,
1818
EnvToken,
19-
EnvBaseURL,
20-
EnvNameserver).
19+
EnvBaseURL).
2120
WithDomain(envDomain)
2221

2322
func TestNewDNSProvider(t *testing.T) {
@@ -30,62 +29,47 @@ func TestNewDNSProvider(t *testing.T) {
3029
{
3130
desc: "success cpanel mode (default)",
3231
envVars: map[string]string{
33-
EnvUsername: "user",
34-
EnvToken: "secret",
35-
EnvBaseURL: "https://example.com",
36-
EnvNameserver: "ns.example.com:53",
32+
EnvUsername: "user",
33+
EnvToken: "secret",
34+
EnvBaseURL: "https://example.com",
3735
},
3836
expectedMode: "cpanel",
3937
},
4038
{
4139
desc: "success whm mode",
4240
envVars: map[string]string{
43-
EnvMode: "whm",
44-
EnvUsername: "user",
45-
EnvToken: "secret",
46-
EnvBaseURL: "https://example.com",
47-
EnvNameserver: "ns.example.com:53",
41+
EnvMode: "whm",
42+
EnvUsername: "user",
43+
EnvToken: "secret",
44+
EnvBaseURL: "https://example.com",
4845
},
4946
expectedMode: "whm",
5047
},
5148
{
5249
desc: "missing user",
5350
envVars: map[string]string{
54-
EnvToken: "secret",
55-
EnvBaseURL: "https://example.com",
56-
EnvNameserver: "ns.example.com:53",
51+
EnvToken: "secret",
52+
EnvBaseURL: "https://example.com",
5753
},
5854
expected: "cpanel: some credentials information are missing: CPANEL_USERNAME",
5955
},
6056
{
6157
desc: "missing token",
6258
envVars: map[string]string{
63-
EnvUsername: "user",
64-
EnvBaseURL: "https://example.com",
65-
EnvNameserver: "ns.example.com:53",
59+
EnvUsername: "user",
60+
EnvBaseURL: "https://example.com",
6661
},
6762
expected: "cpanel: some credentials information are missing: CPANEL_TOKEN",
6863
},
6964
{
7065
desc: "missing base URL",
7166
envVars: map[string]string{
72-
EnvUsername: "user",
73-
EnvToken: "secret",
74-
EnvBaseURL: "",
75-
EnvNameserver: "ns.example.com:53",
67+
EnvUsername: "user",
68+
EnvToken: "secret",
69+
EnvBaseURL: "",
7670
},
7771
expected: "cpanel: some credentials information are missing: CPANEL_BASE_URL",
7872
},
79-
{
80-
desc: "missing nameserver",
81-
envVars: map[string]string{
82-
EnvUsername: "user",
83-
EnvToken: "secret",
84-
EnvBaseURL: "https://example.com",
85-
EnvNameserver: "",
86-
},
87-
expected: "cpanel: some credentials information are missing: CPANEL_NAMESERVER",
88-
},
8973
}
9074

9175
for _, test := range testCases {
@@ -111,74 +95,58 @@ func TestNewDNSProvider(t *testing.T) {
11195

11296
func TestNewDNSProviderConfig(t *testing.T) {
11397
testCases := []struct {
114-
desc string
115-
mode string
116-
username string
117-
token string
118-
baseURL string
119-
nameserver string
120-
expected string
98+
desc string
99+
mode string
100+
username string
101+
token string
102+
baseURL string
103+
expected string
121104
}{
122105
{
123-
desc: "success",
124-
mode: "whm",
125-
username: "user",
126-
token: "secret",
127-
baseURL: "https://example.com",
128-
nameserver: "ns.example.com:53",
129-
},
130-
{
131-
desc: "missing mode",
132-
username: "user",
133-
token: "secret",
134-
baseURL: "https://example.com",
135-
nameserver: "ns.example.com:53",
136-
expected: `cpanel: create client error: unsupported mode: ""`,
106+
desc: "success",
107+
mode: "whm",
108+
username: "user",
109+
token: "secret",
110+
baseURL: "https://example.com",
137111
},
138112
{
139-
desc: "invalid mode",
140-
mode: "test",
141-
username: "user",
142-
token: "secret",
143-
baseURL: "https://example.com",
144-
nameserver: "ns.example.com:53",
145-
expected: `cpanel: create client error: unsupported mode: "test"`,
113+
desc: "missing mode",
114+
username: "user",
115+
token: "secret",
116+
baseURL: "https://example.com",
117+
expected: `cpanel: create client error: unsupported mode: ""`,
146118
},
147119
{
148-
desc: "missing username",
149-
mode: "whm",
150-
username: "",
151-
token: "secret",
152-
baseURL: "https://example.com",
153-
nameserver: "ns.example.com:53",
154-
expected: "cpanel: some credentials information are missing",
120+
desc: "invalid mode",
121+
mode: "test",
122+
username: "user",
123+
token: "secret",
124+
baseURL: "https://example.com",
125+
expected: `cpanel: create client error: unsupported mode: "test"`,
155126
},
156127
{
157-
desc: "missing token",
158-
mode: "whm",
159-
username: "user",
160-
token: "",
161-
baseURL: "https://example.com",
162-
nameserver: "ns.example.com:53",
163-
expected: "cpanel: some credentials information are missing",
128+
desc: "missing username",
129+
mode: "whm",
130+
username: "",
131+
token: "secret",
132+
baseURL: "https://example.com",
133+
expected: "cpanel: some credentials information are missing",
164134
},
165135
{
166-
desc: "missing base URL",
167-
mode: "whm",
168-
username: "user",
169-
token: "secret",
170-
baseURL: "",
171-
nameserver: "ns.example.com:53",
172-
expected: "cpanel: server information are missing",
136+
desc: "missing token",
137+
mode: "whm",
138+
username: "user",
139+
token: "",
140+
baseURL: "https://example.com",
141+
expected: "cpanel: some credentials information are missing",
173142
},
174143
{
175-
desc: "missing nameserver",
176-
mode: "whm",
177-
username: "user",
178-
token: "secret",
179-
baseURL: "https://example.com",
180-
nameserver: "",
181-
expected: "cpanel: server information are missing",
144+
desc: "missing base URL",
145+
mode: "whm",
146+
username: "user",
147+
token: "secret",
148+
baseURL: "",
149+
expected: "cpanel: server information are missing",
182150
},
183151
}
184152

@@ -189,7 +157,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
189157
config.Username = test.username
190158
config.Token = test.token
191159
config.BaseURL = test.baseURL
192-
config.Nameserver = test.nameserver
193160

194161
p, err := NewDNSProviderConfig(config)
195162

0 commit comments

Comments
 (0)