| 1 |
//go:build integration |
| 2 |
|
| 3 |
package workloadservice |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"encoding/json" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"path/filepath" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/google/uuid" |
| 15 |
|
| 16 |
kps "github.com/google/go-tpm-tools/keymanager/key_protection_service" |
| 17 |
keymanager "github.com/google/go-tpm-tools/keymanager/km_common/proto" |
| 18 |
wskcc "github.com/google/go-tpm-tools/keymanager/workload_service/key_custody_core" |
| 19 |
) |
| 20 |
|
| 21 |
// realWorkloadService wraps the actual WSD KCC FFI. |
| 22 |
type realWorkloadService struct{} |
| 23 |
|
| 24 |
func (r *realWorkloadService) GenerateBindingKeypair(algo *keymanager.HpkeAlgorithm, lifespanSecs uint64) (uuid.UUID, []byte, error) { |
| 25 |
return wskcc.GenerateBindingKeypair(algo, lifespanSecs) |
| 26 |
} |
| 27 |
|
| 28 |
func (r *realWorkloadService) DestroyBindingKey(bindingUUID uuid.UUID) error { |
| 29 |
return wskcc.DestroyBindingKey(bindingUUID) |
| 30 |
} |
| 31 |
|
| 32 |
func (r *realWorkloadService) Open(bindingUUID uuid.UUID, enc, ciphertext, aad []byte) ([]byte, error) { |
| 33 |
return wskcc.Open(bindingUUID, enc, ciphertext, aad) |
| 34 |
} |
| 35 |
|
| 36 |
func (r *realWorkloadService) GetBindingKey(id uuid.UUID) ([]byte, *keymanager.HpkeAlgorithm, error) { |
| 37 |
return wskcc.GetBindingKey(id) |
| 38 |
} |
| 39 |
|
| 40 |
func TestIntegrationGenerateKeysEndToEnd(t *testing.T) { |
| 41 |
// Wire up real FFI calls: WSD KCC for binding, KPS KCC (via KPS KOL) for KEM. |
| 42 |
kpsSvc := kps.NewService() |
| 43 |
srv, err := NewServer(kpsSvc, &realWorkloadService{}, "test.sock") |
| 44 |
if err != nil { |
| 45 |
t.Fatalf("failed to create server: %v", err) |
| 46 |
} |
| 47 |
|
| 48 |
reqBody, err := json.Marshal(GenerateKeyRequest{ |
| 49 |
Algorithm: AlgorithmDetails{Type: "kem", Params: AlgorithmParams{KemID: KemAlgorithmDHKEMX25519HKDFSHA256}}, |
| 50 |
Lifespan: 3600, |
| 51 |
}) |
| 52 |
if err != nil { |
| 53 |
t.Fatalf("failed to marshal request: %v", err) |
| 54 |
} |
| 55 |
req := httptest.NewRequest(http.MethodPost, "/v1/keys:generate_key", bytes.NewReader(reqBody)) |
| 56 |
req.Header.Set("Content-Type", "application/json") |
| 57 |
w := httptest.NewRecorder() |
| 58 |
srv.Handler().ServeHTTP(w, req) |
| 59 |
|
| 60 |
if w.Code != http.StatusOK { |
| 61 |
t.Fatalf("expected status 200, got %d: %s", w.Code, w.Body.String()) |
| 62 |
} |
| 63 |
|
| 64 |
var resp GenerateKeyResponse |
| 65 |
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil { |
| 66 |
t.Fatalf("failed to decode response: %v", err) |
| 67 |
} |
| 68 |
|
| 69 |
kemUUID, err := uuid.Parse(resp.KeyHandle.Handle) |
| 70 |
if err != nil { |
| 71 |
t.Fatalf("invalid UUID in response: %v", err) |
| 72 |
} |
| 73 |
if kemUUID == uuid.Nil { |
| 74 |
t.Fatal("expected non-nil KEM UUID") |
| 75 |
} |
| 76 |
|
| 77 |
// Verify the KEM → Binding mapping was stored. |
| 78 |
bindingUUID, ok := srv.LookupBindingUUID(kemUUID) |
| 79 |
if !ok { |
| 80 |
t.Fatal("expected KEM UUID to have a mapping to binding UUID") |
| 81 |
} |
| 82 |
if bindingUUID == uuid.Nil { |
| 83 |
t.Fatal("expected non-nil binding UUID in map") |
| 84 |
} |
| 85 |
|
| 86 |
t.Logf("E2E: KEM key handle=%s, mapped binding handle=%s", kemUUID, bindingUUID) |
| 87 |
} |
| 88 |
|
| 89 |
func TestIntegrationGenerateKeysUniqueMappings(t *testing.T) { |
| 90 |
kpsSvc := kps.NewService() |
| 91 |
srv, err := NewServer(kpsSvc, &realWorkloadService{}, "test.sock") |
| 92 |
if err != nil { |
| 93 |
t.Fatalf("failed to create server: %v", err) |
| 94 |
} |
| 95 |
|
| 96 |
// Generate two key sets. |
| 97 |
var kemUUIDs [2]uuid.UUID |
| 98 |
for i := 0; i < 2; i++ { |
| 99 |
reqBody, err := json.Marshal(GenerateKeyRequest{ |
| 100 |
Algorithm: AlgorithmDetails{Type: "kem", Params: AlgorithmParams{KemID: KemAlgorithmDHKEMX25519HKDFSHA256}}, |
| 101 |
Lifespan: 3600, |
| 102 |
}) |
| 103 |
if err != nil { |
| 104 |
t.Fatalf("call %d: failed to marshal request: %v", i+1, err) |
| 105 |
} |
| 106 |
req := httptest.NewRequest(http.MethodPost, "/v1/keys:generate_key", bytes.NewReader(reqBody)) |
| 107 |
req.Header.Set("Content-Type", "application/json") |
| 108 |
w := httptest.NewRecorder() |
| 109 |
srv.Handler().ServeHTTP(w, req) |
| 110 |
|
| 111 |
if w.Code != http.StatusOK { |
| 112 |
t.Fatalf("call %d: expected status 200, got %d: %s", i+1, w.Code, w.Body.String()) |
| 113 |
} |
| 114 |
|
| 115 |
var resp GenerateKeyResponse |
| 116 |
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil { |
| 117 |
t.Fatalf("call %d: failed to decode response: %v", i+1, err) |
| 118 |
} |
| 119 |
|
| 120 |
id, err := uuid.Parse(resp.KeyHandle.Handle) |
| 121 |
if err != nil { |
| 122 |
t.Fatalf("call %d: invalid UUID: %v", i+1, err) |
| 123 |
} |
| 124 |
kemUUIDs[i] = id |
| 125 |
} |
| 126 |
|
| 127 |
if kemUUIDs[0] == kemUUIDs[1] { |
| 128 |
t.Fatalf("expected unique KEM UUIDs, got same: %s", kemUUIDs[0]) |
| 129 |
} |
| 130 |
|
| 131 |
// Verify mappings are unique. |
| 132 |
binding1, _ := srv.LookupBindingUUID(kemUUIDs[0]) |
| 133 |
binding2, _ := srv.LookupBindingUUID(kemUUIDs[1]) |
| 134 |
if binding1 == binding2 { |
| 135 |
t.Fatalf("expected unique binding UUIDs, got same: %s", binding1) |
| 136 |
} |
| 137 |
|
| 138 |
t.Logf("E2E uniqueness: KEM1=%s→Binding1=%s, KEM2=%s→Binding2=%s", |
| 139 |
kemUUIDs[0], binding1, kemUUIDs[1], binding2) |
| 140 |
} |
| 141 |
|
| 142 |
func TestIntegrationDestroyKey(t *testing.T) { |
| 143 |
kpsSvc := kps.NewService() |
| 144 |
srv, err := NewServer(kpsSvc, &realWorkloadService{}, "") |
| 145 |
if err != nil { |
| 146 |
t.Fatalf("failed to create server: %v", err) |
| 147 |
} |
| 148 |
|
| 149 |
// 1. Generate a key first |
| 150 |
reqBody, _ := json.Marshal(GenerateKeyRequest{ |
| 151 |
Algorithm: AlgorithmDetails{Type: "kem", Params: AlgorithmParams{KemID: KemAlgorithmDHKEMX25519HKDFSHA256}}, |
| 152 |
Lifespan: 3600, |
| 153 |
}) |
| 154 |
reqGen := httptest.NewRequest(http.MethodPost, "/v1/keys:generate_key", bytes.NewReader(reqBody)) |
| 155 |
reqGen.Header.Set("Content-Type", "application/json") |
| 156 |
wGen := httptest.NewRecorder() |
| 157 |
srv.Handler().ServeHTTP(wGen, reqGen) |
| 158 |
|
| 159 |
if wGen.Code != http.StatusOK { |
| 160 |
t.Fatalf("setup: expected generate status 200, got %d: %s", wGen.Code, wGen.Body.String()) |
| 161 |
} |
| 162 |
|
| 163 |
var respGen GenerateKeyResponse |
| 164 |
if err := json.NewDecoder(wGen.Body).Decode(&respGen); err != nil { |
| 165 |
t.Fatalf("setup: failed to decode generate response: %v", err) |
| 166 |
} |
| 167 |
kemHandle := respGen.KeyHandle.Handle |
| 168 |
kemUUID, err := uuid.Parse(kemHandle) |
| 169 |
if err != nil { |
| 170 |
t.Fatalf("setup: invalid KEM UUID: %v", err) |
| 171 |
} |
| 172 |
|
| 173 |
// Verify mapping exists |
| 174 |
_, ok := srv.LookupBindingUUID(kemUUID) |
| 175 |
if !ok { |
| 176 |
t.Fatal("setup: expected mapping to exist") |
| 177 |
} |
| 178 |
|
| 179 |
// 2. Destroy the key |
| 180 |
reqDestroyBody, _ := json.Marshal(DestroyRequest{ |
| 181 |
KeyHandle: KeyHandle{Handle: kemHandle}, |
| 182 |
}) |
| 183 |
reqDestroy := httptest.NewRequest(http.MethodPost, "/v1/keys:destroy", bytes.NewReader(reqDestroyBody)) |
| 184 |
reqDestroy.Header.Set("Content-Type", "application/json") |
| 185 |
wDestroy := httptest.NewRecorder() |
| 186 |
srv.Handler().ServeHTTP(wDestroy, reqDestroy) |
| 187 |
|
| 188 |
if wDestroy.Code != http.StatusNoContent { |
| 189 |
t.Fatalf("expected destroy status 204, got %d: %s", wDestroy.Code, wDestroy.Body.String()) |
| 190 |
} |
| 191 |
|
| 192 |
// 3. Verify mapping is gone |
| 193 |
_, ok = srv.LookupBindingUUID(kemUUID) |
| 194 |
if ok { |
| 195 |
t.Fatal("expected KEM UUID mapping to be removed after destroy") |
| 196 |
} |
| 197 |
|
| 198 |
// 4. Try to destroy again (should fail? or be 404? or 500?) |
| 199 |
// If mapping is gone, it returns 404. |
| 200 |
reqDestroy2 := httptest.NewRequest(http.MethodPost, "/v1/keys:destroy", bytes.NewReader(reqDestroyBody)) |
| 201 |
reqDestroy2.Header.Set("Content-Type", "application/json") |
| 202 |
wDestroy2 := httptest.NewRecorder() |
| 203 |
srv.Handler().ServeHTTP(wDestroy2, reqDestroy2) |
| 204 |
|
| 205 |
if wDestroy2.Code != http.StatusNotFound { |
| 206 |
t.Fatalf("expected second destroy to return 404, got %d", wDestroy2.Code) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func TestIntegrationAutoDestroy(t *testing.T) { |
| 211 |
kpsSvc := kps.NewService() |
| 212 |
srv, err := NewServer(kpsSvc, &realWorkloadService{}, "test.sock") |
| 213 |
if err != nil { |
| 214 |
t.Fatalf("failed to create server: %v", err) |
| 215 |
} |
| 216 |
|
| 217 |
// 1. Generate a key with 1-second lifespan |
| 218 |
reqBody, _ := json.Marshal(GenerateKeyRequest{ |
| 219 |
Algorithm: AlgorithmDetails{Type: "kem", Params: AlgorithmParams{KemID: KemAlgorithmDHKEMX25519HKDFSHA256}}, |
| 220 |
Lifespan: 1, |
| 221 |
}) |
| 222 |
reqGen := httptest.NewRequest(http.MethodPost, "/v1/keys:generate_key", bytes.NewReader(reqBody)) |
| 223 |
reqGen.Header.Set("Content-Type", "application/json") |
| 224 |
wGen := httptest.NewRecorder() |
| 225 |
srv.Handler().ServeHTTP(wGen, reqGen) |
| 226 |
|
| 227 |
if wGen.Code != http.StatusOK { |
| 228 |
t.Fatalf("setup: expected generate status 200, got %d: %s", wGen.Code, wGen.Body.String()) |
| 229 |
} |
| 230 |
|
| 231 |
var respGen GenerateKeyResponse |
| 232 |
json.NewDecoder(wGen.Body).Decode(&respGen) |
| 233 |
kemHandle := respGen.KeyHandle.Handle |
| 234 |
|
| 235 |
// Wait for auto-destroy |
| 236 |
time.Sleep(2 * time.Second) |
| 237 |
|
| 238 |
// 2. Destroy the explicitly auto-destroyed key |
| 239 |
reqDestroyBody, _ := json.Marshal(DestroyRequest{ |
| 240 |
KeyHandle: KeyHandle{Handle: kemHandle}, |
| 241 |
}) |
| 242 |
reqDestroy := httptest.NewRequest(http.MethodPost, "/v1/keys:destroy", bytes.NewReader(reqDestroyBody)) |
| 243 |
reqDestroy.Header.Set("Content-Type", "application/json") |
| 244 |
wDestroy := httptest.NewRecorder() |
| 245 |
srv.Handler().ServeHTTP(wDestroy, reqDestroy) |
| 246 |
|
| 247 |
// In the real system, it gracefully handles destruction and cleans up the KOL mapping. |
| 248 |
if wDestroy.Code != http.StatusNoContent { |
| 249 |
t.Fatalf("expected destroy status 204 or some success, got %d: %s", wDestroy.Code, wDestroy.Body.String()) |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
func TestIntegrationKeyClaims(t *testing.T) { |
| 254 |
kpsSvc := kps.NewService() |
| 255 |
srv, err := NewServer(kpsSvc, &realWorkloadService{}, filepath.Join(t.TempDir(), "test.sock")) |
| 256 |
if err != nil { |
| 257 |
t.Fatalf("failed to create server: %v", err) |
| 258 |
} |
| 259 |
t.Cleanup(func() { |
| 260 |
srv.listener.Close() |
| 261 |
close(srv.claimsChan) |
| 262 |
}) |
| 263 |
|
| 264 |
// 1. Generate a KEM key |
| 265 |
reqBody, _ := json.Marshal(GenerateKeyRequest{ |
| 266 |
Algorithm: AlgorithmDetails{Type: "kem", Params: AlgorithmParams{KemID: KemAlgorithmDHKEMX25519HKDFSHA256}}, |
| 267 |
Lifespan: 3600, |
| 268 |
}) |
| 269 |
req := httptest.NewRequest(http.MethodPost, "/v1/keys:generate_key", bytes.NewReader(reqBody)) |
| 270 |
w := httptest.NewRecorder() |
| 271 |
srv.Handler().ServeHTTP(w, req) |
| 272 |
|
| 273 |
if w.Code != http.StatusOK { |
| 274 |
t.Fatalf("failed to generate KEM key: %s", w.Body.String()) |
| 275 |
} |
| 276 |
|
| 277 |
var resp GenerateKeyResponse |
| 278 |
json.NewDecoder(w.Body).Decode(&resp) |
| 279 |
kemHandle := resp.KeyHandle.Handle |
| 280 |
|
| 281 |
// 2. Test GetKeyClaims for KEM key |
| 282 |
t.Run("KemClaimsSuccess", func(t *testing.T) { |
| 283 |
respChan := make(chan *ClaimsResult) |
| 284 |
req := &keymanager.GetKeyClaimsRequest{ |
| 285 |
KeyHandle: &keymanager.KeyHandle{Handle: kemHandle}, |
| 286 |
KeyType: keymanager.KeyType_KEY_TYPE_VM_PROTECTION_KEY, |
| 287 |
} |
| 288 |
srv.claimsChan <- &ClaimsCall{Request: req, RespChan: respChan} |
| 289 |
|
| 290 |
res := <-respChan |
| 291 |
if res.Err != nil { |
| 292 |
t.Fatalf("unexpected error for KEM claims: %v", res.Err) |
| 293 |
} |
| 294 |
if res.Reply.GetVmKeyClaims() == nil { |
| 295 |
t.Fatal("expected VmKeyClaims") |
| 296 |
} |
| 297 |
}) |
| 298 |
|
| 299 |
// 3. Test GetKeyClaims for Binding key |
| 300 |
t.Run("BindingClaimsSuccess", func(t *testing.T) { |
| 301 |
|
| 302 |
respChan := make(chan *ClaimsResult) |
| 303 |
req := &keymanager.GetKeyClaimsRequest{ |
| 304 |
KeyHandle: &keymanager.KeyHandle{Handle: kemHandle}, |
| 305 |
KeyType: keymanager.KeyType_KEY_TYPE_VM_PROTECTION_BINDING, |
| 306 |
} |
| 307 |
srv.claimsChan <- &ClaimsCall{Request: req, RespChan: respChan} |
| 308 |
|
| 309 |
res := <-respChan |
| 310 |
if res.Err != nil { |
| 311 |
t.Fatalf("unexpected error for binding claims: %v", res.Err) |
| 312 |
} |
| 313 |
if res.Reply.GetVmBindingClaims() == nil { |
| 314 |
t.Fatal("expected VmBindingClaims") |
| 315 |
} |
| 316 |
}) |
| 317 |
|
| 318 |
// 4. Test non-happy cases |
| 319 |
t.Run("NonExistentKey", func(t *testing.T) { |
| 320 |
respChan := make(chan *ClaimsResult) |
| 321 |
req := &keymanager.GetKeyClaimsRequest{ |
| 322 |
KeyHandle: &keymanager.KeyHandle{Handle: uuid.New().String()}, |
| 323 |
KeyType: keymanager.KeyType_KEY_TYPE_VM_PROTECTION_KEY, |
| 324 |
} |
| 325 |
srv.claimsChan <- &ClaimsCall{Request: req, RespChan: respChan} |
| 326 |
|
| 327 |
res := <-respChan |
| 328 |
if res.Err == nil { |
| 329 |
t.Fatal("expected error for non-existent key") |
| 330 |
} |
| 331 |
}) |
| 332 |
|
| 333 |
t.Run("UnsupportedKeyType", func(t *testing.T) { |
| 334 |
respChan := make(chan *ClaimsResult) |
| 335 |
req := &keymanager.GetKeyClaimsRequest{ |
| 336 |
KeyHandle: &keymanager.KeyHandle{Handle: kemHandle}, |
| 337 |
KeyType: keymanager.KeyType_KEY_TYPE_UNSPECIFIED, |
| 338 |
} |
| 339 |
srv.claimsChan <- &ClaimsCall{Request: req, RespChan: respChan} |
| 340 |
|
| 341 |
res := <-respChan |
| 342 |
if res.Err == nil { |
| 343 |
t.Fatal("expected error for unsupported key type") |
| 344 |
} |
| 345 |
}) |
| 346 |
} |