use of com.google.cloud.kms.v1.CryptoKeyVersionName in project java-kms by googleapis.
the class EnableKeyVersion method enableKeyVersion.
// Enable a disabled key version to be used again.
public void enableKeyVersion(String projectId, String locationId, String keyRingId, String keyId, String keyVersionId) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the key version name from the project, location, key ring, key,
// and key version.
CryptoKeyVersionName keyVersionName = CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);
// Build the updated key version, setting it to enabled.
CryptoKeyVersion keyVersion = CryptoKeyVersion.newBuilder().setName(keyVersionName.toString()).setState(CryptoKeyVersionState.ENABLED).build();
// Create a field mask of updated values.
FieldMask fieldMask = FieldMaskUtil.fromString("state");
// Enable the key version.
CryptoKeyVersion response = client.updateCryptoKeyVersion(keyVersion, fieldMask);
System.out.printf("Enabled key version: %s%n", response.getName());
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersionName in project java-kms by googleapis.
the class GetKeyVersionAttestation method getKeyVersionAttestation.
// Get the attestations for a key version
public void getKeyVersionAttestation(String projectId, String locationId, String keyRingId, String keyId, String keyVersionId) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the name from the project, location, key ring, and keyId.
CryptoKeyVersionName keyVersionName = CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);
// Get the key version.
CryptoKeyVersion keyVersion = client.getCryptoKeyVersion(keyVersionName);
// will be nil.
if (!keyVersion.hasAttestation()) {
System.out.println("no attestation");
return;
}
// Print the attestation, base64-encoded.
KeyOperationAttestation attestation = keyVersion.getAttestation();
String format = attestation.getFormat().toString();
byte[] content = attestation.getContent().toByteArray();
System.out.printf("%s: %s", format, Base64.getEncoder().encodeToString(content));
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersionName in project java-kms by googleapis.
the class GetPublicKey method getPublicKey.
// Get the public key associated with an asymmetric key.
public void getPublicKey(String projectId, String locationId, String keyRingId, String keyId, String keyVersionId) throws IOException, GeneralSecurityException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the key version name from the project, location, key ring, key,
// and key version.
CryptoKeyVersionName keyVersionName = CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);
// Get the public key.
PublicKey publicKey = client.getPublicKey(keyVersionName);
System.out.printf("Public key: %s%n", publicKey.getPem());
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersionName in project java-kms by googleapis.
the class DestroyKeyVersion method destroyKeyVersion.
// Schedule destruction of the given key version.
public void destroyKeyVersion(String projectId, String locationId, String keyRingId, String keyId, String keyVersionId) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the key version name from the project, location, key ring, key,
// and key version.
CryptoKeyVersionName keyVersionName = CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);
// Destroy the key version.
CryptoKeyVersion response = client.destroyCryptoKeyVersion(keyVersionName);
System.out.printf("Destroyed key version: %s%n", response.getName());
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersionName in project java-kms by googleapis.
the class DisableKeyVersion method disableKeyVersion.
// Disable a key version from use.
public void disableKeyVersion(String projectId, String locationId, String keyRingId, String keyId, String keyVersionId) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the key version name from the project, location, key ring, key,
// and key version.
CryptoKeyVersionName keyVersionName = CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);
// Build the updated key version, setting it to disbaled.
CryptoKeyVersion keyVersion = CryptoKeyVersion.newBuilder().setName(keyVersionName.toString()).setState(CryptoKeyVersionState.DISABLED).build();
// Create a field mask of updated values.
FieldMask fieldMask = FieldMaskUtil.fromString("state");
// Disable the key version.
CryptoKeyVersion response = client.updateCryptoKeyVersion(keyVersion, fieldMask);
System.out.printf("Disabled key version: %s%n", response.getName());
}
}
Aggregations