diff --git a/README.md b/README.md index baf8e960..df8ff104 100644 --- a/README.md +++ b/README.md @@ -61,11 +61,11 @@ Detailed documentation is available [here](https://go-acme.github.io/lego/dns). | [Linode (v4)](https://go-acme.github.io/lego/dns/linode/) | [Liquid Web](https://go-acme.github.io/lego/dns/liquidweb/) | [Loopia](https://go-acme.github.io/lego/dns/loopia/) | [LuaDNS](https://go-acme.github.io/lego/dns/luadns/) | | [Manual](https://go-acme.github.io/lego/dns/manual/) | [MyDNS.jp](https://go-acme.github.io/lego/dns/mydnsjp/) | [MythicBeasts](https://go-acme.github.io/lego/dns/mythicbeasts/) | [Name.com](https://go-acme.github.io/lego/dns/namedotcom/) | | [Namecheap](https://go-acme.github.io/lego/dns/namecheap/) | [Namesilo](https://go-acme.github.io/lego/dns/namesilo/) | [Netcup](https://go-acme.github.io/lego/dns/netcup/) | [Netlify](https://go-acme.github.io/lego/dns/netlify/) | -| [NIFCloud](https://go-acme.github.io/lego/dns/nifcloud/) | [NS1](https://go-acme.github.io/lego/dns/ns1/) | [Open Telekom Cloud](https://go-acme.github.io/lego/dns/otc/) | [Oracle Cloud](https://go-acme.github.io/lego/dns/oraclecloud/) | -| [OVH](https://go-acme.github.io/lego/dns/ovh/) | [PowerDNS](https://go-acme.github.io/lego/dns/pdns/) | [Rackspace](https://go-acme.github.io/lego/dns/rackspace/) | [reg.ru](https://go-acme.github.io/lego/dns/regru/) | -| [RFC2136](https://go-acme.github.io/lego/dns/rfc2136/) | [RimuHosting](https://go-acme.github.io/lego/dns/rimuhosting/) | [Sakura Cloud](https://go-acme.github.io/lego/dns/sakuracloud/) | [Scaleway](https://go-acme.github.io/lego/dns/scaleway/) | -| [Selectel](https://go-acme.github.io/lego/dns/selectel/) | [Servercow](https://go-acme.github.io/lego/dns/servercow/) | [Stackpath](https://go-acme.github.io/lego/dns/stackpath/) | [TransIP](https://go-acme.github.io/lego/dns/transip/) | -| [VegaDNS](https://go-acme.github.io/lego/dns/vegadns/) | [Versio.[nl/eu/uk]](https://go-acme.github.io/lego/dns/versio/) | [Vscale](https://go-acme.github.io/lego/dns/vscale/) | [Vultr](https://go-acme.github.io/lego/dns/vultr/) | -| [Yandex](https://go-acme.github.io/lego/dns/yandex/) | [Zone.ee](https://go-acme.github.io/lego/dns/zoneee/) | [Zonomi](https://go-acme.github.io/lego/dns/zonomi/) | | +| [NIFCloud](https://go-acme.github.io/lego/dns/nifcloud/) | [Njalla](https://go-acme.github.io/lego/dns/njalla/) | [NS1](https://go-acme.github.io/lego/dns/ns1/) | [Open Telekom Cloud](https://go-acme.github.io/lego/dns/otc/) | +| [Oracle Cloud](https://go-acme.github.io/lego/dns/oraclecloud/) | [OVH](https://go-acme.github.io/lego/dns/ovh/) | [PowerDNS](https://go-acme.github.io/lego/dns/pdns/) | [Rackspace](https://go-acme.github.io/lego/dns/rackspace/) | +| [reg.ru](https://go-acme.github.io/lego/dns/regru/) | [RFC2136](https://go-acme.github.io/lego/dns/rfc2136/) | [RimuHosting](https://go-acme.github.io/lego/dns/rimuhosting/) | [Sakura Cloud](https://go-acme.github.io/lego/dns/sakuracloud/) | +| [Scaleway](https://go-acme.github.io/lego/dns/scaleway/) | [Selectel](https://go-acme.github.io/lego/dns/selectel/) | [Servercow](https://go-acme.github.io/lego/dns/servercow/) | [Stackpath](https://go-acme.github.io/lego/dns/stackpath/) | +| [TransIP](https://go-acme.github.io/lego/dns/transip/) | [VegaDNS](https://go-acme.github.io/lego/dns/vegadns/) | [Versio.[nl/eu/uk]](https://go-acme.github.io/lego/dns/versio/) | [Vscale](https://go-acme.github.io/lego/dns/vscale/) | +| [Vultr](https://go-acme.github.io/lego/dns/vultr/) | [Yandex](https://go-acme.github.io/lego/dns/yandex/) | [Zone.ee](https://go-acme.github.io/lego/dns/zoneee/) | [Zonomi](https://go-acme.github.io/lego/dns/zonomi/) | diff --git a/cmd/zz_gen_cmd_dnshelp.go b/cmd/zz_gen_cmd_dnshelp.go index 634d8671..5bb6b779 100644 --- a/cmd/zz_gen_cmd_dnshelp.go +++ b/cmd/zz_gen_cmd_dnshelp.go @@ -73,6 +73,7 @@ func allDNSCodes() string { "netcup", "netlify", "nifcloud", + "njalla", "ns1", "oraclecloud", "otc", @@ -1345,6 +1346,26 @@ func displayDNSHelp(name string) error { ew.writeln() ew.writeln(`More information: https://go-acme.github.io/lego/dns/nifcloud`) + case "njalla": + // generated from: providers/dns/njalla/njalla.toml + ew.writeln(`Configuration for Njalla.`) + ew.writeln(`Code: 'njalla'`) + ew.writeln(`Since: 'v4.3.0'`) + ew.writeln() + + ew.writeln(`Credentials:`) + ew.writeln(` - "NJALLA_TOKEN": API token`) + ew.writeln() + + ew.writeln(`Additional Configuration:`) + ew.writeln(` - "NJALLA_HTTP_TIMEOUT": API request timeout`) + ew.writeln(` - "NJALLA_POLLING_INTERVAL": Time between DNS propagation check`) + ew.writeln(` - "NJALLA_PROPAGATION_TIMEOUT": Maximum waiting time for DNS propagation`) + ew.writeln(` - "NJALLA_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/njalla`) + case "ns1": // generated from: providers/dns/ns1/ns1.toml ew.writeln(`Configuration for NS1.`) diff --git a/docs/content/dns/zz_gen_njalla.md b/docs/content/dns/zz_gen_njalla.md new file mode 100644 index 00000000..c20a4df2 --- /dev/null +++ b/docs/content/dns/zz_gen_njalla.md @@ -0,0 +1,62 @@ +--- +title: "Njalla" +date: 2019-03-03T16:39:46+01:00 +draft: false +slug: njalla +--- + + + + + +Since: v4.3.0 + +Configuration for [Njalla](https://njal.la). + + + + +- Code: `njalla` + +Here is an example bash command using the Njalla provider: + +```bash +NJALLA_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxx \ +lego --email myemail@example.com --dns njalla --domains my.example.org run +``` + + + + +## Credentials + +| Environment Variable Name | Description | +|-----------------------|-------------| +| `NJALLA_TOKEN` | API token | + +The environment variable names can be suffixed by `_FILE` to reference a file instead of a value. +More information [here](/lego/dns/#configuration-and-credentials). + + +## Additional Configuration + +| Environment Variable Name | Description | +|--------------------------------|-------------| +| `NJALLA_HTTP_TIMEOUT` | API request timeout | +| `NJALLA_POLLING_INTERVAL` | Time between DNS propagation check | +| `NJALLA_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation | +| `NJALLA_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](/lego/dns/#configuration-and-credentials). + + + + +## More information + +- [API documentation](https://njal.la/api/) + + + + diff --git a/providers/dns/dns_providers.go b/providers/dns/dns_providers.go index 1ba6edf3..b6b8fb50 100644 --- a/providers/dns/dns_providers.go +++ b/providers/dns/dns_providers.go @@ -64,6 +64,7 @@ import ( "github.com/go-acme/lego/v4/providers/dns/netcup" "github.com/go-acme/lego/v4/providers/dns/netlify" "github.com/go-acme/lego/v4/providers/dns/nifcloud" + "github.com/go-acme/lego/v4/providers/dns/njalla" "github.com/go-acme/lego/v4/providers/dns/ns1" "github.com/go-acme/lego/v4/providers/dns/oraclecloud" "github.com/go-acme/lego/v4/providers/dns/otc" @@ -212,6 +213,8 @@ func NewDNSChallengeProviderByName(name string) (challenge.Provider, error) { return netlify.NewDNSProvider() case "nifcloud": return nifcloud.NewDNSProvider() + case "njalla": + return njalla.NewDNSProvider() case "ns1": return ns1.NewDNSProvider() case "oraclecloud": diff --git a/providers/dns/njalla/internal/client.go b/providers/dns/njalla/internal/client.go new file mode 100644 index 00000000..8fc69129 --- /dev/null +++ b/providers/dns/njalla/internal/client.go @@ -0,0 +1,137 @@ +package internal + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "time" +) + +const apiEndpoint = "https://njal.la/api/1/" + +// Client is a Njalla API client. +type Client struct { + HTTPClient *http.Client + apiEndpoint string + token string +} + +// NewClient creates a new Client. +func NewClient(token string) *Client { + return &Client{ + HTTPClient: &http.Client{Timeout: 5 * time.Second}, + apiEndpoint: apiEndpoint, + token: token, + } +} + +// AddRecord adds a record. +func (c *Client) AddRecord(record Record) (*Record, error) { + data := APIRequest{ + Method: "add-record", + Params: record, + } + + result, err := c.do(data) + if err != nil { + return nil, err + } + + var rcd Record + err = json.Unmarshal(result, &rcd) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal response result: %w", err) + } + + return &rcd, nil +} + +// RemoveRecord removes a record. +func (c *Client) RemoveRecord(id int, domain string) error { + data := APIRequest{ + Method: "remove-record", + Params: Record{ + ID: id, + Domain: domain, + }, + } + + _, err := c.do(data) + if err != nil { + return err + } + + return nil +} + +// ListRecords list the records for one domain. +func (c *Client) ListRecords(domain string) ([]Record, error) { + data := APIRequest{ + Method: "list-records", + Params: Record{ + Domain: domain, + }, + } + + result, err := c.do(data) + if err != nil { + return nil, err + } + + var rcds Records + err = json.Unmarshal(result, &rcds) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal response result: %w", err) + } + + return rcds.Records, nil +} + +func (c *Client) do(data APIRequest) (json.RawMessage, error) { + req, err := c.createRequest(data) + if err != nil { + return nil, err + } + + resp, err := c.HTTPClient.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to perform request: %w", err) + } + + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("unexpected error: %d", resp.StatusCode) + } + + apiResponse := APIResponse{} + err = json.NewDecoder(resp.Body).Decode(&apiResponse) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal response: %w", err) + } + + if apiResponse.Error != nil { + return nil, apiResponse.Error + } + + return apiResponse.Result, nil +} + +func (c *Client) createRequest(data APIRequest) (*http.Request, error) { + reqBody, err := json.Marshal(data) + if err != nil { + return nil, fmt.Errorf("failed to marshall request body: %w", err) + } + + req, err := http.NewRequest(http.MethodPost, c.apiEndpoint, bytes.NewReader(reqBody)) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + req.Header.Set("Accept", "application/json") + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Njalla "+c.token) + + return req, nil +} diff --git a/providers/dns/njalla/internal/client_test.go b/providers/dns/njalla/internal/client_test.go new file mode 100644 index 00000000..1a8bad79 --- /dev/null +++ b/providers/dns/njalla/internal/client_test.go @@ -0,0 +1,230 @@ +package internal + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func setup(t *testing.T, handler func(http.ResponseWriter, *http.Request)) *Client { + t.Helper() + + mux := http.NewServeMux() + server := httptest.NewServer(mux) + t.Cleanup(server.Close) + + mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { + if req.Method != http.MethodPost { + http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed) + return + } + + token := req.Header.Get("Authorization") + if token != "Njalla secret" { + _, _ = rw.Write([]byte(`{"jsonrpc":"2.0", "Error": {"code": 403, "message": "Invalid token."}}`)) + return + } + + if handler != nil { + handler(rw, req) + } else { + _, _ = rw.Write([]byte(`{"jsonrpc":"2.0"}`)) + } + }) + + client := NewClient("secret") + client.apiEndpoint = server.URL + + return client +} + +func TestClient_AddRecord(t *testing.T) { + client := setup(t, func(rw http.ResponseWriter, req *http.Request) { + apiReq := struct { + Method string `json:"method"` + Params Record `json:"params"` + }{} + + err := json.NewDecoder(req.Body).Decode(&apiReq) + if err != nil { + http.Error(rw, "failed to marshal test request body", http.StatusInternalServerError) + return + } + + apiReq.Params.ID = 123 + + resp := map[string]interface{}{ + "jsonrpc": "2.0", + "id": "897", + "result": apiReq.Params, + } + + err = json.NewEncoder(rw).Encode(resp) + if err != nil { + http.Error(rw, "failed to marshal test response", http.StatusInternalServerError) + return + } + }) + + record := Record{ + Content: "foobar", + Domain: "test", + Name: "example.com", + TTL: 300, + Type: "TXT", + } + + result, err := client.AddRecord(record) + require.NoError(t, err) + + expected := &Record{ + ID: 123, + Content: "foobar", + Domain: "test", + Name: "example.com", + TTL: 300, + Type: "TXT", + } + assert.Equal(t, expected, result) +} + +func TestClient_AddRecord_error(t *testing.T) { + client := setup(t, nil) + client.token = "invalid" + + record := Record{ + Content: "test", + Domain: "test01", + Name: "example.com", + TTL: 300, + Type: "TXT", + } + + result, err := client.AddRecord(record) + require.Error(t, err) + + assert.Nil(t, result) +} + +func TestClient_ListRecords(t *testing.T) { + client := setup(t, func(rw http.ResponseWriter, req *http.Request) { + apiReq := struct { + Method string `json:"method"` + Params Record `json:"params"` + }{} + + err := json.NewDecoder(req.Body).Decode(&apiReq) + if err != nil { + http.Error(rw, "failed to marshal test request body", http.StatusInternalServerError) + return + } + + resp := map[string]interface{}{ + "jsonrpc": "2.0", + "id": "897", + "result": Records{ + Records: []Record{ + { + ID: 1, + Domain: apiReq.Params.Domain, + Content: "test", + Name: "test01", + TTL: 300, + Type: "TXT", + }, + { + ID: 2, + Domain: apiReq.Params.Domain, + Content: "txtTxt", + Name: "test02", + TTL: 120, + Type: "TXT", + }, + }, + }, + } + + err = json.NewEncoder(rw).Encode(resp) + if err != nil { + http.Error(rw, "failed to marshal test response", http.StatusInternalServerError) + return + } + }) + + records, err := client.ListRecords("example.com") + require.NoError(t, err) + + expected := []Record{ + { + ID: 1, + Domain: "example.com", + Content: "test", + Name: "test01", + TTL: 300, + Type: "TXT", + }, + { + ID: 2, + Domain: "example.com", + Content: "txtTxt", + Name: "test02", + TTL: 120, + Type: "TXT", + }, + } + + assert.Equal(t, expected, records) +} + +func TestClient_ListRecords_error(t *testing.T) { + client := setup(t, nil) + client.token = "invalid" + + records, err := client.ListRecords("example.com") + require.Error(t, err) + + assert.Empty(t, records) +} + +func TestClient_RemoveRecord(t *testing.T) { + client := setup(t, func(rw http.ResponseWriter, req *http.Request) { + apiReq := struct { + Method string `json:"method"` + Params Record `json:"params"` + }{} + + err := json.NewDecoder(req.Body).Decode(&apiReq) + if err != nil { + http.Error(rw, "failed to marshal test request body", http.StatusInternalServerError) + return + } + + if apiReq.Params.ID == 0 { + _, _ = rw.Write([]byte(`{"jsonrpc":"2.0", "Error": {"code": 400, "message": ""missing ID"}}`)) + return + } + + if apiReq.Params.Domain == "" { + _, _ = rw.Write([]byte(`{"jsonrpc":"2.0", "Error": {"code": 400, "message": ""missing domain"}}`)) + return + } + + _, _ = rw.Write([]byte(`{"jsonrpc":"2.0"}`)) + }) + + err := client.RemoveRecord(123, "example.com") + require.NoError(t, err) +} + +func TestClient_RemoveRecord_error(t *testing.T) { + client := setup(t, nil) + client.token = "invalid" + + err := client.RemoveRecord(123, "example.com") + require.Error(t, err) +} diff --git a/providers/dns/njalla/internal/types.go b/providers/dns/njalla/internal/types.go new file mode 100644 index 00000000..2864649f --- /dev/null +++ b/providers/dns/njalla/internal/types.go @@ -0,0 +1,45 @@ +package internal + +import ( + "encoding/json" + "fmt" +) + +// APIRequest represents an API request body. +type APIRequest struct { + Method string `json:"method"` + Params interface{} `json:"params"` +} + +// APIResponse represents an API response body. +type APIResponse struct { + ID string `json:"id"` + RPC string `json:"jsonrpc"` + Error *APIError `json:"error,omitempty"` + Result json.RawMessage `json:"result,omitempty"` +} + +// APIError is an API error. +type APIError struct { + Code int + Message string +} + +func (a APIError) Error() string { + return fmt.Sprintf("code: %d, message: %s", a.Code, a.Message) +} + +// Record is a DNS record. +type Record struct { + ID int `json:"id,omitempty"` + Content string `json:"content,omitempty"` + Domain string `json:"domain,omitempty"` + Name string `json:"name,omitempty"` + TTL int `json:"ttl,omitempty"` + Type string `json:"type,omitempty"` +} + +// Records is a list of DNS records. +type Records struct { + Records []Record `json:"records,omitempty"` +} diff --git a/providers/dns/njalla/njalla.go b/providers/dns/njalla/njalla.go new file mode 100644 index 00000000..4a394a42 --- /dev/null +++ b/providers/dns/njalla/njalla.go @@ -0,0 +1,175 @@ +// Package njalla implements a DNS provider for solving the DNS-01 challenge using Njalla. +package njalla + +import ( + "errors" + "fmt" + "net/http" + "sync" + "time" + + "github.com/go-acme/lego/v4/challenge/dns01" + "github.com/go-acme/lego/v4/platform/config/env" + "github.com/go-acme/lego/v4/providers/dns/njalla/internal" + "github.com/miekg/dns" +) + +// Environment variables names. +const ( + envNamespace = "NJALLA_" + + EnvToken = envNamespace + "TOKEN" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + +// Config is used to configure the creation of the DNSProvider. +type Config struct { + Token string + PropagationTimeout time.Duration + PollingInterval time.Duration + TTL int + HTTPClient *http.Client +} + +// NewDefaultConfig returns a default configuration for the DNSProvider. +func NewDefaultConfig() *Config { + return &Config{ + TTL: env.GetOrDefaultInt(EnvTTL, 300), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), + HTTPClient: &http.Client{ + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), + }, + } +} + +// DNSProvider implements the challenge.Provider interface. +type DNSProvider struct { + config *Config + client *internal.Client + + recordIDs map[string]int + recordIDsMu sync.Mutex +} + +// NewDNSProvider returns a DNSProvider instance configured for Njalla. +// Credentials must be passed in the environment variable: NJALLA_TOKEN. +func NewDNSProvider() (*DNSProvider, error) { + values, err := env.Get(EnvToken) + if err != nil { + return nil, fmt.Errorf("njalla: %w", err) + } + + config := NewDefaultConfig() + config.Token = values[EnvToken] + + return NewDNSProviderConfig(config) +} + +// NewDNSProviderConfig return a DNSProvider instance configured for Njalla. +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { + if config == nil { + return nil, errors.New("njalla: the configuration of the DNS provider is nil") + } + + if config.Token == "" { + return nil, errors.New("njalla: missing credentials") + } + + client := internal.NewClient(config.Token) + + if config.HTTPClient != nil { + client.HTTPClient = config.HTTPClient + } + + return &DNSProvider{ + config: config, + client: client, + recordIDs: make(map[string]int), + }, nil +} + +// Timeout returns the timeout and interval to use when checking for DNS propagation. +// Adjusting here to cope with spikes in propagation times. +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { + return d.config.PropagationTimeout, d.config.PollingInterval +} + +// Present creates a TXT record using the specified parameters. +func (d *DNSProvider) Present(domain, token, keyAuth string) error { + fqdn, value := dns01.GetRecord(domain, keyAuth) + + rootDomain, subDomain, err := splitDomain(fqdn) + if err != nil { + return fmt.Errorf("njalla: %w", err) + } + + record := internal.Record{ + Name: subDomain, // TODO need to be tested + Domain: dns01.UnFqdn(rootDomain), // TODO need to be tested + Content: value, + TTL: d.config.TTL, + Type: "TXT", + } + + resp, err := d.client.AddRecord(record) + if err != nil { + return fmt.Errorf("njalla: failed to add record: %w", err) + } + + d.recordIDsMu.Lock() + d.recordIDs[token] = resp.ID + d.recordIDsMu.Unlock() + + return nil +} + +// CleanUp removes the TXT record matching the specified parameters. +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { + fqdn, _ := dns01.GetRecord(domain, keyAuth) + + rootDomain, _, err := splitDomain(fqdn) + if err != nil { + return fmt.Errorf("njalla: %w", err) + } + + // gets the record's unique ID from when we created it + d.recordIDsMu.Lock() + recordID, ok := d.recordIDs[token] + d.recordIDsMu.Unlock() + if !ok { + return fmt.Errorf("njalla: unknown record ID for '%s' '%s'", fqdn, token) + } + + err = d.client.RemoveRecord(recordID, dns01.UnFqdn(rootDomain)) + if err != nil { + return fmt.Errorf("njalla: failed to delete TXT records: fqdn=%s, recordID=%d: %w", fqdn, recordID, err) + } + + // deletes record ID from map + d.recordIDsMu.Lock() + delete(d.recordIDs, token) + d.recordIDsMu.Unlock() + + return nil +} + +func splitDomain(full string) (string, string, error) { + split := dns.Split(full) + if len(split) < 2 { + return "", "", fmt.Errorf("unsupported domain: %s", full) + } + + if len(split) == 2 { + return full, "", nil + } + + domain := full[split[len(split)-2]:] + subDomain := full[:split[len(split)-2]-1] + + return domain, subDomain, nil +} diff --git a/providers/dns/njalla/njalla.toml b/providers/dns/njalla/njalla.toml new file mode 100644 index 00000000..c2ba14da --- /dev/null +++ b/providers/dns/njalla/njalla.toml @@ -0,0 +1,22 @@ +Name = "Njalla" +Description = '''''' +URL = "https://njal.la" +Code = "njalla" +Since = "v4.3.0" + +Example = ''' +NJALLA_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxx \ +lego --email myemail@example.com --dns njalla --domains my.example.org run +''' + +[Configuration] + [Configuration.Credentials] + NJALLA_TOKEN = "API token" + [Configuration.Additional] + NJALLA_POLLING_INTERVAL = "Time between DNS propagation check" + NJALLA_PROPAGATION_TIMEOUT = "Maximum waiting time for DNS propagation" + NJALLA_TTL = "The TTL of the TXT record used for the DNS challenge" + NJALLA_HTTP_TIMEOUT = "API request timeout" + +[Links] + API = "https://njal.la/api/" diff --git a/providers/dns/njalla/njalla_test.go b/providers/dns/njalla/njalla_test.go new file mode 100644 index 00000000..37affbe7 --- /dev/null +++ b/providers/dns/njalla/njalla_test.go @@ -0,0 +1,118 @@ +package njalla + +import ( + "testing" + "time" + + "github.com/go-acme/lego/v4/platform/tester" + "github.com/stretchr/testify/require" +) + +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvToken).WithDomain(envDomain) + +func TestNewDNSProvider(t *testing.T) { + testCases := []struct { + desc string + envVars map[string]string + expected string + }{ + { + desc: "success", + envVars: map[string]string{ + EnvToken: "123", + }, + }, + { + desc: "missing credentials", + envVars: map[string]string{ + EnvToken: "", + }, + expected: "njalla: some credentials information are missing: NJALLA_TOKEN", + }, + } + + for _, test := range testCases { + t.Run(test.desc, func(t *testing.T) { + defer envTest.RestoreEnv() + envTest.ClearEnv() + + envTest.Apply(test.envVars) + + p, err := NewDNSProvider() + + if len(test.expected) == 0 { + require.NoError(t, err) + require.NotNil(t, p) + require.NotNil(t, p.config) + require.NotNil(t, p.client) + } else { + require.EqualError(t, err, test.expected) + } + }) + } +} + +func TestNewDNSProviderConfig(t *testing.T) { + testCases := []struct { + desc string + token string + expected string + }{ + { + desc: "success", + token: "123", + }, + { + desc: "missing credentials", + expected: "njalla: missing credentials", + }, + } + + for _, test := range testCases { + t.Run(test.desc, func(t *testing.T) { + config := NewDefaultConfig() + config.Token = test.token + + p, err := NewDNSProviderConfig(config) + + if len(test.expected) == 0 { + require.NoError(t, err) + require.NotNil(t, p) + require.NotNil(t, p.config) + require.NotNil(t, p.client) + } else { + require.EqualError(t, err, test.expected) + } + }) + } +} + +func TestLivePresent(t *testing.T) { + if !envTest.IsLiveTest() { + t.Skip("skipping live test") + } + + envTest.RestoreEnv() + provider, err := NewDNSProvider() + require.NoError(t, err) + + err = provider.Present(envTest.GetDomain(), "", "123d==") + require.NoError(t, err) +} + +func TestLiveCleanUp(t *testing.T) { + if !envTest.IsLiveTest() { + t.Skip("skipping live test") + } + + envTest.RestoreEnv() + provider, err := NewDNSProvider() + require.NoError(t, err) + + time.Sleep(1 * time.Second) + + err = provider.CleanUp(envTest.GetDomain(), "", "123d==") + require.NoError(t, err) +}