dns: read non-string provider settings from file:

A file: value of a non-string setting, such as acme-dns config,
is the JSON in that file; checks leave it unread.

Fixes #3588
This commit is contained in:
Kristoffer Dalby committed 2026-11-11 17:54:31 +01:00
1 parent c7bfe5b4f7
commit f75c7cf059
8 files changed
+382 -22

No files matched your search

+1
View File
@@ -234,6 +234,7 @@ is not connected, or refuses is "not found". See [the client docs](https://heads
- Renaming a node now stores the name in lowercase; a node renamed with uppercase letters before gets no certificate name until it is renamed again [#3554](https://github.com/juanfont/headscale/pull/3554)
- Add `dns.certificates.enabled` and `dns.certificates.zone` to let nodes get TLS certificates [#3554](https://github.com/juanfont/headscale/pull/3554)
- Add `dns.challenge.provider`, `dns.challenge.propagation_timeout` and `dns.challenge.resolvers` for ACME DNS-01 challenges [#3554](https://github.com/juanfont/headscale/pull/3554)
- A non-string `dns.challenge.provider` setting, such as acme-dns `config`, takes `file:<path>` to a file holding its JSON value [#3588](https://github.com/juanfont/headscale/issues/3588)
- The v2 tailnet setting `httpsEnabled` reports `dns.certificates.enabled` [#3554](https://github.com/juanfont/headscale/pull/3554)
- `tls.acme.challenge_type` accepts `DNS-01`, using `dns.challenge.provider` [#3554](https://github.com/juanfont/headscale/pull/3554)
- Fix the unix socket being created `0700` instead of the documented `0770` when `unix_socket_permission` is unset. The socket grants full admin access without authentication, so members of its group gain that access; check who is in it before upgrading [#3540](https://github.com/juanfont/headscale/pull/3540)
+14
View File
@@ -377,6 +377,20 @@ func TestCheckSecretsUnread(t *testing.T) {
}
})
t.Run("acme-dns registrations from file", func(t *testing.T) {
dir := t.TempDir()
missing := filepath.Join(dir, "missing")
cfg := writeServerConfig(t, dir, acmeDNS+` config: "file:`+missing+`"
`)
_, err := runCheck(t, checkConfigCmd, cfg)
require.NoError(t, err)
out, err := runCheck(t, checkAllCmd, cfg)
require.NoError(t, err)
require.Contains(t, strings.Split(out, "\n"), "skip dns.challenge.provider.config "+missing+" (secret, not read)")
})
// A URL from a file cannot be validated unread.
for name, provider := range map[string]string{
"account": acmeDNS + ` username: user
+127 -1
View File
@@ -2050,6 +2050,11 @@ func TestRemovedKeysAreRejected(t *testing.T) {
file: "dns:\n challenge:\n provider:\n name: cloudflare\n api_token_path: /run/secret\n",
want: []string{"dns.challenge.provider.api_token_path was removed", `dns.challenge.provider.api_token: "file:<path>"`},
},
{
name: "acme-dns registrations path key",
file: "dns:\n challenge:\n provider:\n name: acmedns\n config_path: /run/acmedns.json\n",
want: []string{"dns.challenge.provider.config_path was removed", `dns.challenge.provider.config: "file:<path>"`},
},
{
name: "provider path key without provider",
file: "dns:\n challenge:\n provider:\n api_token_path: /run/secret\n",
@@ -2526,6 +2531,40 @@ func TestDNSChallengeConfig(t *testing.T) {
secret := filepath.Join(t.TempDir(), "token")
require.NoError(t, os.WriteFile(secret, []byte("file-token\n"), 0o600))
regsFile := func(regs string) string {
p := filepath.Join(t.TempDir(), "acmedns.json")
require.NoError(t, os.WriteFile(p, []byte(regs), 0o600))
return p
}
reg := func(name, username, password, subdomain string) string {
return `"` + name + `": {"username": ` + username + `, "password": "` + password + `", "subdomain": "` +
subdomain + `", "server_url": "https://auth.acme-dns.io"}`
}
regs := regsFile("{" + reg("laptop.ts.example.com", `"user"`, "pass", "reg-laptop") + "}")
// What these files hold must stay out of errors, marker- strings most of all.
unknownField := regsFile(`{"marker-name.ts.example.com": {"username": "user", "password": "pass", ` +
`"subdomain": "reg", "server_url": "https://auth.acme-dns.io", "marker-field": 1}}`)
wrongFieldType := regsFile("{" + reg("marker-name.ts.example.com", `{"marker-field": 1}`, "pass", "reg") + "}")
wrongType := regsFile(`"file:/nonexistent/marker-path"`)
nestedMissing := regsFile("{" + reg("marker-name.ts.example.com", `"user"`, "file:/nonexistent/marker-path", "reg") + "}")
shared := regsFile("{" + reg("marker-one.ts.example.com", `"user"`, "pass", "reg") + ", " +
reg("marker-two.ts.example.com", `"user"`, "pass", "reg") + "}")
empty := filepath.Join(t.TempDir(), "empty")
require.NoError(t, os.WriteFile(empty, []byte("\n"), 0o600))
acmeDNSFrom := func(config string) string {
return ` certificates:
enabled: true
challenge:
provider:
name: acmedns
config: "file:` + config + `"
`
}
base := `---
server_url: https://headscale.example.com
noise:
@@ -2622,6 +2661,87 @@ dns:
wantZone: "ts.example.com",
wantIsolates: "laptop.ts.example.com",
},
{
name: "acmedns registrations from file",
yaml: base + acmeDNSFrom(regs),
wantZone: "ts.example.com",
wantIsolates: "laptop.ts.example.com",
},
{
name: "acmedns registrations from file in env",
yaml: base,
env: map[string]string{
"HEADSCALE_DNS_CERTIFICATES_ENABLED": "true",
"HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "acmedns",
"HEADSCALE_DNS_CHALLENGE_PROVIDER_CONFIG": "file:" + regs,
},
wantZone: "ts.example.com",
wantIsolates: "laptop.ts.example.com",
},
{
name: "acmedns registrations from file in env json",
yaml: base,
env: map[string]string{
"HEADSCALE_DNS_CERTIFICATES_ENABLED": "true",
"HEADSCALE_DNS_CHALLENGE_PROVIDER_NAME": "acmedns",
"HEADSCALE_DNS_CHALLENGE_PROVIDER": `{"config":"file:` + regs + `"}`,
},
wantZone: "ts.example.com",
wantIsolates: "laptop.ts.example.com",
},
{
// Unread by the load, registrations from a file count as present.
name: "acmedns registrations file missing",
yaml: base + acmeDNSFrom("/nonexistent/acmedns.json"),
wantErr: "dns.challenge.provider.config: ",
fileErr: true,
},
{
name: "acmedns registrations file empty",
yaml: base + acmeDNSFrom(empty),
wantErr: "dns.challenge.provider.config: " + empty + ": file is empty",
fileErr: true,
},
{
name: "acmedns registrations file with an unknown field",
yaml: base + acmeDNSFrom(unknownField),
wantErr: "dns.challenge.provider.config: " + unknownField + ": file holds an invalid value",
fileErr: true,
},
{
name: "acmedns registrations file with a field of the wrong type",
yaml: base + acmeDNSFrom(wrongFieldType),
wantErr: "dns.challenge.provider.config: " + wrongFieldType + ": file holds an invalid value",
fileErr: true,
},
{
// Rejected as it is, not read as a file: secret.
name: "acmedns registrations file of the wrong type",
yaml: base + acmeDNSFrom(wrongType),
wantErr: "dns.challenge.provider.config: " + wrongType + ": file holds an invalid value",
fileErr: true,
},
{
name: "acmedns registrations file with an unreadable secret",
yaml: base + acmeDNSFrom(nestedMissing),
wantErr: "dns.challenge.provider.config: " + nestedMissing + ": file names a secret that cannot be read",
fileErr: true,
},
{
name: "acmedns registrations file shared by node names",
yaml: base + acmeDNSFrom(shared),
wantErr: "dns.challenge.provider.config: " + shared + ": acme-dns registration mapped to more than one name",
fileErr: true,
},
{
name: "provider settings without a name read nothing",
yaml: base + ` challenge:
provider:
config: "file:/nonexistent/acmedns.json"
`,
wantZone: "ts.example.com",
wantNoProv: true,
},
{
name: "acmedns registration password file missing",
yaml: base + ` certificates:
@@ -2990,8 +3110,14 @@ tls:
require.NoError(t, err)
if tt.fileErr {
_, err = cfg.DNSConfig.Challenge.NewProvider(types.ResolveSecret)
// As startup does: read the files, then check isolation.
built, err := cfg.DNSConfig.Challenge.NewProvider(types.ResolveSecret)
if iso, ok := built.(hsdns.Isolator); ok && err == nil {
err = iso.CheckIsolation()
}
require.ErrorContains(t, err, tt.wantErr)
assert.NotContains(t, err.Error(), "marker-")
return
}
+2 -1
View File
@@ -151,7 +151,8 @@ func (r *reference) secrets(keys []keyInfo) {
}
r.WriteString(referenceSecrets)
fmt.Fprintf(r, "The secrets are %s and every string setting of %s.\n\n", strings.Join(names, ", "), r.link(providerKey))
fmt.Fprintf(r, "The secrets are %s and every setting of %s, where a non-string one, such as acme-dns `config`, is the JSON in the file.\n\n",
strings.Join(names, ", "), r.link(providerKey))
r.WriteString(referenceNixSecret)
}
+1 -1
View File
@@ -734,7 +734,7 @@ func validateDNSProvider(c *types.Config, v *configValidator) {
Current: []types.KV{{Key: "dns.challenge.provider.name", Value: ch.ProviderName()}},
Detail: err.Error(),
Allowed: allowed,
Hint: `check the provider name and its settings; any string setting can be read from a file with "file:<path>"`,
Hint: `check the provider name and its settings; any setting can be read from a file with "file:<path>", a non-string one as JSON`,
See: dnsDocsURL,
Cause: err,
})
+119 -17
View File
@@ -2,6 +2,7 @@ package dns
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
@@ -83,12 +84,18 @@ var (
errAcmeDNSOneAccount = errors.New("acme-dns with one account answers every name's challenge from one record; " +
"node certificates need a registration per name under config")
errAcmeDNSShared = errors.New("acme-dns registration mapped to more than one name")
errNotJSON = errors.New("file is not JSON")
errFileValue = errors.New("file holds an invalid value")
errFileSecret = errors.New("file names a secret that cannot be read")
// ErrSecretUnread is returned, with the value as given, by a secret
// func that leaves a file: secret unread; checks of it are skipped.
ErrSecretUnread = errors.New("secret left unread")
)
// settingsKey is where the provider settings live in the configuration.
const settingsKey = "dns.challenge.provider"
// ProviderNames returns the names of the compiled-in providers.
func ProviderNames() []string {
return slices.Sorted(maps.Keys(providers))
@@ -111,7 +118,8 @@ func SettingNames(provider string) []string {
// a single key as the provider spells it and is consulted for every field
// of the provider, so it wins over a differently cased spelling in
// settings. secret resolves every string setting, nested ones
// included, so any of them can be read from a file. Unknown keys are an
// included, so any of them can be read from a file; a non-string setting
// set to "file:<path>" is the JSON in that file. Unknown keys are an
// error so a typo does not silently leave a credential empty.
func NewProvider(
name string,
@@ -147,6 +155,9 @@ func NewProvider(
return s, err
}
// The origin, "<setting>: <path>", of each setting read from a file.
files := map[string]string{}
fields := jsonFields(provider)
for tag, kind := range fields {
if v, ok := get(tag); ok {
@@ -154,11 +165,19 @@ func NewProvider(
}
value, ok := input[tag]
if _, isMap := value.(map[string]any); !ok || (kind != reflect.String && !isMap) {
if !ok {
continue
}
value, err := resolveSecrets(tag, value, read)
var err error
if s, isString := value.(string); isString && kind != reflect.String && strings.HasPrefix(s, "file:") {
files[tag] = settingsKey + "." + tag + ": " + os.ExpandEnv(strings.TrimPrefix(s, "file:"))
value, err = settingFromFile(entry.build(), tag, s, files[tag], secret)
} else {
value, err = resolveSecrets(tag, value, read)
}
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrProviderConfig, err)
}
@@ -166,22 +185,15 @@ func NewProvider(
input[tag] = value
}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
WeaklyTypedInput: true,
ErrorUnused: true,
Squash: true,
Result: provider,
})
if err != nil {
return nil, fmt.Errorf("creating decoder: %w", err)
}
err = decoder.Decode(input)
err := decode(input, provider)
if err != nil {
return nil, fmt.Errorf("%w for %s: %w", ErrProviderConfig, name, err)
}
if p, ok := provider.(*acmeDNS); ok {
p.configFile = files["config"]
}
// After decoding, so a misspelt setting is reported as that. An unread
// file: value counts as set.
var missing []string
@@ -209,6 +221,63 @@ func NewProvider(
return provider, nil
}
// decode sets provider's fields from input by their json tags.
func decode(input map[string]any, provider Provider) error {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
WeaklyTypedInput: true,
ErrorUnused: true,
Squash: true,
Result: provider,
})
if err != nil {
return fmt.Errorf("creating decoder: %w", err)
}
return decoder.Decode(input)
}
// settingFromFile reads the non-string setting tag from the JSON in the
// file value names, decoded into provider, a fresh one, before any secret
// in it is read. Past reading the file, errors name only origin: decoder
// paths, keys and nested paths all come from the file. Left unread, it is
// an empty map: set, with nothing to check.
func settingFromFile(
provider Provider,
tag, value, origin string,
secret func(key, value string) (string, error),
) (any, error) {
s, err := secret(tag, value)
if errors.Is(err, ErrSecretUnread) {
// shortcut: fits map settings only, the one non-string kind any
// provider has (TestProviderSettingKinds); give another kind its
// own placeholder when a provider adds one.
return map[string]any{}, nil
}
if err != nil {
return nil, err
}
var v any
// json's errors quote the contents.
if json.Unmarshal([]byte(s), &v) != nil {
return nil, fmt.Errorf("%s: %w", origin, errNotJSON)
}
if decode(map[string]any{tag: v}, provider) != nil {
return nil, fmt.Errorf("%s: %w", origin, errFileValue)
}
v, err = resolveSecrets(tag, v, secret)
if err != nil {
return nil, fmt.Errorf("%s: %w", origin, errFileSecret)
}
return v, nil
}
// resolveSecrets resolves every string in value, down through nested
// settings such as acme-dns's per-name registrations; key names the
// setting in errors. Maps are copied, as they may be the caller's.
@@ -317,6 +386,10 @@ const acmeDNSSpacing = time.Second + 100*time.Millisecond
type acmeDNS struct {
acmedns.Provider
// configFile is the origin of Configs read from a file, which errors
// about Configs name instead of anything in it.
configFile string
mu sync.Mutex
// served is what each registration serves, oldest first.
served map[string][2]acmeDNSValue
@@ -347,6 +420,10 @@ func (p *acmeDNS) AppendRecords(
served := p.served[reg]
if served[0].live {
if p.configFile != "" {
reg = "from " + p.configFile
}
return appended, fmt.Errorf(
"%w: acme-dns registration %s would overwrite a value still in use",
ErrTooManyChallenges, reg,
@@ -370,13 +447,32 @@ func (p *acmeDNS) AppendRecords(
}
if err != nil {
return appended, err
return appended, p.redact(err)
}
}
return appended, nil
}
// redact hides err's text, which may quote registrations read from a
// file, behind the file's name; errors.Is and errors.As still see err.
func (p *acmeDNS) redact(err error) error {
if err == nil || p.configFile == "" {
return err
}
return &acmeDNSFileError{origin: p.configFile, err: err}
}
type acmeDNSFileError struct {
origin string
err error
}
func (e *acmeDNSFileError) Error() string { return e.origin + ": acme-dns update failed" }
func (e *acmeDNSFileError) Unwrap() error { return e.err }
func (p *acmeDNS) DeleteRecords(
ctx context.Context,
zone string,
@@ -403,7 +499,9 @@ func (p *acmeDNS) DeleteRecords(
p.served[reg] = served
}
return p.Provider.DeleteRecords(ctx, zone, recs)
deleted, err := p.Provider.DeleteRecords(ctx, zone, recs)
return deleted, p.redact(err)
}
// Isolates reports whether name has a registration of its own;
@@ -427,6 +525,10 @@ func (p *acmeDNS) CheckIsolation() error {
sub := p.Configs[name].Subdomain
if other, shared := names[sub]; shared {
if p.configFile != "" {
return fmt.Errorf("%s: %w", p.configFile, errAcmeDNSShared)
}
// The registration is not named: it may have been read from a file.
return fmt.Errorf("%w: %s and %s share a registration", errAcmeDNSShared, other, name)
}
+115
View File
@@ -3,10 +3,13 @@ package dns
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
@@ -54,6 +57,16 @@ func TestSettingNames(t *testing.T) {
assert.Nil(t, SettingNames(""))
}
// The unread placeholder settingFromFile returns is a map, so checks
// without the files decode only while every setting is a string or a map.
func TestProviderSettingKinds(t *testing.T) {
for name, entry := range providers {
for tag, kind := range jsonFields(entry.build()) {
assert.Contains(t, []reflect.Kind{reflect.String, reflect.Map}, kind, "%s %s", name, tag)
}
}
}
// unreadSecret stands in for a caller that checks without reading files.
func unreadSecret(_, value string) (string, error) {
if strings.HasPrefix(value, "file:") {
@@ -79,6 +92,7 @@ func TestNewProviderUnreadSecrets(t *testing.T) {
{name: "registration server_url from file", settings: map[string]any{"config": map[string]any{"laptop.example.com": account("file:/url")}}},
{name: "literal server_url still checked", settings: account("auth.acme-dns.io"), wantErr: true},
{name: "literal registration server_url still checked", settings: map[string]any{"config": map[string]any{"laptop.example.com": account("auth.acme-dns.io")}}, wantErr: true},
{name: "registrations from file", settings: map[string]any{"config": "file:/acmedns.json"}},
}
for _, tt := range tests {
@@ -99,6 +113,15 @@ func TestNewProvider(t *testing.T) {
secret := filepath.Join(t.TempDir(), "token")
require.NoError(t, os.WriteFile(secret, []byte("from-file\n"), 0o600))
// The registrations' JSON, its password a file: secret as if written inline.
regs := filepath.Join(t.TempDir(), "acmedns.json")
require.NoError(t, os.WriteFile(regs, []byte(`{"node.ts.example.com": {"username": "user", "password": "file:`+secret+
`", "subdomain": "reg", "server_url": "https://auth.acme-dns.io"}}`+"\n"), 0o600))
// json's error would quote the number.
notJSON := filepath.Join(t.TempDir(), "acmedns.json")
require.NoError(t, os.WriteFile(notJSON, []byte("1e1000\n"), 0o600))
tests := []struct {
name string
provider string
@@ -257,6 +280,35 @@ func TestNewProvider(t *testing.T) {
}},
wantErr: ErrProviderConfig,
},
{
name: "acmedns registrations from file",
provider: "acmedns",
settings: map[string]any{"config": "file:" + regs},
want: &acmeDNS{
Configs: map[string]acmedns.DomainConfig{
"node.ts.example.com": {
Username: "user",
Password: "from-file",
Subdomain: "reg",
ServerURL: "https://auth.acme-dns.io",
},
},
configFile: "dns.challenge.provider.config: " + regs,
served: map[string][2]acmeDNSValue{},
},
},
{
name: "acmedns registrations file missing",
provider: "acmedns",
settings: map[string]any{"config": "file:" + filepath.Join(t.TempDir(), "missing")},
wantErr: ErrProviderConfig,
},
{
name: "acmedns registrations file not JSON",
provider: "acmedns",
settings: map[string]any{"config": "file:" + notJSON},
wantErr: ErrProviderConfig,
},
{
name: "acmedns server_url without scheme",
provider: "acmedns",
@@ -291,6 +343,7 @@ func TestNewProvider(t *testing.T) {
got, err := NewProvider(tt.provider, tt.settings, noEnv, readSecret)
if tt.wantErr != nil {
require.ErrorIs(t, err, tt.wantErr)
assert.NotContains(t, err.Error(), "1e1000", "a file's contents are secret")
return
}
@@ -383,6 +436,68 @@ func acmeTXT(node, value string) []libdns.Record {
// validating it, however many values were deleted. Two updates within one
// second carry the same stamp, leaving acme-dns free to overwrite either:
// after a then b, with a freed, c could overwrite b.
// Runtime errors reach the server log: they name registrations written
// inline, but only the file that holds them otherwise.
func TestAcmeDNSRuntimeErrorsFromFile(t *testing.T) {
t.Parallel()
// Refuses connections, so an update fails in transport, quoting its URL.
closed := httptest.NewServer(nil)
closed.Close()
regs := map[string]any{"node.ts.example.com": map[string]any{
"username": "user",
"password": "pass",
"subdomain": "marker-subdomain",
"server_url": closed.URL + "/marker-url",
}}
b, err := json.Marshal(regs)
require.NoError(t, err)
file := filepath.Join(t.TempDir(), "acmedns.json")
require.NoError(t, os.WriteFile(file, b, 0o600))
for name, config := range map[string]any{"inline": regs, "file": "file:" + file} {
t.Run(name, func(t *testing.T) {
t.Parallel()
named := func(err error, marker string) {
t.Helper()
if name == "inline" {
assert.Contains(t, err.Error(), marker)
} else {
assert.NotContains(t, err.Error(), "marker-")
assert.Contains(t, err.Error(), "dns.challenge.provider.config: "+file)
}
}
p, err := NewProvider("acmedns", map[string]any{"config": config}, noEnv, readSecret)
require.NoError(t, err)
ctx := context.Background()
_, err = p.AppendRecords(ctx, testZone, acmeTXT("node", "value"))
require.Error(t, err)
_, isURL := errors.AsType[*url.Error](err)
assert.True(t, isURL, "a transport failure keeps its type")
named(err, "marker-url")
a, ok := p.(*acmeDNS)
require.True(t, ok)
// Both values the registration serves are in use.
a.served["marker-subdomain"] = [2]acmeDNSValue{{live: true}, {live: true}}
_, err = p.AppendRecords(ctx, testZone, acmeTXT("node", "value"))
require.ErrorIs(t, err, ErrTooManyChallenges)
named(err, "marker-subdomain")
})
}
}
func TestAcmeDNSRefusesEviction(t *testing.T) {
t.Parallel()
+3 -2
View File
@@ -391,8 +391,9 @@ type DNSChallengeConfig struct {
// ProviderSettings is the DNS provider that writes challenge records:
// `name` selects it, the other keys are its settings. Keys are
// lowercased on load, at every depth; two that differ only in case
// are an error. A string setting may be `file:<path>` to read it from
// a file. `headscale generate config --running` and `/debug/config`
// are an error. Any setting may be `file:<path>` to read it from a
// file; a non-string one, such as acme-dns `config`, is the JSON in it.
// `headscale generate config --running` and `/debug/config`
// show `name` and `file:` references; every other setting is
// `<redacted>`, as any may be a credential. See
// [DNS-01 challenge provider](https://headscale.net/stable/ref/dns/#dns-01-challenge-provider)