| 1 |
package ita |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"encoding/json" |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/google/go-cmp/cmp" |
| 14 |
"github.com/google/go-tpm-tools/verifier" |
| 15 |
"github.com/google/go-tpm-tools/verifier/models" |
| 16 |
) |
| 17 |
|
| 18 |
var testVerifierRequest = verifier.VerifyAttestationRequest{ |
| 19 |
GcpCredentials: [][]byte{ |
| 20 |
[]byte("test-token1"), |
| 21 |
[]byte("test-token2"), |
| 22 |
}, |
| 23 |
ContainerImageSignatures: []*verifier.ContainerSignature{ |
| 24 |
{ |
| 25 |
Payload: []byte("test-payload1"), |
| 26 |
Signature: []byte("test-signature1"), |
| 27 |
}, |
| 28 |
{ |
| 29 |
Payload: []byte("test-payload2"), |
| 30 |
Signature: []byte("test-signature2"), |
| 31 |
}, |
| 32 |
}, |
| 33 |
TDCCELAttestation: &verifier.TDCCELAttestation{ |
| 34 |
CcelData: []byte("test-ccelData"), |
| 35 |
CanonicalEventLog: []byte("test-cel"), |
| 36 |
TdQuote: []byte("test-quote"), |
| 37 |
AkCert: []byte("test-akcert"), |
| 38 |
IntermediateCerts: [][]byte{ |
| 39 |
[]byte("test-intermediate1"), |
| 40 |
[]byte("test-intermediate2"), |
| 41 |
}, |
| 42 |
}, |
| 43 |
Challenge: &verifier.Challenge{ |
| 44 |
Val: []byte("test-nonce-val"), |
| 45 |
Iat: []byte("123456"), |
| 46 |
Signature: []byte("test-nonce-sig"), |
| 47 |
}, |
| 48 |
TokenOptions: &models.TokenOptions{ |
| 49 |
Audience: "testaud", |
| 50 |
Nonces: []string{"testnonces"}, |
| 51 |
TokenType: "testtokentype", |
| 52 |
}, |
| 53 |
} |
| 54 |
|
| 55 |
func validateHTTPRequest(t *testing.T, r *http.Request, expectedMethod string, expectedHeaders map[string]string, expectedPath string) { |
| 56 |
// Verify HTTP Method. |
| 57 |
if r.Method != expectedMethod { |
| 58 |
t.Errorf("HTTP request does not have expected method: got %v, want %v", r.Method, http.MethodGet) |
| 59 |
} |
| 60 |
|
| 61 |
// Verify HTTP headers. |
| 62 |
for key, val := range expectedHeaders { |
| 63 |
if r.Header.Get(key) != val { |
| 64 |
t.Errorf("HTTP request does not have expected Content-Type header: got %s, want %s", r.Header.Get(key), val) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// Verify requested path. |
| 69 |
if expectedPath != "" && r.URL.Path != expectedPath { |
| 70 |
t.Errorf("HTTP request does not have expected endpoint: got %v, want %v", r.URL.Path, nonceEndpoint) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestCreateChallenge(t *testing.T) { |
| 75 |
testNonce := &itaNonce{ |
| 76 |
Val: []byte("test-val"), |
| 77 |
Iat: []byte("test-iat"), |
| 78 |
Signature: []byte("test-signature"), |
| 79 |
} |
| 80 |
|
| 81 |
expectedNonce, err := createHashedNonce(testNonce) |
| 82 |
if err != nil { |
| 83 |
t.Fatalf("Unable to create expected nonce: %v", err) |
| 84 |
} |
| 85 |
|
| 86 |
expectedAPIKey := "test-api-key" |
| 87 |
expectedHeaders := map[string]string{ |
| 88 |
apiKeyHeader: expectedAPIKey, |
| 89 |
acceptHeader: applicationJSON, |
| 90 |
} |
| 91 |
|
| 92 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 93 |
validateHTTPRequest(t, r, http.MethodGet, expectedHeaders, nonceEndpoint) |
| 94 |
|
| 95 |
// Send HTTP Response. |
| 96 |
marshaled, err := json.Marshal(testNonce) |
| 97 |
if err != nil { |
| 98 |
t.Fatalf("Unable to marshal server response: %s", testNonce) |
| 99 |
} |
| 100 |
|
| 101 |
w.Write(marshaled) |
| 102 |
})) |
| 103 |
|
| 104 |
itaClient := &client{ |
| 105 |
inner: http.DefaultClient, |
| 106 |
apiURL: ts.URL, |
| 107 |
apiKey: expectedAPIKey, |
| 108 |
} |
| 109 |
|
| 110 |
challenge, err := itaClient.CreateChallenge(context.Background()) |
| 111 |
if err != nil { |
| 112 |
t.Fatalf("CreateChallenge() returned error: %v", err) |
| 113 |
} |
| 114 |
|
| 115 |
expectedChallenge := &verifier.Challenge{ |
| 116 |
Name: challengeNamePrefix + string(testNonce.Val), |
| 117 |
Nonce: expectedNonce, |
| 118 |
Val: testNonce.Val, |
| 119 |
Iat: testNonce.Iat, |
| 120 |
Signature: testNonce.Signature, |
| 121 |
} |
| 122 |
|
| 123 |
if diff := cmp.Diff(*challenge, *expectedChallenge); diff != "" { |
| 124 |
t.Errorf("CreateChallenge() did not return the expected challenge: %v", diff) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func TestVerifyAttestation(t *testing.T) { |
| 129 |
expectedReq := convertRequestToTokenRequest(testVerifierRequest) |
| 130 |
|
| 131 |
expectedResp := &verifier.VerifyAttestationResponse{ |
| 132 |
ClaimsToken: []byte("test-ita-token"), |
| 133 |
} |
| 134 |
|
| 135 |
expectedAPIKey := "test-api-key" |
| 136 |
expectedHeaders := map[string]string{ |
| 137 |
apiKeyHeader: expectedAPIKey, |
| 138 |
acceptHeader: applicationJSON, |
| 139 |
contentTypeHeader: applicationJSON, |
| 140 |
} |
| 141 |
|
| 142 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 143 |
validateHTTPRequest(t, r, http.MethodPost, expectedHeaders, tokenEndpoint) |
| 144 |
|
| 145 |
// Verify HTTP Request body. |
| 146 |
defer r.Body.Close() |
| 147 |
reqBody, err := io.ReadAll(r.Body) |
| 148 |
if err != nil { |
| 149 |
t.Fatalf("Error reading HTTP request body: %s", err) |
| 150 |
} |
| 151 |
|
| 152 |
req := tokenRequest{} |
| 153 |
if err = json.Unmarshal(reqBody, &req); err != nil { |
| 154 |
t.Fatalf("Error unmarshaling HTTP request body: %s", err) |
| 155 |
} |
| 156 |
|
| 157 |
if diff := cmp.Diff(req, expectedReq); diff != "" { |
| 158 |
t.Errorf("Incorrect request recieved by server: %v", diff) |
| 159 |
} |
| 160 |
|
| 161 |
// Send HTTP Response. |
| 162 |
resp := tokenResponse{ |
| 163 |
Token: string(expectedResp.ClaimsToken), |
| 164 |
} |
| 165 |
marshaled, err := json.Marshal(resp) |
| 166 |
if err != nil { |
| 167 |
t.Fatalf("Unable to marshal server response: %s", expectedResp) |
| 168 |
} |
| 169 |
|
| 170 |
w.Write(marshaled) |
| 171 |
})) |
| 172 |
|
| 173 |
itaClient := &client{ |
| 174 |
inner: http.DefaultClient, |
| 175 |
apiURL: ts.URL, |
| 176 |
apiKey: expectedAPIKey, |
| 177 |
} |
| 178 |
|
| 179 |
verifyResp, err := itaClient.VerifyAttestation(context.Background(), testVerifierRequest) |
| 180 |
if err != nil { |
| 181 |
t.Fatalf("VerifyAttestation() returned error: %v", err) |
| 182 |
} |
| 183 |
|
| 184 |
if diff := cmp.Diff(verifyResp, expectedResp); diff != "" { |
| 185 |
t.Errorf("VerifyAttestation did not return expected response: %v", diff) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func TestDoHTTPRequest(t *testing.T) { |
| 190 |
expectedHeaders := map[string]string{ |
| 191 |
apiKeyHeader: "testAPIKey", |
| 192 |
acceptHeader: applicationJSON, |
| 193 |
} |
| 194 |
|
| 195 |
expectedMethod := http.MethodPost |
| 196 |
expectedReq := tokenRequest{ |
| 197 |
PolicyMatch: true, |
| 198 |
SigAlg: "testsigalg", |
| 199 |
TDX: tdxEvidence{ |
| 200 |
EventLog: []byte("test event log"), |
| 201 |
CanonicalEventLog: []byte("test CEL"), |
| 202 |
Quote: []byte("test quote"), |
| 203 |
}, |
| 204 |
} |
| 205 |
|
| 206 |
expectedResp := tokenResponse{ |
| 207 |
Token: "test-token", |
| 208 |
} |
| 209 |
|
| 210 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 211 |
validateHTTPRequest(t, r, expectedMethod, expectedHeaders, "") |
| 212 |
|
| 213 |
// Verify HTTP Request body. |
| 214 |
defer r.Body.Close() |
| 215 |
reqBody, err := io.ReadAll(r.Body) |
| 216 |
if err != nil { |
| 217 |
t.Fatalf("Error reading HTTP request body: %s", err) |
| 218 |
} |
| 219 |
|
| 220 |
req := tokenRequest{} |
| 221 |
if err = json.Unmarshal(reqBody, &req); err != nil { |
| 222 |
t.Fatalf("Error unmarshaling HTTP request body: %s", err) |
| 223 |
} |
| 224 |
|
| 225 |
if diff := cmp.Diff(req, expectedReq); diff != "" { |
| 226 |
t.Errorf("Incorrect request recieved by server: %v", diff) |
| 227 |
} |
| 228 |
|
| 229 |
// Send HTTP Response. |
| 230 |
marshaled, err := json.Marshal(expectedResp) |
| 231 |
if err != nil { |
| 232 |
t.Fatalf("Unable to marshal server response: %s", expectedResp) |
| 233 |
} |
| 234 |
|
| 235 |
w.Write(marshaled) |
| 236 |
})) |
| 237 |
|
| 238 |
itaClient := client{ |
| 239 |
inner: http.DefaultClient, |
| 240 |
} |
| 241 |
|
| 242 |
resp := &tokenResponse{} |
| 243 |
if err := itaClient.doHTTPRequest(expectedMethod, ts.URL, expectedReq, expectedHeaders, resp); err != nil { |
| 244 |
t.Fatalf("doHTTPRequest returned error: %v", err) |
| 245 |
} |
| 246 |
|
| 247 |
if diff := cmp.Diff(*resp, expectedResp); diff != "" { |
| 248 |
t.Errorf("doHTTPRequest did not return expected response: %v", diff) |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
func TestConvertRequestToTokenRequest(t *testing.T) { |
| 253 |
expectedRequest := tokenRequest{ |
| 254 |
PolicyMatch: true, |
| 255 |
TDX: tdxEvidence{ |
| 256 |
// Add EventLog field. |
| 257 |
EventLog: testVerifierRequest.TDCCELAttestation.CcelData, |
| 258 |
CanonicalEventLog: testVerifierRequest.TDCCELAttestation.CanonicalEventLog, |
| 259 |
Quote: testVerifierRequest.TDCCELAttestation.TdQuote, |
| 260 |
VerifierNonce: nonce{ |
| 261 |
Val: testVerifierRequest.Challenge.Val, |
| 262 |
Iat: testVerifierRequest.Challenge.Iat, |
| 263 |
Signature: testVerifierRequest.Challenge.Signature, |
| 264 |
}, |
| 265 |
}, |
| 266 |
SigAlg: "RS256", // Figure out what this should be. |
| 267 |
GCP: gcpData{ |
| 268 |
GcpCredentials: []string{ |
| 269 |
string(testVerifierRequest.GcpCredentials[0]), |
| 270 |
string(testVerifierRequest.GcpCredentials[1]), |
| 271 |
}, |
| 272 |
AKCert: testVerifierRequest.TDCCELAttestation.AkCert, |
| 273 |
IntermediateCerts: testVerifierRequest.TDCCELAttestation.IntermediateCerts, |
| 274 |
CSInfo: confidentialSpaceInfo{ |
| 275 |
SignedEntities: []containerSignature{ |
| 276 |
{ |
| 277 |
Payload: testVerifierRequest.ContainerImageSignatures[0].Payload, |
| 278 |
Signature: testVerifierRequest.ContainerImageSignatures[0].Signature, |
| 279 |
}, |
| 280 |
{ |
| 281 |
Payload: testVerifierRequest.ContainerImageSignatures[1].Payload, |
| 282 |
Signature: testVerifierRequest.ContainerImageSignatures[1].Signature, |
| 283 |
}, |
| 284 |
}, |
| 285 |
TokenOpts: tokenOptions{ |
| 286 |
Audience: testVerifierRequest.TokenOptions.Audience, |
| 287 |
Nonces: testVerifierRequest.TokenOptions.Nonces, |
| 288 |
TokenType: testVerifierRequest.TokenOptions.TokenType, |
| 289 |
TokenTypeOpts: tokenTypeOptions{}, |
| 290 |
}, |
| 291 |
}, |
| 292 |
}, |
| 293 |
} |
| 294 |
|
| 295 |
convertedReq := convertRequestToTokenRequest(testVerifierRequest) |
| 296 |
|
| 297 |
if diff := cmp.Diff(convertedReq, expectedRequest); diff != "" { |
| 298 |
t.Errorf("convertRequestToTokenRequest did not return expected tokenRequest: %v", diff) |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
func TestConvertRequestToTokenRequestWithCCELDataPadding(t *testing.T) { |
| 303 |
padding := bytes.Repeat([]byte{255}, 20) |
| 304 |
|
| 305 |
request := verifier.VerifyAttestationRequest{ |
| 306 |
TDCCELAttestation: &verifier.TDCCELAttestation{ |
| 307 |
CcelData: append(testVerifierRequest.TDCCELAttestation.CcelData, padding...), |
| 308 |
CanonicalEventLog: []byte("test-cel"), |
| 309 |
TdQuote: []byte("test-quote"), |
| 310 |
AkCert: []byte("test-akcert"), |
| 311 |
IntermediateCerts: [][]byte{ |
| 312 |
[]byte("test-intermediate1"), |
| 313 |
[]byte("test-intermediate2"), |
| 314 |
}, |
| 315 |
}, |
| 316 |
Challenge: testVerifierRequest.Challenge, |
| 317 |
} |
| 318 |
|
| 319 |
expectedRequest := tokenRequest{ |
| 320 |
PolicyMatch: true, |
| 321 |
TDX: tdxEvidence{ |
| 322 |
// Expect padding to be stripped in converted request. |
| 323 |
EventLog: testVerifierRequest.TDCCELAttestation.CcelData, |
| 324 |
CanonicalEventLog: request.TDCCELAttestation.CanonicalEventLog, |
| 325 |
Quote: request.TDCCELAttestation.TdQuote, |
| 326 |
VerifierNonce: nonce{ |
| 327 |
Val: request.Challenge.Val, |
| 328 |
Iat: request.Challenge.Iat, |
| 329 |
Signature: request.Challenge.Signature, |
| 330 |
}, |
| 331 |
}, |
| 332 |
SigAlg: "RS256", // Figure out what this should be. |
| 333 |
GCP: gcpData{ |
| 334 |
AKCert: testVerifierRequest.TDCCELAttestation.AkCert, |
| 335 |
IntermediateCerts: testVerifierRequest.TDCCELAttestation.IntermediateCerts, |
| 336 |
}, |
| 337 |
} |
| 338 |
|
| 339 |
convertedReq := convertRequestToTokenRequest(request) |
| 340 |
|
| 341 |
if diff := cmp.Diff(convertedReq, expectedRequest); diff != "" { |
| 342 |
t.Errorf("convertRequestToTokenRequest did not return expected tokenRequest: %v", diff) |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
func TestURLFromRegion(t *testing.T) { |
| 347 |
for region, expectedURL := range regionalURLs { |
| 348 |
t.Run(region+" region", func(t *testing.T) { |
| 349 |
url, err := urlFromRegion(region) |
| 350 |
if err != nil { |
| 351 |
t.Fatalf("urlAndKey returned error: %v", err) |
| 352 |
} |
| 353 |
|
| 354 |
if url != expectedURL { |
| 355 |
t.Errorf("urlAndKey did not return expected URL: got %v, want %v", url, expectedURL) |
| 356 |
} |
| 357 |
}) |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
func TestURLFromRegionError(t *testing.T) { |
| 362 |
testcases := []struct { |
| 363 |
name string |
| 364 |
region string |
| 365 |
expectedSubstr string |
| 366 |
}{ |
| 367 |
{ |
| 368 |
name: "Unsupported region", |
| 369 |
region: "ANTARCTICA", |
| 370 |
expectedSubstr: "unsupported region", |
| 371 |
}, |
| 372 |
{ |
| 373 |
name: "Empty input", |
| 374 |
region: "", |
| 375 |
expectedSubstr: "region required", |
| 376 |
}, |
| 377 |
} |
| 378 |
|
| 379 |
for _, tc := range testcases { |
| 380 |
t.Run(tc.name, func(t *testing.T) { |
| 381 |
_, err := urlFromRegion(tc.region) |
| 382 |
if err == nil { |
| 383 |
t.Fatal("urlAndKey returned successfully, expected error") |
| 384 |
} |
| 385 |
|
| 386 |
if !strings.Contains(err.Error(), tc.expectedSubstr) { |
| 387 |
t.Errorf("urlAndKey did not return expected error: got %v, want %v", err.Error(), tc.expectedSubstr) |
| 388 |
} |
| 389 |
}) |
| 390 |
} |
| 391 |
} |