|
| 1 | +package com.ironcorelabs.cachedkey; |
| 2 | + |
| 3 | +import com.ironcorelabs.tenantsecurity.kms.v1.*; |
| 4 | +import com.ironcorelabs.tenantsecurity.kms.v1.exception.TenantSecurityException; |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.concurrent.ExecutionException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Demonstrates using CachedEncryptor and CachedDecryptor to encrypt/decrypt multiple records with a |
| 14 | + * single TSP call. |
| 15 | + */ |
| 16 | +public class CachedKeyExample { |
| 17 | + |
| 18 | + private static final String TSP_ADDR = "http://localhost:32804"; |
| 19 | + |
| 20 | + public static void main(String[] args) throws Exception { |
| 21 | + |
| 22 | + String API_KEY = System.getenv("API_KEY"); |
| 23 | + if (API_KEY == null) { |
| 24 | + System.out.println("Must set the API_KEY environment variable."); |
| 25 | + System.exit(1); |
| 26 | + } |
| 27 | + |
| 28 | + String tenantId = System.getenv("TENANT_ID"); |
| 29 | + if (tenantId == null) { |
| 30 | + tenantId = "tenant-gcp-l"; |
| 31 | + } |
| 32 | + System.out.println("Using tenant " + tenantId); |
| 33 | + |
| 34 | + TenantSecurityClient client = |
| 35 | + new TenantSecurityClient.Builder(TSP_ADDR, API_KEY).allowInsecureHttp(true).build(); |
| 36 | + |
| 37 | + DocumentMetadata metadata = new DocumentMetadata(tenantId, "serviceOrUserId", "PII"); |
| 38 | + |
| 39 | + // Simulate a database table: each row has an encrypted record and its EDEK |
| 40 | + List<Map<String, byte[]>> encryptedRows = new ArrayList<>(); |
| 41 | + String sharedEdek; |
| 42 | + |
| 43 | + // Encrypt: one TSP call, then N local encrypts |
| 44 | + // |
| 45 | + // In a real application this block would be inside a database transaction. The |
| 46 | + // createCachedEncryptor call is the only network round trip — every encrypt() after |
| 47 | + // that is purely local CPU work, so it won't add latency or failure modes to the |
| 48 | + // transaction. |
| 49 | + try (CachedEncryptor encryptor = client.createCachedEncryptor(metadata).get()) { |
| 50 | + String[][] customers = |
| 51 | + {{"000-12-2345", "2825-519 Stone Creek Rd, Bozeman, MT 59715", "Jim Bridger"}, |
| 52 | + {"000-45-6789", "100 Main St, Helena, MT 59601", "John Colter"}, |
| 53 | + {"000-98-7654", "742 Evergreen Terrace, Missoula, MT 59801", "Sacagawea"},}; |
| 54 | + |
| 55 | + for (String[] customer : customers) { |
| 56 | + Map<String, byte[]> record = new HashMap<>(); |
| 57 | + record.put("ssn", customer[0].getBytes(StandardCharsets.UTF_8)); |
| 58 | + record.put("address", customer[1].getBytes(StandardCharsets.UTF_8)); |
| 59 | + record.put("name", customer[2].getBytes(StandardCharsets.UTF_8)); |
| 60 | + |
| 61 | + // This encrypt is local — no TSP call |
| 62 | + EncryptedDocument encrypted = encryptor.encrypt(record, metadata).get(); |
| 63 | + encryptedRows.add(encrypted.getEncryptedFields()); |
| 64 | + } |
| 65 | + |
| 66 | + // All rows share this EDEK; store it alongside the rows (or once per batch) |
| 67 | + sharedEdek = encryptor.getEdek(); |
| 68 | + |
| 69 | + System.out |
| 70 | + .println("Encrypted " + encryptor.getOperationCount() + " records with one TSP call"); |
| 71 | + } |
| 72 | + // leaving the `try` block zeroes the DEK and reports usage to the TSP |
| 73 | + |
| 74 | + // Decrypt: one TSP call, then N local decrypts |
| 75 | + try (CachedDecryptor decryptor = client.createCachedDecryptor(sharedEdek, metadata).get()) { |
| 76 | + for (Map<String, byte[]> row : encryptedRows) { |
| 77 | + EncryptedDocument doc = new EncryptedDocument(row, sharedEdek); |
| 78 | + |
| 79 | + // This decrypt is local — no TSP call |
| 80 | + PlaintextDocument plaintext = decryptor.decrypt(doc, metadata).get(); |
| 81 | + Map<String, byte[]> fields = plaintext.getDecryptedFields(); |
| 82 | + |
| 83 | + System.out.println("Decrypted: " + new String(fields.get("name"), StandardCharsets.UTF_8) |
| 84 | + + " / " + new String(fields.get("ssn"), StandardCharsets.UTF_8)); |
| 85 | + } |
| 86 | + |
| 87 | + System.out |
| 88 | + .println("Decrypted " + decryptor.getOperationCount() + " records with one TSP call"); |
| 89 | + } |
| 90 | + |
| 91 | + System.exit(0); |
| 92 | + } |
| 93 | +} |
0 commit comments