Search in sources :

Example 6 with AccessSecretVersionResponse

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

Example 7 with AccessSecretVersionResponse

use of com.google.cloud.secretmanager.v1beta1.AccessSecretVersionResponse 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 8 with AccessSecretVersionResponse

use of com.google.cloud.secretmanager.v1beta1.AccessSecretVersionResponse in project micronaut-gcp by micronaut-projects.

the class GoogleClientExample method onStartup.

@EventListener
public void onStartup(StartupEvent event) {
    AccessSecretVersionResponse response = client.accessSecretVersion(AccessSecretVersionRequest.newBuilder().setName(SecretVersionName.of("my-cloud-project", "secretName", "latest").toString()).build());
    String secret = response.getPayload().getData().toStringUtf8();
}
Also used : AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse) EventListener(io.micronaut.runtime.event.annotation.EventListener)

Example 9 with AccessSecretVersionResponse

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

the class ITSystemTest method accessSecretVersionTest.

@Test
public void accessSecretVersionTest() {
    AccessSecretVersionRequest request = AccessSecretVersionRequest.newBuilder().setName(formattedSecretVersion).build();
    AccessSecretVersionResponse response = client.accessSecretVersion(request);
    assertEquals(secretVersion.getName(), response.getName());
    assertEquals(PAYLOAD, response.getPayload());
}
Also used : AccessSecretVersionRequest(com.google.cloud.secretmanager.v1beta1.AccessSecretVersionRequest) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1beta1.AccessSecretVersionResponse) Test(org.junit.Test)

Example 10 with AccessSecretVersionResponse

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

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 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);
        // 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);
    }
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)

Aggregations

AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)9 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)5 ByteString (com.google.protobuf.ByteString)5 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)4 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)3 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)3 AccessSecretVersionRequest (com.google.cloud.secretmanager.v1.AccessSecretVersionRequest)2 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)2 Secret (com.google.cloud.secretmanager.v1.Secret)2 Test (org.junit.Test)2 ListSecretVersionsRequest (com.google.cloud.secretmanager.v1.ListSecretVersionsRequest)1 AccessSecretVersionRequest (com.google.cloud.secretmanager.v1beta1.AccessSecretVersionRequest)1 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1beta1.AccessSecretVersionResponse)1 YamlPropertySourceLoader (io.micronaut.context.env.yaml.YamlPropertySourceLoader)1 EventListener (io.micronaut.runtime.event.annotation.EventListener)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1 GoogleConfigProvider (org.springframework.cloud.config.server.environment.secretmanager.GoogleConfigProvider)1 GoogleSecretComparatorByVersion (org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretComparatorByVersion)1