|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.kms.it; |
| 18 | + |
| 19 | +import com.google.auth.oauth2.GoogleCredentials; |
| 20 | +import com.google.cloud.ServiceOptions; |
| 21 | +import com.google.cloud.kms.v1.*; |
| 22 | +import io.grpc.*; |
| 23 | +import io.grpc.auth.MoreCallCredentials; |
| 24 | +import io.grpc.stub.MetadataUtils; |
| 25 | +import java.io.IOException; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +public class ITKmsTest { |
| 31 | + private static final String KMS_KEY_RING_LOCATION = "us"; |
| 32 | + private static final String KMS_KEY_RING_NAME = "gcs_test_kms_key_ring"; |
| 33 | + private static Metadata requestParamsHeader = new Metadata(); |
| 34 | + private static Metadata.Key<String> requestParamsKey = |
| 35 | + Metadata.Key.of("x-goog-request-params", Metadata.ASCII_STRING_MARSHALLER); |
| 36 | + private static KeyManagementServiceGrpc.KeyManagementServiceBlockingStub kmsStub; |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setUp() throws IOException { |
| 40 | + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); |
| 41 | + ManagedChannel kmsChannel = |
| 42 | + ManagedChannelBuilder.forTarget("cloudkms.googleapis.com:443").build(); |
| 43 | + kmsStub = |
| 44 | + KeyManagementServiceGrpc.newBlockingStub(kmsChannel) |
| 45 | + .withCallCredentials(MoreCallCredentials.from(credentials)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void ensureKmsKeyRingExists() { |
| 50 | + String projectId = ServiceOptions.getDefaultProjectId(); |
| 51 | + KeyRing keyRing = getKeyRing(kmsStub, projectId); |
| 52 | + Assert.assertNotNull(keyRing); |
| 53 | + } |
| 54 | + |
| 55 | + private static KeyRing getKeyRing( |
| 56 | + KeyManagementServiceGrpc.KeyManagementServiceBlockingStub kmsStub, String projectId) |
| 57 | + throws StatusRuntimeException { |
| 58 | + String kmsKeyRingResourcePath = |
| 59 | + KeyRingName.of(projectId, ITKmsTest.KMS_KEY_RING_LOCATION, ITKmsTest.KMS_KEY_RING_NAME) |
| 60 | + .toString(); |
| 61 | + try { |
| 62 | + GetKeyRingRequest getKeyRingRequest = |
| 63 | + GetKeyRingRequest.newBuilder().setName(kmsKeyRingResourcePath).build(); |
| 64 | + requestParamsHeader.put(requestParamsKey, "name=" + kmsKeyRingResourcePath); |
| 65 | + KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForGetKeyRing = |
| 66 | + MetadataUtils.attachHeaders(kmsStub, requestParamsHeader); |
| 67 | + return stubForGetKeyRing.getKeyRing(getKeyRingRequest); |
| 68 | + } catch (StatusRuntimeException ex) { |
| 69 | + if (ex.getStatus().getCode() == Status.Code.NOT_FOUND) { |
| 70 | + String keyRingParent = |
| 71 | + LocationName.of(projectId, ITKmsTest.KMS_KEY_RING_LOCATION).toString(); |
| 72 | + CreateKeyRingRequest createKeyRingRequest = |
| 73 | + CreateKeyRingRequest.newBuilder() |
| 74 | + .setParent(keyRingParent) |
| 75 | + .setKeyRingId(ITKmsTest.KMS_KEY_RING_NAME) |
| 76 | + .build(); |
| 77 | + requestParamsHeader.put(requestParamsKey, "parent=" + keyRingParent); |
| 78 | + KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForCreateKeyRing = |
| 79 | + MetadataUtils.attachHeaders(kmsStub, requestParamsHeader); |
| 80 | + return stubForCreateKeyRing.createKeyRing(createKeyRingRequest); |
| 81 | + } else { |
| 82 | + Assert.fail("Error creating or looking up key"); |
| 83 | + } |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | +} |
0 commit comments