Search in sources :

Example 21 with SecretVersion

use of com.google.cloud.secretmanager.v1beta1.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());
    }
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 22 with SecretVersion

use of com.google.cloud.secretmanager.v1beta1.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());
    }
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 23 with SecretVersion

use of com.google.cloud.secretmanager.v1beta1.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");
}
Also used : HttpHeaderGoogleConfigProvider(org.springframework.cloud.config.server.environment.secretmanager.HttpHeaderGoogleConfigProvider) GoogleConfigProvider(org.springframework.cloud.config.server.environment.secretmanager.GoogleConfigProvider) AccessSecretVersionRequest(com.google.cloud.secretmanager.v1.AccessSecretVersionRequest) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) GoogleSecretManagerV1AccessStrategy(org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretManagerV1AccessStrategy) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse) SecretPayload(com.google.cloud.secretmanager.v1.SecretPayload) GoogleSecretComparatorByVersion(org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretComparatorByVersion) ArgumentMatcher(org.mockito.ArgumentMatcher) RestTemplate(org.springframework.web.client.RestTemplate) ListSecretVersionsRequest(com.google.cloud.secretmanager.v1.ListSecretVersionsRequest) Test(org.junit.Test)

Example 24 with SecretVersion

use of com.google.cloud.secretmanager.v1beta1.SecretVersion in project nomulus by google.

the class SecretManagerClientImpl method addSecretVersion.

@Override
public String addSecretVersion(String secretId, String data) {
    checkNotNull(secretId, "secretId");
    checkNotNull(data, "data");
    SecretName secretName = SecretName.of(project, secretId);
    SecretPayload secretPayload = SecretPayload.newBuilder().setData(ByteString.copyFromUtf8(data)).build();
    SecretVersion response = callSecretManager(() -> csmClient.addSecretVersion(secretName, secretPayload));
    checkState(SecretVersionName.isParsableFrom(response.getName()));
    SecretVersionName secretVersionName = SecretVersionName.parse(response.getName());
    return secretVersionName.getSecretVersion();
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretPayload(com.google.cloud.secretmanager.v1.SecretPayload)

Example 25 with SecretVersion

use of com.google.cloud.secretmanager.v1beta1.SecretVersion in project java-secretmanager by googleapis.

the class ITSystemTest method listSecretVersionsTest.

@Test
public void listSecretVersionsTest() {
    ListSecretVersionsRequest request = ListSecretVersionsRequest.newBuilder().setParent(formattedSecretName).build();
    for (SecretVersion actualSecretVersion : client.listSecretVersions(request).iterateAll()) {
        if (secretVersion.getName().equals(actualSecretVersion.getName())) {
            assertEquals(secretVersion.getCreateTime(), actualSecretVersion.getCreateTime());
            assertEquals(SecretVersion.State.ENABLED, actualSecretVersion.getState());
        }
    }
}
Also used : SecretVersion(com.google.cloud.secretmanager.v1beta1.SecretVersion) ListSecretVersionsRequest(com.google.cloud.secretmanager.v1beta1.ListSecretVersionsRequest) Test(org.junit.Test)

Aggregations

SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)22 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)19 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)13 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)7 Test (org.junit.Test)7 SecretName (com.google.cloud.secretmanager.v1.SecretName)5 ByteString (com.google.protobuf.ByteString)5 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)4 SecretVersion (com.google.cloud.secretmanager.v1beta1.SecretVersion)4 AccessSecretVersionRequest (com.google.cloud.secretmanager.v1.AccessSecretVersionRequest)2 ListSecretVersionsRequest (com.google.cloud.secretmanager.v1.ListSecretVersionsRequest)2 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)2 Secret (com.google.cloud.secretmanager.v1.Secret)2 ListSecretVersionsPagedResponse (com.google.cloud.secretmanager.v1beta1.SecretManagerServiceClient.ListSecretVersionsPagedResponse)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 ArrayList (java.util.ArrayList)2 DestroySecretVersionRequest (com.google.cloud.secretmanager.v1.DestroySecretVersionRequest)1 DisableSecretVersionRequest (com.google.cloud.secretmanager.v1.DisableSecretVersionRequest)1 EnableSecretVersionRequest (com.google.cloud.secretmanager.v1.EnableSecretVersionRequest)1 AddSecretVersionRequest (com.google.cloud.secretmanager.v1beta1.AddSecretVersionRequest)1