Search in sources :

Example 1 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project spring-cloud-config by spring-cloud.

the class GoogleSecretManagerEnvironmentRepositoryTests method testSupportedStrategy.

@Test
public void testSupportedStrategy() {
    GoogleSecretManagerEnvironmentProperties properties = new GoogleSecretManagerEnvironmentProperties();
    SecretManagerServiceClient mock = mock(SecretManagerServiceClient.class);
    properties.setVersion(1);
    assertThat(GoogleSecretManagerAccessStrategyFactory.forVersion(null, null, properties, mock) instanceof GoogleSecretManagerV1AccessStrategy).isTrue();
}
Also used : SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) GoogleSecretManagerV1AccessStrategy(org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretManagerV1AccessStrategy) Test(org.junit.Test)

Example 2 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project spring-cloud-config by spring-cloud.

the class GoogleSecretManagerEnvironmentRepositoryTests method testGetUnsupportedStrategy.

@Test(expected = IllegalArgumentException.class)
public void testGetUnsupportedStrategy() {
    GoogleSecretManagerEnvironmentProperties properties = new GoogleSecretManagerEnvironmentProperties();
    SecretManagerServiceClient mock = mock(SecretManagerServiceClient.class);
    properties.setVersion(2);
    GoogleSecretManagerAccessStrategyFactory.forVersion(null, null, properties, mock);
}
Also used : SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) Test(org.junit.Test)

Example 3 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project spring-cloud-config by spring-cloud.

the class GoogleSecretManagerEnvironmentRepositoryTests method testGetSecrets.

@Test
@SuppressWarnings("unchecked")
public void testGetSecrets() 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.ListSecretsPagedResponse response = mock(SecretManagerServiceClient.ListSecretsPagedResponse.class);
    Secret secret = Secret.newBuilder().setName("projects/test-project/secrets/test").build();
    List<Secret> secrets = new ArrayList<Secret>();
    secrets.add(secret);
    when(response.iterateAll()).thenReturn(secrets);
    Mockito.doReturn(response).when(mock).listSecrets(any(ListSecretsRequest.class));
    GoogleSecretManagerV1AccessStrategy strategy = new GoogleSecretManagerV1AccessStrategy(rest, provider, mock);
    assertThat(strategy.getSecrets().size()).isEqualTo(1);
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) HttpHeaderGoogleConfigProvider(org.springframework.cloud.config.server.environment.secretmanager.HttpHeaderGoogleConfigProvider) GoogleConfigProvider(org.springframework.cloud.config.server.environment.secretmanager.GoogleConfigProvider) ListSecretsRequest(com.google.cloud.secretmanager.v1.ListSecretsRequest) RestTemplate(org.springframework.web.client.RestTemplate) ArrayList(java.util.ArrayList) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) GoogleSecretManagerV1AccessStrategy(org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretManagerV1AccessStrategy) Test(org.junit.Test)

Example 4 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class QuickstartIT method afterAll.

@AfterClass
public static void afterAll() throws Exception {
    Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Delete the secret created by quickstart
        SecretName name = SecretName.of(PROJECT_ID, SECRET_ID);
        DeleteSecretRequest deleteRequest = DeleteSecretRequest.newBuilder().setName(name.toString()).build();
        client.deleteSecret(deleteRequest);
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) DeleteSecretRequest(com.google.cloud.secretmanager.v1.DeleteSecretRequest) AfterClass(org.junit.AfterClass)

Example 5 with SecretManagerServiceClient

use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class AddSecretVersion method addSecretVersion.

// Add a new version to the existing secret.
public static void addSecretVersion(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()) {
        SecretName secretName = SecretName.of(projectId, secretId);
        // Create the secret payload.
        SecretPayload payload = SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("my super secret data")).build();
        // Add the secret version.
        SecretVersion version = client.addSecretVersion(secretName, payload);
        System.out.printf("Added secret version %s\n", version.getName());
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) SecretPayload(com.google.cloud.secretmanager.v1.SecretPayload)

Aggregations

SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)51 SecretName (com.google.cloud.secretmanager.v1.SecretName)23 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)19 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)13 Secret (com.google.cloud.secretmanager.v1.Secret)12 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)11 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)6 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)6 ByteString (com.google.protobuf.ByteString)6 Test (org.junit.Test)5 DeleteSecretRequest (com.google.cloud.secretmanager.v1.DeleteSecretRequest)4 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse)4 Binding (com.google.iam.v1.Binding)4 Policy (com.google.iam.v1.Policy)4 ListSecretVersionsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse)3 FieldMask (com.google.protobuf.FieldMask)3 AfterClass (org.junit.AfterClass)3 GoogleSecretManagerV1AccessStrategy (org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretManagerV1AccessStrategy)3 AddSecretVersionRequest (com.google.cloud.secretmanager.v1.AddSecretVersionRequest)2 CreateSecretRequest (com.google.cloud.secretmanager.v1.CreateSecretRequest)2