Skip to content

Agent Mesh

Implement proactive token refresh & admin revocation session management

Antonio Ojead1ca9ce57606
diff --git a/.github/k8s/sam-control-plane-template.yaml b/.github/k8s/sam-control-plane-template.yaml
index ab33eb1a..ba57bf93 100644
--- a/.github/k8s/sam-control-plane-template.yaml
+++ b/.github/k8s/sam-control-plane-template.yaml
@@ -224,6 +224,15 @@ spec:
- path:
type: Exact
value: /policies
+ - path:
+ type: Exact
+ value: /enroll
+ - path:
+ type: Exact
+ value: /enroll/status
+ - path:
+ type: Exact
+ value: /refresh
backendRefs:
- name: sam-control-plane-${ENV_NAME}
port: 8080
diff --git a/api/network.go b/api/network.go
index c6479341..b52c46ce 100644
--- a/api/network.go
+++ b/api/network.go
@@ -18,6 +18,7 @@ import (
"fmt"
"regexp"
"strings"
+ "time"
"github.com/libp2p/go-libp2p/core/protocol"
)
@@ -46,6 +47,26 @@ const (
DefaultAudience = "sam-mesh-audience"
)
+// ============================================================================
+// Token Lifespans & Session Constants
+// ============================================================================
+
+const (
+ // BiscuitTokenTTL is the strict cryptographically enforced lifespan
+ // of a minted Biscuit token (24 hours).
+ // This is verified locally by each peer on every connection.
+ BiscuitTokenTTL = 24 * time.Hour
+
+ // OIDCSessionTTL is the maximum database-enforced lifespan of a node's OIDC
+ // interactive enrollment session (90 days). After this period, the node
+ // must re-authenticate with the OIDC provider to establish a new session.
+ OIDCSessionTTL = 90 * 24 * time.Hour
+
+ // TokenRefreshCheckInterval is the frequency at which the node daemon and router check
+ // if their current Biscuit token is close to expiration and needs to be proactively refreshed.
+ TokenRefreshCheckInterval = 10 * time.Minute
+)
+
// ============================================================================
// SAM Custom HTTP Headers
// ============================================================================
diff --git a/api/sam.pb.go b/api/sam.pb.go
index 2024bb83..6cad9686 100644
--- a/api/sam.pb.go
+++ b/api/sam.pb.go
@@ -374,6 +374,7 @@ type EnrollRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
Jwt string `protobuf:"bytes,1,opt,name=jwt,proto3" json:"jwt,omitempty"`
PeerId string `protobuf:"bytes,2,opt,name=peer_id,json=peerId,proto3" json:"peer_id,omitempty"`
+ PublicKey []byte `protobuf:"bytes,3,opt,name=public_key,json=publicKey,proto3" json:"public_key,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -422,6 +423,13 @@ func (x *EnrollRequest) GetPeerId() string {
return ""
}
+func (x *EnrollRequest) GetPublicKey() []byte {
+ if x != nil {
+ return x.PublicKey
+ }
+ return nil
+}
+
type EnrollResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
BiscuitToken []byte `protobuf:"bytes,1,opt,name=biscuit_token,json=biscuitToken,proto3" json:"biscuit_token,omitempty"`
@@ -1372,6 +1380,214 @@ func (x *KeysResponse) GetPublicKeys() [][]byte {
return nil
}
+type TokenRefreshRequest struct {
+ state protoimpl.MessageState `protogen:"open.v1"`
+ ChallengeSignature []byte `protobuf:"bytes,1,opt,name=challenge_signature,json=challengeSignature,proto3" json:"challenge_signature,omitempty"`
+ Timestamp int64 `protobuf:"varint,2,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
+ unknownFields protoimpl.UnknownFields
+ sizeCache protoimpl.SizeCache
+}
+
+func (x *TokenRefreshRequest) Reset() {
+ *x = TokenRefreshRequest{}
+ mi := &file_api_sam_proto_msgTypes[20]
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ ms.StoreMessageInfo(mi)
+}
+
+func (x *TokenRefreshRequest) String() string {
+ return protoimpl.X.MessageStringOf(x)
+}
+
+func (*TokenRefreshRequest) ProtoMessage() {}
+
+func (x *TokenRefreshRequest) ProtoReflect() protoreflect.Message {
+ mi := &file_api_sam_proto_msgTypes[20]
+ if x != nil {
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ if ms.LoadMessageInfo() == nil {
+ ms.StoreMessageInfo(mi)
+ }
+ return ms
+ }
+ return mi.MessageOf(x)
+}
+
+// Deprecated: Use TokenRefreshRequest.ProtoReflect.Descriptor instead.
+func (*TokenRefreshRequest) Descriptor() ([]byte, []int) {
+ return file_api_sam_proto_rawDescGZIP(), []int{20}
+}
+
+func (x *TokenRefreshRequest) GetChallengeSignature() []byte {
+ if x != nil {
+ return x.ChallengeSignature
+ }
+ return nil
+}
+
+func (x *TokenRefreshRequest) GetTimestamp() int64 {
+ if x != nil {
+ return x.Timestamp
+ }
+ return 0
+}
+
+type TokenRefreshResponse struct {
+ state protoimpl.MessageState `protogen:"open.v1"`
+ BiscuitToken []byte `protobuf:"bytes,1,opt,name=biscuit_token,json=biscuitToken,proto3" json:"biscuit_token,omitempty"`
+ ExpiresAt int64 `protobuf:"varint,2,opt,name=expires_at,json=expiresAt,proto3" json:"expires_at,omitempty"`
+ ErrorMessage string `protobuf:"bytes,3,opt,name=error_message,json=errorMessage,proto3" json:"error_message,omitempty"`
+ unknownFields protoimpl.UnknownFields
+ sizeCache protoimpl.SizeCache
+}
+
+func (x *TokenRefreshResponse) Reset() {
+ *x = TokenRefreshResponse{}
+ mi := &file_api_sam_proto_msgTypes[21]
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ ms.StoreMessageInfo(mi)
+}
+
+func (x *TokenRefreshResponse) String() string {
+ return protoimpl.X.MessageStringOf(x)
+}
+
+func (*TokenRefreshResponse) ProtoMessage() {}
+
+func (x *TokenRefreshResponse) ProtoReflect() protoreflect.Message {
+ mi := &file_api_sam_proto_msgTypes[21]
+ if x != nil {
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ if ms.LoadMessageInfo() == nil {
+ ms.StoreMessageInfo(mi)
+ }
+ return ms
+ }
+ return mi.MessageOf(x)
+}
+
+// Deprecated: Use TokenRefreshResponse.ProtoReflect.Descriptor instead.
+func (*TokenRefreshResponse) Descriptor() ([]byte, []int) {
+ return file_api_sam_proto_rawDescGZIP(), []int{21}
+}
+
+func (x *TokenRefreshResponse) GetBiscuitToken() []byte {
+ if x != nil {
+ return x.BiscuitToken
+ }
+ return nil
+}
+
+func (x *TokenRefreshResponse) GetExpiresAt() int64 {
+ if x != nil {
+ return x.ExpiresAt
+ }
+ return 0
+}
+
+func (x *TokenRefreshResponse) GetErrorMessage() string {
+ if x != nil {
+ return x.ErrorMessage
+ }
+ return ""
+}
+
+type TokenRevokeRequest struct {
+ state protoimpl.MessageState `protogen:"open.v1"`
+ PeerId string `protobuf:"bytes,1,opt,name=peer_id,json=peerId,proto3" json:"peer_id,omitempty"`
+ unknownFields protoimpl.UnknownFields
+ sizeCache protoimpl.SizeCache
+}
+
+func (x *TokenRevokeRequest) Reset() {
+ *x = TokenRevokeRequest{}
+ mi := &file_api_sam_proto_msgTypes[22]
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ ms.StoreMessageInfo(mi)
+}
+
+func (x *TokenRevokeRequest) String() string {
+ return protoimpl.X.MessageStringOf(x)
+}
+
+func (*TokenRevokeRequest) ProtoMessage() {}
+
+func (x *TokenRevokeRequest) ProtoReflect() protoreflect.Message {
+ mi := &file_api_sam_proto_msgTypes[22]
+ if x != nil {
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ if ms.LoadMessageInfo() == nil {
+ ms.StoreMessageInfo(mi)
+ }
+ return ms
+ }
+ return mi.MessageOf(x)
+}
+
+// Deprecated: Use TokenRevokeRequest.ProtoReflect.Descriptor instead.
+func (*TokenRevokeRequest) Descriptor() ([]byte, []int) {
+ return file_api_sam_proto_rawDescGZIP(), []int{22}
+}
+
+func (x *TokenRevokeRequest) GetPeerId() string {
+ if x != nil {
+ return x.PeerId
+ }
+ return ""
+}
+
+type TokenRevokeResponse struct {
+ state protoimpl.MessageState `protogen:"open.v1"`
+ Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"`
+ Error string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
+ unknownFields protoimpl.UnknownFields
+ sizeCache protoimpl.SizeCache
+}
+
+func (x *TokenRevokeResponse) Reset() {
+ *x = TokenRevokeResponse{}
+ mi := &file_api_sam_proto_msgTypes[23]
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ ms.StoreMessageInfo(mi)
+}
+
+func (x *TokenRevokeResponse) String() string {
+ return protoimpl.X.MessageStringOf(x)
+}
+
+func (*TokenRevokeResponse) ProtoMessage() {}
+
+func (x *TokenRevokeResponse) ProtoReflect() protoreflect.Message {
+ mi := &file_api_sam_proto_msgTypes[23]
+ if x != nil {
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ if ms.LoadMessageInfo() == nil {
+ ms.StoreMessageInfo(mi)
+ }
+ return ms
+ }
+ return mi.MessageOf(x)
+}
+
+// Deprecated: Use TokenRevokeResponse.ProtoReflect.Descriptor instead.
+func (*TokenRevokeResponse) Descriptor() ([]byte, []int) {
+ return file_api_sam_proto_rawDescGZIP(), []int{23}
+}
+
+func (x *TokenRevokeResponse) GetSuccess() bool {
+ if x != nil {
+ return x.Success
+ }
+ return false
+}
+
+func (x *TokenRevokeResponse) GetError() string {
+ if x != nil {
+ return x.Error
+ }
+ return ""
+}
+
var File_api_sam_proto protoreflect.FileDescriptor
const file_api_sam_proto_rawDesc = "" +
@@ -1393,10 +1609,12 @@ const file_api_sam_proto_rawDesc = "" +
"\x04Type\x12\n" +
"\n" +
"\x06BANNED\x10\x00\x12\x10\n" +
- "\fKEY_ROTATION\x10\x01\":\n" +
+ "\fKEY_ROTATION\x10\x01\"Y\n" +
"\rEnrollRequest\x12\x10\n" +
"\x03jwt\x18\x01 \x01(\tR\x03jwt\x12\x17\n" +
- "\apeer_id\x18\x02 \x01(\tR\x06peerId\"\xc5\x01\n" +
+ "\apeer_id\x18\x02 \x01(\tR\x06peerId\x12\x1d\n" +
+ "\n" +
+ "public_key\x18\x03 \x01(\fR\tpublicKey\"\xc5\x01\n" +
"\x0eEnrollResponse\x12#\n" +
"\rbiscuit_token\x18\x01 \x01(\fR\fbiscuitToken\x12#\n" +
"\rerror_message\x18\x02 \x01(\tR\ferrorMessage\x12$\n" +
@@ -1468,7 +1686,20 @@ const file_api_sam_proto_rawDesc = "" +
"\x05error\x18\x02 \x01(\tR\x05error\"/\n" +
"\fKeysResponse\x12\x1f\n" +
"\vpublic_keys\x18\x01 \x03(\fR\n" +
- "publicKeys*\x94\x01\n" +
+ "publicKeys\"d\n" +
+ "\x13TokenRefreshRequest\x12/\n" +
+ "\x13challenge_signature\x18\x01 \x01(\fR\x12challengeSignature\x12\x1c\n" +
+ "\ttimestamp\x18\x02 \x01(\x03R\ttimestamp\"\x7f\n" +
+ "\x14TokenRefreshResponse\x12#\n" +
+ "\rbiscuit_token\x18\x01 \x01(\fR\fbiscuitToken\x12\x1d\n" +
+ "\n" +
+ "expires_at\x18\x02 \x01(\x03R\texpiresAt\x12#\n" +
+ "\rerror_message\x18\x03 \x01(\tR\ferrorMessage\"-\n" +
+ "\x12TokenRevokeRequest\x12\x17\n" +
+ "\apeer_id\x18\x01 \x01(\tR\x06peerId\"E\n" +
+ "\x13TokenRevokeResponse\x12\x18\n" +
+ "\asuccess\x18\x01 \x01(\bR\asuccess\x12\x14\n" +
+ "\x05error\x18\x02 \x01(\tR\x05error*\x94\x01\n" +
"\x10EnrollmentStatus\x12!\n" +
"\x1dENROLLMENT_STATUS_UNSPECIFIED\x10\x00\x12\x1d\n" +
"\x19ENROLLMENT_STATUS_PENDING\x10\x01\x12\x1e\n" +
@@ -1492,7 +1723,7 @@ func file_api_sam_proto_rawDescGZIP() []byte {
}
var file_api_sam_proto_enumTypes = make([]protoimpl.EnumInfo, 3)
-var file_api_sam_proto_msgTypes = make([]protoimpl.MessageInfo, 21)
+var file_api_sam_proto_msgTypes = make([]protoimpl.MessageInfo, 25)
var file_api_sam_proto_goTypes = []any{
(EnrollmentStatus)(0), // 0: sam.v1.EnrollmentStatus
(ServiceType)(0), // 1: sam.v1.ServiceType
@@ -1517,13 +1748,17 @@ var file_api_sam_proto_goTypes = []any{
(*PolicyConfigUpdateRequest)(nil), // 20: sam.v1.PolicyConfigUpdateRequest
(*PolicyConfigUpdateResponse)(nil), // 21: sam.v1.PolicyConfigUpdateResponse
(*KeysResponse)(nil), // 22: sam.v1.KeysResponse
- nil, // 23: sam.v1.CommandBackend.EnvEntry
+ (*TokenRefreshRequest)(nil), // 23: sam.v1.TokenRefreshRequest
+ (*TokenRefreshResponse)(nil), // 24: sam.v1.TokenRefreshResponse
+ (*TokenRevokeRequest)(nil), // 25: sam.v1.TokenRevokeRequest
+ (*TokenRevokeResponse)(nil), // 26: sam.v1.TokenRevokeResponse
+ nil, // 27: sam.v1.CommandBackend.EnvEntry
}
var file_api_sam_proto_depIdxs = []int32{
2, // 0: sam.v1.MeshEvent.type:type_name -> sam.v1.MeshEvent.Type
0, // 1: sam.v1.BootstrapEnrollResponse.status:type_name -> sam.v1.EnrollmentStatus
1, // 2: sam.v1.ServiceInfo.type:type_name -> sam.v1.ServiceType
- 23, // 3: sam.v1.CommandBackend.env:type_name -> sam.v1.CommandBackend.EnvEntry
+ 27, // 3: sam.v1.CommandBackend.env:type_name -> sam.v1.CommandBackend.EnvEntry
11, // 4: sam.v1.RegisterServiceRequest.service:type_name -> sam.v1.ServiceInfo
12, // 5: sam.v1.RegisterServiceRequest.command:type_name -> sam.v1.CommandBackend
6, // [6:6] is the sub-list for method output_type
@@ -1548,7 +1783,7 @@ func file_api_sam_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_api_sam_proto_rawDesc), len(file_api_sam_proto_rawDesc)),
NumEnums: 3,
- NumMessages: 21,
+ NumMessages: 25,
NumExtensions: 0,
NumServices: 0,
},
diff --git a/api/sam.proto b/api/sam.proto
index 21cdbed5..b1940a21 100644
--- a/api/sam.proto
+++ b/api/sam.proto
@@ -46,6 +46,7 @@ message MeshEvent {
message EnrollRequest {
string jwt = 1;
string peer_id = 2;
+ bytes public_key = 3;
}
message EnrollResponse {
@@ -153,5 +154,25 @@ message KeysResponse {
repeated bytes public_keys = 1;
}
+message TokenRefreshRequest {
+ bytes challenge_signature = 1;
+ int64 timestamp = 2;
+}
+
+message TokenRefreshResponse {
+ bytes biscuit_token = 1;
+ int64 expires_at = 2;
+ string error_message = 3;
+}
+
+message TokenRevokeRequest {
+ string peer_id = 1;
+}
+
+message TokenRevokeResponse {
+ bool success = 1;
+ string error = 2;
+}
+
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index 6ffebe94..c12a3f9f 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -20,6 +20,7 @@ import (
"crypto/rand"
"crypto/sha256"
"crypto/tls"
+ "encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
@@ -33,10 +34,12 @@ import (
"github.com/biscuit-auth/biscuit-go/v2"
"github.com/coreos/go-oidc/v3/oidc"
+ jwt "github.com/golang-jwt/jwt/v5"
"github.com/google/sam/api"
"github.com/google/sam/internal/identity"
"github.com/google/sam/internal/storage"
golog "github.com/ipfs/go-log/v2"
+ "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"golang.org/x/time/rate"
"google.golang.org/protobuf/proto"
@@ -165,9 +168,11 @@ func (s *Server) Start() error {
mux.HandleFunc("/policies", s.HandlePolicies)
mux.HandleFunc("/enroll", s.HandleEnroll)
mux.HandleFunc("/enroll/status", s.HandleEnrollStatus)
+ mux.HandleFunc("/refresh", s.HandleRefresh)
mux.HandleFunc("/admin/bootstrap-tokens", s.HandleAdminBootstrapTokens)
mux.HandleFunc("/admin/enrollments", s.HandleAdminEnrollments)
mux.HandleFunc("/admin/enrollments/", s.HandleAdminEnrollmentAction)
+ mux.HandleFunc("/admin/revoke", s.HandleAdminRevoke)
s.httpServer = &http.Server{
Handler: mux,
@@ -365,15 +370,46 @@ func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
}
// Mint token
- biscuitData, err := identity.MintBiscuitToken(privKey, claims, token, pID, policy)
+ biscuitExpiry := time.Now().Add(api.BiscuitTokenTTL)
+ biscuitData, resolvedRoles, err := identity.MintBiscuitToken(privKey, claims, token, pID, policy, biscuitExpiry)
if err != nil {
logger.Errorw("Biscuit minting failed", "peer_id", req.PeerId, "error", err)
http.Error(w, "Failed to mint biscuit: "+err.Error(), http.StatusInternalServerError)
return
}
+ // Session TTL is 90 days for OIDC interactive enrollment
+ sessionExpiresAt := time.Now().Add(api.OIDCSessionTTL)
+
+ // Determine primary role from resolved roles
+ primaryRole := api.RoleNode
+ for _, r := range resolvedRoles {
+ if strings.HasPrefix(r, "sam:role:") {
+ primaryRole = r
+ break
+ }
+ }
+
+ claimsBytes, err := json.Marshal(claims)
+ if err != nil {
+ logger.Errorf("Failed to marshal OIDC claims: %v", err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
+ nodeRecord := &storage.EnrolledNode{
+ PeerID: req.PeerId,
+ PublicKey: req.PublicKey,
+ Biscuit: biscuitData,
+ Role: primaryRole,
+ EnrollmentType: "OIDC",
+ ClaimsJSON: string(claimsBytes),
+ EnrolledAt: time.Now(),
+ ExpiresAt: sessionExpiresAt,
+ }
+
// Save to DB
- if err := s.store.EnrollNode(ctx, req.PeerId, biscuitData, token.Expiry); err != nil {
+ if err := s.store.EnrollNode(ctx, nodeRecord); err != nil {
logger.Errorf("Failed to persist node enrollment: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
@@ -410,6 +446,169 @@ func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(respData)
}
+// HandleRefresh HTTP POST `/refresh`
+func (s *Server) HandleRefresh(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost {
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+
+ ctx := r.Context()
+ authHeader := r.Header.Get("Authorization")
+ if !strings.HasPrefix(authHeader, "Bearer ") {
+ http.Error(w, "Missing current Biscuit token in Authorization header", http.StatusUnauthorized)
+ return
+ }
+ currentBiscuitBase64 := strings.TrimPrefix(authHeader, "Bearer ")
+ currentBiscuitBytes, err := base64.StdEncoding.DecodeString(currentBiscuitBase64)
+ if err != nil {
+ http.Error(w, "Malformed base64 token", http.StatusBadRequest)
+ return
+ }
+
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ http.Error(w, "Failed to read body", http.StatusBadRequest)
+ return
+ }
+
+ var req api.TokenRefreshRequest
+ if err := proto.Unmarshal(body, &req); err != nil {
+ http.Error(w, "Invalid request format", http.StatusBadRequest)
+ return
+ }
+
+ // Fetch all valid signing keys
+ validKeys, err := s.store.GetAllValidKeys(ctx)
+ if err != nil {
+ logger.Errorf("Failed to retrieve valid signing keys: %v", err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
+ var trustedKeys []ed25519.PublicKey
+ for _, k := range validKeys {
+ trustedKeys = append(trustedKeys, k.Public)
+ }
+
+ // Verify current biscuit signature and extract peer ID
+ pID, err := identity.VerifyAndExtractPeerID(trustedKeys, currentBiscuitBytes)
+ if err != nil {
+ logger.Warnw("Invalid biscuit presented for refresh", "error", err)
+ http.Error(w, "Invalid biscuit: "+err.Error(), http.StatusUnauthorized)
+ return
+ }
+
+ // Fetch node record
+ nodeRecord, err := s.store.GetNode(ctx, pID.String())
+ if err == storage.ErrNotFound {
+ logger.Warnw("Node not found for refresh", "peer_id", pID.String())
+ http.Error(w, "Node not enrolled", http.StatusUnauthorized)
+ return
+ } else if err != nil {
+ logger.Errorf("Failed to retrieve node record: %v", err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
+ if nodeRecord.Banned {
+ logger.Warnw("Banned node attempted refresh", "peer_id", pID.String())
+ http.Error(w, "Node is banned", http.StatusForbidden)
+ return
+ }
+
+ // Check session expiry (OIDC 90 days expiration)
+ if !nodeRecord.ExpiresAt.IsZero() && time.Now().After(nodeRecord.ExpiresAt) {
+ logger.Warnw("Session expired for node", "peer_id", pID.String(), "expires_at", nodeRecord.ExpiresAt)
+ http.Error(w, "Session expired, please re-enroll interactively", http.StatusUnauthorized)
+ return
+ }
+
+ // Verify challenge signature using stored node public key
+ pubKey, err := crypto.UnmarshalPublicKey(nodeRecord.PublicKey)
+ if err != nil {
+ logger.Errorf("Corrupted public key stored for node %s: %v", nodeRecord.PeerID, err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
+ challengeData := []byte(fmt.Sprintf("%d", req.Timestamp))
+ ok, err := pubKey.Verify(challengeData, req.ChallengeSignature)
+ if err != nil || !ok {
+ logger.Warnw("Challenge signature verification failed", "peer_id", nodeRecord.PeerID, "error", err)
+ http.Error(w, "Challenge signature verification failed", http.StatusUnauthorized)
+ return
+ }
+
+ // Fetch current signing private key and policy config
+ privKey, _, err := s.store.GetCurrentKey(ctx)
+ if err != nil {
+ logger.Errorf("Failed to retrieve current signing key: %v", err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
+ policy, err := s.store.GetPolicy(ctx)
+ if err != nil && err != storage.ErrNotFound {
+ logger.Errorf("Failed to retrieve mesh policy: %v", err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
+ var biscuitBytes []byte
+ biscuitExpiry := time.Now().Add(api.BiscuitTokenTTL)
+
+ if nodeRecord.EnrollmentType == "OIDC" {
+ var claims jwt.MapClaims
+ if err := json.Unmarshal([]byte(nodeRecord.ClaimsJSON), &claims); err != nil {
+ logger.Errorf("Failed to unmarshal OIDC claims for node %s: %v", nodeRecord.PeerID, err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+ bBytes, _, err := identity.MintBiscuitToken(privKey, claims, nil, pID, policy, biscuitExpiry)
+ if err != nil {
+ logger.Errorf("Failed to mint refreshed token for node %s: %v", nodeRecord.PeerID, err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+ biscuitBytes = bBytes
+ } else {
+ // Bootstrap node
+ bBytes, err := identity.MintBootstrapBiscuitToken(privKey, pID, nodeRecord.Role, biscuitExpiry, policy)
+ if err != nil {
+ logger.Errorf("Failed to mint refreshed token for node %s: %v", nodeRecord.PeerID, err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+ biscuitBytes = bBytes
+ }
+
+ // Update node record with new biscuit token
+ nodeRecord.Biscuit = biscuitBytes
+ nodeRecord.EnrolledAt = time.Now()
+ if err := s.store.EnrollNode(ctx, nodeRecord); err != nil {
+ logger.Errorf("Failed to persist node refresh: %v", err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
+ // Write response
+ resp := &api.TokenRefreshResponse{
+ BiscuitToken: biscuitBytes,
+ ExpiresAt: biscuitExpiry.Unix(),
+ }
+
+ respData, err := proto.Marshal(resp)
+ if err != nil {
+ http.Error(w, "Failed to serialize response", http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/x-protobuf")
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write(respData)
+}
+
// HandleKeys HTTP GET `/keys`
func (s *Server) HandleKeys(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
@@ -769,8 +968,7 @@ func (s *Server) HandleEnroll(w http.ResponseWriter, r *http.Request) {
if s.config.AutoApproveEnrollment {
// Mode A: Auto-Approve
- tokenTTL := 24 * time.Hour
- biscuitBytes, err := identity.MintBootstrapBiscuitToken(privKey, pID, tokenRecord.Role, time.Now().Add(tokenTTL), policy)
+ biscuitBytes, err := identity.MintBootstrapBiscuitToken(privKey, pID, tokenRecord.Role, time.Now().Add(api.BiscuitTokenTTL), policy)
if err != nil {
logger.Errorf("Failed to mint bootstrap biscuit: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
@@ -789,6 +987,21 @@ func (s *Server) HandleEnroll(w http.ResponseWriter, r *http.Request) {
return
}
+ nodeRecord := &storage.EnrolledNode{
+ PeerID: req.PeerId,
+ PublicKey: req.PublicKey,
+ Biscuit: biscuitBytes,
+ Role: tokenRecord.Role,
+ EnrollmentType: "BOOTSTRAP",
+ EnrolledAt: time.Now(),
+ ExpiresAt: time.Time{},
+ }
+ if err := s.store.EnrollNode(ctx, nodeRecord); err != nil {
+ logger.Errorf("Failed to enroll active bootstrap node: %v", err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
if err := s.store.IncrementBootstrapTokenUsage(ctx, tokenRecord.ID); err != nil {
logger.Errorf("Failed to increment token usage: %v", err)
}
@@ -1036,7 +1249,7 @@ func (s *Server) HandleAdminEnrollmentAction(w http.ResponseWriter, r *http.Requ
return
}
- biscuitBytes, err := identity.MintBootstrapBiscuitToken(privKey, pID, tokenRecord.Role, time.Now().Add(24*time.Hour), policy)
+ biscuitBytes, err := identity.MintBootstrapBiscuitToken(privKey, pID, tokenRecord.Role, time.Now().Add(api.BiscuitTokenTTL), policy)
if err != nil {
logger.Errorf("Failed to mint bootstrap biscuit: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
@@ -1050,6 +1263,21 @@ func (s *Server) HandleAdminEnrollmentAction(w http.ResponseWriter, r *http.Requ
return
}
+ nodeRecord := &storage.EnrolledNode{
+ PeerID: enrollReq.PeerID,
+ PublicKey: enrollReq.PublicKey,
+ Biscuit: biscuitBytes,
+ Role: tokenRecord.Role,
+ EnrollmentType: "BOOTSTRAP",
+ EnrolledAt: time.Now(),
+ ExpiresAt: time.Time{},
+ }
+ if err := s.store.EnrollNode(ctx, nodeRecord); err != nil {
+ logger.Errorf("Failed to enroll active bootstrap node: %v", err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
if err := s.store.IncrementBootstrapTokenUsage(ctx, tokenRecord.ID); err != nil {
logger.Errorf("Failed to increment token usage: %v", err)
}
@@ -1061,3 +1289,64 @@ func (s *Server) HandleAdminEnrollmentAction(w http.ResponseWriter, r *http.Requ
http.Error(w, "Invalid action", http.StatusBadRequest)
}
+
+// HandleAdminRevoke HTTP POST `/admin/revoke`
+func (s *Server) HandleAdminRevoke(w http.ResponseWriter, r *http.Request) {
+ if !s.checkAdminAuth(w, r) {
+ return
+ }
+
+ if r.Method != http.MethodPost {
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+
+ ctx := r.Context()
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ http.Error(w, "Failed to read body", http.StatusBadRequest)
+ return
+ }
+
+ var req api.TokenRevokeRequest
+ if err := proto.Unmarshal(body, &req); err != nil {
+ http.Error(w, "Invalid request format", http.StatusBadRequest)
+ return
+ }
+
+ if req.PeerId == "" {
+ http.Error(w, "peer_id is required", http.StatusBadRequest)
+ return
+ }
+
+ // Retrieve the node from storage to verify it exists
+ _, err = s.store.GetNode(ctx, req.PeerId)
+ if err == storage.ErrNotFound {
+ http.Error(w, "Node not found", http.StatusNotFound)
+ return
+ } else if err != nil {
+ logger.Errorf("Failed to retrieve node record for revocation: %v", err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
+ // Set node as banned (revoked)
+ if err := s.store.SetNodeBanned(ctx, req.PeerId, true); err != nil {
+ logger.Errorf("Failed to ban/revoke node %s: %v", req.PeerId, err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
+ resp := &api.TokenRevokeResponse{
+ Success: true,
+ }
+ respData, err := proto.Marshal(resp)
+ if err != nil {
+ http.Error(w, "Failed to serialize response", http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/x-protobuf")
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write(respData)
+}
diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.go
index b4ecf5fc..8ccca3c9 100644
--- a/internal/controlplane/server_test.go
+++ b/internal/controlplane/server_test.go
@@ -21,6 +21,7 @@ import (
"crypto/rsa"
"encoding/base64"
"encoding/json"
+ "fmt"
"io"
"math/big"
"net/http"
@@ -36,6 +37,7 @@ import (
"github.com/golang-jwt/jwt/v5"
"github.com/google/sam/api"
"github.com/google/sam/internal/storage"
+ "github.com/google/sam/internal/node"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"google.golang.org/protobuf/proto"
@@ -249,9 +251,11 @@ func TestNodeAndRouterRegistrationFlow(t *testing.T) {
"groups": []string{"users"},
})
+ nodePubKeyBytes, _ := crypto.MarshalPublicKey(privNode.GetPublic())
enrollNodeReq := &api.EnrollRequest{
- Jwt: nodeJWT,
- PeerId: nodePeer.String(),
+ Jwt: nodeJWT,
+ PeerId: nodePeer.String(),
+ PublicKey: nodePubKeyBytes,
}
reqData, _ := proto.Marshal(enrollNodeReq)
@@ -289,9 +293,11 @@ func TestNodeAndRouterRegistrationFlow(t *testing.T) {
"groups": []string{"routers"},
})
+ routerPubKeyBytes, _ := crypto.MarshalPublicKey(privRouter.GetPublic())
enrollRouterReq := &api.EnrollRequest{
- Jwt: routerJWT,
- PeerId: routerPeer.String(),
+ Jwt: routerJWT,
+ PeerId: routerPeer.String(),
+ PublicKey: routerPubKeyBytes,
}
reqData, _ = proto.Marshal(enrollRouterReq)
@@ -491,7 +497,7 @@ func TestEnrollmentWorkflow(t *testing.T) {
t.Fatal(err)
}
pID, _ := peer.IDFromPrivateKey(privNode)
- pubBytes, _ := pubNode.Raw()
+ pubBytes, _ := crypto.MarshalPublicKey(pubNode)
// 2. Submit Enrollment Request (Mode B -> PENDING)
enrollReq := &api.BootstrapEnrollRequest{
@@ -614,13 +620,14 @@ func TestEnrollmentWorkflow(t *testing.T) {
srv.config.AutoApproveEnrollment = true
// Generate new client key
- privNode2, _, _ := crypto.GenerateKeyPair(crypto.Ed25519, -1)
+ privNode2, pubNode2, _ := crypto.GenerateKeyPair(crypto.Ed25519, -1)
pID2, _ := peer.IDFromPrivateKey(privNode2)
+ pubBytes2, _ := crypto.MarshalPublicKey(pubNode2)
enrollReq2 := &api.BootstrapEnrollRequest{
BootstrapToken: tokenDetails.Token, // use remaining usage
PeerId: pID2.String(),
- PublicKey: pubBytes,
+ PublicKey: pubBytes2,
}
enrollReqData2, _ := proto.Marshal(enrollReq2)
@@ -640,3 +647,255 @@ func TestEnrollmentWorkflow(t *testing.T) {
t.Error("biscuit token empty in Auto-Approve response")
}
}
+
+func TestTokenRefreshAndRevocation(t *testing.T) {
+ issuer, mintToken := startCustomMockOIDC(t)
+ srv, store, cpURL := setupTestServer(t, issuer)
+ defer func() {
+ _ = srv.Close()
+ _ = store.Close()
+ }()
+
+ srv.config.AdminToken = "super-secret-admin-token"
+ ctx := context.Background()
+
+ // Setup policy configuration in the database
+ policy := &api.PolicyConfig{
+ Version: "v1alpha1",
+ Bindings: []api.Binding{
+ {Role: api.RoleNode, Members: []string{"group:users"}},
+ },
+ }
+ if err := store.SavePolicy(ctx, policy); err != nil {
+ t.Fatal(err)
+ }
+
+ // 1. Enroll client node via OIDC
+ privNode, pubNode, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ nodePeer, err := peer.IDFromPrivateKey(privNode)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ nodeJWT := mintToken(map[string]interface{}{
+ "sub": "node-alice",
+ "groups": []string{"users"},
+ })
+
+ nodePubKeyBytes, _ := crypto.MarshalPublicKey(pubNode)
+ enrollNodeReq := &api.EnrollRequest{
+ Jwt: nodeJWT,
+ PeerId: nodePeer.String(),
+ PublicKey: nodePubKeyBytes,
+ }
+ reqData, _ := proto.Marshal(enrollNodeReq)
+
+ client := &http.Client{Timeout: 5 * time.Second}
+ resp, err := client.Post(cpURL+"/register", "application/x-protobuf", bytes.NewReader(reqData))
+ if err != nil {
+ t.Fatalf("node /register failed: %v", err)
+ }
+ if resp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(resp.Body)
+ t.Fatalf("node /register status failure: %s (body: %s)", resp.Status, string(body))
+ }
+ body, _ := io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+
+ var enrollNodeResp api.EnrollResponse
+ if err := proto.Unmarshal(body, &enrollNodeResp); err != nil {
+ t.Fatalf("failed to unmarshal EnrollResponse: %v", err)
+ }
+ biscuitToken := enrollNodeResp.BiscuitToken
+
+ // 2. Perform refresh
+ timestamp := time.Now().UnixMilli()
+ challengeData := []byte(fmt.Sprintf("%d", timestamp))
+ challengeSig, err := privNode.Sign(challengeData)
+ if err != nil {
+ t.Fatalf("failed to generate challenge signature: %v", err)
+ }
+
+ refreshReq := &api.TokenRefreshRequest{
+ ChallengeSignature: challengeSig,
+ Timestamp: timestamp,
+ }
+ refreshData, _ := proto.Marshal(refreshReq)
+
+ reqRefresh, _ := http.NewRequest("POST", cpURL+"/refresh", bytes.NewReader(refreshData))
+ b64Biscuit := base64.StdEncoding.EncodeToString(biscuitToken)
+ reqRefresh.Header.Set("Authorization", "Bearer "+b64Biscuit)
+ reqRefresh.Header.Set("Content-Type", "application/x-protobuf")
+
+ resp, err = client.Do(reqRefresh)
+ if err != nil {
+ t.Fatalf("refresh failed: %v", err)
+ }
+ if resp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(resp.Body)
+ t.Fatalf("refresh response failure status %s: %s", resp.Status, string(body))
+ }
+ body, _ = io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+
+ var refreshResp api.TokenRefreshResponse
+ if err := proto.Unmarshal(body, &refreshResp); err != nil {
+ t.Fatalf("failed to unmarshal TokenRefreshResponse: %v", err)
+ }
+ if len(refreshResp.BiscuitToken) == 0 {
+ t.Fatal("refreshed biscuit token is empty")
+ }
+
+ // 3. Admin Revocation
+ revokeReq := &api.TokenRevokeRequest{
+ PeerId: nodePeer.String(),
+ }
+ revokeData, _ := proto.Marshal(revokeReq)
+
+ req, _ := http.NewRequest("POST", cpURL+"/admin/revoke", bytes.NewReader(revokeData))
+ req.Header.Set("Authorization", "Bearer super-secret-admin-token")
+ req.Header.Set("Content-Type", "application/x-protobuf")
+
+ resp, err = client.Do(req)
+ if err != nil {
+ t.Fatalf("admin revoke failed: %v", err)
+ }
+ if resp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(resp.Body)
+ t.Fatalf("admin revoke response status failure: %s (body: %s)", resp.Status, string(body))
+ }
+ _ = resp.Body.Close()
+
+ // 4. Verify refresh is rejected after revocation
+ reqRefreshCompromised, _ := http.NewRequest("POST", cpURL+"/refresh", bytes.NewReader(refreshData))
+ reqRefreshCompromised.Header.Set("Authorization", "Bearer "+b64Biscuit)
+ reqRefreshCompromised.Header.Set("Content-Type", "application/x-protobuf")
+
+ resp, err = client.Do(reqRefreshCompromised)
+ if err != nil {
+ t.Fatalf("refresh check failed: %v", err)
+ }
+ if resp.StatusCode != http.StatusForbidden {
+ t.Fatalf("expected refresh to be forbidden after revocation, got %s", resp.Status)
+ }
+ _ = resp.Body.Close()
+}
+
+func TestNodeProactiveTokenRefresh(t *testing.T) {
+ issuer, mintToken := startCustomMockOIDC(t)
+ srv, store, cpURL := setupTestServer(t, issuer)
+ defer func() {
+ _ = srv.Close()
+ _ = store.Close()
+ }()
+
+ srv.config.AdminToken = "super-secret-admin-token"
+ ctx := context.Background()
+
+ // Setup policy configuration in the database
+ policy := &api.PolicyConfig{
+ Version: "v1alpha1",
+ Bindings: []api.Binding{
+ {Role: api.RoleNode, Members: []string{"group:users"}},
+ },
+ }
+ if err := store.SavePolicy(ctx, policy); err != nil {
+ t.Fatal(err)
+ }
+
+ // Generate node keys
+ privNode, pubNode, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ nodePeer, err := peer.IDFromPrivateKey(privNode)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ nodeJWT := mintToken(map[string]interface{}{
+ "sub": "node-refresh-test",
+ "groups": []string{"users"},
+ })
+
+ // Enroll via registration endpoint
+ nodePubKeyBytes, _ := crypto.MarshalPublicKey(pubNode)
+ enrollNodeReq := &api.EnrollRequest{
+ Jwt: nodeJWT,
+ PeerId: nodePeer.String(),
+ PublicKey: nodePubKeyBytes,
+ }
+ reqData, _ := proto.Marshal(enrollNodeReq)
+
+ client := &http.Client{Timeout: 5 * time.Second}
+ resp, err := client.Post(cpURL+"/register", "application/x-protobuf", bytes.NewReader(reqData))
+ if err != nil {
+ t.Fatalf("node /register failed: %v", err)
+ }
+ if resp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(resp.Body)
+ t.Fatalf("node /register status failure: %s (body: %s)", resp.Status, string(body))
+ }
+ body, _ := io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+
+ var enrollNodeResp api.EnrollResponse
+ if err := proto.Unmarshal(body, &enrollNodeResp); err != nil {
+ t.Fatalf("failed to unmarshal EnrollResponse: %v", err)
+ }
+ biscuitToken := enrollNodeResp.BiscuitToken
+
+ // Set up local node Store
+ tempDir := t.TempDir()
+ nStore, err := node.NewStore(tempDir)
+ if err != nil {
+ t.Fatalf("failed to create node store: %v", err)
+ }
+
+ privKeyBytes, err := crypto.MarshalPrivateKey(privNode)
+ if err != nil {
+ t.Fatalf("failed to marshal private key: %v", err)
+ }
+ if err := nStore.SaveKey(privKeyBytes); err != nil {
+ t.Fatalf("failed to save private key: %v", err)
+ }
+ if err := nStore.SaveHubURL(cpURL); err != nil {
+ t.Fatalf("failed to save hub URL: %v", err)
+ }
+ if err := nStore.SaveIdentity(biscuitToken); err != nil {
+ t.Fatalf("failed to save initial identity: %v", err)
+ }
+ if err := nStore.SaveIdentityExpiration(enrollNodeResp.Expiration); err != nil {
+ t.Fatalf("failed to save initial expiration: %v", err)
+ }
+
+ n := &node.SamNode{
+ Store: nStore,
+ }
+
+ // Trigger proactive refresh
+ err = n.RefreshEnrollment(ctx)
+ if err != nil {
+ t.Fatalf("RefreshEnrollment failed: %v", err)
+ }
+
+ // Assert that token and expiration updated
+ refreshedToken, err := nStore.LoadIdentity()
+ if err != nil {
+ t.Fatal(err)
+ }
+ refreshedExpiration, err := nStore.LoadIdentityExpiration()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if bytes.Equal(refreshedToken, biscuitToken) {
+ t.Error("biscuit token did not change after refresh")
+ }
+ if refreshedExpiration <= enrollNodeResp.Expiration {
+ t.Errorf("expected refreshed expiration %d to be after initial expiration %d", refreshedExpiration, enrollNodeResp.Expiration)
+ }
+}
diff --git a/internal/identity/biscuit.go b/internal/identity/biscuit.go
index ba2bdbd0..3e7078dd 100644
--- a/internal/identity/biscuit.go
+++ b/internal/identity/biscuit.go
@@ -32,12 +32,9 @@ import (
)
// // MintBiscuitToken generates a signed Biscuit token for a peer with policy rules based on JWT claims.
-func MintBiscuitToken(signingKey ed25519.PrivateKey, claims jwt.MapClaims, token *oidc.IDToken, remotePeer peer.ID, policy *api.PolicyConfig) ([]byte, error) {
- if token == nil {
- return nil, fmt.Errorf("token cannot be nil")
- }
+func MintBiscuitToken(signingKey ed25519.PrivateKey, claims jwt.MapClaims, token *oidc.IDToken, remotePeer peer.ID, policy *api.PolicyConfig, biscuitExpiry time.Time) ([]byte, []string, error) {
if claims == nil {
- return nil, fmt.Errorf("claims cannot be nil")
+ return nil, nil, fmt.Errorf("claims cannot be nil")
}
oidcRoles := toStringSlice(claims["roles"])
@@ -109,7 +106,11 @@ func MintBiscuitToken(signingKey ed25519.PrivateKey, claims jwt.MapClaims, token
roles = append(roles, api.RoleNode)
}
- return mintBiscuit(signingKey, remotePeer, roles, token.Expiry, policy, claims)
+ biscuitBytes, err := mintBiscuit(signingKey, remotePeer, roles, biscuitExpiry, policy, claims)
+ if err != nil {
+ return nil, nil, err
+ }
+ return biscuitBytes, roles, nil
}
func mintBiscuit(signingKey ed25519.PrivateKey, remotePeer peer.ID, roles []string, expiration time.Time, policy *api.PolicyConfig, claims jwt.MapClaims) ([]byte, error) {
@@ -466,3 +467,67 @@ func toStringSlice(val any) []string {
func MintBootstrapBiscuitToken(signingKey ed25519.PrivateKey, remotePeer peer.ID, role string, expiration time.Time, policy *api.PolicyConfig) ([]byte, error) {
return mintBiscuit(signingKey, remotePeer, []string{role}, expiration, policy, nil)
}
+
+// VerifyAndExtractPeerID checks that the biscuit is signed by one of the trusted keys and returns the peer ID.
+// This function does NOT perform time checks, making it suitable for token refresh flows.
+func VerifyAndExtractPeerID(trustedPublicKeys []ed25519.PublicKey, biscuitData []byte) (peer.ID, error) {
+ b, err := biscuit.Unmarshal(biscuitData)
+ if err != nil {
+ return "", fmt.Errorf("malformed biscuit: %w", err)
+ }
+
+ var authorizer biscuit.Authorizer
+ var verified bool
+ var lastErr error
+
+ for _, pubKey := range trustedPublicKeys {
+ auth, err := b.Authorizer(pubKey)
+ if err != nil {
+ lastErr = err
+ continue
+ }
+ authorizer = auth
+ verified = true
+ break
+ }
+
+ if !verified {
+ return "", fmt.Errorf("signature verification failed: %v", lastErr)
+ }
+
+ // Extract the peer ID using Datalog query
+ peerRule, err := parser.FromStringRule(`get_peer($p) <- node($p)`)
+ if err != nil {
+ return "", fmt.Errorf("failed to parse query rule: %w", err)
+ }
+
+ // Trigger datalog engine evaluation to copy token facts to authorizer world
+ _ = authorizer.Authorize()
+
+ facts, err := authorizer.Query(peerRule)
+ if err != nil {
+ return "", fmt.Errorf("query failed: %w", err)
+ }
+
+ if len(facts) == 0 {
+ return "", fmt.Errorf("no node fact found in biscuit. Authorizer state: %s", authorizer.PrintWorld())
+ }
+
+ // Extract value from fact
+ pred := facts[0].Predicate
+ if len(pred.IDs) != 1 {
+ return "", fmt.Errorf("unexpected fact structure")
+ }
+
+ strVal, ok := pred.IDs[0].(biscuit.String)
+ if !ok {
+ return "", fmt.Errorf("node fact value is not a string")
+ }
+
+ pID, err := peer.Decode(string(strVal))
+ if err != nil {
+ return "", fmt.Errorf("invalid peer ID in biscuit: %w", err)
+ }
+
+ return pID, nil
+}
diff --git a/internal/identity/biscuit_test.go b/internal/identity/biscuit_test.go
index 6f23ebc6..36255c31 100644
--- a/internal/identity/biscuit_test.go
+++ b/internal/identity/biscuit_test.go
@@ -70,7 +70,7 @@ func TestMintBiscuitToken(t *testing.T) {
claims1 := jwt.MapClaims{
"roles": []any{"admin"},
}
- biscuitData1, err := MintBiscuitToken(priv, claims1, token, dummyPeer, policy)
+ biscuitData1, _, err := MintBiscuitToken(priv, claims1, token, dummyPeer, policy, token.Expiry)
if err != nil {
t.Fatal(err)
}
@@ -104,7 +104,7 @@ func TestMintBiscuitToken(t *testing.T) {
claims2 := jwt.MapClaims{
"groups": []any{"system:serviceaccounts:sam-canary"},
}
- biscuitData2, err := MintBiscuitToken(priv, claims2, token, dummyPeer, policy)
+ biscuitData2, _, err := MintBiscuitToken(priv, claims2, token, dummyPeer, policy, token.Expiry)
if err != nil {
t.Fatal(err)
}
@@ -135,7 +135,7 @@ func TestMintBiscuitToken(t *testing.T) {
"groups": []any{"unknown-group"},
"roles": []any{"undefined-role"},
}
- biscuitData3, err := MintBiscuitToken(priv, claims3, token, dummyPeer, policy)
+ biscuitData3, _, err := MintBiscuitToken(priv, claims3, token, dummyPeer, policy, token.Expiry)
if err != nil {
t.Fatal(err)
}
@@ -164,7 +164,7 @@ func TestMintBiscuitToken(t *testing.T) {
claims4 := jwt.MapClaims{
"sub": "system:serviceaccount:sam-canary:sam-node-sa",
}
- biscuitData4, err := MintBiscuitToken(priv, claims4, token, dummyPeer, policy)
+ biscuitData4, _, err := MintBiscuitToken(priv, claims4, token, dummyPeer, policy, token.Expiry)
if err != nil {
t.Fatal(err)
}
@@ -231,7 +231,7 @@ func TestVerifyBiscuit_Expiration(t *testing.T) {
"roles": []any{"admin"},
}
- biscuitData, err := MintBiscuitToken(priv, claims, token, dummyPeer, &api.PolicyConfig{})
+ biscuitData, _, err := MintBiscuitToken(priv, claims, token, dummyPeer, &api.PolicyConfig{}, token.Expiry)
if err != nil {
t.Fatalf("MintBiscuitToken failed: %v", err)
}
@@ -283,7 +283,7 @@ func TestMintBiscuitToken_ClaimsTranslation(t *testing.T) {
"groups": []any{"beta-testers", "engineering"},
}
- biscuitData, err := MintBiscuitToken(priv, claims, token, dummyPeer, policy)
+ biscuitData, _, err := MintBiscuitToken(priv, claims, token, dummyPeer, policy, token.Expiry)
if err != nil {
t.Fatalf("Failed to mint biscuit: %v", err)
}
@@ -426,7 +426,7 @@ func TestMintBiscuitToken_VariousClaimsTypes(t *testing.T) {
claims["groups"] = tt.groupsClaim
}
- biscuitData, err := MintBiscuitToken(priv, claims, token, dummyPeer, policy)
+ biscuitData, _, err := MintBiscuitToken(priv, claims, token, dummyPeer, policy, token.Expiry)
if err != nil {
t.Fatalf("MintBiscuitToken failed: %v", err)
}
@@ -498,7 +498,7 @@ func TestVerifyBiscuit_Concurrent(t *testing.T) {
"roles": []any{"admin"},
}
- biscuitData, err := MintBiscuitToken(priv, claims, token, dummyPeer, &api.PolicyConfig{})
+ biscuitData, _, err := MintBiscuitToken(priv, claims, token, dummyPeer, &api.PolicyConfig{}, token.Expiry)
if err != nil {
t.Fatalf("MintBiscuitToken failed: %v", err)
}
@@ -556,7 +556,7 @@ func TestMintBiscuitToken_ErrorAggregation(t *testing.T) {
"roles": []any{"admin"},
}
- _, err = MintBiscuitToken(priv, claims, token, dummyPeer, policy)
+ _, _, err = MintBiscuitToken(priv, claims, token, dummyPeer, policy, token.Expiry)
if err == nil {
t.Fatal("Expected MintBiscuitToken to fail on custom datalog syntax errors, got nil error")
}
@@ -608,7 +608,7 @@ func TestMintBiscuitToken_FactDeduplication(t *testing.T) {
"sub": "user-1",
}
- biscuitData, err := MintBiscuitToken(priv, claims, token, dummyPeer, policy)
+ biscuitData, _, err := MintBiscuitToken(priv, claims, token, dummyPeer, policy, token.Expiry)
if err != nil {
t.Fatalf("MintBiscuitToken failed with duplicate facts: %v", err)
}
diff --git a/internal/node/enroll.go b/internal/node/enroll.go
index 1ef64106..44d31a0f 100644
--- a/internal/node/enroll.go
+++ b/internal/node/enroll.go
@@ -56,9 +56,16 @@ func GetOrGenerateKey(s *Store) crypto.PrivKey {
}
func (n *SamNode) Enroll(ctx context.Context, hubURL string, jwt string) error {
+ pubKey := n.Host.Peerstore().PubKey(n.Host.ID())
+ pubBytes, err := crypto.MarshalPublicKey(pubKey)
+ if err != nil {
+ return fmt.Errorf("failed to marshal public key: %w", err)
+ }
+
req := &api.EnrollRequest{
- Jwt: jwt,
- PeerId: n.Host.ID().String(),
+ Jwt: jwt,
+ PeerId: n.Host.ID().String(),
+ PublicKey: pubBytes,
}
data, err := proto.Marshal(req)
if err != nil {
diff --git a/internal/node/node.go b/internal/node/node.go
index fe458e2c..11dc7cf8 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -25,6 +25,7 @@ import (
"encoding/base64"
"errors"
"fmt"
+ "io"
"net"
"net/http"
"os"
@@ -45,6 +46,7 @@ import (
gostream "github.com/libp2p/go-libp2p-gostream"
dht "github.com/libp2p/go-libp2p-kad-dht"
pubsub "github.com/libp2p/go-libp2p-pubsub"
+ "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/discovery"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/host"
@@ -88,10 +90,13 @@ const (
// Reprovide interval
ReprovideInterval = 5 * time.Minute
+)
+
+var (
// Renewal timing defaults
- DefaultRenewalFallback = 50 * time.Minute
- RenewalBuffer = 10 * time.Minute
- RenewalThreshold = 15 * time.Minute
+ DefaultRenewalFallback = (api.BiscuitTokenTTL * 8) / 10 // 80% of TTL (19.2h)
+ RenewalBuffer = api.BiscuitTokenTTL / 5 // 20% of TTL (4.8h)
+ RenewalThreshold = api.BiscuitTokenTTL / 4 // 25% of TTL (6h)
)
var ErrFatalAuth = errors.New("fatal authentication error")
@@ -835,6 +840,16 @@ func (n *SamNode) StartRenewalLoop(ctx context.Context, issuerURL, clientID, cli
return
case <-timer.C:
fmt.Println("Renewing enrollment...")
+
+ // Try proactive biscuit token refresh first
+ refreshErr := n.RefreshEnrollment(ctx)
+ if refreshErr == nil {
+ logger.Infof("Enrollment renewed successfully via proactive refresh.")
+ continue
+ }
+
+ logger.Warnw("Proactive biscuit refresh failed, falling back to full OIDC/JWT re-enrollment", "error", refreshErr)
+
var newJWT string
var fetchErr error
@@ -886,6 +901,118 @@ func (n *SamNode) StartRenewalLoop(ctx context.Context, issuerURL, clientID, cli
}()
}
+type RefreshError struct {
+ StatusCode int
+ Message string
+}
+
+func (e *RefreshError) Error() string {
+ return e.Message
+}
+
+// RefreshEnrollment trades the expiring biscuit token for a new one using a cryptographic challenge.
+func (n *SamNode) RefreshEnrollment(ctx context.Context) error {
+ // 1. Fetch current biscuit
+ currentBiscuit, err := n.Store.LoadIdentity()
+ if err != nil {
+ return fmt.Errorf("failed to load current identity: %w", err)
+ }
+
+ // 2. Load private key
+ privKeyBytes, err := n.Store.LoadKey()
+ if err != nil {
+ return fmt.Errorf("failed to load private key: %w", err)
+ }
+ privKey, err := crypto.UnmarshalPrivateKey(privKeyBytes)
+ if err != nil {
+ return fmt.Errorf("corrupted private key: %w", err)
+ }
+
+ // 3. Generate challenge signature over current timestamp
+ timestamp := time.Now().UnixMilli()
+ challengeData := []byte(fmt.Sprintf("%d", timestamp))
+ sig, err := privKey.Sign(challengeData)
+ if err != nil {
+ return fmt.Errorf("failed to generate signature: %w", err)
+ }
+
+ // 4. Construct request
+ req := &api.TokenRefreshRequest{
+ ChallengeSignature: sig,
+ Timestamp: timestamp,
+ }
+ reqData, err := proto.Marshal(req)
+ if err != nil {
+ return fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ hubURL, err := n.Store.LoadHubURL()
+ if err != nil {
+ return fmt.Errorf("failed to load hub URL: %w", err)
+ }
+
+ if !strings.HasPrefix(hubURL, "http://") && !strings.HasPrefix(hubURL, "https://") {
+ return fmt.Errorf("hub address must be an HTTP or HTTPS URL for renewal: %s", hubURL)
+ }
+
+ url := hubURL + "/refresh"
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(reqData))
+ if err != nil {
+ return fmt.Errorf("failed to create http request: %w", err)
+ }
+ httpReq.Header.Set("Content-Type", "application/x-protobuf")
+ // Set current biscuit in authorization header
+ b64Biscuit := base64.StdEncoding.EncodeToString(currentBiscuit)
+ httpReq.Header.Set("Authorization", "Bearer "+b64Biscuit)
+
+ client := &http.Client{Timeout: 30 * time.Second}
+ resp, err := client.Do(httpReq)
+ if err != nil {
+ return fmt.Errorf("http request failed: %w", err)
+ }
+ defer func() { _ = resp.Body.Close() }()
+
+ if resp.StatusCode == http.StatusForbidden {
+ logger.Errorf("Refresh rejected: Node is banned (403 Forbidden). Initiating hard-kill.")
+ if n.Host != nil {
+ _ = n.Host.Close()
+ }
+ os.Exit(1)
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(resp.Body)
+ return &RefreshError{
+ StatusCode: resp.StatusCode,
+ Message: fmt.Sprintf("refresh failed with status %s: %s", resp.Status, string(body)),
+ }
+ }
+
+ respData, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return fmt.Errorf("failed to read response: %w", err)
+ }
+
+ var refreshResp api.TokenRefreshResponse
+ if err := proto.Unmarshal(respData, &refreshResp); err != nil {
+ return fmt.Errorf("failed to unmarshal response: %w", err)
+ }
+
+ if refreshResp.ErrorMessage != "" {
+ return fmt.Errorf("refresh error: %s", refreshResp.ErrorMessage)
+ }
+
+ // Save new biscuit and its expiration
+ if err := n.Store.SaveIdentity(refreshResp.BiscuitToken); err != nil {
+ return fmt.Errorf("failed to save refreshed identity: %w", err)
+ }
+ if err := n.Store.SaveIdentityExpiration(refreshResp.ExpiresAt); err != nil {
+ return fmt.Errorf("failed to save refreshed expiration: %w", err)
+ }
+
+ return nil
+}
+
func (n *SamNode) renewWithRefreshToken(ctx context.Context, clientSecret string) (string, error) {
if n.Store == nil {
return "", fmt.Errorf("store is not initialized")
diff --git a/internal/router/router.go b/internal/router/router.go
index 485975f5..2f20dc12 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -18,6 +18,7 @@ import (
"bytes"
"context"
"crypto/ed25519"
+ "encoding/base64"
"errors"
"fmt"
"io"
@@ -89,6 +90,7 @@ type Router struct {
// Keys & Identity
biscuitToken []byte
+ biscuitExpiration time.Time
trustedPublicKeys []ed25519.PublicKey
keysMu sync.RWMutex
privKey crypto.PrivKey
@@ -251,10 +253,11 @@ func (r *Router) Start() error {
})
// 5. Start background routines
- r.wg.Add(3)
+ r.wg.Add(4)
go r.runLeaseRenewalLoop()
go r.runKeysSyncLoop()
go r.runFederationLoop()
+ go r.runBiscuitRenewalLoop()
r.isReady.Store(true)
logger.Infof("Router Online. PeerID: %s, ListenAddrs: %v", r.Host.ID(), r.Host.Addrs())
@@ -262,9 +265,16 @@ func (r *Router) Start() error {
}
func (r *Router) enroll(peerID peer.ID) error {
+ pubKey := r.privKey.GetPublic()
+ pubBytes, err := crypto.MarshalPublicKey(pubKey)
+ if err != nil {
+ return fmt.Errorf("failed to marshal public key: %w", err)
+ }
+
req := &api.EnrollRequest{
- Jwt: r.config.OIDCToken,
- PeerId: peerID.String(),
+ Jwt: r.config.OIDCToken,
+ PeerId: peerID.String(),
+ PublicKey: pubBytes,
}
data, err := proto.Marshal(req)
if err != nil {
@@ -294,6 +304,7 @@ func (r *Router) enroll(peerID peer.ID) error {
}
r.biscuitToken = enrollResp.BiscuitToken
+ r.biscuitExpiration = time.Unix(enrollResp.Expiration, 0)
r.keysMu.Lock()
r.trustedPublicKeys = []ed25519.PublicKey{enrollResp.HubPublicKey}
r.keysMu.Unlock()
@@ -410,6 +421,7 @@ func (r *Router) enrollBootstrap(peerID peer.ID) error {
}
r.biscuitToken = enrollResp.BiscuitToken
+ r.biscuitExpiration = time.Unix(enrollResp.Expiration, 0)
r.keysMu.Lock()
r.trustedPublicKeys = []ed25519.PublicKey{enrollResp.HubPublicKey}
r.keysMu.Unlock()
@@ -805,3 +817,105 @@ func isLoopbackOrLinkLocal(addr multiaddr.Multiaddr) bool {
}
return false
}
+
+// RefreshEnrollment trades the expiring biscuit token for a new one using a cryptographic challenge.
+func (r *Router) RefreshEnrollment(ctx context.Context) error {
+ r.keysMu.Lock()
+ defer r.keysMu.Unlock()
+
+ if len(r.biscuitToken) == 0 {
+ return fmt.Errorf("router not enrolled (no biscuit)")
+ }
+
+ // 1. Generate challenge signature over current timestamp
+ timestamp := time.Now().UnixMilli()
+ challengeData := []byte(fmt.Sprintf("%d", timestamp))
+ sig, err := r.privKey.Sign(challengeData)
+ if err != nil {
+ return fmt.Errorf("failed to generate signature: %w", err)
+ }
+
+ // 2. Construct request
+ req := &api.TokenRefreshRequest{
+ ChallengeSignature: sig,
+ Timestamp: timestamp,
+ }
+ reqData, err := proto.Marshal(req)
+ if err != nil {
+ return fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ url := r.config.ControlPlaneURL + "/refresh"
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(reqData))
+ if err != nil {
+ return fmt.Errorf("failed to create http request: %w", err)
+ }
+ httpReq.Header.Set("Content-Type", "application/x-protobuf")
+ // Set current biscuit in authorization header
+ b64Biscuit := base64.StdEncoding.EncodeToString(r.biscuitToken)
+ httpReq.Header.Set("Authorization", "Bearer "+b64Biscuit)
+
+ client := &http.Client{Timeout: 10 * time.Second}
+ resp, err := client.Do(httpReq)
+ if err != nil {
+ return fmt.Errorf("http request failed: %w", err)
+ }
+ defer func() { _ = resp.Body.Close() }()
+
+ if resp.StatusCode == http.StatusForbidden {
+ logger.Errorf("Refresh rejected: Router is banned (403 Forbidden). Hard-killing router.")
+ if r.Host != nil {
+ _ = r.Host.Close()
+ }
+ os.Exit(1)
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(resp.Body)
+ return fmt.Errorf("refresh failed with status %s: %s", resp.Status, string(body))
+ }
+
+ respData, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return fmt.Errorf("failed to read response: %w", err)
+ }
+
+ var refreshResp api.TokenRefreshResponse
+ if err := proto.Unmarshal(respData, &refreshResp); err != nil {
+ return fmt.Errorf("failed to unmarshal response: %w", err)
+ }
+
+ if refreshResp.ErrorMessage != "" {
+ return fmt.Errorf("refresh error: %s", refreshResp.ErrorMessage)
+ }
+
+ // Update local biscuit token and expiration
+ r.biscuitToken = refreshResp.BiscuitToken
+ r.biscuitExpiration = time.Unix(refreshResp.ExpiresAt, 0)
+ logger.Infof("Router biscuit token refreshed successfully.")
+ return nil
+}
+
+func (r *Router) runBiscuitRenewalLoop() {
+ defer r.wg.Done()
+ ticker := time.NewTicker(api.TokenRefreshCheckInterval)
+ defer ticker.Stop()
+
+ for {
+ select {
+ case <-ticker.C:
+ r.keysMu.RLock()
+ expiration := r.biscuitExpiration
+ r.keysMu.RUnlock()
+
+ if !expiration.IsZero() && time.Until(expiration) < api.BiscuitTokenTTL/5 { // 80% elapsed lifespan of 24h (remaining time < 20% of TTL)
+ logger.Infof("Router biscuit expiring in %v, triggering proactive refresh...", time.Until(expiration))
+ if err := r.RefreshEnrollment(r.ctx); err != nil {
+ logger.Errorf("Proactive router biscuit refresh failed: %v", err)
+ }
+ }
+ case <-r.ctx.Done():
+ return
+ }
+ }
+}
diff --git a/internal/router/router_test.go b/internal/router/router_test.go
index 427c4caf..125475a2 100644
--- a/internal/router/router_test.go
+++ b/internal/router/router_test.go
@@ -36,6 +36,7 @@ import (
"github.com/google/sam/internal/identity"
"github.com/google/sam/internal/storage"
"github.com/libp2p/go-libp2p"
+ "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-msgio"
"github.com/multiformats/go-multiaddr"
@@ -223,9 +224,14 @@ func TestRouterIntegration(t *testing.T) {
// Fetch biscuit for client node via Control Plane HTTP client
client := &http.Client{Timeout: 5 * time.Second}
+ nodePubKeyBytes, err := crypto.MarshalPublicKey(nodePrivKey.GetPublic())
+ if err != nil {
+ t.Fatal(err)
+ }
enrollNodeReq := &api.EnrollRequest{
- Jwt: nodeJWT,
- PeerId: nodePeerID.String(),
+ Jwt: nodeJWT,
+ PeerId: nodePeerID.String(),
+ PublicKey: nodePubKeyBytes,
}
reqData, _ := proto.Marshal(enrollNodeReq)
resp, err := client.Post(cpURL+"/register", "application/x-protobuf", bytes.NewReader(reqData))
@@ -390,3 +396,63 @@ func TestRouterFederation(t *testing.T) {
t.Errorf("Router 1 is not authenticated in Router 2's peers")
}
}
+
+func TestRouterProactiveTokenRefresh(t *testing.T) {
+ issuer, mintToken := startCustomMockOIDC(t)
+ cp, cpStore, cpURL := setupControlPlane(t, issuer)
+ defer func() {
+ _ = cp.Close()
+ _ = cpStore.Close()
+ }()
+
+ tempDir := t.TempDir()
+ routerKeyPath := filepath.Join(tempDir, "router.key")
+
+ routerJWT := mintToken(map[string]interface{}{
+ "sub": "router-refresh-test",
+ "groups": []string{"routers"},
+ })
+
+ rOpts := Options{
+ ControlPlaneURL: cpURL,
+ ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},
+ KeysSyncInterval: 20 * time.Second,
+ LeaseRenewInterval: 20 * time.Second,
+ OIDCToken: routerJWT,
+ KeysDBPath: routerKeyPath,
+ AllowLoopback: true,
+ BiscuitTimeout: 1 * time.Second,
+ }
+
+ r, err := NewRouter(context.Background(), rOpts)
+ if err != nil {
+ t.Fatalf("failed to create router: %v", err)
+ }
+
+ if err := r.Start(); err != nil {
+ t.Fatalf("failed to start router: %v", err)
+ }
+ defer func() { _ = r.Close() }()
+
+ // Capture initial token and expiration
+ initialToken := r.biscuitToken
+ initialExpiration := r.biscuitExpiration
+
+ if len(initialToken) == 0 {
+ t.Fatal("initial router biscuit token is empty")
+ }
+
+ // Trigger proactive refresh manually
+ err = r.RefreshEnrollment(context.Background())
+ if err != nil {
+ t.Fatalf("manually triggered RefreshEnrollment failed: %v", err)
+ }
+
+ // Assert that token and expiration updated
+ if bytes.Equal(r.biscuitToken, initialToken) {
+ t.Error("biscuit token did not change after refresh")
+ }
+ if !r.biscuitExpiration.After(initialExpiration) {
+ t.Errorf("expected refreshed expiration %v to be after initial expiration %v", r.biscuitExpiration, initialExpiration)
+ }
+}
diff --git a/internal/storage/sql_store.go b/internal/storage/sql_store.go
index 2f8264d2..25a84a68 100644
--- a/internal/storage/sql_store.go
+++ b/internal/storage/sql_store.go
@@ -89,7 +89,11 @@ func (s *SQLStore) initSchema() error {
nodesSchema = `
CREATE TABLE IF NOT EXISTS nodes (
peer_id VARCHAR(255) PRIMARY KEY,
+ public_key BYTEA NOT NULL,
biscuit_token BYTEA NOT NULL,
+ role VARCHAR(64) NOT NULL,
+ enrollment_type VARCHAR(64) NOT NULL,
+ claims_json TEXT,
enrolled_at BIGINT NOT NULL,
expires_at BIGINT NOT NULL,
banned BOOLEAN DEFAULT FALSE NOT NULL
@@ -142,7 +146,11 @@ func (s *SQLStore) initSchema() error {
nodesSchema = `
CREATE TABLE IF NOT EXISTS nodes (
peer_id TEXT PRIMARY KEY,
+ public_key BLOB NOT NULL,
biscuit_token BLOB NOT NULL,
+ role TEXT NOT NULL,
+ enrollment_type TEXT NOT NULL,
+ claims_json TEXT,
enrolled_at BIGINT NOT NULL,
expires_at BIGINT NOT NULL,
banned BOOLEAN DEFAULT FALSE NOT NULL
@@ -294,32 +302,51 @@ func (s *SQLStore) SaveInitialKey(ctx context.Context, priv ed25519.PrivateKey,
}
// EnrollNode implements Store.
-func (s *SQLStore) EnrollNode(ctx context.Context, peerID string, biscuit []byte, expiresAt time.Time) error {
+func (s *SQLStore) EnrollNode(ctx context.Context, node *EnrolledNode) error {
var query string
if s.isPostgres() {
query = s.rebind(`
- INSERT INTO nodes (peer_id, biscuit_token, enrolled_at, expires_at, banned)
- VALUES (?, ?, ?, ?, FALSE)
+ INSERT INTO nodes (peer_id, public_key, biscuit_token, role, enrollment_type, claims_json, enrolled_at, expires_at, banned)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, FALSE)
ON CONFLICT (peer_id)
- DO UPDATE SET biscuit_token = EXCLUDED.biscuit_token, enrolled_at = EXCLUDED.enrolled_at, expires_at = EXCLUDED.expires_at`)
+ DO UPDATE SET public_key = EXCLUDED.public_key, biscuit_token = EXCLUDED.biscuit_token, role = EXCLUDED.role, enrollment_type = EXCLUDED.enrollment_type, claims_json = EXCLUDED.claims_json, enrolled_at = EXCLUDED.enrolled_at, expires_at = EXCLUDED.expires_at`)
} else {
query = s.rebind(`
- INSERT INTO nodes (peer_id, biscuit_token, enrolled_at, expires_at, banned)
- VALUES (?, ?, ?, ?, 0)
+ INSERT INTO nodes (peer_id, public_key, biscuit_token, role, enrollment_type, claims_json, enrolled_at, expires_at, banned)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0)
ON CONFLICT (peer_id)
- DO UPDATE SET biscuit_token = excluded.biscuit_token, enrolled_at = excluded.enrolled_at, expires_at = excluded.expires_at`)
- }
-
- _, err := s.db.ExecContext(ctx, query, peerID, biscuit, time.Now().UnixMilli(), expiresAt.UnixMilli())
+ DO UPDATE SET public_key = excluded.public_key, biscuit_token = excluded.biscuit_token, role = excluded.role, enrollment_type = excluded.enrollment_type, claims_json = excluded.claims_json, enrolled_at = excluded.enrolled_at, expires_at = excluded.expires_at`)
+ }
+
+ _, err := s.db.ExecContext(ctx, query,
+ node.PeerID,
+ node.PublicKey,
+ node.Biscuit,
+ node.Role,
+ node.EnrollmentType,
+ node.ClaimsJSON,
+ node.EnrolledAt.UnixMilli(),
+ node.ExpiresAt.UnixMilli(),
+ )
return err
}
// GetNode implements Store.
func (s *SQLStore) GetNode(ctx context.Context, peerID string) (*EnrolledNode, error) {
- query := s.rebind(`SELECT peer_id, biscuit_token, enrolled_at, expires_at, banned FROM nodes WHERE peer_id = ?`)
+ query := s.rebind(`SELECT peer_id, public_key, biscuit_token, role, enrollment_type, claims_json, enrolled_at, expires_at, banned FROM nodes WHERE peer_id = ?`)
var node EnrolledNode
var enrolledAtUnix, expiresAtUnix int64
- err := s.db.QueryRowContext(ctx, query, peerID).Scan(&node.PeerID, &node.Biscuit, &enrolledAtUnix, &expiresAtUnix, &node.Banned)
+ err := s.db.QueryRowContext(ctx, query, peerID).Scan(
+ &node.PeerID,
+ &node.PublicKey,
+ &node.Biscuit,
+ &node.Role,
+ &node.EnrollmentType,
+ &node.ClaimsJSON,
+ &enrolledAtUnix,
+ &expiresAtUnix,
+ &node.Banned,
+ )
if err == sql.ErrNoRows {
return nil, ErrNotFound
}
diff --git a/internal/storage/sql_store_test.go b/internal/storage/sql_store_test.go
index 28a94274..c40fa5e7 100644
--- a/internal/storage/sql_store_test.go
+++ b/internal/storage/sql_store_test.go
@@ -163,7 +163,17 @@ func TestNodeEnrollmentOps(t *testing.T) {
// Enroll node
biscuitData := []byte("some-biscuit-token")
expiresAt := time.Now().Add(24 * time.Hour)
- err = store.EnrollNode(ctx, peerID, biscuitData, expiresAt)
+ mockPubKey := []byte("mock-public-key-32bytes-long-12345")
+ nodeRecord := &EnrolledNode{
+ PeerID: peerID,
+ PublicKey: mockPubKey,
+ Biscuit: biscuitData,
+ Role: "node",
+ EnrollmentType: "OIDC",
+ EnrolledAt: time.Now(),
+ ExpiresAt: expiresAt,
+ }
+ err = store.EnrollNode(ctx, nodeRecord)
if err != nil {
t.Fatalf("failed to enroll node: %v", err)
}
@@ -192,7 +202,8 @@ func TestNodeEnrollmentOps(t *testing.T) {
}
// Re-enroll (unbans? No, re-enroll updates details, but let's check what our schema does. We set banned to false or keep? Wait, our EnrollNode query does 'FALSE' / 0 on INSERT, but during UPDATE it doesn't modify banned)
- err = store.EnrollNode(ctx, peerID, []byte("new-biscuit"), expiresAt)
+ nodeRecord.Biscuit = []byte("new-biscuit")
+ err = store.EnrollNode(ctx, nodeRecord)
if err != nil {
t.Fatalf("failed to enroll node: %v", err)
}
diff --git a/internal/storage/storage.go b/internal/storage/storage.go
index 0383acd7..151898d7 100644
--- a/internal/storage/storage.go
+++ b/internal/storage/storage.go
@@ -44,11 +44,15 @@ type RouterLease struct {
// EnrolledNode represents a node enrolled in the mesh.
type EnrolledNode struct {
- PeerID string
- Biscuit []byte
- EnrolledAt time.Time
- ExpiresAt time.Time
- Banned bool
+ PeerID string
+ PublicKey []byte
+ Biscuit []byte
+ Role string
+ EnrollmentType string
+ ClaimsJSON string
+ EnrolledAt time.Time
+ ExpiresAt time.Time
+ Banned bool
}
// BootstrapToken represents a pre-shared token for node enrollment.
@@ -91,7 +95,7 @@ type Store interface {
SaveInitialKey(ctx context.Context, priv ed25519.PrivateKey, pub ed25519.PublicKey) error
// EnrollNode registers or updates a node enrollment.
- EnrollNode(ctx context.Context, peerID string, biscuit []byte, expiresAt time.Time) error
+ EnrollNode(ctx context.Context, node *EnrolledNode) error
// GetNode retrieves node enrollment details.
GetNode(ctx context.Context, peerID string) (*EnrolledNode, error)
diff --git a/site/content/docs/user/control-plane-configuration.md b/site/content/docs/user/control-plane-configuration.md
index 14691b57..673b77c5 100644
--- a/site/content/docs/user/control-plane-configuration.md
+++ b/site/content/docs/user/control-plane-configuration.md
@@ -119,3 +119,37 @@ Here is a script demonstrating how to boot both services in a secure development
--keys-path "./router.key" \
--oidc-token "my-router-jwt-token"
```
+
+---
+
+## 5. Token Refresh & Session Revocation
+
+To enforce centralized access control without sacrificing offline verification performance at the edge, SAM implements a decoupled token refresh and session revocation model.
+
+### Lifespan Model
+
+1. **Short-Lived Biscuit Tokens (TTL = 24 Hours)**:
+ All minted Biscuit tokens are cryptographically bound to a strict 24-hour expiration. Peers verify this expiration locally without hitting the Control Plane.
+2. **Long-Lived Sessions (The Right to Refresh)**:
+ * **OIDC Interactive Enrollment**: 90-day database session limit. After 90 days, the user must re-enroll interactively.
+ * **Bootstrap Flow (Headless Nodes/Routers)**: Infinite session limit (sessions never expire).
+
+### Proactive Refresh Lifecycle
+
+Nodes and Routers run a background task that periodically checks the remaining Biscuit expiration:
+* **Check Interval**: Every 10 minutes (`api.TokenRefreshCheckInterval`).
+* **Threshold**: When the remaining token lifespan is less than 20% of its initial TTL (~4.8 hours remaining), the daemon proactively trades the expiring Biscuit for a fresh one.
+* **Challenge Handshake**: The client signs a current timestamp using its private key and sends it to the Control Plane `/refresh` endpoint along with its expiring Biscuit in the `Authorization: Bearer <token>` header. The Control Plane verifies the signature against the registered node's public key in the database before issuing a new Biscuit.
+
+### Administrative Revocation
+
+Administrators can immediately revoke any active session to disable a node's ability to renew its token.
+* **Endpoint**: `POST /admin/revoke`
+* **Authentication**: Requires the `--admin-token` in the headers.
+* **Payload**:
+ ```json
+ {
+ "peer_id": "12D3KooW..."
+ }
+ ```
+* **Enforcement**: Revoked nodes are marked as banned in the database. When the node next attempts a proactive `/refresh` handshake, the request is denied with a `403 Forbidden` status, and the node's local daemon immediately terminates.
diff --git a/tests/integration/multimaster_test.go b/tests/integration/multimaster_test.go
index 266aadbc..4b4bf62e 100644
--- a/tests/integration/multimaster_test.go
+++ b/tests/integration/multimaster_test.go
@@ -28,6 +28,7 @@ import (
"github.com/google/sam/api"
"github.com/libp2p/go-libp2p"
+ "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-msgio"
"github.com/multiformats/go-multiaddr"
@@ -209,8 +210,12 @@ roles: {}
}
defer func() { _ = clientHost.Close() }()
- // 8. Enroll client host on CP B (obtaining a biscuit minted by CP B)
- clientBiscuit := enrollClientOnControlPlane(t, portB, clientHost.ID(), nodeJWT)
+ pubKey := clientHost.Peerstore().PubKey(clientHost.ID())
+ pubBytes, err := crypto.MarshalPublicKey(pubKey)
+ if err != nil {
+ t.Fatal(err)
+ }
+ clientBiscuit := enrollClientOnControlPlane(t, portB, clientHost.ID(), pubBytes, nodeJWT)
// 9. Assert that client host can connect and authenticate directly with Router A (registered with CP A!)
// This proves Router A accepts biscuits issued by CP B because they share the key-ring.
@@ -270,12 +275,13 @@ roles: {}
t.Log("Successfully verified multi-master control plane signature trust!")
}
-func enrollClientOnControlPlane(t *testing.T, cpPort int, clientID peer.ID, jwtToken string) []byte {
+func enrollClientOnControlPlane(t *testing.T, cpPort int, clientID peer.ID, pubBytes []byte, jwtToken string) []byte {
t.Helper()
req := &api.EnrollRequest{
- Jwt: jwtToken,
- PeerId: clientID.String(),
+ Jwt: jwtToken,
+ PeerId: clientID.String(),
+ PublicKey: pubBytes,
}
reqBytes, err := proto.Marshal(req)
if err != nil {