| 1 |
package teeserver |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"encoding/json" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"net/http" |
| 11 |
"net/http/httptest" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/google/go-cmp/cmp" |
| 16 |
gecel "github.com/google/go-eventlog/cel" |
| 17 |
keymanager "github.com/google/go-tpm-tools/keymanager/km_common/proto" |
| 18 |
"github.com/google/go-tpm-tools/launcher/agent" |
| 19 |
"github.com/google/go-tpm-tools/launcher/internal/experiments" |
| 20 |
"github.com/google/go-tpm-tools/launcher/internal/logging" |
| 21 |
"github.com/google/go-tpm-tools/launcher/spec" |
| 22 |
"github.com/google/go-tpm-tools/verifier" |
| 23 |
"github.com/google/go-tpm-tools/verifier/models" |
| 24 |
"google.golang.org/grpc/codes" |
| 25 |
"google.golang.org/grpc/status" |
| 26 |
"google.golang.org/protobuf/encoding/protojson" |
| 27 |
|
| 28 |
attestationpb "github.com/GoogleCloudPlatform/confidential-space/server/proto/gen/attestation" |
| 29 |
) |
| 30 |
|
| 31 |
// Implements verifier.Client interface so it can be used to initialize test attestHandlers |
| 32 |
type fakeVerifierClient struct{} |
| 33 |
|
| 34 |
func (f *fakeVerifierClient) CreateChallenge(_ context.Context) (*verifier.Challenge, error) { |
| 35 |
return nil, fmt.Errorf("unimplemented") |
| 36 |
} |
| 37 |
|
| 38 |
func (f *fakeVerifierClient) VerifyAttestation(_ context.Context, _ verifier.VerifyAttestationRequest) (*verifier.VerifyAttestationResponse, error) { |
| 39 |
return nil, fmt.Errorf("unimplemented") |
| 40 |
} |
| 41 |
|
| 42 |
func (f *fakeVerifierClient) VerifyConfidentialSpace(_ context.Context, _ verifier.VerifyAttestationRequest) (*verifier.VerifyAttestationResponse, error) { |
| 43 |
return nil, fmt.Errorf("unimplemented") |
| 44 |
} |
| 45 |
|
| 46 |
type fakeAttestationAgent struct { |
| 47 |
measureEventFunc func(gecel.Content) error |
| 48 |
attestFunc func(context.Context, agent.AttestAgentOpts) ([]byte, error) |
| 49 |
attestWithClientFunc func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) |
| 50 |
attestationEvidenceFunc func(context.Context, []byte, []byte) (*attestationpb.VmAttestation, error) |
| 51 |
} |
| 52 |
|
| 53 |
func (f fakeAttestationAgent) Attest(c context.Context, a agent.AttestAgentOpts) ([]byte, error) { |
| 54 |
return f.attestFunc(c, a) |
| 55 |
} |
| 56 |
|
| 57 |
func (f fakeAttestationAgent) AttestWithClient(c context.Context, a agent.AttestAgentOpts, v verifier.Client) ([]byte, error) { |
| 58 |
return f.attestWithClientFunc(c, a, v) |
| 59 |
} |
| 60 |
|
| 61 |
func (f fakeAttestationAgent) AttestationEvidence(c context.Context, nonce []byte, extraData []byte) (*attestationpb.VmAttestation, error) { |
| 62 |
return f.attestationEvidenceFunc(c, nonce, extraData) |
| 63 |
} |
| 64 |
|
| 65 |
func (f fakeAttestationAgent) MeasureEvent(c gecel.Content) error { |
| 66 |
return f.measureEventFunc(c) |
| 67 |
} |
| 68 |
|
| 69 |
func (f fakeAttestationAgent) Refresh(_ context.Context) error { |
| 70 |
return nil |
| 71 |
} |
| 72 |
|
| 73 |
func (f fakeAttestationAgent) Close() error { |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
// Mock for KeyClaimsProvider interface |
| 78 |
type mockClaimsProvider struct { |
| 79 |
claims map[keymanager.KeyType]*keymanager.KeyClaims |
| 80 |
err error |
| 81 |
} |
| 82 |
|
| 83 |
func (m *mockClaimsProvider) GetKeyClaims(_ context.Context, _ string, kt keymanager.KeyType) (*keymanager.KeyClaims, error) { |
| 84 |
if m.err != nil { |
| 85 |
return nil, m.err |
| 86 |
} |
| 87 |
return m.claims[kt], nil |
| 88 |
} |
| 89 |
|
| 90 |
func TestGetDefaultToken(t *testing.T) { |
| 91 |
testTokenContent := "test token" |
| 92 |
|
| 93 |
ah := attestHandler{ |
| 94 |
logger: logging.SimpleLogger(), |
| 95 |
clients: AttestClients{ |
| 96 |
GCA: &fakeVerifierClient{}, |
| 97 |
}, |
| 98 |
attestAgent: fakeAttestationAgent{ |
| 99 |
attestWithClientFunc: func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) { |
| 100 |
return []byte(testTokenContent), nil |
| 101 |
}, |
| 102 |
}} |
| 103 |
|
| 104 |
req := httptest.NewRequest(http.MethodGet, "/v1/token", nil) |
| 105 |
w := httptest.NewRecorder() |
| 106 |
|
| 107 |
ah.getToken(w, req) |
| 108 |
data, err := io.ReadAll(w.Result().Body) |
| 109 |
if err != nil { |
| 110 |
t.Error(err) |
| 111 |
} |
| 112 |
|
| 113 |
if w.Code != http.StatusOK { |
| 114 |
t.Errorf("got return code: %d, want: %d", w.Code, http.StatusOK) |
| 115 |
} |
| 116 |
if diff := cmp.Diff(testTokenContent, string(data)); diff != "" { |
| 117 |
t.Errorf("getToken() response body mismatch (-want +got):\n%s", diff) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func TestGetDefaultTokenServerError(t *testing.T) { |
| 122 |
ah := attestHandler{ |
| 123 |
logger: logging.SimpleLogger(), |
| 124 |
clients: AttestClients{ |
| 125 |
GCA: &fakeVerifierClient{}, |
| 126 |
}, |
| 127 |
attestAgent: fakeAttestationAgent{ |
| 128 |
attestWithClientFunc: func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) { |
| 129 |
return nil, errors.New("internal server error from agent") |
| 130 |
}, |
| 131 |
}} |
| 132 |
|
| 133 |
req := httptest.NewRequest(http.MethodGet, "/v1/token", nil) |
| 134 |
w := httptest.NewRecorder() |
| 135 |
|
| 136 |
ah.getToken(w, req) |
| 137 |
data, err := io.ReadAll(w.Result().Body) |
| 138 |
if err != nil { |
| 139 |
t.Error(err) |
| 140 |
} |
| 141 |
|
| 142 |
if w.Code != http.StatusInternalServerError { |
| 143 |
t.Errorf("got return code: %d, want: %d", w.Code, http.StatusInternalServerError) |
| 144 |
} |
| 145 |
expectedError := "failed to retrieve attestation service token: internal server error from agent" |
| 146 |
if diff := cmp.Diff(expectedError, string(data)); diff != "" { |
| 147 |
t.Errorf("getToken() response body mismatch (-want +got):\n%s", diff) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func TestCustomToken(t *testing.T) { |
| 152 |
tests := []struct { |
| 153 |
testName string |
| 154 |
body string |
| 155 |
attestWithClientFunc func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) |
| 156 |
want int |
| 157 |
}{ |
| 158 |
{ |
| 159 |
testName: "TestNoAudiencePostRequest", |
| 160 |
body: `{ |
| 161 |
"audience": "", |
| 162 |
"nonces": ["thisIsAcustomNonce"], |
| 163 |
"token_type": "OIDC" |
| 164 |
}`, |
| 165 |
attestWithClientFunc: func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) { |
| 166 |
t.Errorf("This method should not be called") |
| 167 |
return nil, nil |
| 168 |
}, |
| 169 |
want: http.StatusBadRequest, |
| 170 |
}, |
| 171 |
{ |
| 172 |
testName: "TestRequestFailurePassedToCaller", |
| 173 |
body: `{ |
| 174 |
"audience": "audience", |
| 175 |
"nonces": ["thisIsAcustomNonce"], |
| 176 |
"token_type": "OIDC" |
| 177 |
}`, |
| 178 |
attestWithClientFunc: func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) { |
| 179 |
return nil, errors.New("Error") |
| 180 |
}, |
| 181 |
want: http.StatusInternalServerError, |
| 182 |
}, |
| 183 |
{ |
| 184 |
testName: "TestTokenTypeRequired", |
| 185 |
body: `{ |
| 186 |
"audience": "audience", |
| 187 |
"nonces": ["thisIsAcustomNonce"], |
| 188 |
"token_type": "" |
| 189 |
}`, |
| 190 |
attestWithClientFunc: func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) { |
| 191 |
t.Errorf("This method should not be called") |
| 192 |
return nil, nil |
| 193 |
}, |
| 194 |
want: http.StatusBadRequest, |
| 195 |
}, |
| 196 |
{ |
| 197 |
testName: "TestRequestSuccessPassedToCaller", |
| 198 |
body: `{ |
| 199 |
"audience": "audience", |
| 200 |
"nonces": ["thisIsAcustomNonce"], |
| 201 |
"token_type": "OIDC" |
| 202 |
}`, |
| 203 |
attestWithClientFunc: func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) { |
| 204 |
return []byte{}, nil |
| 205 |
}, |
| 206 |
want: http.StatusOK, |
| 207 |
}, |
| 208 |
{ |
| 209 |
testName: "TestPrincipalTagOptionsSuccess", |
| 210 |
body: `{ |
| 211 |
"audience": "audience", |
| 212 |
"nonces": ["thisIsAcustomNonce"], |
| 213 |
"token_type": "OIDC", |
| 214 |
"aws_principal_tag_options" : { |
| 215 |
"allowed_principal_tags": { |
| 216 |
"container_image_signatures" : { |
| 217 |
"key_ids": ["test1", "test2"] |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
}`, |
| 222 |
attestWithClientFunc: func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) { |
| 223 |
return []byte{}, nil |
| 224 |
}, |
| 225 |
want: http.StatusOK, |
| 226 |
}, |
| 227 |
} |
| 228 |
|
| 229 |
verifiers := []struct { |
| 230 |
name string |
| 231 |
url string |
| 232 |
tokenMethod func(ah *attestHandler, w http.ResponseWriter, r *http.Request) |
| 233 |
}{ |
| 234 |
{ |
| 235 |
name: "GCA Handler", |
| 236 |
url: "/v1/token", |
| 237 |
tokenMethod: (*attestHandler).getToken, |
| 238 |
}, |
| 239 |
{ |
| 240 |
name: "ITA Handler", |
| 241 |
url: "/v1/intel/token", |
| 242 |
tokenMethod: (*attestHandler).getITAToken, |
| 243 |
}, |
| 244 |
} |
| 245 |
|
| 246 |
for _, vf := range verifiers { |
| 247 |
t.Run(vf.name, func(t *testing.T) { |
| 248 |
for _, test := range tests { |
| 249 |
ah := attestHandler{ |
| 250 |
logger: logging.SimpleLogger(), |
| 251 |
clients: AttestClients{ |
| 252 |
GCA: &fakeVerifierClient{}, |
| 253 |
ITA: &fakeVerifierClient{}, |
| 254 |
}, |
| 255 |
attestAgent: fakeAttestationAgent{ |
| 256 |
attestWithClientFunc: test.attestWithClientFunc, |
| 257 |
}} |
| 258 |
|
| 259 |
b := strings.NewReader(test.body) |
| 260 |
|
| 261 |
req := httptest.NewRequest(http.MethodPost, vf.url, b) |
| 262 |
w := httptest.NewRecorder() |
| 263 |
|
| 264 |
vf.tokenMethod(&ah, w, req) |
| 265 |
|
| 266 |
_, err := io.ReadAll(w.Result().Body) |
| 267 |
if err != nil { |
| 268 |
t.Error(err) |
| 269 |
} |
| 270 |
|
| 271 |
if w.Code != test.want { |
| 272 |
t.Errorf("testcase '%v': got return code: %d, want: %d", test.testName, w.Code, test.want) |
| 273 |
} |
| 274 |
} |
| 275 |
}) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
func TestHandleAttestError(t *testing.T) { |
| 280 |
body := `{ |
| 281 |
"audience": "audience", |
| 282 |
"nonces": ["thisIsAcustomNonce"], |
| 283 |
"token_type": "OIDC" |
| 284 |
}` |
| 285 |
|
| 286 |
errorCases := []struct { |
| 287 |
name string |
| 288 |
err error |
| 289 |
wantStatusCode int |
| 290 |
}{ |
| 291 |
{ |
| 292 |
name: "FailedPrecondition error", |
| 293 |
err: status.New(codes.FailedPrecondition, "bad state").Err(), |
| 294 |
wantStatusCode: http.StatusBadRequest, |
| 295 |
}, |
| 296 |
{ |
| 297 |
name: "PermissionDenied error", |
| 298 |
err: status.New(codes.PermissionDenied, "denied").Err(), |
| 299 |
wantStatusCode: http.StatusBadRequest, |
| 300 |
}, |
| 301 |
{ |
| 302 |
name: "Internal error", |
| 303 |
err: status.New(codes.Internal, "internal server error").Err(), |
| 304 |
wantStatusCode: http.StatusInternalServerError, |
| 305 |
}, |
| 306 |
{ |
| 307 |
name: "Unavailable error", |
| 308 |
err: status.New(codes.Unavailable, "service unavailable").Err(), |
| 309 |
wantStatusCode: http.StatusInternalServerError, |
| 310 |
}, |
| 311 |
{ |
| 312 |
name: "non-gRPC error", |
| 313 |
err: errors.New("a generic error"), |
| 314 |
wantStatusCode: http.StatusInternalServerError, |
| 315 |
}, |
| 316 |
} |
| 317 |
|
| 318 |
verifiers := []struct { |
| 319 |
name string |
| 320 |
url string |
| 321 |
tokenMethod func(ah *attestHandler, w http.ResponseWriter, r *http.Request) |
| 322 |
}{ |
| 323 |
{ |
| 324 |
name: "GCA Handler", |
| 325 |
url: "/v1/token", |
| 326 |
tokenMethod: (*attestHandler).getToken, |
| 327 |
}, |
| 328 |
{ |
| 329 |
name: "ITA Handler", |
| 330 |
url: "/v1/intel/token", |
| 331 |
tokenMethod: (*attestHandler).getITAToken, |
| 332 |
}, |
| 333 |
} |
| 334 |
|
| 335 |
for _, vf := range verifiers { |
| 336 |
t.Run(vf.name, func(t *testing.T) { |
| 337 |
for _, tc := range errorCases { |
| 338 |
t.Run(tc.name, func(t *testing.T) { |
| 339 |
ah := attestHandler{ |
| 340 |
logger: logging.SimpleLogger(), |
| 341 |
clients: AttestClients{ |
| 342 |
GCA: &fakeVerifierClient{}, |
| 343 |
ITA: &fakeVerifierClient{}, |
| 344 |
}, |
| 345 |
attestAgent: fakeAttestationAgent{ |
| 346 |
attestWithClientFunc: func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) { |
| 347 |
return nil, tc.err |
| 348 |
}, |
| 349 |
}, |
| 350 |
} |
| 351 |
|
| 352 |
req := httptest.NewRequest(http.MethodPost, vf.url, strings.NewReader(body)) |
| 353 |
w := httptest.NewRecorder() |
| 354 |
|
| 355 |
vf.tokenMethod(&ah, w, req) |
| 356 |
|
| 357 |
if w.Code != tc.wantStatusCode { |
| 358 |
t.Errorf("got status code %d, want %d", w.Code, tc.wantStatusCode) |
| 359 |
} |
| 360 |
|
| 361 |
_, err := io.ReadAll(w.Result().Body) |
| 362 |
if err != nil { |
| 363 |
t.Errorf("failed to read response body: %v", err) |
| 364 |
} |
| 365 |
}) |
| 366 |
} |
| 367 |
}) |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
func TestHandleAttestError_NilClient(t *testing.T) { |
| 372 |
verifiers := []struct { |
| 373 |
name string |
| 374 |
url string |
| 375 |
handler func(ah *attestHandler, w http.ResponseWriter, r *http.Request) |
| 376 |
}{ |
| 377 |
{name: "GCA Handler", url: "/v1/token", handler: (*attestHandler).getToken}, |
| 378 |
{name: "ITA Handler", url: "/v1/intel/token", handler: (*attestHandler).getITAToken}, |
| 379 |
} |
| 380 |
|
| 381 |
for _, vf := range verifiers { |
| 382 |
t.Run(vf.name, func(t *testing.T) { |
| 383 |
ah := attestHandler{ |
| 384 |
logger: logging.SimpleLogger(), |
| 385 |
clients: AttestClients{}, // No clients defined |
| 386 |
} |
| 387 |
|
| 388 |
req := httptest.NewRequest(http.MethodPost, vf.url, strings.NewReader("")) |
| 389 |
w := httptest.NewRecorder() |
| 390 |
vf.handler(&ah, w, req) |
| 391 |
|
| 392 |
const wantStatusCode = http.StatusInternalServerError |
| 393 |
if w.Code != wantStatusCode { |
| 394 |
t.Errorf("got status code %d, want %d", w.Code, wantStatusCode) |
| 395 |
} |
| 396 |
}) |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
func TestCustomTokenDataParsedSuccessfully(t *testing.T) { |
| 401 |
tests := []struct { |
| 402 |
testName string |
| 403 |
body string |
| 404 |
attestFunc func(context.Context, agent.AttestAgentOpts) ([]byte, error) |
| 405 |
wantCode int |
| 406 |
wantOpts agent.AttestAgentOpts |
| 407 |
}{ |
| 408 |
{ |
| 409 |
testName: "TestKeyIdsReadSuccessfullyEvenWithInvalidTokenTypeMatch", |
| 410 |
body: `{ |
| 411 |
"audience": "audience", |
| 412 |
"nonces": ["thisIsAcustomNonce"], |
| 413 |
"token_type": "OIDC", |
| 414 |
"aws_principal_tag_options" : { |
| 415 |
"allowed_principal_tags": { |
| 416 |
"container_image_signatures" : { |
| 417 |
"key_ids": ["test1", "test2"] |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
}`, |
| 422 |
wantCode: http.StatusOK, |
| 423 |
wantOpts: agent.AttestAgentOpts{ |
| 424 |
TokenOptions: &models.TokenOptions{ |
| 425 |
Audience: "audience", |
| 426 |
Nonces: []string{"thisIsAcustomNonce"}, |
| 427 |
TokenType: "OIDC", |
| 428 |
PrincipalTagOptions: &models.AWSPrincipalTagsOptions{ |
| 429 |
AllowedPrincipalTags: &models.AllowedPrincipalTags{ |
| 430 |
ContainerImageSignatures: &models.ContainerImageSignatures{ |
| 431 |
KeyIDs: []string{"test1", "test2"}, |
| 432 |
}, |
| 433 |
}, |
| 434 |
}, |
| 435 |
}, |
| 436 |
}, |
| 437 |
}, |
| 438 |
{ |
| 439 |
testName: "PartialAwsPrincipalTagOptionsOK", |
| 440 |
body: `{ |
| 441 |
"audience": "audience", |
| 442 |
"nonces": ["thisIsAcustomNonce"], |
| 443 |
"token_type": "OIDC", |
| 444 |
"aws_principal_tag_options" : { |
| 445 |
} |
| 446 |
}`, |
| 447 |
wantCode: http.StatusOK, |
| 448 |
wantOpts: agent.AttestAgentOpts{ |
| 449 |
TokenOptions: &models.TokenOptions{ |
| 450 |
Audience: "audience", |
| 451 |
Nonces: []string{"thisIsAcustomNonce"}, |
| 452 |
TokenType: "OIDC", |
| 453 |
PrincipalTagOptions: &models.AWSPrincipalTagsOptions{}, |
| 454 |
}, |
| 455 |
}, |
| 456 |
}, |
| 457 |
{ |
| 458 |
testName: "MorePartialAwsPrincipalTagOptionsOK", |
| 459 |
body: `{ |
| 460 |
"audience": "audience", |
| 461 |
"nonces": ["thisIsAcustomNonce"], |
| 462 |
"token_type": "OIDC", |
| 463 |
"aws_principal_tag_options" : { |
| 464 |
"allowed_principal_tags": { |
| 465 |
} |
| 466 |
} |
| 467 |
}`, |
| 468 |
wantCode: http.StatusOK, |
| 469 |
wantOpts: agent.AttestAgentOpts{ |
| 470 |
TokenOptions: &models.TokenOptions{ |
| 471 |
Audience: "audience", |
| 472 |
Nonces: []string{"thisIsAcustomNonce"}, |
| 473 |
TokenType: "OIDC", |
| 474 |
PrincipalTagOptions: &models.AWSPrincipalTagsOptions{ |
| 475 |
AllowedPrincipalTags: &models.AllowedPrincipalTags{}, |
| 476 |
}, |
| 477 |
}, |
| 478 |
}, |
| 479 |
}, |
| 480 |
{ |
| 481 |
testName: "InvalidJSONNotOkay", |
| 482 |
body: `{ |
| 483 |
"audience": "audience", |
| 484 |
"nonces": ["thisIsAcustomNonce"], |
| 485 |
"token_type": "OIDC", |
| 486 |
"aws_principal_tag_options" : { |
| 487 |
"allowed_principal_tag": { |
| 488 |
} |
| 489 |
} |
| 490 |
}`, |
| 491 |
wantCode: http.StatusBadRequest, |
| 492 |
wantOpts: agent.AttestAgentOpts{ |
| 493 |
TokenOptions: &models.TokenOptions{ |
| 494 |
Audience: "audience", |
| 495 |
Nonces: []string{"thisIsAcustomNonce"}, |
| 496 |
TokenType: "OIDC", |
| 497 |
PrincipalTagOptions: &models.AWSPrincipalTagsOptions{ |
| 498 |
AllowedPrincipalTags: &models.AllowedPrincipalTags{}, |
| 499 |
}, |
| 500 |
}, |
| 501 |
}, |
| 502 |
}, |
| 503 |
} |
| 504 |
|
| 505 |
for i, test := range tests { |
| 506 |
ah := attestHandler{ |
| 507 |
logger: logging.SimpleLogger(), |
| 508 |
clients: AttestClients{ |
| 509 |
GCA: &fakeVerifierClient{}, |
| 510 |
}, |
| 511 |
attestAgent: fakeAttestationAgent{ |
| 512 |
attestWithClientFunc: func(_ context.Context, gotOpts agent.AttestAgentOpts, _ verifier.Client) ([]byte, error) { |
| 513 |
diff := cmp.Diff(test.wantOpts, gotOpts) |
| 514 |
if diff != "" { |
| 515 |
t.Errorf("%v: got unexpected agent.AttestAgentOpts. diff:\n%v", test.testName, diff) |
| 516 |
} |
| 517 |
return []byte{}, nil |
| 518 |
}, |
| 519 |
}} |
| 520 |
|
| 521 |
b := strings.NewReader(test.body) |
| 522 |
|
| 523 |
req := httptest.NewRequest(http.MethodPost, "/v1/token", b) |
| 524 |
w := httptest.NewRecorder() |
| 525 |
ah.getToken(w, req) |
| 526 |
_, err := io.ReadAll(w.Result().Body) |
| 527 |
if err != nil { |
| 528 |
t.Error(err) |
| 529 |
} |
| 530 |
|
| 531 |
if w.Code != test.wantCode { |
| 532 |
t.Errorf("testcase %d, '%v': got return code: %d, want: %d", i, test.testName, w.Code, test.wantCode) |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
func TestCustomHandleAttestError(t *testing.T) { |
| 538 |
body := `{ |
| 539 |
"audience": "audience", |
| 540 |
"nonces": ["thisIsAcustomNonce"], |
| 541 |
"token_type": "OIDC" |
| 542 |
}` |
| 543 |
|
| 544 |
testcases := []struct { |
| 545 |
name string |
| 546 |
err error |
| 547 |
wantStatusCode int |
| 548 |
}{ |
| 549 |
{ |
| 550 |
name: "FailedPrecondition error", |
| 551 |
err: status.New(codes.FailedPrecondition, "bad state").Err(), |
| 552 |
wantStatusCode: http.StatusBadRequest, |
| 553 |
}, |
| 554 |
{ |
| 555 |
name: "PermissionDenied error", |
| 556 |
err: status.New(codes.PermissionDenied, "denied").Err(), |
| 557 |
wantStatusCode: http.StatusBadRequest, |
| 558 |
}, |
| 559 |
{ |
| 560 |
name: "Internal error", |
| 561 |
err: status.New(codes.Internal, "internal server error").Err(), |
| 562 |
wantStatusCode: http.StatusInternalServerError, |
| 563 |
}, |
| 564 |
{ |
| 565 |
name: "Unavailable error", |
| 566 |
err: status.New(codes.Unavailable, "service unavailable").Err(), |
| 567 |
wantStatusCode: http.StatusInternalServerError, |
| 568 |
}, |
| 569 |
{ |
| 570 |
name: "non-gRPC error", |
| 571 |
err: errors.New("a generic error"), |
| 572 |
wantStatusCode: http.StatusInternalServerError, |
| 573 |
}, |
| 574 |
} |
| 575 |
for _, tc := range testcases { |
| 576 |
t.Run(tc.name, func(t *testing.T) { |
| 577 |
ah := attestHandler{ |
| 578 |
logger: logging.SimpleLogger(), |
| 579 |
clients: AttestClients{ |
| 580 |
GCA: &fakeVerifierClient{}, |
| 581 |
}, |
| 582 |
attestAgent: fakeAttestationAgent{ |
| 583 |
attestWithClientFunc: func(context.Context, agent.AttestAgentOpts, verifier.Client) ([]byte, error) { |
| 584 |
return nil, tc.err |
| 585 |
}, |
| 586 |
}, |
| 587 |
} |
| 588 |
|
| 589 |
req := httptest.NewRequest(http.MethodPost, "/v1/token", strings.NewReader(body)) |
| 590 |
w := httptest.NewRecorder() |
| 591 |
|
| 592 |
ah.getToken(w, req) |
| 593 |
|
| 594 |
if w.Code != tc.wantStatusCode { |
| 595 |
t.Errorf("got status code %d, want %d", w.Code, tc.wantStatusCode) |
| 596 |
} |
| 597 |
|
| 598 |
_, err := io.ReadAll(w.Result().Body) |
| 599 |
if err != nil { |
| 600 |
t.Errorf("failed to read response body: %v", err) |
| 601 |
} |
| 602 |
}) |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
func TestAttestationEvidence(t *testing.T) { |
| 607 |
ah := attestHandler{ |
| 608 |
logger: logging.SimpleLogger(), |
| 609 |
attestAgent: fakeAttestationAgent{ |
| 610 |
attestationEvidenceFunc: func(_ context.Context, _ []byte, _ []byte) (*attestationpb.VmAttestation, error) { |
| 611 |
return &attestationpb.VmAttestation{}, nil |
| 612 |
}, |
| 613 |
}, |
| 614 |
} |
| 615 |
|
| 616 |
req := httptest.NewRequest(http.MethodPost, "/v1/evidence", strings.NewReader("{\"challenge\": \"dGVzdA==\"}")) |
| 617 |
w := httptest.NewRecorder() |
| 618 |
|
| 619 |
ah.getAttestationEvidence(w, req) |
| 620 |
|
| 621 |
if w.Code != http.StatusOK { |
| 622 |
t.Errorf("got return code: %d, want: %d", w.Code, http.StatusOK) |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
func TestGetKeyEndorsement(t *testing.T) { |
| 627 |
testHandle := "66eeec4b-0b20-4b30-987b-9c46b74ecc16" |
| 628 |
testChallenge := []byte("test-challenge") |
| 629 |
|
| 630 |
tests := []struct { |
| 631 |
name string |
| 632 |
reqBody interface{} |
| 633 |
enableKM bool |
| 634 |
claimsErr error |
| 635 |
attestErr error |
| 636 |
wantStatus int |
| 637 |
expectErrMsg string |
| 638 |
}{ |
| 639 |
{ |
| 640 |
name: "success", |
| 641 |
reqBody: map[string]interface{}{ |
| 642 |
"challenge": testChallenge, |
| 643 |
"key_handle": map[string]string{"handle": testHandle}, |
| 644 |
}, |
| 645 |
enableKM: true, |
| 646 |
wantStatus: http.StatusOK, |
| 647 |
}, |
| 648 |
{ |
| 649 |
name: "key manager disabled", |
| 650 |
reqBody: map[string]interface{}{ |
| 651 |
"challenge": testChallenge, |
| 652 |
"key_handle": map[string]string{"handle": testHandle}, |
| 653 |
}, |
| 654 |
enableKM: false, |
| 655 |
wantStatus: http.StatusForbidden, |
| 656 |
}, |
| 657 |
{ |
| 658 |
name: "missing key handle", |
| 659 |
reqBody: map[string]interface{}{ |
| 660 |
"challenge": testChallenge, |
| 661 |
"key_handle": map[string]string{"handle": ""}, |
| 662 |
}, |
| 663 |
enableKM: true, |
| 664 |
wantStatus: http.StatusBadRequest, |
| 665 |
}, |
| 666 |
{ |
| 667 |
name: "claims provider error", |
| 668 |
reqBody: map[string]interface{}{ |
| 669 |
"challenge": testChallenge, |
| 670 |
"key_handle": map[string]string{"handle": testHandle}, |
| 671 |
}, |
| 672 |
enableKM: true, |
| 673 |
claimsErr: fmt.Errorf("internal provider error"), |
| 674 |
wantStatus: http.StatusInternalServerError, |
| 675 |
}, |
| 676 |
{ |
| 677 |
name: "attestation agent error", |
| 678 |
reqBody: map[string]interface{}{ |
| 679 |
"challenge": testChallenge, |
| 680 |
"key_handle": map[string]string{"handle": testHandle}, |
| 681 |
}, |
| 682 |
enableKM: true, |
| 683 |
attestErr: fmt.Errorf("tpm failure"), |
| 684 |
wantStatus: http.StatusInternalServerError, |
| 685 |
}, |
| 686 |
} |
| 687 |
|
| 688 |
for _, tt := range tests { |
| 689 |
t.Run(tt.name, func(t *testing.T) { |
| 690 |
mClaims := &mockClaimsProvider{ |
| 691 |
err: tt.claimsErr, |
| 692 |
claims: map[keymanager.KeyType]*keymanager.KeyClaims{ |
| 693 |
keymanager.KeyType_KEY_TYPE_VM_PROTECTION_KEY: {Claims: &keymanager.KeyClaims_VmKeyClaims{}}, |
| 694 |
keymanager.KeyType_KEY_TYPE_VM_PROTECTION_BINDING: {Claims: &keymanager.KeyClaims_VmBindingClaims{}}, |
| 695 |
}, |
| 696 |
} |
| 697 |
mAgent := &fakeAttestationAgent{ |
| 698 |
attestationEvidenceFunc: func(_ context.Context, _, b2 []byte) (*attestationpb.VmAttestation, error) { |
| 699 |
if tt.attestErr != nil { |
| 700 |
return nil, tt.attestErr |
| 701 |
} |
| 702 |
return &attestationpb.VmAttestation{ExtraData: b2}, nil |
| 703 |
}, |
| 704 |
} |
| 705 |
handler := &attestHandler{ |
| 706 |
ctx: context.Background(), |
| 707 |
attestAgent: mAgent, |
| 708 |
keyClaimsProvider: mClaims, |
| 709 |
logger: logging.SimpleLogger(), |
| 710 |
launchSpec: spec.LaunchSpec{ |
| 711 |
Experiments: experiments.Experiments{EnableKeyManager: tt.enableKM}, |
| 712 |
}, |
| 713 |
} |
| 714 |
body, _ := json.Marshal(tt.reqBody) |
| 715 |
req := httptest.NewRequest(http.MethodPost, endorsementEndpoint, bytes.NewBuffer(body)) |
| 716 |
rr := httptest.NewRecorder() |
| 717 |
handler.getKeyEndorsement(rr, req) |
| 718 |
|
| 719 |
if rr.Code != tt.wantStatus { |
| 720 |
t.Errorf("%s: got status %v, want %v. Body: %s", tt.name, rr.Code, tt.wantStatus, rr.Body.String()) |
| 721 |
} |
| 722 |
if tt.wantStatus == http.StatusOK { |
| 723 |
var endorsement attestationpb.KeyEndorsement |
| 724 |
if err := protojson.Unmarshal(rr.Body.Bytes(), &endorsement); err != nil { |
| 725 |
t.Fatalf("failed to unmarshal response: %v", err) |
| 726 |
} |
| 727 |
vmEndorsement := endorsement.GetVmProtectedKeyEndorsement() |
| 728 |
if vmEndorsement == nil { |
| 729 |
t.Fatal("response missing VmProtectedKeyEndorsement") |
| 730 |
} |
| 731 |
if vmEndorsement.BindingKeyAttestation.Attestation == nil || |
| 732 |
vmEndorsement.ProtectedKeyAttestation.Attestation == nil { |
| 733 |
t.Error("one or both attestations are nil") |
| 734 |
} |
| 735 |
} |
| 736 |
}) |
| 737 |
} |
| 738 |
} |