use of com.google.cloud.kms.v1.KeyRingName in project java-kms by googleapis.
the class CreateKeyMac method createKeyMac.
// Create a new key for use with MacSign.
public void createKeyMac(String projectId, String locationId, String keyRingId, String id) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the parent name from the project, location, and key ring.
KeyRingName keyRingName = KeyRingName.of(projectId, locationId, keyRingId);
// Build the mac key to create.
CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.MAC).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setAlgorithm(CryptoKeyVersionAlgorithm.HMAC_SHA256)).build();
// Create the key.
CryptoKey createdKey = client.createCryptoKey(keyRingName, id, key);
System.out.printf("Created mac key %s%n", createdKey.getName());
}
}
use of com.google.cloud.kms.v1.KeyRingName in project java-storage by googleapis.
the class ITStorageTest method ensureKmsKeyRingExistsForTests.
private static String ensureKmsKeyRingExistsForTests(KeyManagementServiceBlockingStub kmsStub, String projectId, String location, String keyRingName) throws StatusRuntimeException {
String kmsKeyRingResourcePath = KeyRingName.of(projectId, location, keyRingName).toString();
try {
// Attempt to Get KeyRing
GetKeyRingRequest getKeyRingRequest = GetKeyRingRequest.newBuilder().setName(kmsKeyRingResourcePath).build();
requestParamsHeader.put(requestParamsKey, "name=" + kmsKeyRingResourcePath);
KeyManagementServiceBlockingStub stubForGetKeyRing = MetadataUtils.attachHeaders(kmsStub, requestParamsHeader);
stubForGetKeyRing.getKeyRing(getKeyRingRequest);
} catch (StatusRuntimeException ex) {
if (ex.getStatus().getCode() == Status.Code.NOT_FOUND) {
// Create KmsKeyRing
String keyRingParent = LocationName.of(projectId, location).toString();
CreateKeyRingRequest createKeyRingRequest = CreateKeyRingRequest.newBuilder().setParent(keyRingParent).setKeyRingId(keyRingName).build();
requestParamsHeader.put(requestParamsKey, "parent=" + keyRingParent);
KeyManagementServiceBlockingStub stubForCreateKeyRing = MetadataUtils.attachHeaders(kmsStub, requestParamsHeader);
stubForCreateKeyRing.createKeyRing(createKeyRingRequest);
} else {
throw ex;
}
}
return kmsKeyRingResourcePath;
}
use of com.google.cloud.kms.v1.KeyRingName in project gapic-generator-java by googleapis.
the class SyncGetKeyRingKeyringname method syncGetKeyRingKeyringname.
public static void syncGetKeyRingKeyringname() throws Exception {
// It may require modifications to work in your environment.
try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
KeyRingName name = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]");
KeyRing response = keyManagementServiceClient.getKeyRing(name);
}
}
use of com.google.cloud.kms.v1.KeyRingName in project gcp-ingestion by mozilla.
the class KeyStoreIntegrationTest method ensureKmsResources.
/**
* Creates key rings and crypto keys in KMS if they do not exist. These
* objects are immutable and cannot be deleted once they are created, so use
* this function with caution.
*/
private void ensureKmsResources(KeyManagementServiceClient client, String resourceId) {
CryptoKeyName name = CryptoKeyName.parse(resourceId);
assertEquals(projectId, name.getProject());
assertEquals("global", name.getLocation());
assertEquals(keyRingId, name.getKeyRing());
// getOrCreateKeyRing
KeyRingName keyRingName = KeyRingName.of(projectId, "global", name.getKeyRing());
try {
client.getKeyRing(keyRingName);
} catch (NotFoundException e) {
LocationName parent = LocationName.of(projectId, "global");
KeyRing request = KeyRing.newBuilder().build();
client.createKeyRing(parent, name.getKeyRing(), request);
}
// getOrCreateCryptoKey
CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, "global", name.getKeyRing(), name.getCryptoKey());
try {
client.getCryptoKey(cryptoKeyName);
} catch (NotFoundException e) {
CryptoKey request = CryptoKey.newBuilder().setPurpose(CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT).build();
client.createCryptoKey(keyRingName, name.getCryptoKey(), request);
}
}
Aggregations