Skip to content

Commit 227b62d

Browse files
committed
review: 5
1 parent caf1120 commit 227b62d

File tree

13 files changed

+581
-184
lines changed

13 files changed

+581
-184
lines changed

cmd/zz_gen_cmd_dnshelp.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,17 +1727,17 @@ func displayDNSHelp(w io.Writer, name string) error {
17271727
ew.writeln()
17281728

17291729
ew.writeln(`Credentials:`)
1730-
ew.writeln(` - "NIC_RU_PASSWORD": Password for account in RU CENTER`)
1731-
ew.writeln(` - "NIC_RU_SECRET": Secret for application in DNS-hosting RU CENTER`)
1732-
ew.writeln(` - "NIC_RU_SERVICE_ID": Service ID for application in DNS-hosting RU CENTER`)
1733-
ew.writeln(` - "NIC_RU_SERVICE_NAME": Service Name for DNS-hosting RU CENTER`)
1734-
ew.writeln(` - "NIC_RU_USER": Agreement for account in RU CENTER`)
1730+
ew.writeln(` - "NICRU_PASSWORD": Password for account in RU CENTER`)
1731+
ew.writeln(` - "NICRU_SECRET": Secret for application in DNS-hosting RU CENTER`)
1732+
ew.writeln(` - "NICRU_SERVICE_ID": Service ID for application in DNS-hosting RU CENTER`)
1733+
ew.writeln(` - "NICRU_SERVICE_NAME": Service Name for DNS-hosting RU CENTER`)
1734+
ew.writeln(` - "NICRU_USER": Agreement for account in RU CENTER`)
17351735
ew.writeln()
17361736

17371737
ew.writeln(`Additional Configuration:`)
1738-
ew.writeln(` - "NIC_RU_POLLING_INTERVAL": Time between DNS propagation check`)
1739-
ew.writeln(` - "NIC_RU_PROPAGATION_TIMEOUT": Maximum waiting time for DNS propagation`)
1740-
ew.writeln(` - "NIC_RU_TTL": The TTL of the TXT record used for the DNS challenge`)
1738+
ew.writeln(` - "NICRU_POLLING_INTERVAL": Time between DNS propagation check`)
1739+
ew.writeln(` - "NICRU_PROPAGATION_TIMEOUT": Maximum waiting time for DNS propagation`)
1740+
ew.writeln(` - "NICRU_TTL": The TTL of the TXT record used for the DNS challenge`)
17411741

17421742
ew.writeln()
17431743
ew.writeln(`More information: https://go-acme.github.io/lego/dns/nicru`)

docs/content/dns/zz_gen_nicru.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ Configuration for [RU CENTER](https://nic.ru/).
2626
Here is an example bash command using the RU CENTER provider:
2727

2828
```bash
29-
NIC_RU_USER="<your_user>" \
30-
NIC_RU_PASSWORD="<your_password>" \
31-
NIC_RU_SERVICE_ID="<service_id>" \
32-
NIC_RU_SECRET="<service_secret>" \
33-
NIC_RU_SERVICE_NAME="<service_name>" \
29+
NICRU_USER="<your_user>" \
30+
NICRU_PASSWORD="<your_password>" \
31+
NICRU_SERVICE_ID="<service_id>" \
32+
NICRU_SECRET="<service_secret>" \
33+
NICRU_SERVICE_NAME="<service_name>" \
3434
./lego --dns nicru --domains "*.example.com" --email you@example.com run
3535
```
3636

@@ -41,11 +41,11 @@ NIC_RU_SERVICE_NAME="<service_name>" \
4141

4242
| Environment Variable Name | Description |
4343
|-----------------------|-------------|
44-
| `NIC_RU_PASSWORD` | Password for account in RU CENTER |
45-
| `NIC_RU_SECRET` | Secret for application in DNS-hosting RU CENTER |
46-
| `NIC_RU_SERVICE_ID` | Service ID for application in DNS-hosting RU CENTER |
47-
| `NIC_RU_SERVICE_NAME` | Service Name for DNS-hosting RU CENTER |
48-
| `NIC_RU_USER` | Agreement for account in RU CENTER |
44+
| `NICRU_PASSWORD` | Password for account in RU CENTER |
45+
| `NICRU_SECRET` | Secret for application in DNS-hosting RU CENTER |
46+
| `NICRU_SERVICE_ID` | Service ID for application in DNS-hosting RU CENTER |
47+
| `NICRU_SERVICE_NAME` | Service Name for DNS-hosting RU CENTER |
48+
| `NICRU_USER` | Agreement for account in RU CENTER |
4949

5050
The environment variable names can be suffixed by `_FILE` to reference a file instead of a value.
5151
More information [here]({{< ref "dns#configuration-and-credentials" >}}).
@@ -55,9 +55,9 @@ More information [here]({{< ref "dns#configuration-and-credentials" >}}).
5555

5656
| Environment Variable Name | Description |
5757
|--------------------------------|-------------|
58-
| `NIC_RU_POLLING_INTERVAL` | Time between DNS propagation check |
59-
| `NIC_RU_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation |
60-
| `NIC_RU_TTL` | The TTL of the TXT record used for the DNS challenge |
58+
| `NICRU_POLLING_INTERVAL` | Time between DNS propagation check |
59+
| `NICRU_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation |
60+
| `NICRU_TTL` | The TTL of the TXT record used for the DNS challenge |
6161

6262
The environment variable names can be suffixed by `_FILE` to reference a file instead of a value.
6363
More information [here]({{< ref "dns#configuration-and-credentials" >}}).
@@ -66,13 +66,13 @@ More information [here]({{< ref "dns#configuration-and-credentials" >}}).
6666

6767
You can find information about service ID and secret https://www.nic.ru/manager/oauth.cgi?step=oauth.app_list
6868

69-
| ENV Variable | Parameter from page | Example |
70-
|----------------------|--------------------------------|-------------------|
71-
| NIC_RU_USER | Username (Number of agreement) | NNNNNNN/NIC-D |
72-
| NIC_RU_PASSWORD | Password account | |
73-
| NIC_RU_SERVICE_ID | Application ID | hex-based, len 32 |
74-
| NIC_RU_SECRET | Identity endpoint | string len 91 |
75-
| NIC_RU_SERVICE_NAME | Service name in DNS-hosting | DPNNNNNNNNNN |
69+
| ENV Variable | Parameter from page | Example |
70+
|---------------------|--------------------------------|-------------------|
71+
| NICRU_USER | Username (Number of agreement) | NNNNNNN/NIC-D |
72+
| NICRU_PASSWORD | Password account | |
73+
| NICRU_SERVICE_ID | Application ID | hex-based, len 32 |
74+
| NICRU_SECRET | Identity endpoint | string len 91 |
75+
| NICRU_SERVICE_NAME | Service name in DNS-hosting | DPNNNNNNNNNN |
7676

7777

7878

providers/dns/nicru/internal/client.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/xml"
77
"errors"
88
"fmt"
9+
"io"
910
"net/http"
1011
"net/url"
1112
"strconv"
@@ -20,6 +21,19 @@ const tokenURL = defaultBaseURL + "/oauth/token"
2021

2122
const successStatus = "success"
2223

24+
// Trimmer trim all XML fields.
25+
type Trimmer struct {
26+
decoder *xml.Decoder
27+
}
28+
29+
func (tr Trimmer) Token() (xml.Token, error) {
30+
t, err := tr.decoder.Token()
31+
if cd, ok := t.(xml.CharData); ok {
32+
t = xml.CharData(bytes.TrimSpace(cd))
33+
}
34+
return t, err
35+
}
36+
2337
// OauthConfiguration credentials.
2438
type OauthConfiguration struct {
2539
OAuth2ClientID string
@@ -77,7 +91,7 @@ func NewClient(httpClient *http.Client, serviceName string) (*Client, error) {
7791
}, nil
7892
}
7993

80-
func (c *Client) GetZones() ([]*Zone, error) {
94+
func (c *Client) GetZones() ([]Zone, error) {
8195
endpoint := c.baseURL.JoinPath("dns-master", "services", c.serviceName, "zones")
8296

8397
req, err := http.NewRequest(http.MethodGet, endpoint.String(), nil)
@@ -93,7 +107,7 @@ func (c *Client) GetZones() ([]*Zone, error) {
93107
return apiResponse.Data.Zone, nil
94108
}
95109

96-
func (c *Client) GetRecords(fqdn string) ([]*RR, error) {
110+
func (c *Client) GetRecords(fqdn string) ([]RR, error) {
97111
endpoint := c.baseURL.JoinPath("dns-master", "services", c.serviceName, "zones", fqdn, "records")
98112

99113
req, err := http.NewRequest(http.MethodGet, endpoint.String(), nil)
@@ -106,7 +120,7 @@ func (c *Client) GetRecords(fqdn string) ([]*RR, error) {
106120
return nil, err
107121
}
108122

109-
var records []*RR
123+
var records []RR
110124
for _, zone := range apiResponse.Data.Zone {
111125
records = append(records, zone.RR...)
112126
}
@@ -115,7 +129,7 @@ func (c *Client) GetRecords(fqdn string) ([]*RR, error) {
115129
}
116130

117131
func (c *Client) AddTxtRecord(zoneName string, name string, content string, ttl int) (*Response, error) {
118-
request := &Request{RRList: &RrList{RR: []*RR{{
132+
request := &Request{RRList: &RrList{RR: []RR{{
119133
Name: name,
120134
TTL: strconv.Itoa(ttl),
121135
Type: "TXT",
@@ -155,6 +169,9 @@ func (c *Client) addRecords(zoneName string, request *Request) (*Response, error
155169
return nil, err
156170
}
157171

172+
// PUT https://api.nic.ru/dns-master/services/<service_id>/zones/<zone_name>/records
173+
// PUT https://api.nic.ru/dns-master/services/TESTSERVICE/zones/TEST.RU/records
174+
158175
req, err := http.NewRequest(http.MethodPut, endpoint.String(), body)
159176
if err != nil {
160177
return nil, err
@@ -168,15 +185,23 @@ func (c *Client) do(req *http.Request) (*Response, error) {
168185
if err != nil {
169186
return nil, err
170187
}
188+
171189
defer func() { _ = resp.Body.Close() }()
172190

173191
apiResponse := &Response{}
174192

175-
err = xml.NewDecoder(resp.Body).Decode(apiResponse)
193+
raw, err := io.ReadAll(resp.Body)
176194
if err != nil {
177195
return nil, err
178196
}
179197

198+
decoder := xml.NewTokenDecoder(Trimmer{decoder: xml.NewDecoder(bytes.NewReader(raw))})
199+
200+
err = decoder.Decode(apiResponse)
201+
if err != nil {
202+
return nil, fmt.Errorf("[status code=%d] %s", resp.StatusCode, string(raw))
203+
}
204+
180205
if apiResponse.Status != successStatus {
181206
return nil, apiResponse.Errors.Error
182207
}

0 commit comments

Comments
 (0)