use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method addSecretVersion.
private static SecretVersion addSecretVersion(Secret secret) throws IOException {
SecretName parent = SecretName.parse(secret.getName());
AddSecretVersionRequest request = AddSecretVersionRequest.newBuilder().setParent(parent.toString()).setPayload(SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("my super secret data")).build()).build();
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
return client.addSecretVersion(request);
}
}
use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method testDeleteSecret.
@Test
public void testDeleteSecret() throws IOException {
SecretName name = SecretName.parse(TEST_SECRET_TO_DELETE.getName());
DeleteSecret.deleteSecret(name.getProject(), name.getSecret());
assertThat(stdOut.toString()).contains("Deleted secret");
}
use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.
the class AccessSecretVersion method accessSecretVersion.
// Access the payload for the given secret version if one exists. The version
// can be a version number as a string (e.g. "5") or an alias (e.g. "latest").
public static void accessSecretVersion(String projectId, String secretId, String versionId) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
SecretVersionName secretVersionName = SecretVersionName.of(projectId, secretId, versionId);
// Access the secret version.
AccessSecretVersionResponse response = client.accessSecretVersion(secretVersionName);
// Verify checksum. The used library is available in Java 9+.
// If using Java 8, you may use the following:
// https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/files/Crc32c
byte[] data = response.getPayload().getData().toByteArray();
Checksum checksum = new CRC32C();
checksum.update(data, 0, data.length);
if (response.getPayload().getDataCrc32C() != checksum.getValue()) {
System.out.printf("Data corruption detected.");
return;
}
// Print the secret payload.
//
// WARNING: Do not print the secret in a production environment - this
// snippet is showing how to access the secret material.
String payload = response.getPayload().getData().toStringUtf8();
System.out.printf("Plaintext: %s\n", payload);
}
}
use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.
the class CreateSecret method createSecret.
// Add a new version to the existing secret.
public static void createSecret(String projectId, String secretId) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Build the parent name from the project.
ProjectName projectName = ProjectName.of(projectId);
// Build the secret to create.
Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
// Create the secret.
Secret createdSecret = client.createSecret(projectName, secretId, secret);
System.out.printf("Created secret %s\n", createdSecret.getName());
}
}
use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.
the class DeleteSecret method deleteSecret.
// Delete an existing secret with the given name.
public static void deleteSecret(String projectId, String secretId) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Build the secret name.
SecretName secretName = SecretName.of(projectId, secretId);
// Delete the secret.
client.deleteSecret(secretName);
System.out.printf("Deleted secret %s\n", secretId);
}
}
Aggregations