Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
Decouple control plane and router from monolithic sam-hub
Antonio Ojea06fccab9ff8c
diff --git a/.github/k8s/dns-sync-cronjob-template.yaml b/.github/k8s/dns-sync-cronjob-template.yamlindex eb7cf0fd..e92d2daf 100644--- a/.github/k8s/dns-sync-cronjob-template.yaml+++ b/.github/k8s/dns-sync-cronjob-template.yaml@@ -51,9 +51,9 @@ spec:- -c- |set -e- # Extract IPs and PeerIDs of pods running sam-hub- echo "Finding sam-hub pods in ${NAMESPACE}..."- PODS=$(kubectl get pods -n ${NAMESPACE} -l app=sam-hub-${ENV_NAME} -o jsonpath='{range .items[?(@.status.phase=="Running")]}{.metadata.name}{" "}{.spec.nodeName}{"\n"}{end}')+ # Extract IPs and PeerIDs of pods running sam-router+ echo "Finding sam-router pods in ${NAMESPACE}..."+ PODS=$(kubectl get pods -n ${NAMESPACE} -l app=sam-router-${ENV_NAME} -o jsonpath='{range .items[?(@.status.phase=="Running")]}{.metadata.name}{" "}{.spec.nodeName}{"\n"}{end}')declare -a IP_ARRAY=()declare -a TXT_ARRAY=()@@ -64,8 +64,8 @@ spec:IP_LIST=$(kubectl get node $node -o jsonpath='{.status.addresses[?(@.type=="ExternalIP")].address}')POD_IP=$(kubectl get pod $pod -n ${NAMESPACE} -o jsonpath='{.status.podIP}')- # Get the PeerID by extracting the base58 string from the protobuf response- PEER_ID=$(curl -s http://${POD_IP}:9090/info | grep -a -o '/p2p/[1-9A-HJ-NP-Za-km-z]*' | head -n 1 | cut -d/ -f3 || true)+ # Get the PeerID by extracting it from the router pod logs+ PEER_ID=$(kubectl logs $pod -n ${NAMESPACE} | grep -o 'PeerID: [a-zA-Z0-9]*' | head -n 1 | cut -d' ' -f2 || true)if [ -n "$IP_LIST" ]; thenecho "Found IP $IP_LIST "diff --git a/.github/k8s/sam-control-plane-template.yaml b/.github/k8s/sam-control-plane-template.yamlnew file mode 100644index 00000000..542a6df6--- /dev/null+++ b/.github/k8s/sam-control-plane-template.yaml@@ -0,0 +1,179 @@+apiVersion: v1+kind: Service+metadata:+ name: sam-control-plane-${ENV_NAME}+ namespace: ${NAMESPACE}+spec:+ ports:+ - port: 8080+ targetPort: 8080+ protocol: TCP+ name: http+ selector:+ app: sam-control-plane-${ENV_NAME}+---+apiVersion: apps/v1+kind: Deployment+metadata:+ name: sam-control-plane-${ENV_NAME}+ namespace: ${NAMESPACE}+spec:+ replicas: 1+ selector:+ matchLabels:+ app: sam-control-plane-${ENV_NAME}+ template:+ metadata:+ labels:+ app: sam-control-plane-${ENV_NAME}+ spec:+ containers:+ - name: sam-control-plane+ image: ghcr.io/google/sam-control-plane:${IMAGE_TAG}+ ports:+ - containerPort: 8080+ protocol: TCP+ name: http+ readinessProbe:+ httpGet:+ path: /info+ port: http+ scheme: HTTP+ initialDelaySeconds: 5+ periodSeconds: 10+ livenessProbe:+ httpGet:+ path: /info+ port: http+ scheme: HTTP+ initialDelaySeconds: 15+ periodSeconds: 20+ args:+ - "--bind-address=0.0.0.0:8080"+ - "--db-driver=sqlite"+ - "--db-dsn=/data/control-plane.db"+ - "--issuer=https://auth.sam-mesh.dev,https://container.googleapis.com/v1/projects/${GCP_PROJECT_ID}/locations/${CLUSTER_REGION}/clusters/${CLUSTER_NAME}"+ - "--allowed-audiences=sam-mesh-audience,sam-hub-audience"+ - "--policy-file=/etc/sam/policies/policies.yaml"+ resources:+ requests:+ cpu: 100m+ memory: 128Mi+ limits:+ cpu: 500m+ memory: 512Mi+ volumeMounts:+ - name: policies-volume+ mountPath: /etc/sam/policies+ readOnly: true+ - name: db-data+ mountPath: /data+ volumes:+ - name: policies-volume+ configMap:+ name: sam-control-plane-policies-${ENV_NAME}+ - name: db-data+ emptyDir: {}+---+apiVersion: v1+kind: ConfigMap+metadata:+ name: sam-control-plane-policies-${ENV_NAME}+ namespace: ${NAMESPACE}+data:+ policies.yaml: |+ version: "v1alpha1"+ bindings:+ - members: ["user:system:serviceaccount:sam-canary-${ENV_NAME}:sam-node-sa"]+ role: "sam-canary"+ - members: ["group:routers", "user:system:serviceaccount:${NAMESPACE}:sam-router-sa"]+ role: "router-role"+ - members: ["system:authenticated"]+ role: "public-mesh"+ roles:+ sam-canary:+ allowed_services:+ - "*"+ allowed_targets:+ - "*"+ router-role:+ allowed_services:+ - "*"+ allowed_targets:+ - "*"+ public-mesh:+ allowed_services:+ - "*"+ allowed_targets:+ - "*"+---+apiVersion: networking.gke.io/v1+kind: HealthCheckPolicy+metadata:+ name: sam-control-plane-healthcheck-${ENV_NAME}+ namespace: ${NAMESPACE}+spec:+ default:+ checkIntervalSec: 15+ timeoutSec: 5+ healthyThreshold: 1+ unhealthyThreshold: 2+ config:+ type: HTTP+ httpHealthCheck:+ requestPath: /info+ targetRef:+ group: ""+ kind: Service+ name: sam-control-plane-${ENV_NAME}+---+apiVersion: gateway.networking.k8s.io/v1+kind: Gateway+metadata:+ name: sam-control-plane-gateway-${ENV_NAME}+ namespace: ${NAMESPACE}+ annotations:+ networking.gke.io/certmap: sam-hub-cert-map-${ENV_NAME}+spec:+ gatewayClassName: gke-l7-global-external-managed+ addresses:+ - type: NamedAddress+ value: sam-hub-ip-${ENV_NAME}+ listeners:+ - name: https+ protocol: HTTPS+ port: 443+ allowedRoutes:+ namespaces:+ from: Same+---+apiVersion: gateway.networking.k8s.io/v1+kind: HTTPRoute+metadata:+ name: sam-control-plane-route-${ENV_NAME}+ namespace: ${NAMESPACE}+spec:+ parentRefs:+ - name: sam-control-plane-gateway-${ENV_NAME}+ hostnames:+ - "${ENV_NAME}.sam-mesh.dev"+ rules:+ - matches:+ - path:+ type: Exact+ value: /register+ - path:+ type: Exact+ value: /info+ - path:+ type: Exact+ value: /keys+ - path:+ type: Exact+ value: /routers/lease+ - path:+ type: Exact+ value: /policies+ backendRefs:+ - name: sam-control-plane-${ENV_NAME}+ port: 8080diff --git a/.github/k8s/sam-node-template.yaml b/.github/k8s/sam-node-template.yamlindex 40ad62ed..c03a95a2 100644--- a/.github/k8s/sam-node-template.yaml+++ b/.github/k8s/sam-node-template.yaml@@ -45,7 +45,7 @@ spec:args:- "run"- "--config=/etc/sam/sam-node.yaml"- - "--hub=http://sam-hub-${ENV_NAME}.${NAMESPACE}.svc.cluster.local:9090"+ - "--hub=http://sam-control-plane-${ENV_NAME}.${NAMESPACE}.svc.cluster.local:8080"- "--jwt-path=/var/run/secrets/tokens/sam-token"- "--api-token=secret-token"ports:diff --git a/.github/k8s/sam-router-template.yaml b/.github/k8s/sam-router-template.yamlnew file mode 100644index 00000000..9e934a45--- /dev/null+++ b/.github/k8s/sam-router-template.yaml@@ -0,0 +1,89 @@+apiVersion: v1+kind: ServiceAccount+metadata:+ name: sam-router-sa+ namespace: ${NAMESPACE}+---+apiVersion: v1+kind: Service+metadata:+ name: sam-router-${ENV_NAME}+ namespace: ${NAMESPACE}+spec:+ clusterIP: None+ ports:+ - port: 4501+ targetPort: 4501+ protocol: TCP+ name: p2p-tcp+ - port: 4501+ targetPort: 4501+ protocol: UDP+ name: p2p-udp+ selector:+ app: sam-router-${ENV_NAME}+---+apiVersion: apps/v1+kind: StatefulSet+metadata:+ name: sam-router-${ENV_NAME}+ namespace: ${NAMESPACE}+spec:+ serviceName: "sam-router-${ENV_NAME}"+ replicas: 3+ selector:+ matchLabels:+ app: sam-router-${ENV_NAME}+ template:+ metadata:+ labels:+ app: sam-router-${ENV_NAME}+ spec:+ serviceAccountName: sam-router-sa+ containers:+ - name: sam-router+ image: ghcr.io/google/sam-router:${IMAGE_TAG}+ ports:+ - containerPort: 4501+ hostPort: 4501+ protocol: TCP+ name: p2p-tcp+ - containerPort: 4501+ hostPort: 4501+ protocol: UDP+ name: p2p-udp+ args:+ - "--control-plane=http://sam-control-plane-${ENV_NAME}.${NAMESPACE}.svc.cluster.local:8080"+ - "--listen=/ip4/0.0.0.0/tcp/4501"+ - "--listen=/ip4/0.0.0.0/udp/4501/quic-v1"+ - "--jwt-path=/var/run/secrets/tokens/sam-token"+ - "--keys-path=/data/router.key"+ resources:+ requests:+ cpu: 100m+ memory: 128Mi+ limits:+ cpu: 500m+ memory: 512Mi+ volumeMounts:+ - name: sam-token+ mountPath: /var/run/secrets/tokens+ readOnly: true+ - name: router-data+ mountPath: /data+ volumes:+ - name: sam-token+ projected:+ sources:+ - serviceAccountToken:+ path: sam-token+ expirationSeconds: 3600+ audience: "sam-hub-audience"+ volumeClaimTemplates:+ - metadata:+ name: router-data+ spec:+ accessModes: [ "ReadWriteOnce" ]+ resources:+ requests:+ storage: 1Gidiff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yamlindex 3c1424cc..7cc63e59 100644--- a/.github/workflows/deploy.yaml+++ b/.github/workflows/deploy.yaml@@ -37,11 +37,11 @@ jobs:username: ${{ github.actor }}password: ${{ secrets.GITHUB_TOKEN }}- - name: Extract metadata for Hub- id: meta-hub+ - name: Extract metadata for Control Plane+ id: meta-cpuses: docker/metadata-action@v6with:- images: ${{ env.REGISTRY }}/google/sam-hub+ images: ${{ env.REGISTRY }}/google/sam-control-planetags: |type=ref,event=branchtype=ref,event=tag@@ -49,15 +49,37 @@ jobs:type=raw,value=latest,enable={{is_default_branch}}type=raw,value=stable,enable=${{ startsWith(github.ref, 'refs/tags/v') }}- - name: Build and push Hub image+ - name: Build and push Control Plane imageuses: docker/build-push-action@v7with:context: .- file: Dockerfile.sam-hub+ file: Dockerfile.sam-control-planeplatforms: ${{ github.ref_type == 'tag' && 'linux/amd64,linux/arm64' || 'linux/amd64' }}push: true- tags: ${{ steps.meta-hub.outputs.tags }}- labels: ${{ steps.meta-hub.outputs.labels }}+ tags: ${{ steps.meta-cp.outputs.tags }}+ labels: ${{ steps.meta-cp.outputs.labels }}++ - name: Extract metadata for Router+ id: meta-router+ uses: docker/metadata-action@v6+ with:+ images: ${{ env.REGISTRY }}/google/sam-router+ tags: |+ type=ref,event=branch+ type=ref,event=tag+ type=raw,value=${{ github.sha }}+ type=raw,value=latest,enable={{is_default_branch}}+ type=raw,value=stable,enable=${{ startsWith(github.ref, 'refs/tags/v') }}++ - name: Build and push Router image+ uses: docker/build-push-action@v7+ with:+ context: .+ file: Dockerfile.sam-router+ platforms: ${{ github.ref_type == 'tag' && 'linux/amd64,linux/arm64' || 'linux/amd64' }}+ push: true+ tags: ${{ steps.meta-router.outputs.tags }}+ labels: ${{ steps.meta-router.outputs.labels }}- name: Extract metadata for Nodeid: meta-node@@ -200,7 +222,7 @@ jobs:- - name: Deploy Hub+ - name: Deploy Control Plane and Routersrun: |print_rollout_diagnostics() {local namespace="$1"@@ -228,28 +250,33 @@ jobs:sudo apt-get install -y gettext-baseexport ENV_NAME="${{ vars.ENV_NAME }}"- export LOAD_BALANCER_IP="${{ vars.LOAD_BALANCER_IP }}"- export EXTERNAL_MULTIADDR="${{ vars.EXTERNAL_MULTIADDR }}"export NAMESPACE="sam-${ENV_NAME}"export IMAGE_TAG="${{ env.IMAGE_TAG }}"export GCP_PROJECT_ID="${{ vars.GCP_PROJECT_ID }}"export CLUSTER_REGION="${{ vars.CLUSTER_REGION }}"export CLUSTER_NAME="${{ vars.CLUSTER_NAME }}"- envsubst '${ENV_NAME} ${NAMESPACE} ${GCP_PROJECT_ID} ${CLUSTER_NAME} ${CLUSTER_REGION} ${IMAGE_TAG}' < .github/k8s/sam-hub-template.yaml | kubectl apply -f -+ envsubst '${ENV_NAME} ${NAMESPACE} ${GCP_PROJECT_ID} ${CLUSTER_NAME} ${CLUSTER_REGION} ${IMAGE_TAG}' < .github/k8s/sam-control-plane-template.yaml | kubectl apply -f -+ envsubst '${ENV_NAME} ${NAMESPACE} ${GCP_PROJECT_ID} ${CLUSTER_NAME} ${CLUSTER_REGION} ${IMAGE_TAG}' < .github/k8s/sam-router-template.yaml | kubectl apply -f -envsubst '${ENV_NAME} ${NAMESPACE} ${GCP_PROJECT_ID}' < .github/k8s/dns-sync-cronjob-template.yaml | kubectl apply -f -- kubectl rollout status statefulset/sam-hub-${ENV_NAME} -n ${NAMESPACE} --timeout=120s || {- echo "Hub Deployment failed!"- print_rollout_diagnostics "${NAMESPACE}" "statefulset/sam-hub-${ENV_NAME}" "app=sam-hub-${ENV_NAME}"+ kubectl rollout status deployment/sam-control-plane-${ENV_NAME} -n ${NAMESPACE} --timeout=120s || {+ echo "Control Plane Deployment failed!"+ print_rollout_diagnostics "${NAMESPACE}" "deployment/sam-control-plane-${ENV_NAME}" "app=sam-control-plane-${ENV_NAME}"+ exit 1+ }++ kubectl rollout status statefulset/sam-router-${ENV_NAME} -n ${NAMESPACE} --timeout=120s || {+ echo "Router Deployment failed!"+ print_rollout_diagnostics "${NAMESPACE}" "statefulset/sam-router-${ENV_NAME}" "app=sam-router-${ENV_NAME}"- echo "Rolling back Hub deployment..."- kubectl rollout undo statefulset/sam-hub-${ENV_NAME} -n ${NAMESPACE}- kubectl rollout status statefulset/sam-hub-${ENV_NAME} -n ${NAMESPACE}+ echo "Rolling back Router deployment..."+ kubectl rollout undo statefulset/sam-router-${ENV_NAME} -n ${NAMESPACE}+ kubectl rollout status statefulset/sam-router-${ENV_NAME} -n ${NAMESPACE}exit 1}- echo "Triggering immediate DNS sync after Hub rollout..."+ echo "Triggering immediate DNS sync after rollout..."kubectl delete job dns-sync-rollout-${ENV_NAME} -n ${NAMESPACE} --ignore-not-found || truekubectl create job --from=cronjob/dns-sync-cronjob dns-sync-rollout-${ENV_NAME} -n ${NAMESPACE}echo "Waiting for DNS sync job to complete..."diff --git a/.github/workflows/kind-mesh-e2e.yml b/.github/workflows/kind-mesh-e2e.ymlindex bead74e0..0638a10b 100644--- a/.github/workflows/kind-mesh-e2e.yml+++ b/.github/workflows/kind-mesh-e2e.yml@@ -63,7 +63,9 @@ jobs:run: |mkdir -p _artifactskubectl --context kind-sam-kind -n sam-kind get pods -o wide > _artifacts/pods.txt 2>&1 || true- for d in sam-hub node-a node-b node-c; do+ kubectl --context kind-sam-kind -n sam-kind logs deploy/sam-control-plane --all-containers --tail=-1 > _artifacts/sam-control-plane.log 2>&1 || true+ kubectl --context kind-sam-kind -n sam-kind logs statefulset/sam-router --all-containers --tail=-1 > _artifacts/sam-router.log 2>&1 || true+ for d in node-a node-b node-c; dokubectl --context kind-sam-kind -n sam-kind logs "deploy/$d" --all-containers --tail=-1 > "_artifacts/$d.log" 2>&1 || truedonecp "$RUNNER_TEMP/local-node.log" _artifacts/ 2>/dev/null || truediff --git a/Dockerfile.sam-control-plane b/Dockerfile.sam-control-planenew file mode 100644index 00000000..1eb4759f--- /dev/null+++ b/Dockerfile.sam-control-plane@@ -0,0 +1,12 @@+# Stage 1: Build+FROM golang:1.26.4 AS builder+WORKDIR /app+COPY go.mod go.sum ./+RUN go mod download+COPY . .+RUN CGO_ENABLED=0 GOOS=linux go build -buildvcs=false -o sam-control-plane ./cmd/sam-control-plane++# Stage 2: Final+FROM gcr.io/distroless/static:latest+COPY --from=builder /app/sam-control-plane /+ENTRYPOINT ["/sam-control-plane"]diff --git a/Dockerfile.sam-hub b/Dockerfile.sam-routersimilarity index 52%rename from Dockerfile.sam-hubrename to Dockerfile.sam-routerindex 77b9285a..bb7487a6 100644--- a/Dockerfile.sam-hub+++ b/Dockerfile.sam-router@@ -4,9 +4,9 @@ WORKDIR /appCOPY go.mod go.sum ./RUN go mod downloadCOPY . .-RUN CGO_ENABLED=0 GOOS=linux go build -buildvcs=false -o sam-hub ./cmd/sam-hub+RUN CGO_ENABLED=0 GOOS=linux go build -buildvcs=false -o sam-router ./cmd/sam-router# Stage 2: FinalFROM gcr.io/distroless/static:latest-COPY --from=builder /app/sam-hub /-ENTRYPOINT ["/sam-hub"]+COPY --from=builder /app/sam-router /+ENTRYPOINT ["/sam-router"]diff --git a/Makefile b/Makefileindex 0437e59c..414b65b0 100644--- a/Makefile+++ b/Makefile@@ -22,7 +22,8 @@ ANDROID_CC_X86_64=$(ANDROID_NDK_TOOLCHAIN)/x86_64-linux-android30-clangbuild:go build -v -o "$(OUT_DIR)/sam-node" ./cmd/sam-node- go build -v -o "$(OUT_DIR)/sam-hub" ./cmd/sam-hub+ go build -v -o "$(OUT_DIR)/sam-control-plane" ./cmd/sam-control-plane+ go build -v -o "$(OUT_DIR)/sam-router" ./cmd/sam-routergo build -v -o "$(OUT_DIR)/mcp-client" ./cmd/mcp-client.PHONY: mobile-ffi-host mobile-ffi-android mobile-ffi-android-x86_64 mobile-ffi-ios mobile-ffi mobile-app-apk mobile-app-apk-emulator@@ -129,7 +130,7 @@ test-e2e: build docker-buildexit 1; \fi; \}- SAM_NODE_BINARY=$(OUT_DIR)/sam-node SAM_HUB_BINARY=$(OUT_DIR)/sam-hub bats --verbose-run tests/e2e/+ SAM_NODE_BINARY=$(OUT_DIR)/sam-node SAM_CONTROL_PLANE_BINARY=$(OUT_DIR)/sam-control-plane SAM_ROUTER_BINARY=$(OUT_DIR)/sam-router bats --verbose-run tests/e2e/test-e2e-container: build docker-build@command -v docker >/dev/null 2>&1 || { echo "docker not found"; exit 1; }@@ -153,8 +154,11 @@ verify:update:go mod tidy-docker-build-hub:- docker build --load -t sam-hub:local -f Dockerfile.sam-hub .+docker-build-control-plane:+ docker build --load -t sam-control-plane:local -f Dockerfile.sam-control-plane .++docker-build-router:+ docker build --load -t sam-router:local -f Dockerfile.sam-router .docker-build-node:docker build --load --no-cache -t sam-node:local -f Dockerfile.sam-node .@@ -165,6 +169,6 @@ docker-build-mock-oidc:docker-build-e2e-runtime:docker build --load -t sam-e2e-runtime:local -f tests/e2e/docker/Dockerfile.sam-runtime .-docker-build: docker-build-hub docker-build-node docker-build-mock-oidc docker-build-e2e-runtime+docker-build: docker-build-control-plane docker-build-router docker-build-node docker-build-mock-oidc docker-build-e2e-runtime-.PHONY: docker-build-hub docker-build-node docker-build-mock-oidc docker-build-e2e-runtime docker-build+.PHONY: docker-build-control-plane docker-build-router docker-build-node docker-build-mock-oidc docker-build-e2e-runtime docker-builddiff --git a/api/datalog.go b/api/datalog.goindex d13a92ee..5023fe66 100644--- a/api/datalog.go+++ b/api/datalog.go@@ -80,6 +80,10 @@ const (// Example Datalog: allow if role("mesh-member")FactRole = "role"+ // Standard role values+ RoleRouter = "router"+ RoleBootstrap = "bootstrap"+// FactUser defines the subject (username/userID) claim extracted from the OIDC token.// Contains: biscuit.String(username)// Example Datalog: allow if user("alice")diff --git a/api/sam.pb.go b/api/sam.pb.goindex 28ef1be7..ed3224ad 100644--- a/api/sam.pb.go+++ b/api/sam.pb.go@@ -185,7 +185,8 @@ func (x *AuthFrame) GetTargetService() string {type AuthResponse 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"` // populated only if success is false+ Error string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"` // populated only if success is false+ Biscuit []byte `protobuf:"bytes,3,opt,name=biscuit,proto3" json:"biscuit,omitempty"` // The server's own Biscuit token (for mutual auth)unknownFields protoimpl.UnknownFieldssizeCache protoimpl.SizeCache}@@ -234,6 +235,13 @@ func (x *AuthResponse) GetError() string {return ""}+func (x *AuthResponse) GetBiscuit() []byte {+ if x != nil {+ return x.Biscuit+ }+ return nil+}+type MeshEvent struct {state protoimpl.MessageState `protogen:"open.v1"`Type MeshEvent_Type `protobuf:"varint,1,opt,name=type,proto3,enum=sam.v1.MeshEvent_Type" json:"type,omitempty"`@@ -776,6 +784,346 @@ func (x *HubInfoResponse) GetHubAddresses() []string {return nil}+type RouterLeaseRequest struct {+ state protoimpl.MessageState `protogen:"open.v1"`+ PeerId string `protobuf:"bytes,1,opt,name=peer_id,json=peerId,proto3" json:"peer_id,omitempty"`+ Addresses []string `protobuf:"bytes,2,rep,name=addresses,proto3" json:"addresses,omitempty"`+ Biscuit []byte `protobuf:"bytes,3,opt,name=biscuit,proto3" json:"biscuit,omitempty"`+ unknownFields protoimpl.UnknownFields+ sizeCache protoimpl.SizeCache+}++func (x *RouterLeaseRequest) Reset() {+ *x = RouterLeaseRequest{}+ mi := &file_api_sam_proto_msgTypes[10]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+}++func (x *RouterLeaseRequest) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*RouterLeaseRequest) ProtoMessage() {}++func (x *RouterLeaseRequest) ProtoReflect() protoreflect.Message {+ mi := &file_api_sam_proto_msgTypes[10]+ 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 RouterLeaseRequest.ProtoReflect.Descriptor instead.+func (*RouterLeaseRequest) Descriptor() ([]byte, []int) {+ return file_api_sam_proto_rawDescGZIP(), []int{10}+}++func (x *RouterLeaseRequest) GetPeerId() string {+ if x != nil {+ return x.PeerId+ }+ return ""+}++func (x *RouterLeaseRequest) GetAddresses() []string {+ if x != nil {+ return x.Addresses+ }+ return nil+}++func (x *RouterLeaseRequest) GetBiscuit() []byte {+ if x != nil {+ return x.Biscuit+ }+ return nil+}++type RouterLeaseResponse 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"`+ ExpiresAt int64 `protobuf:"varint,3,opt,name=expires_at,json=expiresAt,proto3" json:"expires_at,omitempty"`+ unknownFields protoimpl.UnknownFields+ sizeCache protoimpl.SizeCache+}++func (x *RouterLeaseResponse) Reset() {+ *x = RouterLeaseResponse{}+ mi := &file_api_sam_proto_msgTypes[11]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+}++func (x *RouterLeaseResponse) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*RouterLeaseResponse) ProtoMessage() {}++func (x *RouterLeaseResponse) ProtoReflect() protoreflect.Message {+ mi := &file_api_sam_proto_msgTypes[11]+ 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 RouterLeaseResponse.ProtoReflect.Descriptor instead.+func (*RouterLeaseResponse) Descriptor() ([]byte, []int) {+ return file_api_sam_proto_rawDescGZIP(), []int{11}+}++func (x *RouterLeaseResponse) GetSuccess() bool {+ if x != nil {+ return x.Success+ }+ return false+}++func (x *RouterLeaseResponse) GetError() string {+ if x != nil {+ return x.Error+ }+ return ""+}++func (x *RouterLeaseResponse) GetExpiresAt() int64 {+ if x != nil {+ return x.ExpiresAt+ }+ return 0+}++type PolicyConfigGetRequest struct {+ state protoimpl.MessageState `protogen:"open.v1"`+ unknownFields protoimpl.UnknownFields+ sizeCache protoimpl.SizeCache+}++func (x *PolicyConfigGetRequest) Reset() {+ *x = PolicyConfigGetRequest{}+ mi := &file_api_sam_proto_msgTypes[12]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+}++func (x *PolicyConfigGetRequest) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*PolicyConfigGetRequest) ProtoMessage() {}++func (x *PolicyConfigGetRequest) ProtoReflect() protoreflect.Message {+ mi := &file_api_sam_proto_msgTypes[12]+ 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 PolicyConfigGetRequest.ProtoReflect.Descriptor instead.+func (*PolicyConfigGetRequest) Descriptor() ([]byte, []int) {+ return file_api_sam_proto_rawDescGZIP(), []int{12}+}++type PolicyConfigGetResponse struct {+ state protoimpl.MessageState `protogen:"open.v1"`+ YamlContent string `protobuf:"bytes,1,opt,name=yaml_content,json=yamlContent,proto3" json:"yaml_content,omitempty"`+ unknownFields protoimpl.UnknownFields+ sizeCache protoimpl.SizeCache+}++func (x *PolicyConfigGetResponse) Reset() {+ *x = PolicyConfigGetResponse{}+ mi := &file_api_sam_proto_msgTypes[13]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+}++func (x *PolicyConfigGetResponse) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*PolicyConfigGetResponse) ProtoMessage() {}++func (x *PolicyConfigGetResponse) ProtoReflect() protoreflect.Message {+ mi := &file_api_sam_proto_msgTypes[13]+ 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 PolicyConfigGetResponse.ProtoReflect.Descriptor instead.+func (*PolicyConfigGetResponse) Descriptor() ([]byte, []int) {+ return file_api_sam_proto_rawDescGZIP(), []int{13}+}++func (x *PolicyConfigGetResponse) GetYamlContent() string {+ if x != nil {+ return x.YamlContent+ }+ return ""+}++type PolicyConfigUpdateRequest struct {+ state protoimpl.MessageState `protogen:"open.v1"`+ YamlContent string `protobuf:"bytes,1,opt,name=yaml_content,json=yamlContent,proto3" json:"yaml_content,omitempty"`+ unknownFields protoimpl.UnknownFields+ sizeCache protoimpl.SizeCache+}++func (x *PolicyConfigUpdateRequest) Reset() {+ *x = PolicyConfigUpdateRequest{}+ mi := &file_api_sam_proto_msgTypes[14]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+}++func (x *PolicyConfigUpdateRequest) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*PolicyConfigUpdateRequest) ProtoMessage() {}++func (x *PolicyConfigUpdateRequest) ProtoReflect() protoreflect.Message {+ mi := &file_api_sam_proto_msgTypes[14]+ 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 PolicyConfigUpdateRequest.ProtoReflect.Descriptor instead.+func (*PolicyConfigUpdateRequest) Descriptor() ([]byte, []int) {+ return file_api_sam_proto_rawDescGZIP(), []int{14}+}++func (x *PolicyConfigUpdateRequest) GetYamlContent() string {+ if x != nil {+ return x.YamlContent+ }+ return ""+}++type PolicyConfigUpdateResponse 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 *PolicyConfigUpdateResponse) Reset() {+ *x = PolicyConfigUpdateResponse{}+ mi := &file_api_sam_proto_msgTypes[15]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+}++func (x *PolicyConfigUpdateResponse) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*PolicyConfigUpdateResponse) ProtoMessage() {}++func (x *PolicyConfigUpdateResponse) ProtoReflect() protoreflect.Message {+ mi := &file_api_sam_proto_msgTypes[15]+ 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 PolicyConfigUpdateResponse.ProtoReflect.Descriptor instead.+func (*PolicyConfigUpdateResponse) Descriptor() ([]byte, []int) {+ return file_api_sam_proto_rawDescGZIP(), []int{15}+}++func (x *PolicyConfigUpdateResponse) GetSuccess() bool {+ if x != nil {+ return x.Success+ }+ return false+}++func (x *PolicyConfigUpdateResponse) GetError() string {+ if x != nil {+ return x.Error+ }+ return ""+}++type KeysResponse struct {+ state protoimpl.MessageState `protogen:"open.v1"`+ PublicKeys [][]byte `protobuf:"bytes,1,rep,name=public_keys,json=publicKeys,proto3" json:"public_keys,omitempty"`+ unknownFields protoimpl.UnknownFields+ sizeCache protoimpl.SizeCache+}++func (x *KeysResponse) Reset() {+ *x = KeysResponse{}+ mi := &file_api_sam_proto_msgTypes[16]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+}++func (x *KeysResponse) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*KeysResponse) ProtoMessage() {}++func (x *KeysResponse) ProtoReflect() protoreflect.Message {+ mi := &file_api_sam_proto_msgTypes[16]+ 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 KeysResponse.ProtoReflect.Descriptor instead.+func (*KeysResponse) Descriptor() ([]byte, []int) {+ return file_api_sam_proto_rawDescGZIP(), []int{16}+}++func (x *KeysResponse) GetPublicKeys() [][]byte {+ if x != nil {+ return x.PublicKeys+ }+ return nil+}+var File_api_sam_proto protoreflect.FileDescriptorconst file_api_sam_proto_rawDesc = "" +@@ -783,10 +1131,11 @@ const file_api_sam_proto_rawDesc = "" +"\rapi/sam.proto\x12\x06sam.v1\"L\n" +"\tAuthFrame\x12\x18\n" +"\abiscuit\x18\x01 \x01(\fR\abiscuit\x12%\n" +- "\x0etarget_service\x18\x02 \x01(\tR\rtargetService\">\n" ++ "\x0etarget_service\x18\x02 \x01(\tR\rtargetService\"X\n" +"\fAuthResponse\x12\x18\n" +"\asuccess\x18\x01 \x01(\bR\asuccess\x12\x14\n" +- "\x05error\x18\x02 \x01(\tR\x05error\"\xd8\x01\n" ++ "\x05error\x18\x02 \x01(\tR\x05error\x12\x18\n" ++ "\abiscuit\x18\x03 \x01(\fR\abiscuit\"\xd8\x01\n" +"\tMeshEvent\x12*\n" +"\x04type\x18\x01 \x01(\x0e2\x16.sam.v1.MeshEvent.TypeR\x04type\x12\x17\n" +"\apeer_id\x18\x02 \x01(\tR\x06peerId\x12\x1c\n" +@@ -834,7 +1183,27 @@ const file_api_sam_proto_rawDesc = "" +"oidcIssuer\x12\x1b\n" +"\tclient_id\x18\x02 \x01(\tR\bclientId\x12\x1a\n" +"\baudience\x18\x03 \x01(\tR\baudience\x12#\n" +- "\rhub_addresses\x18\x04 \x03(\tR\fhubAddresses*]\n" ++ "\rhub_addresses\x18\x04 \x03(\tR\fhubAddresses\"e\n" ++ "\x12RouterLeaseRequest\x12\x17\n" ++ "\apeer_id\x18\x01 \x01(\tR\x06peerId\x12\x1c\n" ++ "\taddresses\x18\x02 \x03(\tR\taddresses\x12\x18\n" ++ "\abiscuit\x18\x03 \x01(\fR\abiscuit\"d\n" ++ "\x13RouterLeaseResponse\x12\x18\n" ++ "\asuccess\x18\x01 \x01(\bR\asuccess\x12\x14\n" ++ "\x05error\x18\x02 \x01(\tR\x05error\x12\x1d\n" ++ "\n" ++ "expires_at\x18\x03 \x01(\x03R\texpiresAt\"\x18\n" ++ "\x16PolicyConfigGetRequest\"<\n" ++ "\x17PolicyConfigGetResponse\x12!\n" ++ "\fyaml_content\x18\x01 \x01(\tR\vyamlContent\">\n" ++ "\x19PolicyConfigUpdateRequest\x12!\n" ++ "\fyaml_content\x18\x01 \x01(\tR\vyamlContent\"L\n" ++ "\x1aPolicyConfigUpdateResponse\x12\x18\n" ++ "\asuccess\x18\x01 \x01(\bR\asuccess\x12\x14\n" ++ "\x05error\x18\x02 \x01(\tR\x05error\"/\n" ++ "\fKeysResponse\x12\x1f\n" ++ "\vpublic_keys\x18\x01 \x03(\fR\n" ++ "publicKeys*]\n" +"\vServiceType\x12\x1c\n" +"\x18SERVICE_TYPE_UNSPECIFIED\x10\x00\x12\x14\n" +"\x10SERVICE_TYPE_MCP\x10\x01\x12\x1a\n" +@@ -853,26 +1222,33 @@ func file_api_sam_proto_rawDescGZIP() []byte {}var file_api_sam_proto_enumTypes = make([]protoimpl.EnumInfo, 2)-var file_api_sam_proto_msgTypes = make([]protoimpl.MessageInfo, 11)+var file_api_sam_proto_msgTypes = make([]protoimpl.MessageInfo, 18)var file_api_sam_proto_goTypes = []any{- (ServiceType)(0), // 0: sam.v1.ServiceType- (MeshEvent_Type)(0), // 1: sam.v1.MeshEvent.Type- (*AuthFrame)(nil), // 2: sam.v1.AuthFrame- (*AuthResponse)(nil), // 3: sam.v1.AuthResponse- (*MeshEvent)(nil), // 4: sam.v1.MeshEvent- (*EnrollRequest)(nil), // 5: sam.v1.EnrollRequest- (*EnrollResponse)(nil), // 6: sam.v1.EnrollResponse- (*ServiceInfo)(nil), // 7: sam.v1.ServiceInfo- (*CommandBackend)(nil), // 8: sam.v1.CommandBackend- (*RegisterServiceRequest)(nil), // 9: sam.v1.RegisterServiceRequest- (*DiscoveredProvider)(nil), // 10: sam.v1.DiscoveredProvider- (*HubInfoResponse)(nil), // 11: sam.v1.HubInfoResponse- nil, // 12: sam.v1.CommandBackend.EnvEntry+ (ServiceType)(0), // 0: sam.v1.ServiceType+ (MeshEvent_Type)(0), // 1: sam.v1.MeshEvent.Type+ (*AuthFrame)(nil), // 2: sam.v1.AuthFrame+ (*AuthResponse)(nil), // 3: sam.v1.AuthResponse+ (*MeshEvent)(nil), // 4: sam.v1.MeshEvent+ (*EnrollRequest)(nil), // 5: sam.v1.EnrollRequest+ (*EnrollResponse)(nil), // 6: sam.v1.EnrollResponse+ (*ServiceInfo)(nil), // 7: sam.v1.ServiceInfo+ (*CommandBackend)(nil), // 8: sam.v1.CommandBackend+ (*RegisterServiceRequest)(nil), // 9: sam.v1.RegisterServiceRequest+ (*DiscoveredProvider)(nil), // 10: sam.v1.DiscoveredProvider+ (*HubInfoResponse)(nil), // 11: sam.v1.HubInfoResponse+ (*RouterLeaseRequest)(nil), // 12: sam.v1.RouterLeaseRequest+ (*RouterLeaseResponse)(nil), // 13: sam.v1.RouterLeaseResponse+ (*PolicyConfigGetRequest)(nil), // 14: sam.v1.PolicyConfigGetRequest+ (*PolicyConfigGetResponse)(nil), // 15: sam.v1.PolicyConfigGetResponse+ (*PolicyConfigUpdateRequest)(nil), // 16: sam.v1.PolicyConfigUpdateRequest+ (*PolicyConfigUpdateResponse)(nil), // 17: sam.v1.PolicyConfigUpdateResponse+ (*KeysResponse)(nil), // 18: sam.v1.KeysResponse+ nil, // 19: sam.v1.CommandBackend.EnvEntry}var file_api_sam_proto_depIdxs = []int32{1, // 0: sam.v1.MeshEvent.type:type_name -> sam.v1.MeshEvent.Type0, // 1: sam.v1.ServiceInfo.type:type_name -> sam.v1.ServiceType- 12, // 2: sam.v1.CommandBackend.env:type_name -> sam.v1.CommandBackend.EnvEntry+ 19, // 2: sam.v1.CommandBackend.env:type_name -> sam.v1.CommandBackend.EnvEntry7, // 3: sam.v1.RegisterServiceRequest.service:type_name -> sam.v1.ServiceInfo8, // 4: sam.v1.RegisterServiceRequest.command:type_name -> sam.v1.CommandBackend5, // [5:5] is the sub-list for method output_type@@ -897,7 +1273,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: 2,- NumMessages: 11,+ NumMessages: 18,NumExtensions: 0,NumServices: 0,},diff --git a/api/sam.proto b/api/sam.protoindex 8aa775a1..f39c33b8 100644--- a/api/sam.proto+++ b/api/sam.proto@@ -26,6 +26,7 @@ message AuthFrame {message AuthResponse {bool success = 1;string error = 2; // populated only if success is false+ bytes biscuit = 3; // The server's own Biscuit token (for mutual auth)}@@ -94,3 +95,36 @@ message HubInfoResponse {repeated string hub_addresses = 4;}+message RouterLeaseRequest {+ string peer_id = 1;+ repeated string addresses = 2;+ bytes biscuit = 3;+}++message RouterLeaseResponse {+ bool success = 1;+ string error = 2;+ int64 expires_at = 3;+}++message PolicyConfigGetRequest {}++message PolicyConfigGetResponse {+ string yaml_content = 1;+}++message PolicyConfigUpdateRequest {+ string yaml_content = 1;+}++message PolicyConfigUpdateResponse {+ bool success = 1;+ string error = 2;+}++message KeysResponse {+ repeated bytes public_keys = 1;+}+++diff --git a/cmd/sam-control-plane/main.go b/cmd/sam-control-plane/main.gonew file mode 100644index 00000000..9f80bbb5--- /dev/null+++ b/cmd/sam-control-plane/main.go@@ -0,0 +1,187 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package main++import (+ "context"+ "os"+ "strings"+ "time"++ "github.com/google/sam/api"+ "github.com/google/sam/internal/controlplane"+ "github.com/google/sam/internal/storage"+ golog "github.com/ipfs/go-log/v2"+ "github.com/spf13/cobra"+)++var (+ bindAddress string+ dbDriver string+ dbDSN string+ oidcIssuer string+ allowedAudiencesFlag string+ policyFile string+ keyRotationInterval time.Duration+ keyGracePeriod time.Duration+ leaseDuration time.Duration+ adminToken string+ insecureSkipTLSVerify bool+ logLevel string+)++var logger = golog.Logger("sam-control-plane-cli")++func main() {+ rootCmd := &cobra.Command{+ Use: "sam-control-plane",+ Short: "Sovereign Agent Mesh - Control Plane",+ Run: func(cmd *cobra.Command, args []string) {+ // Initialize logging+ if os.Getenv("LOG_FORMAT") == "json" {+ _ = os.Setenv("GOLOG_LOG_FMT", "json")+ }+ golog.SetAllLoggers(golog.LevelInfo)+ if logLevel != "" {+ lvl, err := golog.LevelFromString(logLevel)+ if err == nil {+ golog.SetAllLoggers(lvl)+ }+ }++ if oidcIssuer == "" {+ logger.Fatalf("OIDC issuer is required (use --issuer flag)")+ }++ var auds []string+ for _, aud := range strings.Split(allowedAudiencesFlag, ",") {+ aud = strings.TrimSpace(aud)+ if aud != "" {+ auds = append(auds, aud)+ }+ }+ if len(auds) == 0 {+ auds = []string{api.DefaultAudience}+ }++ // Open DB Store+ store, err := storage.NewSQLStore(dbDriver, dbDSN)+ if err != nil {+ logger.Fatalf("Failed to initialize database store: %v", err)+ }+ defer func() {+ if err := store.Close(); err != nil {+ logger.Errorf("Failed to close database: %v", err)+ }+ }()++ opts := controlplane.Options{+ ListenAddr: bindAddress,+ DriverName: dbDriver,+ DataSourceName: dbDSN,+ OIDCIssuer: oidcIssuer,+ AllowedAudiences: auds,+ LeaseDuration: leaseDuration,+ KeyRotationInterval: keyRotationInterval,+ KeyGracePeriod: keyGracePeriod,+ InsecureSkipTLSVerify: insecureSkipTLSVerify,+ PolicyPath: policyFile,+ BiscuitTimeout: 10 * time.Second,+ }++ srv, err := controlplane.NewServer(opts, store)+ if err != nil {+ logger.Fatalf("Failed to create control plane server: %v", err)+ }+ defer func() {+ if err := srv.Close(); err != nil {+ logger.Errorf("Failed to stop control plane: %v", err)+ }+ }()++ if err := srv.Start(); err != nil {+ logger.Fatalf("Failed to start control plane: %v", err)+ }++ logger.Infof("SAM Control Plane Online on %s", bindAddress)+ <-cmd.Context().Done()+ },+ }++ rootCmd.Flags().StringVar(&bindAddress, "bind-address", "0.0.0.0:8080", "Address to listen on for web API services")+ rootCmd.PersistentFlags().StringVar(&dbDriver, "db-driver", "sqlite", "Database driver (sqlite or postgres)")+ rootCmd.PersistentFlags().StringVar(&dbDSN, "db-dsn", "control-plane.db", "Database DSN/Connection URL")+ rootCmd.Flags().StringVar(&oidcIssuer, "issuer", "", "OIDC Issuer URL (comma-separated)")+ rootCmd.Flags().StringVar(&allowedAudiencesFlag, "allowed-audiences", api.DefaultAudience, "Comma-separated list of allowed OIDC audiences")+ rootCmd.Flags().StringVar(&policyFile, "policy-file", "policies.yaml", "Path to policies.yaml (bootstrapping only)")+ rootCmd.Flags().DurationVar(&keyRotationInterval, "key-rotation-interval", 24*time.Hour, "Key rotation interval (e.g. 24h). 0 disables rotation.")+ rootCmd.Flags().DurationVar(&keyGracePeriod, "key-grace-period", 1*time.Hour, "Key grace period for rotated keys.")+ rootCmd.Flags().DurationVar(&leaseDuration, "lease-duration", 60*time.Second, "Router lease registration TTL.")+ rootCmd.Flags().StringVar(&adminToken, "admin-token", "", "Token for authenticating policy REST API requests")+ rootCmd.Flags().BoolVar(&insecureSkipTLSVerify, "insecure-skip-tls-verify", false, "Skip TLS verification for OIDC providers")+ rootCmd.Flags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error)")++ adminCmd := &cobra.Command{+ Use: "admin",+ Short: "Administrative operations",+ }++ var peerIDFlag string+ banCmd := &cobra.Command{+ Use: "ban",+ Short: "Ban a node peer ID",+ Run: func(cmd *cobra.Command, args []string) {+ store, err := storage.NewSQLStore(dbDriver, dbDSN)+ if err != nil {+ logger.Fatalf("Failed to initialize database store: %v", err)+ }+ defer store.Close()+ if err := store.SetNodeBanned(cmd.Context(), peerIDFlag, true); err != nil {+ logger.Fatalf("Failed to ban node: %v", err)+ }+ logger.Infof("Successfully banned node %s", peerIDFlag)+ },+ }+ banCmd.Flags().StringVar(&peerIDFlag, "peer", "", "Node Peer ID to ban")+ _ = banCmd.MarkFlagRequired("peer")++ unbanCmd := &cobra.Command{+ Use: "unban",+ Short: "Unban a node peer ID",+ Run: func(cmd *cobra.Command, args []string) {+ store, err := storage.NewSQLStore(dbDriver, dbDSN)+ if err != nil {+ logger.Fatalf("Failed to initialize database store: %v", err)+ }+ defer store.Close()+ if err := store.SetNodeBanned(cmd.Context(), peerIDFlag, false); err != nil {+ logger.Fatalf("Failed to unban node: %v", err)+ }+ logger.Infof("Successfully unbanned node %s", peerIDFlag)+ },+ }+ unbanCmd.Flags().StringVar(&peerIDFlag, "peer", "", "Node Peer ID to unban")+ _ = unbanCmd.MarkFlagRequired("peer")++ adminCmd.AddCommand(banCmd, unbanCmd)+ rootCmd.AddCommand(adminCmd)++ ctx, cancel := context.WithCancel(context.Background())+ defer cancel()++ if err := rootCmd.ExecuteContext(ctx); err != nil {+ os.Exit(1)+ }+}diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.godeleted file mode 100644index a4713b1f..00000000--- a/cmd/sam-hub/main.go+++ /dev/null@@ -1,324 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package main--import (- "context"- "crypto/tls"- "crypto/x509"- "fmt"- "net"- "net/http"- "os"- "strings"- "time"-- "github.com/google/sam/api"- "github.com/google/sam/internal/hub"- golog "github.com/ipfs/go-log/v2"- madns "github.com/multiformats/go-multiaddr-dns"- "github.com/prometheus/client_golang/prometheus"- "github.com/prometheus/client_golang/prometheus/promhttp"- "github.com/spf13/cobra"-)--func init() {- if dnsServer := os.Getenv("SAM_TEST_DNS_SERVER"); dnsServer != "" {- customResolver := &net.Resolver{- PreferGo: true,- Dial: func(ctx context.Context, network, address string) (net.Conn, error) {- d := net.Dialer{Timeout: 5 * time.Second}- return d.DialContext(ctx, "udp", dnsServer)- },- }- net.DefaultResolver = customResolver- madns.DefaultResolver, _ = madns.NewResolver(madns.WithDefaultResolver(customResolver))- }-}--var (- oidcIssuer string- clientID string- biscuitHex string- listenAddrs []string- meshName string- insecureSkipTLSVerify bool- logLevel string- policyFile string- keyRotationInterval time.Duration- keyGracePeriod time.Duration- keysDBPath string- bindAddress string- adminToken string- tlsCertFile string- tlsKeyFile string- tlsCAFile string- externalMultiaddrs []string- allowedAudiencesFlag string- allowLoopbackFlag bool-)--var logger = golog.Logger("sam-hub-cli")--func main() {- rootCmd := &cobra.Command{- Use: "sam-hub",- Short: "Sovereign Agent Mesh - Multi-Transport Hub",- Run: func(cmd *cobra.Command, args []string) {- ctx := cmd.Context()- // Initialize logging- if os.Getenv("LOG_FORMAT") == "json" {- _ = os.Setenv("GOLOG_LOG_FMT", "json")- }- golog.SetAllLoggers(golog.LevelInfo)- if logLevel != "" {- lvl, err := golog.LevelFromString(logLevel)- if err == nil {- golog.SetAllLoggers(lvl)- }- }- // Suppress noisy DHT logs- _ = golog.SetLogLevel("dht", "fatal")- _ = golog.SetLogLevel("dht/RtRefreshManager", "fatal")-- policyConfig, err := hub.LoadPolicyConfig(policyFile)- if err != nil {- logger.Fatal(err)- }-- var serverTlsConfig *tls.Config- if tlsCertFile != "" && tlsKeyFile != "" {- cert, err := tls.LoadX509KeyPair(tlsCertFile, tlsKeyFile)- if err != nil {- logger.Fatalf("Failed to load TLS cert: %v", err)- }- serverTlsConfig = &tls.Config{- Certificates: []tls.Certificate{cert},- }- }-- var auds []string- for _, aud := range strings.Split(allowedAudiencesFlag, ",") {- aud = strings.TrimSpace(aud)- if aud != "" {- auds = append(auds, aud)- }- }- if len(auds) == 0 {- auds = []string{api.DefaultAudience}- }-- hOpts := hub.Options{- OIDCIssuer: oidcIssuer,- ClientID: clientID,- BiscuitHex: biscuitHex,- ListenAddrs: listenAddrs,- MeshName: meshName,- InsecureSkipTLSVerify: insecureSkipTLSVerify,- PolicyFile: policyFile,- KeyRotationInterval: keyRotationInterval,- KeyGracePeriod: keyGracePeriod,- KeysDBPath: keysDBPath,- BindAddress: bindAddress,- AdminToken: adminToken,- TLSCertFile: tlsCertFile,- TLSKeyFile: tlsKeyFile,- TLSCAFile: tlsCAFile,- ExternalMultiaddrs: externalMultiaddrs,- AllowedAudiences: auds,- AllowLoopback: allowLoopbackFlag,- Policy: policyConfig,- }-- h, err := hub.NewHub(hOpts)- if err != nil {- logger.Fatal(err)- }- defer func() {- if err := h.Close(); err != nil {- logger.Errorf("Failed to close hub: %v", err)- }- }()-- if err := h.Start(ctx); err != nil {- logger.Fatalf("Failed to start hub: %v", err)- }-- mux := http.NewServeMux()- mux.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))- mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {- if h.Ready() {- w.WriteHeader(http.StatusOK)- _, _ = w.Write([]byte("ok"))- } else {- w.WriteHeader(http.StatusServiceUnavailable)- _, _ = w.Write([]byte("not ready"))- }- })-- mux.HandleFunc("/register", h.HandleRegisterHTTP)- mux.HandleFunc("/info", h.HandleInfoHTTP)- mux.HandleFunc("/admin/ban", func(w http.ResponseWriter, r *http.Request) {- if adminToken != "" {- authHeader := r.Header.Get("Authorization")- if authHeader != "Bearer "+adminToken {- http.Error(w, "Unauthorized", http.StatusUnauthorized)- return- }- }- hub.HandleBan(h)(w, r)- })-- server := &http.Server{- Addr: bindAddress,- Handler: mux,- ReadHeaderTimeout: 5 * time.Second,- ReadTimeout: 10 * time.Second,- WriteTimeout: 10 * time.Second,- IdleTimeout: 120 * time.Second,- }-- go func() {- if serverTlsConfig != nil {- server.TLSConfig = serverTlsConfig- if err := server.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {- logger.Fatalf("HTTP server failed: %v", err)- }- } else {- if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {- logger.Fatalf("HTTP server failed: %v", err)- }- }- }()-- logger.Infof("SAM Hub Online (HTTP on %s, P2P on %v)", bindAddress, listenAddrs)- h.SetReady(true)- logger.Infof("MeshID: %s", h.MeshID)- logger.Infof("PeerID: %s", h.Host.ID())- <-ctx.Done()-- shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)- defer cancel()- _ = server.Shutdown(shutdownCtx)- },- }-- defIssuer := os.Getenv("SAM_OIDC_ISSUER")- if defIssuer == "" {- defIssuer = hub.DefaultOIDCIssuer- }- rootCmd.Flags().StringVar(&oidcIssuer, "issuer", defIssuer, "OIDC Issuer URL")- rootCmd.Flags().StringVar(&clientID, "client-id", os.Getenv("SAM_OIDC_ID"), "OIDC Client ID")- rootCmd.Flags().StringVar(&biscuitHex, "key", os.Getenv("SAM_HUB_KEY"), "Hub Private Key (32-byte Hex)")- rootCmd.Flags().StringSliceVar(&listenAddrs, "listen", []string{}, "libp2p Listen Addrs")- rootCmd.Flags().StringSliceVar(&externalMultiaddrs, "external-multiaddr", []string{}, "External multiaddrs to announce")- rootCmd.Flags().StringVar(&meshName, "mesh", hub.DefaultMeshName, "Mesh federation name")- rootCmd.Flags().StringVar(&allowedAudiencesFlag, "allowed-audiences", api.DefaultAudience, "Comma-separated list of allowed OIDC audiences")- rootCmd.Flags().BoolVar(&insecureSkipTLSVerify, "insecure-skip-tls-verify", false, "Skip TLS verification for OIDC issuers")- rootCmd.Flags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error)")- rootCmd.Flags().StringVar(&policyFile, "policy-file", hub.DefaultPolicyFile, "Path to policies.yaml")- rootCmd.Flags().DurationVar(&keyRotationInterval, "key-rotation-interval", 0, "Key rotation interval (e.g. 12h). 0 disables rotation.")- rootCmd.Flags().DurationVar(&keyGracePeriod, "key-grace-period", 24*time.Hour, "Key grace period for old keys (e.g. 24h).")- rootCmd.Flags().StringVar(&keysDBPath, "keys-db", hub.DefaultKeysDBPath, "Path to BoltDB file for keys")- rootCmd.PersistentFlags().StringVar(&bindAddress, "bind-address", hub.DefaultBindAddress, "Address to listen on for HTTP/HTTPS service")- rootCmd.PersistentFlags().StringVar(&adminToken, "admin-token", "", "Secret token for authorizing admin requests")- rootCmd.PersistentFlags().StringVar(&tlsCertFile, "tls-cert-file", "", "Path to TLS certificate for the server")- rootCmd.PersistentFlags().StringVar(&tlsKeyFile, "tls-key-file", "", "Path to TLS private key for the server")- rootCmd.PersistentFlags().StringVar(&tlsCAFile, "tls-ca-file", "", "Path to CA certificate to verify client certificates (enables mTLS)")- rootCmd.Flags().BoolVar(&allowLoopbackFlag, "allow-loopback", false, "Allow publishing and connecting to loopback/link-local addresses")-- var peerToBan string- var banReason string- var connectAddr string-- adminCmd := &cobra.Command{- Use: "admin",- Short: "Administrative commands for SAM Hub",- }-- banCmd := &cobra.Command{- Use: "ban",- Short: "Ban a peer from the mesh",- Run: func(cmd *cobra.Command, args []string) {- if peerToBan == "" {- logger.Fatal("Missing --peer flag")- }-- targetAddr := bindAddress- if strings.HasPrefix(targetAddr, ":") {- targetAddr = "localhost" + targetAddr- }-- scheme := "http"- var tlsConfig *tls.Config- if tlsCertFile != "" || tlsCAFile != "" {- scheme = "https"- tlsConfig = &tls.Config{- InsecureSkipVerify: true, // For tests, usually self-signed- }- if tlsCAFile != "" {- caCert, err := os.ReadFile(tlsCAFile)- if err != nil {- logger.Fatalf("Failed to read CA cert: %v", err)- }- caCertPool := x509.NewCertPool()- caCertPool.AppendCertsFromPEM(caCert)- tlsConfig.RootCAs = caCertPool- tlsConfig.InsecureSkipVerify = false // Verify if CA is provided!- }- }-- client := &http.Client{- Timeout: 30 * time.Second,- Transport: &http.Transport{- TLSClientConfig: tlsConfig,- },- }-- url := fmt.Sprintf("%s://%s/admin/ban?peer=%s", scheme, targetAddr, peerToBan)- req, err := http.NewRequest("POST", url, nil)- if err != nil {- logger.Fatalf("Failed to create request: %v", err)- }-- if adminToken != "" {- req.Header.Set("Authorization", "Bearer "+adminToken)- }-- resp, err := client.Do(req)- if err != nil {- logger.Fatalf("Failed to send ban request: %v", err)- }- defer func() { _ = resp.Body.Close() }()-- if resp.StatusCode != http.StatusOK {- logger.Fatalf("Ban request failed: %s", resp.Status)- }-- fmt.Printf("Published BANNED event for peer %s\n", peerToBan)- },- }-- banCmd.Flags().StringVar(&peerToBan, "peer", "", "Peer ID to ban")- banCmd.Flags().StringVar(&banReason, "reason", "", "Reason for banning")- banCmd.Flags().StringVar(&connectAddr, "connect", "", "Address of a peer in the mesh to connect to")-- adminCmd.AddCommand(banCmd)- rootCmd.AddCommand(adminCmd)-- if err := rootCmd.Execute(); err != nil {- os.Exit(1)- }-}diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.goindex e27b606c..25d7a63f 100644--- a/cmd/sam-node/main.go+++ b/cmd/sam-node/main.go@@ -515,8 +515,8 @@ func main() {runCmd.Flags().StringSliceVar(&listenAddrs, "listen", []string{"/ip4/0.0.0.0/udp/5001/quic-v1", "/ip4/0.0.0.0/tcp/5002"}, "libp2p Listen Addrs")runCmd.Flags().StringVar(&jwtFlag, "jwt", "", "Pre-fetched JWT token")runCmd.Flags().StringVar(&jwtPathFlag, "jwt-path", "", "Path to file containing JWT token")- runCmd.Flags().StringVar(&clientIDFlag, "client-id", os.Getenv("SAM_OIDC_ID"), "OIDC Client ID for M2M")- runCmd.Flags().StringVar(&clientSecretFlag, "client-secret", os.Getenv("SAM_OIDC_SECRET"), "OIDC Client Secret for M2M")+ runCmd.Flags().StringVar(&clientIDFlag, "client-id", "", "OIDC Client ID for M2M")+ runCmd.Flags().StringVar(&clientSecretFlag, "client-secret", "", "OIDC Client Secret for M2M")runCmd.Flags().StringVar(&hubPublicKeyFlag, "hub-public-key", "", "Hub Public Key (32-byte Hex)")runCmd.Flags().StringVar(&bindAddrFlag, "bind-addr", "127.0.0.1:8080", "Local TCP address for the HTTP server (MCP and Sidecar API)")runCmd.Flags().StringVar(&meshFlag, "mesh", node.DefaultMeshName, "Mesh federation name")@@ -543,7 +543,7 @@ func main() {rootCmd.PersistentFlags().StringVar(&oidcIssuerFlag, "oidc-issuer", "", "OIDC Issuer URL")rootCmd.PersistentFlags().StringVar(&deviceAuthURLFlag, "device-auth-url", "", "OIDC Device Authorization URL")rootCmd.PersistentFlags().StringVar(&audienceFlag, "audience", api.DefaultAudience, "OIDC Audience")- rootCmd.PersistentFlags().StringVar(&dataDirFlag, "data-dir", os.Getenv("SAM_DATA_DIR"), "Override directory for the agent store (defaults to OS user config dir)")+ rootCmd.PersistentFlags().StringVar(&dataDirFlag, "data-dir", "", "Override directory for the agent store (defaults to OS user config dir)")rootCmd.PersistentFlags().BoolVar(&headlessFlag, "headless", false, "Force headless out-of-band (OOB) authentication flow")rootCmd.AddCommand(runCmd)diff --git a/cmd/sam-router/main.go b/cmd/sam-router/main.gonew file mode 100644index 00000000..989c716a--- /dev/null+++ b/cmd/sam-router/main.go@@ -0,0 +1,106 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package main++import (+ "context"+ "os"+ "time"++ "github.com/google/sam/internal/router"+ golog "github.com/ipfs/go-log/v2"+ "github.com/spf13/cobra"+)++var (+ controlPlaneURL string+ listenAddrs []string+ externalAddrs []string+ keysSyncInterval time.Duration+ leaseRenewInterval time.Duration+ oidcToken string+ jwtPath string+ keysPath string+ allowLoopback bool+ logLevel string+)++var logger = golog.Logger("sam-router-cli")++func main() {+ rootCmd := &cobra.Command{+ Use: "sam-router",+ Short: "Sovereign Agent Mesh - libp2p Router Node",+ Run: func(cmd *cobra.Command, args []string) {+ // Initialize logging+ if os.Getenv("LOG_FORMAT") == "json" {+ _ = os.Setenv("GOLOG_LOG_FMT", "json")+ }+ golog.SetAllLoggers(golog.LevelInfo)+ if logLevel != "" {+ lvl, err := golog.LevelFromString(logLevel)+ if err == nil {+ golog.SetAllLoggers(lvl)+ }+ }++ opts := router.Options{+ ControlPlaneURL: controlPlaneURL,+ ListenAddrs: listenAddrs,+ ExternalAddrs: externalAddrs,+ KeysSyncInterval: keysSyncInterval,+ LeaseRenewInterval: leaseRenewInterval,+ OIDCToken: oidcToken,+ JWTPath: jwtPath,+ KeysDBPath: keysPath,+ AllowLoopback: allowLoopback,+ }++ r, err := router.NewRouter(cmd.Context(), opts)+ if err != nil {+ logger.Fatalf("Failed to initialize router: %v", err)+ }+ defer func() {+ if err := r.Close(); err != nil {+ logger.Errorf("Failed to stop router: %v", err)+ }+ }()++ if err := r.Start(); err != nil {+ logger.Fatalf("Failed to start router: %v", err)+ }++ <-cmd.Context().Done()+ },+ }++ rootCmd.Flags().StringVar(&controlPlaneURL, "control-plane", "http://127.0.0.1:8080", "Control Plane web service URL")+ rootCmd.Flags().StringSliceVar(&listenAddrs, "listen", []string{"/ip4/0.0.0.0/tcp/5001", "/ip6/::/tcp/5001"}, "libp2p Listen Addresses")+ rootCmd.Flags().StringSliceVar(&externalAddrs, "external-addr", []string{}, "External addresses to announce to control plane")+ rootCmd.Flags().DurationVar(&keysSyncInterval, "keys-sync-interval", 5*time.Minute, "Key synchronization polling interval")+ rootCmd.Flags().DurationVar(&leaseRenewInterval, "lease-renew-interval", 300*time.Second, "Lease renewal registration interval")+ rootCmd.Flags().StringVar(&oidcToken, "oidc-token", "", "OIDC ID token or bootstrap secret token for enrollment")+ rootCmd.Flags().StringVar(&jwtPath, "jwt-path", "", "Path to file containing OIDC JWT token")+ rootCmd.Flags().StringVar(&keysPath, "keys-path", "router.key", "Path to save/load persistent private key")+ rootCmd.Flags().BoolVar(&allowLoopback, "allow-loopback", false, "Allow loopback and link-local addresses for discovery")+ rootCmd.Flags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error)")++ ctx, cancel := context.WithCancel(context.Background())+ defer cancel()++ if err := rootCmd.ExecuteContext(ctx); err != nil {+ os.Exit(1)+ }+}diff --git a/development/kind/10-control-plane.yaml b/development/kind/10-control-plane.yamlnew file mode 100644index 00000000..08e447d8--- /dev/null+++ b/development/kind/10-control-plane.yaml@@ -0,0 +1,104 @@+apiVersion: v1+kind: Service+metadata:+ name: sam-control-plane+ namespace: ${NAMESPACE}+spec:+ type: ClusterIP+ selector:+ app: sam-control-plane+ ports:+ - { name: http, port: 8080, targetPort: 8080, protocol: TCP }+---+apiVersion: apps/v1+kind: Deployment+metadata:+ name: sam-control-plane+ namespace: ${NAMESPACE}+spec:+ replicas: 1+ selector:+ matchLabels:+ app: sam-control-plane+ template:+ metadata:+ labels:+ app: sam-control-plane+ spec:+ nodeSelector:+ sam-role: hub+ containers:+ - name: sam-control-plane+ image: sam-control-plane:${IMAGE_TAG}+ imagePullPolicy: IfNotPresent+ args:+ - "--bind-address=0.0.0.0:8080"+ - "--db-driver=sqlite"+ - "--db-dsn=/data/control-plane.db"+ - "--issuer=${ISSUER}"+ - "--allowed-audiences=sam-mesh-audience,sam-hub-audience"+ - "--policy-file=/etc/sam/policies/policies.yaml"+ ports:+ - { containerPort: 8080, name: http, protocol: TCP }+ readinessProbe:+ httpGet:+ path: /info+ port: http+ periodSeconds: 2+ volumeMounts:+ - name: policies-volume+ mountPath: /etc/sam/policies+ readOnly: true+ - name: db-data+ mountPath: /data+ volumes:+ - name: policies-volume+ configMap:+ name: sam-control-plane-policies+ - name: db-data+ emptyDir: {}+---+# Host-facing access for a locally-built sam-node. Reachable on the host at+# 127.0.0.1:9090 via the control-plane extraPortMappings.+apiVersion: v1+kind: Service+metadata:+ name: sam-control-plane-nodeport+ namespace: ${NAMESPACE}+spec:+ type: NodePort+ selector:+ app: sam-control-plane+ ports:+ - { name: http, port: 8080, targetPort: 8080, nodePort: 30090, protocol: TCP }+---+apiVersion: v1+kind: ConfigMap+metadata:+ name: sam-control-plane-policies+ namespace: ${NAMESPACE}+data:+ policies.yaml: |+ version: "v1alpha1"+ bindings:+ - members: ["user:system:serviceaccount:${NAMESPACE}:node-a-sa"]+ role: "sam-admin"+ - members: ["user:system:serviceaccount:${NAMESPACE}:node-b-sa"]+ role: "sam-admin"+ - members: ["user:system:serviceaccount:${NAMESPACE}:node-c-sa"]+ role: "sam-admin"+ - members: ["user:system:serviceaccount:${NAMESPACE}:local-node-sa"]+ role: "sam-admin"+ - members: ["group:routers", "user:system:serviceaccount:${NAMESPACE}:sam-router-sa"]+ role: "router"+ roles:+ sam-admin:+ allowed_services:+ - "*"+ allowed_targets:+ - "*"+ router:+ allowed_services:+ - "*"+ allowed_targets:+ - "*"diff --git a/development/kind/10-hub.yaml b/development/kind/10-hub.yamldeleted file mode 100644index f43b803d..00000000--- a/development/kind/10-hub.yaml+++ /dev/null@@ -1,104 +0,0 @@-apiVersion: apps/v1-kind: Deployment-metadata:- name: sam-hub- namespace: ${NAMESPACE}-spec:- replicas: 1- selector:- matchLabels:- app: sam-hub- template:- metadata:- labels:- app: sam-hub- spec:- nodeSelector:- sam-role: hub- containers:- - name: sam-hub- image: sam-hub:${IMAGE_TAG}- imagePullPolicy: IfNotPresent- args:- - "--issuer=${ISSUER}"- - "--key=0000000000000000000000000000000000000000000000000000000000000000"- - "--allowed-audiences=sam-mesh-audience"- - "--insecure-skip-tls-verify"- - "--listen=/ip4/0.0.0.0/tcp/4001,/ip4/0.0.0.0/udp/4001/quic-v1"- # The hub announces ONLY these addrs at enrollment (ExternalAddrs- # replaces the auto-detected ones), so list both the in-cluster service- # addr (for pods) and the host-mapped addr (for a local sam-node).- - "--external-multiaddr=/dns4/sam-hub.${NAMESPACE}.svc.cluster.local/tcp/4001"- - "--external-multiaddr=/ip4/127.0.0.1/tcp/4001"- - "--policy-file=/etc/sam/policies/policies.yaml"- ports:- - { containerPort: 9090, name: http, protocol: TCP }- - { containerPort: 4001, name: p2p-tcp, protocol: TCP }- - { containerPort: 4001, name: p2p-udp, protocol: UDP }- # Gate readiness on the register endpoint so run.sh can wait for the- # hub before starting the nodes (avoids the node enrollment race).- readinessProbe:- tcpSocket: { port: 9090 }- periodSeconds: 2- volumeMounts:- - name: policies-volume- mountPath: /etc/sam/policies- readOnly: true- volumes:- - name: policies-volume- configMap:- name: sam-hub-policies-----apiVersion: v1-kind: Service-metadata:- name: sam-hub- namespace: ${NAMESPACE}-spec:- type: ClusterIP- selector:- app: sam-hub- ports:- - { name: http, port: 9090, targetPort: 9090, protocol: TCP }- - { name: p2p-tcp, port: 4001, targetPort: 4001, protocol: TCP }- - { name: p2p-udp, port: 4001, targetPort: 4001, protocol: UDP }-----# Host-facing access for a locally-built sam-node. Reachable on the host at-# 127.0.0.1:9090 / 127.0.0.1:4001 via the control-plane extraPortMappings.-apiVersion: v1-kind: Service-metadata:- name: sam-hub-nodeport- namespace: ${NAMESPACE}-spec:- type: NodePort- selector:- app: sam-hub- ports:- - { name: http, port: 9090, targetPort: 9090, nodePort: 30090, protocol: TCP }- - { name: p2p-tcp, port: 4001, targetPort: 4001, nodePort: 30401, protocol: TCP }------apiVersion: v1-kind: ConfigMap-metadata:- name: sam-hub-policies- namespace: ${NAMESPACE}-data:- policies.yaml: |- version: "v1alpha1"- bindings:- - members: ["user:system:serviceaccount:${NAMESPACE}:node-a-sa"]- role: "sam-admin"- - members: ["user:system:serviceaccount:${NAMESPACE}:node-b-sa"]- role: "sam-admin"- - members: ["user:system:serviceaccount:${NAMESPACE}:node-c-sa"]- role: "sam-admin"- - members: ["user:system:serviceaccount:${NAMESPACE}:local-node-sa"]- role: "sam-admin"- roles:- sam-admin:- allowed_services:- - "*"- allowed_targets:- - "*"diff --git a/development/kind/11-router.yaml b/development/kind/11-router.yamlnew file mode 100644index 00000000..fe54f7fb--- /dev/null+++ b/development/kind/11-router.yaml@@ -0,0 +1,84 @@+apiVersion: v1+kind: ServiceAccount+metadata:+ name: sam-router-sa+ namespace: ${NAMESPACE}+---+apiVersion: v1+kind: Service+metadata:+ name: sam-router+ namespace: ${NAMESPACE}+spec:+ type: ClusterIP+ selector:+ app: sam-router+ ports:+ - { name: p2p-tcp, port: 4501, targetPort: 4501, protocol: TCP }+ - { name: p2p-udp, port: 4501, targetPort: 4501, protocol: UDP }+---+apiVersion: apps/v1+kind: StatefulSet+metadata:+ name: sam-router+ namespace: ${NAMESPACE}+spec:+ serviceName: "sam-router"+ replicas: 1+ selector:+ matchLabels:+ app: sam-router+ template:+ metadata:+ labels:+ app: sam-router+ spec:+ serviceAccountName: sam-router-sa+ nodeSelector:+ sam-role: hub+ containers:+ - name: sam-router+ image: sam-router:${IMAGE_TAG}+ imagePullPolicy: IfNotPresent+ ports:+ - { containerPort: 4501, name: p2p-tcp, protocol: TCP }+ - { containerPort: 4501, name: p2p-udp, protocol: UDP }+ args:+ - "--control-plane=http://sam-control-plane.${NAMESPACE}.svc.cluster.local:8080"+ - "--listen=/ip4/0.0.0.0/tcp/4501"+ - "--listen=/ip4/0.0.0.0/udp/4501/quic-v1"+ - "--external-addr=/dns4/sam-router.${NAMESPACE}.svc.cluster.local/tcp/4501"+ - "--external-addr=/ip4/127.0.0.1/tcp/4001"+ - "--jwt-path=/var/run/secrets/tokens/sam-token"+ - "--keys-path=/data/router.key"+ - "--allow-loopback"+ volumeMounts:+ - name: sam-token+ mountPath: /var/run/secrets/tokens+ readOnly: true+ - name: router-data+ mountPath: /data+ volumes:+ - name: sam-token+ projected:+ sources:+ - serviceAccountToken:+ path: sam-token+ expirationSeconds: 3600+ audience: "sam-hub-audience"+ - name: router-data+ emptyDir: {}+---+# Host-facing access for a locally-built sam-node. Reachable on the host at+# 127.0.0.1:4001 via the control-plane extraPortMappings.+apiVersion: v1+kind: Service+metadata:+ name: sam-router-nodeport+ namespace: ${NAMESPACE}+spec:+ type: NodePort+ selector:+ app: sam-router+ ports:+ - { name: p2p-tcp, port: 4501, targetPort: 4501, nodePort: 30401, protocol: TCP }diff --git a/development/kind/node.template.yaml b/development/kind/node.template.yamlindex ef29f690..745b3660 100644--- a/development/kind/node.template.yaml+++ b/development/kind/node.template.yaml@@ -28,7 +28,7 @@ spec:imagePullPolicy: IfNotPresentargs:- "run"- - "--hub=http://sam-hub:9090"+ - "--hub=http://sam-control-plane:8080"- "--jwt-path=/var/run/secrets/tokens/sam-token"- "--api-token=devtoken"- "--bind-addr=0.0.0.0:8080"diff --git a/development/kind/run.sh b/development/kind/run.shindex 2cb61e89..5743ec59 100755--- a/development/kind/run.sh+++ b/development/kind/run.sh@@ -55,9 +55,10 @@ logs() { echo "printf '\\033[1;36m==== %s ====\\033[0m\\n' '$1'; kubectl --contetmuxs() { tmux -L samsocket -f /dev/null "$@"; }show_cluster_logs() {- tmuxs kill-session -t "${SESSION}" 2>/dev/null || true+ tmuxs kill-session -t "${SESSION}" 2>/dev/null || true- tmuxs new-session -d -s "${SESSION}" -n mesh "$(logs hub 'deploy/sam-hub')" \; set -t "${SESSION}" destroy-unattached off+ tmuxs new-session -d -s "${SESSION}" -n mesh "$(logs control-plane 'deploy/sam-control-plane')" \; set -t "${SESSION}" destroy-unattached off+ tmuxs split-window -t "${SESSION}:0" "$(logs router 'statefulset/sam-router')"for node in "${NODES[@]}"; dotmuxs split-window -t "${SESSION}:0" "$(logs "$node" "deploy/${node} -c sam-node")"tmuxs select-layout -t "${SESSION}:0" tiled@@ -65,8 +66,8 @@ show_cluster_logs() {tmuxs set-option -t "${SESSION}" -g pane-border-status toptmuxs set-option -t "${SESSION}" -g pane-border-format ' #{pane_title} '- # Title the tmux panes in creation order: hub first, then the nodes.- titles=(hub "${NODES[@]}")+ # Title the tmux panes in creation order: control-plane, router, then the nodes.+ titles=(control-plane router "${NODES[@]}")i=0for pane in $(tmuxs list-panes -t "${SESSION}:0" -F '#{pane_id}'); dotmuxs select-pane -t "$pane" -T "${titles[$i]}"@@ -106,24 +107,26 @@ echo "== Creating kind cluster '${CLUSTER}' =="kind create cluster --name "${CLUSTER}" --config "${SCRIPT_DIR}/kind-config.yaml"echo "== Building sam images =="-make docker-build-hub docker-build-node+make docker-build-control-plane docker-build-router docker-build-nodeecho "== Loading sam images into kind =="-kind load docker-image --name "${CLUSTER}" "sam-hub:${IMAGE_TAG}" "sam-node:${IMAGE_TAG}"+kind load docker-image --name "${CLUSTER}" "sam-control-plane:${IMAGE_TAG}" "sam-router:${IMAGE_TAG}" "sam-node:${IMAGE_TAG}"read_mesh_nodes-# Apply the hub and wait until it accepts connections+# Apply the control plane and router, and wait until they accept connectionsISSUER="$(kubectl --context "${KCTX}" get --raw /.well-known/openid-configuration | jq -r .issuer)"[[ -n "$ISSUER" ]] || { echo "could not determine cluster OIDC issuer" >&2; exit 1; }export NAMESPACE ISSUER IMAGE_TAG-echo "== Applying sam-hub (issuer: ${ISSUER}) =="-for f in "${SCRIPT_DIR}"/00-*.yaml "${SCRIPT_DIR}"/10-*.yaml; do+echo "== Applying control plane and router (issuer: ${ISSUER}) =="+for f in "${SCRIPT_DIR}"/00-*.yaml "${SCRIPT_DIR}"/10-*.yaml "${SCRIPT_DIR}"/11-*.yaml; doenvsubst '${NAMESPACE} ${ISSUER} ${IMAGE_TAG}' < "$f" | kubectl --context "${KCTX}" apply -f -done-echo "== Waiting for sam-hub to be ready =="-kubectl --context "${KCTX}" -n "${NAMESPACE}" wait --for=condition=available --timeout=180s deployment/sam-hub+echo "== Waiting for control plane to be ready =="+kubectl --context "${KCTX}" -n "${NAMESPACE}" wait --for=condition=available --timeout=180s deployment/sam-control-plane+echo "== Waiting for router to be ready =="+kubectl --context "${KCTX}" -n "${NAMESPACE}" wait --for=condition=ready --timeout=180s pod -l app=sam-routerecho "== Applying sam-nodes =="for line in "${NODE_LINES[@]}"; dodiff --git a/go.mod b/go.modindex 9c677b70..5460161e 100644--- a/go.mod+++ b/go.mod@@ -9,6 +9,7 @@ require (github.com/hashicorp/golang-lru/v2 v2.0.7github.com/ipfs/go-cid v0.6.1github.com/ipfs/go-log/v2 v2.9.2+ github.com/jackc/pgx/v5 v5.10.0github.com/libp2p/go-libp2p v0.48.1-0.20260603104142-e6309a296286github.com/libp2p/go-libp2p-gostream v0.6.0github.com/libp2p/go-libp2p-http v0.5.0@@ -30,6 +31,7 @@ require (golang.org/x/time v0.15.0google.golang.org/protobuf v1.36.11gopkg.in/yaml.v2 v2.4.0+ modernc.org/sqlite v1.53.0)require (@@ -42,6 +44,7 @@ require (github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirectgithub.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.1 // indirectgithub.com/dunglas/httpsfv v1.1.0 // indirect+ github.com/dustin/go-humanize v1.0.1 // indirectgithub.com/filecoin-project/go-clock v0.1.0 // indirectgithub.com/flynn/noise v1.1.0 // indirectgithub.com/go-jose/go-jose/v4 v4.1.4 // indirect@@ -58,6 +61,9 @@ require (github.com/ipfs/boxo v0.40.0 // indirectgithub.com/ipfs/go-datastore v0.9.1 // indirectgithub.com/ipld/go-ipld-prime v0.24.0 // indirect+ github.com/jackc/pgpassfile v1.0.0 // indirect+ github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect+ github.com/jackc/puddle/v2 v2.2.2 // indirectgithub.com/jackpal/go-nat-pmp v1.0.2 // indirectgithub.com/jbenet/go-temp-err-catcher v0.1.0 // indirectgithub.com/klauspost/cpuid/v2 v2.3.0 // indirect@@ -86,6 +92,7 @@ require (github.com/multiformats/go-multistream v0.6.1 // indirectgithub.com/multiformats/go-varint v0.1.0 // indirectgithub.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect+ github.com/ncruces/go-strftime v1.0.0 // indirectgithub.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirectgithub.com/pion/datachannel v1.6.0 // indirectgithub.com/pion/dtls/v3 v3.1.3 // indirect@@ -109,6 +116,7 @@ require (github.com/quic-go/qpack v0.6.0 // indirectgithub.com/quic-go/quic-go v0.59.1 // indirectgithub.com/quic-go/webtransport-go v0.10.0 // indirect+ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirectgithub.com/segmentio/asm v1.2.1 // indirectgithub.com/segmentio/encoding v0.5.4 // indirectgithub.com/spaolacci/murmur3 v1.1.0 // indirect@@ -138,4 +146,7 @@ require (golang.org/x/tools v0.45.0 // indirectgonum.org/v1/gonum v0.17.0 // indirectlukechampine.com/blake3 v1.4.1 // indirect+ modernc.org/libc v1.73.4 // indirect+ modernc.org/mathutil v1.7.1 // indirect+ modernc.org/memory v1.11.0 // indirect)diff --git a/go.sum b/go.sumindex 3ce87449..37cab06e 100644--- a/go.sum+++ b/go.sum@@ -32,6 +32,8 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.1 h1:5RVFMOWjMyRy8cARdy79nAmgYw3hgithub.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.1/go.mod h1:ZXNYxsqcloTdSy/rNShjYzMhyjf0LaoftYK0p+A3h40=github.com/dunglas/httpsfv v1.1.0 h1:Jw76nAyKWKZKFrpMMcL76y35tOpYHqQPzHQiwDvpe54=github.com/dunglas/httpsfv v1.1.0/go.mod h1:zID2mqw9mFsnt7YC3vYQ9/cjq30q41W+1AnDwH8TiMg=+github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=+github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=github.com/filecoin-project/go-clock v0.1.0 h1:SFbYIM75M8NnFm1yMHhN9Ahy3W5bEZV9gd6MPfXbKVU=github.com/filecoin-project/go-clock v0.1.0/go.mod h1:4uB/O4PvOjlx1VCMdZ9MyDZXRm//gkj1ELEbxfI1AZs=github.com/flynn/noise v1.1.0 h1:KjPQoQCEFdZDiP03phOvGi11+SVVhBG2wOWAorLsstg=@@ -55,6 +57,8 @@ github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kFgithub.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=github.com/google/jsonschema-go v0.4.3 h1:/DBOLZTfDow7pe2GmaJNhltueGTtDKICi8V8p+DQPd0=github.com/google/jsonschema-go v0.4.3/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=+github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=+github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=@@ -87,6 +91,14 @@ github.com/ipfs/go-test v0.3.0 h1:0Y4Uve3tp9HI+2lIJjfOliOrOgv/YpXg/l1y3P4DEYE=github.com/ipfs/go-test v0.3.0/go.mod h1:JK+U8pRpATZb7lsYNSJlCj3WYB3cFfWIbI6nWRM/GFk=github.com/ipld/go-ipld-prime v0.24.0 h1:6th8Z6Peh5bCWuRAVZcDO1sHzZdVF6F2cCCDG3681tg=github.com/ipld/go-ipld-prime v0.24.0/go.mod h1:DYZxr/5caLNFbcuU6zLOgwSW7CgUEoC4wJiZMEU8Zhs=+github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=+github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=+github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=+github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=+github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=+github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=+github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=+github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=github.com/jbenet/go-temp-err-catcher v0.1.0 h1:zpb3ZH6wIE8Shj2sKS+khgRvf7T7RABoLk/+KKHggpk=@@ -189,6 +201,8 @@ github.com/multiformats/go-varint v0.1.0 h1:i2wqFp4sdl3IcIxfAonHQV9qU5OsZ4Ts9IOogithub.com/multiformats/go-varint v0.1.0/go.mod h1:5KVAVXegtfmNQQm/lCY+ATvDzvJJhSkUlGQV9wgObdI=github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=+github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=+github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=github.com/openai/openai-go v1.12.0 h1:NBQCnXzqOTv5wsgNC36PrFEiskGfO5wccfCWDo9S1U0=github.com/openai/openai-go v1.12.0/go.mod h1:g461MYGXEXBVdV5SaR/5tNzNbSfwTBBefwc+LlDCK0Y=github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=@@ -245,6 +259,8 @@ github.com/quic-go/quic-go v0.59.1 h1:0Gmua0HW1Tv7ANR7hUYwRyD0MG5OJfgvYSZasGZzBigithub.com/quic-go/quic-go v0.59.1/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=github.com/quic-go/webtransport-go v0.10.0 h1:LqXXPOXuETY5Xe8ITdGisBzTYmUOy5eSj+9n4hLTjHI=github.com/quic-go/webtransport-go v0.10.0/go.mod h1:LeGIXr5BQKE3UsynwVBeQrU1TPrbh73MGoC6jd+V7ow=+github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=+github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=@@ -264,7 +280,9 @@ github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3Angithub.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=@@ -383,7 +401,36 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVgopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=lukechampine.com/blake3 v1.4.1 h1:I3Smz7gso8w4/TunLKec6K2fn+kyKtDxr/xcQEN84Wg=lukechampine.com/blake3 v1.4.1/go.mod h1:QFosUxmjB8mnrWFSNwKmvxHpfY72bmD2tQ0kBMM3kwo=+modernc.org/cc/v4 v4.28.4 h1:Hd/4Es+MBj+/7hSdZaisNyu6bv3V0Dp2MdllyfqaH+c=+modernc.org/cc/v4 v4.28.4/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI=+modernc.org/ccgo/v4 v4.34.4 h1:OVnSOWQjVKOYkFxoHYB+qQmSHK5gqMqARM+K9DpR/Ws=+modernc.org/ccgo/v4 v4.34.4/go.mod h1:qdKqE8FNIYyysougB1RX9MxCzp5oJOcQXSobANJ4TuE=+modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM=+modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU=+modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=+modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=+modernc.org/gc/v3 v3.1.3 h1:6QAplYyVO+KdPW3pGnqmJDUxtkec8ooEWvks/hhU3lc=+modernc.org/gc/v3 v3.1.3/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=+modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=+modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=+modernc.org/libc v1.73.4 h1:+ra4Ui8ngyt8HDcO1FTDPWlkAh6yOdaO2yAoh8MddQA=+modernc.org/libc v1.73.4/go.mod h1:DXZ3eO8qMCNn2SnmTNCiC71nJ9Rcq3PsnpU6Vc4rWK8=+modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=+modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=+modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=+modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=+modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg=+modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=+modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=+modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=+modernc.org/sqlite v1.53.0 h1:20WG8N9q4ji/dEqGk4uiI0c6OPjSeLTNYGFCc3+7c1M=+modernc.org/sqlite v1.53.0/go.mod h1:xoEpOIpGrgT48H5iiyt/YXPCZPEzlfmfFwtk8Lklw8s=+modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=+modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=+modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=+modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=diff --git a/internal/controlplane/config.go b/internal/controlplane/config.gonew file mode 100644index 00000000..3ae9479b--- /dev/null+++ b/internal/controlplane/config.go@@ -0,0 +1,66 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package controlplane++import (+ "fmt"+ "time"+)++// Options holds configuration for the control plane.+type Options struct {+ ListenAddr string+ DriverName string+ DataSourceName string+ OIDCIssuer string+ AllowedAudiences []string+ LeaseDuration time.Duration+ KeyRotationInterval time.Duration+ KeyGracePeriod time.Duration+ InsecureSkipTLSVerify bool+ BiscuitTimeout time.Duration+ PolicyPath string // Optional: path to bootstrap policy configuration+}++// Default sets default values for control plane options.+func (o *Options) Default() {+ if o.ListenAddr == "" {+ o.ListenAddr = "0.0.0.0:8080"+ }+ if o.DriverName == "" {+ o.DriverName = "sqlite"+ o.DataSourceName = "control-plane.db"+ }+ if o.LeaseDuration <= 0 {+ o.LeaseDuration = 60 * time.Second+ }+ if o.KeyRotationInterval <= 0 {+ o.KeyRotationInterval = 24 * time.Hour+ }+ if o.KeyGracePeriod <= 0 {+ o.KeyGracePeriod = 1 * time.Hour+ }+}++// Validate ensures options are valid.+func (o *Options) Validate() error {+ if o.DriverName == "" {+ return fmt.Errorf("DriverName must be specified")+ }+ if o.DataSourceName == "" {+ return fmt.Errorf("DataSourceName must be specified")+ }+ return nil+}diff --git a/internal/hub/config.go b/internal/controlplane/policy.gosimilarity index 99%rename from internal/hub/config.gorename to internal/controlplane/policy.goindex 93ffb164..f1964797 100644--- a/internal/hub/config.go+++ b/internal/controlplane/policy.go@@ -12,7 +12,7 @@// See the License for the specific language governing permissions and// limitations under the License.-package hub+package controlplaneimport ("fmt"diff --git a/internal/controlplane/server.go b/internal/controlplane/server.gonew file mode 100644index 00000000..f5165d29--- /dev/null+++ b/internal/controlplane/server.go@@ -0,0 +1,617 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package controlplane++import (+ "context"+ "crypto/ed25519"+ "crypto/rand"+ "crypto/tls"+ "encoding/hex"+ "errors"+ "fmt"+ "io"+ "net"+ "net/http"+ "strings"+ "sync"+ "time"++ "github.com/biscuit-auth/biscuit-go/v2"+ "github.com/coreos/go-oidc/v3/oidc"+ "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/peer"+ "golang.org/x/time/rate"+ "google.golang.org/protobuf/proto"+ "gopkg.in/yaml.v2"+)++var logger = golog.Logger("sam-control-plane")++const (+ EnrollRateLimit = 10+ EnrollBurst = 20+ JWTVerificationTimeout = 10 * time.Second+)++// Server implements the SAM Control Plane web app.+type Server struct {+ config Options+ store storage.Store+ httpServer *http.Server+ listener net.Listener+ limiter *rate.Limiter++ providersMu sync.RWMutex+ providers map[string]*oidc.Provider++ ctx context.Context+ cancel context.CancelFunc+ wg sync.WaitGroup+ shutdown bool+}++// NewServer initializes the control plane server and stores configuration.+func NewServer(config Options, store storage.Store) (*Server, error) {+ config.Default()+ if err := config.Validate(); err != nil {+ return nil, err+ }++ ctx, cancel := context.WithCancel(context.Background())++ return &Server{+ config: config,+ store: store,+ limiter: rate.NewLimiter(rate.Limit(EnrollRateLimit), EnrollBurst),+ providers: make(map[string]*oidc.Provider),+ ctx: ctx,+ cancel: cancel,+ }, nil+}++// Start boots up HTTP services, sets up OIDC providers, loads initial keys and policies, and schedules rotations.+func (s *Server) Start() error {+ // Initialize Keyring+ ctx := context.Background()+ _, _, err := s.store.GetCurrentKey(ctx)+ if err == storage.ErrNotFound {+ logger.Info("Generating initial control plane signing keys...")+ pub, priv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ return fmt.Errorf("failed to generate initial key: %w", err)+ }+ if err := s.store.SaveInitialKey(ctx, priv, pub); err != nil {+ return fmt.Errorf("failed to save initial key: %w", err)+ }+ } else if err != nil {+ return fmt.Errorf("failed to query initial keyring status: %w", err)+ }++ // Bootstrap Policy from file if DB is empty and path is provided+ if s.config.PolicyPath != "" {+ _, err := s.store.GetPolicy(ctx)+ if err == storage.ErrNotFound {+ logger.Infof("Bootstrapping mesh policy from %s...", s.config.PolicyPath)+ policy, err := loadPolicyFromFile(s.config.PolicyPath)+ if err != nil {+ return fmt.Errorf("failed to load boot policy file: %w", err)+ }+ if err := s.store.SavePolicy(ctx, policy); err != nil {+ return fmt.Errorf("failed to save boot policy: %w", err)+ }+ }+ }++ // Initialize OIDC Providers+ if err := s.discoverProviders(); err != nil {+ return fmt.Errorf("failed OIDC discovery: %w", err)+ }++ // Setup listener+ l, err := net.Listen("tcp", s.config.ListenAddr)+ if err != nil {+ return err+ }+ s.listener = l++ mux := http.NewServeMux()+ mux.HandleFunc("/info", s.HandleInfo)+ mux.HandleFunc("/register", s.HandleRegister)+ mux.HandleFunc("/keys", s.HandleKeys)+ mux.HandleFunc("/routers/lease", s.HandleRouterLease)+ mux.HandleFunc("/policies", s.HandlePolicies)++ s.httpServer = &http.Server{+ Handler: mux,+ }++ s.wg.Add(1)+ go func() {+ defer s.wg.Done()+ logger.Infof("SAM Control Plane listening on http://%s", s.config.ListenAddr)+ if err := s.httpServer.Serve(l); err != nil && !errors.Is(err, http.ErrServerClosed) {+ logger.Errorf("HTTP Server error: %v", err)+ }+ }()++ // Start key rotation routine+ s.wg.Add(1)+ go s.runKeyRotationLoop()++ return nil+}++func (s *Server) discoverProviders() error {+ s.providersMu.Lock()+ defer s.providersMu.Unlock()++ issuers := strings.Split(s.config.OIDCIssuer, ",")+ for _, iss := range issuers {+ iss = strings.TrimSpace(iss)+ if iss == "" {+ continue+ }+ tr := http.DefaultTransport.(*http.Transport).Clone()+ tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: s.config.InsecureSkipTLSVerify}+ client := &http.Client{+ Timeout: 30 * time.Second,+ Transport: tr,+ }+ providerCtx := oidc.ClientContext(s.ctx, client)+ provider, err := oidc.NewProvider(providerCtx, iss)+ if err != nil {+ return fmt.Errorf("failed to create provider for %s: %w", iss, err)+ }+ s.providers[iss] = provider+ }+ return nil+}++func (s *Server) getProviders() map[string]*oidc.Provider {+ s.providersMu.RLock()+ defer s.providersMu.RUnlock()++ pCopy := make(map[string]*oidc.Provider)+ for k, v := range s.providers {+ pCopy[k] = v+ }+ return pCopy+}++func (s *Server) runKeyRotationLoop() {+ defer s.wg.Done()+ if s.config.KeyRotationInterval <= 0 {+ return+ }++ ticker := time.NewTicker(s.config.KeyRotationInterval)+ defer ticker.Stop()++ for {+ select {+ case <-ticker.C:+ logger.Info("Rotating Biscuit signing keys...")+ newPub, newPriv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ logger.Errorf("Failed to generate key pair for rotation: %v", err)+ continue+ }+ err = s.store.RotateKeys(s.ctx, newPriv, newPub, s.config.KeyGracePeriod)+ if err != nil {+ logger.Errorf("Failed to rotate keyring: %v", err)+ } else {+ logger.Infof("Key rotation committed. New current public key: %s", hex.EncodeToString(newPub))+ }+ case <-s.ctx.Done():+ return+ }+ }+}++// HandleInfo HTTP GET `/info`+func (s *Server) HandleInfo(w http.ResponseWriter, r *http.Request) {+ if r.Method != http.MethodGet {+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)+ return+ }++ issuer := s.config.OIDCIssuer+ if strings.Contains(issuer, ",") {+ parts := strings.Split(issuer, ",")+ issuer = strings.TrimSpace(parts[0])+ }++ aud := api.DefaultAudience+ if len(s.config.AllowedAudiences) > 0 {+ aud = s.config.AllowedAudiences[0]+ }++ // Fetch active routers+ activeRouters, err := s.store.GetActiveRouters(r.Context())+ if err != nil {+ logger.Errorf("Failed to retrieve active routers: %v", err)+ http.Error(w, "Internal server error", http.StatusInternalServerError)+ return+ }++ var routerAddrs []string+ for _, r := range activeRouters {+ routerAddrs = append(routerAddrs, r.Addresses...)+ }++ resp := &api.HubInfoResponse{+ OidcIssuer: issuer,+ ClientId: aud,+ Audience: aud,+ HubAddresses: routerAddrs, // Reused this field for back-compatibility with bootstrap routers list+ }++ 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)+}++// HandleRegister HTTP POST `/register`+func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {+ if r.Method != http.MethodPost {+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)+ return+ }++ body, err := io.ReadAll(r.Body)+ if err != nil {+ http.Error(w, "Failed to read body", http.StatusBadRequest)+ return+ }+ defer func() { _ = r.Body.Close() }()++ var req api.EnrollRequest+ if err := proto.Unmarshal(body, &req); err != nil {+ http.Error(w, "Invalid request format", http.StatusBadRequest)+ return+ }++ if !s.limiter.Allow() {+ http.Error(w, "Rate limit exceeded", http.StatusTooManyRequests)+ return+ }++ logger.Infow("New enrollment request", "peer_id", req.PeerId)++ ctx, cancel := context.WithTimeout(r.Context(), JWTVerificationTimeout)+ defer cancel()++ claims, token, err := identity.VerifyJWT(ctx, req.Jwt, s.config.AllowedAudiences, s.getProviders())+ if err != nil {+ logger.Errorw("JWT verification failed", "peer_id", req.PeerId, "error", err)+ http.Error(w, "JWT validation failed: "+err.Error(), http.StatusUnauthorized)+ return+ }++ pID, err := peer.Decode(req.PeerId)+ if err != nil {+ http.Error(w, "Invalid Peer ID", http.StatusBadRequest)+ return+ }++ // Fetch mesh policy+ 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+ }++ // Fetch current signing private key+ privKey, pubKey, 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+ }++ // Mint token+ biscuitData, err := identity.MintBiscuitToken(privKey, claims, token, pID, policy)+ 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+ }++ // Save to DB+ if err := s.store.EnrollNode(ctx, req.PeerId, biscuitData, token.Expiry); err != nil {+ logger.Errorf("Failed to persist node enrollment: %v", err)+ http.Error(w, "Internal server error", http.StatusInternalServerError)+ return+ }++ // Fetch active routers+ activeRouters, err := s.store.GetActiveRouters(ctx)+ if err != nil {+ logger.Errorf("Failed to retrieve active routers: %v", err)+ http.Error(w, "Internal server error", http.StatusInternalServerError)+ return+ }++ var routerAddrs []string+ for _, r := range activeRouters {+ routerAddrs = append(routerAddrs, r.Addresses...)+ }++ resp := &api.EnrollResponse{+ BiscuitToken: biscuitData,+ HubPublicKey: pubKey,+ HubAddresses: routerAddrs, // routers nodes multiaddresses+ Expiration: token.Expiry.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 {+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)+ return+ }++ validKeys, err := s.store.GetAllValidKeys(r.Context())+ if err != nil {+ logger.Errorf("Failed to retrieve valid keys: %v", err)+ http.Error(w, "Internal server error", http.StatusInternalServerError)+ return+ }++ var pubKeys [][]byte+ for _, k := range validKeys {+ pubKeys = append(pubKeys, k.Public)+ }++ resp := &api.KeysResponse{+ PublicKeys: pubKeys,+ }++ 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)+}++// HandleRouterLease HTTP POST `/routers/lease`+func (s *Server) HandleRouterLease(w http.ResponseWriter, r *http.Request) {+ if r.Method != http.MethodPost {+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)+ return+ }++ body, err := io.ReadAll(r.Body)+ if err != nil {+ http.Error(w, "Failed to read body", http.StatusBadRequest)+ return+ }+ defer func() { _ = r.Body.Close() }()++ var req api.RouterLeaseRequest+ if err := proto.Unmarshal(body, &req); err != nil {+ http.Error(w, "Invalid request format", http.StatusBadRequest)+ return+ }++ pID, err := peer.Decode(req.PeerId)+ if err != nil {+ http.Error(w, "Invalid Peer ID", http.StatusBadRequest)+ return+ }++ // Fetch all valid public keys from CP to authorize router biscuit+ validKeys, err := s.store.GetAllValidKeys(r.Context())+ if err != nil {+ logger.Errorf("Failed to retrieve valid keys: %v", err)+ http.Error(w, "Internal server error", http.StatusInternalServerError)+ return+ }++ var cpPubKeys []ed25519.PublicKey+ for _, k := range validKeys {+ cpPubKeys = append(cpPubKeys, k.Public)+ }++ // Verify Biscuit and enforce expected remote peer id+ b, err := identity.VerifyBiscuit(req.Biscuit, pID, cpPubKeys, s.config.BiscuitTimeout)+ if err != nil {+ logger.Warnf("Router %s failed biscuit verification: %v", req.PeerId, err)+ http.Error(w, "Biscuit verification failed: "+err.Error(), http.StatusUnauthorized)+ return+ }++ // Enforce role("router") or role("bootstrap") inside the biscuit+ authorizer, err := b.Authorizer(cpPubKeys[0]) // First public key is generally the current one, but biscuit-go handles matches internally+ if err != nil {+ http.Error(w, "Internal authorizer error", http.StatusInternalServerError)+ return+ }++ authorizer.AddCheck(biscuit.Check{Queries: []biscuit.Rule{+ {+ Body: []biscuit.Predicate{+ {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(api.RoleRouter)}},+ },+ },+ {+ Body: []biscuit.Predicate{+ {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(api.RoleBootstrap)}},+ },+ },+ }})+ authorizer.AddPolicy(api.AllowIfTruePolicy)++ if err := authorizer.Authorize(); err != nil {+ logger.Warnf("Router %s lacks router role in its biscuit: %v", req.PeerId, err)+ http.Error(w, "Unauthorized: entity is not a router", http.StatusForbidden)+ return+ }++ // Expose lease renewal+ expiresAt := time.Now().Add(s.config.LeaseDuration)+ lease := &storage.RouterLease{+ PeerID: req.PeerId,+ Addresses: req.Addresses,+ LastRenewal: time.Now(),+ ExpiresAt: expiresAt,+ }++ if err := s.store.UpsertRouterLease(r.Context(), lease); err != nil {+ logger.Errorf("Failed to upsert router lease: %v", err)+ http.Error(w, "Internal server error", http.StatusInternalServerError)+ return+ }++ resp := &api.RouterLeaseResponse{+ Success: true,+ ExpiresAt: expiresAt.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)+}++// HandlePolicies HTTP GET/POST/PUT `/policies`+func (s *Server) HandlePolicies(w http.ResponseWriter, r *http.Request) {+ // Simple HTTP admin methods for policies+ switch r.Method {+ case http.MethodGet:+ policy, err := s.store.GetPolicy(r.Context())+ if err == storage.ErrNotFound {+ http.Error(w, "No policy configured", http.StatusNotFound)+ return+ }+ if err != nil {+ logger.Errorf("Failed to load policy: %v", err)+ http.Error(w, "Internal server error", http.StatusInternalServerError)+ return+ }++ yamlData, err := yaml.Marshal(policy)+ if err != nil {+ http.Error(w, "Failed to marshal yaml", http.StatusInternalServerError)+ return+ }++ resp := &api.PolicyConfigGetResponse{YamlContent: string(yamlData)}+ respData, _ := proto.Marshal(resp)+ w.Header().Set("Content-Type", "application/x-protobuf")+ w.WriteHeader(http.StatusOK)+ _, _ = w.Write(respData)++ case http.MethodPost, http.MethodPut:+ body, err := io.ReadAll(r.Body)+ if err != nil {+ http.Error(w, "Failed to read body", http.StatusBadRequest)+ return+ }+ defer func() { _ = r.Body.Close() }()++ var req api.PolicyConfigUpdateRequest+ if err := proto.Unmarshal(body, &req); err != nil {+ http.Error(w, "Invalid request format", http.StatusBadRequest)+ return+ }++ var policy api.PolicyConfig+ if err := yaml.Unmarshal([]byte(req.YamlContent), &policy); err != nil {+ http.Error(w, "Invalid YAML policy format: "+err.Error(), http.StatusBadRequest)+ return+ }++ // Run validation similar to Hub config loading+ if err := ValidatePolicyConfig(&policy); err != nil {+ http.Error(w, "Invalid policy structure: "+err.Error(), http.StatusBadRequest)+ return+ }++ if err := s.store.SavePolicy(r.Context(), &policy); err != nil {+ logger.Errorf("Failed to save policy: %v", err)+ http.Error(w, "Internal server error", http.StatusInternalServerError)+ return+ }++ resp := &api.PolicyConfigUpdateResponse{Success: true}+ respData, _ := proto.Marshal(resp)+ w.Header().Set("Content-Type", "application/x-protobuf")+ w.WriteHeader(http.StatusOK)+ _, _ = w.Write(respData)++ default:+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)+ }+}++// Close shuts down background loops and HTTP server.+func (s *Server) Close() error {+ s.shutdown = true+ s.cancel()++ var errs []error+ if s.httpServer != nil {+ if err := s.httpServer.Shutdown(context.Background()); err != nil {+ errs = append(errs, err)+ }+ }+ s.wg.Wait()+ return errors.Join(errs...)+}++func loadPolicyFromFile(path string) (*api.PolicyConfig, error) {+ // Reused load script from config.go+ return LoadPolicyConfig(path)+}++// Addr returns the network address the server is listening on.+func (s *Server) Addr() string {+ if s.listener == nil {+ return ""+ }+ return s.listener.Addr().String()+}diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.gonew file mode 100644index 00000000..a375131c--- /dev/null+++ b/internal/controlplane/server_test.go@@ -0,0 +1,439 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package controlplane++import (+ "bytes"+ "context"+ "crypto/rand"+ "crypto/rsa"+ "encoding/base64"+ "encoding/json"+ "io"+ "math/big"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "reflect"+ "strings"+ "testing"+ "time"++ "github.com/golang-jwt/jwt/v5"+ "github.com/google/sam/api"+ "github.com/google/sam/internal/storage"+ "github.com/libp2p/go-libp2p/core/crypto"+ "github.com/libp2p/go-libp2p/core/peer"+ "google.golang.org/protobuf/proto"+)++func startCustomMockOIDC(t *testing.T) (string, func(claims map[string]interface{}) string) {+ t.Helper()+ privKey, err := rsa.GenerateKey(rand.Reader, 2048)+ if err != nil {+ t.Fatalf("failed to generate rsa key: %v", err)+ }++ mux := http.NewServeMux()+ srv := httptest.NewServer(mux)+ t.Cleanup(srv.Close)++ issuer := srv.URL++ mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "issuer": issuer,+ "jwks_uri": issuer + "/keys",+ })+ })++ mux.HandleFunc("/keys", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "keys": []map[string]interface{}{+ {+ "kty": "RSA",+ "alg": "RS256",+ "use": "sig",+ "kid": "mock-key",+ "n": base64.RawURLEncoding.EncodeToString(privKey.N.Bytes()),+ "e": base64.RawURLEncoding.EncodeToString(big.NewInt(int64(privKey.E)).Bytes()),+ },+ },+ })+ })++ mintToken := func(customClaims map[string]interface{}) string {+ claims := jwt.MapClaims{+ "iss": issuer,+ "aud": "sam-mesh-audience",+ "exp": time.Now().Add(time.Hour).Unix(),+ }+ for k, v := range customClaims {+ claims[k] = v+ }+ token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)+ token.Header["kid"] = "mock-key"+ jwtStr, err := token.SignedString(privKey)+ if err != nil {+ t.Fatalf("failed to sign jwt: %v", err)+ }+ return jwtStr+ }++ return issuer, mintToken+}++func setupTestServer(t *testing.T, oidcIssuer string) (*Server, storage.Store, string) {+ t.Helper()++ tempDir, err := os.MkdirTemp("", "sam-cp-test-*")+ if err != nil {+ t.Fatalf("failed to create temp dir: %v", err)+ }+ t.Cleanup(func() {+ _ = os.RemoveAll(tempDir)+ })++ dbPath := filepath.Join(tempDir, "control-plane.db")+ store, err := storage.NewSQLStore("sqlite", dbPath)+ if err != nil {+ t.Fatalf("failed to create store: %v", err)+ }++ opts := Options{+ ListenAddr: "127.0.0.1:0", // Auto-allocate port+ DriverName: "sqlite",+ DataSourceName: dbPath,+ OIDCIssuer: oidcIssuer,+ AllowedAudiences: []string{"sam-mesh-audience"},+ LeaseDuration: 10 * time.Second,+ KeyRotationInterval: 12 * time.Hour,+ KeyGracePeriod: 10 * time.Minute,+ InsecureSkipTLSVerify: true,+ BiscuitTimeout: 1 * time.Second,+ }++ srv, err := NewServer(opts, store)+ if err != nil {+ t.Fatalf("failed to create control plane server: %v", err)+ }++ if err := srv.Start(); err != nil {+ t.Fatalf("failed to start control plane server: %v", err)+ }++ serverAddr := srv.listener.Addr().String()++ return srv, store, "http://" + serverAddr+}++func TestControlPlaneBasic(t *testing.T) {+ issuer, _ := startCustomMockOIDC(t)+ srv, store, baseURL := setupTestServer(t, issuer)+ defer func() {+ _ = srv.Close()+ _ = store.Close()+ }()++ client := &http.Client{Timeout: 5 * time.Second}++ // 1. Test /info (no routers registered yet)+ resp, err := client.Get(baseURL + "/info")+ if err != nil {+ t.Fatalf("GET /info failed: %v", err)+ }+ if resp.StatusCode != http.StatusOK {+ t.Fatalf("unexpected GET /info status: %s", resp.Status)+ }+ body, err := io.ReadAll(resp.Body)+ _ = resp.Body.Close()+ if err != nil {+ t.Fatalf("failed to read /info body: %v", err)+ }++ var info api.HubInfoResponse+ if err := proto.Unmarshal(body, &info); err != nil {+ t.Fatalf("failed to unmarshal HubInfoResponse: %v", err)+ }+ if info.OidcIssuer != issuer || info.ClientId != "sam-mesh-audience" {+ t.Errorf("unexpected info response claims: %+v", &info)+ }+ if len(info.HubAddresses) != 0 {+ t.Errorf("expected 0 active routers, got %d", len(info.HubAddresses))+ }++ // 2. Test /keys+ resp, err = client.Get(baseURL + "/keys")+ if err != nil {+ t.Fatalf("GET /keys failed: %v", err)+ }+ if resp.StatusCode != http.StatusOK {+ t.Fatalf("unexpected GET /keys status: %s", resp.Status)+ }+ body, err = io.ReadAll(resp.Body)+ _ = resp.Body.Close()+ if err != nil {+ t.Fatalf("failed to read /keys body: %v", err)+ }++ var keys api.KeysResponse+ if err := proto.Unmarshal(body, &keys); err != nil {+ t.Fatalf("failed to unmarshal KeysResponse: %v", err)+ }+ if len(keys.PublicKeys) != 1 {+ t.Errorf("expected 1 valid public key, got %d", len(keys.PublicKeys))+ }+}++func TestNodeAndRouterRegistrationFlow(t *testing.T) {+ issuer, mintToken := startCustomMockOIDC(t)+ srv, store, baseURL := setupTestServer(t, issuer)+ defer func() {+ _ = srv.Close()+ _ = store.Close()+ }()++ ctx := context.Background()++ // 1. Setup policy configuration in the database+ policy := &api.PolicyConfig{+ Version: "v1alpha1",+ Bindings: []api.Binding{+ {Role: "router", Members: []string{"group:routers"}},+ {Role: "user-role", Members: []string{"group:users"}},+ },+ Roles: map[string]api.RolePolicy{+ "router": {+ AllowedServices: []string{"*"},+ AllowedTargets: []string{"*"},+ },+ "user-role": {+ AllowedServices: []string{"mcp:read"},+ },+ },+ }+ if err := store.SavePolicy(ctx, policy); err != nil {+ t.Fatalf("failed to seed policy: %v", err)+ }++ client := &http.Client{Timeout: 5 * time.Second}++ // 2. Enroll a Node+ privNode, _, 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"},+ })++ enrollNodeReq := &api.EnrollRequest{+ Jwt: nodeJWT,+ PeerId: nodePeer.String(),+ }+ reqData, _ := proto.Marshal(enrollNodeReq)++ resp, err := client.Post(baseURL+"/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)+ }+ if len(enrollNodeResp.BiscuitToken) == 0 {+ t.Fatalf("received empty biscuit token for node")+ }++ // 3. Enroll a Router+ privRouter, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ if err != nil {+ t.Fatal(err)+ }+ routerPeer, err := peer.IDFromPrivateKey(privRouter)+ if err != nil {+ t.Fatal(err)+ }++ routerJWT := mintToken(map[string]interface{}{+ "sub": "router-host-1",+ "groups": []string{"routers"},+ })++ enrollRouterReq := &api.EnrollRequest{+ Jwt: routerJWT,+ PeerId: routerPeer.String(),+ }+ reqData, _ = proto.Marshal(enrollRouterReq)++ resp, err = client.Post(baseURL+"/register", "application/x-protobuf", bytes.NewReader(reqData))+ if err != nil {+ t.Fatalf("router /register failed: %v", err)+ }+ if resp.StatusCode != http.StatusOK {+ t.Fatalf("router /register status failure: %s", resp.Status)+ }+ body, _ = io.ReadAll(resp.Body)+ _ = resp.Body.Close()++ var enrollRouterResp api.EnrollResponse+ if err := proto.Unmarshal(body, &enrollRouterResp); err != nil {+ t.Fatalf("failed to unmarshal EnrollResponse: %v", err)+ }+ if len(enrollRouterResp.BiscuitToken) == 0 {+ t.Fatalf("received empty biscuit token for router")+ }++ // 4. Register Router Lease+ routerAddresses := []string{"/ip4/127.0.0.1/tcp/5001/p2p/" + routerPeer.String()}+ leaseReq := &api.RouterLeaseRequest{+ PeerId: routerPeer.String(),+ Addresses: routerAddresses,+ Biscuit: enrollRouterResp.BiscuitToken,+ }+ reqData, _ = proto.Marshal(leaseReq)++ resp, err = client.Post(baseURL+"/routers/lease", "application/x-protobuf", bytes.NewReader(reqData))+ if err != nil {+ t.Fatalf("POST /routers/lease failed: %v", err)+ }+ if resp.StatusCode != http.StatusOK {+ body, _ := io.ReadAll(resp.Body)+ t.Fatalf("POST /routers/lease status failure: %s (body: %s)", resp.Status, string(body))+ }+ body, _ = io.ReadAll(resp.Body)+ _ = resp.Body.Close()++ var leaseResp api.RouterLeaseResponse+ if err := proto.Unmarshal(body, &leaseResp); err != nil {+ t.Fatalf("failed to unmarshal RouterLeaseResponse: %v", err)+ }+ if !leaseResp.Success {+ t.Errorf("lease registration was not successful: %s", leaseResp.Error)+ }++ // 5. Query /info again, checking if the active router address list is now populated+ resp, err = client.Get(baseURL + "/info")+ if err != nil {+ t.Fatalf("GET /info failed: %v", err)+ }+ body, _ = io.ReadAll(resp.Body)+ _ = resp.Body.Close()++ var info api.HubInfoResponse+ _ = proto.Unmarshal(body, &info)++ if !reflect.DeepEqual(info.HubAddresses, routerAddresses) {+ t.Errorf("expected active routers address list %v, got %v", routerAddresses, info.HubAddresses)+ }++ // 6. Rogue Node tries to lease as a router (lacks 'router' role)+ rogueLeaseReq := &api.RouterLeaseRequest{+ PeerId: nodePeer.String(),+ Addresses: []string{"/ip4/127.0.0.1/tcp/6001/p2p/" + nodePeer.String()},+ Biscuit: enrollNodeResp.BiscuitToken, // Node biscuit doesn't have router role+ }+ reqData, _ = proto.Marshal(rogueLeaseReq)++ resp, err = client.Post(baseURL+"/routers/lease", "application/x-protobuf", bytes.NewReader(reqData))+ if err != nil {+ t.Fatalf("POST rogue /routers/lease failed: %v", err)+ }+ if resp.StatusCode != http.StatusForbidden {+ t.Errorf("expectedStatusForbidden (403) for rogue router lease, got: %s", resp.Status)+ }+}++func TestPoliciesConfigurationREST(t *testing.T) {+ issuer, _ := startCustomMockOIDC(t)+ srv, store, baseURL := setupTestServer(t, issuer)+ defer func() {+ _ = srv.Close()+ _ = store.Close()+ }()++ client := &http.Client{Timeout: 5 * time.Second}++ // 1. Get policies (should return 404 since none exists)+ resp, err := client.Get(baseURL + "/policies")+ if err != nil {+ t.Fatalf("GET /policies failed: %v", err)+ }+ if resp.StatusCode != http.StatusNotFound {+ t.Errorf("expected 404 for missing policy, got %d", resp.StatusCode)+ }+ _ = resp.Body.Close()++ // 2. Put policies+ newPolicyYaml := `+version: v1alpha1+bindings:+ - role: dev+ members:+ - group:developers+roles:+ dev:+ allowed_services:+ - mcp://git+`+ updateReq := &api.PolicyConfigUpdateRequest{YamlContent: newPolicyYaml}+ reqData, _ := proto.Marshal(updateReq)++ resp, err = client.Post(baseURL+"/policies", "application/x-protobuf", bytes.NewReader(reqData))+ if err != nil {+ t.Fatalf("POST /policies failed: %v", err)+ }+ if resp.StatusCode != http.StatusOK {+ body, _ := io.ReadAll(resp.Body)+ t.Fatalf("unexpected POST /policies status: %s (body: %s)", resp.Status, string(body))+ }+ _ = resp.Body.Close()++ // 3. Get policies again (verify content)+ resp, err = client.Get(baseURL + "/policies")+ if err != nil {+ t.Fatalf("GET /policies failed: %v", err)+ }+ if resp.StatusCode != http.StatusOK {+ t.Fatalf("GET /policies status failed: %s", resp.Status)+ }+ body, _ := io.ReadAll(resp.Body)+ _ = resp.Body.Close()++ var getResp api.PolicyConfigGetResponse+ if err := proto.Unmarshal(body, &getResp); err != nil {+ t.Fatalf("failed to unmarshal PolicyConfigGetResponse: %v", err)+ }++ if !strings.Contains(getResp.YamlContent, "role: dev") || !strings.Contains(getResp.YamlContent, "group:developers") {+ t.Errorf("returned policy content mismatch: %s", getResp.YamlContent)+ }+}diff --git a/internal/hub/admin.go b/internal/hub/admin.godeleted file mode 100644index cead2e68..00000000--- a/internal/hub/admin.go+++ /dev/null@@ -1,67 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "context"- "fmt"- "net/http"- "time"-- "github.com/google/sam/api"- golog "github.com/ipfs/go-log/v2"-)--var adminLogger = golog.Logger("sam-hub-admin")--// HubAdmin defines the interface needed by admin handlers.-type HubAdmin interface {- PublishEvent(ctx context.Context, event *api.MeshEvent) error-}--// HandleBan returns a handler for the /admin/ban endpoint.-func HandleBan(admin HubAdmin) http.HandlerFunc {- return func(w http.ResponseWriter, r *http.Request) {- if r.Method != http.MethodPost {- http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)- return- }- peerID := r.URL.Query().Get("peer")- adminLogger.Infof("[Admin API] Received ban request for peer: %s", peerID)-- if peerID == "" {- http.Error(w, "Missing peer parameter", http.StatusBadRequest)- return- }-- // Create MeshEvent_BANNED- event := &api.MeshEvent{- Type: api.MeshEvent_BANNED,- PeerId: peerID,- Timestamp: time.Now().UnixMilli(),- }-- adminLogger.Infof("[Admin API] Publishing ban event for peer: %s", peerID)- if err := admin.PublishEvent(r.Context(), event); err != nil {- adminLogger.Errorf("[Admin API] Failed to publish event: %v", err)- http.Error(w, fmt.Sprintf("Failed to publish event: %v", err), http.StatusInternalServerError)- return- }-- adminLogger.Infof("[Admin API] Successfully published ban event for peer: %s", peerID)- w.WriteHeader(http.StatusOK)- _, _ = fmt.Fprintf(w, "Published BANNED event for peer %s", peerID)- }-}diff --git a/internal/hub/admin_test.go b/internal/hub/admin_test.godeleted file mode 100644index 325dc02b..00000000--- a/internal/hub/admin_test.go+++ /dev/null@@ -1,90 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "context"- "net/http"- "net/http/httptest"- "testing"-- "github.com/google/sam/api"-)--type mockHubAdmin struct {- publishedEvents []*api.MeshEvent- publishErr error-}--func (m *mockHubAdmin) PublishEvent(ctx context.Context, event *api.MeshEvent) error {- if m.publishErr != nil {- return m.publishErr- }- m.publishedEvents = append(m.publishedEvents, event)- return nil-}--func TestHandleBan(t *testing.T) {- mockAdmin := &mockHubAdmin{}- handler := HandleBan(mockAdmin)-- // Test valid request- req, err := http.NewRequest("POST", "/admin/ban?peer=test-peer-id", nil)- if err != nil {- t.Fatal(err)- }- rr := httptest.NewRecorder()- handler.ServeHTTP(rr, req)-- if status := rr.Code; status != http.StatusOK {- t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)- }-- if len(mockAdmin.publishedEvents) != 1 {- t.Errorf("expected 1 published event, got %d", len(mockAdmin.publishedEvents))- }-- event := mockAdmin.publishedEvents[0]- if event.Type != api.MeshEvent_BANNED {- t.Errorf("expected BANNED event, got %v", event.Type)- }- if event.PeerId != "test-peer-id" {- t.Errorf("expected peer ID test-peer-id, got %s", event.PeerId)- }-- // Test invalid method- req, err = http.NewRequest("GET", "/admin/ban?peer=test-peer-id", nil)- if err != nil {- t.Fatal(err)- }- rr = httptest.NewRecorder()- handler.ServeHTTP(rr, req)-- if status := rr.Code; status != http.StatusMethodNotAllowed {- t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusMethodNotAllowed)- }-- // Test missing parameter- req, err = http.NewRequest("POST", "/admin/ban", nil)- if err != nil {- t.Fatal(err)- }- rr = httptest.NewRecorder()- handler.ServeHTTP(rr, req)-- if status := rr.Code; status != http.StatusBadRequest {- t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)- }-}diff --git a/internal/hub/biscuit_benchmark_test.go b/internal/hub/biscuit_benchmark_test.godeleted file mode 100644index 13cddc35..00000000--- a/internal/hub/biscuit_benchmark_test.go+++ /dev/null@@ -1,131 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "path/filepath"- "testing"- "time"-- "github.com/coreos/go-oidc/v3/oidc"- "github.com/golang-jwt/jwt/v5"- "github.com/google/sam/api"- "github.com/libp2p/go-libp2p/core/crypto"- "github.com/libp2p/go-libp2p/core/peer"-)--func BenchmarkMintBiscuitToken(b *testing.B) {- dir := b.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)- if err != nil {- b.Fatal(err)- }- defer func() { _ = kr.Close() }()-- hub := &Hub{- KeyRing: kr,- Policy: &api.PolicyConfig{- Bindings: []api.Binding{- {Role: "developer-role", Members: []string{"group:engineering"}},- },- Roles: map[string]api.RolePolicy{- "developer-role": {- AllowedServices: []string{"mcp:git-helper", "mcp:mcp-server-2"},- AllowedTargets: []string{"target-1", "target-2"},- CustomDatalog: []string{- "department(\"analytics\");",- },- },- },- },- }-- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)- if err != nil {- b.Fatal(err)- }- dummyPeer, err := peer.IDFromPrivateKey(priv)- if err != nil {- b.Fatal(err)- }-- token := &oidc.IDToken{- Expiry: time.Now().Add(1 * time.Hour),- }-- claims := jwt.MapClaims{- "sub": "user-12345",- "email": "[email protected]",- "groups": []any{"beta-testers", "engineering"},- }-- b.ResetTimer()- for i := 0; i < b.N; i++ {- _, err := hub.mintBiscuitToken(claims, token, dummyPeer)- if err != nil {- b.Fatal(err)- }- }-}--func BenchmarkVerifyBiscuit(b *testing.B) {- dir := b.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)- if err != nil {- b.Fatal(err)- }- defer func() { _ = kr.Close() }()-- hub := &Hub{- KeyRing: kr,- Policy: &api.PolicyConfig{},- BiscuitTimeout: 500 * time.Millisecond,- }-- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)- if err != nil {- b.Fatal(err)- }- dummyPeer, err := peer.IDFromPrivateKey(priv)- if err != nil {- b.Fatal(err)- }-- token := &oidc.IDToken{- Expiry: time.Now().Add(1 * time.Hour),- }-- claims := jwt.MapClaims{- "sub": "user-123",- "roles": []any{"admin"},- }-- biscuitData, err := hub.mintBiscuitToken(claims, token, dummyPeer)- if err != nil {- b.Fatal(err)- }-- b.ResetTimer()- for i := 0; i < b.N; i++ {- _, err := hub.verifyBiscuit(biscuitData, dummyPeer)- if err != nil {- b.Fatal(err)- }- }-}diff --git a/internal/hub/config_test.go b/internal/hub/config_test.godeleted file mode 100644index 4cd423aa..00000000--- a/internal/hub/config_test.go+++ /dev/null@@ -1,361 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "os"- "path/filepath"- "testing"-- "github.com/google/sam/api"-)--func TestLoadPolicyConfig(t *testing.T) {- tests := []struct {- name string- yamlContent string- nonexistent bool- wantErr bool- verify func(t *testing.T, config *api.PolicyConfig)- }{- {- name: "Valid comprehensive config from docs",- yamlContent: `-version: "v1alpha1"--# Bindings map OIDC identities (sub/user, email, groups) to SAM Roles.-# Note: Kubernetes projected service account tokens do not carry 'groups' claims,-# so they must be bound explicitly using their 'user' claim format.-bindings:- # 1. Global Admins (Infrastructure SAs, Lead Architects)- - members: ["user:system:serviceaccount:sam-mesh:admin-sa"]- role: "admin"- - members: ["group:infrastructure-leads"]- role: "admin"-- # 2. Software Developers- - members: ["group:software-engineering-team"]- role: "developer"-- # 3. Data Scientists & AI Engineers- - members: ["group:data-science-team"]- role: "data-scientist"-- # 4. Contractors / Read-Only Audits- - members: ["email:[email protected]"]- role: "auditor"--# Roles define the allowed destinations (allowed_targets) and tools (allowed_services)-roles:- # Admins have full, unrestricted access to the entire mesh- admin:- allowed_targets:- - "*"- allowed_services:- - "*"-- # Developers can call development tools on dev nodes- developer:- allowed_targets:- - "group:dev-nodes" # Can only call nodes in the 'dev-nodes' target group- allowed_services:- - "mcp://code-reviewer" # Can call the code reviewer tool- - "mcp://git-helper" # Can call git helper tools- - "mcp://build-runner.*" # Wildcard: matches any build runner sub-service (e.g. build-runner.go)-- # Data Scientists can call database tools and all AI inference endpoints- data-scientist:- allowed_targets:- - "group:data-nodes" # Can call nodes in the 'data-nodes' target group- - "node:12D3KooWSpecialNode" # Can call a specific high-compute node directly- allowed_services:- - "mcp://db-reader" # Can query databases- - "inference://*" # Wildcard: can access any LLM inference service-- # Auditors can only query metadata catalogs and cannot call operational tools- auditor:- allowed_targets:- - "*"- allowed_services:- - "system://sam.catalog" # Strictly limited to tool discovery/metadata--`,- wantErr: false,- },- {- name: "Valid config with exact services",- yamlContent: `-version: "v1alpha1"-roles:- data-scientist:- allowed_targets: ["node:db-agent.data-mesh"]- allowed_services: ["system://query-database", "mcp://db-agent"]- custom_datalog:- - 'department("analytics");'-`,- wantErr: false,- verify: func(t *testing.T, config *api.PolicyConfig) {- if config.Version != "v1alpha1" {- t.Errorf("expected version v1alpha1, got %s", config.Version)- }- role, ok := config.Roles["data-scientist"]- if !ok {- t.Fatal("expected role data-scientist to exist")- }- if len(role.AllowedTargets) != 1 || role.AllowedTargets[0] != "node:db-agent.data-mesh" {- t.Errorf("unexpected allowed targets: %v", role.AllowedTargets)- }- if len(role.AllowedServices) != 2 || role.AllowedServices[0] != "system://query-database" || role.AllowedServices[1] != "mcp://db-agent" {- t.Errorf("unexpected allowed services: %v", role.AllowedServices)- }- if len(role.CustomDatalog) != 1 || role.CustomDatalog[0] != `department("analytics");` {- t.Errorf("unexpected custom datalog: %v", role.CustomDatalog)- }- },- },- {- name: "Invalid YAML syntax",- yamlContent: `-version: "v1alpha1"-roles:- data-scientist:- allowed_targets: [missing closing bracket-`,- wantErr: true,- },- {- name: "Nonexistent file loads empty config",- nonexistent: true,- wantErr: false,- verify: func(t *testing.T, config *api.PolicyConfig) {- if config == nil {- t.Fatal("expected non-nil config for missing file")- }- if len(config.Roles) != 0 {- t.Errorf("expected empty roles, got %d", len(config.Roles))- }- },- },- {- name: "Invalid custom datalog fact syntax",- yamlContent: `-version: "v1alpha1"-roles:- data-scientist:- custom_datalog:- - 'invalid fact syntax'-`,- wantErr: true,- },- {- name: "Valid group binding",- yamlContent: `-version: "v1alpha1"-bindings:- - members: ["group:system:serviceaccounts:sam-canary-bananas"]- role: "mesh-member"-roles:- mesh-member:- allowed_services: ["mcp://1.0.0"]-`,- wantErr: false,- verify: func(t *testing.T, config *api.PolicyConfig) {- if len(config.Bindings) != 1 {- t.Errorf("expected 1 binding, got %d", len(config.Bindings))- }- if len(config.Bindings[0].Members) == 0 || config.Bindings[0].Members[0] != "group:system:serviceaccounts:sam-canary-bananas" || config.Bindings[0].Role != "mesh-member" {- t.Errorf("unexpected binding values: %+v", config.Bindings[0])- }- },- },- {- name: "Invalid binding mapping to nonexistent role",- yamlContent: `-version: "v1alpha1"-bindings:- - members: ["group:system:serviceaccounts:sam-canary-bananas"]- role: "non-existent-role"-roles:- mesh-member:- allowed_services: ["mcp://1.0.0"]-`,- wantErr: true,- },- {- name: "Valid user binding",- yamlContent: `-version: "v1alpha1"-bindings:- - members: ["user:system:serviceaccount:sam-canary:sam-node-sa"]- role: "mesh-member"-roles:- mesh-member:- allowed_services: ["mcp://1.0.0"]-`,- wantErr: false,- verify: func(t *testing.T, config *api.PolicyConfig) {- if len(config.Bindings) != 1 {- t.Errorf("expected 1 binding, got %d", len(config.Bindings))- }- if len(config.Bindings[0].Members) == 0 || config.Bindings[0].Members[0] != "user:system:serviceaccount:sam-canary:sam-node-sa" || config.Bindings[0].Role != "mesh-member" {- t.Errorf("unexpected binding values: %+v", config.Bindings[0])- }- },- },- {- name: "Invalid binding with no members",- yamlContent: `-version: "v1alpha1"-bindings:- - role: "mesh-member"-roles:- mesh-member:- allowed_services: ["mcp://1.0.0"]-`,- wantErr: true,- },- {- name: "Invalid binding with invalid prefix",- yamlContent: `-version: "v1alpha1"-bindings:- - members: ["invalid-prefix:some-user"]- role: "mesh-member"-roles:- mesh-member:- allowed_services: ["mcp://1.0.0"]-`,- wantErr: true,- },- {- name: "Valid binding with all valid member prefixes",- yamlContent: `-version: "v1alpha1"-bindings:- - members: ["user:bob", "group:eng", "email:[email protected]", "node:12D3KooW", "system:authenticated"]- role: "mesh-member"-roles:- mesh-member:- allowed_services: ["mcp://1.0.0"]-`,- wantErr: false,- verify: func(t *testing.T, config *api.PolicyConfig) {- if len(config.Bindings) != 1 {- t.Errorf("expected 1 binding, got %d", len(config.Bindings))- }- if len(config.Bindings[0].Members) != 5 {- t.Errorf("expected 5 members, got %d", len(config.Bindings[0].Members))- }- },- },- {- name: "Invalid binding missing colon in member string",- yamlContent: `-version: "v1alpha1"-bindings:- - members: ["userbob"]- role: "mesh-member"-roles:- mesh-member:- allowed_services: ["mcp://1.0.0"]-`,- wantErr: true,- },- {- name: "Valid wildcard - global wildcard",- yamlContent: `-version: "v1alpha1"-roles:- admin:- allowed_services: ["*"]-`,- wantErr: false,- },- {- name: "Valid wildcard - type wildcard",- yamlContent: `-version: "v1alpha1"-roles:- admin:- allowed_services: ["mcp://*"]-`,- wantErr: false,- },- {- name: "Valid wildcard - domain prefix wildcard",- yamlContent: `-version: "v1alpha1"-roles:- admin:- allowed_services: ["mcp://*.service.local"]-`,- wantErr: false,- },- {- name: "Valid wildcard - domain suffix wildcard",- yamlContent: `-version: "v1alpha1"-roles:- admin:- allowed_services: ["mcp://service.*"]-`,- wantErr: false,- },- {- name: "Invalid wildcard - arbitrary partial prefix matching",- yamlContent: `-version: "v1alpha1"-roles:- admin:- allowed_services: ["mcp://dev-*"]-`,- wantErr: true,- },- {- name: "Invalid wildcard - arbitrary partial suffix matching",- yamlContent: `-version: "v1alpha1"-roles:- admin:- allowed_services: ["mcp://*-prod"]-`,- wantErr: true,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- var path string- if tt.nonexistent {- path = filepath.Join(t.TempDir(), "nonexistent.yaml")- } else {- dir := t.TempDir()- path = filepath.Join(dir, "policy.yaml")- if err := os.WriteFile(path, []byte(tt.yamlContent), 0644); err != nil {- t.Fatalf("failed to write temp policy file: %v", err)- }- }-- config, err := LoadPolicyConfig(path)- if (err != nil) != tt.wantErr {- t.Fatalf("LoadPolicyConfig() error = %v, wantErr %v", err, tt.wantErr)- }-- if !tt.wantErr && tt.verify != nil {- tt.verify(t, config)- }- })- }-}diff --git a/internal/hub/hub.go b/internal/hub/hub.godeleted file mode 100644index 3ce24596..00000000--- a/internal/hub/hub.go+++ /dev/null@@ -1,629 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "context"- "crypto/ed25519"- "crypto/tls"- "encoding/hex"- "errors"- "fmt"- "io"- "net"- "net/http"- "strings"- "sync"- "sync/atomic"- "time"-- "github.com/biscuit-auth/biscuit-go/v2"- "github.com/coreos/go-oidc/v3/oidc"- "github.com/golang-jwt/jwt/v5"- "github.com/google/sam/api"- golog "github.com/ipfs/go-log/v2"- "github.com/libp2p/go-libp2p"- dht "github.com/libp2p/go-libp2p-kad-dht"- pubsub "github.com/libp2p/go-libp2p-pubsub"- "github.com/libp2p/go-libp2p/core/host"- "github.com/libp2p/go-libp2p/core/network"- "github.com/libp2p/go-libp2p/core/peer"- "github.com/libp2p/go-libp2p/p2p/net/connmgr"- "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"- libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"- "github.com/libp2p/go-msgio"- "github.com/multiformats/go-multiaddr"- madns "github.com/multiformats/go-multiaddr-dns"- "golang.org/x/time/rate"- "google.golang.org/protobuf/proto"-)--const (- GracePeriod = 60 * time.Second-- // Rate limiting defaults- EnrollRateLimit = 10- EnrollBurst = 20-- // ConnManager limits- LowWaterMark = 100- HighWaterMark = 400- ConnGracePeriod = 1 * time.Minute-- // Timeouts- JWTVerificationTimeout = 10 * time.Second-)--type relayACL struct {- hub *Hub-}--func (a *relayACL) AllowReserve(p peer.ID, addr multiaddr.Multiaddr) bool {- _, ok := a.hub.authenticatedPeers.Load(p)- if !ok {- if a.hub.logger != nil {- a.hub.logger.Errorf("[Relay] Rejecting reservation for %s: not authenticated", p)- }- } else {- if a.hub.logger != nil {- a.hub.logger.Infof("[Relay] Allowing reservation for %s", p)- }- }- return ok-}--func (a *relayACL) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest peer.ID) bool {- _, ok := a.hub.authenticatedPeers.Load(dest)- if !ok {- if a.hub.logger != nil {- a.hub.logger.Errorf("[Relay] Rejecting connect from %s to %s: dest not authenticated", src, dest)- }- } else {- if a.hub.logger != nil {- a.hub.logger.Infof("[Relay] Allowing connect from %s to %s", src, dest)- }- }- return ok-}--// Hub handles identity bridging and network discovery-type Hub struct {- config Options- Host host.Host- DHT *dht.IpfsDHT- Providers map[string]*oidc.Provider- KeyRing *KeyRing- MeshID string- PubSub *pubsub.PubSub- EventTopic *pubsub.Topic- Policy *api.PolicyConfig- limiter *rate.Limiter- ExternalAddrs []string- AllowedAudiences []string- AllowLoopback bool- BiscuitTimeout time.Duration- authenticatedPeers sync.Map- logger *golog.ZapEventLogger- oidcIssuer string- keyRotationInterval time.Duration- keyGracePeriod time.Duration- insecureSkipTLSVerify bool- isReady atomic.Bool-}--// NewHub initializes configuration and KeyRing without starting background tasks or network interfaces.-func NewHub(config Options) (*Hub, error) {- config.Default()- if err := config.Validate(); err != nil {- return nil, err- }-- var initialSeed []byte- if config.BiscuitHex != "" {- var err error- initialSeed, err = hex.DecodeString(strings.TrimSpace(config.BiscuitHex))- if err != nil {- return nil, fmt.Errorf("failed to decode key flag: %w", err)- }- }- kr, err := NewKeyRing(config.KeysDBPath, config.KeyGracePeriod, initialSeed)- if err != nil {- return nil, fmt.Errorf("failed to create keyring: %w", err)- }-- hub := &Hub{- config: config,- KeyRing: kr,- MeshID: config.MeshName,- Policy: config.Policy,- limiter: rate.NewLimiter(rate.Limit(EnrollRateLimit), EnrollBurst),- AllowedAudiences: config.AllowedAudiences,- AllowLoopback: config.AllowLoopback,- logger: golog.Logger("sam-hub"),- oidcIssuer: config.OIDCIssuer,- keyRotationInterval: config.KeyRotationInterval,- keyGracePeriod: config.KeyGracePeriod,- insecureSkipTLSVerify: config.InsecureSkipTLSVerify,- }-- return hub, nil-}--// Start initializes the libp2p host, DHT, PubSub, OIDC providers, and starts runtime components.-func (h *Hub) Start(ctx context.Context) error {- // Connection Manager for DoS protection- cm, err := connmgr.NewConnManager(LowWaterMark, HighWaterMark, connmgr.WithGracePeriod(ConnGracePeriod))- if err != nil {- return fmt.Errorf("failed to create connection manager: %w", err)- }-- p2pOpts := []libp2p.Option{- libp2p.DefaultTransports,- libp2p.ListenAddrStrings(h.config.ListenAddrs...),- // FIPS compliant Security- libp2p.Security(libp2ptls.ID, libp2ptls.New),- libp2p.ConnectionManager(cm),- libp2p.EnableAutoNATv2(),- libp2p.EnableNATService(),- libp2p.AddrsFactory(func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {- if h.config.AllowLoopback {- return addrs- }- var filtered []multiaddr.Multiaddr- for _, addr := range addrs {- if !isLoopbackOrLinkLocal(addr) {- filtered = append(filtered, addr)- }- }- return filtered- }),- }-- hostNode, err := libp2p.New(p2pOpts...)- if err != nil {- return err- }- h.Host = hostNode-- kadDHT, err := dht.New(ctx, hostNode, dht.Mode(dht.ModeServer), dht.ProtocolPrefix("/sam"))- if err != nil {- return err- }- h.DHT = kadDHT-- if err = kadDHT.Bootstrap(ctx); err != nil {- return err- }-- issuers := strings.Split(h.config.OIDCIssuer, ",")- providers := make(map[string]*oidc.Provider)- for _, iss := range issuers {- iss = strings.TrimSpace(iss)- if iss == "" {- continue- }- tr := http.DefaultTransport.(*http.Transport).Clone()- tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: h.config.InsecureSkipTLSVerify}- client := &http.Client{- Timeout: 30 * time.Second,- Transport: tr,- }- providerCtx := oidc.ClientContext(ctx, client)- provider, err := oidc.NewProvider(providerCtx, iss)- if err != nil {- return fmt.Errorf("failed to create provider for %s: %w", iss, err)- }- providers[iss] = provider- }- h.Providers = providers-- var filteredExternal []string- for _, addr := range h.config.ExternalMultiaddrs {- if !strings.HasPrefix(addr, "http://") && !strings.HasPrefix(addr, "https://") {- filteredExternal = append(filteredExternal, addr)- }- }- h.ExternalAddrs = filteredExternal-- ps, err := pubsub.NewGossipSub(ctx, hostNode)- if err != nil {- return err- }- h.PubSub = ps-- topic, err := ps.Join(api.GossipEvents)- if err != nil {- return err- }- h.EventTopic = topic-- _, err = relay.New(hostNode, relay.WithACL(&relayACL{hub: h}))- if err != nil {- return err- }-- hostNode.SetStreamHandler(api.AuthProtocolID, h.HandleAuthHandshake)-- hostNode.Network().Notify(&network.NotifyBundle{- DisconnectedF: func(n network.Network, c network.Conn) {- p := c.RemotePeer()- if len(hostNode.Network().ConnsToPeer(p)) == 0 {- h.authenticatedPeers.Delete(p)- }- },- })-- h.logger.Infof("[OIDC] Trusted issuers: %s", h.config.OIDCIssuer)-- // Start key rotation if enabled- h.StartRotation(ctx)-- // Start bootstrap federation if enabled- if len(h.config.ExternalMultiaddrs) > 0 {- go h.StartBootstrapFederation(ctx, h.config.ExternalMultiaddrs)- }-- return nil-}--// Ready returns the ready status of the Hub.-func (h *Hub) Ready() bool {- return h.isReady.Load()-}--// SetReady sets the ready status of the Hub.-func (h *Hub) SetReady(ready bool) {- h.isReady.Store(ready)-}--// Close closes the underlying p2p host and keyring db.-func (h *Hub) Close() error {- var errs []error- if h.DHT != nil {- if err := h.DHT.Close(); err != nil {- errs = append(errs, err)- }- }- if h.Host != nil {- if err := h.Host.Close(); err != nil {- errs = append(errs, err)- }- }- if h.KeyRing != nil {- if err := h.KeyRing.Close(); err != nil {- errs = append(errs, err)- }- }- return errors.Join(errs...)-}--func (h *Hub) parseAndVerifyJWT(ctx context.Context, jwtStr string, allowedAudiences []string) (jwt.MapClaims, *oidc.IDToken, error) {- jwtParser := jwt.Parser{}- jwtToken, _, err := jwtParser.ParseUnverified(jwtStr, jwt.MapClaims{})- if err != nil {- return nil, nil, fmt.Errorf("failed to parse JWT: %w", err)- }-- // 1. Defend against downgrade attacks immediately- alg, ok := jwtToken.Header["alg"].(string)- if !ok || alg == "" || strings.ToLower(alg) == "none" {- return nil, nil, fmt.Errorf("invalid or missing alg header")- }-- claims, ok := jwtToken.Claims.(jwt.MapClaims)- if !ok {- return nil, nil, fmt.Errorf("invalid JWT claims")- }- iss, _ := claims["iss"].(string)-- // 2. Extract the audience- var aud string- switch a := claims["aud"].(type) {- case string:- aud = a- case []any:- if len(a) > 0 {- aud, _ = a[0].(string)- }- }-- if aud == "" {- return nil, nil, fmt.Errorf("missing aud claim")- }-- // 3. Verify the audience matches one of your expected tenants/platforms- validAudience := false- for _, allowed := range allowedAudiences {- if aud == allowed {- validAudience = true- break- }- }- if !validAudience {- return nil, nil, fmt.Errorf("untrusted audience: %s", aud)- }-- // 4. Route to the correct provider- provider, ok := h.Providers[iss]- if !ok {- return nil, nil, fmt.Errorf("unknown issuer: %s", iss)- }-- // 5. Verify cryptographic signature, bypassing the strict single-clientID check- // because we already validated the audience against our allowed list above.- verifier := provider.Verifier(&oidc.Config{- SkipClientIDCheck: true,- })-- token, err := verifier.Verify(ctx, jwtStr)- if err != nil {- return nil, nil, fmt.Errorf("JWT validation failed: %w", err)- }-- return claims, token, nil-}--// StartRotation periodically checks for keys to rotate.-func (h *Hub) StartRotation(ctx context.Context) {- if h.keyRotationInterval <= 0 {- return- }- go func() {- ticker := time.NewTicker(h.keyRotationInterval)- defer ticker.Stop()- for {- select {- case <-ticker.C:- h.logger.Infow("Rotating keys")- newPub, newPriv, oldPriv, err := h.KeyRing.PrepareRotation()- if err != nil {- h.logger.Errorw("Failed to prepare key rotation", "error", err)- continue- }- samHubKeyRotationsTotal.Inc()-- // Broadcast event- event := &api.MeshEvent{- Type: api.MeshEvent_KEY_ROTATION,- Timestamp: time.Now().UnixMilli(),- NewPublicKey: newPub,- }-- // Sign with the OLD key so nodes can verify it!- event.Signature = nil- data, err := proto.Marshal(event)- if err != nil {- h.logger.Errorw("Failed to marshal key rotation event", "error", err)- continue- }- event.Signature = ed25519.Sign(oldPriv, data)-- eventData, err := proto.Marshal(event)- if err != nil {- h.logger.Errorw("Failed to marshal key rotation event", "error", err)- continue- }- if err := h.EventTopic.Publish(ctx, eventData); err != nil {- h.logger.Errorw("Failed to publish key rotation event", "error", err)- } else {- h.logger.Infow("Broadcasted new public key", "public_key", hex.EncodeToString(newPub))- samHubMeshEventsTotal.WithLabelValues("KEY_ROTATION").Inc()-- // Promote the new key after successful broadcast- if err := h.KeyRing.CommitRotation(newPub, newPriv, h.keyGracePeriod); err != nil {- h.logger.Errorw("Failed to commit key rotation", "error", err)- } else {- h.logger.Infow("Committed new key to keyring")- }- }- case <-ctx.Done():- return- }- }- }()-}--func (h *Hub) signEvent(event *api.MeshEvent) error {- event.Signature = nil- data, err := proto.Marshal(event)- if err != nil {- return err- }- event.Signature = ed25519.Sign(h.KeyRing.GetCurrentKey(), data)- return nil-}--// PublishEvent signs and broadcasts a mesh event.-func (h *Hub) PublishEvent(ctx context.Context, event *api.MeshEvent) error {- if err := h.signEvent(event); err != nil {- return err- }- data, err := proto.Marshal(event)- if err != nil {- return err- }- return h.EventTopic.Publish(ctx, data)-}--func isLoopbackOrLinkLocal(addr multiaddr.Multiaddr) bool {- for _, proto := range addr.Protocols() {- if proto.Code == multiaddr.P_IP4 || proto.Code == multiaddr.P_IP6 {- value, err := addr.ValueForProtocol(proto.Code)- if err == nil {- ip := net.ParseIP(value)- if ip != nil {- if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {- return true- }- }- }- }- }- return false-}--// HandleAuthHandshake processes the inbound auth protocol handshake.-func (h *Hub) HandleAuthHandshake(s network.Stream) {- defer func() {- if err := s.Close(); err != nil {- h.logger.Debugf("[AuthN] Failed to close auth stream: %v", err)- }- }()- remotePeer := s.Conn().RemotePeer()-- reader := msgio.NewVarintReaderSize(s, 1024*64)- msg, err := reader.ReadMsg()- if err != nil {- h.logger.Errorf("[AuthN] Failed to read handshake from %s: %v", remotePeer, err)- return- }- defer reader.ReleaseMsg(msg)-- var exchange api.AuthFrame- if err := proto.Unmarshal(msg, &exchange); err != nil {- h.logger.Warnf("[AuthN] Invalid protobuf from %s", remotePeer)- return- }-- b, err := h.verifyBiscuit(exchange.Biscuit, remotePeer)- if err != nil {- h.logger.Warnf("[AuthN] Authorization failed for %s: %v", remotePeer, err)- return- }-- // Enforce hardware binding- boundFact := biscuit.Fact{Predicate: biscuit.Predicate{- Name: "node",- IDs: []biscuit.Term{biscuit.String(remotePeer.String())},- }}- if _, err := b.GetBlockID(boundFact); err != nil {- h.logger.Warnf("[AuthN] Token is not bound to peer %s", remotePeer)- return- }-- h.authenticatedPeers.Store(remotePeer, true)- h.logger.Infof("[AuthN] Successfully authenticated peer %s", remotePeer)-- writer := msgio.NewVarintWriter(s)- resp := &api.AuthResponse{Success: true}- respBytes, _ := proto.Marshal(resp)- if err := writer.WriteMsg(respBytes); err != nil {- h.logger.Errorf("[AuthN] Failed to write ACK to %s: %v", remotePeer, err)- }-}--// StartBootstrapFederation handles background bootstrap federation.-func (h *Hub) StartBootstrapFederation(ctx context.Context, peers []string) {- if len(peers) == 0 {- return- }- ticker := time.NewTicker(30 * time.Second)- defer ticker.Stop()-- h.connectBootstrap(ctx, peers)-- for {- select {- case <-ctx.Done():- return- case <-ticker.C:- h.connectBootstrap(ctx, peers)- }- }-}--func (h *Hub) connectBootstrap(ctx context.Context, peers []string) {- for _, peerStr := range peers {- if strings.HasPrefix(peerStr, "http://") || strings.HasPrefix(peerStr, "https://") {- client := &http.Client{Timeout: 10 * time.Second}- resp, err := client.Get(peerStr + "/info")- if err != nil {- h.logger.Errorf("[Federation] HTTP Get error for %s: %v", peerStr, err)- continue- }- if resp.StatusCode != http.StatusOK {- h.logger.Errorf("[Federation] HTTP Get for %s returned status %d", peerStr, resp.StatusCode)- _ = resp.Body.Close()- continue- }- bodyBytes, err := io.ReadAll(resp.Body)- _ = resp.Body.Close()- if err != nil {- h.logger.Errorf("[Federation] ReadAll error: %v", err)- continue- }-- var info api.HubInfoResponse- if err := proto.Unmarshal(bodyBytes, &info); err != nil {- h.logger.Errorf("[Federation] Proto unmarshal error: %v", err)- continue- }-- for _, addrStr := range info.HubAddresses {- ma, err := multiaddr.NewMultiaddr(addrStr)- if err != nil {- h.logger.Errorf("[Federation] Multiaddr error: %v", err)- continue- }-- pi, err := peer.AddrInfoFromP2pAddr(ma)- if err != nil {- h.logger.Errorf("[Federation] AddrInfo error: %v", err)- continue- }-- if pi.ID == h.Host.ID() {- continue- }-- if len(h.Host.Network().ConnsToPeer(pi.ID)) > 0 {- continue- }-- if err := h.Host.Connect(ctx, *pi); err == nil {- h.logger.Infof("[Federation] Connected to bootstrap peer: %s via HTTP %s", pi.ID, peerStr)- } else {- h.logger.Errorf("[Federation] Failed to connect to %s: %v", pi.ID, err)- }- }- continue- }-- ma, err := multiaddr.NewMultiaddr(peerStr)- if err != nil {- continue- }- resolvedAddrs, err := madns.DefaultResolver.Resolve(ctx, ma)- if err != nil {- resolvedAddrs = []multiaddr.Multiaddr{ma}- }- for _, resolved := range resolvedAddrs {- pi, err := peer.AddrInfoFromP2pAddr(resolved)- if err != nil || pi.ID == h.Host.ID() {- continue- }- if len(h.Host.Network().ConnsToPeer(pi.ID)) == 0 {- if err := h.Host.Connect(ctx, *pi); err == nil {- h.logger.Infof("[Federation] Connected to bootstrap peer: %s via %s", pi.ID, peerStr)- }- }- }- }-}--// GetAuthenticatedPeers returns the underlying sync.Map containing authenticated peers.-func (h *Hub) GetAuthenticatedPeers() *sync.Map {- return &h.authenticatedPeers-}diff --git a/internal/hub/hub_test.go b/internal/hub/hub_test.godeleted file mode 100644index 08fbbfc5..00000000--- a/internal/hub/hub_test.go+++ /dev/null@@ -1,89 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "io"- "net/http"- "net/http/httptest"- "path/filepath"- "testing"- "time"-- "github.com/google/sam/api"- "github.com/libp2p/go-libp2p"- "github.com/libp2p/go-libp2p/core/crypto"- "google.golang.org/protobuf/proto"-)--func TestHandleInfoHTTP(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)- if err != nil {- t.Fatal(err)- }- defer func() { _ = kr.Close() }()-- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)- if err != nil {- t.Fatal(err)- }- h, err := libp2p.New(libp2p.Identity(priv), libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))- if err != nil {- t.Fatal(err)- }- defer h.Close() //nolint:errcheck-- hub := &Hub{- KeyRing: kr,- AllowedAudiences: []string{"test-audience-1", "test-audience-2"},- Host: h,- oidcIssuer: "http://mock-issuer",- }-- req := httptest.NewRequest("GET", "/info", nil)- w := httptest.NewRecorder()-- hub.HandleInfoHTTP(w, req)-- resp := w.Result()- if resp.StatusCode != http.StatusOK {- t.Errorf("Expected status 200, got %d", resp.StatusCode)- }-- body, err := io.ReadAll(resp.Body)- if err != nil {- t.Fatal(err)- }-- var info api.HubInfoResponse- if err := proto.Unmarshal(body, &info); err != nil {- t.Fatalf("Failed to unmarshal protobuf: %v", err)- }-- if info.OidcIssuer != "http://mock-issuer" {- t.Errorf("Expected issuer 'http://mock-issuer', got %q", info.OidcIssuer)- }-- if info.ClientId != "test-audience-1" {- t.Errorf("Expected client ID 'test-audience-1', got %q", info.ClientId)- }-- if info.Audience != "test-audience-1" {- t.Errorf("Expected audience 'test-audience-1', got %q", info.Audience)- }-}diff --git a/internal/hub/keyring.go b/internal/hub/keyring.godeleted file mode 100644index 5b3f74eb..00000000--- a/internal/hub/keyring.go+++ /dev/null@@ -1,210 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "bytes"- "crypto/ed25519"- "crypto/rand"- "encoding/json"- "fmt"- "sync"- "time"-- "go.etcd.io/bbolt"-)--// KeyPair holds an ed25519 keypair and its expiration time if it is a previous key.-type KeyPair struct {- Private []byte `json:"private"`- Public []byte `json:"public"`- Expiration time.Time `json:"expiration"`-}--// KeyRing manages the active and expired keys for the Hub.-type KeyRing struct {- Current KeyPair `json:"current"`- Previous []KeyPair `json:"previous"`- mu sync.RWMutex- db *bbolt.DB-}--// NewKeyRing opens or creates a BoltDB file to store the keyring.-func NewKeyRing(dbPath string, gracePeriod time.Duration, initialSeed []byte) (*KeyRing, error) {- db, err := bbolt.Open(dbPath, 0600, nil)- if err != nil {- return nil, fmt.Errorf("failed to open db: %w", err)- }-- kr := &KeyRing{db: db}-- err = db.Update(func(tx *bbolt.Tx) error {- _, err := tx.CreateBucketIfNotExists([]byte("keyring"))- return err- })- if err != nil {- _ = db.Close()- return nil, fmt.Errorf("failed to create bucket: %w", err)- }-- if err := kr.load(); err != nil {- _ = db.Close()- return nil, fmt.Errorf("failed to load keyring: %w", err)- }-- if len(initialSeed) > 0 {- if len(initialSeed) != ed25519.SeedSize {- _ = db.Close()- return nil, fmt.Errorf("invalid seed size: %d, expected %d", len(initialSeed), ed25519.SeedSize)- }- priv := ed25519.NewKeyFromSeed(initialSeed)- pub := priv.Public().(ed25519.PublicKey)-- if !bytes.Equal(kr.Current.Private, priv) {- if len(kr.Current.Private) > 0 {- kr.Current.Expiration = time.Now().Add(gracePeriod)- kr.Previous = append(kr.Previous, kr.Current)- }- kr.Current = KeyPair{- Private: priv,- Public: pub,- }- if err := kr.save(); err != nil {- _ = db.Close()- return nil, fmt.Errorf("failed to save initial key: %w", err)- }- }- } else if len(kr.Current.Private) == 0 {- pub, priv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- _ = db.Close()- return nil, fmt.Errorf("failed to generate initial key: %w", err)- }- kr.Current = KeyPair{- Private: priv,- Public: pub,- }- if err := kr.save(); err != nil {- _ = db.Close()- return nil, fmt.Errorf("failed to save initial key: %w", err)- }- }-- return kr, nil-}--func (kr *KeyRing) load() error {- return kr.db.View(func(tx *bbolt.Tx) error {- b := tx.Bucket([]byte("keyring"))- data := b.Get([]byte("data"))- if data == nil {- return nil- }- return json.Unmarshal(data, kr)- })-}--func (kr *KeyRing) save() error {- data, err := json.Marshal(kr)- if err != nil {- return err- }- return kr.db.Update(func(tx *bbolt.Tx) error {- b := tx.Bucket([]byte("keyring"))- return b.Put([]byte("data"), data)- })-}--// PrepareRotation generates a new keypair and returns it along with the old private key for signing.-// It does NOT update the current key in the keyring yet.-func (kr *KeyRing) PrepareRotation() (newPub ed25519.PublicKey, newPriv ed25519.PrivateKey, oldPriv ed25519.PrivateKey, err error) {- kr.mu.RLock()- defer kr.mu.RUnlock()-- oldPriv = ed25519.PrivateKey(kr.Current.Private)- newPub, newPriv, err = ed25519.GenerateKey(rand.Reader)- return newPub, newPriv, oldPriv, err-}--// CommitRotation promotes the new key to current and moves the old current to previous.-func (kr *KeyRing) CommitRotation(newPub ed25519.PublicKey, newPriv ed25519.PrivateKey, gracePeriod time.Duration) error {- kr.mu.Lock()- defer kr.mu.Unlock()-- if len(kr.Current.Private) > 0 {- kr.Current.Expiration = time.Now().Add(gracePeriod)- kr.Previous = append(kr.Previous, kr.Current)- }-- kr.Current = KeyPair{- Private: newPriv,- Public: newPub,- }-- // Clean up expired keys- now := time.Now()- var activePrevious []KeyPair- for _, kp := range kr.Previous {- if kp.Expiration.After(now) {- activePrevious = append(activePrevious, kp)- }- }- kr.Previous = activePrevious-- return kr.save()-}--// GetCurrentKey returns the current private key for signing.-func (kr *KeyRing) GetCurrentKey() ed25519.PrivateKey {- kr.mu.RLock()- defer kr.mu.RUnlock()- return ed25519.PrivateKey(kr.Current.Private)-}--// GetCurrentPublicKey returns the current public key.-func (kr *KeyRing) GetCurrentPublicKey() ed25519.PublicKey {- kr.mu.RLock()- defer kr.mu.RUnlock()- return ed25519.PublicKey(kr.Current.Public)-}--// GetAllValidKeys returns all private keys that are still valid (current + non-expired previous).-func (kr *KeyRing) GetAllValidKeys() []ed25519.PrivateKey {- kr.mu.RLock()- defer kr.mu.RUnlock()-- keys := []ed25519.PrivateKey{ed25519.PrivateKey(kr.Current.Private)}- for _, kp := range kr.Previous {- keys = append(keys, ed25519.PrivateKey(kp.Private))- }- return keys-}--// GetAllValidPublicKeys returns all public keys that are still valid.-func (kr *KeyRing) GetAllValidPublicKeys() []ed25519.PublicKey {- kr.mu.RLock()- defer kr.mu.RUnlock()-- keys := []ed25519.PublicKey{ed25519.PublicKey(kr.Current.Public)}- for _, kp := range kr.Previous {- keys = append(keys, ed25519.PublicKey(kp.Public))- }- return keys-}--// Close closes the BoltDB database.-func (kr *KeyRing) Close() error {- return kr.db.Close()-}diff --git a/internal/hub/keyring_test.go b/internal/hub/keyring_test.godeleted file mode 100644index fa9bfe76..00000000--- a/internal/hub/keyring_test.go+++ /dev/null@@ -1,145 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "bytes"- "path/filepath"- "testing"- "time"-)--func TestNewKeyRing(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)- if err != nil {- t.Fatal(err)- }- defer func() { _ = kr.Close() }()-- if len(kr.Current.Private) == 0 {- t.Error("Expected current private key to be initialized")- }- if len(kr.Current.Public) == 0 {- t.Error("Expected current public key to be initialized")- }-}--func TestKeyRingRotate(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)- if err != nil {- t.Fatal(err)- }- defer func() { _ = kr.Close() }()-- oldPub := kr.Current.Public-- newPub, newPriv, _, err := kr.PrepareRotation()- if err != nil {- t.Fatal(err)- }- err = kr.CommitRotation(newPub, newPriv, 24*time.Hour)- if err != nil {- t.Fatal(err)- }-- if bytes.Equal(oldPub, newPub) {- t.Error("Expected new public key to be different from old public key")- }-- if len(kr.Previous) != 1 {- t.Errorf("Expected 1 previous key, got %d", len(kr.Previous))- }- if !bytes.Equal(kr.Previous[0].Public, oldPub) {- t.Error("Expected previous key to match old public key")- }-}--func TestKeyRingPersistence(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)- if err != nil {- t.Fatal(err)- }-- currentPub := kr.Current.Public- _ = kr.Close()-- // Reopen- kr2, err := NewKeyRing(dbPath, 24*time.Hour, nil)- if err != nil {- t.Fatal(err)- }- defer func() { _ = kr2.Close() }()-- if !bytes.Equal(kr2.Current.Public, currentPub) {- t.Error("Expected persisted public key to match original")- }-}--func TestKeyRingCleanup(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 1*time.Millisecond, nil) // Short grace period- if err != nil {- t.Fatal(err)- }- defer func() { _ = kr.Close() }()-- oldPub := kr.Current.Public-- newPub, newPriv, _, err := kr.PrepareRotation()- if err != nil {- t.Fatal(err)- }- err = kr.CommitRotation(newPub, newPriv, 1*time.Millisecond)- if err != nil {- t.Fatal(err)- }-- if len(kr.Previous) != 1 {- t.Errorf("Expected 1 previous key before expiration, got %d", len(kr.Previous))- }-- time.Sleep(5 * time.Millisecond) // Wait for expiration-- // Rotate again to trigger cleanup- newPub, newPriv, _, err = kr.PrepareRotation()- if err != nil {- t.Fatal(err)- }- err = kr.CommitRotation(newPub, newPriv, 1*time.Millisecond)- if err != nil {- t.Fatal(err)- }-- // The first previous key should be cleaned up!- // So we should still have 1 previous key (the one from the second rotation),- // but its public key should NOT be oldPub!- if len(kr.Previous) != 1 {- t.Errorf("Expected 1 previous key after cleanup, got %d", len(kr.Previous))- }- if bytes.Equal(kr.Previous[0].Public, oldPub) {- t.Error("Expected expired key to be pruned")- }-}diff --git a/internal/hub/metrics.go b/internal/hub/metrics.godeleted file mode 100644index 03721aaf..00000000--- a/internal/hub/metrics.go+++ /dev/null@@ -1,37 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "github.com/prometheus/client_golang/prometheus"- "github.com/prometheus/client_golang/prometheus/promauto"-)--var (- samHubEnrollmentTotal = promauto.NewCounterVec(prometheus.CounterOpts{- Name: "sam_hub_enrollment_total",- Help: "Total enrollment requests, partitioned by status.",- }, []string{"status"})-- samHubMeshEventsTotal = promauto.NewCounterVec(prometheus.CounterOpts{- Name: "sam_hub_mesh_events_total",- Help: "Total gossip events processed, partitioned by event_type.",- }, []string{"event_type"})-- samHubKeyRotationsTotal = promauto.NewCounter(prometheus.CounterOpts{- Name: "sam_hub_key_rotations_total",- Help: "The number of times the Hub has rotated its cryptographic keys.",- })-)diff --git a/internal/hub/options.go b/internal/hub/options.godeleted file mode 100644index 7deff958..00000000--- a/internal/hub/options.go+++ /dev/null@@ -1,86 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "fmt"- "time"-- "github.com/google/sam/api"-)--const (- DefaultOIDCIssuer = "https://accounts.google.com"- DefaultMeshName = "public-mesh"- DefaultPolicyFile = "policies.yaml"- DefaultKeysDBPath = "keys.db"- DefaultBindAddress = ":9090"-)--// Options holds configuration settings for the Hub.-type Options struct {- OIDCIssuer string- ClientID string- BiscuitHex string- ListenAddrs []string- MeshName string- InsecureSkipTLSVerify bool- PolicyFile string- KeyRotationInterval time.Duration- KeyGracePeriod time.Duration- KeysDBPath string- BindAddress string- AdminToken string- TLSCertFile string- TLSKeyFile string- TLSCAFile string- ExternalMultiaddrs []string- AllowedAudiences []string- AllowLoopback bool- Policy *api.PolicyConfig-}--// Default sets non-zero default values for options.-func (o *Options) Default() {- if o.OIDCIssuer == "" {- o.OIDCIssuer = DefaultOIDCIssuer- }- if o.MeshName == "" {- o.MeshName = DefaultMeshName- }- if o.PolicyFile == "" {- o.PolicyFile = DefaultPolicyFile- }- if o.KeysDBPath == "" {- o.KeysDBPath = DefaultKeysDBPath- }- if o.BindAddress == "" {- o.BindAddress = DefaultBindAddress- }- if o.KeyGracePeriod == 0 {- o.KeyGracePeriod = 24 * time.Hour- }-}--// Validate checks that all required options are set and valid.-func (o *Options) Validate() error {- if o.KeysDBPath == "" {- return fmt.Errorf("keys database path is required")- }- if o.Policy == nil {- return fmt.Errorf("policy configuration is required")- }- return nil-}diff --git a/internal/hub/relay_acl_test.go b/internal/hub/relay_acl_test.godeleted file mode 100644index 172754c3..00000000--- a/internal/hub/relay_acl_test.go+++ /dev/null@@ -1,66 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "testing"-- "github.com/libp2p/go-libp2p/core/peer"- "github.com/multiformats/go-multiaddr"-)--func TestRelayACL_AllowConnect(t *testing.T) {- hub := &Hub{}- acl := &relayACL{hub: hub}-- srcPeer := peer.ID("src-peer")- destPeer := peer.ID("dest-peer")- srcAddr, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")-- // Neither is authenticated- if acl.AllowConnect(srcPeer, srcAddr, destPeer) {- t.Errorf("Expected AllowConnect to return false when dest is not authenticated")- }-- // Src is authenticated, dest is not -> should fail- hub.authenticatedPeers.Store(srcPeer, true)- if acl.AllowConnect(srcPeer, srcAddr, destPeer) {- t.Errorf("Expected AllowConnect to return false when dest is not authenticated, even if src is")- }-- // Dest is authenticated, src is not -> should succeed- hub.authenticatedPeers.Delete(srcPeer)- hub.authenticatedPeers.Store(destPeer, true)- if !acl.AllowConnect(srcPeer, srcAddr, destPeer) {- t.Errorf("Expected AllowConnect to return true when dest is authenticated")- }-}--func TestRelayACL_AllowReserve(t *testing.T) {- hub := &Hub{}- acl := &relayACL{hub: hub}-- peerID := peer.ID("some-peer")- addr, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")-- if acl.AllowReserve(peerID, addr) {- t.Errorf("Expected AllowReserve to return false when peer is not authenticated")- }-- hub.authenticatedPeers.Store(peerID, true)- if !acl.AllowReserve(peerID, addr) {- t.Errorf("Expected AllowReserve to return true when peer is authenticated")- }-}diff --git a/internal/hub/server.go b/internal/hub/server.godeleted file mode 100644index 30f3f25d..00000000--- a/internal/hub/server.go+++ /dev/null@@ -1,186 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package hub--import (- "context"- "io"- "net/http"- "sort"- "strings"-- "github.com/google/sam/api"- "github.com/libp2p/go-libp2p/core/peer"- "google.golang.org/protobuf/proto"-)--// HandleRegisterHTTP registers HTTP enrollment request.-func (h *Hub) HandleRegisterHTTP(w http.ResponseWriter, r *http.Request) {- if r.Method != http.MethodPost {- http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)- return- }- if r.URL.Path != "/register" {- http.NotFound(w, r)- return- }-- body, err := io.ReadAll(r.Body)- if err != nil {- http.Error(w, "Failed to read body", http.StatusBadRequest)- return- }- defer func() {- if err := r.Body.Close(); err != nil {- h.logger.Errorf("failed to close request body: %v", err)- }- }()-- var req api.EnrollRequest- if err := proto.Unmarshal(body, &req); err != nil {- http.Error(w, "Invalid request format", http.StatusBadRequest)- return- }-- if !h.limiter.Allow() {- http.Error(w, "Rate limit exceeded", http.StatusTooManyRequests)- return- }-- h.logger.Infow("New HTTP enrollment request", "peer_id", req.PeerId)-- ctx, cancel := context.WithTimeout(context.Background(), JWTVerificationTimeout)- defer cancel()-- claims, token, err := h.parseAndVerifyJWT(ctx, req.Jwt, h.AllowedAudiences)- if err != nil {- h.logger.Errorw("JWT verification failed", "peer_id", req.PeerId, "error", err)- http.Error(w, "JWT validation failed: "+err.Error(), http.StatusUnauthorized)- return- }-- pID, err := peer.Decode(req.PeerId)- if err != nil {- h.logger.Errorw("Invalid Peer ID", "peer_id", req.PeerId, "error", err)- http.Error(w, "Invalid Peer ID", http.StatusBadRequest)- return- }-- biscuitData, err := h.mintBiscuitToken(claims, token, pID)- if err != nil {- h.logger.Errorw("Biscuit minting failed", "peer_id", req.PeerId, "error", err)- http.Error(w, "Failed to mint biscuit", http.StatusInternalServerError)- return- }- allHubAddrs := getMyHubAddrs(h)-- resp := &api.EnrollResponse{- BiscuitToken: biscuitData,- HubPublicKey: h.KeyRing.GetCurrentPublicKey(),- HubAddresses: allHubAddrs,- Expiration: token.Expiry.Unix(),- }-- respData, err := proto.Marshal(resp)- if err != nil {- h.logger.Errorf("[Enroll] Failed to marshal response: %v", err)- 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)-- h.logger.Infow("Successfully enrolled peer via HTTP", "peer_id", req.PeerId)- samHubEnrollmentTotal.WithLabelValues("success").Inc()-}--// HandleInfoHTTP retrieves basic metadata info of the Hub.-func (h *Hub) HandleInfoHTTP(w http.ResponseWriter, r *http.Request) {- if r.Method != http.MethodGet {- http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)- return- }- if r.URL.Path != "/info" {- http.NotFound(w, r)- return- }-- // Find the OIDC issuer deterministically by sorting the map keys- issuer := ""- if len(h.Providers) > 0 {- issuers := make([]string, 0, len(h.Providers))- for iss := range h.Providers {- issuers = append(issuers, iss)- }- sort.Strings(issuers)- issuer = issuers[0]- }-- // Fallback if Providers isn't fully populated (e.g. in unit tests)- if issuer == "" {- issuer = h.oidcIssuer- }- // If multiple issuers in fallback, pick the first one deterministically- if strings.Contains(issuer, ",") {- parts := strings.Split(issuer, ",")- issuer = strings.TrimSpace(parts[0])- }-- // Get primary client ID / audience- aud := api.DefaultAudience- if len(h.AllowedAudiences) > 0 {- aud = h.AllowedAudiences[0]- }-- resp := &api.HubInfoResponse{- OidcIssuer: issuer,- ClientId: aud,- Audience: aud,- HubAddresses: getMyHubAddrs(h),- }-- respData, err := proto.Marshal(resp)- if err != nil {- h.logger.Errorf("[Info] Failed to marshal info response: %v", err)- 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)-}--func getMyHubAddrs(h *Hub) []string {- var addrs []string- if len(h.ExternalAddrs) > 0 {- for _, addr := range h.ExternalAddrs {- addrs = append(addrs, addr+"/p2p/"+h.Host.ID().String())- }- }-- for _, addr := range h.Host.Addrs() {- if h.AllowLoopback {- addrs = append(addrs, addr.String()+"/p2p/"+h.Host.ID().String())- } else {- // We don't have isLoopbackOrLinkLocal easily here so just include all or rely on AddrsFactory from main.go- addrs = append(addrs, addr.String()+"/p2p/"+h.Host.ID().String())- }- }-- return addrs-}diff --git a/internal/hub/biscuit.go b/internal/identity/biscuit.gosimilarity index 87%rename from internal/hub/biscuit.gorename to internal/identity/biscuit.goindex fc54161e..9ce1354a 100644--- a/internal/hub/biscuit.go+++ b/internal/identity/biscuit.go@@ -12,9 +12,10 @@// See the License for the specific language governing permissions and// limitations under the License.-package hub+package identityimport (+ "crypto/ed25519""errors""fmt""sort"@@ -30,7 +31,8 @@ import ("github.com/libp2p/go-libp2p/core/peer")-func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remotePeer peer.ID) ([]byte, error) {+// 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")}@@ -43,11 +45,9 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remoteoidcSub, _ := claims["sub"].(string)oidcEmail, _ := claims["email"].(string)- // Resolve roles based on configured bindings and explicit OIDC rolesresolvedRoles := make(map[string]bool)- if h.Policy != nil {- // 1. Evaluate explicit Policy Bindings- for _, b := range h.Policy.Bindings {+ if policy != nil {+ for _, b := range policy.Bindings {for _, member := range b.Members {if member == api.SystemAuthenticated {resolvedRoles[b.Role] = true@@ -56,7 +56,7 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remoteparts := strings.SplitN(member, ":", 2)if len(parts) != 2 {- continue // validated at startup, but ignore if broken+ continue}prefix := parts[0]value := parts[1]@@ -84,16 +84,14 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remote}}- // 2. Validate and accept pre-resolved OIDC roles directly if defined in policy (Zero-Trust check)for _, r := range oidcRoles {- if _, exists := h.Policy.Roles[r]; exists {+ if _, exists := policy.Roles[r]; exists {resolvedRoles[r] = true}}}- builder := biscuit.NewBuilder(h.KeyRing.GetCurrentKey())-+ builder := biscuit.NewBuilder(signingKey)addedFacts := make(map[string]bool)addFact := func(fact biscuit.Fact) error {factStr := fact.String()@@ -128,12 +126,10 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remotereturn nil, fmt.Errorf("failed to add client_peer_id fact: %w", err)}- // Dynamic claims to facts mapping using api.OIDCClaimToFactif err := translateClaimsToFacts(addFact, claims); err != nil {return nil, err}- // Assert resolved authorized roles in the tokenroles := make([]string, 0, len(resolvedRoles))for role := range resolvedRoles {roles = append(roles, role)@@ -150,8 +146,8 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remotecontinue}- if h.Policy != nil {- if rolePolicy, ok := h.Policy.Roles[role]; ok {+ if policy != nil {+ if rolePolicy, ok := policy.Roles[role]; ok {for _, svc := range rolePolicy.AllowedServices {svcType, svcName := api.ParseServiceTarget(svc)@@ -267,8 +263,8 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remotehasTargets := falsefor _, role := range roles {- if h.Policy != nil {- if rolePolicy, ok := h.Policy.Roles[role]; ok {+ if policy != nil {+ if rolePolicy, ok := policy.Roles[role]; ok {if len(rolePolicy.AllowedTargets) > 0 {hasTargets = truebreak@@ -303,20 +299,25 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remotereturn biscuitData, nil}-func (h *Hub) verifyBiscuit(biscuitData []byte, remotePeer peer.ID) (*biscuit.Biscuit, error) {+// VerifyBiscuit verifies the validity of a Biscuit token.+// It ensures that:+// 1. The token is cryptographically signed by one of the trustedPublicKeys.+// 2. The token is not expired.+// 3. The token is securely bound to the expected remotePeer.+func VerifyBiscuit(biscuitData []byte, expectedPeer peer.ID, trustedPublicKeys []ed25519.PublicKey, timeout time.Duration) (*biscuit.Biscuit, error) {b, err := biscuit.Unmarshal(biscuitData)if err != nil {return nil, fmt.Errorf("malformed biscuit: %w", err)}var authOpts []biscuit.AuthorizerOption- if h.BiscuitTimeout > 0 {- authOpts = append(authOpts, biscuit.WithWorldOptions(datalog.WithMaxDuration(h.BiscuitTimeout)))+ if timeout > 0 {+ authOpts = append(authOpts, biscuit.WithWorldOptions(datalog.WithMaxDuration(timeout)))}- keys := h.KeyRing.GetAllValidPublicKeys()var lastErr error- for _, pubKey := range keys {+ var authorized bool+ for _, pubKey := range trustedPublicKeys {authorizer, err := b.Authorizer(pubKey, authOpts...)if err != nil {lastErr = err@@ -334,13 +335,27 @@ func (h *Hub) verifyBiscuit(biscuitData []byte, remotePeer peer.ID) (*biscuit.Biauthorizer.AddPolicy(api.AllowIfTruePolicy)if err := authorizer.Authorize(); err == nil {- return b, nil+ authorized = true+ break} else {lastErr = err}}- return nil, fmt.Errorf("no valid key found for verification: %v", lastErr)+ if !authorized {+ return nil, fmt.Errorf("no valid key found for verification: %v", lastErr)+ }++ // Enforce hardware binding+ boundFact := biscuit.Fact{Predicate: biscuit.Predicate{+ Name: "node",+ IDs: []biscuit.Term{biscuit.String(expectedPeer.String())},+ }}+ if _, err := b.GetBlockID(boundFact); err != nil {+ return nil, fmt.Errorf("token is not bound to peer %s: %w", expectedPeer, err)+ }++ return b, nil}func translateClaimsToFacts(addFact func(biscuit.Fact) error, claims map[string]any) error {diff --git a/internal/hub/biscuit_test.go b/internal/identity/biscuit_test.gosimilarity index 58%rename from internal/hub/biscuit_test.gorename to internal/identity/biscuit_test.goindex 4b6ea74e..3dfcb85b 100644--- a/internal/hub/biscuit_test.go+++ b/internal/identity/biscuit_test.go@@ -12,10 +12,11 @@// See the License for the specific language governing permissions and// limitations under the License.-package hub+package identityimport (- "path/filepath"+ "crypto/ed25519"+ "crypto/rand""strings""sync""testing"@@ -31,40 +32,32 @@ import ()func TestMintBiscuitToken(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)+ pub, priv, err := ed25519.GenerateKey(rand.Reader)if err != nil {t.Fatal(err)}- defer func() { _ = kr.Close() }()- hub := &Hub{- KeyRing: kr,- Policy: &api.PolicyConfig{- Bindings: []api.Binding{- {Role: "canary-role", Members: []string{"group:system:serviceaccounts:sam-canary"}},- {Role: "canary-role", Members: []string{"user:system:serviceaccount:sam-canary:sam-node-sa"}},+ policy := &api.PolicyConfig{+ Bindings: []api.Binding{+ {Role: "canary-role", Members: []string{"group:system:serviceaccounts:sam-canary"}},+ {Role: "canary-role", Members: []string{"user:system:serviceaccount:sam-canary:sam-node-sa"}},+ },+ Roles: map[string]api.RolePolicy{+ "admin": {+ AllowedServices: []string{"mcp:read", "mcp:write"},+ AllowedTargets: []string{"mcp:target1"},},- Roles: map[string]api.RolePolicy{- "admin": {- AllowedServices: []string{"mcp:read", "mcp:write"},- AllowedTargets: []string{"mcp:target1"},- },- "canary-role": {- AllowedServices: []string{"mcp:1.0.0"},- },+ "canary-role": {+ AllowedServices: []string{"mcp:1.0.0"},},},- BiscuitTimeout: 500 * time.Millisecond,}- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ privNode, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)if err != nil {t.Fatal(err)}- dummyPeer, err := peer.IDFromPrivateKey(priv)+ dummyPeer, err := peer.IDFromPrivateKey(privNode)if err != nil {t.Fatal(err)}@@ -77,19 +70,16 @@ func TestMintBiscuitToken(t *testing.T) {claims1 := jwt.MapClaims{"roles": []any{"admin"},}- biscuitData1, err := hub.mintBiscuitToken(claims1, token, dummyPeer)+ biscuitData1, err := MintBiscuitToken(priv, claims1, token, dummyPeer, policy)if err != nil {t.Fatal(err)}- if len(biscuitData1) == 0 {- t.Error("Expected non-empty biscuit data for direct role")- }b1, err := biscuit.Unmarshal(biscuitData1)if err != nil {t.Fatal(err)}- authorizer1, err := b1.Authorizer(kr.GetCurrentPublicKey(), biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))+ authorizer1, err := b1.Authorizer(pub, biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))if err != nil {t.Fatal(err)}@@ -114,19 +104,16 @@ func TestMintBiscuitToken(t *testing.T) {claims2 := jwt.MapClaims{"groups": []any{"system:serviceaccounts:sam-canary"},}- biscuitData2, err := hub.mintBiscuitToken(claims2, token, dummyPeer)+ biscuitData2, err := MintBiscuitToken(priv, claims2, token, dummyPeer, policy)if err != nil {t.Fatal(err)}- if len(biscuitData2) == 0 {- t.Error("Expected non-empty biscuit data for mapped group")- }b2, err := biscuit.Unmarshal(biscuitData2)if err != nil {t.Fatal(err)}- authorizer2, err := b2.Authorizer(kr.GetCurrentPublicKey(), biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))+ authorizer2, err := b2.Authorizer(pub, biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))if err != nil {t.Fatal(err)}@@ -148,7 +135,7 @@ func TestMintBiscuitToken(t *testing.T) {"groups": []any{"unknown-group"},"roles": []any{"undefined-role"},}- biscuitData3, err := hub.mintBiscuitToken(claims3, token, dummyPeer)+ biscuitData3, err := MintBiscuitToken(priv, claims3, token, dummyPeer, policy)if err != nil {t.Fatal(err)}@@ -156,11 +143,10 @@ func TestMintBiscuitToken(t *testing.T) {if err != nil {t.Fatal(err)}- authorizer3, err := b3.Authorizer(kr.GetCurrentPublicKey(), biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))+ authorizer3, err := b3.Authorizer(pub, biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))if err != nil {t.Fatal(err)}- // Verify no role matchesrule3 := biscuit.Policy{Queries: []biscuit.Rule{{Head: biscuit.Predicate{Name: "allow", IDs: []biscuit.Term{}},@@ -178,7 +164,7 @@ func TestMintBiscuitToken(t *testing.T) {claims4 := jwt.MapClaims{"sub": "system:serviceaccount:sam-canary:sam-node-sa",}- biscuitData4, err := hub.mintBiscuitToken(claims4, token, dummyPeer)+ biscuitData4, err := MintBiscuitToken(priv, claims4, token, dummyPeer, policy)if err != nil {t.Fatal(err)}@@ -186,11 +172,10 @@ func TestMintBiscuitToken(t *testing.T) {if err != nil {t.Fatal(err)}- authorizer4, err := b4.Authorizer(kr.GetCurrentPublicKey(), biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))+ authorizer4, err := b4.Authorizer(pub, biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))if err != nil {t.Fatal(err)}- // Verify that it mapped the derived group "system:serviceaccounts:sam-canary" to "canary-role"rule4 := biscuit.Policy{Queries: []biscuit.Rule{{Head: biscuit.Predicate{Name: "allow", IDs: []biscuit.Term{}},@@ -206,26 +191,16 @@ func TestMintBiscuitToken(t *testing.T) {}func TestVerifyBiscuit_Expiration(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)+ pub, priv, err := ed25519.GenerateKey(rand.Reader)if err != nil {t.Fatal(err)}- defer func() { _ = kr.Close() }()-- hub := &Hub{- KeyRing: kr,- Policy: &api.PolicyConfig{},- BiscuitTimeout: 500 * time.Millisecond,- }- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ privNode, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)if err != nil {t.Fatal(err)}- dummyPeer, err := peer.IDFromPrivateKey(priv)+ dummyPeer, err := peer.IDFromPrivateKey(privNode)if err != nil {t.Fatal(err)}@@ -256,12 +231,12 @@ func TestVerifyBiscuit_Expiration(t *testing.T) {"roles": []any{"admin"},}- biscuitData, err := hub.mintBiscuitToken(claims, token, dummyPeer)+ biscuitData, err := MintBiscuitToken(priv, claims, token, dummyPeer, &api.PolicyConfig{})if err != nil {- t.Fatalf("mintBiscuitToken failed: %v", err)+ t.Fatalf("MintBiscuitToken failed: %v", err)}- _, err = hub.verifyBiscuit(biscuitData, dummyPeer)+ _, err = VerifyBiscuit(biscuitData, dummyPeer, []ed25519.PublicKey{pub}, 500*time.Millisecond)if tt.expectError && err == nil {t.Errorf("Expected error due to expiration, got nil")}@@ -273,35 +248,27 @@ func TestVerifyBiscuit_Expiration(t *testing.T) {}func TestMintBiscuitToken_ClaimsTranslation(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)+ pub, priv, err := ed25519.GenerateKey(rand.Reader)if err != nil {t.Fatal(err)}- defer func() { _ = kr.Close() }()- hub := &Hub{- KeyRing: kr,- Policy: &api.PolicyConfig{- Bindings: []api.Binding{- {Role: "developer-role", Members: []string{"group:engineering"}},- },- Roles: map[string]api.RolePolicy{- "developer-role": {- AllowedServices: []string{"mcp:git-helper"},- },+ policy := &api.PolicyConfig{+ Bindings: []api.Binding{+ {Role: "developer-role", Members: []string{"group:engineering"}},+ },+ Roles: map[string]api.RolePolicy{+ "developer-role": {+ AllowedServices: []string{"mcp:git-helper"},},},- BiscuitTimeout: 500 * time.Millisecond,}- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ privNode, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)if err != nil {t.Fatal(err)}- dummyPeer, err := peer.IDFromPrivateKey(priv)+ dummyPeer, err := peer.IDFromPrivateKey(privNode)if err != nil {t.Fatal(err)}@@ -316,7 +283,7 @@ func TestMintBiscuitToken_ClaimsTranslation(t *testing.T) {"groups": []any{"beta-testers", "engineering"},}- biscuitData, err := hub.mintBiscuitToken(claims, token, dummyPeer)+ biscuitData, err := MintBiscuitToken(priv, claims, token, dummyPeer, policy)if err != nil {t.Fatalf("Failed to mint biscuit: %v", err)}@@ -326,7 +293,7 @@ func TestMintBiscuitToken_ClaimsTranslation(t *testing.T) {t.Fatalf("Failed to unmarshal biscuit: %v", err)}- authorizer, err := b.Authorizer(kr.GetCurrentPublicKey(), biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))+ authorizer, err := b.Authorizer(pub, biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))if err != nil {t.Fatalf("Failed to get authorizer: %v", err)}@@ -371,8 +338,6 @@ func TestMintBiscuitToken_ClaimsTranslation(t *testing.T) {}}authorizer.AddCheck(checkGroupEng)- // To authorize, we also need to allow since checks run in authorizer.- // We will add an allow policy that matches anythingauthorizer.AddPolicy(biscuit.Policy{Queries: []biscuit.Rule{{Head: biscuit.Predicate{Name: "allow", IDs: []biscuit.Term{}},@@ -385,100 +350,27 @@ func TestMintBiscuitToken_ClaimsTranslation(t *testing.T) {}}-func TestMintBiscuitToken_NilToken(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)- if err != nil {- t.Fatal(err)- }- defer func() { _ = kr.Close() }()-- hub := &Hub{- KeyRing: kr,- BiscuitTimeout: 500 * time.Millisecond,- }-- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)- if err != nil {- t.Fatal(err)- }- dummyPeer, err := peer.IDFromPrivateKey(priv)- if err != nil {- t.Fatal(err)- }-- claims := jwt.MapClaims{"sub": "user-123"}- _, err = hub.mintBiscuitToken(claims, nil, dummyPeer)- if err == nil {- t.Error("Expected mintBiscuitToken to fail with nil token, got nil error")- }-}--func TestMintBiscuitToken_NilClaims(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)- if err != nil {- t.Fatal(err)- }- defer func() { _ = kr.Close() }()-- hub := &Hub{- KeyRing: kr,- BiscuitTimeout: 500 * time.Millisecond,- }-- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)- if err != nil {- t.Fatal(err)- }- dummyPeer, err := peer.IDFromPrivateKey(priv)- if err != nil {- t.Fatal(err)- }-- token := &oidc.IDToken{- Expiry: time.Now().Add(1 * time.Hour),- }-- _, err = hub.mintBiscuitToken(nil, token, dummyPeer)- if err == nil {- t.Error("Expected mintBiscuitToken to fail with nil claims, got nil error")- }-}-func TestMintBiscuitToken_VariousClaimsTypes(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)+ pub, priv, err := ed25519.GenerateKey(rand.Reader)if err != nil {t.Fatal(err)}- defer func() { _ = kr.Close() }()- hub := &Hub{- KeyRing: kr,- Policy: &api.PolicyConfig{- Bindings: []api.Binding{- {Role: "eng-role", Members: []string{"group:eng-group"}},- },- Roles: map[string]api.RolePolicy{- "admin": {},- "eng-role": {},- },+ policy := &api.PolicyConfig{+ Bindings: []api.Binding{+ {Role: "eng-role", Members: []string{"group:eng-group"}},+ },+ Roles: map[string]api.RolePolicy{+ "admin": {},+ "eng-role": {},},- BiscuitTimeout: 500 * time.Millisecond,}- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ privNode, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)if err != nil {t.Fatal(err)}- dummyPeer, err := peer.IDFromPrivateKey(priv)+ dummyPeer, err := peer.IDFromPrivateKey(privNode)if err != nil {t.Fatal(err)}@@ -498,7 +390,7 @@ func TestMintBiscuitToken_VariousClaimsTypes(t *testing.T) {name: "String slice (standard go code paths)",rolesClaim: []string{"admin"},groupsClaim: []string{"eng-group", "beta"},- expectedRoles: []string{"admin", "eng-role"}, // eng-role comes from eng-group mapping+ expectedRoles: []string{"admin", "eng-role"},expectedGroups: []string{"eng-group", "beta"},},{@@ -534,9 +426,9 @@ func TestMintBiscuitToken_VariousClaimsTypes(t *testing.T) {claims["groups"] = tt.groupsClaim}- biscuitData, err := hub.mintBiscuitToken(claims, token, dummyPeer)+ biscuitData, err := MintBiscuitToken(priv, claims, token, dummyPeer, policy)if err != nil {- t.Fatalf("mintBiscuitToken failed: %v", err)+ t.Fatalf("MintBiscuitToken failed: %v", err)}b, err := biscuit.Unmarshal(biscuitData)@@ -544,12 +436,11 @@ func TestMintBiscuitToken_VariousClaimsTypes(t *testing.T) {t.Fatalf("Unmarshal biscuit failed: %v", err)}- authorizer, err := b.Authorizer(kr.GetCurrentPublicKey(), biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))+ authorizer, err := b.Authorizer(pub, biscuit.WithWorldOptions(datalog.WithMaxDuration(500*time.Millisecond)))if err != nil {t.Fatalf("Authorizer failed: %v", err)}- // Add checks to verify the output factsfor _, r := range tt.expectedRoles {authorizer.AddCheck(biscuit.Check{Queries: []biscuit.Rule{{@@ -585,26 +476,16 @@ func TestMintBiscuitToken_VariousClaimsTypes(t *testing.T) {}func TestVerifyBiscuit_Concurrent(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)+ pub, priv, err := ed25519.GenerateKey(rand.Reader)if err != nil {t.Fatal(err)}- defer func() { _ = kr.Close() }()- hub := &Hub{- KeyRing: kr,- Policy: &api.PolicyConfig{},- BiscuitTimeout: 500 * time.Millisecond,- }-- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ privNode, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)if err != nil {t.Fatal(err)}- dummyPeer, err := peer.IDFromPrivateKey(priv)+ dummyPeer, err := peer.IDFromPrivateKey(privNode)if err != nil {t.Fatal(err)}@@ -617,12 +498,11 @@ func TestVerifyBiscuit_Concurrent(t *testing.T) {"roles": []any{"admin"},}- biscuitData, err := hub.mintBiscuitToken(claims, token, dummyPeer)+ biscuitData, err := MintBiscuitToken(priv, claims, token, dummyPeer, &api.PolicyConfig{})if err != nil {- t.Fatalf("mintBiscuitToken failed: %v", err)+ t.Fatalf("MintBiscuitToken failed: %v", err)}- // Spin up 50 goroutines performing verification concurrently to detect any data races on static check/policy globalsconst workers = 50var wg sync.WaitGroupwg.Add(workers)@@ -631,7 +511,7 @@ func TestVerifyBiscuit_Concurrent(t *testing.T) {go func() {defer wg.Done()for j := 0; j < 100; j++ {- _, err := hub.verifyBiscuit(biscuitData, dummyPeer)+ _, err := VerifyBiscuit(biscuitData, dummyPeer, []ed25519.PublicKey{pub}, 500*time.Millisecond)if err != nil {t.Errorf("Concurrent verification failed: %v", err)return@@ -643,35 +523,27 @@ func TestVerifyBiscuit_Concurrent(t *testing.T) {}func TestMintBiscuitToken_ErrorAggregation(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)+ _, priv, err := ed25519.GenerateKey(rand.Reader)if err != nil {t.Fatal(err)}- defer func() { _ = kr.Close() }()- hub := &Hub{- KeyRing: kr,- Policy: &api.PolicyConfig{- Roles: map[string]api.RolePolicy{- "admin": {- CustomDatalog: []string{- "invalid-datalog-fact(123", // syntax error- "another-bad-fact);", // syntax error- },+ policy := &api.PolicyConfig{+ Roles: map[string]api.RolePolicy{+ "admin": {+ CustomDatalog: []string{+ "invalid-datalog-fact(123", // syntax error+ "another-bad-fact);", // syntax error},},},- BiscuitTimeout: 500 * time.Millisecond,}- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ privNode, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)if err != nil {t.Fatal(err)}- dummyPeer, err := peer.IDFromPrivateKey(priv)+ dummyPeer, err := peer.IDFromPrivateKey(privNode)if err != nil {t.Fatal(err)}@@ -684,9 +556,9 @@ func TestMintBiscuitToken_ErrorAggregation(t *testing.T) {"roles": []any{"admin"},}- _, err = hub.mintBiscuitToken(claims, token, dummyPeer)+ _, err = MintBiscuitToken(priv, claims, token, dummyPeer, policy)if err == nil {- t.Fatal("Expected mintBiscuitToken to fail on custom datalog syntax errors, got nil error")+ t.Fatal("Expected MintBiscuitToken to fail on custom datalog syntax errors, got nil error")}errStr := err.Error()@@ -694,46 +566,37 @@ func TestMintBiscuitToken_ErrorAggregation(t *testing.T) {t.Errorf("Expected error message to contain parse failure info, got: %s", errStr)}- // Verify that BOTH errors are aggregated in the error messageif !strings.Contains(errStr, "invalid-datalog-fact") || !strings.Contains(errStr, "another-bad-fact") {t.Errorf("Expected error to aggregate both failures, got: %s", errStr)}}func TestMintBiscuitToken_FactDeduplication(t *testing.T) {- dir := t.TempDir()- dbPath := filepath.Join(dir, "test.db")-- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)+ _, priv, err := ed25519.GenerateKey(rand.Reader)if err != nil {t.Fatal(err)}- defer func() { _ = kr.Close() }()- hub := &Hub{- KeyRing: kr,- Policy: &api.PolicyConfig{- Bindings: []api.Binding{- {Role: "role-a", Members: []string{"user:user-1"}},- {Role: "role-b", Members: []string{"user:user-1"}},+ policy := &api.PolicyConfig{+ Bindings: []api.Binding{+ {Role: "role-a", Members: []string{"user:user-1"}},+ {Role: "role-b", Members: []string{"user:user-1"}},+ },+ Roles: map[string]api.RolePolicy{+ "role-a": {+ AllowedTargets: []string{"*:*"},},- Roles: map[string]api.RolePolicy{- "role-a": {- AllowedTargets: []string{"*:*"},- },- "role-b": {- AllowedTargets: []string{"*:*"},- },+ "role-b": {+ AllowedTargets: []string{"*:*"},},},- BiscuitTimeout: 500 * time.Millisecond,}- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ privNode, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)if err != nil {t.Fatal(err)}- dummyPeer, err := peer.IDFromPrivateKey(priv)+ dummyPeer, err := peer.IDFromPrivateKey(privNode)if err != nil {t.Fatal(err)}@@ -745,10 +608,9 @@ func TestMintBiscuitToken_FactDeduplication(t *testing.T) {"sub": "user-1",}- // This should not fail with "fact already exists" since the target is duplicated across both roles.- biscuitData, err := hub.mintBiscuitToken(claims, token, dummyPeer)+ biscuitData, err := MintBiscuitToken(priv, claims, token, dummyPeer, policy)if err != nil {- t.Fatalf("mintBiscuitToken failed with duplicate facts: %v", err)+ t.Fatalf("MintBiscuitToken failed with duplicate facts: %v", err)}if len(biscuitData) == 0 {diff --git a/internal/identity/oidc.go b/internal/identity/oidc.gonew file mode 100644index 00000000..3a1a19f1--- /dev/null+++ b/internal/identity/oidc.go@@ -0,0 +1,92 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package identity++import (+ "context"+ "fmt"+ "strings"++ "github.com/coreos/go-oidc/v3/oidc"+ "github.com/golang-jwt/jwt/v5"+)++// VerifyJWT parses and cryptographically validates a JWT token against a list of allowed audiences+// and resolved OIDC providers.+func VerifyJWT(ctx context.Context, jwtStr string, allowedAudiences []string, providers map[string]*oidc.Provider) (jwt.MapClaims, *oidc.IDToken, error) {+ jwtParser := jwt.Parser{}+ jwtToken, _, err := jwtParser.ParseUnverified(jwtStr, jwt.MapClaims{})+ if err != nil {+ return nil, nil, fmt.Errorf("failed to parse JWT: %w", err)+ }++ // 1. Defend against downgrade attacks immediately+ alg, ok := jwtToken.Header["alg"].(string)+ if !ok || alg == "" || strings.ToLower(alg) == "none" {+ return nil, nil, fmt.Errorf("invalid or missing alg header")+ }++ claims, ok := jwtToken.Claims.(jwt.MapClaims)+ if !ok {+ return nil, nil, fmt.Errorf("invalid JWT claims")+ }+ iss, _ := claims["iss"].(string)++ // 2. Extract the audience+ var aud string+ switch a := claims["aud"].(type) {+ case string:+ aud = a+ case []any:+ if len(a) > 0 {+ aud, _ = a[0].(string)+ }+ }++ if aud == "" {+ return nil, nil, fmt.Errorf("missing aud claim")+ }++ // 3. Verify the audience matches one of your expected tenants/platforms+ validAudience := false+ for _, allowed := range allowedAudiences {+ if aud == allowed {+ validAudience = true+ break+ }+ }+ if !validAudience {+ return nil, nil, fmt.Errorf("untrusted audience: %s", aud)+ }++ // 4. Route to the correct provider+ provider, ok := providers[iss]+ if !ok {+ return nil, nil, fmt.Errorf("unknown issuer: %s", iss)+ }++ // 5. Verify cryptographic signature, bypassing the strict single-clientID check+ // because we already validated the audience against our allowed list above.+ verifier := provider.Verifier(&oidc.Config{+ SkipClientIDCheck: true,+ })++ token, err := verifier.Verify(ctx, jwtStr)+ if err != nil {+ return nil, nil, fmt.Errorf("JWT validation failed: %w", err)+ }++ return claims, token, nil+}diff --git a/internal/node/gate_test.go b/internal/node/gate_test.goindex da14f49e..39b3c5cd 100644--- a/internal/node/gate_test.go+++ b/internal/node/gate_test.go@@ -125,11 +125,11 @@ func startBareNode(t *testing.T, ctx context.Context) (*SamNode, func()) {AllowLoopback: true,MonitorBootstrap: 2 * time.Minute,MonitorInterval: 1 * time.Minute,- BiscuitTimeout: 500 * time.Millisecond,})if err != nil {t.Fatal(err)}+ node.BiscuitTimeout = 500 * time.Millisecondif err := node.Start(ctx); err != nil {t.Fatal(err)}diff --git a/internal/node/node.go b/internal/node/node.goindex ca9d49ea..0ccaf3f3 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -37,6 +37,7 @@ import ("github.com/biscuit-auth/biscuit-go/v2""github.com/biscuit-auth/biscuit-go/v2/datalog""github.com/google/sam/api"+ "github.com/google/sam/internal/identity"lru "github.com/hashicorp/golang-lru/v2""github.com/ipfs/go-cid"golog "github.com/ipfs/go-log/v2"@@ -243,7 +244,7 @@ func NewSamNode(cfg Options) (*SamNode, error) {reprovideTrigger: make(chan struct{}, 1),logger: golog.Logger("sam-node"),}- node.BiscuitTimeout = cfg.BiscuitTimeout+ node.BiscuitTimeout = 2 * time.Secondvar err errornode.rateLimiter, err = NewPeerRateLimiter(RateLimiterSize)@@ -698,7 +699,7 @@ func (n *SamNode) ConnectAndAuthWithHub(ctx context.Context, addr multiaddr.Mult}_ = s.SetDeadline(time.Now().Add(5 * time.Second))- success, err := n.performHubAuthHandshake(s, biscuitBytes)+ success, err := n.performHubAuthHandshake(s, biscuitBytes, addrInfo.ID)if err != nil {_ = s.Reset()cancel()@@ -734,7 +735,7 @@ func (n *SamNode) ConnectAndAuthWithHub(ctx context.Context, addr multiaddr.Multreturn fmt.Errorf("failed to authenticate with any hub addresses: %w", errors.Join(errs...))}-func (n *SamNode) performHubAuthHandshake(s network.Stream, biscuitBytes []byte) (bool, error) {+func (n *SamNode) performHubAuthHandshake(s network.Stream, biscuitBytes []byte, expectedRouter peer.ID) (bool, error) {writer := msgio.NewVarintWriter(s)authFrame := &api.AuthFrame{Biscuit: biscuitBytes}data, err := proto.Marshal(authFrame)@@ -761,6 +762,52 @@ func (n *SamNode) performHubAuthHandshake(s network.Stream, biscuitBytes []byte)return false, fmt.Errorf("%w: auth failed: %s", ErrFatalAuth, resp.Error)}+ // Mutual Auth: Verify server/router biscuit+ if len(resp.Biscuit) == 0 {+ return false, fmt.Errorf("%w: remote router returned empty biscuit", ErrFatalAuth)+ }++ n.keysMu.RLock()+ var trustedKeys []ed25519.PublicKey+ for _, tk := range n.trustedKeys {+ trustedKeys = append(trustedKeys, tk.Key)+ }+ n.keysMu.RUnlock()++ if len(trustedKeys) == 0 {+ return false, fmt.Errorf("%w: no trusted control plane keys loaded", ErrFatalAuth)+ }++ // Verify the router's biscuit using the control plane keys+ b, err := identity.VerifyBiscuit(resp.Biscuit, expectedRouter, trustedKeys, n.BiscuitTimeout)+ if err != nil {+ return false, fmt.Errorf("%w: failed to verify router biscuit: %w", ErrFatalAuth, err)+ }++ // Enforce role("router"), role("router-role") or role("bootstrap") inside the biscuit+ authorizer, err := b.Authorizer(trustedKeys[0])+ if err != nil {+ return false, fmt.Errorf("authorizer instantiation failed: %w", err)+ }++ authorizer.AddCheck(biscuit.Check{Queries: []biscuit.Rule{+ {+ Body: []biscuit.Predicate{+ {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(api.RoleRouter)}},+ },+ },+ {+ Body: []biscuit.Predicate{+ {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(api.RoleBootstrap)}},+ },+ },+ }})+ authorizer.AddPolicy(api.AllowIfTruePolicy)++ if err := authorizer.Authorize(); err != nil {+ return false, fmt.Errorf("%w: remote peer lacks router authorization role: %w", ErrFatalAuth, err)+ }+return true, nil}diff --git a/internal/node/options.go b/internal/node/options.goindex dc961edc..994e8ea7 100644--- a/internal/node/options.go+++ b/internal/node/options.go@@ -26,7 +26,7 @@ import (const (DefaultMeshName = "public-mesh"DefaultDiscoveryInterval = "30s"- DefaultHubURL = "http://localhost:8080"+ DefaultHubURL = "https://bananas.sam-mesh.dev"DefaultConfigFile = "sam-node.yaml"DefaultHubConnectTimeout = 5 * time.Second)@@ -51,8 +51,6 @@ type Options struct {AutoRelayBackoff time.Duration// HubConnectTimeout bounds each hub address's dial (connect + stream open).HubConnectTimeout time.Duration-- BiscuitTimeout time.Duration}// Default applies default values to Options if they are not specified.diff --git a/internal/node/proxy_test.go b/internal/node/proxy_test.goindex f86776f2..9b57a04c 100644--- a/internal/node/proxy_test.go+++ b/internal/node/proxy_test.go@@ -68,11 +68,11 @@ func TestDatapathIntegration(t *testing.T) {AllowLoopback: true,MonitorBootstrap: 2 * time.Minute,MonitorInterval: 1 * time.Minute,- BiscuitTimeout: 500 * time.Millisecond,})if err != nil {t.Fatal(err)}+ nodeA.BiscuitTimeout = 500 * time.Millisecondif err := nodeA.Start(ctx); err != nil {t.Fatal(err)}@@ -95,11 +95,11 @@ func TestDatapathIntegration(t *testing.T) {AllowLoopback: true,MonitorBootstrap: 2 * time.Minute,MonitorInterval: 1 * time.Minute,- BiscuitTimeout: 500 * time.Millisecond,})if err != nil {t.Fatal(err)}+ nodeB.BiscuitTimeout = 500 * time.Millisecondif err := nodeB.Start(ctx); err != nil {t.Fatal(err)}@@ -272,11 +272,11 @@ func TestDatapathIntegration_Unauthenticated(t *testing.T) {AllowLoopback: true,MonitorBootstrap: 2 * time.Minute,MonitorInterval: 1 * time.Minute,- BiscuitTimeout: 500 * time.Millisecond,})if err != nil {t.Fatal(err)}+ nodeA.BiscuitTimeout = 500 * time.Millisecondif err := nodeA.Start(ctx); err != nil {t.Fatal(err)}@@ -299,11 +299,11 @@ func TestDatapathIntegration_Unauthenticated(t *testing.T) {AllowLoopback: true,MonitorBootstrap: 2 * time.Minute,MonitorInterval: 1 * time.Minute,- BiscuitTimeout: 500 * time.Millisecond,})if err != nil {t.Fatal(err)}+ nodeB.BiscuitTimeout = 500 * time.Millisecondif err := nodeB.Start(ctx); err != nil {t.Fatal(err)}@@ -421,11 +421,11 @@ func TestStdioDatapathIntegration(t *testing.T) {AllowLoopback: true,MonitorBootstrap: 2 * time.Minute,MonitorInterval: 1 * time.Minute,- BiscuitTimeout: 500 * time.Millisecond,})if err != nil {t.Fatal(err)}+ nodeA.BiscuitTimeout = 500 * time.Millisecondif err := nodeA.Start(ctx); err != nil {t.Fatal(err)}@@ -444,11 +444,11 @@ func TestStdioDatapathIntegration(t *testing.T) {AllowLoopback: true,MonitorBootstrap: 2 * time.Minute,MonitorInterval: 1 * time.Minute,- BiscuitTimeout: 500 * time.Millisecond,})if err != nil {t.Fatal(err)}+ nodeB.BiscuitTimeout = 500 * time.Millisecondif err := nodeB.Start(ctx); err != nil {t.Fatal(err)}@@ -611,11 +611,11 @@ func TestDatapathHeadersAndRoutingTable(t *testing.T) {AllowLoopback: true,MonitorBootstrap: 2 * time.Minute,MonitorInterval: 1 * time.Minute,- BiscuitTimeout: 500 * time.Millisecond,})if err != nil {t.Fatal(err)}+ nodeA.BiscuitTimeout = 500 * time.Millisecondif err := nodeA.Start(ctx); err != nil {t.Fatal(err)}@@ -638,11 +638,11 @@ func TestDatapathHeadersAndRoutingTable(t *testing.T) {AllowLoopback: true,MonitorBootstrap: 2 * time.Minute,MonitorInterval: 1 * time.Minute,- BiscuitTimeout: 500 * time.Millisecond,})if err != nil {t.Fatal(err)}+ nodeB.BiscuitTimeout = 500 * time.Millisecondif err := nodeB.Start(ctx); err != nil {t.Fatal(err)}diff --git a/internal/node/static_service_test.go b/internal/node/static_service_test.goindex 202ed303..13a76b2b 100644--- a/internal/node/static_service_test.go+++ b/internal/node/static_service_test.go@@ -2,6 +2,7 @@ package nodeimport ("context"+ "crypto/ed25519""crypto/rand""io""net/http"@@ -10,6 +11,7 @@ import ("testing""time"+ "github.com/biscuit-auth/biscuit-go/v2""github.com/google/sam/api""github.com/libp2p/go-libp2p"dht "github.com/libp2p/go-libp2p-kad-dht"@@ -34,11 +36,46 @@ func startMockLibp2pHub(t *testing.T) (peer.ID, string) {t.Fatalf("failed to create DHT on mock hub: %v", err)}+ // Generate mock control plane keys+ cpPub, cpPriv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatalf("failed to generate CP keys: %v", err)+ }++ // Mint router's biscuit token+ builder := biscuit.NewBuilder(cpPriv)+ _ = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: api.FactNode,+ IDs: []biscuit.Term{biscuit.String(h.ID().String())},+ }})+ _ = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: api.FactRole,+ IDs: []biscuit.Term{biscuit.String("router")},+ }})+ _ = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: api.FactExpiration,+ IDs: []biscuit.Term{biscuit.Date(time.Now().Add(24 * time.Hour))},+ }})+ _ = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: api.FactTargetUnrestricted,+ }})+ tok, err := builder.Build()+ if err != nil {+ t.Fatalf("failed to build mock router biscuit: %v", err)+ }+ routerBiscuit, err := tok.Serialize()+ if err != nil {+ t.Fatalf("failed to serialize mock router biscuit: %v", err)+ }+// Add dummy auth handlerh.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) {defer func() { _ = s.Close() }()- // Just send a success response to make the client happy- resp := &api.AuthResponse{Success: true}+ // Mutual auth response+ resp := &api.AuthResponse{+ Success: true,+ Biscuit: routerBiscuit,+ }data, _ := proto.Marshal(resp)writer := msgio.NewVarintWriter(s)_ = writer.WriteMsg(data)@@ -64,7 +101,7 @@ func startMockLibp2pHub(t *testing.T) (peer.ID, string) {resp := &api.EnrollResponse{BiscuitToken: []byte("mock-biscuit-token"),- HubPublicKey: make([]byte, 32),+ HubPublicKey: cpPub,HubAddresses: []string{h.Addrs()[0].String() + "/p2p/" + h.ID().String()},}data, err := proto.Marshal(resp)diff --git a/internal/router/config.go b/internal/router/config.gonew file mode 100644index 00000000..4f693bd0--- /dev/null+++ b/internal/router/config.go@@ -0,0 +1,66 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package router++import (+ "fmt"+ "time"+)++// Options holds configuration details for the sam-router.+type Options struct {+ ControlPlaneURL string+ ListenAddrs []string+ ExternalAddrs []string+ KeysSyncInterval time.Duration+ LeaseRenewInterval time.Duration+ OIDCProvider string // For OIDC enrollment+ OIDCToken string // OIDC JWT or bootstrap token for enrollment+ JWTPath string // File path containing OIDC JWT token+ KeysDBPath string // Path to save peer private key+ AllowLoopback bool+ BiscuitTimeout time.Duration+ LogVerbose bool+}++// Default sets default values for options.+func (o *Options) Default() {+ if o.ControlPlaneURL == "" {+ o.ControlPlaneURL = "http://127.0.0.1:8080"+ }+ if len(o.ListenAddrs) == 0 {+ o.ListenAddrs = []string{"/ip4/0.0.0.0/tcp/5001", "/ip6/::/tcp/5001"}+ }+ if o.KeysSyncInterval <= 0 {+ o.KeysSyncInterval = 5 * time.Minute+ }+ if o.LeaseRenewInterval <= 0 {+ o.LeaseRenewInterval = 30 * time.Second+ }+ if o.KeysDBPath == "" {+ o.KeysDBPath = "router.key"+ }+ if o.BiscuitTimeout <= 0 {+ o.BiscuitTimeout = 100 * time.Millisecond+ }+}++// Validate ensures options are valid.+func (o *Options) Validate() error {+ if o.ControlPlaneURL == "" {+ return fmt.Errorf("ControlPlaneURL must be specified")+ }+ return nil+}diff --git a/internal/router/router.go b/internal/router/router.gonew file mode 100644index 00000000..8919718a--- /dev/null+++ b/internal/router/router.go@@ -0,0 +1,681 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package router++import (+ "bytes"+ "context"+ "crypto/ed25519"+ "errors"+ "fmt"+ "io"+ "net"+ "net/http"+ "os"+ "path/filepath"+ "strings"+ "sync"+ "sync/atomic"+ "time"++ "github.com/biscuit-auth/biscuit-go/v2"+ "github.com/google/sam/api"+ "github.com/google/sam/internal/identity"+ golog "github.com/ipfs/go-log/v2"+ "github.com/libp2p/go-libp2p"+ 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/host"+ "github.com/libp2p/go-libp2p/core/network"+ "github.com/libp2p/go-libp2p/core/peer"+ "github.com/libp2p/go-libp2p/p2p/net/connmgr"+ "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"+ libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"+ "github.com/libp2p/go-msgio"+ "github.com/multiformats/go-multiaddr"+ madns "github.com/multiformats/go-multiaddr-dns"+ "google.golang.org/protobuf/proto"+)++var logger = golog.Logger("sam-router")++const (+ LowWaterMark = 100+ HighWaterMark = 400+ ConnGracePeriod = 1 * time.Minute+)++type relayACL struct {+ r *Router+}++func (a *relayACL) AllowReserve(p peer.ID, addr multiaddr.Multiaddr) bool {+ _, ok := a.r.authenticatedPeers.Load(p)+ if !ok {+ logger.Debugf("[Relay] Rejecting reservation for %s: not authenticated", p)+ }+ return ok+}++func (a *relayACL) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest peer.ID) bool {+ _, ok := a.r.authenticatedPeers.Load(dest)+ if !ok {+ logger.Debugf("[Relay] Rejecting connect from %s to %s: dest not authenticated", src, dest)+ }+ return ok+}++// Router represents a libp2p bootstrap/relay node.+type Router struct {+ config Options+ Host host.Host+ DHT *dht.IpfsDHT+ PubSub *pubsub.PubSub+ EventTopic *pubsub.Topic+ authenticatedPeers sync.Map++ // Keys & Identity+ biscuitToken []byte+ trustedPublicKeys []ed25519.PublicKey+ keysMu sync.RWMutex+ privKey crypto.PrivKey++ // Control contexts+ ctx context.Context+ cancel context.CancelFunc+ wg sync.WaitGroup+ isReady atomic.Bool+ shutdown bool+}++// NewRouter initializes the router.+func NewRouter(ctx context.Context, config Options) (*Router, error) {+ config.Default()+ if err := config.Validate(); err != nil {+ return nil, err+ }++ ctx, cancel := context.WithCancel(ctx)++ return &Router{+ config: config,+ ctx: ctx,+ cancel: cancel,+ }, nil+}++// Start performs enrollment, syncs keys, launches libp2p host, and starts tasks.+func (r *Router) Start() error {+ // 1. Load or Generate persistent identity key+ priv, err := getOrGeneratePeerKey(r.config.KeysDBPath)+ if err != nil {+ return fmt.Errorf("failed to load/generate peer identity: %w", err)+ }+ r.privKey = priv++ peerID, err := peer.IDFromPrivateKey(priv)+ if err != nil {+ return err+ }++ // 2. Enroll with Control Plane+ if r.config.OIDCToken == "" && r.config.JWTPath != "" {+ tokenData, err := os.ReadFile(r.config.JWTPath)+ if err != nil {+ return fmt.Errorf("failed to read JWT from path %s: %w", r.config.JWTPath, err)+ }+ r.config.OIDCToken = strings.TrimSpace(string(tokenData))+ }++ if r.config.OIDCToken != "" {+ logger.Infof("Enrolling router %s with Control Plane at %s...", peerID, r.config.ControlPlaneURL)+ if err := r.enroll(peerID); err != nil {+ return fmt.Errorf("failed router enrollment: %w", err)+ }+ } else {+ logger.Warn("Router started without OIDCToken/JWTPath. Enrollment bypassed. Expecting local keys sync.")+ }++ // 3. Initial Keys Sync+ if err := r.syncKeys(); err != nil {+ return fmt.Errorf("failed initial keys sync: %w", err)+ }++ // 4. Initialize libp2p host+ cm, err := connmgr.NewConnManager(LowWaterMark, HighWaterMark, connmgr.WithGracePeriod(ConnGracePeriod))+ if err != nil {+ return fmt.Errorf("failed to create connection manager: %w", err)+ }++ p2pOpts := []libp2p.Option{+ libp2p.Identity(r.privKey),+ libp2p.DefaultTransports,+ libp2p.ListenAddrStrings(r.config.ListenAddrs...),+ libp2p.Security(libp2ptls.ID, libp2ptls.New),+ libp2p.ConnectionManager(cm),+ libp2p.EnableAutoNATv2(),+ libp2p.EnableNATService(),+ libp2p.AddrsFactory(func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {+ if r.config.AllowLoopback {+ return addrs+ }+ var filtered []multiaddr.Multiaddr+ for _, addr := range addrs {+ if !isLoopbackOrLinkLocal(addr) {+ filtered = append(filtered, addr)+ }+ }+ return filtered+ }),+ }++ hostNode, err := libp2p.New(p2pOpts...)+ if err != nil {+ return err+ }+ r.Host = hostNode++ // Setup DHT+ kadDHT, err := dht.New(r.ctx, hostNode, dht.Mode(dht.ModeServer), dht.ProtocolPrefix("/sam"))+ if err != nil {+ _ = hostNode.Close()+ return err+ }+ r.DHT = kadDHT++ if err = kadDHT.Bootstrap(r.ctx); err != nil {+ _ = hostNode.Close()+ return err+ }++ // Setup Relay+ _, err = relay.New(hostNode, relay.WithACL(&relayACL{r: r}))+ if err != nil {+ _ = hostNode.Close()+ return err+ }++ // Setup PubSub+ ps, err := pubsub.NewGossipSub(r.ctx, hostNode)+ if err != nil {+ _ = hostNode.Close()+ return err+ }+ r.PubSub = ps++ topic, err := ps.Join(api.GossipEvents)+ if err != nil {+ _ = hostNode.Close()+ return err+ }+ r.EventTopic = topic++ // Setup authentication stream handler+ hostNode.SetStreamHandler(api.AuthProtocolID, r.HandleAuthHandshake)++ // Clean authenticated status on peer disconnection+ hostNode.Network().Notify(&network.NotifyBundle{+ DisconnectedF: func(n network.Network, c network.Conn) {+ p := c.RemotePeer()+ if len(hostNode.Network().ConnsToPeer(p)) == 0 {+ r.authenticatedPeers.Delete(p)+ }+ },+ })++ // 5. Start background routines+ r.wg.Add(3)+ go r.runLeaseRenewalLoop()+ go r.runKeysSyncLoop()+ go r.runFederationLoop()++ r.isReady.Store(true)+ logger.Infof("Router Online. PeerID: %s, ListenAddrs: %v", r.Host.ID(), r.Host.Addrs())+ return nil+}++func (r *Router) enroll(peerID peer.ID) error {+ req := &api.EnrollRequest{+ Jwt: r.config.OIDCToken,+ PeerId: peerID.String(),+ }+ data, err := proto.Marshal(req)+ if err != nil {+ return err+ }++ client := &http.Client{Timeout: 30 * time.Second}+ resp, err := client.Post(r.config.ControlPlaneURL+"/register", "application/x-protobuf", bytes.NewReader(data))+ if err != nil {+ return err+ }+ defer func() { _ = resp.Body.Close() }()++ if resp.StatusCode != http.StatusOK {+ body, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("enrollment response status %s: %s", resp.Status, string(body))+ }++ body, err := io.ReadAll(resp.Body)+ if err != nil {+ return err+ }++ var enrollResp api.EnrollResponse+ if err := proto.Unmarshal(body, &enrollResp); err != nil {+ return err+ }++ r.biscuitToken = enrollResp.BiscuitToken+ r.keysMu.Lock()+ r.trustedPublicKeys = []ed25519.PublicKey{enrollResp.HubPublicKey}+ r.keysMu.Unlock()++ return nil+}++func (r *Router) syncKeys() error {+ client := &http.Client{Timeout: 10 * time.Second}+ resp, err := client.Get(r.config.ControlPlaneURL + "/keys")+ if err != nil {+ return err+ }+ defer func() { _ = resp.Body.Close() }()++ if resp.StatusCode != http.StatusOK {+ return fmt.Errorf("failed to sync keys, status %s", resp.Status)+ }++ body, err := io.ReadAll(resp.Body)+ if err != nil {+ return err+ }++ var keysResp api.KeysResponse+ if err := proto.Unmarshal(body, &keysResp); err != nil {+ return err+ }++ r.keysMu.Lock()+ var newKeys []ed25519.PublicKey+ for _, kb := range keysResp.PublicKeys {+ if len(kb) == ed25519.PublicKeySize {+ newKeys = append(newKeys, ed25519.PublicKey(kb))+ }+ }+ r.trustedPublicKeys = newKeys+ r.keysMu.Unlock()++ logger.Debugf("Synced %d valid public keys from control plane", len(newKeys))+ return nil+}++func (r *Router) getTrustedPublicKeys() []ed25519.PublicKey {+ r.keysMu.RLock()+ defer r.keysMu.RUnlock()+ return append([]ed25519.PublicKey(nil), r.trustedPublicKeys...)+}++func (r *Router) runKeysSyncLoop() {+ defer r.wg.Done()+ ticker := time.NewTicker(r.config.KeysSyncInterval)+ defer ticker.Stop()++ for {+ select {+ case <-ticker.C:+ if err := r.syncKeys(); err != nil {+ logger.Errorf("Failed periodic keys sync: %v", err)+ }+ case <-r.ctx.Done():+ return+ }+ }+}++func (r *Router) runLeaseRenewalLoop() {+ defer r.wg.Done()+ ticker := time.NewTicker(r.config.LeaseRenewInterval)+ defer ticker.Stop()++ // Initial renewal after host is online+ r.renewLease()++ for {+ select {+ case <-ticker.C:+ r.renewLease()+ case <-r.ctx.Done():+ return+ }+ }+}++func (r *Router) renewLease() {+ if len(r.biscuitToken) == 0 {+ logger.Warn("Cannot renew lease: router is not enrolled (no biscuit)")+ return+ }++ var addrs []string+ if len(r.config.ExternalAddrs) > 0 {+ for _, addr := range r.config.ExternalAddrs {+ addrs = append(addrs, addr+"/p2p/"+r.Host.ID().String())+ }+ } else {+ for _, addr := range r.Host.Addrs() {+ addrs = append(addrs, addr.String()+"/p2p/"+r.Host.ID().String())+ }+ }++ req := &api.RouterLeaseRequest{+ PeerId: r.Host.ID().String(),+ Addresses: addrs,+ Biscuit: r.biscuitToken,+ }+ data, _ := proto.Marshal(req)++ client := &http.Client{Timeout: 10 * time.Second}+ resp, err := client.Post(r.config.ControlPlaneURL+"/routers/lease", "application/x-protobuf", bytes.NewReader(data))+ if err != nil {+ logger.Errorf("Failed to renew lease with control plane: %v", err)+ return+ }+ defer func() { _ = resp.Body.Close() }()++ if resp.StatusCode != http.StatusOK {+ body, _ := io.ReadAll(resp.Body)+ logger.Errorf("Control plane lease renewal rejected, status %s: %s", resp.Status, string(body))+ return+ }++ var leaseResp api.RouterLeaseResponse+ body, _ := io.ReadAll(resp.Body)+ if err := proto.Unmarshal(body, &leaseResp); err != nil {+ logger.Errorf("Failed to parse lease response: %v", err)+ return+ }++ if !leaseResp.Success {+ logger.Errorf("Lease renewal failed: %s", leaseResp.Error)+ } else {+ logger.Debugf("Lease renewed successfully. Expires at: %s", time.Unix(leaseResp.ExpiresAt, 0))+ }+}++func (r *Router) runFederationLoop() {+ defer r.wg.Done()+ ticker := time.NewTicker(30 * time.Second)+ defer ticker.Stop()++ r.connectBootstrapRouters()++ for {+ select {+ case <-ticker.C:+ r.connectBootstrapRouters()+ case <-r.ctx.Done():+ return+ }+ }+}++func (r *Router) connectBootstrapRouters() {+ client := &http.Client{Timeout: 10 * time.Second}+ resp, err := client.Get(r.config.ControlPlaneURL + "/info")+ if err != nil {+ logger.Errorf("[Federation] Failed to fetch router info: %v", err)+ return+ }+ defer func() { _ = resp.Body.Close() }()++ if resp.StatusCode != http.StatusOK {+ logger.Errorf("[Federation] GET /info returned status %d", resp.StatusCode)+ return+ }++ body, err := io.ReadAll(resp.Body)+ if err != nil {+ return+ }++ var info api.HubInfoResponse+ if err := proto.Unmarshal(body, &info); err != nil {+ return+ }++ for _, addrStr := range info.HubAddresses {+ ma, err := multiaddr.NewMultiaddr(addrStr)+ if err != nil {+ continue+ }++ resolvedAddrs, err := madns.DefaultResolver.Resolve(r.ctx, ma)+ if err != nil {+ resolvedAddrs = []multiaddr.Multiaddr{ma}+ }++ for _, resolved := range resolvedAddrs {+ pi, err := peer.AddrInfoFromP2pAddr(resolved)+ if err != nil || pi.ID == r.Host.ID() {+ continue+ }++ if len(r.Host.Network().ConnsToPeer(pi.ID)) == 0 {+ logger.Infof("[Federation] Connecting to peer router: %s via %s", pi.ID, resolved)+ // Create a timeout context+ connectCtx, cancel := context.WithTimeout(r.ctx, 10*time.Second)+ if err := r.Host.Connect(connectCtx, *pi); err == nil {+ // Initiate stream to perform mutual auth handshake+ s, err := r.Host.NewStream(connectCtx, pi.ID, api.AuthProtocolID)+ if err == nil {+ if err := r.performMutualAuth(s); err != nil {+ logger.Errorf("[Federation] Mutual auth handshake failed with %s: %v", pi.ID, err)+ _ = s.Reset()+ } else {+ logger.Infof("[Federation] Mutually authenticated with peer router %s", pi.ID)+ _ = s.Close()+ }+ } else {+ logger.Errorf("[Federation] Failed to open auth stream to %s: %v", pi.ID, err)+ }+ } else {+ logger.Errorf("[Federation] Failed to connect to %s: %v", pi.ID, err)+ }+ cancel()+ }+ }+ }+}++// HandleAuthHandshake processes incoming auth connections.+// It is part of mutual auth:+// 1. Receives client's Biscuit.+// 2. Verifies it against CP public keys.+// 3. Responds with success and the router's own Biscuit.+func (r *Router) HandleAuthHandshake(s network.Stream) {+ defer func() { _ = s.Close() }()+ remotePeer := s.Conn().RemotePeer()++ reader := msgio.NewVarintReaderSize(s, 1024*64)+ msg, err := reader.ReadMsg()+ if err != nil {+ logger.Errorf("[AuthN] Failed to read handshake from %s: %v", remotePeer, err)+ return+ }+ defer reader.ReleaseMsg(msg)++ var exchange api.AuthFrame+ if err := proto.Unmarshal(msg, &exchange); err != nil {+ logger.Warnf("[AuthN] Invalid protobuf from %s", remotePeer)+ return+ }++ // Verify Biscuit+ _, err = identity.VerifyBiscuit(exchange.Biscuit, remotePeer, r.getTrustedPublicKeys(), r.config.BiscuitTimeout)+ if err != nil {+ logger.Warnf("[AuthN] Authorization failed for peer %s: %v", remotePeer, err)+ _ = s.Reset()+ return+ }++ r.authenticatedPeers.Store(remotePeer, true)+ logger.Infof("[AuthN] Successfully authenticated peer %s", remotePeer)++ // Send mutual response (our biscuit)+ writer := msgio.NewVarintWriter(s)+ resp := &api.AuthResponse{+ Success: true,+ Biscuit: r.biscuitToken,+ }+ respBytes, _ := proto.Marshal(resp)+ if err := writer.WriteMsg(respBytes); err != nil {+ logger.Errorf("[AuthN] Failed to write mutual ACK to %s: %v", remotePeer, err)+ }+}++// performMutualAuth initiates client-side mutual authentication handshake.+func (r *Router) performMutualAuth(s network.Stream) error {+ remotePeer := s.Conn().RemotePeer()+ _ = s.SetDeadline(time.Now().Add(5 * time.Second))++ // Send our biscuit+ writer := msgio.NewVarintWriter(s)+ authFrame := &api.AuthFrame{Biscuit: r.biscuitToken}+ data, _ := proto.Marshal(authFrame)+ if err := writer.WriteMsg(data); err != nil {+ return fmt.Errorf("write mutual auth frame: %w", err)+ }++ // Read response (success + remote biscuit)+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ respMsg, err := reader.ReadMsg()+ if err != nil {+ return fmt.Errorf("read mutual auth response: %w", err)+ }+ defer reader.ReleaseMsg(respMsg)++ var resp api.AuthResponse+ if err := proto.Unmarshal(respMsg, &resp); err != nil {+ return fmt.Errorf("unmarshal mutual auth response: %w", err)+ }++ if !resp.Success {+ return fmt.Errorf("mutual auth handshake rejected: %s", resp.Error)+ }++ trustedKeys := r.getTrustedPublicKeys()+ if len(trustedKeys) == 0 {+ return fmt.Errorf("no trusted control plane keys loaded")+ }++ // Verify remote biscuit+ b, err := identity.VerifyBiscuit(resp.Biscuit, remotePeer, trustedKeys, r.config.BiscuitTimeout)+ if err != nil {+ return fmt.Errorf("failed to verify peer router biscuit: %w", err)+ }++ // Enforce role("router"), role("router-role") or role("bootstrap") inside the biscuit+ authorizer, err := b.Authorizer(trustedKeys[0])+ if err != nil {+ return fmt.Errorf("authorizer instantiation failed: %w", err)+ }++ authorizer.AddCheck(biscuit.Check{Queries: []biscuit.Rule{+ {+ Body: []biscuit.Predicate{+ {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(api.RoleRouter)}},+ },+ },+ {+ Body: []biscuit.Predicate{+ {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(api.RoleBootstrap)}},+ },+ },+ }})+ authorizer.AddPolicy(api.AllowIfTruePolicy)++ if err := authorizer.Authorize(); err != nil {+ return fmt.Errorf("remote peer lacks router authorization role: %w", err)+ }++ r.authenticatedPeers.Store(remotePeer, true)+ return nil+}++// Close closes the underlying p2p host and keyring db.+func (r *Router) Close() error {+ r.shutdown = true+ r.cancel()++ var errs []error+ if r.DHT != nil {+ if err := r.DHT.Close(); err != nil {+ errs = append(errs, err)+ }+ }+ if r.Host != nil {+ if err := r.Host.Close(); err != nil {+ errs = append(errs, err)+ }+ }+ r.wg.Wait()+ return errors.Join(errs...)+}++func getOrGeneratePeerKey(keyPath string) (crypto.PrivKey, error) {+ if err := os.MkdirAll(filepath.Dir(keyPath), 0755); err != nil {+ return nil, err+ }++ if _, err := os.Stat(keyPath); err == nil {+ data, err := os.ReadFile(keyPath)+ if err != nil {+ return nil, err+ }+ return crypto.UnmarshalPrivateKey(data)+ }++ priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ if err != nil {+ return nil, err+ }+ data, err := crypto.MarshalPrivateKey(priv)+ if err != nil {+ return nil, err+ }+ if err := os.WriteFile(keyPath, data, 0600); err != nil {+ return nil, err+ }+ return priv, nil+}++func isLoopbackOrLinkLocal(addr multiaddr.Multiaddr) bool {+ for _, proto := range addr.Protocols() {+ if proto.Code == multiaddr.P_IP4 || proto.Code == multiaddr.P_IP6 {+ value, err := addr.ValueForProtocol(proto.Code)+ if err == nil {+ ip := net.ParseIP(value)+ if ip != nil {+ if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {+ return true+ }+ }+ }+ }+ }+ return false+}diff --git a/internal/router/router_test.go b/internal/router/router_test.gonew file mode 100644index 00000000..6db2084e--- /dev/null+++ b/internal/router/router_test.go@@ -0,0 +1,392 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package router++import (+ "bytes"+ "context"+ "crypto/ed25519"+ "crypto/rand"+ "crypto/rsa"+ "encoding/base64"+ "encoding/json"+ "io"+ "math/big"+ "net/http"+ "net/http/httptest"+ "path/filepath"+ "testing"+ "time"++ "github.com/golang-jwt/jwt/v5"+ "github.com/google/sam/api"+ "github.com/google/sam/internal/controlplane"+ "github.com/google/sam/internal/identity"+ "github.com/google/sam/internal/storage"+ "github.com/libp2p/go-libp2p"+ "github.com/libp2p/go-libp2p/core/peer"+ "github.com/libp2p/go-msgio"+ "github.com/multiformats/go-multiaddr"+ "google.golang.org/protobuf/proto"+)++func startCustomMockOIDC(t *testing.T) (string, func(claims map[string]interface{}) string) {+ t.Helper()+ privKey, err := rsa.GenerateKey(rand.Reader, 2048)+ if err != nil {+ t.Fatalf("failed to generate rsa key: %v", err)+ }++ mux := http.NewServeMux()+ srv := httptest.NewServer(mux)+ t.Cleanup(srv.Close)++ issuer := srv.URL++ mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "issuer": issuer,+ "jwks_uri": issuer + "/keys",+ })+ })++ mux.HandleFunc("/keys", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "keys": []map[string]interface{}{+ {+ "kty": "RSA",+ "alg": "RS256",+ "use": "sig",+ "kid": "mock-key",+ "n": base64.RawURLEncoding.EncodeToString(privKey.N.Bytes()),+ "e": base64.RawURLEncoding.EncodeToString(big.NewInt(int64(privKey.E)).Bytes()),+ },+ },+ })+ })++ mintToken := func(customClaims map[string]interface{}) string {+ claims := jwt.MapClaims{+ "iss": issuer,+ "aud": "sam-mesh-audience",+ "exp": time.Now().Add(time.Hour).Unix(),+ }+ for k, v := range customClaims {+ claims[k] = v+ }+ token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)+ token.Header["kid"] = "mock-key"+ jwtStr, err := token.SignedString(privKey)+ if err != nil {+ t.Fatalf("failed to sign jwt: %v", err)+ }+ return jwtStr+ }++ return issuer, mintToken+}++func setupControlPlane(t *testing.T, oidcIssuer string) (*controlplane.Server, storage.Store, string) {+ t.Helper()+ tempDir := t.TempDir()+ dbPath := filepath.Join(tempDir, "control-plane.db")++ store, err := storage.NewSQLStore("sqlite", dbPath)+ if err != nil {+ t.Fatalf("failed to create store: %v", err)+ }++ opts := controlplane.Options{+ ListenAddr: "127.0.0.1:0", // Auto-allocate+ DriverName: "sqlite",+ DataSourceName: dbPath,+ OIDCIssuer: oidcIssuer,+ AllowedAudiences: []string{"sam-mesh-audience"},+ LeaseDuration: 5 * time.Second,+ KeyRotationInterval: 12 * time.Hour,+ KeyGracePeriod: 10 * time.Minute,+ InsecureSkipTLSVerify: true,+ BiscuitTimeout: 1 * time.Second,+ }++ // Bootstrap Policy granting 'router' role to group 'routers'+ policy := &api.PolicyConfig{+ Version: "v1alpha1",+ Bindings: []api.Binding{+ {Role: "router", Members: []string{"group:routers"}},+ {Role: "user-role", Members: []string{"group:users"}},+ },+ Roles: map[string]api.RolePolicy{+ "router": {+ AllowedServices: []string{"*"},+ AllowedTargets: []string{"*"},+ },+ "user-role": {+ AllowedServices: []string{"mcp://read"},+ },+ },+ }+ if err := store.SavePolicy(context.Background(), policy); err != nil {+ t.Fatalf("failed to save policy: %v", err)+ }++ srv, err := controlplane.NewServer(opts, store)+ if err != nil {+ t.Fatalf("failed to create CP server: %v", err)+ }++ if err := srv.Start(); err != nil {+ t.Fatalf("failed to start CP: %v", err)+ }++ return srv, store, "http://" + srv.Addr()+}++func TestRouterIntegration(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-1",+ "groups": []string{"routers"},+ })++ // 1. Create and Start Router+ rOpts := Options{+ ControlPlaneURL: cpURL,+ ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},+ KeysSyncInterval: 2 * time.Second,+ LeaseRenewInterval: 2 * 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() }()++ // Verify lease renewal populated the database+ time.Sleep(3 * time.Second) // wait for lease renewal loop to fire+ activeRouters, err := cpStore.GetActiveRouters(context.Background())+ if err != nil {+ t.Fatalf("failed to get active routers from store: %v", err)+ }+ if len(activeRouters) != 1 {+ t.Fatalf("expected 1 active router in DB, got %d", len(activeRouters))+ }+ if activeRouters[0].PeerID != r.Host.ID().String() {+ t.Errorf("router peer ID mismatch in DB lease")+ }++ // 2. Perform client connection and Mutual Auth Handshake+ // Boot a client node and register it on control plane to get a Biscuit+ nodeJWT := mintToken(map[string]interface{}{+ "sub": "node-user-1",+ "groups": []string{"users"},+ })++ nodeKeyPath := filepath.Join(tempDir, "node.key")+ nodePrivKey, err := getOrGeneratePeerKey(nodeKeyPath)+ if err != nil {+ t.Fatal(err)+ }+ nodePeerID, _ := peer.IDFromPrivateKey(nodePrivKey)++ // Fetch biscuit for client node via Control Plane HTTP client+ client := &http.Client{Timeout: 5 * time.Second}+ enrollNodeReq := &api.EnrollRequest{+ Jwt: nodeJWT,+ PeerId: nodePeerID.String(),+ }+ reqData, _ := proto.Marshal(enrollNodeReq)+ resp, err := client.Post(cpURL+"/register", "application/x-protobuf", bytes.NewReader(reqData))+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = resp.Body.Close() }()+ body, _ := io.ReadAll(resp.Body)+ var enrollNodeResp api.EnrollResponse+ _ = proto.Unmarshal(body, &enrollNodeResp)++ // Now create a client libp2p Host+ clientHost, err := libp2p.New(+ libp2p.Identity(nodePrivKey),+ libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"),+ )+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = clientHost.Close() }()++ // Connect to Router+ routerAddr := r.Host.Addrs()[0]+ routerInfo, err := peer.AddrInfoFromP2pAddr(routerAddr.Encapsulate(multiaddr.StringCast("/p2p/" + r.Host.ID().String())))+ if err != nil {+ t.Fatal(err)+ }++ if err := clientHost.Connect(context.Background(), *routerInfo); err != nil {+ t.Fatalf("failed to connect client to router: %v", err)+ }++ // Open mutual auth stream+ s, err := clientHost.NewStream(context.Background(), routerInfo.ID, api.AuthProtocolID)+ if err != nil {+ t.Fatalf("failed to open auth stream: %v", err)+ }+ defer func() { _ = s.Close() }()++ // Send Node Biscuit+ writer := msgio.NewVarintWriter(s)+ authFrame := &api.AuthFrame{Biscuit: enrollNodeResp.BiscuitToken}+ data, _ := proto.Marshal(authFrame)+ if err := writer.WriteMsg(data); err != nil {+ t.Fatalf("failed to write auth frame: %v", err)+ }++ // Read Router response+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ respMsg, err := reader.ReadMsg()+ if err != nil {+ t.Fatalf("failed to read response: %v", err)+ }+ defer reader.ReleaseMsg(respMsg)++ var authResp api.AuthResponse+ if err := proto.Unmarshal(respMsg, &authResp); err != nil {+ t.Fatalf("failed to unmarshal response: %v", err)+ }++ if !authResp.Success {+ t.Fatalf("mutual auth rejected: %s", authResp.Error)+ }++ // Verify Router Biscuit+ // Fetch CP public keys to verify router biscuit+ validKeys, _ := cpStore.GetAllValidKeys(context.Background())+ var cpPubKeys []ed25519.PublicKey+ for _, k := range validKeys {+ cpPubKeys = append(cpPubKeys, k.Public)+ }++ _, err = identity.VerifyBiscuit(authResp.Biscuit, r.Host.ID(), cpPubKeys, 1*time.Second)+ if err != nil {+ t.Fatalf("failed client-side verification of router biscuit: %v", err)+ }++ t.Log("Mutual Authentication completed successfully!")+}++func TestRouterFederation(t *testing.T) {+ issuer, mintToken := startCustomMockOIDC(t)+ cp, cpStore, cpURL := setupControlPlane(t, issuer)+ defer func() {+ _ = cp.Close()+ _ = cpStore.Close()+ }()++ tempDir := t.TempDir()++ // Mint token for Router 1+ router1JWT := mintToken(map[string]interface{}{+ "sub": "router-1",+ "groups": []string{"routers"},+ })+ // Mint token for Router 2+ router2JWT := mintToken(map[string]interface{}{+ "sub": "router-2",+ "groups": []string{"routers"},+ })++ r1KeyPath := filepath.Join(tempDir, "router1.key")+ r1, err := NewRouter(context.Background(), Options{+ ControlPlaneURL: cpURL,+ ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},+ KeysSyncInterval: 2 * time.Second,+ LeaseRenewInterval: 2 * time.Second,+ OIDCToken: router1JWT,+ KeysDBPath: r1KeyPath,+ AllowLoopback: true,+ BiscuitTimeout: 1 * time.Second,+ })+ if err != nil {+ t.Fatal(err)+ }+ if err := r1.Start(); err != nil {+ t.Fatal(err)+ }+ defer func() { _ = r1.Close() }()++ r2KeyPath := filepath.Join(tempDir, "router2.key")+ r2, err := NewRouter(context.Background(), Options{+ ControlPlaneURL: cpURL,+ ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},+ KeysSyncInterval: 2 * time.Second,+ LeaseRenewInterval: 2 * time.Second,+ OIDCToken: router2JWT,+ KeysDBPath: r2KeyPath,+ AllowLoopback: true,+ BiscuitTimeout: 1 * time.Second,+ })+ if err != nil {+ t.Fatal(err)+ }+ if err := r2.Start(); err != nil {+ t.Fatal(err)+ }+ defer func() { _ = r2.Close() }()++ // Wait for both routers to renew their lease so they are both in control plane DB+ time.Sleep(3 * time.Second)++ // Trigger connection sync manually to form the federation link+ r1.connectBootstrapRouters()+ r2.connectBootstrapRouters()++ // Wait a moment for stream handshakes to complete+ time.Sleep(1 * time.Second)++ // Assert that Router 1 connected to Router 2+ if len(r1.Host.Network().ConnsToPeer(r2.Host.ID())) == 0 {+ t.Errorf("Router 1 is not connected to Router 2")+ }++ // Assert that Router 1 mutually authenticated Router 2+ if _, authenticated := r1.authenticatedPeers.Load(r2.Host.ID()); !authenticated {+ t.Errorf("Router 2 is not authenticated in Router 1's peers")+ }++ // Assert that Router 2 mutually authenticated Router 1+ if _, authenticated := r2.authenticatedPeers.Load(r1.Host.ID()); !authenticated {+ t.Errorf("Router 1 is not authenticated in Router 2's peers")+ }+}diff --git a/internal/storage/sql_store.go b/internal/storage/sql_store.gonew file mode 100644index 00000000..5f3582a5--- /dev/null+++ b/internal/storage/sql_store.go@@ -0,0 +1,444 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package storage++import (+ "context"+ "crypto/ed25519"+ "database/sql"+ "encoding/json"+ "fmt"+ "strings"+ "time"++ "github.com/google/sam/api"+ "gopkg.in/yaml.v2"++ // Register PG and SQLite drivers+ _ "github.com/jackc/pgx/v5/stdlib"+ _ "modernc.org/sqlite"+)++// SQLStore implements Store interface using database/sql.+type SQLStore struct {+ db *sql.DB+ driverName string+}++// NewSQLStore creates a new SQLStore, connects to the database, and initializes tables.+func NewSQLStore(driverName, dataSourceName string) (*SQLStore, error) {+ actualDriver := driverName+ if driverName == "postgres" || driverName == "postgresql" {+ actualDriver = "pgx"+ }+ if driverName == "sqlite" && !strings.Contains(dataSourceName, "?") {+ // SQLite driver options are configured via DSN query parameters so they apply+ // to all connections in the connection pool. We default to WAL mode for write+ // concurrency and busy_timeout to prevent SQLITE_BUSY locking errors.+ // Callers (e.g. integration tests that copy DB files) can override this by+ // passing a DSN containing custom query parameter parameters (e.g. "?_pragma=journal_mode(DELETE)&_pragma=busy_timeout(5000)").+ dataSourceName = dataSourceName + "?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)"+ }+ db, err := sql.Open(actualDriver, dataSourceName)+ if err != nil {+ return nil, fmt.Errorf("failed to open database: %w", err)+ }++ store := &SQLStore{+ db: db,+ driverName: driverName,+ }++ if err := store.initSchema(); err != nil {+ _ = db.Close()+ return nil, fmt.Errorf("failed to initialize schema: %w", err)+ }++ return store, nil+}++func (s *SQLStore) isPostgres() bool {+ return strings.Contains(s.driverName, "postgres") || strings.Contains(s.driverName, "pgx")+}++func (s *SQLStore) initSchema() error {+ var keyringSchema, nodesSchema, routersSchema, policiesSchema string++ if s.isPostgres() {+ keyringSchema = `+ CREATE TABLE IF NOT EXISTS keyring (+ id SERIAL PRIMARY KEY,+ private_key BYTEA NOT NULL,+ public_key BYTEA NOT NULL UNIQUE,+ expiration TIMESTAMP WITH TIME ZONE,+ created_at TIMESTAMP WITH TIME ZONE NOT NULL+ )`+ nodesSchema = `+ CREATE TABLE IF NOT EXISTS nodes (+ peer_id VARCHAR(255) PRIMARY KEY,+ biscuit_token BYTEA NOT NULL,+ enrolled_at TIMESTAMP WITH TIME ZONE NOT NULL,+ expires_at TIMESTAMP WITH TIME ZONE NOT NULL,+ banned BOOLEAN DEFAULT FALSE NOT NULL+ )`+ routersSchema = `+ CREATE TABLE IF NOT EXISTS routers (+ peer_id VARCHAR(255) PRIMARY KEY,+ multiaddresses TEXT NOT NULL,+ last_lease_renewal TIMESTAMP WITH TIME ZONE NOT NULL,+ expires_at TIMESTAMP WITH TIME ZONE NOT NULL+ )`+ policiesSchema = `+ CREATE TABLE IF NOT EXISTS policies (+ id VARCHAR(255) PRIMARY KEY,+ content TEXT NOT NULL,+ updated_at TIMESTAMP WITH TIME ZONE NOT NULL+ )`+ } else {+ keyringSchema = `+ CREATE TABLE IF NOT EXISTS keyring (+ id INTEGER PRIMARY KEY AUTOINCREMENT,+ private_key BLOB NOT NULL,+ public_key BLOB NOT NULL UNIQUE,+ expiration DATETIME,+ created_at DATETIME NOT NULL+ )`+ nodesSchema = `+ CREATE TABLE IF NOT EXISTS nodes (+ peer_id TEXT PRIMARY KEY,+ biscuit_token BLOB NOT NULL,+ enrolled_at DATETIME NOT NULL,+ expires_at DATETIME NOT NULL,+ banned BOOLEAN DEFAULT FALSE NOT NULL+ )`+ routersSchema = `+ CREATE TABLE IF NOT EXISTS routers (+ peer_id TEXT PRIMARY KEY,+ multiaddresses TEXT NOT NULL,+ last_lease_renewal DATETIME NOT NULL,+ expires_at DATETIME NOT NULL+ )`+ policiesSchema = `+ CREATE TABLE IF NOT EXISTS policies (+ id TEXT PRIMARY KEY,+ content TEXT NOT NULL,+ updated_at DATETIME NOT NULL+ )`+ }++ schemas := []string{keyringSchema, nodesSchema, routersSchema, policiesSchema}+ for _, schema := range schemas {+ if _, err := s.db.Exec(schema); err != nil {+ return err+ }+ }+ return nil+}++func (s *SQLStore) rebind(query string) string {+ if !s.isPostgres() {+ return query+ }+ var result strings.Builder+ paramIndex := 1+ for _, char := range query {+ if char == '?' {+ fmt.Fprintf(&result, "$%d", paramIndex)+ paramIndex+++ } else {+ result.WriteRune(char)+ }+ }+ return result.String()+}++// GetCurrentKey implements Store.+func (s *SQLStore) GetCurrentKey(ctx context.Context) (ed25519.PrivateKey, ed25519.PublicKey, error) {+ query := s.rebind(`SELECT private_key, public_key FROM keyring WHERE expiration IS NULL ORDER BY id DESC LIMIT 1`)+ var privBytes, pubBytes []byte+ err := s.db.QueryRowContext(ctx, query).Scan(&privBytes, &pubBytes)+ if err == sql.ErrNoRows {+ return nil, nil, ErrNotFound+ }+ if err != nil {+ return nil, nil, err+ }+ return ed25519.PrivateKey(privBytes), ed25519.PublicKey(pubBytes), nil+}++// GetAllValidKeys implements Store.+func (s *SQLStore) GetAllValidKeys(ctx context.Context) ([]KeyPair, error) {+ query := s.rebind(`SELECT private_key, public_key, expiration FROM keyring WHERE expiration IS NULL OR expiration > ?`)+ rows, err := s.db.QueryContext(ctx, query, time.Now())+ if err != nil {+ return nil, err+ }+ defer func() { _ = rows.Close() }()++ var keys []KeyPair+ for rows.Next() {+ var priv, pub []byte+ var exp NullTime+ if err := rows.Scan(&priv, &pub, &exp); err != nil {+ return nil, err+ }+ var expiration time.Time+ if exp.Valid {+ expiration = exp.Time+ }+ keys = append(keys, KeyPair{+ Private: ed25519.PrivateKey(priv),+ Public: ed25519.PublicKey(pub),+ Expiration: expiration,+ })+ }+ return keys, rows.Err()+}++// RotateKeys implements Store.+func (s *SQLStore) RotateKeys(ctx context.Context, newPriv ed25519.PrivateKey, newPub ed25519.PublicKey, gracePeriod time.Duration) error {+ tx, err := s.db.BeginTx(ctx, nil)+ if err != nil {+ return err+ }+ defer func() { _ = tx.Rollback() }()++ now := time.Now()+ expireTime := now.Add(gracePeriod)++ // Set expiration on the current active key+ updateQuery := s.rebind(`UPDATE keyring SET expiration = ? WHERE expiration IS NULL`)+ if _, err := tx.ExecContext(ctx, updateQuery, expireTime); err != nil {+ return err+ }++ // Insert the new key+ insertQuery := s.rebind(`INSERT INTO keyring (private_key, public_key, created_at) VALUES (?, ?, ?)`)+ if _, err := tx.ExecContext(ctx, insertQuery, []byte(newPriv), []byte(newPub), now); err != nil {+ return err+ }++ // Clean up expired keys+ deleteQuery := s.rebind(`DELETE FROM keyring WHERE expiration <= ?`)+ if _, err := tx.ExecContext(ctx, deleteQuery, now); err != nil {+ return err+ }++ return tx.Commit()+}++// SaveInitialKey implements Store.+func (s *SQLStore) SaveInitialKey(ctx context.Context, priv ed25519.PrivateKey, pub ed25519.PublicKey) error {+ query := s.rebind(`INSERT INTO keyring (private_key, public_key, created_at) VALUES (?, ?, ?)`)+ _, err := s.db.ExecContext(ctx, query, []byte(priv), []byte(pub), time.Now())+ return err+}++// EnrollNode implements Store.+func (s *SQLStore) EnrollNode(ctx context.Context, peerID string, biscuit []byte, expiresAt time.Time) error {+ var query string+ if s.isPostgres() {+ query = s.rebind(`+ INSERT INTO nodes (peer_id, biscuit_token, 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`)+ } else {+ query = s.rebind(`+ INSERT INTO nodes (peer_id, biscuit_token, 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(), expiresAt)+ 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 = ?`)+ var node EnrolledNode+ err := s.db.QueryRowContext(ctx, query, peerID).Scan(&node.PeerID, &node.Biscuit, &node.EnrolledAt, &node.ExpiresAt, &node.Banned)+ if err == sql.ErrNoRows {+ return nil, ErrNotFound+ }+ if err != nil {+ return nil, err+ }+ return &node, nil+}++// SetNodeBanned implements Store.+func (s *SQLStore) SetNodeBanned(ctx context.Context, peerID string, banned bool) error {+ query := s.rebind(`UPDATE nodes SET banned = ? WHERE peer_id = ?`)+ _, err := s.db.ExecContext(ctx, query, banned, peerID)+ return err+}++// IsNodeBanned implements Store.+func (s *SQLStore) IsNodeBanned(ctx context.Context, peerID string) (bool, error) {+ query := s.rebind(`SELECT banned FROM nodes WHERE peer_id = ?`)+ var banned bool+ err := s.db.QueryRowContext(ctx, query, peerID).Scan(&banned)+ if err == sql.ErrNoRows {+ return false, nil // Not found nodes are not banned by default+ }+ if err != nil {+ return false, err+ }+ return banned, nil+}++// UpsertRouterLease implements Store.+func (s *SQLStore) UpsertRouterLease(ctx context.Context, lease *RouterLease) error {+ addrsBytes, err := json.Marshal(lease.Addresses)+ if err != nil {+ return err+ }++ var query string+ if s.isPostgres() {+ query = s.rebind(`+ INSERT INTO routers (peer_id, multiaddresses, last_lease_renewal, expires_at)+ VALUES (?, ?, ?, ?)+ ON CONFLICT (peer_id)+ DO UPDATE SET multiaddresses = EXCLUDED.multiaddresses, last_lease_renewal = EXCLUDED.last_lease_renewal, expires_at = EXCLUDED.expires_at`)+ } else {+ query = s.rebind(`+ INSERT INTO routers (peer_id, multiaddresses, last_lease_renewal, expires_at)+ VALUES (?, ?, ?, ?)+ ON CONFLICT (peer_id)+ DO UPDATE SET multiaddresses = excluded.multiaddresses, last_lease_renewal = excluded.last_lease_renewal, expires_at = excluded.expires_at`)+ }++ _, err = s.db.ExecContext(ctx, query, lease.PeerID, string(addrsBytes), lease.LastRenewal, lease.ExpiresAt)+ return err+}++// GetActiveRouters implements Store.+func (s *SQLStore) GetActiveRouters(ctx context.Context) ([]RouterLease, error) {+ query := s.rebind(`SELECT peer_id, multiaddresses, last_lease_renewal, expires_at FROM routers WHERE expires_at > ?`)+ rows, err := s.db.QueryContext(ctx, query, time.Now())+ if err != nil {+ return nil, err+ }+ defer func() { _ = rows.Close() }()++ var leases []RouterLease+ for rows.Next() {+ var l RouterLease+ var addrsStr string+ if err := rows.Scan(&l.PeerID, &addrsStr, &l.LastRenewal, &l.ExpiresAt); err != nil {+ return nil, err+ }+ if err := json.Unmarshal([]byte(addrsStr), &l.Addresses); err != nil {+ return nil, err+ }+ leases = append(leases, l)+ }+ return leases, rows.Err()+}++// SavePolicy implements Store.+func (s *SQLStore) SavePolicy(ctx context.Context, policy *api.PolicyConfig) error {+ data, err := yaml.Marshal(policy)+ if err != nil {+ return err+ }++ var query string+ if s.isPostgres() {+ query = s.rebind(`+ INSERT INTO policies (id, content, updated_at)+ VALUES ('mesh-policy', ?, ?)+ ON CONFLICT (id)+ DO UPDATE SET content = EXCLUDED.content, updated_at = EXCLUDED.updated_at`)+ } else {+ query = s.rebind(`+ INSERT INTO policies (id, content, updated_at)+ VALUES ('mesh-policy', ?, ?)+ ON CONFLICT (id)+ DO UPDATE SET content = excluded.content, updated_at = excluded.updated_at`)+ }++ _, err = s.db.ExecContext(ctx, query, string(data), time.Now())+ return err+}++// GetPolicy implements Store.+func (s *SQLStore) GetPolicy(ctx context.Context) (*api.PolicyConfig, error) {+ query := s.rebind(`SELECT content FROM policies WHERE id = 'mesh-policy'`)+ var data string+ err := s.db.QueryRowContext(ctx, query).Scan(&data)+ if err == sql.ErrNoRows {+ return nil, ErrNotFound+ }+ if err != nil {+ return nil, err+ }++ var policy api.PolicyConfig+ if err := yaml.Unmarshal([]byte(data), &policy); err != nil {+ return nil, err+ }+ return &policy, nil+}++// Close implements Store.+func (s *SQLStore) Close() error {+ return s.db.Close()+}++// NullTime represents a time.Time that may be null.+type NullTime struct {+ Time time.Time+ Valid bool // Valid is true if Time is not NULL+}++// Scan implements the Scanner interface.+func (nt *NullTime) Scan(value any) error {+ if value == nil {+ nt.Time, nt.Valid = time.Time{}, false+ return nil+ }+ nt.Valid = true+ switch v := value.(type) {+ case time.Time:+ nt.Time = v+ case []byte:+ // SQLite might return string representation of date+ t, err := time.Parse("2006-01-02 15:04:05", string(v))+ if err != nil {+ t, err = time.Parse(time.RFC3339, string(v))+ }+ if err != nil {+ return err+ }+ nt.Time = t+ case string:+ t, err := time.Parse("2006-01-02 15:04:05", v)+ if err != nil {+ t, err = time.Parse(time.RFC3339, v)+ }+ if err != nil {+ return err+ }+ nt.Time = t+ default:+ return fmt.Errorf("cannot scan %T into NullTime", value)+ }+ return nil+}diff --git a/internal/storage/sql_store_test.go b/internal/storage/sql_store_test.gonew file mode 100644index 00000000..2e4b6b32--- /dev/null+++ b/internal/storage/sql_store_test.go@@ -0,0 +1,309 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package storage++import (+ "bytes"+ "context"+ "crypto/ed25519"+ "crypto/rand"+ "os"+ "path/filepath"+ "reflect"+ "testing"+ "time"++ "github.com/google/sam/api"+)++func newTestStore(t *testing.T) Store {+ t.Helper()+ // Use a temporary file for SQLite testing to avoid concurrency sharing bugs in parallel tests+ tempDir, err := os.MkdirTemp("", "sam-store-test-*")+ if err != nil {+ t.Fatalf("failed to create temp dir: %v", err)+ }+ t.Cleanup(func() {+ _ = os.RemoveAll(tempDir)+ })++ dbPath := filepath.Join(tempDir, "test.db")+ store, err := NewSQLStore("sqlite", dbPath)+ if err != nil {+ t.Fatalf("failed to create test store: %v", err)+ }+ return store+}++func TestKeyRingOps(t *testing.T) {+ store := newTestStore(t)+ defer func() { _ = store.Close() }()++ ctx := context.Background()++ // Initial State: Keyring empty+ _, _, err := store.GetCurrentKey(ctx)+ if err != ErrNotFound {+ t.Fatalf("expected ErrNotFound, got %v", err)+ }++ // Generate and save initial key+ pub1, priv1, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatalf("failed to generate key: %v", err)+ }+ err = store.SaveInitialKey(ctx, priv1, pub1)+ if err != nil {+ t.Fatalf("failed to save initial key: %v", err)+ }++ // GetCurrentKey+ curPriv, curPub, err := store.GetCurrentKey(ctx)+ if err != nil {+ t.Fatalf("failed to get current key: %v", err)+ }+ if !bytes.Equal(curPriv, priv1) || !bytes.Equal(curPub, pub1) {+ t.Fatalf("returned key pair does not match initial key pair")+ }++ // Get all valid keys (only 1 valid key right now)+ validKeys, err := store.GetAllValidKeys(ctx)+ if err != nil {+ t.Fatalf("failed to get all valid keys: %v", err)+ }+ if len(validKeys) != 1 {+ t.Fatalf("expected 1 valid key, got %d", len(validKeys))+ }+ if !bytes.Equal(validKeys[0].Private, priv1) {+ t.Fatalf("valid key mismatch")+ }++ // Rotate keys+ pub2, priv2, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatalf("failed to generate key: %v", err)+ }+ gracePeriod := 1 * time.Hour+ err = store.RotateKeys(ctx, priv2, pub2, gracePeriod)+ if err != nil {+ t.Fatalf("failed to rotate keys: %v", err)+ }++ // GetCurrentKey should return the new key+ curPriv, curPub, err = store.GetCurrentKey(ctx)+ if err != nil {+ t.Fatalf("failed to get current key: %v", err)+ }+ if !bytes.Equal(curPriv, priv2) || !bytes.Equal(curPub, pub2) {+ t.Fatalf("returned key pair does not match rotated key pair")+ }++ // GetAllValidKeys should return both+ validKeys, err = store.GetAllValidKeys(ctx)+ if err != nil {+ t.Fatalf("failed to get all valid keys: %v", err)+ }+ if len(validKeys) != 2 {+ t.Fatalf("expected 2 valid keys, got %d", len(validKeys))+ }++ // Wait, check if key clean up works. Let's rotate with negative grace period to expire the second key immediately+ pub3, priv3, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatalf("failed to generate key: %v", err)+ }+ err = store.RotateKeys(ctx, priv3, pub3, -1*time.Second) // Expires immediately+ if err != nil {+ t.Fatalf("failed to rotate keys: %v", err)+ }++ // GetAllValidKeys should only return 2 keys now (the new current key [pub3] and key 2 [pub2] since it hasn't expired, but key 1 [pub1] was cleaned up because its expiration was reached)+ validKeys, err = store.GetAllValidKeys(ctx)+ if err != nil {+ t.Fatalf("failed to get all valid keys: %v", err)+ }+ if len(validKeys) != 2 {+ t.Fatalf("expected 2 valid keys, got %d", len(validKeys))+ }+}++func TestNodeEnrollmentOps(t *testing.T) {+ store := newTestStore(t)+ defer func() { _ = store.Close() }()++ ctx := context.Background()+ peerID := "12D3KooWLTpP4335eb4e414e21415eb66b"++ // Not enrolled yet+ _, err := store.GetNode(ctx, peerID)+ if err != ErrNotFound {+ t.Fatalf("expected ErrNotFound, got %v", err)+ }++ banned, err := store.IsNodeBanned(ctx, peerID)+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if banned {+ t.Fatalf("expected not banned by default")+ }++ // Enroll node+ biscuitData := []byte("some-biscuit-token")+ expiresAt := time.Now().Add(24 * time.Hour)+ err = store.EnrollNode(ctx, peerID, biscuitData, expiresAt)+ if err != nil {+ t.Fatalf("failed to enroll node: %v", err)+ }++ // Get node+ n, err := store.GetNode(ctx, peerID)+ if err != nil {+ t.Fatalf("failed to get enrolled node: %v", err)+ }+ if n.PeerID != peerID || !bytes.Equal(n.Biscuit, biscuitData) || n.Banned {+ t.Fatalf("node data mismatch: %+v", n)+ }++ // Ban node+ err = store.SetNodeBanned(ctx, peerID, true)+ if err != nil {+ t.Fatalf("failed to ban node: %v", err)+ }++ banned, err = store.IsNodeBanned(ctx, peerID)+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if !banned {+ t.Fatalf("expected node to be banned")+ }++ // 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)+ if err != nil {+ t.Fatalf("failed to enroll node: %v", err)+ }++ banned, err = store.IsNodeBanned(ctx, peerID)+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if !banned {+ t.Fatalf("expected node to remain banned unless explicitly unbanned")+ }+}++func TestRouterLeaseOps(t *testing.T) {+ store := newTestStore(t)+ defer func() { _ = store.Close() }()++ ctx := context.Background()+ peerID := "12D3KooWLTpRouterPeer"++ // No routers+ routers, err := store.GetActiveRouters(ctx)+ if err != nil {+ t.Fatalf("failed to get active routers: %v", err)+ }+ if len(routers) != 0 {+ t.Fatalf("expected 0 routers, got %d", len(routers))+ }++ // Add a lease+ lease1 := &RouterLease{+ PeerID: peerID,+ Addresses: []string{"/ip4/127.0.0.1/tcp/5001/p2p/" + peerID},+ LastRenewal: time.Now(),+ ExpiresAt: time.Now().Add(10 * time.Second),+ }+ err = store.UpsertRouterLease(ctx, lease1)+ if err != nil {+ t.Fatalf("failed to upsert router lease: %v", err)+ }++ // Get active routers+ routers, err = store.GetActiveRouters(ctx)+ if err != nil {+ t.Fatalf("failed to get active routers: %v", err)+ }+ if len(routers) != 1 {+ t.Fatalf("expected 1 router, got %d", len(routers))+ }+ if routers[0].PeerID != peerID || !reflect.DeepEqual(routers[0].Addresses, lease1.Addresses) {+ t.Fatalf("lease details mismatch: %+v", routers[0])+ }++ // Add an expired lease+ expiredLease := &RouterLease{+ PeerID: "expired-router",+ Addresses: []string{"/ip4/127.0.0.1/tcp/5002/p2p/expired-router"},+ LastRenewal: time.Now().Add(-5 * time.Minute),+ ExpiresAt: time.Now().Add(-1 * time.Minute),+ }+ err = store.UpsertRouterLease(ctx, expiredLease)+ if err != nil {+ t.Fatalf("failed to upsert expired lease: %v", err)+ }++ // Active routers should still be 1+ routers, err = store.GetActiveRouters(ctx)+ if err != nil {+ t.Fatalf("failed to get active routers: %v", err)+ }+ if len(routers) != 1 {+ t.Fatalf("expected 1 router, got %d", len(routers))+ }+}++func TestPolicyOps(t *testing.T) {+ store := newTestStore(t)+ defer func() { _ = store.Close() }()++ ctx := context.Background()++ // Policy should be empty+ _, err := store.GetPolicy(ctx)+ if err != ErrNotFound {+ t.Fatalf("expected ErrNotFound, got %v", err)+ }++ // Save policy+ p := &api.PolicyConfig{+ Version: "v1alpha1",+ Bindings: []api.Binding{+ {Role: "admin", Members: []string{"user:alice"}},+ },+ Roles: map[string]api.RolePolicy{+ "admin": {+ AllowedServices: []string{"*"},+ AllowedTargets: []string{"*"},+ },+ },+ }+ err = store.SavePolicy(ctx, p)+ if err != nil {+ t.Fatalf("failed to save policy: %v", err)+ }++ // Get policy+ retPolicy, err := store.GetPolicy(ctx)+ if err != nil {+ t.Fatalf("failed to get policy: %v", err)+ }+ if retPolicy.Version != p.Version || len(retPolicy.Bindings) != 1 || retPolicy.Bindings[0].Role != "admin" {+ t.Fatalf("policy contents mismatch: %+v", retPolicy)+ }+}diff --git a/internal/storage/storage.go b/internal/storage/storage.gonew file mode 100644index 00000000..36fd1f50--- /dev/null+++ b/internal/storage/storage.go@@ -0,0 +1,94 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package storage++import (+ "context"+ "crypto/ed25519"+ "errors"+ "time"++ "github.com/google/sam/api"+)++var (+ ErrNotFound = errors.New("not found")+)++// KeyPair holds cryptographic key information.+type KeyPair struct {+ Private ed25519.PrivateKey+ Public ed25519.PublicKey+ Expiration time.Time+}++// RouterLease represents a router registered with the control plane.+type RouterLease struct {+ PeerID string+ Addresses []string+ LastRenewal time.Time+ ExpiresAt time.Time+}++// EnrolledNode represents a node enrolled in the mesh.+type EnrolledNode struct {+ PeerID string+ Biscuit []byte+ EnrolledAt time.Time+ ExpiresAt time.Time+ Banned bool+}++// Store defines the persistent operations for the SAM control plane.+type Store interface {+ // GetCurrentKey retrieves the active key pair for biscuit signing.+ GetCurrentKey(ctx context.Context) (ed25519.PrivateKey, ed25519.PublicKey, error)++ // GetAllValidKeys retrieves the active key pair and any non-expired historical key pairs.+ GetAllValidKeys(ctx context.Context) ([]KeyPair, error)++ // RotateKeys rotates the current key to a new key pair and sets the expiration of the old key.+ RotateKeys(ctx context.Context, newPriv ed25519.PrivateKey, newPub ed25519.PublicKey, gracePeriod time.Duration) error++ // SaveInitialKey sets the initial key pair if no keys exist yet.+ 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++ // GetNode retrieves node enrollment details.+ GetNode(ctx context.Context, peerID string) (*EnrolledNode, error)++ // SetNodeBanned updates the banned status of a node.+ SetNodeBanned(ctx context.Context, peerID string, banned bool) error++ // IsNodeBanned checks if a node is currently banned.+ IsNodeBanned(ctx context.Context, peerID string) (bool, error)++ // UpsertRouterLease updates or creates a lease for a sam-router.+ UpsertRouterLease(ctx context.Context, lease *RouterLease) error++ // GetActiveRouters retrieves all routers whose leases are still valid.+ GetActiveRouters(ctx context.Context) ([]RouterLease, error)++ // SavePolicy persists the mesh configurations.+ SavePolicy(ctx context.Context, policy *api.PolicyConfig) error++ // GetPolicy loads the mesh configurations.+ GetPolicy(ctx context.Context) (*api.PolicyConfig, error)++ // Close closes the underlying database connection.+ Close() error+}diff --git a/mobile/sam-node-ffi/ffi/ffi_test.go b/mobile/sam-node-ffi/ffi/ffi_test.goindex b3c82dc1..b760210e 100644--- a/mobile/sam-node-ffi/ffi/ffi_test.go+++ b/mobile/sam-node-ffi/ffi/ffi_test.go@@ -16,12 +16,16 @@ package ffiimport ("context"+ "crypto/ed25519"+ "crypto/rand""encoding/json""io""net/http""net/http/httptest""testing"+ "time"+ "github.com/biscuit-auth/biscuit-go/v2""github.com/google/sam/api""github.com/libp2p/go-libp2p"dht "github.com/libp2p/go-libp2p-kad-dht"@@ -44,12 +48,67 @@ func TestMobileFFILifecycle(t *testing.T) {}defer func() { _ = hubDHT.Close() }()+ // Generate key-pair for Mock Control Plane signing+ cpPubKey, cpPrivKey, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+hubHost.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) {+ println("--- MOCK HUB: received auth stream connection")defer func() { _ = s.Close() }()- resp := &api.AuthResponse{Success: true}++ // Mint a valid Biscuit token signed by CP key and bound to mock hub's Peer ID+ builder := biscuit.NewBuilder(cpPrivKey)+ if err := builder.AddAuthorityFact(biscuit.Fact{+ Predicate: biscuit.Predicate{+ Name: "node",+ IDs: []biscuit.Term{biscuit.String(hubHost.ID().String())},+ },+ }); err != nil {+ println("--- MOCK HUB: failed to add node fact:", err.Error())+ return+ }+ if err := builder.AddAuthorityFact(biscuit.Fact{+ Predicate: biscuit.Predicate{+ Name: api.FactRole,+ IDs: []biscuit.Term{biscuit.String(api.RoleRouter)},+ },+ }); err != nil {+ println("--- MOCK HUB: failed to add role fact:", err.Error())+ return+ }+ if err := builder.AddAuthorityFact(biscuit.Fact{+ Predicate: biscuit.Predicate{+ Name: api.FactExpiration,+ IDs: []biscuit.Term{biscuit.Date(time.Now().Add(time.Hour))},+ },+ }); err != nil {+ println("--- MOCK HUB: failed to add expiration fact:", err.Error())+ return+ }+ b, err := builder.Build()+ if err != nil {+ println("--- MOCK HUB: failed to build biscuit:", err.Error())+ return+ }+ biscuitBytes, err := b.Serialize()+ if err != nil {+ println("--- MOCK HUB: failed to serialize biscuit:", err.Error())+ return+ }++ resp := &api.AuthResponse{+ Success: true,+ Biscuit: biscuitBytes,+ }data, _ := proto.Marshal(resp)writer := msgio.NewVarintWriter(s)- _ = writer.WriteMsg(data)+ if err := writer.WriteMsg(data); err != nil {+ println("--- MOCK HUB: failed to write AuthResponse:", err.Error())+ return+ }+ println("--- MOCK HUB: wrote AuthResponse success with valid biscuit")})mux := http.NewServeMux()@@ -60,7 +119,7 @@ func TestMobileFFILifecycle(t *testing.T) {resp := &api.EnrollResponse{BiscuitToken: []byte("mock-biscuit-token"),- HubPublicKey: make([]byte, 32),+ HubPublicKey: cpPubKey,HubAddresses: []string{hubHost.Addrs()[0].String() + "/p2p/" + hubHost.ID().String()},}data, _ := proto.Marshal(resp)diff --git a/tests/e2e/auth_flows.bats b/tests/e2e/auth_flows.batsindex 3c58f195..42af116f 100644--- a/tests/e2e/auth_flows.bats+++ b/tests/e2e/auth_flows.bats@@ -51,7 +51,7 @@ teardown() {$(mesh_get_add_hosts) \-v "${data_vol}:/root/.config/sam-mesh" \"sam-node:local" \- join "http://sam-hub:9090" > "/tmp/${node_name}-join.out" 2>&1 &+ join "http://sam-control-plane:8080" > "/tmp/${node_name}-join.out" 2>&1 &local join_pid=$!MESH_CONTAINERS+=("${node_name}-join")@@ -137,7 +137,7 @@ teardown() {-v "${token_vol}:/var/run/secrets/tokens" \"sam-node:local" \run \- --hub "http://sam-hub:9090" \+ --hub "http://sam-control-plane:8080" \--jwt-path "/var/run/secrets/tokens/sa-token" \--api-token "secret-token"MESH_CONTAINERS+=("${node_name}")diff --git a/tests/e2e/docker/mock_oidc.py b/tests/e2e/docker/mock_oidc.pyindex fbd5da4b..7c42c077 100644--- a/tests/e2e/docker/mock_oidc.py+++ b/tests/e2e/docker/mock_oidc.py@@ -110,6 +110,8 @@ class Handler(BaseHTTPRequestHandler):groups = ['data-scientist']if client_id == 'admin-client':groups = ['admin']+ elif client_id == 'router-client':+ groups = ['routers']payload = {'iss': ISSUER,diff --git a/tests/e2e/fixtures/kind-cluster.yaml b/tests/e2e/fixtures/kind-cluster.yamlnew file mode 100644index 00000000..752e993c--- /dev/null+++ b/tests/e2e/fixtures/kind-cluster.yaml@@ -0,0 +1,6 @@+kind: Cluster+apiVersion: kind.x-k8s.io/v1alpha4+nodes:+- role: control-plane+- role: worker+- role: workerdiff --git a/tests/e2e/fixtures/sam-hub.yaml b/tests/e2e/fixtures/sam-hub.yamlindex 1d49a689..b97b7e68 100644--- a/tests/e2e/fixtures/sam-hub.yaml+++ b/tests/e2e/fixtures/sam-hub.yaml@@ -5,7 +5,6 @@ metadata:data:SAM_OIDC_ISSUER: "${ISSUERS}"SAM_OIDC_ID: "sam-mesh-audience"- SAM_HUB_KEY: "0000000000000000000000000000000000000000000000000000000000000000"policy.yaml: |version: v1alpha1bindings:@@ -17,6 +16,8 @@ data:role: admin- members: ["user:admin-user"]role: admin+ - members: ["group:routers"]+ role: routerroles:admin:allowed_services:@@ -32,17 +33,126 @@ data:- "system://sam.catalog"allowed_targets:- "*"+ router:+ allowed_services:+ - "*"+ allowed_targets:+ - "*"---apiVersion: v1kind: Servicemetadata:- name: sam-hub+ name: sam-db+spec:+ ports:+ - name: postgres+ port: 5432+ targetPort: 5432+ selector:+ app: sam-db+---+apiVersion: apps/v1+kind: Deployment+metadata:+ name: sam-db+spec:+ replicas: 1+ selector:+ matchLabels:+ app: sam-db+ template:+ metadata:+ labels:+ app: sam-db+ spec:+ containers:+ - name: postgres+ image: postgres:16-alpine+ env:+ - name: POSTGRES_DB+ value: sam_mesh+ - name: POSTGRES_USER+ value: sam+ - name: POSTGRES_PASSWORD+ value: sam-secret-password+ ports:+ - containerPort: 5432+ protocol: TCP+---+apiVersion: v1+kind: Service+metadata:+ name: sam-control-planespec:- clusterIP: Noneports:- name: http- port: 9090- targetPort: 9090+ port: 8080+ targetPort: 8080+ selector:+ app: sam-control-plane+---+apiVersion: apps/v1+kind: Deployment+metadata:+ name: sam-control-plane+spec:+ replicas: 2+ strategy:+ type: Recreate+ selector:+ matchLabels:+ app: sam-control-plane+ template:+ metadata:+ labels:+ app: sam-control-plane+ spec:+ affinity:+ podAntiAffinity:+ requiredDuringSchedulingIgnoredDuringExecution:+ - labelSelector:+ matchExpressions:+ - key: app+ operator: In+ values:+ - sam-control-plane+ topologyKey: "kubernetes.io/hostname"+ initContainers:+ - name: wait-for-db+ image: busybox:latest+ command: ['sh', '-c', 'until nc -z -w3 sam-db 5432; do echo waiting for db; sleep 1; done']+ containers:+ - name: sam-control-plane+ image: sam-control-plane:local+ ports:+ - containerPort: 8080+ hostPort: 8080+ protocol: TCP+ args:+ - "--bind-address=0.0.0.0:8080"+ - "--db-driver=postgres"+ - "--db-dsn=postgres://sam:sam-secret-password@sam-db:5432/sam_mesh?sslmode=disable"+ - "--issuer=${ISSUERS}"+ - "--allowed-audiences=sam-mesh-audience,sam-hub-audience"+ - "--policy-file=/etc/sam/policy.yaml"+ - "--insecure-skip-tls-verify"+ - "--log-level=debug"+ volumeMounts:+ - name: config-volume+ mountPath: /etc/sam/policy.yaml+ subPath: policy.yaml+ volumes:+ - name: config-volume+ configMap:+ name: sam-hub-config+---+apiVersion: v1+kind: Service+metadata:+ name: sam-router+spec:+ clusterIP: None+ ports:- port: 4501targetPort: 4501protocol: TCP@@ -52,63 +162,62 @@ spec:protocol: UDPname: p2p-udpselector:- app: sam-hub+ app: sam-router---apiVersion: apps/v1kind: StatefulSetmetadata:- name: sam-hub+ name: sam-routerspec:- serviceName: "sam-hub"- replicas: 2+ serviceName: "sam-router"+ replicas: 1selector:matchLabels:- app: sam-hub+ app: sam-routertemplate:metadata:labels:- app: sam-hub+ app: sam-routerspec:containers:- - name: sam-hub- image: sam-hub:local- envFrom:- - configMapRef:- name: sam-hub-config- env:- - name: MY_NODE_NAME- valueFrom:- fieldRef:- fieldPath: spec.nodeName+ - name: sam-router+ image: sam-router:localports:- - containerPort: 9090- hostPort: 9090- protocol: TCP- containerPort: 4501hostPort: 4501protocol: TCP- containerPort: 4501hostPort: 4501protocol: UDP+ env:+ - name: ROUTER_NODE_IP+ valueFrom:+ fieldRef:+ fieldPath: status.hostIPargs:- - "--issuer"- - "${ISSUERS}"- - "--key"- - "0000000000000000000000000000000000000000000000000000000000000000"- - "--insecure-skip-tls-verify"- - "--policy-file=/etc/sam/policy.yaml"+ - "--control-plane=http://sam-control-plane:8080"- "--listen=/ip4/0.0.0.0/tcp/4501"- "--listen=/ip4/0.0.0.0/udp/4501/quic-v1"- - "--external-multiaddr=/dns4/$(MY_NODE_NAME)/tcp/4501"- - "--external-multiaddr=/dns4/$(MY_NODE_NAME)/udp/4501/quic-v1"- - "--external-multiaddr=http://sam-hub:9090"- - "--log-level"- - "debug"+ - "--external-addr=/dns4/sam-router/tcp/4501"+ - "--external-addr=/dns4/sam-router/udp/4501/quic-v1"+ - "--external-addr=/ip4/$(ROUTER_NODE_IP)/tcp/4501"+ - "--external-addr=/ip4/$(ROUTER_NODE_IP)/udp/4501/quic-v1"+ - "--jwt-path=/var/run/secrets/tokens/sa-token"+ - "--keys-path=/data/router.key"+ - "--allow-loopback"+ - "--log-level=debug"volumeMounts:- - name: config-volume- mountPath: /etc/sam/policy.yaml- subPath: policy.yaml+ - name: router-data+ mountPath: /data+ - name: router-token-vol+ mountPath: /var/run/secrets/tokens+ readOnly: truevolumes:- - name: config-volume- configMap:- name: sam-hub-config+ - name: router-data+ emptyDir: {}+ - name: router-token-vol+ secret:+ secretName: sam-router-token+ items:+ - key: token+ path: sa-tokendiff --git a/tests/e2e/key_rotation_test.bats b/tests/e2e/key_rotation_test.batsindex 6da02353..07bd5517 100644--- a/tests/e2e/key_rotation_test.bats+++ b/tests/e2e/key_rotation_test.bats@@ -20,21 +20,8 @@ teardown() {local keykey="$(mesh_gen_hex32)"- docker run -d \- --name "${hub_name}" \- --network "${MESH_NETWORK}" \- --network-alias sam-hub \- $(mesh_get_add_hosts) \- "sam-hub:local" \- --issuer "http://mock-oidc:18080" \- --client-id "sam-e2e" \- --key "${key}" \- --listen "/ip4/0.0.0.0/tcp/4002" \- --external-multiaddr "/dns4/sam-hub/tcp/4002" \- --mesh "${MESH_PREFIX}" \- --key-rotation-interval 5s >/dev/null+ mesh_start_standalone_hub "${hub_name}" "5s" "2s"- MESH_CONTAINERS+=("${hub_name}")mesh_wait_for_log "${hub_name}" "PeerID:" 20local hub_peer_iddiff --git a/tests/e2e/lib/container_mesh.bash b/tests/e2e/lib/container_mesh.bashindex ace38c54..dfaa2614 100644--- a/tests/e2e/lib/container_mesh.bash+++ b/tests/e2e/lib/container_mesh.bash@@ -187,14 +187,19 @@ if [[ -z "${MESH_HELPERS_LOADED:-}" ]]; thenlocal custom_hub="${MESH_PREFIX}-hub"if docker inspect "${custom_hub}" >/dev/null 2>&1; thenhub_ip=$(docker inspect -f "{{(index .NetworkSettings.Networks \"${net}\").IPAddress}}" "${custom_hub}")- echo "--add-host mock-oidc:${oidc_node_ip} --add-host sam-hub:${hub_ip}"+ local cp_ip=""+ local custom_cp="${MESH_PREFIX}-hub-cp"+ if docker inspect "${custom_cp}" >/dev/null 2>&1; then+ cp_ip=$(docker inspect -f "{{(index .NetworkSettings.Networks \"${net}\").IPAddress}}" "${custom_cp}")+ fi+ echo "--add-host mock-oidc:${oidc_node_ip} --add-host sam-hub:${hub_ip} --add-host sam-control-plane:${cp_ip}"else- # Resolve sam-hub-0 node IP- local hub_node- hub_node=$(kubectl --context="${KUBECONTEXT:-kind-sam-wi-test}" get pod sam-hub-0 -o jsonpath='{.spec.nodeName}')- local hub_node_ip- hub_node_ip=$(docker inspect -f "{{(index .NetworkSettings.Networks \"${net}\").IPAddress}}" "${hub_node}")- echo "--add-host mock-oidc:${oidc_node_ip} --add-host sam-hub:${hub_node_ip} --add-host ${hub_node}:${hub_node_ip}"+ # Resolve sam-router-0 node IP+ local router_node+ router_node=$(kubectl --context="${KUBECONTEXT:-kind-sam-wi-test}" get pod sam-router-0 -o jsonpath='{.spec.nodeName}')+ local router_node_ip+ router_node_ip=$(docker inspect -f "{{(index .NetworkSettings.Networks \"${net}\").IPAddress}}" "${router_node}")+ echo "--add-host mock-oidc:${oidc_node_ip} --add-host sam-hub:${router_node_ip} --add-host sam-router:${router_node_ip} --add-host sam-control-plane:${router_node_ip} --add-host ${router_node}:${router_node_ip}"fi}@@ -222,7 +227,7 @@ if [[ -z "${MESH_HELPERS_LOADED:-}" ]]; thenmakemake docker-build- if [[ ! -x "./bin/sam-node" || ! -x "./bin/sam-hub" || ! -x "./bin/mcp-client" ]]; then+ if [[ ! -x "./bin/sam-node" || ! -x "./bin/sam-control-plane" || ! -x "./bin/sam-router" || ! -x "./bin/mcp-client" ]]; thenecho "missing binaries; run: make build" >&2return 1fi@@ -232,22 +237,16 @@ if [[ -z "${MESH_HELPERS_LOADED:-}" ]]; thenif ! kind get clusters | grep -q "^${KUBERNETES_CLUSTER_NAME}$"; thenkind delete cluster --name "${KUBERNETES_CLUSTER_NAME}" >/dev/null 2>&1 || true- cat <<KIND | kind create cluster --name "${KUBERNETES_CLUSTER_NAME}" --config=--kind: Cluster-apiVersion: kind.x-k8s.io/v1alpha4-nodes:-- role: control-plane-- role: worker-- role: worker-KIND-- kind load docker-image sam-hub:local --name "${KUBERNETES_CLUSTER_NAME}"- kind load docker-image sam-node:local --name "${KUBERNETES_CLUSTER_NAME}"- kind load docker-image sam-mock-oidc:local --name "${KUBERNETES_CLUSTER_NAME}"+ kind create cluster --name "${KUBERNETES_CLUSTER_NAME}" --config=tests/e2e/fixtures/kind-cluster.yamlelsekind export kubeconfig --name "${KUBERNETES_CLUSTER_NAME}"fi+ kind load docker-image sam-control-plane:local --name "${KUBERNETES_CLUSTER_NAME}"+ kind load docker-image sam-router:local --name "${KUBERNETES_CLUSTER_NAME}"+ kind load docker-image sam-node:local --name "${KUBERNETES_CLUSTER_NAME}"+ kind load docker-image sam-mock-oidc:local --name "${KUBERNETES_CLUSTER_NAME}"+kubectl --context="${KUBECONTEXT}" apply -f tests/e2e/fixtures/mock-oidc.yamlkubectl --context="${KUBECONTEXT}" rollout status deployment/mock-oidc --timeout=60s@@ -257,20 +256,43 @@ KINDkubectl --context="${KUBECONTEXT}" apply -f tests/e2e/fixtures/allow-anonymous-oidc.yaml- export ISSUERS="${kube_issuer},http://mock-oidc:18080"+ export ISSUERS="http://mock-oidc:18080,${kube_issuer}"++ local oidc_node+ oidc_node=$(kubectl --context="${KUBECONTEXT}" get pod -l app=mock-oidc -o jsonpath='{.items[0].spec.nodeName}')+ local oidc_node_ip+ oidc_node_ip=$(docker inspect -f "{{(index .NetworkSettings.Networks \"${MESH_NETWORK:-kind}\").IPAddress}}" "${oidc_node}")++ local router_token=""+ local i+ for ((i=0; i<15; i++)); do+ router_token=$(curl -s -d "client_id=router-client" http://${oidc_node_ip}:18080/token | jq -r .access_token || true)+ if [[ -n "${router_token}" && "${router_token}" != "null" ]]; then+ break+ fi+ sleep 0.5+ done+ [[ -n "${router_token}" && "${router_token}" != "null" ]]++ kubectl --context="${KUBECONTEXT}" create secret generic sam-router-token --from-literal=token="${router_token}" --dry-run=client -o yaml | kubectl --context="${KUBECONTEXT}" apply -f -+envsubst '$ISSUERS' < tests/e2e/fixtures/sam-hub.yaml | kubectl --context="${KUBECONTEXT}" apply -f -- kubectl --context="${KUBECONTEXT}" rollout restart statefulset/sam-hub- kubectl --context="${KUBECONTEXT}" rollout status statefulset/sam-hub --timeout=60s+ kubectl --context="${KUBECONTEXT}" rollout restart deployment/sam-db || true+ kubectl --context="${KUBECONTEXT}" rollout status deployment/sam-db --timeout=60s+ kubectl --context="${KUBECONTEXT}" rollout restart deployment/sam-control-plane+ kubectl --context="${KUBECONTEXT}" rollout restart statefulset/sam-router+ kubectl --context="${KUBECONTEXT}" rollout status deployment/sam-control-plane --timeout=60s+ kubectl --context="${KUBECONTEXT}" rollout status statefulset/sam-router --timeout=60slocal ifor ((i=0; i<200; i++)); do- if kubectl --context="${KUBECONTEXT}" logs "sam-hub-0" 2>&1 | grep -q "PeerID:"; then+ if kubectl --context="${KUBECONTEXT}" logs "sam-router-0" 2>&1 | grep -q "PeerID:"; thenbreakfisleep 0.1donelocal hub_peer_id- hub_peer_id=$(kubectl --context="${KUBECONTEXT}" logs "sam-hub-0" | grep -oE '12D3Koo[a-zA-Z0-9]+' | head -n 1 || true)+ hub_peer_id=$(kubectl --context="${KUBECONTEXT}" logs "sam-router-0" | grep -oE '12D3Koo[a-zA-Z0-9]+' | head -n 1 || true)[[ -n "${hub_peer_id}" ]]echo "${hub_peer_id}" > "/tmp/sam-wi-test-hub-peer-id"@@ -280,7 +302,7 @@ KINDmesh_teardown_suite() {cd "${BATS_TEST_DIRNAME}/../.."mesh_cleanup_stale_resources- kind delete cluster --name "${KUBERNETES_CLUSTER_NAME:-sam-wi-test}" >/dev/null 2>&1 || true+ # kind delete cluster --name "${KUBERNETES_CLUSTER_NAME:-sam-wi-test}" >/dev/null 2>&1 || trueecho "teardown suite"}@@ -316,7 +338,7 @@ KIND${flags} \--log-level debug \--discovery-interval 2s \- --hub "http://sam-hub:9090" \+ --hub "http://sam-control-plane:8080" \--client-id "sam-mesh-audience" \--client-secret "sam-e2e-secret" \--oidc-issuer "http://mock-oidc:18080" \@@ -346,11 +368,100 @@ KINDmesh_assert_container_running() {local name="$1"if [[ "${name}" == *"-hub" ]]; then- kubectl --context="${KUBECONTEXT:-kind-sam-wi-test}" get pod sam-hub-0 -o jsonpath='{.status.phase}' | grep -q "Running"+ kubectl --context="${KUBECONTEXT:-kind-sam-wi-test}" get pod sam-router-0 -o jsonpath='{.status.phase}' | grep -q "Running"return $?filocal statestate="$(docker inspect -f '{{.State.Running}}' "${name}" 2>/dev/null || true)"[[ "${state}" == "true" ]]}++ mesh_start_standalone_hub() {+ local hub_name="$1"+ local rotation_interval="${2:-0}"+ local grace_period="${3:-0}"++ local policy_file="/tmp/${MESH_PREFIX}-policies.yaml"+ cat <<EOF > "${policy_file}"+version: "v1alpha1"+bindings:+ - members: ["group:routers", "user:system:serviceaccount:default:sam-router-sa"]+ role: "router"+ - members: ["system:authenticated"]+ role: "admin"+roles:+ admin:+ allowed_services: ["*"]+ allowed_targets: ["*"]+ router:+ allowed_services: ["*"]+ allowed_targets: ["*"]+EOF++ # Create data volume for control plane+ local cp_db_vol="${hub_name}-cp-data"+ docker volume create "${cp_db_vol}" >/dev/null+ CLEANUP_VOLUMES+=("${cp_db_vol}")++ # Start Control Plane+ docker run -d \+ --name "${hub_name}-cp" \+ --network "${MESH_NETWORK}" \+ --network-alias sam-control-plane \+ $(mesh_get_add_hosts) \+ -v "${policy_file}:/etc/sam/policies.yaml:ro" \+ -v "${cp_db_vol}:/data" \+ "sam-control-plane:local" \+ --issuer "http://mock-oidc:18080" \+ --allowed-audiences "sam-e2e,sam-hub-audience,sam-mesh-audience" \+ --db-driver "sqlite" \+ --db-dsn "/data/control-plane.db" \+ --key-rotation-interval "${rotation_interval}" \+ --key-grace-period "${grace_period}" \+ --policy-file "/etc/sam/policies.yaml" >/dev/null++ MESH_CONTAINERS+=("${hub_name}-cp")++ # Get a mock token for the router+ local router_token=""+ local i+ for ((i=0; i<15; i++)); do+ router_token=$(docker run --rm --network "${MESH_NETWORK}" $(mesh_get_add_hosts) python:3.12 python3 -c "import urllib.request; import json; req = urllib.request.Request('http://mock-oidc:18080/token', data=b'client_id=router-client'); resp = urllib.request.urlopen(req); print(json.loads(resp.read().decode())['access_token'])" || true)+ if [[ -n "${router_token}" && "${router_token}" != "null" ]]; then+ break+ fi+ sleep 0.5+ done+ [[ -n "${router_token}" && "${router_token}" != "null" ]]++ # Write token to a temp path and mount it into the router+ local token_dir+ token_dir="/tmp/${MESH_PREFIX}-router-token"+ mkdir -p "${token_dir}"+ echo -n "${router_token}" > "${token_dir}/sa-token"++ # Create data volume for router+ local router_data_vol="${hub_name}-router-data"+ docker volume create "${router_data_vol}" >/dev/null+ CLEANUP_VOLUMES+=("${router_data_vol}")++ # Start Router+ docker run -d \+ --name "${hub_name}" \+ --network "${MESH_NETWORK}" \+ --network-alias sam-hub \+ $(mesh_get_add_hosts) \+ -v "${token_dir}:/var/run/secrets/tokens:ro" \+ -v "${router_data_vol}:/data" \+ "sam-router:local" \+ --control-plane "http://sam-control-plane:8080" \+ --listen "/ip4/0.0.0.0/tcp/4002" \+ --external-addr "/dns4/sam-hub/tcp/4002" \+ --jwt-path "/var/run/secrets/tokens/sa-token" \+ --keys-path "/data/router.key" \+ --allow-loopback \+ --log-level debug >/dev/null++ MESH_CONTAINERS+=("${hub_name}")+ }fidiff --git a/tests/e2e/relay.bats b/tests/e2e/relay.batsindex 0e051d9a..ad0d6d92 100644--- a/tests/e2e/relay.bats+++ b/tests/e2e/relay.bats@@ -63,7 +63,7 @@ teardown() {CLEANUP_NETWORKS+=("${MESH_NETWORK_2}")local hub_node- hub_node=$(kubectl --context="${KUBECONTEXT}" get pod sam-hub-0 -o jsonpath='{.spec.nodeName}')+ hub_node=$(kubectl --context="${KUBECONTEXT}" get pod sam-router-0 -o jsonpath='{.spec.nodeName}')local oidc_nodeoidc_node=$(kubectl --context="${KUBECONTEXT}" get pod -l app=mock-oidc -o jsonpath='{.items[0].spec.nodeName}')diff --git a/tests/e2e/revocation_test.bats b/tests/e2e/revocation_test.batsindex 63fa14b9..9702bb16 100644--- a/tests/e2e/revocation_test.bats+++ b/tests/e2e/revocation_test.bats@@ -19,22 +19,8 @@ teardown() {local hub_keyhub_key="$(mesh_gen_hex32)"- docker run -d \- --name "${hub_name}" \- --network "${MESH_NETWORK}" \- --network-alias sam-hub \- $(mesh_get_add_hosts) \- "sam-hub:local" \- --issuer "http://mock-oidc:18080" \- --client-id "sam-e2e" \- --key "${hub_key}" \- --mesh "${MESH_PREFIX}" \- --admin-token "e2e-token" \- --listen "/ip4/0.0.0.0/tcp/4002" \- --external-multiaddr "/dns4/sam-hub/tcp/4002" \- --log-level debug >/dev/null-- MESH_CONTAINERS+=("${hub_name}")+ mesh_start_standalone_hub "${hub_name}"+mesh_wait_for_log "${hub_name}" "PeerID:" 20local hub_peer_id@@ -82,15 +68,15 @@ teardown() {# Publish ban event for Node 2echo "[$(date +%T)] Publishing ban event"- run docker exec "${hub_name}" /sam-hub admin ban --peer "${node2_peer_id}" --admin-token "e2e-token"+ run docker exec "${hub_name}-cp" /sam-control-plane admin ban --peer "${node2_peer_id}" --db-driver sqlite --db-dsn /data/control-plane.dbecho "admin ban output: $output"[[ "$status" -eq 0 ]]- if [[ "$output" != *"Published BANNED event"* ]]; then- echo "Hub logs:"- docker logs "${hub_name}"+ if [[ "$output" != *"Successfully banned node"* ]]; then+ echo "Control Plane logs:"+ docker logs "${hub_name}-cp"fi- [[ "$output" == *"Published BANNED event"* ]]+ [[ "$output" == *"Successfully banned node"* ]]# Verify Node 1 receives the ban event and logs itrun mesh_wait_for_log "${node1_name}" "Peer banned: ${node2_peer_id}" 20diff --git a/tests/e2e/sam.bats b/tests/e2e/sam.batsindex 955d089b..50d263aa 100644--- a/tests/e2e/sam.bats+++ b/tests/e2e/sam.bats@@ -2,7 +2,8 @@setup() {export SAM_NODE_BINARY="${SAM_NODE_BINARY:-./bin/sam-node}"- export SAM_HUB_BINARY="${SAM_HUB_BINARY:-./bin/sam-hub}"+ export SAM_CONTROL_PLANE_BINARY="${SAM_CONTROL_PLANE_BINARY:-./bin/sam-control-plane}"+ export SAM_ROUTER_BINARY="${SAM_ROUTER_BINARY:-./bin/sam-router}"export MCP_CLIENT_BINARY="${MCP_CLIENT_BINARY:-./bin/mcp-client}"export TEST_TMPDIR@@ -55,8 +56,14 @@ teardown() {-@test "sam-hub --help returns success" {- run "$SAM_HUB_BINARY" --help+@test "sam-control-plane --help returns success" {+ run "$SAM_CONTROL_PLANE_BINARY" --help[[ "$status" -eq 0 ]]- [[ "$output" == *"Sovereign Agent Mesh - Multi-Transport Hub"* ]]+ [[ "$output" == *"Sovereign Agent Mesh - Control Plane"* ]]+}++@test "sam-router --help returns success" {+ run "$SAM_ROUTER_BINARY" --help+ [[ "$status" -eq 0 ]]+ [[ "$output" == *"Sovereign Agent Mesh - libp2p Router Node"* ]]}diff --git a/tests/integration/failover_test.go b/tests/integration/failover_test.goindex 388f5463..104d36d7 100644--- a/tests/integration/failover_test.go+++ b/tests/integration/failover_test.go@@ -17,6 +17,7 @@ package integration_testimport ("context""fmt"+ "io""net/http""net/http/httptest""net/http/httputil"@@ -29,13 +30,16 @@ import ("testing""time"+ "github.com/google/sam/api""github.com/libp2p/go-libp2p""github.com/libp2p/go-libp2p/core/peer""github.com/multiformats/go-multiaddr"+ "google.golang.org/protobuf/proto")func TestFailoverUpdatesRelay(t *testing.T) {- hubBin := buildBinary(t, "./cmd/sam-hub")+ cpBin := buildBinary(t, "./cmd/sam-control-plane")+ routerBin := buildBinary(t, "./cmd/sam-router")nodeBin := buildBinary(t, "./cmd/sam-node")tmpDir := t.TempDir()@@ -46,25 +50,33 @@ func TestFailoverUpdatesRelay(t *testing.T) {bindings: []roles: {}`- if err := os.WriteFile(policyFile, []byte(policyContent), 0644); err != nil {- t.Fatal(err)- }+ writePolicyWithRouter(t, policyFile, policyContent)- httpPortA := getFreePort(t)- p2pPortA := getFreePort(t)- httpPortB := getFreePort(t)- p2pPortB := getFreePort(t)+ httpPortCP_A := getFreePort(t)+ routerPortA := getFreePort(t)+ httpPortCP_B := getFreePort(t)+ routerPortB := getFreePort(t)// Mock OIDC- oidcURL, jwtTokenStr := startMockOIDC(t)+ oidcURL, mintToken := startCustomMockOIDC(t)++ routerJWT := mintToken(map[string]interface{}{+ "sub": "router-integration-1",+ "groups": []string{"routers"},+ })++ nodeJWT := mintToken(map[string]interface{}{+ "sub": "mock-user",+ })+jwtPath := filepath.Join(tmpDir, "jwt.txt")- if err := os.WriteFile(jwtPath, []byte(jwtTokenStr), 0644); err != nil {+ if err := os.WriteFile(jwtPath, []byte(nodeJWT), 0644); err != nil {t.Fatal(err)}// Dynamic Load Balancer for HTTP discoveryvar lbMu sync.Mutex- activeHubPort := httpPortA+ activeHubPort := httpPortCP_Alb := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {lbMu.Lock()@@ -77,58 +89,111 @@ roles: {}}))defer lb.Close()- // Start Hub A- cmdA := exec.Command(hubBin,- "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortA),- "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortA),+ dbPathA := filepath.Join(tmpDir, "cp_keysA.db")+ dbPathB := filepath.Join(tmpDir, "cp_keysB.db")+ // We override journal_mode to DELETE (disabling WAL) because WAL mode caches+ // transactions in a separate -wal file. Since this test copies dbPathA to dbPathB+ // while CP A is running, WAL mode would cause the copy to be stale/corrupt.+ // We also retain busy_timeout(5000) so that concurrent operations wait for locks+ // rather than returning SQLITE_BUSY immediately.+ dsnA := dbPathA + "?_pragma=journal_mode(DELETE)&_pragma=busy_timeout(5000)"+ dsnB := dbPathB + "?_pragma=journal_mode(DELETE)&_pragma=busy_timeout(5000)"++ // 1. Start Control Plane A temporarily to generate keyring+ cmdCP_A_temp := exec.Command(cpBin,+ "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortCP_A),"--policy-file", policyFile,- "--keys-db", filepath.Join(tmpDir, "keysA.db"),- "--allow-loopback",+ "--db-dsn", dsnA,"--issuer", oidcURL,"--insecure-skip-tls-verify",)- var stdoutA, stderrA safeBuffer- cmdA.Stdout = &stdoutA- cmdA.Stderr = &stderrA- if err := cmdA.Start(); err != nil {- t.Fatalf("failed to start Hub A: %v", err)- }-- // Fetch Hub A's Peer ID- peerIDA := fetchPeerID(t, httpPortA)- t.Logf("Hub A PeerID: %s", peerIDA) // Copy keysA.db to keysB.db to avoid bbolt file lock conflicts- keysA := filepath.Join(tmpDir, "keysA.db")- keysB := filepath.Join(tmpDir, "keysB.db")- data, err := os.ReadFile(keysA)+ if err := cmdCP_A_temp.Start(); err != nil {+ t.Fatalf("failed to start temporary CP A: %v", err)+ }+ waitForControlPlane(t, httpPortCP_A)+ _ = cmdCP_A_temp.Process.Signal(os.Interrupt)+ _ = cmdCP_A_temp.Wait()++ // 2. Copy CP A keys to CP B to share the key-ring+ keysData, err := os.ReadFile(dbPathA)if err != nil {- t.Fatalf("Failed to read keysA.db: %v", err)+ t.Fatalf("Failed to read cp_keysA.db: %v", err)}- if err := os.WriteFile(keysB, data, 0600); err != nil {- t.Fatalf("Failed to write keysB.db: %v", err)+ if err := os.WriteFile(dbPathB, keysData, 0600); err != nil {+ t.Fatalf("Failed to write cp_keysB.db: %v", err)}- // Start Hub B- cmdB := exec.Command(hubBin,- "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortB),- "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortB),+ // 3. Start CP A persistently+ cmdCP_A := exec.Command(cpBin,+ "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortCP_A),"--policy-file", policyFile,- "--keys-db", keysB,+ "--db-dsn", dsnA,+ "--issuer", oidcURL,+ "--insecure-skip-tls-verify",+ )+ if err := cmdCP_A.Start(); err != nil {+ t.Fatalf("failed to start CP A: %v", err)+ }+ defer func() { _ = cmdCP_A.Process.Kill(); _ = cmdCP_A.Wait() }()++ waitForControlPlane(t, httpPortCP_A)++ // 4. Start Router A+ cmdRouterA := exec.Command(routerBin,+ "--control-plane", fmt.Sprintf("http://127.0.0.1:%d", httpPortCP_A),+ "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", routerPortA),+ "--keys-path", filepath.Join(tmpDir, "router_keysA.db"),"--allow-loopback",+ "--oidc-token", routerJWT,+ )+ if err := cmdRouterA.Start(); err != nil {+ t.Fatalf("failed to start Router A: %v", err)+ }+ defer func() { _ = cmdRouterA.Process.Kill(); _ = cmdRouterA.Wait() }()++ // Wait for Router A to renew lease and register in CP A+ time.Sleep(3 * time.Second)++ // 5. Start CP B+ cmdCP_B := exec.Command(cpBin,+ "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortCP_B),+ "--policy-file", policyFile,+ "--db-dsn", dsnB,"--issuer", oidcURL,"--insecure-skip-tls-verify",)- var stdoutB, stderrB safeBuffer- cmdB.Stdout = &stdoutB- cmdB.Stderr = &stderrB- if err := cmdB.Start(); err != nil {- t.Fatalf("failed to start Hub B: %v", err)+ if err := cmdCP_B.Start(); err != nil {+ t.Fatalf("failed to start CP B: %v", err)+ }+ defer func() { _ = cmdCP_B.Process.Kill(); _ = cmdCP_B.Wait() }()++ waitForControlPlane(t, httpPortCP_B)++ // Start Router B+ cmdRouterB := exec.Command(routerBin,+ "--control-plane", fmt.Sprintf("http://127.0.0.1:%d", httpPortCP_B),+ "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", routerPortB),+ "--keys-path", filepath.Join(tmpDir, "router_keysB.db"),+ "--allow-loopback",+ "--oidc-token", routerJWT,+ )+ if err := cmdRouterB.Start(); err != nil {+ t.Fatalf("failed to start Router B: %v", err)}- defer func() { _ = cmdB.Process.Kill() }()+ defer func() { _ = cmdRouterB.Process.Kill(); _ = cmdRouterB.Wait() }()- peerIDB := fetchPeerID(t, httpPortB)- t.Logf("Hub B PeerID: %s", peerIDB)+ // Wait for Router B to renew lease and register in CP B+ time.Sleep(3 * time.Second)- // Start Node B+ // Fetch Router B PeerID+ peerInfoList := fetchActiveRouters(t, httpPortCP_B)+ if len(peerInfoList) != 1 {+ t.Fatalf("expected 1 router registered on CP B, got %d", len(peerInfoList))+ }+ peerIDB := peerInfoList[0]+ t.Logf("Router B PeerID: %s", peerIDB)++ // Start NodenodeHome := filepath.Join(tmpDir, "node-home")env := append(os.Environ(),"HOME="+nodeHome,@@ -155,9 +220,9 @@ roles: {}if err := cmdNode.Start(); err != nil {t.Fatal(err)}- defer func() { _ = cmdNode.Process.Kill() }()+ defer func() { _ = cmdNode.Process.Kill(); _ = cmdNode.Wait() }()- // Wait for Node B to get a reservation on Hub A+ // Wait for Node to get a reservation on Router Avar nodePeerID stringvar out stringfor i := 0; i < 100; i++ {@@ -175,18 +240,19 @@ roles: {}time.Sleep(100 * time.Millisecond)}if nodePeerID == "" {- t.Fatalf("Node B failed to get PeerID in time.\nOutput:\n%s", out)+ t.Fatalf("Node failed to get PeerID in time.\nOutput:\n%s", out)}- t.Logf("Node B started. Node PeerID: %s", nodePeerID)+ t.Logf("Node started. Node PeerID: %s", nodePeerID)- // Now FAILOVER: Switch LB to Hub B and KILL Hub A+ // Now FAILOVER: Switch LB to CP B and KILL CP A and Router At.Logf("Initiating failover to Hub B...")lbMu.Lock()- activeHubPort = httpPortB+ activeHubPort = httpPortCP_BlbMu.Unlock()- _ = cmdA.Process.Kill()+ _ = cmdCP_A.Process.Kill()+ _ = cmdRouterA.Process.Kill()- // Wait for Node B's AutoRelay to get updated+ // Wait for Node's AutoRelay to get updatedfor i := 0; i < 150; i++ {out = stdoutNode.String() + stderrNode.String()if strings.Contains(out, "Successfully reconnected to Hub via HTTP fallback") {@@ -195,12 +261,12 @@ roles: {}time.Sleep(100 * time.Millisecond)}if !strings.Contains(out, "Successfully reconnected to Hub via HTTP fallback") {- t.Fatalf("Node B failed to detect failover and reconnect.\nOutput:\n%s", out)+ t.Fatalf("Node failed to detect failover and reconnect.\nOutput:\n%s", out)}- t.Log("Node B successfully reconnected to Hub B!")+ t.Log("Node successfully reconnected to Hub B!")- // Final verification: Ensure we can actually reach Node B via the Hub B relay- relayAddrStr := fmt.Sprintf("/ip4/127.0.0.1/tcp/%d/p2p/%s/p2p-circuit/p2p/%s", p2pPortB, peerIDB, nodePeerID)+ // Final verification: Ensure we can actually reach Node B via the Router B relay+ relayAddrStr := fmt.Sprintf("/ip4/127.0.0.1/tcp/%d/p2p/%s/p2p-circuit/p2p/%s", routerPortB, peerIDB, nodePeerID)relayAddr, err := multiaddr.NewMultiaddr(relayAddrStr)if err != nil {t.Fatal(err)@@ -220,7 +286,7 @@ roles: {}t.Fatal(err)}connectErr = clientHost.Connect(ctx, *addrInfo)- clientHost.Close() // nolint:errcheck+ _ = clientHost.Close()if connectErr == nil {break}@@ -232,3 +298,40 @@ roles: {}}t.Log("Successfully connected to Node B via Hub B relay circuit!")}++func fetchActiveRouters(t *testing.T, httpPort int) []string {+ t.Helper()+ client := &http.Client{Timeout: 5 * time.Second}+ resp, err := client.Get(fmt.Sprintf("http://127.0.0.1:%d/info", httpPort))+ if err != nil {+ t.Fatalf("failed to query /info: %v", err)+ }+ defer func() { _ = resp.Body.Close() }()++ if resp.StatusCode != http.StatusOK {+ t.Fatalf("query /info returned status %d", resp.StatusCode)+ }++ body, err := io.ReadAll(resp.Body)+ if err != nil {+ t.Fatal(err)+ }++ var info api.HubInfoResponse+ if err := proto.Unmarshal(body, &info); err != nil {+ t.Fatalf("failed to unmarshal HubInfoResponse: %v", err)+ }++ var peerIDs []string+ for _, addrStr := range info.HubAddresses {+ ma, err := multiaddr.NewMultiaddr(addrStr)+ if err != nil {+ continue+ }+ pi, err := peer.AddrInfoFromP2pAddr(ma)+ if err == nil {+ peerIDs = append(peerIDs, pi.ID.String())+ }+ }+ return peerIDs+}diff --git a/tests/integration/fallback_test.go b/tests/integration/fallback_test.goindex 9730853e..3a2cedbb 100644--- a/tests/integration/fallback_test.go+++ b/tests/integration/fallback_test.go@@ -17,7 +17,9 @@ package integration_testimport ("bytes""crypto/ed25519"+ "crypto/rand""encoding/json"+ "io""os""os/exec"@@ -69,6 +71,11 @@ func TestSelfHealingHTTPFallback(t *testing.T) {var mu sync.Mutexvar currentP2PAddr string+ pub, priv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatalf("Failed to generate hub key: %v", err)+ }+createNewHost := func() host.Host {newH, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))if err != nil {@@ -84,7 +91,10 @@ func TestSelfHealingHTTPFallback(t *testing.T) {defer reader.ReleaseMsg(msg)writer := msgio.NewVarintWriter(s)- resp := &api.AuthResponse{Success: true}+ resp := &api.AuthResponse{+ Success: true,+ Biscuit: createMockBiscuitToken(t, newH.ID().String(), priv),+ }respBytes, _ := proto.Marshal(resp)_ = writer.WriteMsg(respBytes)})@@ -132,9 +142,20 @@ func TestSelfHealingHTTPFallback(t *testing.T) {mu.Lock()addr := currentP2PAddrmu.Unlock()- pub, _, _ := ed25519.GenerateKey(nil)++ body, err := io.ReadAll(r.Body)+ if err != nil {+ http.Error(w, "Failed to read body", http.StatusBadRequest)+ return+ }+ var enrollReq api.EnrollRequest+ if err := proto.Unmarshal(body, &enrollReq); err != nil {+ http.Error(w, "Invalid request", http.StatusBadRequest)+ return+ }+resp := &api.EnrollResponse{- BiscuitToken: []byte("mock-biscuit-token"),+ BiscuitToken: createMockBiscuitToken(t, enrollReq.PeerId, priv),HubPublicKey: pub,HubAddresses: []string{addr},}diff --git a/tests/integration/federation_test.go b/tests/integration/federation_test.goindex f189d8df..922b8a3e 100644--- a/tests/integration/federation_test.go+++ b/tests/integration/federation_test.go@@ -1,3 +1,17 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+package integration_testimport (@@ -15,12 +29,13 @@ import ("testing""time"- "github.com/google/sam/api"- "google.golang.org/protobuf/proto"+ "github.com/libp2p/go-libp2p/core/crypto"+ "github.com/libp2p/go-libp2p/core/peer")func TestHubFederationAndRelay(t *testing.T) {- hubBin := buildBinary(t, "./cmd/sam-hub")+ cpBin := buildBinary(t, "./cmd/sam-control-plane")+ routerBin := buildBinary(t, "./cmd/sam-router")nodeBin := buildBinary(t, "./cmd/sam-node")clientBin := buildBinary(t, "./cmd/mcp-client")@@ -38,14 +53,11 @@ roles:- "mcp://*"- "system://sam.catalog"`- if err := os.WriteFile(policyFile, []byte(policyContent), 0644); err != nil {- t.Fatal(err)- }+ writePolicyWithRouter(t, policyFile, policyContent)- portA := getFreePort(t)- portB := getFreePort(t)- p2pPortA := getFreePort(t)- p2pPortB := getFreePort(t)+ cpPort := getFreePort(t)+ routerPortA := getFreePort(t)+ routerPortB := getFreePort(t)// Start Fake DNS ServerdnsServer, err := NewFakeDNSServer(map[string]string{@@ -56,103 +68,168 @@ roles:}t.Cleanup(func() { dnsServer.Hijack(t) })- // Start Hub A+ // 1. Pre-generate keys and Peer IDs for Router A and Router B+ privA, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ if err != nil {+ t.Fatal(err)+ }+ privAData, err := crypto.MarshalPrivateKey(privA)+ if err != nil {+ t.Fatal(err)+ }+ peerIDA, err := peer.IDFromPrivateKey(privA)+ if err != nil {+ t.Fatal(err)+ }+ routerKeysPathA := filepath.Join(tmpDir, "router_keysA.db")+ if err := os.WriteFile(routerKeysPathA, privAData, 0600); err != nil {+ t.Fatal(err)+ }++ privB, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ if err != nil {+ t.Fatal(err)+ }+ privBData, err := crypto.MarshalPrivateKey(privB)+ if err != nil {+ t.Fatal(err)+ }+ peerIDB, err := peer.IDFromPrivateKey(privB)+ if err != nil {+ t.Fatal(err)+ }+ routerKeysPathB := filepath.Join(tmpDir, "router_keysB.db")+ if err := os.WriteFile(routerKeysPathB, privBData, 0600); err != nil {+ t.Fatal(err)+ }+// Mock OIDC- oidcURL, jwtTokenStr := startMockOIDC(t)+ oidcURL, mintToken := startCustomMockOIDC(t)++ routerJWT := mintToken(map[string]interface{}{+ "sub": "router-integration-1",+ "groups": []string{"routers"},+ })- cmdA := exec.Command(hubBin,- "--bind-address", fmt.Sprintf("127.0.0.1:%d", portA),- "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortA),+ // Start Control Plane. We use journal_mode(DELETE) and busy_timeout(5000)+ // to ensure concurrent node enrollment writes do not trigger SQLITE_BUSY locking failures.+ cmdCP := exec.Command(cpBin,+ "--bind-address", fmt.Sprintf("127.0.0.1:%d", cpPort),"--policy-file", policyFile,- "--keys-db", filepath.Join(tmpDir, "keysA.db"),- "--allow-loopback",- "--external-multiaddr", "/dnsaddr/test-hub.local",+ "--db-dsn", filepath.Join(tmpDir, "cp-keys.db")+"?_pragma=journal_mode(DELETE)&_pragma=busy_timeout(5000)","--issuer", oidcURL,"--insecure-skip-tls-verify",- "--key", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",)- cmdA.Env = append(os.Environ(), "SAM_TEST_DNS_SERVER="+dnsServer.conn.LocalAddr().String())- var stdoutA, stderrA safeBuffer- cmdA.Stdout = &stdoutA- cmdA.Stderr = &stderrA-- if err := cmdA.Start(); err != nil {- t.Fatalf("failed to start Hub A: %v", err)+ cmdCP.Stdout = os.Stdout+ cmdCP.Stderr = os.Stderr+ if err := cmdCP.Start(); err != nil {+ t.Fatalf("failed to start CP: %v", err)}- defer func() { _ = cmdA.Process.Kill() }()+ defer func() { _ = cmdCP.Process.Kill(); _ = cmdCP.Wait() }()- // Fetch Hub A's Peer ID (which actively loops and waits for readiness)- peerIDA := fetchPeerID(t, portA)+ waitForControlPlane(t, cpPort)- // Update Fake DNS with Hub A's TXT record- dnsServer.UpdateTXT("_dnsaddr.test-hub.local", []string{- fmt.Sprintf("dnsaddr=/ip4/127.0.0.1/tcp/%d/p2p/%s", p2pPortA, peerIDA),- })+ // Start Router A+ cmdRouterA := exec.Command(routerBin,+ "--control-plane", fmt.Sprintf("http://127.0.0.1:%d", cpPort),+ "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", routerPortA),+ "--keys-path", routerKeysPathA,+ "--allow-loopback",+ "--external-addr", "/dnsaddr/test-hub.local",+ "--oidc-token", routerJWT,+ )+ var stdoutRouterA, stderrRouterA safeBuffer+ cmdRouterA.Stdout = &stdoutRouterA+ cmdRouterA.Stderr = &stderrRouterA+ cmdRouterA.Env = append(os.Environ(), "SAM_TEST_DNS_SERVER="+dnsServer.conn.LocalAddr().String())+ if err := cmdRouterA.Start(); err != nil {+ t.Fatalf("failed to start Router A: %v", err)+ }+ defer func() { _ = cmdRouterA.Process.Kill(); _ = cmdRouterA.Wait() }()- // Start Hub B, explicitly federating with Hub A via DNS- cmdB := exec.Command(hubBin,- "--bind-address", fmt.Sprintf("127.0.0.1:%d", portB),- "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortB),- "--policy-file", policyFile,- "--keys-db", filepath.Join(tmpDir, "keysB.db"),+ // Start Router B+ cmdRouterB := exec.Command(routerBin,+ "--control-plane", fmt.Sprintf("http://127.0.0.1:%d", cpPort),+ "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", routerPortB),+ "--keys-path", routerKeysPathB,"--allow-loopback",- "--external-multiaddr", "/dnsaddr/test-hub.local",- "--issuer", oidcURL,- "--insecure-skip-tls-verify",- "--key", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",+ "--external-addr", "/dnsaddr/test-hub.local",+ "--oidc-token", routerJWT,)- cmdB.Env = append(os.Environ(), "SAM_TEST_DNS_SERVER="+dnsServer.conn.LocalAddr().String())- var stdoutB, stderrB safeBuffer- cmdB.Stdout = io.MultiWriter(os.Stdout, &stdoutB)- cmdB.Stderr = io.MultiWriter(os.Stderr, &stderrB)+ var stdoutRouterB, stderrRouterB safeBuffer+ cmdRouterB.Stdout = &stdoutRouterB+ cmdRouterB.Stderr = &stderrRouterB+ cmdRouterB.Env = append(os.Environ(), "SAM_TEST_DNS_SERVER="+dnsServer.conn.LocalAddr().String())+ if err := cmdRouterB.Start(); err != nil {+ t.Fatalf("failed to start Router B: %v", err)+ }+ defer func() { _ = cmdRouterB.Process.Kill(); _ = cmdRouterB.Wait() }()++ // Wait for routers to lease+ time.Sleep(3 * time.Second)- if err := cmdB.Start(); err != nil {- t.Fatalf("failed to start Hub B: %v", err)+ // Verify both routers registered on the control plane+ peerInfoList := fetchActiveRouters(t, cpPort)+ if len(peerInfoList) != 2 {+ t.Fatalf("expected 2 active routers registered on control plane, got %d\nRouter A Stderr:\n%s\nRouter B Stderr:\n%s",+ len(peerInfoList), stderrRouterA.String(), stderrRouterB.String())}- defer func() { _ = cmdB.Process.Kill() }()+ // Update Fake DNS with Router A and Router B multiaddresses+ dnsServer.UpdateTXT("_dnsaddr.test-hub.local", []string{+ fmt.Sprintf("dnsaddr=/ip4/127.0.0.1/tcp/%d/p2p/%s", routerPortA, peerIDA),+ fmt.Sprintf("dnsaddr=/ip4/127.0.0.1/tcp/%d/p2p/%s", routerPortB, peerIDB),+ })++ // Trigger connection sync manually to form the federation link between Router A and Router Btime.Sleep(2 * time.Second)- // Node A connects to Hub A+ nodeJWT := mintToken(map[string]interface{}{+ "sub": "mock-user",+ })++ // Node A connects to Router A (via shared CP)apiPortA := getFreePort(t)- nodeACmd := exec.Command(nodeBin, "run", "--hub", fmt.Sprintf("http://127.0.0.1:%d", portA),+ nodeACmd := exec.Command(nodeBin, "run", "--hub", fmt.Sprintf("http://127.0.0.1:%d", cpPort),"--data-dir", filepath.Join(tmpDir, "nodeA"),"--bind-addr", fmt.Sprintf("127.0.0.1:%d", apiPortA),"--api-token", "tokenA",- "--jwt", jwtTokenStr,+ "--jwt", nodeJWT,"--listen", "/ip4/127.0.0.1/tcp/0","--listen", "/ip4/127.0.0.1/udp/0/quic-v1","--discovery-interval", "100ms","--enable-relay=true","--allow-loopback=true",)+ nodeACmd.Env = append(os.Environ(), "SAM_TEST_DNS_SERVER="+dnsServer.conn.LocalAddr().String())var nodeStdoutA, nodeStderrA safeBuffernodeACmd.Stdout = io.MultiWriter(os.Stdout, &nodeStdoutA)nodeACmd.Stderr = io.MultiWriter(os.Stderr, &nodeStderrA)if err := nodeACmd.Start(); err != nil {t.Fatalf("failed to start Node A: %v", err)}- defer func() { _ = nodeACmd.Process.Kill() }()+ defer func() { _ = nodeACmd.Process.Kill(); _ = nodeACmd.Wait() }()- // Node B connects to Hub B+ // Node B connects to Router B (via shared CP)apiPortB := getFreePort(t)- nodeBCmd := exec.Command(nodeBin, "run", "--hub", fmt.Sprintf("http://127.0.0.1:%d", portB),+ nodeBCmd := exec.Command(nodeBin, "run", "--hub", fmt.Sprintf("http://127.0.0.1:%d", cpPort),"--data-dir", filepath.Join(tmpDir, "nodeB"),"--bind-addr", fmt.Sprintf("127.0.0.1:%d", apiPortB),"--api-token", "tokenB",- "--jwt", jwtTokenStr,+ "--jwt", nodeJWT,"--listen", "/ip4/127.0.0.1/tcp/0","--listen", "/ip4/127.0.0.1/udp/0/quic-v1","--discovery-interval", "100ms","--allow-loopback=true",)+ nodeBCmd.Env = append(os.Environ(), "SAM_TEST_DNS_SERVER="+dnsServer.conn.LocalAddr().String())var nodeStdoutB, nodeStderrB safeBuffernodeBCmd.Stdout = io.MultiWriter(os.Stdout, &nodeStdoutB)nodeBCmd.Stderr = io.MultiWriter(os.Stderr, &nodeStderrB)if err := nodeBCmd.Start(); err != nil {t.Fatalf("failed to start Node B: %v", err)}- defer func() { _ = nodeBCmd.Process.Kill() }()+ defer func() { _ = nodeBCmd.Process.Kill(); _ = nodeBCmd.Wait() }()// Give nodes time to connect and syncwaitForDHTReady(t, clientBin, apiPortA, "tokenA")@@ -180,7 +257,7 @@ roles:"-args", `{"type": "mcp"}`,)- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)+ ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)defer cancel()var found bool@@ -191,11 +268,10 @@ roles:t.Logf("Node A Stderr: %s", nodeStderrA.String())t.Logf("Node B Stdout: %s", nodeStdoutB.String())t.Logf("Node B Stderr: %s", nodeStderrB.String())- t.Fatalf("timed out waiting to discover tool from Node B. Hub A logs: %s, Hub B logs: %s", stderrA.String(), stderrB.String())+ t.Fatalf("timed out waiting to discover tool from Node B.")default:out, err := searchCmd.Output()if err == nil {- // For simplicity, just check if the output contains the nameif strings.Contains(string(out), "federated-tool") {found = truebreak@@ -241,77 +317,3 @@ roles:}t.Log("Datapath works!")}--func fetchPeerID(t *testing.T, port int) string {- var bodyBytes []byte- var err error-- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)- defer cancel()-- for {- var resp *http.Response- resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:%d/info", port))- if err == nil {- bodyBytes, err = io.ReadAll(resp.Body)- _ = resp.Body.Close()- if err == nil {- break- }- }-- select {- case <-ctx.Done():- t.Fatalf("timed out waiting for /info: %v", err)- case <-time.After(100 * time.Millisecond):- }- }-- var info api.HubInfoResponse- if err := proto.Unmarshal(bodyBytes, &info); err != nil {- t.Fatalf("failed to decode /info: %v", err)- }-- // Hub's PeerID is derived from its first listen multiaddr's /p2p/ segment- var peerID string- if len(info.HubAddresses) > 0 {- peerID = extractPeerID(info.HubAddresses[0])- }- if peerID == "" {- t.Fatalf("empty peer id from /info")- }- return peerID-}--func extractPeerID(maddrStr string) string {- parts := strings.Split(maddrStr, "/")- for i, part := range parts {- if part == "p2p" && i+1 < len(parts) {- return parts[i+1]- }- }- return ""-}--func waitForDHTReady(t *testing.T, clientBin string, apiPort int, token string) {- t.Helper()- deadline := time.Now().Add(45 * time.Second)- cmdArgs := []string{- "-url", fmt.Sprintf("http://127.0.0.1:%d/mcp", apiPort),- "-token", token,- "-tool", "get_mesh_info",- "-args", `{}`,- }-- for time.Now().Before(deadline) {- cmd := exec.Command(clientBin, cmdArgs...)- out, err := cmd.Output()- if err == nil {- if strings.Contains(string(out), `"dht_size":`) && !strings.Contains(string(out), `"dht_size":0`) {- return- }- }- time.Sleep(500 * time.Millisecond)- }- t.Fatalf("DHT not ready on port %d", apiPort)-}diff --git a/tests/integration/local_policy_test.go b/tests/integration/local_policy_test.goindex 1694fddc..4b1649f9 100644--- a/tests/integration/local_policy_test.go+++ b/tests/integration/local_policy_test.go@@ -10,7 +10,6 @@ import ()func TestLocalPolicyCanGrantPermissions(t *testing.T) {- hubBin := buildBinary(t, "./cmd/sam-hub")nodeBin := buildBinary(t, "./cmd/sam-node")tmpDir := t.TempDir()@@ -33,25 +32,8 @@ bindings:t.Fatal(err)}- httpPortHub := getFreePort(t)- p2pPortHub := getFreePort(t)-- cmdHub := exec.Command(hubBin,- "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortHub),- "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortHub),- "--policy-file", hubPolicyFile,- "--keys-db", filepath.Join(tmpDir, "keys.db"),- "--allow-loopback",- "--issuer", oidcURL,- "--insecure-skip-tls-verify",- )- cmdHub.Stdout = os.Stdout- cmdHub.Stderr = os.Stderr- if err := cmdHub.Start(); err != nil {- t.Fatalf("Failed to start Hub: %v", err)- }- defer func() { _ = cmdHub.Process.Kill(); _ = cmdHub.Wait() }()- fetchPeerID(t, httpPortHub)+ httpPortHub, cleanupHub := startControlPlaneAndRouter(t, tmpDir, oidcURL, mintToken, hubPolicyFile)+ defer cleanupHub()// Node B Config with a permissive local policynodeBPolicyFile := filepath.Join(tmpDir, "nodeB_config.yaml")@@ -170,7 +152,6 @@ attenuation:}func TestLocalPolicyCannotBypassHubTargetConstraint(t *testing.T) {- hubBin := buildBinary(t, "./cmd/sam-hub")nodeBin := buildBinary(t, "./cmd/sam-node")tmpDir := t.TempDir()@@ -193,25 +174,8 @@ bindings:t.Fatal(err)}- httpPortHub := getFreePort(t)- p2pPortHub := getFreePort(t)-- cmdHub := exec.Command(hubBin,- "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortHub),- "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortHub),- "--policy-file", hubPolicyFile,- "--keys-db", filepath.Join(tmpDir, "keys.db"),- "--allow-loopback",- "--issuer", oidcURL,- "--insecure-skip-tls-verify",- )- cmdHub.Stdout = os.Stdout- cmdHub.Stderr = os.Stderr- if err := cmdHub.Start(); err != nil {- t.Fatalf("Failed to start Hub: %v", err)- }- defer func() { _ = cmdHub.Process.Kill(); _ = cmdHub.Wait() }()- fetchPeerID(t, httpPortHub)+ httpPortHub, cleanupHub := startControlPlaneAndRouter(t, tmpDir, oidcURL, mintToken, hubPolicyFile)+ defer cleanupHub()// Node B Config with a permissive local policy ("allow if true;")// Node B will NOT claim "group:admin-only", so it will fail the Hub's target check.diff --git a/tests/integration/mcp_local_tools_test.go b/tests/integration/mcp_local_tools_test.goindex a1473f1f..6ddaa498 100644--- a/tests/integration/mcp_local_tools_test.go+++ b/tests/integration/mcp_local_tools_test.go@@ -18,14 +18,12 @@ import ("encoding/json""fmt""os"- "os/exec""path/filepath""strings""testing")func TestMCPLocalTools(t *testing.T) {- hubBin := buildBinary(t, "./cmd/sam-hub")nodeBin := buildBinary(t, "./cmd/sam-node")tmpDir := t.TempDir()@@ -40,29 +38,9 @@ roles: {}t.Fatal(err)}- httpPortHub := getFreePort(t)- p2pPortHub := getFreePort(t)-- // Mock OIDC- oidcURL, jwtTokenStr := startMockOIDC(t)-- // Start Real Hub- cmdHub := exec.Command(hubBin,- "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortHub),- "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortHub),- "--policy-file", policyFile,- "--keys-db", filepath.Join(tmpDir, "keys.db"),- "--allow-loopback",- "--issuer", oidcURL,- "--insecure-skip-tls-verify",- )- var stdoutHub, stderrHub safeBuffer- cmdHub.Stdout = &stdoutHub- cmdHub.Stderr = &stderrHub- if err := cmdHub.Start(); err != nil {- t.Fatalf("failed to start Hub: %v", err)- }- defer func() { _ = cmdHub.Process.Kill() }()+ oidcURL, mintToken := startCustomMockOIDC(t)+ httpPortHub, cleanupHub := startControlPlaneAndRouter(t, tmpDir, oidcURL, mintToken, policyFile)+ defer cleanupHub()fetchPeerID(t, httpPortHub)@@ -72,6 +50,10 @@ roles: {}homeA := t.TempDir()homeB := t.TempDir()+ nodeJWT := mintToken(map[string]interface{}{+ "sub": "mock-user",+ })+// Start Node At.Log("Starting Node A...")_ = startBackgroundNode(t, nodeBin, hubURL, homeA,@@ -79,7 +61,7 @@ roles: {}"--listen", "/ip4/127.0.0.1/tcp/0","--bind-addr", "127.0.0.1:0","--api-token", apiToken,- "--jwt", jwtTokenStr,+ "--jwt", nodeJWT,)// Start Node B@@ -89,7 +71,7 @@ roles: {}"--listen", "/ip4/127.0.0.1/tcp/0","--bind-addr", "127.0.0.1:0","--api-token", apiToken,- "--jwt", jwtTokenStr,+ "--jwt", nodeJWT,)// Resolve actual MCP address from logdiff --git a/tests/integration/mesh_test.go b/tests/integration/mesh_test.goindex 0fe329b4..f6f0ead4 100644--- a/tests/integration/mesh_test.go+++ b/tests/integration/mesh_test.go@@ -20,14 +20,26 @@ import ("time")-func TestSamHubHelp(t *testing.T) {- hubBin := buildBinary(t, "./cmd/sam-hub")- stdout, stderr, err := runCommand(t, repoRoot(t), 10*time.Second, nil, "", hubBin, "--help")+func TestSamControlPlaneHelp(t *testing.T) {+ cpBin := buildBinary(t, "./cmd/sam-control-plane")+ stdout, stderr, err := runCommand(t, repoRoot(t), 10*time.Second, nil, "", cpBin, "--help")if err != nil {- t.Fatalf("sam-hub --help failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)+ t.Fatalf("sam-control-plane --help failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)}out := stdout + stderr- if !strings.Contains(out, "Sovereign Agent Mesh - Multi-Transport Hub") {+ if !strings.Contains(out, "Sovereign Agent Mesh - Control Plane") {+ t.Fatalf("unexpected help output:\n%s", out)+ }+}++func TestSamRouterHelp(t *testing.T) {+ routerBin := buildBinary(t, "./cmd/sam-router")+ stdout, stderr, err := runCommand(t, repoRoot(t), 10*time.Second, nil, "", routerBin, "--help")+ if err != nil {+ t.Fatalf("sam-router --help failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)+ }+ out := stdout + stderr+ if !strings.Contains(out, "Sovereign Agent Mesh - libp2p Router Node") {t.Fatalf("unexpected help output:\n%s", out)}}diff --git a/tests/integration/minimal_helpers_test.go b/tests/integration/minimal_helpers_test.goindex 9dc12465..7737a160 100644--- a/tests/integration/minimal_helpers_test.go+++ b/tests/integration/minimal_helpers_test.go@@ -18,6 +18,7 @@ import ("bytes""context""errors"+ "fmt""os""os/exec""path/filepath"@@ -29,6 +30,7 @@ import ("crypto/ed25519""crypto/rand""io"+ "net""net/http""net/http/httptest""net/url"@@ -208,6 +210,11 @@ func runCommandWithCallback(func startMockLibp2pHub(t *testing.T) (peer.ID, string) {t.Helper()+ pub, priv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatalf("Failed to generate hub key: %v", err)+ }+h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))if err != nil {t.Fatalf("failed to create mock libp2p host: %v", err)@@ -223,7 +230,10 @@ func startMockLibp2pHub(t *testing.T) (peer.ID, string) {defer reader.ReleaseMsg(msg)writer := msgio.NewVarintWriter(s)- resp := &api.AuthResponse{Success: true}+ resp := &api.AuthResponse{+ Success: true,+ Biscuit: createMockBiscuitToken(t, h.ID().String(), priv),+ }respBytes, _ := proto.Marshal(resp)_ = writer.WriteMsg(respBytes)})@@ -234,10 +244,6 @@ func startMockLibp2pHub(t *testing.T) (peer.ID, string) {}// Start HTTP server for enrollment- pub, priv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatalf("Failed to generate hub key: %v", err)- }mux := http.NewServeMux()mux.HandleFunc("/register", func(w http.ResponseWriter, r *http.Request) {@@ -285,6 +291,11 @@ func startMockLibp2pHub(t *testing.T) (peer.ID, string) {func startMockLibp2pHubWithOIDC(t *testing.T, oidcIssuerURL string) (peer.ID, string) {t.Helper()+ pub, priv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatalf("Failed to generate hub key: %v", err)+ }+h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))if err != nil {t.Fatalf("failed to create mock libp2p host: %v", err)@@ -300,7 +311,10 @@ func startMockLibp2pHubWithOIDC(t *testing.T, oidcIssuerURL string) (peer.ID, stdefer reader.ReleaseMsg(msg)writer := msgio.NewVarintWriter(s)- resp := &api.AuthResponse{Success: true}+ resp := &api.AuthResponse{+ Success: true,+ Biscuit: createMockBiscuitToken(t, h.ID().String(), priv),+ }respBytes, _ := proto.Marshal(resp)_ = writer.WriteMsg(respBytes)})@@ -311,10 +325,6 @@ func startMockLibp2pHubWithOIDC(t *testing.T, oidcIssuerURL string) (peer.ID, st}// Start HTTP server for enrollment and info- pub, priv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatalf("Failed to generate hub key: %v", err)- }mux := http.NewServeMux()mux.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {@@ -380,9 +390,28 @@ func startMockLibp2pHubWithOIDC(t *testing.T, oidcIssuerURL string) (peer.ID, stfunc createMockBiscuitToken(t *testing.T, peerID string, priv ed25519.PrivateKey) []byte {builder := biscuit.NewBuilder(priv)- _ = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "target_unrestricted"}})+ err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "target_unrestricted"}})+ if err != nil {+ t.Fatalf("failed to add target_unrestricted: %v", err)+ }- err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ err = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: api.FactExpiration,+ IDs: []biscuit.Term{biscuit.Date(time.Now().Add(1 * time.Hour))},+ }})+ if err != nil {+ t.Fatalf("failed to add FactExpiration fact: %v", err)+ }++ err = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: api.FactRole,+ IDs: []biscuit.Term{biscuit.String(api.RoleRouter)},+ }})+ if err != nil {+ t.Fatalf("failed to add role fact: %v", err)+ }++ err = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "node",IDs: []biscuit.Term{biscuit.String(peerID)},}})@@ -415,3 +444,196 @@ func createMockBiscuitToken(t *testing.T, peerID string, priv ed25519.PrivateKey}return serialized}++func startControlPlaneAndRouter(t *testing.T, tmpDir string, oidcURL string, mintToken func(map[string]interface{}) string, policyFile string) (int, func()) {+ cpBin := buildBinary(t, "./cmd/sam-control-plane")+ routerBin := buildBinary(t, "./cmd/sam-router")++ cpPort := getFreePort(t)+ routerPort := getFreePort(t)++ // Automatically adjust the policy file to grant the "router" role to group "routers"+ originalPolicy, err := os.ReadFile(policyFile)+ if err == nil {+ writePolicyWithRouter(t, policyFile, string(originalPolicy))+ }++ // 1. Start Control Plane+ cpCmd := exec.Command(cpBin,+ "--bind-address", fmt.Sprintf("127.0.0.1:%d", cpPort),+ "--policy-file", policyFile,+ "--db-dsn", filepath.Join(tmpDir, "cp-keys.db"),+ "--issuer", oidcURL,+ "--insecure-skip-tls-verify",+ )+ cpCmd.Stdout = os.Stdout+ cpCmd.Stderr = os.Stderr+ if err := cpCmd.Start(); err != nil {+ t.Fatalf("failed to start control plane: %v", err)+ }++ // Wait for CP to be up+ waitForControlPlane(t, cpPort)++ // 2. Start Router+ routerJWT := mintToken(map[string]interface{}{+ "sub": "router-integration-1",+ "groups": []string{"routers"},+ })++ routerCmd := exec.Command(routerBin,+ "--control-plane", fmt.Sprintf("http://127.0.0.1:%d", cpPort),+ "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", routerPort),+ "--keys-path", filepath.Join(tmpDir, "router-keys.db"),+ "--allow-loopback",+ "--oidc-token", routerJWT,+ )+ routerCmd.Stdout = os.Stdout+ routerCmd.Stderr = os.Stderr+ if err := routerCmd.Start(); err != nil {+ _ = cpCmd.Process.Kill()+ _ = cpCmd.Wait()+ t.Fatalf("failed to start router: %v", err)+ }++ // Wait for router lease to be active and registered+ fetchPeerID(t, cpPort)++ cleanup := func() {+ _ = routerCmd.Process.Kill()+ _ = routerCmd.Wait()+ _ = cpCmd.Process.Kill()+ _ = cpCmd.Wait()+ }++ return cpPort, cleanup+}++func getFreePort(t *testing.T) int {+ t.Helper()+ l, err := net.Listen("tcp", "127.0.0.1:0")+ if err != nil {+ t.Fatal(err)+ }+ port := l.Addr().(*net.TCPAddr).Port+ _ = l.Close()+ return port+}++func fetchPeerID(t *testing.T, port int) string {+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)+ defer cancel()++ for {+ var peerID string+ resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/info", port))+ if err == nil {+ bodyBytes, err := io.ReadAll(resp.Body)+ _ = resp.Body.Close()+ if err == nil {+ var info api.HubInfoResponse+ if err := proto.Unmarshal(bodyBytes, &info); err == nil {+ if len(info.HubAddresses) > 0 {+ peerID = extractPeerID(info.HubAddresses[0])+ }+ }+ }+ }++ if peerID != "" {+ return peerID+ }++ select {+ case <-ctx.Done():+ t.Fatalf("timed out waiting for router lease registration on /info: %v", err)+ case <-time.After(100 * time.Millisecond):+ }+ }+}++func extractPeerID(maddrStr string) string {+ parts := strings.Split(maddrStr, "/")+ for i, part := range parts {+ if part == "p2p" && i+1 < len(parts) {+ return parts[i+1]+ }+ }+ return ""+}++func waitForDHTReady(t *testing.T, clientBin string, apiPort int, token string) {+ t.Helper()+ deadline := time.Now().Add(45 * time.Second)+ cmdArgs := []string{+ "-url", fmt.Sprintf("http://127.0.0.1:%d/mcp", apiPort),+ "-token", token,+ "-tool", "get_mesh_info",+ "-args", `{}`,+ }++ for time.Now().Before(deadline) {+ cmd := exec.Command(clientBin, cmdArgs...)+ out, err := cmd.Output()+ if err == nil {+ if strings.Contains(string(out), `"dht_size":`) && !strings.Contains(string(out), `"dht_size":0`) {+ return+ }+ }+ time.Sleep(500 * time.Millisecond)+ }+ t.Fatalf("DHT not ready on port %d", apiPort)+}++func waitForControlPlane(t *testing.T, port int) {+ t.Helper()+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)+ defer cancel()++ for {+ resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/info", port))+ if err == nil {+ _ = resp.Body.Close()+ if resp.StatusCode == http.StatusOK {+ return+ }+ }++ select {+ case <-ctx.Done():+ t.Fatalf("timed out waiting for control plane: %v", err)+ case <-time.After(100 * time.Millisecond):+ }+ }+}++func writePolicyWithRouter(t *testing.T, path string, yamlContent string) {+ t.Helper()+ content := yamlContent++ // Replace empty markers first+ content = strings.ReplaceAll(content, "bindings: []", "bindings:")+ content = strings.ReplaceAll(content, "roles: {}", "roles:")++ routerRole := ` router:+ allowed_services: []+ allowed_targets: []`+ routerBinding := ` - role: router+ members: ["group:routers"]`++ if strings.Contains(content, "roles:") {+ content = strings.Replace(content, "roles:", "roles:\n"+routerRole, 1)+ } else {+ content += "\nroles:\n" + routerRole+ }++ if strings.Contains(content, "bindings:") {+ content = strings.Replace(content, "bindings:", "bindings:\n"+routerBinding, 1)+ } else {+ content += "\nbindings:\n" + routerBinding+ }++ if err := os.WriteFile(path, []byte(content), 0644); err != nil {+ t.Fatal(err)+ }+}diff --git a/tests/integration/mock_oidc_test.go b/tests/integration/mock_oidc_test.goindex 764aa0d5..5ec45ef3 100644--- a/tests/integration/mock_oidc_test.go+++ b/tests/integration/mock_oidc_test.go@@ -14,55 +14,6 @@ import ("github.com/golang-jwt/jwt/v5")-func startMockOIDC(t *testing.T) (string, string) {- privKey, err := rsa.GenerateKey(rand.Reader, 2048)- if err != nil {- t.Fatalf("failed to generate rsa key: %v", err)- }-- mux := http.NewServeMux()- srv := httptest.NewServer(mux)- issuer := srv.URL-- mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {- w.Header().Set("Content-Type", "application/json")- _ = json.NewEncoder(w).Encode(map[string]interface{}{- "issuer": issuer,- "jwks_uri": issuer + "/keys",- })- })-- mux.HandleFunc("/keys", func(w http.ResponseWriter, r *http.Request) {- w.Header().Set("Content-Type", "application/json")- _ = json.NewEncoder(w).Encode(map[string]interface{}{- "keys": []map[string]interface{}{- {- "kty": "RSA",- "alg": "RS256",- "use": "sig",- "kid": "mock-key",- "n": base64.RawURLEncoding.EncodeToString(privKey.N.Bytes()),- "e": base64.RawURLEncoding.EncodeToString(big.NewInt(int64(privKey.E)).Bytes()),- },- },- })- })-- token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{- "iss": issuer,- "aud": "sam-mesh-audience",- "sub": "mock-user",- "exp": time.Now().Add(time.Hour).Unix(),- })- token.Header["kid"] = "mock-key"- jwtStr, err := token.SignedString(privKey)- if err != nil {- t.Fatalf("failed to sign jwt: %v", err)- }-- return issuer, jwtStr-}-func startCustomMockOIDC(t *testing.T) (string, func(claims map[string]interface{}) string) {privKey, err := rsa.GenerateKey(rand.Reader, 2048)if err != nil {@@ -71,6 +22,7 @@ func startCustomMockOIDC(t *testing.T) (string, func(claims map[string]interfacemux := http.NewServeMux()srv := httptest.NewServer(mux)+ t.Cleanup(srv.Close)issuer := srv.URLmux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {diff --git a/tests/integration/multimaster_test.go b/tests/integration/multimaster_test.goindex 9b85b856..266aadbc 100644--- a/tests/integration/multimaster_test.go+++ b/tests/integration/multimaster_test.go@@ -15,34 +15,29 @@package integration_testimport (+ "bytes""context""fmt"- "net"+ "io"+ "net/http""os""os/exec""path/filepath"- "strings""testing""time"+ "github.com/google/sam/api""github.com/libp2p/go-libp2p""github.com/libp2p/go-libp2p/core/peer"+ "github.com/libp2p/go-msgio""github.com/multiformats/go-multiaddr"+ "google.golang.org/protobuf/proto")-func getFreePort(t *testing.T) int {- t.Helper()- l, err := net.Listen("tcp", "127.0.0.1:0")- if err != nil {- t.Fatal(err)- }- port := l.Addr().(*net.TCPAddr).Port- _ = l.Close()- return port-}-func TestMultiMasterHub(t *testing.T) {- hubBin := buildBinary(t, "./cmd/sam-hub")+ cpBin := buildBinary(t, "./cmd/sam-control-plane")+ routerBin := buildBinary(t, "./cmd/sam-router")+tmpDir := t.TempDir()// Create a mock policy file@@ -51,132 +46,266 @@ func TestMultiMasterHub(t *testing.T) {bindings: []roles: {}`- if err := os.WriteFile(policyFile, []byte(policyContent), 0644); err != nil {- t.Fatal(err)- }+ writePolicyWithRouter(t, policyFile, policyContent)portA := getFreePort(t)portB := getFreePort(t)- p2pPortA := getFreePort(t)- p2pPortB := getFreePort(t)+ routerPortA := getFreePort(t)+ routerPortB := getFreePort(t)- // Start Replica A- cmdA := exec.Command(hubBin,+ // Mock OIDC+ oidcURL, mintToken := startCustomMockOIDC(t)++ routerJWT := mintToken(map[string]interface{}{+ "sub": "router-integration-1",+ "groups": []string{"routers"},+ })++ nodeJWT := mintToken(map[string]interface{}{+ "sub": "mock-user",+ })++ jwtPath := filepath.Join(tmpDir, "jwt.txt")+ if err := os.WriteFile(jwtPath, []byte(nodeJWT), 0644); err != nil {+ t.Fatal(err)+ }++ dbPathA := filepath.Join(tmpDir, "cp_keysA.db")+ dbPathB := filepath.Join(tmpDir, "cp_keysB.db")+ // We override journal_mode to DELETE (disabling WAL) because WAL mode caches+ // transactions in a separate -wal file. Since this test copies dbPathA to dbPathB+ // while CP A is running, WAL mode would cause the copy to be stale/corrupt.+ // We also retain busy_timeout(5000) so that concurrent operations wait for locks+ // rather than returning SQLITE_BUSY immediately.+ dsnA := dbPathA + "?_pragma=journal_mode(DELETE)&_pragma=busy_timeout(5000)"+ dsnB := dbPathB + "?_pragma=journal_mode(DELETE)&_pragma=busy_timeout(5000)"++ // 1. Start Control Plane A temporarily to generate keyring+ cmdCP_A_temp := exec.Command(cpBin,"--bind-address", fmt.Sprintf("127.0.0.1:%d", portA),- "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortA),"--policy-file", policyFile,- "--keys-db", filepath.Join(tmpDir, "keysA.db"),- "--allow-loopback",+ "--db-dsn", dsnA,+ "--issuer", oidcURL,+ "--insecure-skip-tls-verify",+ )+ var stdoutCP_A_temp, stderrCP_A_temp safeBuffer+ cmdCP_A_temp.Stdout = &stdoutCP_A_temp+ cmdCP_A_temp.Stderr = &stderrCP_A_temp+ if err := cmdCP_A_temp.Start(); err != nil {+ t.Fatalf("failed to start temporary CP A: %v", err)+ }+ waitForControlPlane(t, portA)+ _ = cmdCP_A_temp.Process.Signal(os.Interrupt)+ _ = cmdCP_A_temp.Wait()+ t.Logf("Temp CP A Stderr:\n%s\nTemp CP A Stdout:\n%s", stderrCP_A_temp.String(), stdoutCP_A_temp.String())++ // 2. Copy CP A keys to CP B to share the key-ring+ keysData, err := os.ReadFile(dbPathA)+ if err != nil {+ t.Fatalf("Failed to read cp_keysA.db: %v", err)+ }+ if err := os.WriteFile(dbPathB, keysData, 0600); err != nil {+ t.Fatalf("Failed to write cp_keysB.db: %v", err)+ }++ // 3. Start CP A persistently+ cmdCP_A := exec.Command(cpBin,+ "--bind-address", fmt.Sprintf("127.0.0.1:%d", portA),+ "--policy-file", policyFile,+ "--db-dsn", dsnA,+ "--issuer", oidcURL,+ "--insecure-skip-tls-verify",)- var stdoutA, stderrA safeBuffer- cmdA.Stdout = &stdoutA- cmdA.Stderr = &stderrA+ var stdoutCP_A, stderrCP_A safeBuffer+ cmdCP_A.Stdout = &stdoutCP_A+ cmdCP_A.Stderr = &stderrCP_A+ if err := cmdCP_A.Start(); err != nil {+ t.Fatalf("failed to start CP A: %v", err)+ }+ defer func() { _ = cmdCP_A.Process.Kill(); _ = cmdCP_A.Wait() }()- if err := cmdA.Start(); err != nil {- t.Fatalf("failed to start Hub Replica A: %v", err)+ waitForControlPlane(t, portA)++ // 4. Start Router A (registered with CP A)+ cmdRouterA := exec.Command(routerBin,+ "--control-plane", fmt.Sprintf("http://127.0.0.1:%d", portA),+ "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", routerPortA),+ "--keys-path", filepath.Join(tmpDir, "router_keysA.db"),+ "--allow-loopback",+ "--oidc-token", routerJWT,+ )+ var stdoutRouterA, stderrRouterA safeBuffer+ cmdRouterA.Stdout = &stdoutRouterA+ cmdRouterA.Stderr = &stderrRouterA+ if err := cmdRouterA.Start(); err != nil {+ t.Fatalf("failed to start Router A: %v", err)}- defer func() { _ = cmdA.Process.Kill() }()+ defer func() { _ = cmdRouterA.Process.Kill(); _ = cmdRouterA.Wait() }()- // Start Replica B- cmdB := exec.Command(hubBin,+ // Wait for Router A to renew lease and register in CP A+ time.Sleep(3 * time.Second)++ // 5. Start Control Plane B (sharing CP A's keys)+ cmdCP_B := exec.Command(cpBin,"--bind-address", fmt.Sprintf("127.0.0.1:%d", portB),- "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortB),"--policy-file", policyFile,- "--keys-db", filepath.Join(tmpDir, "keysB.db"),+ "--db-dsn", dsnB,+ "--issuer", oidcURL,+ "--insecure-skip-tls-verify",+ )+ var stdoutCP_B, stderrCP_B safeBuffer+ cmdCP_B.Stdout = &stdoutCP_B+ cmdCP_B.Stderr = &stderrCP_B+ if err := cmdCP_B.Start(); err != nil {+ t.Fatalf("failed to start CP B: %v", err)+ }+ defer func() { _ = cmdCP_B.Process.Kill(); _ = cmdCP_B.Wait() }()++ waitForControlPlane(t, portB)++ // 5. Start Router B (registered with CP B)+ cmdRouterB := exec.Command(routerBin,+ "--control-plane", fmt.Sprintf("http://127.0.0.1:%d", portB),+ "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", routerPortB),+ "--keys-path", filepath.Join(tmpDir, "router_keysB.db"),"--allow-loopback",+ "--oidc-token", routerJWT,)- var stdoutB, stderrB safeBuffer- cmdB.Stdout = &stdoutB- cmdB.Stderr = &stderrB-- if err := cmdB.Start(); err != nil {- t.Fatalf("failed to start Hub Replica B: %v", err)- }- defer func() { _ = cmdB.Process.Kill() }()-- // Wait for both replicas to be online and parse their Peer IDs- var peerIDA, peerIDB string- for i := 0; i < 50; i++ {- outA := stdoutA.String() + stderrA.String()- outB := stdoutB.String() + stderrB.String()-- if peerIDA == "" {- for _, line := range strings.Split(outA, "\n") {- if strings.Contains(line, "PeerID:") {- parts := strings.Split(line, " ")- peerIDA = strings.TrimSpace(parts[len(parts)-1])- }- }- }-- if peerIDB == "" {- for _, line := range strings.Split(outB, "\n") {- if strings.Contains(line, "PeerID:") {- parts := strings.Split(line, " ")- peerIDB = strings.TrimSpace(parts[len(parts)-1])- }- }- }-- if peerIDA != "" && peerIDB != "" {- break- }- time.Sleep(100 * time.Millisecond)- }-- if peerIDA == "" || peerIDB == "" {- t.Fatalf("replicas failed to report Peer ID in time. A: %q, B: %q\nStdout A:\n%s\nStderr A:\n%s\nStdout B:\n%s\nStderr B:\n%s", peerIDA, peerIDB, stdoutA.String(), stderrA.String(), stdoutB.String(), stderrB.String())- }-- // ASSERT: Both replicas must have different Peer IDs!+ var stdoutRouterB, stderrRouterB safeBuffer+ cmdRouterB.Stdout = &stdoutRouterB+ cmdRouterB.Stderr = &stderrRouterB+ if err := cmdRouterB.Start(); err != nil {+ t.Fatalf("failed to start Router B: %v", err)+ }+ defer func() { _ = cmdRouterB.Process.Kill(); _ = cmdRouterB.Wait() }()++ // Wait for Router B to renew lease and register in CP B+ time.Sleep(3 * time.Second)++ // 6. Fetch router peer IDs+ peerInfoListA := fetchActiveRouters(t, portA)+ if len(peerInfoListA) != 1 {+ t.Fatalf("expected 1 router registered on CP A, got %d", len(peerInfoListA))+ }+ peerIDA := peerInfoListA[0]++ peerInfoListB := fetchActiveRouters(t, portB)+ if len(peerInfoListB) != 1 {+ t.Fatalf("expected 1 router registered on CP B, got %d", len(peerInfoListB))+ }+ peerIDB := peerInfoListB[0]++ // ASSERT: Router A and Router B have unique Peer IDsif peerIDA == peerIDB {- t.Fatalf("expected replicas to have unique Peer IDs, but both got: %s", peerIDA)+ t.Fatalf("expected routers to have unique Peer IDs, but both got: %s", peerIDA)}- t.Logf("Replicas successfully started. Replica A: %s, Replica B: %s", peerIDA, peerIDB)+ t.Logf("Replicas successfully started. Router A: %s, Router B: %s", peerIDA, peerIDB)- // Create a separate libp2p client host- clientHost, err := libp2p.New(- libp2p.NoListenAddrs,- )+ // 7. Start a client libp2p host to represent a node+ clientHost, err := libp2p.New(libp2p.NoListenAddrs)if err != nil {t.Fatal(err)}defer func() { _ = clientHost.Close() }()- // Validate connecting to Replica A- addrA, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d/p2p/%s", p2pPortA, peerIDA))+ // 8. Enroll client host on CP B (obtaining a biscuit minted by CP B)+ clientBiscuit := enrollClientOnControlPlane(t, portB, clientHost.ID(), 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.+ routerAddrA, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d/p2p/%s", routerPortA, peerIDA))+ if err != nil {+ t.Fatal(err)+ }+ routerInfoA, err := peer.AddrInfoFromP2pAddr(routerAddrA)if err != nil {t.Fatal(err)}- infoA, err := peer.AddrInfoFromP2pAddr(addrA)++ t.Logf("Connecting client host %s to Router A at %s...", clientHost.ID(), routerAddrA)+ ctxConnect, cancelConnect := context.WithTimeout(context.Background(), 5*time.Second)+ defer cancelConnect()+ if err := clientHost.Connect(ctxConnect, *routerInfoA); err != nil {+ t.Fatalf("failed to connect client to Router A: %v", err)+ }++ t.Log("Opening auth stream to Router A...")+ s, err := clientHost.NewStream(context.Background(), routerInfoA.ID, api.AuthProtocolID)+ if err != nil {+ t.Fatalf("failed to open auth stream: %v", err)+ }+ defer func() { _ = s.Close() }()++ t.Log("Writing auth frame with CP B biscuit to Router A...")+ writer := msgio.NewVarintWriter(s)+ authFrame := &api.AuthFrame{Biscuit: clientBiscuit}+ authFrameBytes, err := proto.Marshal(authFrame)if err != nil {t.Fatal(err)}+ if err := writer.WriteMsg(authFrameBytes); err != nil {+ t.Fatalf("failed to write auth frame: %v", err)+ }- t.Logf("Dialing Hub Replica A at %s...", addrA)- ctxA, cancelA := context.WithTimeout(context.Background(), 5*time.Second)- defer cancelA()- if err := clientHost.Connect(ctxA, *infoA); err != nil {- t.Fatalf("failed to connect to Hub Replica A: %v", err)+ t.Log("Reading auth response from Router A...")+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ respMsg, err := reader.ReadMsg()+ if err != nil {+ t.Fatalf("failed to read response from Router A: %v\nRouter A Stderr:\n%s\nRouter A Stdout:\n%s\nCP A Stderr:\n%s\nCP B Stderr:\n%s",+ err, stderrRouterA.String(), stdoutRouterA.String(), stderrCP_A.String(), stderrCP_B.String())+ }+ defer reader.ReleaseMsg(respMsg)++ var authResp api.AuthResponse+ if err := proto.Unmarshal(respMsg, &authResp); err != nil {+ t.Fatalf("failed to unmarshal response: %v", err)+ }++ if !authResp.Success {+ t.Fatalf("mutual auth with Router A was rejected: %s\nRouter A Stderr:\n%s\nRouter A Stdout:\n%s\nCP A Stderr:\n%s\nCP B Stderr:\n%s",+ authResp.Error, stderrRouterA.String(), stdoutRouterA.String(), stderrCP_A.String(), stderrCP_B.String())}- t.Log("Successfully connected and authenticated with Hub Replica A!")- // Validate connecting to Replica B- addrB, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d/p2p/%s", p2pPortB, peerIDB))+ t.Log("Successfully verified multi-master control plane signature trust!")+}++func enrollClientOnControlPlane(t *testing.T, cpPort int, clientID peer.ID, jwtToken string) []byte {+ t.Helper()++ req := &api.EnrollRequest{+ Jwt: jwtToken,+ PeerId: clientID.String(),+ }+ reqBytes, err := proto.Marshal(req)if err != nil {t.Fatal(err)}- infoB, err := peer.AddrInfoFromP2pAddr(addrB)++ resp, err := http.Post(+ fmt.Sprintf("http://127.0.0.1:%d/register", cpPort),+ "application/octet-stream",+ bytes.NewReader(reqBytes),+ )+ if err != nil {+ t.Fatalf("failed to send enroll request: %v", err)+ }+ defer func() { _ = resp.Body.Close() }()++ if resp.StatusCode != http.StatusOK {+ body, _ := io.ReadAll(resp.Body)+ t.Fatalf("enroll request failed with status %d: %s", resp.StatusCode, string(body))+ }++ respBytes, err := io.ReadAll(resp.Body)if err != nil {t.Fatal(err)}- t.Logf("Dialing Hub Replica B at %s...", addrB)- ctxB, cancelB := context.WithTimeout(context.Background(), 5*time.Second)- defer cancelB()- if err := clientHost.Connect(ctxB, *infoB); err != nil {- t.Fatalf("failed to connect to Hub Replica B: %v", err)+ var enrollResp api.EnrollResponse+ if err := proto.Unmarshal(respBytes, &enrollResp); err != nil {+ t.Fatalf("failed to decode enroll response: %v", err)}- t.Log("Successfully connected and authenticated with Hub Replica B!")++ return enrollResp.BiscuitToken}diff --git a/tests/integration/policy_permutations_test.go b/tests/integration/policy_permutations_test.goindex 29a98bbc..d18a816a 100644--- a/tests/integration/policy_permutations_test.go+++ b/tests/integration/policy_permutations_test.go@@ -68,7 +68,6 @@ func callMCPAllowError(t *testing.T, mcpAddr string, apiToken string, toolName s}func TestPolicyPermutations(t *testing.T) {- hubBin := buildBinary(t, "./cmd/sam-hub")nodeBin := buildBinary(t, "./cmd/sam-node")tmpDir := t.TempDir()@@ -116,25 +115,8 @@ bindings:t.Fatal(err)}- httpPortHub := getFreePort(t)- p2pPortHub := getFreePort(t)-- cmdHub := exec.Command(hubBin,- "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortHub),- "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortHub),- "--policy-file", hubPolicyFile,- "--keys-db", filepath.Join(tmpDir, "keys.db"),- "--allow-loopback",- "--issuer", oidcURL,- "--insecure-skip-tls-verify",- )- cmdHub.Stdout = os.Stdout- cmdHub.Stderr = os.Stderr- if err := cmdHub.Start(); err != nil {- t.Fatalf("Failed to start Hub: %v", err)- }- defer func() { _ = cmdHub.Process.Kill(); _ = cmdHub.Wait() }()- fetchPeerID(t, httpPortHub)+ httpPortHub, cleanupHub := startControlPlaneAndRouter(t, tmpDir, oidcURL, mintToken, hubPolicyFile)+ defer cleanupHub()// 2. Node B (Target) ConfignodeBPolicyFile := filepath.Join(tmpDir, "nodeB_config.yaml")