use of com.google.cloud.secretmanager.v1.SecretVersion in project java-docs-samples by GoogleCloudPlatform.
the class EnableSecretVersion method enableSecretVersion.
// Enable an existing secret version.
public static void enableSecretVersion(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()) {
// Build the name from the version.
SecretVersionName secretVersionName = SecretVersionName.of(projectId, secretId, versionId);
// Enable the secret version.
SecretVersion version = client.enableSecretVersion(secretVersionName);
System.out.printf("Enabled secret version %s\n", version.getName());
}
}
use of com.google.cloud.secretmanager.v1.SecretVersion in project java-docs-samples by GoogleCloudPlatform.
the class EnableSecretVersionWithEtag method enableSecretVersion.
// Enable an existing secret version.
public static void enableSecretVersion(String projectId, String secretId, String versionId, String etag) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Build the name from the version.
SecretVersionName secretVersionName = SecretVersionName.of(projectId, secretId, versionId);
// Build the request.
EnableSecretVersionRequest request = EnableSecretVersionRequest.newBuilder().setName(secretVersionName.toString()).setEtag(etag).build();
// Enable the secret version.
SecretVersion version = client.enableSecretVersion(request);
System.out.printf("Enabled secret version %s\n", version.getName());
}
}
use of com.google.cloud.secretmanager.v1.SecretVersion in project java-docs-samples by GoogleCloudPlatform.
the class GetSecretVersion method getSecretVersion.
// Get an existing secret version.
public static void getSecretVersion(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()) {
// Build the name from the version.
SecretVersionName secretVersionName = SecretVersionName.of(projectId, secretId, versionId);
// Create the secret.
SecretVersion version = client.getSecretVersion(secretVersionName);
System.out.printf("Secret version %s, state %s\n", version.getName(), version.getState());
}
}
use of com.google.cloud.secretmanager.v1.SecretVersion in project java-docs-samples by GoogleCloudPlatform.
the class DestroySecretVersion method destroySecretVersion.
// Destroy an existing secret version.
public static void destroySecretVersion(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()) {
// Build the name from the version.
SecretVersionName secretVersionName = SecretVersionName.of(projectId, secretId, versionId);
// Destroy the secret version.
SecretVersion version = client.destroySecretVersion(secretVersionName);
System.out.printf("Destroyed secret version %s\n", version.getName());
}
}
use of com.google.cloud.secretmanager.v1.SecretVersion in project spring-cloud-config by spring-cloud.
the class GoogleSecretManagerEnvironmentRepositoryTests method testGetSecretValues.
@Test
@SuppressWarnings("unchecked")
public void testGetSecretValues() throws IOException {
RestTemplate rest = mock(RestTemplate.class);
GoogleConfigProvider provider = mock(HttpHeaderGoogleConfigProvider.class);
when(provider.getValue(HttpHeaderGoogleConfigProvider.PROJECT_ID_HEADER, true)).thenReturn("test-project");
SecretManagerServiceClient mock = mock(SecretManagerServiceClient.class);
SecretManagerServiceClient.ListSecretVersionsPagedResponse response = mock(SecretManagerServiceClient.ListSecretVersionsPagedResponse.class);
SecretVersion secret1 = SecretVersion.newBuilder().setName("projects/test-project/secrets/test/versions/1").setState(SecretVersion.State.ENABLED).build();
SecretVersion secret2 = SecretVersion.newBuilder().setName("projects/test-project/secrets/test/versions/2").setState(SecretVersion.State.DISABLED).build();
List<SecretVersion> secrets = new ArrayList<SecretVersion>();
secrets.add(secret1);
secrets.add(secret2);
when(response.iterateAll()).thenReturn(secrets);
Mockito.doReturn(response).when(mock).listSecretVersions(any(ListSecretVersionsRequest.class));
GoogleSecretManagerV1AccessStrategy strategy = new GoogleSecretManagerV1AccessStrategy(rest, provider, mock);
AccessSecretVersionResponse accessSecretVersionResponse = mock(AccessSecretVersionResponse.class);
SecretPayload payload = mock(SecretPayload.class);
ByteString data = mock(ByteString.class);
when(accessSecretVersionResponse.getPayload()).thenReturn(payload);
when(payload.getData()).thenReturn(data);
when(data.toStringUtf8()).thenReturn("test-value");
ArgumentMatcher<AccessSecretVersionRequest> matcher = new ArgumentMatcher<AccessSecretVersionRequest>() {
@Override
public boolean matches(AccessSecretVersionRequest accessSecretVersionRequest) {
if (accessSecretVersionRequest.getName().equals("projects/test-project/secrets/test/versions/1")) {
return true;
}
return false;
}
};
Mockito.doReturn(accessSecretVersionResponse).when(mock).accessSecretVersion(ArgumentMatchers.argThat(matcher));
assertThat(strategy.getSecretValue(Secret.newBuilder().setName("projects/test-project/secrets/test").build(), new GoogleSecretComparatorByVersion())).isEqualTo("test-value");
}
Aggregations