[management] Make the certificate challenge window one knob to turn

Renewal was timed against the window in two different ways: the period derived
from it, the sweep interval did not. Shortening the window to watch a renewal in
an end-to-end run would have left the refresher still looking for due accounts
every quarter of an hour, so nothing would have been renewed in time and the
test would have reported the feature broken.

Derive the sweep from the period, within bounds that keep a very short window
from spinning and a normal one from checking less often than is useful, and
allow the window itself to be set through the environment so a run can take
seconds instead of half a day. A value that cannot be parsed or falls outside
the bounds keeps the default, because a window nobody intended is a security
property nobody chose, and an override is logged at warning level since it sets
how long a device keeps passing the check after its key is gone.

Every instance has to be given the same value: the window is part of the nonce,
so instances that disagree reject each other's.
This commit is contained in:
riccardom
2026-10-02 16:21:04 +02:00
parent 7ad4a0df37
commit 0e38fcf409
4 changed files with 140 additions and 18 deletions
+47 -1
View File
@@ -5,12 +5,27 @@ import (
"crypto/sha256"
"encoding/binary"
"errors"
"os"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
const (
// Window is the default challenge window. A nonce is accepted for its own window
// and the one before it, so a peer re-proves possession of its key between once
// and twice per window.
Window = 12 * time.Hour
// EnvWindow overrides Window, for end-to-end tests that cannot wait half a day to
// watch a renewal. Every management instance has to be given the same value: the
// window is part of the nonce, so instances that disagree reject each other's.
EnvWindow = "NB_CERT_CHALLENGE_WINDOW"
minWindow = time.Second
maxWindow = 24 * time.Hour
challengeDomain = "netbird-cert-challenge-v1"
windowLen = 8
nonceLen = windowLen + sha256.Size
@@ -19,6 +34,37 @@ const (
NonceSize = nonceLen
)
var effectiveWindow = sync.OnceValue(resolveWindow)
// EffectiveWindow returns the challenge window in force, which is Window unless
// EnvWindow overrides it. Everything timed against the window derives from this, so a
// test that shortens it shortens the renewal that goes with it.
func EffectiveWindow() time.Duration {
return effectiveWindow()
}
func resolveWindow() time.Duration {
val := os.Getenv(EnvWindow)
if val == "" {
return Window
}
window, err := time.ParseDuration(val)
if err != nil {
log.Warnf("failed to parse %s, keeping the %s certificate challenge window: %v", EnvWindow, Window, err)
return Window
}
if window < minWindow || window > maxWindow {
log.Warnf("%s of %s is outside %s..%s, keeping the %s certificate challenge window", EnvWindow, window, minWindow, maxWindow, Window)
return Window
}
// Loud on purpose: this sets how long a device can pass the certificate check after
// its key has gone, and it has to match on every instance.
log.Warnf("certificate challenge window overridden to %s by %s", window, EnvWindow)
return window
}
var (
ErrNonceMalformed = errors.New("certificate challenge nonce is malformed")
ErrNonceExpired = errors.New("certificate challenge nonce is expired")
@@ -33,7 +79,7 @@ type Challenger struct {
}
func NewChallenger(secret []byte) *Challenger {
return &Challenger{secret: secret, window: Window}
return &Challenger{secret: secret, window: EffectiveWindow()}
}
func (c *Challenger) Nonce(peerKey []byte, now time.Time) []byte {
@@ -0,0 +1,53 @@
package certposture
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestResolveWindow(t *testing.T) {
// An end-to-end run shortens the window so a renewal can be watched in seconds
// rather than half a day. Anything it cannot make sense of leaves the default in
// place, because a window nobody intended is a security property nobody chose.
tests := []struct {
name string
env string
want time.Duration
}{
{name: "unset keeps the default", env: "", want: Window},
{name: "a test-sized window is taken", env: "30s", want: 30 * time.Second},
{name: "garbage keeps the default", env: "soon", want: Window},
{name: "below the floor keeps the default", env: "10ms", want: Window},
{name: "above the ceiling keeps the default", env: "100h", want: Window},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv(EnvWindow, tt.env)
if tt.env == "" {
require.NoError(t, os.Unsetenv(EnvWindow))
}
assert.Equal(t, tt.want, resolveWindow())
})
}
}
func TestChallenger_HonoursAShortenedWindow(t *testing.T) {
// The window is what a nonce is stamped with, so a shortened one has to make a
// nonce expire sooner, not just change a number in a log line.
short := 2 * time.Second
c := &Challenger{secret: []byte("secret"), window: short}
peerKey := []byte("peer-key")
issued := time.Unix(1_790_000_000, 0)
nonce := c.Nonce(peerKey, issued)
require.NoError(t, c.verifyNonce(nonce, peerKey, issued), "a nonce is valid when issued")
require.NoError(t, c.verifyNonce(nonce, peerKey, issued.Add(short)), "and through the window after it")
assert.ErrorIs(t, c.verifyNonce(nonce, peerKey, issued.Add(3*short)), ErrNonceExpired,
"a shortened window must actually expire the nonce sooner")
}