public static KeyRingName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "KeyRingName.parse: formattedString not in valid format"); return of(matchMap.get("project"), matchMap.get("location"), matchMap.get("key_ring")); }
@Test @SuppressWarnings("all") public void testIamPermissionsExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockIAMPolicy.addException(exception); try { KeyName resource = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); List<String> permissions = new ArrayList<>(); client.testIamPermissions(resource, permissions); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } } }
@Test @SuppressWarnings("all") public void getKeyRingExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockKeyManagementService.addException(exception); try { KeyRingName name = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); client.getKeyRing(name); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void listCryptoKeysExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockKeyManagementService.addException(exception); try { KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); client.listCryptoKeys(parent); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void getIamPolicyExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockIAMPolicy.addException(exception); try { KeyName resource = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); client.getIamPolicy(resource); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void testIamPermissionsTest() { TestIamPermissionsResponse expectedResponse = TestIamPermissionsResponse.newBuilder().build(); mockIAMPolicy.addResponse(expectedResponse); KeyName resource = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); List<String> permissions = new ArrayList<>(); TestIamPermissionsResponse actualResponse = client.testIamPermissions(resource, permissions); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests(); Assert.assertEquals(1, actualRequests.size()); TestIamPermissionsRequest actualRequest = (TestIamPermissionsRequest) actualRequests.get(0); Assert.assertEquals(Objects.toString(resource), Objects.toString(actualRequest.getResource())); Assert.assertEquals(permissions, actualRequest.getPermissionsList()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void getIamPolicyTest() { int version = 351608024; ByteString etag = ByteString.copyFromUtf8("21"); Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); mockIAMPolicy.addResponse(expectedResponse); KeyName resource = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); Policy actualResponse = client.getIamPolicy(resource); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests(); Assert.assertEquals(1, actualRequests.size()); GetIamPolicyRequest actualRequest = (GetIamPolicyRequest) actualRequests.get(0); Assert.assertEquals(Objects.toString(resource), Objects.toString(actualRequest.getResource())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void getKeyRingTest() { KeyRingName name2 = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); KeyRing expectedResponse = KeyRing.newBuilder().setName(name2.toString()).build(); mockKeyManagementService.addResponse(expectedResponse); KeyRingName name = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); KeyRing actualResponse = client.getKeyRing(name); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockKeyManagementService.getRequests(); Assert.assertEquals(1, actualRequests.size()); GetKeyRingRequest actualRequest = (GetKeyRingRequest) actualRequests.get(0); Assert.assertEquals(name, KeyRingName.parse(actualRequest.getName())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void setIamPolicyExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockIAMPolicy.addException(exception); try { KeyName resource = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); Policy policy = Policy.newBuilder().build(); client.setIamPolicy(resource, policy); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void setIamPolicyTest() { int version = 351608024; ByteString etag = ByteString.copyFromUtf8("21"); Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); mockIAMPolicy.addResponse(expectedResponse); KeyName resource = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); Policy policy = Policy.newBuilder().build(); Policy actualResponse = client.setIamPolicy(resource, policy); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests(); Assert.assertEquals(1, actualRequests.size()); SetIamPolicyRequest actualRequest = (SetIamPolicyRequest) actualRequests.get(0); Assert.assertEquals(Objects.toString(resource), Objects.toString(actualRequest.getResource())); Assert.assertEquals(policy, actualRequest.getPolicy()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void listCryptoKeysTest() { String nextPageToken = ""; int totalSize = 705419236; CryptoKey cryptoKeysElement = CryptoKey.newBuilder().build(); List<CryptoKey> cryptoKeys = Arrays.asList(cryptoKeysElement); ListCryptoKeysResponse expectedResponse = ListCryptoKeysResponse.newBuilder() .setNextPageToken(nextPageToken) .setTotalSize(totalSize) .addAllCryptoKeys(cryptoKeys) .build(); mockKeyManagementService.addResponse(expectedResponse); KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); ListCryptoKeysPagedResponse pagedListResponse = client.listCryptoKeys(parent); List<CryptoKey> resources = Lists.newArrayList(pagedListResponse.iterateAll()); Assert.assertEquals(1, resources.size()); Assert.assertEquals(expectedResponse.getCryptoKeysList().get(0), resources.get(0)); List<GeneratedMessageV3> actualRequests = mockKeyManagementService.getRequests(); Assert.assertEquals(1, actualRequests.size()); ListCryptoKeysRequest actualRequest = (ListCryptoKeysRequest) actualRequests.get(0); Assert.assertEquals(parent, KeyRingName.parse(actualRequest.getParent())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void createKeyRingTest() { KeyRingName name = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); KeyRing expectedResponse = KeyRing.newBuilder().setName(name.toString()).build(); mockKeyManagementService.addResponse(expectedResponse); LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); String keyRingId = "keyRingId-2056646742"; KeyRing keyRing = KeyRing.newBuilder().build(); KeyRing actualResponse = client.createKeyRing(parent, keyRingId, keyRing); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockKeyManagementService.getRequests(); Assert.assertEquals(1, actualRequests.size()); CreateKeyRingRequest actualRequest = (CreateKeyRingRequest) actualRequests.get(0); Assert.assertEquals(parent, LocationName.parse(actualRequest.getParent())); Assert.assertEquals(keyRingId, actualRequest.getKeyRingId()); Assert.assertEquals(keyRing, actualRequest.getKeyRing()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void createCryptoKeyExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockKeyManagementService.addException(exception); try { KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); String cryptoKeyId = "my-app-key"; CryptoKey.CryptoKeyPurpose purpose = CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT; long seconds = 2147483647L; Timestamp nextRotationTime = Timestamp.newBuilder().setSeconds(seconds).build(); long seconds2 = 604800L; Duration rotationPeriod = Duration.newBuilder().setSeconds(seconds2).build(); CryptoKey cryptoKey = CryptoKey.newBuilder() .setPurpose(purpose) .setNextRotationTime(nextRotationTime) .setRotationPeriod(rotationPeriod) .build(); client.createCryptoKey(parent, cryptoKeyId, cryptoKey); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
private static void ensureKmsKeyRingIamPermissionsForTests( IAMPolicyGrpc.IAMPolicyBlockingStub iamStub, String projectId, String location, String keyRingName) throws StatusRuntimeException { ServiceAccount serviceAccount = storage.getServiceAccount(projectId); String kmsKeyRingResourcePath = KeyRingName.of(projectId, location, keyRingName).toString(); Binding binding = Binding.newBuilder() .setRole("roles/cloudkms.cryptoKeyEncrypterDecrypter") .addMembers("serviceAccount:" + serviceAccount.getEmail()) .build(); com.google.iam.v1.Policy policy = com.google.iam.v1.Policy.newBuilder().addBindings(binding).build(); SetIamPolicyRequest setIamPolicyRequest = SetIamPolicyRequest.newBuilder() .setResource(kmsKeyRingResourcePath) .setPolicy(policy) .build(); requestParamsHeader.put(requestParamsKey, "parent=" + kmsKeyRingResourcePath); iamStub = MetadataUtils.attachHeaders(iamStub, requestParamsHeader); iamStub.setIamPolicy(setIamPolicyRequest); }
mockKeyManagementService.addResponse(expectedResponse); KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); String cryptoKeyId = "my-app-key"; CryptoKey.CryptoKeyPurpose purpose = CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT;
String keyRingName) throws StatusRuntimeException { String kmsKeyRingResourcePath = KeyRingName.of(projectId, location, keyRingName).toString(); try {
} catch (StatusRuntimeException ex) { if (ex.getStatus().getCode() == Status.Code.NOT_FOUND) { String kmsKeyRingResourcePath = KeyRingName.of(projectId, location, keyRingName).toString(); CryptoKey cryptoKey = CryptoKey.newBuilder().setPurpose(CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT).build();
public static KeyRingName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "KeyRingName.parse: formattedString not in valid format"); return of(matchMap.get("project"), matchMap.get("location"), matchMap.get("key_ring")); }