Skip to content

Agent Mesh

test(integration): verify identity evidence as owner

Antonio Ojea79e662a7c8bc
diff --git a/tests/integration/identity_evidence_test.go b/tests/integration/identity_evidence_test.go
index 60a5e188..6c10c377 100644
--- a/tests/integration/identity_evidence_test.go
+++ b/tests/integration/identity_evidence_test.go
@@ -16,6 +16,11 @@ package integration_test
import (
"context"
+ "crypto/ed25519"
+ "crypto/rand"
+ "crypto/x509"
+ "encoding/hex"
+ "fmt"
"io"
"net"
"net/http"
@@ -26,7 +31,10 @@ import (
"testing"
"time"
+ "github.com/biscuit-auth/biscuit-go/v2"
+ "github.com/biscuit-auth/biscuit-go/v2/datalog"
"github.com/google/sam/api"
+ "github.com/libp2p/go-libp2p/core/peer"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
@@ -37,7 +45,7 @@ import (
// peer evidence before any provider request.
func TestIdentityEvidenceOperatorFlow(t *testing.T) {
nodeBin := buildBinary(t, "./cmd/sam-node")
- _, controlPlaneURL := startMockRouter(t)
+ _, controlPlaneURL, pinnedControlPlaneKey := startMockRouterWithControlPlaneKey(t)
ownerHome := t.TempDir()
providerHome := t.TempDir()
@@ -77,18 +85,135 @@ func TestIdentityEvidenceOperatorFlow(t *testing.T) {
var local api.IdentityEvidenceResponse
getIdentityEvidenceJSON(t, client, "/sam/identity", &local)
- if local.PeerId == "" || len(local.Biscuit) == 0 || len(local.TrustedControlPlaneKeys) == 0 {
- t.Fatalf("local identity evidence is incomplete: peer=%q biscuit=%d keys=%d", local.PeerId, len(local.Biscuit), len(local.TrustedControlPlaneKeys))
- }
- if local.ControlPlaneUrl != controlPlaneURL {
- t.Fatalf("control plane URL = %q, want %q", local.ControlPlaneUrl, controlPlaneURL)
- }
var remote api.PeerEvidenceResponse
getIdentityEvidenceJSON(t, client, "/sam/peer/"+url.PathEscape(providerPeerID.String())+"/evidence", &remote)
- if remote.PeerId != providerPeerID.String() || len(remote.Biscuit) == 0 || len(remote.VerifyingKey) == 0 {
- t.Fatalf("remote handshake evidence is incomplete: peer=%q biscuit=%d key=%d", remote.PeerId, len(remote.Biscuit), len(remote.VerifyingKey))
+
+ verifyIdentityEvidence(t, controlPlaneURL, pinnedControlPlaneKey, providerPeerID, &local, &remote)
+}
+
+// verifyIdentityEvidence models the trust decision of an owner application.
+// It has only its pinned control-plane key and the two sidecar responses.
+func verifyIdentityEvidence(t *testing.T, controlPlaneURL string, pinnedControlPlaneKey ed25519.PublicKey, expectedProvider peer.ID, local *api.IdentityEvidenceResponse, remote *api.PeerEvidenceResponse) {
+ t.Helper()
+
+ if local.ControlPlaneUrl != controlPlaneURL || len(local.Biscuit) == 0 || local.CheckedAt <= 0 || local.BiscuitExpiresAt < local.CheckedAt {
+ t.Fatalf("invalid local identity evidence: %+v", local)
+ }
+ localPeer, err := peer.Decode(local.PeerId)
+ if err != nil {
+ t.Fatalf("decode local PeerID: %v", err)
+ }
+ trustedKeys := parseEvidenceKeys(t, local.TrustedControlPlaneKeys)
+ if !containsEvidenceKey(trustedKeys, pinnedControlPlaneKey) {
+ t.Fatal("local response does not contain the independently pinned control-plane key")
+ }
+ if _, err := verifyBiscuitForApplication(local.Biscuit, localPeer, []ed25519.PublicKey{pinnedControlPlaneKey}); err != nil {
+ t.Fatalf("third-party verification of local Biscuit failed: %v", err)
+ }
+
+ if remote.PeerId != expectedProvider.String() || len(remote.Biscuit) == 0 || len(remote.VerifyingKey) == 0 || remote.CheckedAt <= 0 || remote.Expiration < remote.CheckedAt || len(remote.RevocationIds) == 0 {
+ t.Fatalf("invalid remote identity evidence: %+v", remote)
+ }
+ if len(remote.Roles) != 1 || remote.Roles[0] != api.RoleNode || len(remote.Labels) != 0 {
+ t.Fatalf("unexpected remote claims: roles=%v labels=%v", remote.Roles, remote.Labels)
+ }
+ remotePeer, err := peer.Decode(remote.PeerId)
+ if err != nil {
+ t.Fatalf("decode remote PeerID: %v", err)
+ }
+ verifyingKey := parseEvidenceKey(t, remote.VerifyingKey)
+ if !containsEvidenceKey(trustedKeys, verifyingKey) {
+ t.Fatal("remote verifying key is absent from the local trusted key set")
+ }
+ verifiedRemote, err := verifyBiscuitForApplication(remote.Biscuit, remotePeer, []ed25519.PublicKey{verifyingKey})
+ if err != nil {
+ t.Fatalf("third-party verification of remote Biscuit failed: %v", err)
+ }
+ if !matchesRevocationIDs(remote.RevocationIds, verifiedRemote.RevocationIds()) {
+ t.Fatalf("published revocation IDs %v do not match the verified Biscuit", remote.RevocationIds)
+ }
+
+ untrustedKey, _, err := ed25519.GenerateKey(rand.Reader)
+ if err != nil {
+ t.Fatalf("generate unrelated verification key: %v", err)
+ }
+ if _, err := verifyBiscuitForApplication(remote.Biscuit, remotePeer, []ed25519.PublicKey{untrustedKey}); err == nil {
+ t.Fatal("remote Biscuit verified with an unrelated control-plane key")
+ }
+}
+
+func verifyBiscuitForApplication(encodedBiscuit []byte, expectedPeer peer.ID, trustedKeys []ed25519.PublicKey) (*biscuit.Biscuit, error) {
+ parsed, err := biscuit.Unmarshal(encodedBiscuit)
+ if err != nil {
+ return nil, err
+ }
+ for _, trustedKey := range trustedKeys {
+ authorizer, err := parsed.Authorizer(trustedKey, biscuit.WithWorldOptions(datalog.WithMaxDuration(5*time.Second)))
+ if err != nil {
+ continue
+ }
+ authorizer.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: api.FactTime,
+ IDs: []biscuit.Term{biscuit.Date(time.Now())},
+ }})
+ authorizer.AddCheck(api.ControlPlaneStaticTimeCheck)
+ authorizer.AddPolicy(api.AllowIfTruePolicy)
+ if err := authorizer.Authorize(); err != nil {
+ continue
+ }
+ boundPeer := biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: api.FactNode,
+ IDs: []biscuit.Term{biscuit.String(expectedPeer.String())},
+ }}
+ if _, err := parsed.GetBlockID(boundPeer); err == nil {
+ return parsed, nil
+ }
+ }
+ return nil, fmt.Errorf("Biscuit is not valid for peer %s and the configured control-plane keys", expectedPeer)
+}
+
+func parseEvidenceKeys(t *testing.T, encodedKeys [][]byte) []ed25519.PublicKey {
+ t.Helper()
+ keys := make([]ed25519.PublicKey, 0, len(encodedKeys))
+ for _, encodedKey := range encodedKeys {
+ keys = append(keys, parseEvidenceKey(t, encodedKey))
+ }
+ return keys
+}
+
+func parseEvidenceKey(t *testing.T, encodedKey []byte) ed25519.PublicKey {
+ t.Helper()
+ parsed, err := x509.ParsePKIXPublicKey(encodedKey)
+ if err != nil {
+ t.Fatalf("parse control-plane SPKI: %v", err)
+ }
+ key, ok := parsed.(ed25519.PublicKey)
+ if !ok {
+ t.Fatalf("control-plane SPKI has type %T, want Ed25519", parsed)
+ }
+ return key
+}
+
+func containsEvidenceKey(keys []ed25519.PublicKey, expected ed25519.PublicKey) bool {
+ for _, key := range keys {
+ if key.Equal(expected) {
+ return true
+ }
+ }
+ return false
+}
+
+func matchesRevocationIDs(published []string, rawIDs [][]byte) bool {
+ if len(published) != len(rawIDs) {
+ return false
+ }
+ for index, rawID := range rawIDs {
+ if published[index] != hex.EncodeToString(rawID) {
+ return false
+ }
}
+ return true
}
func identityEvidenceSocketClient(socketPath string) *http.Client {
diff --git a/tests/integration/minimal_helpers_test.go b/tests/integration/minimal_helpers_test.go
index 9e177bdd..9a15d735 100644
--- a/tests/integration/minimal_helpers_test.go
+++ b/tests/integration/minimal_helpers_test.go
@@ -211,6 +211,11 @@ func runCommandWithCallback(
}
func startMockRouter(t *testing.T) (peer.ID, string) {
+ routerID, controlPlaneURL, _ := startMockRouterWithControlPlaneKey(t)
+ return routerID, controlPlaneURL
+}
+
+func startMockRouterWithControlPlaneKey(t *testing.T) (peer.ID, string, ed25519.PublicKey) {
t.Helper()
pub, priv, err := ed25519.GenerateKey(rand.Reader)
@@ -288,7 +293,7 @@ func startMockRouter(t *testing.T) (peer.ID, string) {
_ = h.Close()
})
- return h.ID(), httpServer.URL
+ return h.ID(), httpServer.URL, pub
}
func startMockRouterWithOIDC(t *testing.T, oidcIssuerURL string) (peer.ID, string) {