Search in sources :

Example 26 with KeyRingName

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());
    }
}
Also used : CryptoKey(com.google.cloud.kms.v1.CryptoKey) KeyRingName(com.google.cloud.kms.v1.KeyRingName) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 27 with KeyRingName

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;
}
Also used : GetKeyRingRequest(com.google.cloud.kms.v1.GetKeyRingRequest) CreateKeyRingRequest(com.google.cloud.kms.v1.CreateKeyRingRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) KeyManagementServiceBlockingStub(com.google.cloud.kms.v1.KeyManagementServiceGrpc.KeyManagementServiceBlockingStub)

Example 28 with KeyRingName

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);
    }
}
Also used : KeyRing(com.google.cloud.kms.v1.KeyRing) KeyRingName(com.google.cloud.kms.v1.KeyRingName) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 29 with KeyRingName

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);
    }
}
Also used : KeyRing(com.google.cloud.kms.v1.KeyRing) CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) CryptoKey(com.google.cloud.kms.v1.CryptoKey) NotFoundException(com.google.api.gax.rpc.NotFoundException) KeyRingName(com.google.cloud.kms.v1.KeyRingName) LocationName(com.google.cloud.kms.v1.LocationName)

Aggregations

KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)22 KeyRingName (com.google.cloud.kms.v1.KeyRingName)17 CryptoKey (com.google.cloud.kms.v1.CryptoKey)16 CryptoKeyName (com.google.cloud.kms.v1.CryptoKeyName)7 Binding (com.google.iam.v1.Binding)6 Policy (com.google.iam.v1.Policy)6 AbstractMessage (com.google.protobuf.AbstractMessage)4 Test (org.junit.Test)4 ListCryptoKeysPagedResponse (com.google.cloud.kms.v1.KeyManagementServiceClient.ListCryptoKeysPagedResponse)2 ListImportJobsPagedResponse (com.google.cloud.kms.v1.KeyManagementServiceClient.ListImportJobsPagedResponse)2 KeyManagementServiceBlockingStub (com.google.cloud.kms.v1.KeyManagementServiceGrpc.KeyManagementServiceBlockingStub)2 KeyRing (com.google.cloud.kms.v1.KeyRing)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 NotFoundException (com.google.api.gax.rpc.NotFoundException)1 CreateCryptoKeyRequest (com.google.cloud.kms.v1.CreateCryptoKeyRequest)1 CreateKeyRingRequest (com.google.cloud.kms.v1.CreateKeyRingRequest)1 GetCryptoKeyRequest (com.google.cloud.kms.v1.GetCryptoKeyRequest)1 GetKeyRingRequest (com.google.cloud.kms.v1.GetKeyRingRequest)1 ImportJob (com.google.cloud.kms.v1.ImportJob)1 KeyManagementServiceGrpc (com.google.cloud.kms.v1.KeyManagementServiceGrpc)1