| 1 |
package rest |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/google/go-cmp/cmp" |
| 7 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 8 |
"github.com/google/go-tpm-tools/proto/tpm" |
| 9 |
"github.com/google/go-tpm-tools/verifier" |
| 10 |
"github.com/google/go-tpm-tools/verifier/models" |
| 11 |
"github.com/google/uuid" |
| 12 |
"google.golang.org/genproto/googleapis/rpc/status" |
| 13 |
"google.golang.org/protobuf/encoding/prototext" |
| 14 |
"google.golang.org/protobuf/testing/protocmp" |
| 15 |
|
| 16 |
ccpb "cloud.google.com/go/confidentialcomputing/apiv1/confidentialcomputingpb" |
| 17 |
sabi "github.com/google/go-sev-guest/abi" |
| 18 |
spb "github.com/google/go-sev-guest/proto/sevsnp" |
| 19 |
tabi "github.com/google/go-tdx-guest/abi" |
| 20 |
tpb "github.com/google/go-tdx-guest/proto/tdx" |
| 21 |
tgtestdata "github.com/google/go-tdx-guest/testing/testdata" |
| 22 |
attestpb "github.com/google/go-tpm-tools/proto/attest" |
| 23 |
) |
| 24 |
|
| 25 |
var ( |
| 26 |
tokenOptionsCompareOpts = []cmp.Option{ |
| 27 |
cmpopts.IgnoreUnexported(ccpb.TokenOptions{}), |
| 28 |
cmpopts.IgnoreUnexported(ccpb.TokenOptions_AwsPrincipalTagsOptions{}), |
| 29 |
cmpopts.IgnoreUnexported(ccpb.AwsPrincipalTagsOptions{}), |
| 30 |
cmpopts.IgnoreUnexported(ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags{}), |
| 31 |
cmpopts.IgnoreUnexported(ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags_ContainerImageSignatures{}), |
| 32 |
cmpopts.IgnoreUnexported(ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions{}), |
| 33 |
} |
| 34 |
) |
| 35 |
|
| 36 |
// Make sure our conversion function can handle empty values. |
| 37 |
func TestConvertEmpty(t *testing.T) { |
| 38 |
if _, err := convertChallengeFromREST(&ccpb.Challenge{}); err != nil { |
| 39 |
t.Errorf("Converting empty challenge: %v", err) |
| 40 |
} |
| 41 |
_ = convertRequestToREST(verifier.VerifyAttestationRequest{}) |
| 42 |
if _, err := convertResponseFromREST(&ccpb.VerifyAttestationResponse{}); err != nil { |
| 43 |
t.Errorf("Converting empty challenge: %v", err) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
const ( |
| 48 |
emptyReport = ` |
| 49 |
version: 2 |
| 50 |
policy: 0xa0000 |
| 51 |
signature_algo: 1 |
| 52 |
report_data: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01' |
| 53 |
family_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 54 |
image_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 55 |
measurement: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 56 |
host_data: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 57 |
id_key_digest: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 58 |
author_key_digest: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 59 |
report_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 60 |
report_id_ma: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 61 |
chip_id: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 62 |
signature: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 63 |
` |
| 64 |
extraGUID = "00000000-0000-c0de-0000-000000000000" |
| 65 |
) |
| 66 |
|
| 67 |
func TestConvertSEVSNPProtoToREST(t *testing.T) { |
| 68 |
report := &spb.Report{} |
| 69 |
if err := prototext.Unmarshal([]byte(emptyReport), report); err != nil { |
| 70 |
t.Fatalf("Unable to unmarshal SEV-SNP report proto: %v", err) |
| 71 |
} |
| 72 |
|
| 73 |
rawCertTable := testRawCertTable(t) |
| 74 |
certTable := &sabi.CertTable{} |
| 75 |
if err := certTable.Unmarshal(rawCertTable.table); err != nil { |
| 76 |
t.Fatalf("Failed to unmarshal certTable bytes: %v", err) |
| 77 |
} |
| 78 |
sevsnp := &spb.Attestation{Report: report, CertificateChain: certTable.Proto()} |
| 79 |
|
| 80 |
got, err := convertSEVSNPProtoToREST(sevsnp) |
| 81 |
if err != nil { |
| 82 |
t.Errorf("failed to convert SEVSNP proto to API proto: %v", err) |
| 83 |
} |
| 84 |
|
| 85 |
wantReport, err := sabi.ReportToAbiBytes(report) |
| 86 |
if err != nil { |
| 87 |
t.Fatalf("Unable to convert SEV-SNP report proto to ABI bytes: %v", err) |
| 88 |
} |
| 89 |
|
| 90 |
want := &ccpb.VerifyAttestationRequest_SevSnpAttestation{ |
| 91 |
SevSnpAttestation: &ccpb.SevSnpAttestation{ |
| 92 |
AuxBlob: rawCertTable.table, |
| 93 |
Report: wantReport, |
| 94 |
}, |
| 95 |
} |
| 96 |
|
| 97 |
if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" { |
| 98 |
t.Errorf("SEVSNP API proto mismatch: %s", diff) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
type testCertTable struct { |
| 103 |
table []byte |
| 104 |
extraraw []byte |
| 105 |
} |
| 106 |
|
| 107 |
func testRawCertTable(t testing.TB) *testCertTable { |
| 108 |
t.Helper() |
| 109 |
headers := make([]sabi.CertTableHeaderEntry, 6) // ARK, ASK, VCEK, VLEK, extra, NULL |
| 110 |
arkraw := []byte("ark") |
| 111 |
askraw := []byte("ask") |
| 112 |
vcekraw := []byte("vcek") |
| 113 |
vlekraw := []byte("vlek") |
| 114 |
extraraw := []byte("extra") |
| 115 |
|
| 116 |
var err error |
| 117 |
headers[0].GUID, err = uuid.Parse(sabi.ArkGUID) |
| 118 |
if err != nil { |
| 119 |
t.Fatalf("cannot parse uuid: %v", err) |
| 120 |
} |
| 121 |
headers[0].Offset = uint32(len(headers) * sabi.CertTableEntrySize) |
| 122 |
headers[0].Length = uint32(len(arkraw)) |
| 123 |
|
| 124 |
headers[1].GUID, err = uuid.Parse(sabi.AskGUID) |
| 125 |
|
| 126 |
if err != nil { |
| 127 |
t.Fatalf("cannot parse uuid: %v", err) |
| 128 |
} |
| 129 |
headers[1].Offset = headers[0].Offset + headers[0].Length |
| 130 |
headers[1].Length = uint32(len(askraw)) |
| 131 |
|
| 132 |
headers[2].GUID, err = uuid.Parse(sabi.VcekGUID) |
| 133 |
if err != nil { |
| 134 |
t.Fatalf("cannot parse uuid: %v", err) |
| 135 |
} |
| 136 |
headers[2].Offset = headers[1].Offset + headers[1].Length |
| 137 |
headers[2].Length = uint32(len(vcekraw)) |
| 138 |
|
| 139 |
headers[3].GUID, err = uuid.Parse(sabi.VlekGUID) |
| 140 |
if err != nil { |
| 141 |
t.Fatalf("cannot parse uuid: %v", err) |
| 142 |
} |
| 143 |
headers[3].Offset = headers[2].Offset + headers[2].Length |
| 144 |
headers[3].Length = uint32(len(vlekraw)) |
| 145 |
|
| 146 |
headers[4].GUID, err = uuid.Parse(extraGUID) |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("cannot parse uuid: %v", err) |
| 149 |
} |
| 150 |
headers[4].Offset = headers[3].Offset + headers[3].Length |
| 151 |
headers[4].Length = uint32(len(extraraw)) |
| 152 |
|
| 153 |
result := &testCertTable{ |
| 154 |
table: make([]byte, headers[4].Offset+headers[4].Length), |
| 155 |
extraraw: extraraw, |
| 156 |
} |
| 157 |
for i, cert := range [][]byte{arkraw, askraw, vcekraw, vlekraw, extraraw} { |
| 158 |
if err := (&headers[i]).Write(result.table[i*sabi.CertTableEntrySize:]); err != nil { |
| 159 |
t.Fatalf("could not write header %d: %v", i, err) |
| 160 |
} |
| 161 |
copy(result.table[headers[i].Offset:], cert) |
| 162 |
} |
| 163 |
return result |
| 164 |
} |
| 165 |
|
| 166 |
func TestConvertTDXProtoToREST(t *testing.T) { |
| 167 |
testCases := []struct { |
| 168 |
name string |
| 169 |
quote func() *tpb.QuoteV4 |
| 170 |
wantPass bool |
| 171 |
}{ |
| 172 |
{ |
| 173 |
name: "successful TD quote conversion", |
| 174 |
quote: func() *tpb.QuoteV4 { |
| 175 |
tdx, err := tabi.QuoteToProto(tgtestdata.RawQuote) |
| 176 |
if err != nil { |
| 177 |
t.Fatalf("Unable to convert Raw TD Quote to TDX V4 quote: %v", err) |
| 178 |
} |
| 179 |
|
| 180 |
quote, ok := tdx.(*tpb.QuoteV4) |
| 181 |
if !ok { |
| 182 |
t.Fatal("Quote format not supported, want QuoteV4 format") |
| 183 |
} |
| 184 |
return quote |
| 185 |
}, |
| 186 |
wantPass: true, |
| 187 |
}, |
| 188 |
{ |
| 189 |
name: "nil TD quote conversion", |
| 190 |
quote: func() *tpb.QuoteV4 { return nil }, |
| 191 |
wantPass: false, |
| 192 |
}, |
| 193 |
} |
| 194 |
|
| 195 |
for _, tc := range testCases { |
| 196 |
got, err := convertTDXProtoToREST(tc.quote()) |
| 197 |
if err != nil && tc.wantPass { |
| 198 |
t.Errorf("failed to convert TDX proto to API proto: %v", err) |
| 199 |
} |
| 200 |
|
| 201 |
if tc.wantPass { |
| 202 |
want := &ccpb.VerifyAttestationRequest_TdCcel{ |
| 203 |
TdCcel: &ccpb.TdxCcelAttestation{ |
| 204 |
TdQuote: tgtestdata.RawQuote, |
| 205 |
}, |
| 206 |
} |
| 207 |
|
| 208 |
if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" { |
| 209 |
t.Errorf("TDX API proto mismatch: %s", diff) |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func TestConvertTokenOptionsToREST(t *testing.T) { |
| 216 |
testCases := []struct { |
| 217 |
name string |
| 218 |
tokenOptions *models.TokenOptions |
| 219 |
wantpb *ccpb.TokenOptions |
| 220 |
}{ |
| 221 |
{ |
| 222 |
name: "NilTokenOptions", |
| 223 |
tokenOptions: nil, |
| 224 |
wantpb: nil, |
| 225 |
}, |
| 226 |
{ |
| 227 |
name: "EmptyTokenOptions", |
| 228 |
tokenOptions: &models.TokenOptions{}, |
| 229 |
wantpb: &ccpb.TokenOptions{}, |
| 230 |
}, |
| 231 |
{ |
| 232 |
name: "TokenOptionsHappyPath", |
| 233 |
tokenOptions: &models.TokenOptions{ |
| 234 |
Audience: "TestingAudience", |
| 235 |
Nonces: []string{"thisisthefirstnonce", "thisisthesecondnonce"}, |
| 236 |
TokenType: "AWS_PRINCIPALTAGS", |
| 237 |
PrincipalTagOptions: &models.AWSPrincipalTagsOptions{ |
| 238 |
AllowedPrincipalTags: &models.AllowedPrincipalTags{ |
| 239 |
ContainerImageSignatures: &models.ContainerImageSignatures{ |
| 240 |
KeyIDs: []string{"abcdefg", "12345"}, |
| 241 |
}, |
| 242 |
}, |
| 243 |
}, |
| 244 |
}, |
| 245 |
wantpb: &ccpb.TokenOptions{ |
| 246 |
Audience: "TestingAudience", |
| 247 |
Nonce: []string{"thisisthefirstnonce", "thisisthesecondnonce"}, |
| 248 |
TokenType: ccpb.TokenType_TOKEN_TYPE_AWS_PRINCIPALTAGS, |
| 249 |
TokenTypeOptions: &ccpb.TokenOptions_AwsPrincipalTagsOptions{ |
| 250 |
AwsPrincipalTagsOptions: &ccpb.AwsPrincipalTagsOptions{ |
| 251 |
AllowedPrincipalTags: &ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags{ |
| 252 |
ContainerImageSignatures: &ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags_ContainerImageSignatures{ |
| 253 |
KeyIds: []string{"abcdefg", "12345"}, |
| 254 |
}, |
| 255 |
}, |
| 256 |
}, |
| 257 |
}, |
| 258 |
}, |
| 259 |
}, |
| 260 |
{ |
| 261 |
name: "TokenTypeOptionsMissingSubClasses", |
| 262 |
tokenOptions: &models.TokenOptions{ |
| 263 |
Audience: "TestingAudience", |
| 264 |
Nonces: []string{"thisisthefirstnonce", "thisisthesecondnonce"}, |
| 265 |
TokenType: "AWS_PRINCIPALTAGS", |
| 266 |
PrincipalTagOptions: &models.AWSPrincipalTagsOptions{ |
| 267 |
AllowedPrincipalTags: &models.AllowedPrincipalTags{}, |
| 268 |
}, |
| 269 |
}, |
| 270 |
wantpb: &ccpb.TokenOptions{ |
| 271 |
Audience: "TestingAudience", |
| 272 |
Nonce: []string{"thisisthefirstnonce", "thisisthesecondnonce"}, |
| 273 |
TokenType: ccpb.TokenType_TOKEN_TYPE_AWS_PRINCIPALTAGS, |
| 274 |
TokenTypeOptions: &ccpb.TokenOptions_AwsPrincipalTagsOptions{ |
| 275 |
AwsPrincipalTagsOptions: &ccpb.AwsPrincipalTagsOptions{ |
| 276 |
AllowedPrincipalTags: &ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags{}, |
| 277 |
}, |
| 278 |
}, |
| 279 |
}, |
| 280 |
}, |
| 281 |
{ |
| 282 |
name: "MissingAudNonceTokenType", |
| 283 |
tokenOptions: &models.TokenOptions{ |
| 284 |
PrincipalTagOptions: &models.AWSPrincipalTagsOptions{ |
| 285 |
AllowedPrincipalTags: &models.AllowedPrincipalTags{}, |
| 286 |
}, |
| 287 |
}, |
| 288 |
wantpb: &ccpb.TokenOptions{ |
| 289 |
TokenTypeOptions: nil, |
| 290 |
}, |
| 291 |
}, |
| 292 |
{ |
| 293 |
name: "MissingAudNonce", |
| 294 |
tokenOptions: &models.TokenOptions{ |
| 295 |
TokenType: "AWS_PRINCIPALTAGS", |
| 296 |
PrincipalTagOptions: &models.AWSPrincipalTagsOptions{ |
| 297 |
AllowedPrincipalTags: &models.AllowedPrincipalTags{}, |
| 298 |
}, |
| 299 |
}, |
| 300 |
wantpb: &ccpb.TokenOptions{ |
| 301 |
TokenType: ccpb.TokenType_TOKEN_TYPE_AWS_PRINCIPALTAGS, |
| 302 |
TokenTypeOptions: &ccpb.TokenOptions_AwsPrincipalTagsOptions{ |
| 303 |
AwsPrincipalTagsOptions: &ccpb.AwsPrincipalTagsOptions{ |
| 304 |
AllowedPrincipalTags: &ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags{}, |
| 305 |
}, |
| 306 |
}, |
| 307 |
}, |
| 308 |
}, |
| 309 |
{ |
| 310 |
name: "TokenOptionsHappyPath", |
| 311 |
tokenOptions: &models.TokenOptions{ |
| 312 |
Audience: "TestingAudience", |
| 313 |
Nonces: []string{"thisisthefirstnonce", "thisisthesecondnonce"}, |
| 314 |
TokenType: "AWS_PRINCIPALTAGS", |
| 315 |
PrincipalTagOptions: &models.AWSPrincipalTagsOptions{ |
| 316 |
AllowedPrincipalTags: &models.AllowedPrincipalTags{ |
| 317 |
ContainerImageSignatures: &models.ContainerImageSignatures{ |
| 318 |
KeyIDs: []string{"abcdefg", "12345"}, |
| 319 |
}, |
| 320 |
}, |
| 321 |
}, |
| 322 |
}, |
| 323 |
wantpb: &ccpb.TokenOptions{ |
| 324 |
Audience: "TestingAudience", |
| 325 |
Nonce: []string{"thisisthefirstnonce", "thisisthesecondnonce"}, |
| 326 |
TokenType: ccpb.TokenType_TOKEN_TYPE_AWS_PRINCIPALTAGS, |
| 327 |
TokenTypeOptions: &ccpb.TokenOptions_AwsPrincipalTagsOptions{ |
| 328 |
AwsPrincipalTagsOptions: &ccpb.AwsPrincipalTagsOptions{ |
| 329 |
AllowedPrincipalTags: &ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags{ |
| 330 |
ContainerImageSignatures: &ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags_ContainerImageSignatures{ |
| 331 |
KeyIds: []string{"abcdefg", "12345"}, |
| 332 |
}, |
| 333 |
}, |
| 334 |
}, |
| 335 |
}, |
| 336 |
}, |
| 337 |
}, |
| 338 |
{ |
| 339 |
name: "OIDCTokenType", |
| 340 |
tokenOptions: &models.TokenOptions{ |
| 341 |
TokenType: "OIDC", |
| 342 |
}, |
| 343 |
wantpb: &ccpb.TokenOptions{ |
| 344 |
TokenType: ccpb.TokenType_TOKEN_TYPE_OIDC, |
| 345 |
}, |
| 346 |
}, |
| 347 |
{ |
| 348 |
name: "OIDCTokenType", |
| 349 |
tokenOptions: &models.TokenOptions{ |
| 350 |
TokenType: "OIDC", |
| 351 |
}, |
| 352 |
wantpb: &ccpb.TokenOptions{ |
| 353 |
TokenType: ccpb.TokenType_TOKEN_TYPE_OIDC, |
| 354 |
}, |
| 355 |
}, |
| 356 |
{ |
| 357 |
name: "LimitedAWSTokenType", |
| 358 |
tokenOptions: &models.TokenOptions{ |
| 359 |
TokenType: "LIMITED_AWS", |
| 360 |
}, |
| 361 |
wantpb: &ccpb.TokenOptions{ |
| 362 |
TokenType: ccpb.TokenType_TOKEN_TYPE_LIMITED_AWS, |
| 363 |
}, |
| 364 |
}, |
| 365 |
{ |
| 366 |
name: "SingleNonce", |
| 367 |
tokenOptions: &models.TokenOptions{ |
| 368 |
Nonces: []string{"thisistheonlynonce"}, |
| 369 |
}, |
| 370 |
wantpb: &ccpb.TokenOptions{ |
| 371 |
Nonce: []string{"thisistheonlynonce"}, |
| 372 |
}, |
| 373 |
}, |
| 374 |
} |
| 375 |
|
| 376 |
for _, tc := range testCases { |
| 377 |
pbTokenOpts := convertTokenOptionsToREST(tc.tokenOptions) |
| 378 |
diff := cmp.Diff(tc.wantpb, pbTokenOpts, tokenOptionsCompareOpts...) |
| 379 |
if diff != "" { |
| 380 |
t.Errorf("%v: %s", tc.name, diff) |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
func TestConvertTokenOptionsToCSOptions(t *testing.T) { |
| 386 |
testcases := []struct { |
| 387 |
name string |
| 388 |
tokenOpts *ccpb.TokenOptions |
| 389 |
expectedOpts *ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions |
| 390 |
}{ |
| 391 |
{ |
| 392 |
name: "nil input", |
| 393 |
tokenOpts: nil, |
| 394 |
expectedOpts: &ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions{ |
| 395 |
TokenProfile: ccpb.TokenProfile_TOKEN_PROFILE_DEFAULT_EAT, |
| 396 |
}, |
| 397 |
}, |
| 398 |
{ |
| 399 |
name: "custom audience and nonce", |
| 400 |
tokenOpts: &ccpb.TokenOptions{ |
| 401 |
Audience: "test-audience", |
| 402 |
Nonce: []string{"test-nonce-1", "test-nonce-2"}, |
| 403 |
}, |
| 404 |
expectedOpts: &ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions{ |
| 405 |
Audience: "test-audience", |
| 406 |
Nonce: []string{"test-nonce-1", "test-nonce-2"}, |
| 407 |
TokenProfile: ccpb.TokenProfile_TOKEN_PROFILE_DEFAULT_EAT, |
| 408 |
}, |
| 409 |
}, |
| 410 |
{ |
| 411 |
name: "OIDC token type", |
| 412 |
tokenOpts: &ccpb.TokenOptions{ |
| 413 |
TokenType: ccpb.TokenType_TOKEN_TYPE_OIDC, |
| 414 |
}, |
| 415 |
expectedOpts: &ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions{ |
| 416 |
SignatureType: ccpb.SignatureType_SIGNATURE_TYPE_OIDC, |
| 417 |
TokenProfile: ccpb.TokenProfile_TOKEN_PROFILE_DEFAULT_EAT, |
| 418 |
}, |
| 419 |
}, |
| 420 |
{ |
| 421 |
name: "PKI token type", |
| 422 |
tokenOpts: &ccpb.TokenOptions{ |
| 423 |
TokenType: ccpb.TokenType_TOKEN_TYPE_PKI, |
| 424 |
}, |
| 425 |
expectedOpts: &ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions{ |
| 426 |
SignatureType: ccpb.SignatureType_SIGNATURE_TYPE_PKI, |
| 427 |
TokenProfile: ccpb.TokenProfile_TOKEN_PROFILE_DEFAULT_EAT, |
| 428 |
}, |
| 429 |
}, |
| 430 |
{ |
| 431 |
name: "AWS token type", |
| 432 |
tokenOpts: &ccpb.TokenOptions{ |
| 433 |
TokenType: ccpb.TokenType_TOKEN_TYPE_AWS_PRINCIPALTAGS, |
| 434 |
TokenTypeOptions: &ccpb.TokenOptions_AwsPrincipalTagsOptions{ |
| 435 |
AwsPrincipalTagsOptions: &ccpb.AwsPrincipalTagsOptions{ |
| 436 |
AllowedPrincipalTags: &ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags{ |
| 437 |
ContainerImageSignatures: &ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags_ContainerImageSignatures{ |
| 438 |
KeyIds: []string{"keyid1", "keyid2"}, |
| 439 |
}, |
| 440 |
}, |
| 441 |
}, |
| 442 |
}, |
| 443 |
}, |
| 444 |
expectedOpts: &ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions{ |
| 445 |
SignatureType: ccpb.SignatureType_SIGNATURE_TYPE_OIDC, |
| 446 |
TokenProfile: ccpb.TokenProfile_TOKEN_PROFILE_AWS, |
| 447 |
TokenProfileOptions: &ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions_AwsPrincipalTagsOptions{ |
| 448 |
AwsPrincipalTagsOptions: &ccpb.AwsPrincipalTagsOptions{ |
| 449 |
AllowedPrincipalTags: &ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags{ |
| 450 |
ContainerImageSignatures: &ccpb.AwsPrincipalTagsOptions_AllowedPrincipalTags_ContainerImageSignatures{ |
| 451 |
KeyIds: []string{"keyid1", "keyid2"}, |
| 452 |
}, |
| 453 |
}, |
| 454 |
}, |
| 455 |
}, |
| 456 |
}, |
| 457 |
}, |
| 458 |
} |
| 459 |
|
| 460 |
for _, tc := range testcases { |
| 461 |
t.Run(tc.name, func(t *testing.T) { |
| 462 |
gotOpts := convertToCSOpts(tc.tokenOpts) |
| 463 |
if diff := cmp.Diff(gotOpts, tc.expectedOpts, tokenOptionsCompareOpts...); diff != "" { |
| 464 |
t.Errorf("convertToCSOpts did not return expected opts (-got, +want): %v", diff) |
| 465 |
} |
| 466 |
}) |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
func TestConvertCSRequestToREST(t *testing.T) { |
| 471 |
testcases := []struct { |
| 472 |
name string |
| 473 |
verifierReq verifier.VerifyAttestationRequest |
| 474 |
expectedReq *ccpb.VerifyConfidentialSpaceRequest |
| 475 |
}{ |
| 476 |
{ |
| 477 |
name: "TPM attestation request", |
| 478 |
verifierReq: verifier.VerifyAttestationRequest{ |
| 479 |
Attestation: &attestpb.Attestation{ |
| 480 |
Quotes: []*tpm.Quote{{ |
| 481 |
Quote: []byte("raw quote 1"), |
| 482 |
RawSig: []byte("raw sig 1"), |
| 483 |
Pcrs: &tpm.PCRs{ |
| 484 |
Hash: tpm.HashAlgo_SHA1, |
| 485 |
Pcrs: map[uint32][]byte{ |
| 486 |
1: []byte("PCR A"), |
| 487 |
2: []byte("PCR B"), |
| 488 |
}, |
| 489 |
}, |
| 490 |
}}, |
| 491 |
EventLog: []byte("test-tcg-event-log"), |
| 492 |
CanonicalEventLog: []byte("test-canonical-event-log"), |
| 493 |
AkCert: []byte("test-ak-cert"), |
| 494 |
IntermediateCerts: [][]byte{[]byte("chain-1"), []byte("chain-2")}, |
| 495 |
}, |
| 496 |
ContainerImageSignatures: []*verifier.ContainerSignature{{ |
| 497 |
Payload: []byte("test-payload"), |
| 498 |
Signature: []byte("test-signature"), |
| 499 |
}}, |
| 500 |
GcpCredentials: [][]byte{[]byte("[email protected]")}, |
| 501 |
TokenOptions: &models.TokenOptions{ |
| 502 |
Audience: "test-aud", |
| 503 |
Nonces: []string{"test-nonce"}, |
| 504 |
TokenType: "PKI", |
| 505 |
}, |
| 506 |
}, |
| 507 |
expectedReq: &ccpb.VerifyConfidentialSpaceRequest{ |
| 508 |
TeeAttestation: &ccpb.VerifyConfidentialSpaceRequest_TpmAttestation{ |
| 509 |
TpmAttestation: &ccpb.TpmAttestation{ |
| 510 |
Quotes: []*ccpb.TpmAttestation_Quote{ |
| 511 |
{ |
| 512 |
RawQuote: []byte("raw quote 1"), |
| 513 |
RawSignature: []byte("raw sig 1"), |
| 514 |
HashAlgo: int32(tpm.HashAlgo_SHA1), |
| 515 |
PcrValues: map[int32][]byte{ |
| 516 |
1: []byte("PCR A"), |
| 517 |
2: []byte("PCR B"), |
| 518 |
}, |
| 519 |
}, |
| 520 |
}, |
| 521 |
TcgEventLog: []byte("test-tcg-event-log"), |
| 522 |
CanonicalEventLog: []byte("test-canonical-event-log"), |
| 523 |
AkCert: []byte("test-ak-cert"), |
| 524 |
CertChain: [][]byte{[]byte("chain-1"), []byte("chain-2")}, |
| 525 |
}, |
| 526 |
}, |
| 527 |
SignedEntities: []*ccpb.SignedEntity{{ |
| 528 |
ContainerImageSignatures: []*ccpb.ContainerImageSignature{{ |
| 529 |
Payload: []byte("test-payload"), |
| 530 |
Signature: []byte("test-signature"), |
| 531 |
}}, |
| 532 |
}}, |
| 533 |
GcpCredentials: &ccpb.GcpCredentials{ServiceAccountIdTokens: []string{"[email protected]"}}, |
| 534 |
Options: &ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions{ |
| 535 |
Audience: "test-aud", |
| 536 |
Nonce: []string{"test-nonce"}, |
| 537 |
TokenProfile: ccpb.TokenProfile_TOKEN_PROFILE_DEFAULT_EAT, |
| 538 |
SignatureType: ccpb.SignatureType_SIGNATURE_TYPE_PKI, |
| 539 |
}, |
| 540 |
}, |
| 541 |
}, |
| 542 |
{ |
| 543 |
name: "TDCCEL Attestation", |
| 544 |
verifierReq: verifier.VerifyAttestationRequest{ |
| 545 |
TDCCELAttestation: &verifier.TDCCELAttestation{ |
| 546 |
TdQuote: []byte("test td quote"), |
| 547 |
CcelAcpiTable: []byte("test CCEL table"), |
| 548 |
CcelData: []byte("test CCEL data"), |
| 549 |
CanonicalEventLog: []byte("test CEL"), |
| 550 |
AkCert: []byte("test-ak-cert"), |
| 551 |
IntermediateCerts: [][]byte{[]byte("chain-1"), []byte("chain-2")}, |
| 552 |
}, |
| 553 |
}, |
| 554 |
expectedReq: &ccpb.VerifyConfidentialSpaceRequest{ |
| 555 |
TeeAttestation: &ccpb.VerifyConfidentialSpaceRequest_TdCcel{ |
| 556 |
TdCcel: &ccpb.TdxCcelAttestation{ |
| 557 |
TdQuote: []byte("test td quote"), |
| 558 |
CcelAcpiTable: []byte("test CCEL table"), |
| 559 |
CcelData: []byte("test CCEL data"), |
| 560 |
CanonicalEventLog: []byte("test CEL"), |
| 561 |
}, |
| 562 |
}, |
| 563 |
GceShieldedIdentity: &ccpb.GceShieldedIdentity{ |
| 564 |
AkCert: []byte("test-ak-cert"), |
| 565 |
AkCertChain: [][]byte{[]byte("chain-1"), []byte("chain-2")}, |
| 566 |
}, |
| 567 |
Options: &ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions{ |
| 568 |
TokenProfile: ccpb.TokenProfile_TOKEN_PROFILE_DEFAULT_EAT, |
| 569 |
}, |
| 570 |
GcpCredentials: &ccpb.GcpCredentials{ServiceAccountIdTokens: []string{}}, |
| 571 |
SignedEntities: []*ccpb.SignedEntity{{ContainerImageSignatures: []*ccpb.ContainerImageSignature{}}}, |
| 572 |
}, |
| 573 |
}, |
| 574 |
} |
| 575 |
|
| 576 |
cmpOpts := append( |
| 577 |
tokenOptionsCompareOpts, |
| 578 |
cmpopts.IgnoreUnexported(ccpb.VerifyConfidentialSpaceRequest{}), |
| 579 |
cmpopts.IgnoreUnexported(ccpb.TpmAttestation{}), |
| 580 |
cmpopts.IgnoreUnexported(ccpb.TpmAttestation_Quote{}), |
| 581 |
cmpopts.IgnoreUnexported(ccpb.TdxCcelAttestation{}), |
| 582 |
cmpopts.IgnoreUnexported(ccpb.GceShieldedIdentity{}), |
| 583 |
cmpopts.IgnoreUnexported(ccpb.GcpCredentials{}), |
| 584 |
cmpopts.IgnoreUnexported(ccpb.VerifyConfidentialSpaceRequest_ConfidentialSpaceOptions{}), |
| 585 |
cmpopts.IgnoreUnexported(ccpb.ContainerImageSignature{}), |
| 586 |
cmpopts.IgnoreUnexported(ccpb.SignedEntity{}), |
| 587 |
) |
| 588 |
|
| 589 |
for _, tc := range testcases { |
| 590 |
t.Run(tc.name, func(t *testing.T) { |
| 591 |
gotReq := convertCSRequestToREST(tc.verifierReq) |
| 592 |
if diff := cmp.Diff(gotReq, tc.expectedReq, cmpOpts...); diff != "" { |
| 593 |
t.Errorf("convertCSRequestToREST returned unexpected output (-got, +want): %v", diff) |
| 594 |
} |
| 595 |
}) |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
func TestConvertCSResponseFromREST(t *testing.T) { |
| 600 |
expectedResp := &verifier.VerifyAttestationResponse{ |
| 601 |
ClaimsToken: []byte("test-token"), |
| 602 |
PartialErrs: []*status.Status{ |
| 603 |
{Code: 404, Message: "Partial Error Message"}, |
| 604 |
}, |
| 605 |
} |
| 606 |
|
| 607 |
csResp := &ccpb.VerifyConfidentialSpaceResponse{ |
| 608 |
AttestationToken: string(expectedResp.ClaimsToken), |
| 609 |
PartialErrors: expectedResp.PartialErrs, |
| 610 |
} |
| 611 |
|
| 612 |
gotResp := convertCSResponseFromREST(csResp) |
| 613 |
if diff := cmp.Diff(gotResp, expectedResp, cmpopts.IgnoreUnexported(status.Status{})); diff != "" { |
| 614 |
t.Errorf("convertCSResponseFromREST(%v) did not return expected output(-got, +want): %v", csResp, diff) |
| 615 |
} |
| 616 |
} |