Skip to content

Commit

Permalink
review: 5
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 12, 2023
1 parent caf1120 commit 4f9e9d4
Show file tree
Hide file tree
Showing 13 changed files with 582 additions and 184 deletions.
16 changes: 8 additions & 8 deletions cmd/zz_gen_cmd_dnshelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1727,17 +1727,17 @@ func displayDNSHelp(w io.Writer, name string) error {
ew.writeln()

ew.writeln(`Credentials:`)
ew.writeln(` - "NIC_RU_PASSWORD": Password for account in RU CENTER`)
ew.writeln(` - "NIC_RU_SECRET": Secret for application in DNS-hosting RU CENTER`)
ew.writeln(` - "NIC_RU_SERVICE_ID": Service ID for application in DNS-hosting RU CENTER`)
ew.writeln(` - "NIC_RU_SERVICE_NAME": Service Name for DNS-hosting RU CENTER`)
ew.writeln(` - "NIC_RU_USER": Agreement for account in RU CENTER`)
ew.writeln(` - "NICRU_PASSWORD": Password for account in RU CENTER`)
ew.writeln(` - "NICRU_SECRET": Secret for application in DNS-hosting RU CENTER`)
ew.writeln(` - "NICRU_SERVICE_ID": Service ID for application in DNS-hosting RU CENTER`)
ew.writeln(` - "NICRU_SERVICE_NAME": Service Name for DNS-hosting RU CENTER`)
ew.writeln(` - "NICRU_USER": Agreement for account in RU CENTER`)
ew.writeln()

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

ew.writeln()
ew.writeln(`More information: https://go-acme.github.io/lego/dns/nicru`)
Expand Down
40 changes: 20 additions & 20 deletions docs/content/dns/zz_gen_nicru.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Configuration for [RU CENTER](https://nic.ru/).
Here is an example bash command using the RU CENTER provider:

```bash
NIC_RU_USER="<your_user>" \
NIC_RU_PASSWORD="<your_password>" \
NIC_RU_SERVICE_ID="<service_id>" \
NIC_RU_SECRET="<service_secret>" \
NIC_RU_SERVICE_NAME="<service_name>" \
NICRU_USER="<your_user>" \
NICRU_PASSWORD="<your_password>" \
NICRU_SERVICE_ID="<service_id>" \
NICRU_SECRET="<service_secret>" \
NICRU_SERVICE_NAME="<service_name>" \
./lego --dns nicru --domains "*.example.com" --email you@example.com run
```

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

| Environment Variable Name | Description |
|-----------------------|-------------|
| `NIC_RU_PASSWORD` | Password for account in RU CENTER |
| `NIC_RU_SECRET` | Secret for application in DNS-hosting RU CENTER |
| `NIC_RU_SERVICE_ID` | Service ID for application in DNS-hosting RU CENTER |
| `NIC_RU_SERVICE_NAME` | Service Name for DNS-hosting RU CENTER |
| `NIC_RU_USER` | Agreement for account in RU CENTER |
| `NICRU_PASSWORD` | Password for account in RU CENTER |
| `NICRU_SECRET` | Secret for application in DNS-hosting RU CENTER |
| `NICRU_SERVICE_ID` | Service ID for application in DNS-hosting RU CENTER |
| `NICRU_SERVICE_NAME` | Service Name for DNS-hosting RU CENTER |
| `NICRU_USER` | Agreement for account in RU CENTER |

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

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

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

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

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



Expand Down
35 changes: 30 additions & 5 deletions providers/dns/nicru/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
Expand All @@ -20,6 +21,19 @@ const tokenURL = defaultBaseURL + "/oauth/token"

const successStatus = "success"

// Trimmer trim all XML fields.
type Trimmer struct {
decoder *xml.Decoder
}

func (tr Trimmer) Token() (xml.Token, error) {
t, err := tr.decoder.Token()
if cd, ok := t.(xml.CharData); ok {
t = xml.CharData(bytes.TrimSpace(cd))
}
return t, err
}

// OauthConfiguration credentials.
type OauthConfiguration struct {
OAuth2ClientID string
Expand Down Expand Up @@ -77,7 +91,7 @@ func NewClient(httpClient *http.Client, serviceName string) (*Client, error) {
}, nil
}

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

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

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

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

var records []*RR
var records []RR
for _, zone := range apiResponse.Data.Zone {
records = append(records, zone.RR...)
}
Expand All @@ -115,7 +129,7 @@ func (c *Client) GetRecords(fqdn string) ([]*RR, error) {
}

func (c *Client) AddTxtRecord(zoneName string, name string, content string, ttl int) (*Response, error) {
request := &Request{RRList: &RrList{RR: []*RR{{
request := &Request{RRList: &RrList{RR: []RR{{
Name: name,
TTL: strconv.Itoa(ttl),
Type: "TXT",
Expand Down Expand Up @@ -155,6 +169,9 @@ func (c *Client) addRecords(zoneName string, request *Request) (*Response, error
return nil, err
}

// PUT https://api.nic.ru/dns-master/services/<service_id>/zones/<zone_name>/records
// PUT https://api.nic.ru/dns-master/services/TESTSERVICE/zones/TEST.RU/records

req, err := http.NewRequest(http.MethodPut, endpoint.String(), body)
if err != nil {
return nil, err
Expand All @@ -168,15 +185,23 @@ func (c *Client) do(req *http.Request) (*Response, error) {
if err != nil {
return nil, err
}

defer func() { _ = resp.Body.Close() }()

apiResponse := &Response{}

err = xml.NewDecoder(resp.Body).Decode(apiResponse)
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

decoder := xml.NewTokenDecoder(Trimmer{decoder: xml.NewDecoder(bytes.NewReader(raw))})

err = decoder.Decode(apiResponse)
if err != nil {
return nil, fmt.Errorf("[status code=%d] %s", resp.StatusCode, string(raw))
}

if apiResponse.Status != successStatus {
return nil, apiResponse.Errors.Error
}
Expand Down

0 comments on commit 4f9e9d4

Please sign in to comment.