cmd/k8s-operator,ipn/store/kubestore,kube/kubetypes: share ACME account key per tailnet

Introduce a per-tailnet shared ACME account key so that all ingress
ProxyGroup replicas on a tailnet present the same account identity to
Let's Encrypt. This lets renewals claim the ARI "replaces" exemption
from the 50-certs-per-week rate limit, surviving Pod restarts,
ProxyGroup recreation, and cluster migrations.

The operator provisions a "tailscale-acme-accounts" Secret in its
namespace, guarded by a finalizer and a deletion warning event, and
watched so it is recreated promptly if removed. Proxies migrate any
pre-existing per-pod key into the shared Secret on first boot, adopt
the shared key on subsequent boots, and restore it on cert writes if
the Secret was recreated empty. Certs are stamped with the fingerprint
of the issuing account so renewals skip the "replaces" claim when the
account doesn't match.

Opt-in per-ProxyGroup via the tailscale.com/share-acme-account
annotation, or operator-wide via OPERATOR_SHARED_ACME_ACCOUNT_KEY.

Updates #18251
Updates #20288

Signed-off-by: chaosinthecrd <tom@tmlabs.co.uk>
This commit is contained in:
chaosinthecrd
2026-07-27 12:06:41 +01:00
committed by Tom Meadows
parent 2900f3494a
commit 97a75c837d
13 changed files with 975 additions and 34 deletions
+183 -3
View File
@@ -5,7 +5,9 @@
package kubestore
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"net"
@@ -47,6 +49,17 @@ const (
keyTLSCert = "tls.crt"
keyTLSKey = "tls.key"
// keyACMEAcctFP is the cert Secret field that records the SHA-256
// fingerprint of the PEM-encoded ACME account key that issued the
// cert. The renewal path uses this to decide whether to include the
// ARI "replaces" hint: only if the current account key matches
// (otherwise Let's Encrypt rejects the claim).
keyACMEAcctFP = "acme-account-fingerprint"
// acmeAccountStateKey is the ipn.StateStore key under which tailscaled
// stores its ACME account private key. Mirrors the acmePEMName constant
// in ipn/ipnlocal/cert.go. Duplicated here to avoid an import cycle.
acmeAccountStateKey = "acme-account.key.pem"
)
// Store is an ipn.StateStore that uses a Kubernetes Secret for persistence.
@@ -57,6 +70,17 @@ type Store struct {
certShareMode string // 'ro', 'rw', or empty
podName string
// acmeAccountsSecretName, when non-empty in "rw" cert share mode,
// routes reads and writes of acmeAccountStateKey to acmeAccountField
// inside this shared per-tailnet Secret. See #18251.
acmeAccountsSecretName string
acmeAccountField string
// preAdoptedLocalKey is the SHA-256 of the per-pod ACME account key
// that was in the local state Secret before we adopted a foreign
// shared key. Non-nil only when adoption changed the key.
preAdoptedLocalKey []byte
logf logger.Logf
// memory holds the latest tailscale state. Writes write state to a kube
@@ -106,6 +130,17 @@ func newWithClient(logf logger.Logf, c kubeclient.Client, secretName string) (*S
s.certShareMode = "ro"
}
// Configure shared ACME account lookup. Only meaningful for the cert
// issuer (cert share "rw") — read replicas never issue.
if s.certShareMode == "rw" {
s.acmeAccountsSecretName = os.Getenv("TS_ACME_ACCOUNT_SECRET_NAME")
s.acmeAccountField = os.Getenv("TS_ACME_ACCOUNT_FIELD")
if s.acmeAccountsSecretName != "" && s.acmeAccountField == "" {
s.logf("[unexpected] TS_ACME_ACCOUNT_SECRET_NAME set without TS_ACME_ACCOUNT_FIELD; ignoring shared ACME account configuration")
s.acmeAccountsSecretName = ""
}
}
// Load latest state from kube Secret if it already exists.
if err := s.loadState(); err != nil && err != ipn.ErrStateNotExist {
return nil, fmt.Errorf("error loading state from kube Secret: %w", err)
@@ -126,9 +161,135 @@ func newWithClient(logf logger.Logf, c kubeclient.Client, secretName string) (*S
if s.certShareMode == "ro" {
go s.runCertReload(context.Background())
}
if s.acmeAccountsSecretName != "" {
if err := s.reconcileSharedACMEAccountKey(); err != nil {
// Non-fatal: the cert loop will retry on next issuance.
s.logf("kubestore: reconciling shared ACME account key: %v", err)
}
}
return s, nil
}
// reconcileSharedACMEAccountKey aligns the in-memory ACME account key with
// the shared per-tailnet field: adopt the shared value if present, otherwise
// copy the local per-pod key up so upgrading deployments keep renewal
// continuity.
func (s *Store) reconcileSharedACMEAccountKey() error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
sharedSecret, err := s.client.GetSecret(ctx, s.acmeAccountsSecretName)
if err != nil && !kubeclient.IsNotFoundErr(err) {
return fmt.Errorf("reading shared ACME accounts Secret %q: %w", s.acmeAccountsSecretName, err)
}
var sharedKey []byte
if sharedSecret != nil {
sharedKey = sharedSecret.Data[sanitizeKey(s.acmeAccountField)]
}
if len(sharedKey) > 0 {
// Shared field already populated for this tailnet. Adopt it. If
// our local per-pod key differs, remember its fingerprint so
// legacy certs on this pod (issued before we started stamping
// fingerprints) can be recognised as mis-aligned on renewal.
localKey, err := s.memory.ReadState(ipn.StateKey(acmeAccountStateKey))
if err == nil && len(localKey) > 0 && !bytes.Equal(localKey, sharedKey) {
sum := sha256.Sum256(localKey)
s.preAdoptedLocalKey = sum[:]
}
s.memory.WriteState(ipn.StateKey(acmeAccountStateKey), sharedKey)
return nil
}
// Shared field is empty. If we have a per-pod key from the state
// Secret, copy it up so existing renewals stay exempt.
localKey, err := s.memory.ReadState(ipn.StateKey(acmeAccountStateKey))
if err != nil || len(localKey) == 0 {
// Nothing local either; the cert loop will generate one on first
// use and route the write through writeSharedACMEAccountKey.
return nil
}
if err := s.writeSharedACMEAccountKey(localKey); err != nil {
return fmt.Errorf("copying per-pod ACME account key to shared Secret: %w", err)
}
s.logf("kubestore: migrated per-pod ACME account key into shared Secret %q field %q", s.acmeAccountsSecretName, s.acmeAccountField)
return nil
}
// writeSharedACMEAccountKey writes key to acmeAccountField inside
// acmeAccountsSecretName, using whichever access pattern (patch or update)
// this Store has permission for.
func (s *Store) writeSharedACMEAccountKey(key []byte) error {
return s.updateSecret(map[string][]byte{s.acmeAccountField: key}, s.acmeAccountsSecretName)
}
// maybeRestoreSharedACMEAccountKey writes the in-memory ACME account key to
// the shared Secret's per-tailnet field if that field is empty or missing,
// e.g. because the Secret was deleted and recreated while this process was
// running. It never overwrites an existing shared key.
func (s *Store) maybeRestoreSharedACMEAccountKey() error {
if s.acmeAccountsSecretName == "" {
return nil
}
key, err := s.memory.ReadState(ipn.StateKey(acmeAccountStateKey))
if err != nil || len(key) == 0 {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
shared, err := s.client.GetSecret(ctx, s.acmeAccountsSecretName)
if err != nil && !kubeclient.IsNotFoundErr(err) {
return fmt.Errorf("reading shared ACME accounts Secret %q: %w", s.acmeAccountsSecretName, err)
}
if shared != nil && len(shared.Data[sanitizeKey(s.acmeAccountField)]) > 0 {
return nil
}
s.logf("kubestore: shared ACME accounts Secret %q field %q is empty; restoring account key from memory", s.acmeAccountsSecretName, s.acmeAccountField)
return s.writeSharedACMEAccountKey(key)
}
// acmeAccountKeyFingerprint returns the SHA-256 of the current in-memory
// PEM-encoded ACME account key, or (nil, false) if the key isn't set.
func acmeAccountKeyFingerprint(m *mem.Store) ([]byte, bool) {
key, err := m.ReadState(ipn.StateKey(acmeAccountStateKey))
if err != nil || len(key) == 0 {
return nil, false
}
sum := sha256.Sum256(key)
return sum[:], true
}
// ShouldUseARIReplacesForRenewal reports whether the current ACME account
// key matches the one that issued the cert for domain. See #18251.
func (s *Store) ShouldUseARIReplacesForRenewal(domain string) (bool, error) {
if s.certShareMode != "rw" {
return true, nil
}
curFP, ok := acmeAccountKeyFingerprint(&s.memory)
if !ok {
// No current account key in memory yet; nothing to compare.
return true, nil
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
sec, err := s.client.GetSecret(ctx, domain)
if err != nil {
if kubeclient.IsNotFoundErr(err) {
return true, nil
}
return true, fmt.Errorf("getting TLS Secret %q: %w", domain, err)
}
certFP := sec.Data[keyACMEAcctFP]
if len(certFP) == 0 {
// Legacy cert, no fingerprint stamp. Assume misaligned only if
// we adopted a foreign shared key.
if len(s.preAdoptedLocalKey) > 0 {
return false, nil
}
return true, nil
}
return bytes.Equal(certFP, curFP), nil
}
func (s *Store) SetDialer(d func(ctx context.Context, network, address string) (net.Conn, error)) {
s.client.SetDialer(d)
}
@@ -147,6 +308,12 @@ func (s *Store) WriteState(id ipn.StateKey, bs []byte) (err error) {
s.memory.WriteState(ipn.StateKey(sanitizeKey(id)), bs)
}
}()
if s.acmeAccountsSecretName != "" && string(id) == acmeAccountStateKey {
if bs == nil {
return s.removeSecretField(s.acmeAccountField, s.acmeAccountsSecretName)
}
return s.writeSharedACMEAccountKey(bs)
}
if bs == nil {
return s.removeSecretField(string(id), s.secretName)
}
@@ -154,7 +321,9 @@ func (s *Store) WriteState(id ipn.StateKey, bs []byte) (err error) {
}
// WriteTLSCertAndKey writes a TLS cert and key to domain.crt, domain.key fields
// of a Tailscale Kubernetes node's state Secret.
// of a Tailscale Kubernetes node's state Secret. In cert-share "rw" mode it
// also stamps acme-account-fingerprint alongside the cert so the renewal path
// can tell whether the current ACME account key issued this cert.
func (s *Store) WriteTLSCertAndKey(domain string, cert, key []byte) (err error) {
if s.certShareMode == "ro" {
s.logf("[unexpected] TLS cert and key write in read-only mode")
@@ -175,6 +344,14 @@ func (s *Store) WriteTLSCertAndKey(domain string, cert, key []byte) (err error)
keyTLSCert: cert,
keyTLSKey: key,
}
if fp, ok := acmeAccountKeyFingerprint(&s.memory); ok {
data[keyACMEAcctFP] = fp
}
// The shared Secret may have been deleted and recreated empty
// while we were running; re-assert the account key if so.
if err := s.maybeRestoreSharedACMEAccountKey(); err != nil {
s.logf("kubestore: restoring shared ACME account key: %v", err)
}
}
if err := s.updateSecret(data, secretName); err != nil {
return fmt.Errorf("error writing TLS cert and key to Secret: %w", err)
@@ -489,7 +666,9 @@ func (s *Store) loadCerts(ctx context.Context, sel map[string]string) error {
// canCreateSecret returns true if this node should be allowed to create the given
// Secret in its namespace.
func (s *Store) canCreateSecret(secret string) bool {
// Only allow creating the state Secret (and not TLS Secrets).
// Only allow creating the state Secret (and not TLS Secrets). The
// shared ACME accounts Secret is precreated by the operator, so write
// replicas never need create permission for it.
return secret == s.secretName
}
@@ -498,7 +677,8 @@ func (s *Store) canCreateSecret(secret string) bool {
func (s *Store) canPatchSecret(secret string) bool {
// For backwards compatibility reasons, setups where the proxies are not
// given PATCH permissions for state Secrets are allowed. For TLS
// Secrets, we should always have PATCH permissions.
// Secrets and the shared ACME accounts Secret, we should always have
// PATCH permissions.
if secret == s.secretName {
return s.canPatch
}
+439
View File
@@ -6,6 +6,7 @@ package kubestore
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"strings"
@@ -847,3 +848,441 @@ func TestNewWithClient(t *testing.T) {
})
}
}
func TestSharedACMEAccountKey(t *testing.T) {
const (
stateSecretName = "ingress-proxies-0"
sharedSecretName = kubetypes.ACMEAccountsSecretName
sharedField = "my-tailnet" + kubetypes.ACMEAccountKeySuffix
)
existingKey := []byte("-----BEGIN PRIVATE KEY-----\nexisting\n-----END PRIVATE KEY-----")
freshKey := []byte("-----BEGIN PRIVATE KEY-----\nfresh\n-----END PRIVATE KEY-----")
tests := []struct {
name string
certMode string
stateSecret map[string][]byte
sharedSecret map[string][]byte // nil = Secret does not exist
envSecretName string
envField string
writeAfterInit []byte // if non-nil, call WriteState(acmeAccountStateKey, …) after init
wantMemoryACME []byte
wantSharedSecret map[string][]byte
wantStateSecret map[string][]byte // optional: when set, asserts state Secret was not touched on the ACME path
wantPreAdopted []byte // expected s.preAdoptedLocalKey (sha256 of pre-adoption local key)
}{
{
name: "adopts_shared_key_when_present",
certMode: "rw",
envSecretName: sharedSecretName,
envField: sharedField,
stateSecret: map[string][]byte{},
sharedSecret: map[string][]byte{sharedField: existingKey},
wantMemoryACME: existingKey,
wantSharedSecret: map[string][]byte{sharedField: existingKey},
},
{
name: "shared_overrides_per_pod_key",
certMode: "rw",
envSecretName: sharedSecretName,
envField: sharedField,
stateSecret: map[string][]byte{
acmeAccountStateKey: freshKey, // would be stale
},
sharedSecret: map[string][]byte{sharedField: existingKey},
wantMemoryACME: existingKey,
wantSharedSecret: map[string][]byte{sharedField: existingKey},
wantPreAdopted: sha256Sum(freshKey),
},
{
name: "adopting_matching_local_leaves_preadopted_nil",
certMode: "rw",
envSecretName: sharedSecretName,
envField: sharedField,
stateSecret: map[string][]byte{
acmeAccountStateKey: existingKey, // matches shared
},
sharedSecret: map[string][]byte{sharedField: existingKey},
wantMemoryACME: existingKey,
wantSharedSecret: map[string][]byte{sharedField: existingKey},
},
{
name: "migrates_per_pod_key_when_shared_field_empty",
certMode: "rw",
envSecretName: sharedSecretName,
envField: sharedField,
stateSecret: map[string][]byte{
acmeAccountStateKey: existingKey,
},
sharedSecret: map[string][]byte{}, // exists but no field for this tailnet
wantMemoryACME: existingKey,
wantSharedSecret: map[string][]byte{sharedField: existingKey},
},
{
name: "no_op_when_no_keys_anywhere",
certMode: "rw",
envSecretName: sharedSecretName,
envField: sharedField,
stateSecret: map[string][]byte{},
sharedSecret: map[string][]byte{},
wantMemoryACME: nil,
wantSharedSecret: map[string][]byte{},
},
{
name: "ro_mode_ignores_env_vars",
certMode: "ro",
envSecretName: sharedSecretName,
envField: sharedField,
stateSecret: map[string][]byte{
acmeAccountStateKey: freshKey,
},
sharedSecret: map[string][]byte{sharedField: existingKey},
wantMemoryACME: freshKey, // per-pod copy stays; shared Secret never consulted
},
{
name: "write_routes_to_shared_secret",
certMode: "rw",
envSecretName: sharedSecretName,
envField: sharedField,
stateSecret: map[string][]byte{},
sharedSecret: map[string][]byte{},
writeAfterInit: freshKey,
wantMemoryACME: freshKey,
wantSharedSecret: map[string][]byte{sharedField: freshKey},
wantStateSecret: map[string][]byte{}, // state Secret untouched by the ACME write
},
{
name: "write_in_ro_mode_goes_to_state_secret",
certMode: "ro",
envSecretName: sharedSecretName,
envField: sharedField,
stateSecret: map[string][]byte{},
sharedSecret: map[string][]byte{},
writeAfterInit: freshKey,
wantMemoryACME: freshKey,
wantSharedSecret: map[string][]byte{}, // env vars ignored in ro mode
wantStateSecret: map[string][]byte{acmeAccountStateKey: freshKey},
},
{
name: "no_env_vars_routes_to_state_secret",
certMode: "rw",
envSecretName: "", // shared account disabled
envField: "",
stateSecret: map[string][]byte{
acmeAccountStateKey: existingKey,
},
sharedSecret: nil, // does not exist; should not be touched
wantMemoryACME: existingKey,
wantStateSecret: map[string][]byte{acmeAccountStateKey: existingKey},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("TS_CERT_SHARE_MODE", tt.certMode)
t.Setenv("TS_ACME_ACCOUNT_SECRET_NAME", tt.envSecretName)
t.Setenv("TS_ACME_ACCOUNT_FIELD", tt.envField)
t.Setenv("POD_NAME", stateSecretName)
stateData := cloneMap(tt.stateSecret)
sharedData := cloneMap(tt.sharedSecret)
sharedExists := tt.sharedSecret != nil
client := &kubeclient.FakeClient{
GetSecretImpl: func(ctx context.Context, name string) (*kubeapi.Secret, error) {
switch name {
case stateSecretName:
return &kubeapi.Secret{Data: stateData}, nil
case sharedSecretName:
if !sharedExists {
return nil, &kubeapi.Status{Code: 404}
}
return &kubeapi.Secret{Data: sharedData}, nil
}
return nil, &kubeapi.Status{Code: 404}
},
CheckSecretPermissionsImpl: func(ctx context.Context, name string) (bool, bool, error) {
return true, true, nil
},
JSONPatchResourceImpl: func(ctx context.Context, name, resourceType string, patches []kubeclient.JSONPatch) error {
var target *map[string][]byte
switch name {
case stateSecretName:
target = &stateData
case sharedSecretName:
target = &sharedData
sharedExists = true
default:
t.Errorf("unexpected patch target Secret %q", name)
return nil
}
if *target == nil {
*target = map[string][]byte{}
}
for _, p := range patches {
if p.Op == "add" && p.Path == "/data" {
*target = p.Value.(map[string][]byte)
} else if p.Op == "add" && strings.HasPrefix(p.Path, "/data/") {
key := strings.TrimPrefix(p.Path, "/data/")
(*target)[key] = p.Value.([]byte)
}
}
return nil
},
CreateSecretImpl: func(ctx context.Context, s *kubeapi.Secret) error {
switch s.Name {
case stateSecretName:
stateData = s.Data
case sharedSecretName:
sharedData = s.Data
sharedExists = true
default:
t.Errorf("unexpected create target Secret %q", s.Name)
}
return nil
},
ListSecretsImpl: func(ctx context.Context, selector map[string]string) (*kubeapi.SecretList, error) {
// Used by ro-mode TLS Secret preload; irrelevant to this test.
return &kubeapi.SecretList{}, nil
},
}
s, err := newWithClient(t.Logf, client, stateSecretName)
if err != nil {
t.Fatalf("newWithClient: %v", err)
}
if tt.writeAfterInit != nil {
if err := s.WriteState(ipn.StateKey(acmeAccountStateKey), tt.writeAfterInit); err != nil {
t.Fatalf("WriteState(ACME key): %v", err)
}
}
gotMemACME, err := s.memory.ReadState(ipn.StateKey(acmeAccountStateKey))
if err != nil && tt.wantMemoryACME != nil {
t.Errorf("memory ReadState(ACME key): %v", err)
}
if !bytes.Equal(gotMemACME, tt.wantMemoryACME) {
t.Errorf("memory ACME key = %q, want %q", gotMemACME, tt.wantMemoryACME)
}
if tt.wantSharedSecret != nil {
if diff := cmp.Diff(sharedData, tt.wantSharedSecret); diff != "" {
t.Errorf("shared Secret contents mismatch (-got +want):\n%s", diff)
}
}
if tt.wantStateSecret != nil {
if diff := cmp.Diff(stateData, tt.wantStateSecret); diff != "" {
t.Errorf("state Secret contents mismatch (-got +want):\n%s", diff)
}
}
if !bytes.Equal(s.preAdoptedLocalKey, tt.wantPreAdopted) {
t.Errorf("preAdoptedLocalKey = %x, want %x", s.preAdoptedLocalKey, tt.wantPreAdopted)
}
})
}
}
func cloneMap(m map[string][]byte) map[string][]byte {
if m == nil {
return nil
}
out := make(map[string][]byte, len(m))
for k, v := range m {
out[k] = append([]byte(nil), v...)
}
return out
}
func sha256Sum(b []byte) []byte {
sum := sha256.Sum256(b)
return sum[:]
}
func TestShouldUseARIReplacesForRenewal(t *testing.T) {
const domain = "app.tailnetxyz.ts.net"
acmeKey := []byte("-----BEGIN PRIVATE KEY-----\ncurrent\n-----END PRIVATE KEY-----")
otherKey := []byte("-----BEGIN PRIVATE KEY-----\nother\n-----END PRIVATE KEY-----")
curFP := sha256Sum(acmeKey)
otherFP := sha256Sum(otherKey)
tests := []struct {
name string
certShareMode string
acmeInMemory []byte // per-pod ACME key present in memory
preAdopted []byte // sha256 of pre-adoption local key (foreign-key path)
certSecret map[string][]byte
certGetErr error
want bool
wantErr bool
}{
{
name: "non_rw_mode_returns_true",
certShareMode: "",
want: true,
},
{
name: "ro_mode_returns_true",
certShareMode: "ro",
want: true,
},
{
name: "no_acme_key_returns_true",
certShareMode: "rw",
want: true,
},
{
name: "cert_not_found_returns_true",
certShareMode: "rw",
acmeInMemory: acmeKey,
certGetErr: &kubeapi.Status{Code: 404},
want: true,
},
{
name: "cert_get_error_returns_true_with_err",
certShareMode: "rw",
acmeInMemory: acmeKey,
certGetErr: fmt.Errorf("api down"),
want: true,
wantErr: true,
},
{
name: "fingerprint_matches",
certShareMode: "rw",
acmeInMemory: acmeKey,
certSecret: map[string][]byte{keyACMEAcctFP: curFP},
want: true,
},
{
name: "fingerprint_differs",
certShareMode: "rw",
acmeInMemory: acmeKey,
certSecret: map[string][]byte{keyACMEAcctFP: otherFP},
want: false,
},
{
name: "legacy_cert_no_preadopted_returns_true",
certShareMode: "rw",
acmeInMemory: acmeKey,
certSecret: map[string][]byte{}, // no fingerprint field
want: true,
},
{
name: "legacy_cert_with_preadopted_returns_false",
certShareMode: "rw",
acmeInMemory: acmeKey,
preAdopted: sha256Sum(otherKey),
certSecret: map[string][]byte{}, // no fingerprint field
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := &kubeclient.FakeClient{
GetSecretImpl: func(ctx context.Context, name string) (*kubeapi.Secret, error) {
if tt.certGetErr != nil {
return nil, tt.certGetErr
}
return &kubeapi.Secret{Data: tt.certSecret}, nil
},
}
s := &Store{
client: client,
certShareMode: tt.certShareMode,
memory: mem.Store{},
preAdoptedLocalKey: tt.preAdopted,
logf: t.Logf,
}
if len(tt.acmeInMemory) > 0 {
s.memory.WriteState(ipn.StateKey(acmeAccountStateKey), tt.acmeInMemory)
}
got, err := s.ShouldUseARIReplacesForRenewal(domain)
if (err != nil) != tt.wantErr {
t.Errorf("err = %v, wantErr = %v", err, tt.wantErr)
}
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
func TestWriteTLSCertAndKeyStampsFingerprint(t *testing.T) {
const domain = "app.tailnetxyz.ts.net"
acmeKey := []byte("-----BEGIN PRIVATE KEY-----\naccount\n-----END PRIVATE KEY-----")
wantFP := sha256Sum(acmeKey)
tests := []struct {
name string
certMode string
acmeInMemory []byte
wantFP []byte // expected value of keyACMEAcctFP field; nil means field must be absent
}{
{
name: "rw_mode_with_acme_key_stamps_fingerprint",
certMode: "rw",
acmeInMemory: acmeKey,
wantFP: wantFP,
},
{
name: "rw_mode_no_acme_key_no_stamp",
certMode: "rw",
},
{
name: "non_share_mode_no_stamp",
certMode: "",
acmeInMemory: acmeKey,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
secret := map[string][]byte{}
client := &kubeclient.FakeClient{
GetSecretImpl: func(ctx context.Context, name string) (*kubeapi.Secret, error) {
return &kubeapi.Secret{Data: secret}, nil
},
CheckSecretPermissionsImpl: func(ctx context.Context, name string) (bool, bool, error) {
return true, true, nil
},
JSONPatchResourceImpl: func(ctx context.Context, name, resourceType string, patches []kubeclient.JSONPatch) error {
for _, p := range patches {
if p.Op == "add" && p.Path == "/data" {
secret = p.Value.(map[string][]byte)
} else if p.Op == "add" && strings.HasPrefix(p.Path, "/data/") {
secret[strings.TrimPrefix(p.Path, "/data/")] = p.Value.([]byte)
}
}
return nil
},
}
s := &Store{
client: client,
canPatch: true,
secretName: "ts-state",
certShareMode: tt.certMode,
memory: mem.Store{},
logf: t.Logf,
}
if len(tt.acmeInMemory) > 0 {
s.memory.WriteState(ipn.StateKey(acmeAccountStateKey), tt.acmeInMemory)
}
if err := s.WriteTLSCertAndKey(domain, []byte("cert"), []byte("key")); err != nil {
t.Fatalf("WriteTLSCertAndKey: %v", err)
}
gotFP, present := secret[keyACMEAcctFP]
if tt.wantFP == nil {
if present {
t.Errorf("unexpected fingerprint stamped: %x", gotFP)
}
return
}
if !bytes.Equal(gotFP, tt.wantFP) {
t.Errorf("fingerprint = %x, want %x", gotFP, tt.wantFP)
}
})
}
}