-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DNS provider for Yandex 360 (#1975)
- Loading branch information
Showing
16 changed files
with
739 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
title: "Yandex 360" | ||
date: 2019-03-03T16:39:46+01:00 | ||
draft: false | ||
slug: yandex360 | ||
dnsprovider: | ||
since: "v4.14.0" | ||
code: "yandex360" | ||
url: "https://360.yandex.ru" | ||
--- | ||
|
||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
<!-- providers/dns/yandex360/yandex360.toml --> | ||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
|
||
|
||
Configuration for [Yandex 360](https://360.yandex.ru). | ||
|
||
|
||
<!--more--> | ||
|
||
- Code: `yandex360` | ||
- Since: v4.14.0 | ||
|
||
|
||
Here is an example bash command using the Yandex 360 provider: | ||
|
||
```bash | ||
YANDEX360_OAUTH_TOKEN=<your OAuth Token> \ | ||
YANDEX360_ORG_ID=<your organization ID> \ | ||
lego --email you@example.com --dns yandex360 --domains my.example.org run | ||
``` | ||
|
||
|
||
|
||
|
||
## Credentials | ||
|
||
| Environment Variable Name | Description | | ||
|-----------------------|-------------| | ||
| `YANDEX360_OAUTH_TOKEN` | The OAuth Token | | ||
| `YANDEX360_ORG_ID` | The organization ID | | ||
|
||
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" >}}). | ||
|
||
|
||
## Additional Configuration | ||
|
||
| Environment Variable Name | Description | | ||
|--------------------------------|-------------| | ||
| `YANDEX360_HTTP_TIMEOUT` | API request timeout | | ||
| `YANDEX360_POLLING_INTERVAL` | Time between DNS propagation check | | ||
| `YANDEX360_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation | | ||
| `YANDEX360_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" >}}). | ||
|
||
|
||
|
||
|
||
## More information | ||
|
||
- [API documentation](https://yandex.ru/dev/api360/doc/ref/DomainDNSService.html) | ||
|
||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
<!-- providers/dns/yandex360/yandex360.toml --> | ||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/go-acme/lego/v4/providers/dns/internal/errutils" | ||
) | ||
|
||
const defaultBaseURL = "https://api360.yandex.net/" | ||
|
||
type Client struct { | ||
oauthToken string | ||
orgID int64 | ||
|
||
baseURL *url.URL | ||
HTTPClient *http.Client | ||
} | ||
|
||
func NewClient(oauthToken string, orgID int64) (*Client, error) { | ||
if oauthToken == "" { | ||
return nil, errors.New("OAuth token is required") | ||
} | ||
|
||
if orgID == 0 { | ||
return nil, errors.New("orgID is required") | ||
} | ||
|
||
baseURL, _ := url.Parse(defaultBaseURL) | ||
|
||
return &Client{ | ||
oauthToken: oauthToken, | ||
orgID: orgID, | ||
baseURL: baseURL, | ||
HTTPClient: &http.Client{Timeout: 10 * time.Second}, | ||
}, nil | ||
} | ||
|
||
// AddRecord Adds a DNS record. | ||
// POST https://api30.yandex.net/directory/v1/org/{orgId}/domains/{domain}/dns | ||
// https://yandex.ru/dev/api360/doc/ref/DomainDNSService/DomainDNSService_Create.html | ||
func (c Client) AddRecord(ctx context.Context, domain string, record Record) (*Record, error) { | ||
endpoint := c.baseURL.JoinPath("directory", "v1", "org", strconv.FormatInt(c.orgID, 10), "domains", domain, "dns") | ||
|
||
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var newRecord Record | ||
|
||
err = c.do(req, &newRecord) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &newRecord, nil | ||
} | ||
|
||
// DeleteRecord Deletes a DNS record. | ||
// DELETE https://api360.yandex.net/directory/v1/org/{orgId}/domains/{domain}/dns/{recordId} | ||
// https://yandex.ru/dev/api360/doc/ref/DomainDNSService/DomainDNSService_Delete.html | ||
func (c Client) DeleteRecord(ctx context.Context, domain string, recordID int64) error { | ||
endpoint := c.baseURL.JoinPath("directory", "v1", "org", strconv.FormatInt(c.orgID, 10), "domains", domain, "dns", strconv.FormatInt(recordID, 10)) | ||
|
||
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.do(req, nil) | ||
} | ||
|
||
func (c Client) do(req *http.Request, result any) error { | ||
req.Header.Set("Authorization", "OAuth "+c.oauthToken) | ||
|
||
resp, err := c.HTTPClient.Do(req) | ||
if err != nil { | ||
return errutils.NewHTTPDoError(req, err) | ||
} | ||
|
||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if resp.StatusCode/100 != 2 { | ||
return parseError(req, resp) | ||
} | ||
|
||
if result == nil { | ||
return nil | ||
} | ||
|
||
raw, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return errutils.NewReadResponseError(req, resp.StatusCode, err) | ||
} | ||
|
||
err = json.Unmarshal(raw, result) | ||
if err != nil { | ||
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) { | ||
buf := new(bytes.Buffer) | ||
|
||
if payload != nil { | ||
err := json.NewEncoder(buf).Encode(payload) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create request JSON body: %w", err) | ||
} | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create request: %w", err) | ||
} | ||
|
||
req.Header.Set("Accept", "application/json") | ||
|
||
if payload != nil { | ||
req.Header.Set("Content-Type", "application/json") | ||
} | ||
|
||
return req, nil | ||
} | ||
|
||
func parseError(req *http.Request, resp *http.Response) error { | ||
raw, _ := io.ReadAll(resp.Body) | ||
|
||
var apiErr APIError | ||
err := json.Unmarshal(raw, &apiErr) | ||
if err != nil { | ||
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw) | ||
} | ||
|
||
return fmt.Errorf("[status code: %d] %w", resp.StatusCode, apiErr) | ||
} |
Oops, something went wrong.