Search in sources :

Example 1 with GoogleConfigProvider

use of org.springframework.cloud.config.server.environment.secretmanager.GoogleConfigProvider 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 2 with GoogleConfigProvider

use of org.springframework.cloud.config.server.environment.secretmanager.GoogleConfigProvider 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)

Aggregations

SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 GoogleConfigProvider (org.springframework.cloud.config.server.environment.secretmanager.GoogleConfigProvider)2 GoogleSecretManagerV1AccessStrategy (org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretManagerV1AccessStrategy)2 HttpHeaderGoogleConfigProvider (org.springframework.cloud.config.server.environment.secretmanager.HttpHeaderGoogleConfigProvider)2 RestTemplate (org.springframework.web.client.RestTemplate)2 AccessSecretVersionRequest (com.google.cloud.secretmanager.v1.AccessSecretVersionRequest)1 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)1 ListSecretVersionsRequest (com.google.cloud.secretmanager.v1.ListSecretVersionsRequest)1 ListSecretsRequest (com.google.cloud.secretmanager.v1.ListSecretsRequest)1 Secret (com.google.cloud.secretmanager.v1.Secret)1 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)1 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)1 ByteString (com.google.protobuf.ByteString)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1 GoogleSecretComparatorByVersion (org.springframework.cloud.config.server.environment.secretmanager.GoogleSecretComparatorByVersion)1