njalla

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 14 Imported by: 6 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Njalla for libdns

Go Reference

Manage Njalla DNS records through the libdns interfaces. Implements RecordGetter, RecordAppender, RecordSetter, RecordDeleter, and ZoneLister.

Install

go get github.com/libdns/njalla

Authentication

Create a token under Settings → API:

provider := njalla.Provider{APIToken: "your-njalla-api-token"}

Grant the token the methods you call:

libdns method Njalla API methods
GetRecords list-records
AppendRecords add-record
SetRecords list-records, add-record, edit-record, remove-record
DeleteRecords remove-record
ListZones list-domains

Njalla's token editor offers an ACME option for the DNS-01 challenge. It grants the four record methods, scoped to TXT records under _acme-challenge. That scope also narrows list-records to the same prefix, so GetRecords returns nothing outside it. DeleteRecords never lists, so challenge cleanup keeps working.

Usage

package main

import (
	"context"
	"fmt"
	"net/netip"
	"time"

	"github.com/libdns/libdns"
	"github.com/libdns/njalla"
)

func main() {
	provider := njalla.Provider{APIToken: "your-njalla-api-token"}
	ctx := context.Background()
	zone := "example.com."

	added, err := provider.AppendRecords(ctx, zone, []libdns.Record{
		libdns.Address{
			Name: "test",
			IP:   netip.MustParseAddr("192.0.2.1"),
			TTL:  time.Hour,
		},
	})
	if err != nil {
		fmt.Printf("adding record: %v\n", err)
		return
	}

	records, err := provider.GetRecords(ctx, zone)
	if err != nil {
		fmt.Printf("getting records: %v\n", err)
		return
	}
	fmt.Printf("%d records in %s\n", len(records), zone)

	if _, err := provider.DeleteRecords(ctx, zone, added); err != nil {
		fmt.Printf("deleting records: %v\n", err)
	}
}

Record types

A, AAAA, CAA, CNAME, MX, NS, TXT, and HTTPS map to their libdns types. Anything else, including Njalla's Redirect and Dynamic pseudo-types, arrives as libdns.RR. You can pass any type as input.

Limitations

Njalla refuses these records. The provider checks each one before sending the request, so the error names the cause.

Record Constraint
SRV Zone apex only
SVCB Unsupported
MX Preference 0
TXT Empty values, and values containing "

SRV. Njalla validates the last two labels of the name, so it takes _sip._tcp and rejects the RFC 2782 form _sip._tcp.voice. Reordering to voice._sip._tcp passes validation and publishes an invalid SRV location, so the provider sends the name unchanged.

SVCB. libdns names a SVCB record _scheme.name, and Njalla rejects underscore labels for this type. HTTPS carries no such prefix and works.

Behaviour

Trailing dots. Njalla stores hostname values as you send them and matches them byte for byte, so the provider passes them through untouched in both directions. Deleting a record tries both spellings.

TXT encoding. Values round trip as sent, including spaces, semicolons, non-ASCII text, and lengths past 255 bytes. The provider adds no escaping or chunking.

TTL. A TTL of 0 leaves the field unset, and Njalla applies 10800 seconds. Pass a sub-second duration to ask for 0, as libdns.RR describes.

Underscore names. Njalla refuses them for A, AAAA, and MX. TXT, CNAME, and SRV accept them.

Atomicity. Njalla has no batch endpoint, so AppendRecords, SetRecords, and DeleteRecords apply one change at a time. When one returns an error, it also returns the records it already changed.

DNSSEC. GetRecords omits DNSSEC records, and SetRecords does not handle them.

Testing

Unit tests need no credentials:

go test ./...

libdnstest/ is a separate module running the shared libdns conformance suite against a real zone. It creates and deletes records, so point it at a zone you can afford to modify.

cd libdnstest
NJALLA_API_TOKEN=your-token NJALLA_TEST_ZONE=example.com. go test -v ./...

The suite skips SRV and SVCB for the reasons under Limitations.

Documentation

Overview

Package njalla implements the libdns interfaces for Njalla (https://njal.la), allowing DNS records to be managed through the Njalla JSON-RPC API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err is a Njalla 404. The remove-record method returns this when no record matched the supplied fields, which the libdns RecordDeleter contract requires us to treat as "nothing to do" rather than as a failure.

Types

type APIError

type APIError struct {
	Code    int
	Message string
}

APIError is returned when Njalla reports a JSON-RPC error. Callers can use errors.As to inspect the code, and IsNotFound to detect the specific "not found" response that remove-record returns when nothing matched.

func (*APIError) Error

func (e *APIError) Error() string

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is the subset of http.Client used by this package. Providing a custom implementation is useful for tests and for callers that need control over transports, proxies, or timeouts.

type Provider

type Provider struct {
	// APIToken is the Njalla API token used for authentication. Tokens are
	// created from the Njalla account settings page.
	//
	// The token needs the add-record, edit-record, remove-record, and
	// list-records methods. A token created with Njalla's "acme" option
	// covers the ACME DNS-01 challenge, but restricts listing to the
	// _acme-challenge prefix.
	APIToken string `json:"api_token,omitempty"`

	// HTTPClient is used for API requests. If nil, a client with a 30 second
	// timeout is used.
	HTTPClient HTTPClient `json:"-"`
	// contains filtered or unexported fields
}

Provider facilitates DNS record manipulation with Njalla.

Record types

A, AAAA, CAA, CNAME, MX, NS, SRV, TXT, HTTPS, and SVCB records are returned as the corresponding concrete libdns types. Any other type, including Njalla's proprietary Redirect and Dynamic pseudo-types, is returned as an opaque libdns.RR. Records of any type may be supplied as input.

Provider-specific behaviour

Values are stored exactly as supplied. Njalla preserves trailing dots on CNAME, NS, MX, and SRV targets rather than normalising them, and matches on them byte-for-byte, so this package does not rewrite them either.

A TTL of 0 leaves the TTL unset, which Njalla defaults to 10800 seconds. Use a sub-second duration to request a TTL of 0, as described by libdns.RR.

Njalla rejects a few otherwise-valid records: TXT values that are empty or contain a double quote, and MX records with a preference of 0. These are reported as errors before the API is called.

DNSSEC records are not returned by GetRecords and are not supported by SetRecords.

Atomicity

The Njalla API offers no batch operations, so AppendRecords, SetRecords, and DeleteRecords are not atomic. If one of them returns an error, some of the requested changes may already have been applied; the records that were successfully changed are returned alongside the error.

All methods are safe for concurrent use.

func (*Provider) AppendRecords

func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error)

AppendRecords adds records to the zone, leaving existing records untouched. It returns the records that were created.

func (*Provider) DeleteRecords

func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error)

DeleteRecords deletes records from the zone, returning those that were deleted. Records that do not exist are silently ignored.

Deletion matches on name, type, and value. Leaving the value empty deletes every record with that name and type, and leaving the type empty deletes every record with that name; per the libdns contract, TTL is not part of the match. Njalla performs the matching itself, so no listing is required and this works with tokens that are restricted to the ACME challenge prefix. The one exception is deleting by name alone, which needs a listing because the API requires a type.

func (*Provider) GetRecords

func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error)

GetRecords lists all the records in the zone.

DNSSEC-related records are not included. Record types libdns does not model are returned as libdns.RR.

func (*Provider) ListZones

func (p *Provider) ListZones(ctx context.Context) ([]libdns.Zone, error)

ListZones returns the domains available to the API token.

func (*Provider) SetRecords

func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error)

SetRecords makes the given records the complete contents of their RRsets.

For every (name, type) pair present in the input, the records supplied become the only records in the zone with that name and type: surplus records are deleted and missing ones created. Records with other names or types are left alone. Existing record IDs are reused where possible, so an RRset is never momentarily empty.

DNSSEC records are not supported.

type RecordID

type RecordID string

RecordID is the Njalla-assigned identifier for a record. It is attached to every record this package returns via the libdns ProviderData field, so that later calls can address the record directly instead of searching for it.

ProviderData is not portable across providers or zones, and this package never depends on it for correctness: every operation works when it is absent. Records of types libdns models as the opaque libdns.RR struct cannot carry it at all, since RR has no ProviderData field.

type RetryConfig

type RetryConfig struct {
	MaxRetries   int
	BaseDelay    time.Duration
	MaxDelay     time.Duration
	RandomFactor float64
}

RetryConfig controls how transient failures are retried.

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns the retry policy used when none is configured. libdns asks implementations to recover from connection errors, 5xx, and 429 without adding substantial latency, so the ceiling is deliberately low.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址