diff --git a/.kokoro/continuous/kms-it.cfg b/.kokoro/continuous/kms-it.cfg new file mode 100644 index 000000000000..5b1d08260c1b --- /dev/null +++ b/.kokoro/continuous/kms-it.cfg @@ -0,0 +1,27 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Configure the docker image for kokoro-trampoline. +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/java8" +} + +env_vars: { + key: "INTEGRATION_TEST_ARGS" + value: "google-cloud-clients/google-cloud-kms" +} + +env_vars: { + key: "JOB_TYPE" + value: "integration" +} + +env_vars: { + key: "GCLOUD_PROJECT" + value: "gcloud-devel" +} + +env_vars: { + key: "GOOGLE_APPLICATION_CREDENTIALS" + value: "keystore/73713_java_it_service_account" +} diff --git a/.kokoro/nightly/kms-it.cfg b/.kokoro/nightly/kms-it.cfg new file mode 100644 index 000000000000..5b1d08260c1b --- /dev/null +++ b/.kokoro/nightly/kms-it.cfg @@ -0,0 +1,27 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Configure the docker image for kokoro-trampoline. +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/java8" +} + +env_vars: { + key: "INTEGRATION_TEST_ARGS" + value: "google-cloud-clients/google-cloud-kms" +} + +env_vars: { + key: "JOB_TYPE" + value: "integration" +} + +env_vars: { + key: "GCLOUD_PROJECT" + value: "gcloud-devel" +} + +env_vars: { + key: "GOOGLE_APPLICATION_CREDENTIALS" + value: "keystore/73713_java_it_service_account" +} diff --git a/.kokoro/presubmit/kms-it.cfg b/.kokoro/presubmit/kms-it.cfg new file mode 100644 index 000000000000..5b1d08260c1b --- /dev/null +++ b/.kokoro/presubmit/kms-it.cfg @@ -0,0 +1,27 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Configure the docker image for kokoro-trampoline. +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/java8" +} + +env_vars: { + key: "INTEGRATION_TEST_ARGS" + value: "google-cloud-clients/google-cloud-kms" +} + +env_vars: { + key: "JOB_TYPE" + value: "integration" +} + +env_vars: { + key: "GCLOUD_PROJECT" + value: "gcloud-devel" +} + +env_vars: { + key: "GOOGLE_APPLICATION_CREDENTIALS" + value: "keystore/73713_java_it_service_account" +} diff --git a/google-cloud-clients/google-cloud-kms/src/test/java/com/google/cloud/kms/it/ITKmsTest.java b/google-cloud-clients/google-cloud-kms/src/test/java/com/google/cloud/kms/it/ITKmsTest.java new file mode 100644 index 000000000000..ad4b65a9ed88 --- /dev/null +++ b/google-cloud-clients/google-cloud-kms/src/test/java/com/google/cloud/kms/it/ITKmsTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.kms.it; + +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.ServiceOptions; +import com.google.cloud.kms.v1.*; +import io.grpc.*; +import io.grpc.auth.MoreCallCredentials; +import io.grpc.stub.MetadataUtils; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ITKmsTest { + private static final String KMS_KEY_RING_LOCATION = "us"; + private static final String KMS_KEY_RING_NAME = "gcs_test_kms_key_ring"; + private static Metadata requestParamsHeader = new Metadata(); + private static Metadata.Key requestParamsKey = + Metadata.Key.of("x-goog-request-params", Metadata.ASCII_STRING_MARSHALLER); + private static KeyManagementServiceGrpc.KeyManagementServiceBlockingStub kmsStub; + + @Before + public void setUp() throws IOException { + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); + ManagedChannel kmsChannel = + ManagedChannelBuilder.forTarget("cloudkms.googleapis.com:443").build(); + kmsStub = + KeyManagementServiceGrpc.newBlockingStub(kmsChannel) + .withCallCredentials(MoreCallCredentials.from(credentials)); + } + + @Test + public void ensureKmsKeyRingExists() { + String projectId = ServiceOptions.getDefaultProjectId(); + KeyRing keyRing = getKeyRing(kmsStub, projectId); + Assert.assertNotNull(keyRing); + } + + private static KeyRing getKeyRing( + KeyManagementServiceGrpc.KeyManagementServiceBlockingStub kmsStub, String projectId) + throws StatusRuntimeException { + String kmsKeyRingResourcePath = + KeyRingName.of(projectId, ITKmsTest.KMS_KEY_RING_LOCATION, ITKmsTest.KMS_KEY_RING_NAME) + .toString(); + try { + GetKeyRingRequest getKeyRingRequest = + GetKeyRingRequest.newBuilder().setName(kmsKeyRingResourcePath).build(); + requestParamsHeader.put(requestParamsKey, "name=" + kmsKeyRingResourcePath); + KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForGetKeyRing = + MetadataUtils.attachHeaders(kmsStub, requestParamsHeader); + return stubForGetKeyRing.getKeyRing(getKeyRingRequest); + } catch (StatusRuntimeException ex) { + if (ex.getStatus().getCode() == Status.Code.NOT_FOUND) { + String keyRingParent = + LocationName.of(projectId, ITKmsTest.KMS_KEY_RING_LOCATION).toString(); + CreateKeyRingRequest createKeyRingRequest = + CreateKeyRingRequest.newBuilder() + .setParent(keyRingParent) + .setKeyRingId(ITKmsTest.KMS_KEY_RING_NAME) + .build(); + requestParamsHeader.put(requestParamsKey, "parent=" + keyRingParent); + KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForCreateKeyRing = + MetadataUtils.attachHeaders(kmsStub, requestParamsHeader); + return stubForCreateKeyRing.createKeyRing(createKeyRingRequest); + } else { + Assert.fail("Error creating or looking up key"); + } + } + return null; + } +}