Просмотр исходного кода

Test remote-attest-01's TDX path through a stand-in verifier

The provisioner holds the TDX and SEV-SNP verifiers behind small
interfaces, so a test can replace the TDX one. A real quote's report
data cannot be chosen, and go-tdx-guest's test quote cannot pass
Intel's collateral, so a successful TDX dispatch was previously
untested.

The new test covers:
- a successful TDX dispatch (the outblob as the quote, the result, the
  bound data, media type and evidence);
- an invalid quote;
- a collateral failure, which is not reported as invalid evidence;
- report data of the wrong size.

Claude-Session: https://claude.ai/code/session_01CjfEMLGpnHQaWsmxe1zmLF
Josh Drake 1 месяц назад
Родитель
Сommit
93326f1b51

+ 14 - 2
authority/provisioner/acme_remote_attestation.go

@@ -87,8 +87,8 @@ func (o *ACMERemoteAttestation) GetClaimsHint() []string {
 // challenges with the provisioner's configuration.
 type remoteAttestationVerifier struct {
 	formats     []attestation.Format
-	tdx         *tdx.Verifier
-	sevsnp      *sevsnp.Verifier
+	tdx         tdxVerifier
+	sevsnp      sevSNPVerifier
 	csRoots     *x509.CertPool
 	csAudience  string
 	csMaxAge    time.Duration
@@ -96,6 +96,18 @@ type remoteAttestationVerifier struct {
 	now         func() time.Time
 }
 
+// tdxVerifier is implemented by *tdx.Verifier. Tests replace it, since a real
+// quote's report data cannot be chosen and a test quote cannot pass Intel's
+// collateral.
+type tdxVerifier interface {
+	Verify(ctx context.Context, quote []byte) (*attestation.TDX, []byte, error)
+}
+
+// sevSNPVerifier is implemented by *sevsnp.Verifier.
+type sevSNPVerifier interface {
+	Verify(ctx context.Context, report, certTable []byte) (*attestation.SEVSNP, []byte, error)
+}
+
 // initializeRemoteAttestation validates the remote attestation options and
 // creates the verifiers. It returns early if remote-attest-01 is not enabled.
 func (p *ACME) initializeRemoteAttestation() error {

+ 60 - 0
authority/provisioner/acme_remote_attestation_test.go

@@ -306,3 +306,63 @@ func TestWebhookController_AppendRequestBodyOptions(t *testing.T) {
 	require.NoError(t, wc.Authorize(context.Background(), req))
 	assert.Equal(t, a, req.Attestation)
 }
+
+// fakeTDXVerifier stands in for the TDX verifier: a real quote's report data
+// cannot be chosen, and a test quote cannot pass Intel's collateral.
+type fakeTDXVerifier struct {
+	result *attestation.TDX
+	bound  []byte
+	err    error
+	quote  []byte
+}
+
+func (f *fakeTDXVerifier) Verify(_ context.Context, quote []byte) (*attestation.TDX, []byte, error) {
+	f.quote = quote
+	return f.result, f.bound, f.err
+}
+
+func TestACME_VerifyRemoteAttestation_tdx(t *testing.T) {
+	bound := testBound()
+	const mediaType = `application/vnd.veraison.tsm-report+json; provider="tdx_guest"`
+	value := []byte(`{"provider":"tdx_guest","outblob":"AAEC"}`)
+
+	t.Run("ok", func(t *testing.T) {
+		p := newRemoteAttestationACME(t, nil, staticHTTPClient{})
+		fake := &fakeTDXVerifier{result: &attestation.TDX{MRTD: "aa", TCBStatus: "UpToDate"}, bound: bound}
+		p.remoteAttestation.tdx = fake
+		got, gotBound, err := p.VerifyRemoteAttestation(context.Background(), jsonCMW(mediaType, value, cmw.Evidence), "")
+		require.NoError(t, err)
+		assert.Equal(t, []byte{0, 1, 2}, fake.quote, "the outblob is the quote")
+		assert.Equal(t, bound, gotBound)
+		assert.Equal(t, attestation.FormatTDX, got.Format)
+		assert.Equal(t, mediaType, got.MediaType)
+		assert.Equal(t, value, got.Evidence)
+		assert.Equal(t, fake.result, got.TDX)
+		assert.Nil(t, got.SEVSNP)
+		assert.Nil(t, got.ConfidentialSpace)
+	})
+
+	tests := []struct {
+		name        string
+		fake        *fakeTDXVerifier
+		wantErr     string
+		wantInvalid bool
+	}{
+		{"fail invalid quote", &fakeTDXVerifier{err: attestation.Invalidf("tdx quote is from a debug trust domain")},
+			"debug trust domain", true},
+		// A collateral fetch failure is the CA's, not the evidence's.
+		{"fail collateral", &fakeTDXVerifier{err: errors.New("error fetching tdx collateral")},
+			"error fetching tdx collateral", false},
+		{"fail report data size", &fakeTDXVerifier{result: &attestation.TDX{}, bound: bound[:32]},
+			"evidence binds 32 bytes, not 64", true},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			p := newRemoteAttestationACME(t, nil, staticHTTPClient{})
+			p.remoteAttestation.tdx = tt.fake
+			_, _, err := p.VerifyRemoteAttestation(context.Background(), jsonCMW(mediaType, value, 0), "")
+			assert.ErrorContains(t, err, tt.wantErr)
+			assert.Equal(t, tt.wantInvalid, errors.Is(err, attestation.ErrInvalidEvidence))
+		})
+	}
+}