|
16 | 16 |
|
17 | 17 | package com.google.cloud.kms.it; |
18 | 18 |
|
19 | | -import com.google.auth.oauth2.GoogleCredentials; |
20 | | -import com.google.cloud.ServiceOptions; |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | + |
21 | 21 | import com.google.cloud.kms.v1.CreateKeyRingRequest; |
22 | 22 | import com.google.cloud.kms.v1.GetKeyRingRequest; |
23 | | -import com.google.cloud.kms.v1.KeyManagementServiceGrpc; |
| 23 | +import com.google.cloud.kms.v1.KeyManagementServiceClient; |
24 | 24 | import com.google.cloud.kms.v1.KeyRing; |
25 | 25 | import com.google.cloud.kms.v1.KeyRingName; |
26 | 26 | import com.google.cloud.kms.v1.LocationName; |
27 | | -import io.grpc.ManagedChannel; |
28 | | -import io.grpc.ManagedChannelBuilder; |
29 | | -import io.grpc.Metadata; |
30 | | -import io.grpc.Status; |
31 | | -import io.grpc.StatusRuntimeException; |
32 | | -import io.grpc.auth.MoreCallCredentials; |
33 | | -import io.grpc.stub.MetadataUtils; |
34 | 27 | import java.io.IOException; |
35 | | -import org.junit.Assert; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import org.junit.After; |
36 | 30 | import org.junit.Before; |
37 | 31 | import org.junit.Test; |
38 | 32 |
|
39 | 33 | public class ITKmsTest { |
| 34 | + |
| 35 | + private KeyManagementServiceClient kmsClient; |
| 36 | + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
40 | 37 | private static final String KMS_KEY_RING_LOCATION = "us"; |
41 | | - private static final String KMS_KEY_RING_NAME = "gcs_test_kms_key_ring"; |
42 | | - private static Metadata requestParamsHeader = new Metadata(); |
43 | | - private static Metadata.Key<String> requestParamsKey = |
44 | | - Metadata.Key.of("x-goog-request-params", Metadata.ASCII_STRING_MARSHALLER); |
45 | | - private static KeyManagementServiceGrpc.KeyManagementServiceBlockingStub kmsStub; |
| 38 | + private static final String KMS_KEY_RING_ID = "gcs_test_kms_key_ring"; |
| 39 | + private static final String KMS_KEY_RING_NAME = |
| 40 | + KeyRingName.of(PROJECT_ID, KMS_KEY_RING_LOCATION, KMS_KEY_RING_ID).toString(); |
46 | 41 |
|
47 | 42 | @Before |
48 | 43 | public void setUp() throws IOException { |
49 | | - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); |
50 | | - ManagedChannel kmsChannel = |
51 | | - ManagedChannelBuilder.forTarget("cloudkms.googleapis.com:443").build(); |
52 | | - kmsStub = |
53 | | - KeyManagementServiceGrpc.newBlockingStub(kmsChannel) |
54 | | - .withCallCredentials(MoreCallCredentials.from(credentials)); |
| 44 | + kmsClient = KeyManagementServiceClient.create(); |
| 45 | + |
| 46 | + String parent = LocationName.of(PROJECT_ID, KMS_KEY_RING_LOCATION).toString(); |
| 47 | + KeyManagementServiceClient.ListKeyRingsPagedResponse listKeyRingsPagedResponse = |
| 48 | + kmsClient.listKeyRings(parent); |
| 49 | + boolean foundTestKeyRing = false; |
| 50 | + for (KeyRing keyRing : listKeyRingsPagedResponse.iterateAll()) { |
| 51 | + // keyRing.getName() returns the fully qualified name and not the ID |
| 52 | + if (KMS_KEY_RING_NAME.equals(keyRing.getName())) { |
| 53 | + foundTestKeyRing = true; |
| 54 | + } |
| 55 | + } |
| 56 | + // Only create the test key ring if it doesn't exist |
| 57 | + if (!foundTestKeyRing) { |
| 58 | + kmsClient.createKeyRing( |
| 59 | + CreateKeyRingRequest.newBuilder() |
| 60 | + .setParent(parent) |
| 61 | + .setKeyRingId(KMS_KEY_RING_ID) |
| 62 | + .setKeyRing(KeyRing.newBuilder().build()) |
| 63 | + .build()); |
| 64 | + } |
55 | 65 | } |
56 | 66 |
|
57 | | - @Test |
58 | | - public void ensureKmsKeyRingExists() { |
59 | | - String projectId = ServiceOptions.getDefaultProjectId(); |
60 | | - KeyRing keyRing = getKeyRing(kmsStub, projectId); |
61 | | - Assert.assertNotNull(keyRing); |
| 67 | + @After |
| 68 | + public void cleanUp() throws InterruptedException { |
| 69 | + kmsClient.close(); |
| 70 | + kmsClient.awaitTermination(10, TimeUnit.SECONDS); |
62 | 71 | } |
63 | 72 |
|
64 | | - private static KeyRing getKeyRing( |
65 | | - KeyManagementServiceGrpc.KeyManagementServiceBlockingStub kmsStub, String projectId) |
66 | | - throws StatusRuntimeException { |
67 | | - String kmsKeyRingResourcePath = |
68 | | - KeyRingName.of(projectId, ITKmsTest.KMS_KEY_RING_LOCATION, ITKmsTest.KMS_KEY_RING_NAME) |
69 | | - .toString(); |
70 | | - try { |
71 | | - GetKeyRingRequest getKeyRingRequest = |
72 | | - GetKeyRingRequest.newBuilder().setName(kmsKeyRingResourcePath).build(); |
73 | | - requestParamsHeader.put(requestParamsKey, "name=" + kmsKeyRingResourcePath); |
74 | | - KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForGetKeyRing = |
75 | | - kmsStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(requestParamsHeader)); |
76 | | - return stubForGetKeyRing.getKeyRing(getKeyRingRequest); |
77 | | - } catch (StatusRuntimeException ex) { |
78 | | - if (ex.getStatus().getCode() == Status.Code.NOT_FOUND) { |
79 | | - String keyRingParent = |
80 | | - LocationName.of(projectId, ITKmsTest.KMS_KEY_RING_LOCATION).toString(); |
81 | | - CreateKeyRingRequest createKeyRingRequest = |
82 | | - CreateKeyRingRequest.newBuilder() |
83 | | - .setParent(keyRingParent) |
84 | | - .setKeyRingId(ITKmsTest.KMS_KEY_RING_NAME) |
85 | | - .build(); |
86 | | - requestParamsHeader.put(requestParamsKey, "parent=" + keyRingParent); |
87 | | - KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForCreateKeyRing = |
88 | | - kmsStub.withInterceptors( |
89 | | - MetadataUtils.newAttachHeadersInterceptor(requestParamsHeader)); |
90 | | - return stubForCreateKeyRing.createKeyRing(createKeyRingRequest); |
91 | | - } else { |
92 | | - throw ex; |
93 | | - } |
94 | | - } |
| 73 | + @Test |
| 74 | + public void getKeyRing() { |
| 75 | + GetKeyRingRequest getKeyRingRequest = |
| 76 | + GetKeyRingRequest.newBuilder().setName(KMS_KEY_RING_NAME).build(); |
| 77 | + KeyRing keyRing = kmsClient.getKeyRing(getKeyRingRequest); |
| 78 | + assertEquals(KMS_KEY_RING_NAME, keyRing.getName()); |
95 | 79 | } |
96 | 80 | } |
0 commit comments