-
-
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 Webnames (#2077)
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
- Loading branch information
1 parent
68dc83a
commit 3ba40ff
Showing
13 changed files
with
649 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,72 @@ | ||
--- | ||
title: "Webnames" | ||
date: 2019-03-03T16:39:46+01:00 | ||
draft: false | ||
slug: webnames | ||
dnsprovider: | ||
since: "v4.15.0" | ||
code: "webnames" | ||
url: "https://www.webnames.ru/" | ||
--- | ||
|
||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
<!-- providers/dns/webnames/webnames.toml --> | ||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
|
||
|
||
Configuration for [Webnames](https://www.webnames.ru/). | ||
|
||
|
||
<!--more--> | ||
|
||
- Code: `webnames` | ||
- Since: v4.15.0 | ||
|
||
|
||
Here is an example bash command using the Webnames provider: | ||
|
||
```bash | ||
WEBNAMES_API_KEY=xxxxxx \ | ||
lego --email you@example.com --dns webnames --domains my.example.org run | ||
``` | ||
|
||
|
||
|
||
|
||
## Credentials | ||
|
||
| Environment Variable Name | Description | | ||
|-----------------------|-------------| | ||
| `WEBNAMES_API_KEY` | Domain API key | | ||
|
||
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 | | ||
|--------------------------------|-------------| | ||
| `WEBNAMES_HTTP_TIMEOUT` | API request timeout | | ||
| `WEBNAMES_POLLING_INTERVAL` | Time between DNS propagation check | | ||
| `WEBNAMES_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation | | ||
| `WEBNAMES_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" >}}). | ||
|
||
## API Key | ||
|
||
To obtain the key, you need to change the DNS server to `*.nameself.com`: Personal account / My domains and services / Select the required domain / DNS servers | ||
|
||
The API key can be found: Personal account / My domains and services / Select the required domain / Zone management / acme.sh or certbot settings | ||
|
||
|
||
|
||
## More information | ||
|
||
- [API documentation](https://github.com/regtime-ltd/certbot-dns-webnames) | ||
|
||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
<!-- providers/dns/webnames/webnames.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-acme/lego/v4/providers/dns/internal/errutils" | ||
) | ||
|
||
const defaultBaseURL = "https://www.webnames.ru/scripts/json_domain_zone_manager.pl" | ||
|
||
// Client the Webnames API client. | ||
type Client struct { | ||
apiKey string | ||
|
||
baseURL string | ||
HTTPClient *http.Client | ||
} | ||
|
||
// NewClient Creates a new Client. | ||
func NewClient(apiKey string) *Client { | ||
return &Client{ | ||
apiKey: apiKey, | ||
baseURL: defaultBaseURL, | ||
HTTPClient: &http.Client{Timeout: 10 * time.Second}, | ||
} | ||
} | ||
|
||
// AddTXTRecord adds a TXT record. | ||
// Inspired by https://github.com/regtime-ltd/certbot-dns-webnames/blob/master/authenticator.sh | ||
func (c *Client) AddTXTRecord(ctx context.Context, domain, subDomain, value string) error { | ||
data := url.Values{} | ||
data.Set("domain", domain) | ||
data.Set("type", "TXT") | ||
data.Set("record", subDomain+":"+value) | ||
data.Set("action", "add") | ||
|
||
return c.doRequest(ctx, data) | ||
} | ||
|
||
// RemoveTXTRecord removes a TXT record. | ||
// Inspired by https://github.com/regtime-ltd/certbot-dns-webnames/blob/master/cleanup.sh | ||
func (c *Client) RemoveTXTRecord(ctx context.Context, domain, subDomain, value string) error { | ||
data := url.Values{} | ||
data.Set("domain", domain) | ||
data.Set("type", "TXT") | ||
data.Set("record", subDomain+":"+value) | ||
data.Set("action", "delete") | ||
|
||
return c.doRequest(ctx, data) | ||
} | ||
|
||
func (c *Client) doRequest(ctx context.Context, data url.Values) error { | ||
data.Set("apikey", c.apiKey) | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL, strings.NewReader(data.Encode())) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
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 errutils.NewUnexpectedResponseStatusCodeError(req, resp) | ||
} | ||
|
||
raw, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return errutils.NewReadResponseError(req, resp.StatusCode, err) | ||
} | ||
|
||
var r APIResponse | ||
err = json.Unmarshal(raw, &r) | ||
if err != nil { | ||
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err) | ||
} | ||
|
||
if r.Result == "OK" { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("%s: %s", r.Result, r.Details) | ||
} |
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,155 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func setupTest(t *testing.T, filename string, expectedParams url.Values) *Client { | ||
t.Helper() | ||
|
||
mux := http.NewServeMux() | ||
|
||
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { | ||
if req.Method != http.MethodPost { | ||
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
if req.Header.Get("Content-Type") != "application/x-www-form-urlencoded" { | ||
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err := req.ParseForm() | ||
if err != nil { | ||
http.Error(rw, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
for k, v := range expectedParams { | ||
val := req.PostForm.Get(k) | ||
if len(v) == 0 { | ||
http.Error(rw, fmt.Sprintf("%s: no value", k), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if val != v[0] { | ||
http.Error(rw, fmt.Sprintf("%s: invalid value: %s != %s", k, val, v[0]), http.StatusBadRequest) | ||
return | ||
} | ||
} | ||
|
||
file, err := os.Open(path.Join("fixtures", filename)) | ||
if err != nil { | ||
http.Error(rw, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
defer func() { _ = file.Close() }() | ||
|
||
_, err = io.Copy(rw, file) | ||
if err != nil { | ||
http.Error(rw, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
}) | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
client := NewClient("secret") | ||
client.baseURL = server.URL | ||
client.HTTPClient = server.Client() | ||
|
||
return client | ||
} | ||
|
||
func TestClient_AddTXTRecord(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
filename string | ||
require require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
desc: "ok", | ||
filename: "ok.json", | ||
require: require.NoError, | ||
}, | ||
{ | ||
desc: "error", | ||
filename: "error.json", | ||
require: require.Error, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
test := test | ||
t.Run(test.desc, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
data := url.Values{} | ||
data.Set("domain", "example.com") | ||
data.Set("type", "TXT") | ||
data.Set("record", "foo:txtTXTtxt") | ||
data.Set("action", "add") | ||
|
||
client := setupTest(t, test.filename, data) | ||
|
||
domain := "example.com" | ||
subDomain := "foo" | ||
content := "txtTXTtxt" | ||
|
||
err := client.AddTXTRecord(context.Background(), domain, subDomain, content) | ||
test.require(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestClient_RemoveTxtRecord(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
filename string | ||
require require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
desc: "ok", | ||
filename: "ok.json", | ||
require: require.NoError, | ||
}, | ||
{ | ||
desc: "error", | ||
filename: "error.json", | ||
require: require.Error, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
test := test | ||
t.Run(test.desc, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
data := url.Values{} | ||
data.Set("domain", "example.com") | ||
data.Set("type", "TXT") | ||
data.Set("record", "foo:txtTXTtxt") | ||
data.Set("action", "delete") | ||
|
||
client := setupTest(t, test.filename, data) | ||
|
||
domain := "example.com" | ||
subDomain := "foo" | ||
content := "txtTXTtxt" | ||
|
||
err := client.RemoveTXTRecord(context.Background(), domain, subDomain, content) | ||
test.require(t, err) | ||
}) | ||
} | ||
} |
Oops, something went wrong.