Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralise vault environment variable name definitions #651

Merged
merged 3 commits into from Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ public final class EnvironmentVariables {
public static final String SERVER_TRUSTSTORE_PWD = "TESSERA_SERVER_TRUSTSTORE_PWD";
public static final String CLIENT_KEYSTORE_PWD = "TESSERA_CLIENT_KEYSTORE_PWD";
public static final String CLIENT_TRUSTSTORE_PWD = "TESSERA_CLIENT_TRUSTSTORE_PWD";
public static final String AZURE_CLIENT_ID = "AZURE_CLIENT_ID";
public static final String AZURE_CLIENT_SECRET = "AZURE_CLIENT_SECRET";
public static final String HASHICORP_ROLE_ID = "HASHICORP_ROLE_ID";
public static final String HASHICORP_SECRET_ID = "HASHICORP_SECRET_ID";
public static final String HASHICORP_TOKEN = "HASHICORP_TOKEN";
public static final String HASHICORP_CLIENT_KEYSTORE_PWD = "HASHICORP_CLIENT_KEYSTORE_PWD";
public static final String HASHICORP_CLIENT_TRUSTSTORE_PWD = "HASHICORP_CLIENT_TRUSTSTORE_PWD";

private EnvironmentVariables() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static org.mockito.Mockito.*;

public class GrpcAppTest {
private static final String contextName = "context";
private static final String CONTEXT_NAME = "context";

private ServiceLocator serviceLocator;

Expand All @@ -29,7 +29,7 @@ public class GrpcAppTest {
@Before
public void setUp() {
serviceLocator = mock(ServiceLocator.class);
grpcApp = new GrpcApp(serviceLocator, contextName);
grpcApp = new GrpcApp(serviceLocator, CONTEXT_NAME);
}

@After
Expand All @@ -40,17 +40,17 @@ public void tearDown() {
@Test
public void getBindableServices() {
grpcApp.getBindableServices();
grpcApp = new P2PGrpcApp(serviceLocator, contextName);
grpcApp = new P2PGrpcApp(serviceLocator, CONTEXT_NAME);
grpcApp.getBindableServices();
grpcApp = new Q2TGrpcApp(serviceLocator, contextName);
grpcApp = new Q2TGrpcApp(serviceLocator, CONTEXT_NAME);
grpcApp.getBindableServices();
verify(serviceLocator, times(3)).getServices(contextName);
verify(serviceLocator, times(3)).getServices(CONTEXT_NAME);
}

@Test
public void createWithNoServiceLocator() {

final Throwable throwable = catchThrowable(() -> new GrpcApp(null, contextName));
final Throwable throwable = catchThrowable(() -> new GrpcApp(null, CONTEXT_NAME));
assertThat(throwable).isInstanceOf(NullPointerException.class);

final Throwable throwableName = catchThrowable(() -> new GrpcApp(serviceLocator, null));
Expand All @@ -63,13 +63,13 @@ public void onCreateApiObjects() {
TesseraGrpcService apiObject = new TesseraGrpcService();
Object nonApiObject = new HashMap<>();

when(serviceLocator.getServices(contextName))
when(serviceLocator.getServices(CONTEXT_NAME))
.thenReturn(Stream.of(apiObject, nonApiObject)
.collect(Collectors.toSet()));

Set<BindableService> result = grpcApp.getBindableServices();
assertThat(result).containsOnly(apiObject);
verify(serviceLocator).getServices(contextName);
verify(serviceLocator).getServices(CONTEXT_NAME);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class AdminRestAppTest {

private static final String contextName = "context";
private static final String CONTEXT_NAME = "context";

private ServiceLocator serviceLocator;

Expand All @@ -19,7 +19,7 @@ public class AdminRestAppTest {
public void setUp() {
this.serviceLocator = mock(ServiceLocator.class);

this.adminRestApp = new AdminRestApp(serviceLocator, contextName);
this.adminRestApp = new AdminRestApp(serviceLocator, CONTEXT_NAME);
}

@After
Expand All @@ -31,7 +31,7 @@ public void tearDown() {
public void getSingletons() {
this.adminRestApp.getSingletons();

verify(serviceLocator).getServices(contextName);
verify(serviceLocator).getServices(CONTEXT_NAME);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.quorum.tessera.app;

import com.quorum.tessera.api.exception.DefaultExceptionMapper;
import com.quorum.tessera.p2p.ApiResource;
import com.quorum.tessera.p2p.P2PRestApp;
import com.quorum.tessera.api.exception.DefaultExceptionMapper;
import com.quorum.tessera.service.locator.ServiceLocator;
import org.junit.After;
import org.junit.Before;
Expand All @@ -19,7 +19,7 @@

public class RestAppTest {

private static final String contextName = "context";
private static final String CONTEXT_NAME = "context";

private ServiceLocator serviceLocator;

Expand All @@ -28,7 +28,7 @@ public class RestAppTest {
@Before
public void setUp() {
serviceLocator = mock(ServiceLocator.class);
p2PRestApp = new P2PRestApp(serviceLocator, contextName);
p2PRestApp = new P2PRestApp(serviceLocator, CONTEXT_NAME);
}

@After
Expand All @@ -39,13 +39,13 @@ public void tearDown() {
@Test
public void getSingletons() {
p2PRestApp.getSingletons();
verify(serviceLocator).getServices(contextName);
verify(serviceLocator).getServices(CONTEXT_NAME);
}

@Test
public void createWithNoServiceLocator() {

final Throwable throwable = catchThrowable(() -> new P2PRestApp(null, contextName));
final Throwable throwable = catchThrowable(() -> new P2PRestApp(null, CONTEXT_NAME));
assertThat(throwable).isInstanceOf(NullPointerException.class);

final Throwable throwableName = catchThrowable(() -> new P2PRestApp(serviceLocator, null));
Expand All @@ -59,13 +59,13 @@ public void onCreateApiObjects() {
DefaultExceptionMapper nestedApiObject = new DefaultExceptionMapper();
Object nonApiObject = new HashMap<>();

when(serviceLocator.getServices(contextName))
when(serviceLocator.getServices(CONTEXT_NAME))
.thenReturn(Stream.of(apiObject, nestedApiObject, nonApiObject)
.collect(Collectors.toSet()));

Set<Object> result = p2PRestApp.getSingletons();
assertThat(result).containsOnly(apiObject, nestedApiObject);
verify(serviceLocator).getServices(contextName);
verify(serviceLocator).getServices(CONTEXT_NAME);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class P2PRestAppTest {

private static final String contextName = "context";
private static final String CONTEXT_NAME = "context";

private ServiceLocator serviceLocator;

Expand All @@ -18,7 +18,7 @@ public class P2PRestAppTest {
@Before
public void setUp() {
serviceLocator = mock(ServiceLocator.class);
p2PRestApp = new P2PRestApp(serviceLocator, contextName);
p2PRestApp = new P2PRestApp(serviceLocator, CONTEXT_NAME);
}

@After
Expand All @@ -29,6 +29,6 @@ public void tearDown() {
@Test
public void getSingletons() {
p2PRestApp.getSingletons();
verify(serviceLocator).getServices(contextName);
verify(serviceLocator).getServices(CONTEXT_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class Q2TRestAppTest {

private static final String contextName = "context";
private static final String CONTEXT_NAME = "context";

private ServiceLocator serviceLocator;

Expand All @@ -18,7 +18,7 @@ public class Q2TRestAppTest {
@Before
public void setUp() {
serviceLocator = mock(ServiceLocator.class);
q2TRestApp = new Q2TRestApp(serviceLocator, contextName);
q2TRestApp = new Q2TRestApp(serviceLocator, CONTEXT_NAME);
}

@After
Expand All @@ -29,6 +29,6 @@ public void tearDown() {
@Test
public void getSingletons() {
q2TRestApp.getSingletons();
verify(serviceLocator).getServices(contextName);
verify(serviceLocator).getServices(CONTEXT_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class ThirdPartyTest {

private static final String contextName = "context";
private static final String CONTEXT_NAME = "context";

private ServiceLocator serviceLocator;

Expand All @@ -18,7 +18,7 @@ public class ThirdPartyTest {
@Before
public void setUp() {
serviceLocator = mock(ServiceLocator.class);
thirdParty = new ThirdPartyRestApp(serviceLocator, contextName);
thirdParty = new ThirdPartyRestApp(serviceLocator, CONTEXT_NAME);
}

@After
Expand All @@ -29,6 +29,6 @@ public void tearDown() {
@Test
public void getSingletons() {
thirdParty.getSingletons();
verify(serviceLocator).getServices(contextName);
verify(serviceLocator).getServices(CONTEXT_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

import static com.quorum.tessera.config.util.EnvironmentVariables.AZURE_CLIENT_ID;
import static com.quorum.tessera.config.util.EnvironmentVariables.AZURE_CLIENT_SECRET;

public class AzureKeyVaultClientCredentials extends KeyVaultCredentials {

private static final Logger LOGGER = LoggerFactory.getLogger(AzureKeyVaultClientCredentials.class);
Expand All @@ -37,7 +40,7 @@ void setAuthenticationContext(AuthenticationContext authenticationContext) {
@Override
public String doAuthenticate(String authorization, String resource, String scope) {
if(clientId == null || clientSecret == null) {
throw new AzureCredentialNotSetException("AZURE_CLIENT_ID and AZURE_CLIENT_SECRET environment variables must be set");
throw new AzureCredentialNotSetException(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set");
}
try {
if(Objects.isNull(authenticationContext)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
import java.util.Optional;
import java.util.concurrent.Executors;

public class AzureKeyVaultServiceFactory implements KeyVaultServiceFactory {
import static com.quorum.tessera.config.util.EnvironmentVariables.AZURE_CLIENT_ID;
import static com.quorum.tessera.config.util.EnvironmentVariables.AZURE_CLIENT_SECRET;

private static final String clientIdEnvVar = "AZURE_CLIENT_ID";
private static final String clientSecretEnvVar = "AZURE_CLIENT_SECRET";
public class AzureKeyVaultServiceFactory implements KeyVaultServiceFactory {

@Override
public KeyVaultService create(Config config, EnvironmentVariableProvider envProvider) {
Objects.requireNonNull(config);
Objects.requireNonNull(envProvider);

String clientId = envProvider.getEnv(clientIdEnvVar);
String clientSecret = envProvider.getEnv(clientSecretEnvVar);
String clientId = envProvider.getEnv(AZURE_CLIENT_ID);
String clientSecret = envProvider.getEnv(AZURE_CLIENT_SECRET);

if(clientId == null || clientSecret == null) {
throw new AzureCredentialNotSetException(clientIdEnvVar + " and " + clientSecretEnvVar + " environment variables must be set");
throw new AzureCredentialNotSetException(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set");
}

AzureKeyVaultConfig keyVaultConfig = Optional.ofNullable(config.getKeys())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import static com.quorum.tessera.config.util.EnvironmentVariables.AZURE_CLIENT_ID;
import static com.quorum.tessera.config.util.EnvironmentVariables.AZURE_CLIENT_SECRET;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -118,7 +120,7 @@ public void nullClientIdThrowsRuntimeException() {
final Throwable ex = catchThrowable(() -> credentials.doAuthenticate(goodUrl, null, null));

assertThat(ex).isInstanceOf(RuntimeException.class)
.hasMessageContaining("AZURE_CLIENT_ID and AZURE_CLIENT_SECRET environment variables must be set");
.hasMessageContaining(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set");
}

@Test
Expand All @@ -129,7 +131,7 @@ public void nullClientSecretThrowsRuntimeException() {
final Throwable ex = catchThrowable(() -> credentials.doAuthenticate(goodUrl, null, null));

assertThat(ex).isInstanceOf(RuntimeException.class)
.hasMessageContaining("AZURE_CLIENT_ID and AZURE_CLIENT_SECRET environment variables must be set");
.hasMessageContaining(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set");
}

@Test
Expand All @@ -140,7 +142,7 @@ public void nullClientIdAndSecretThrowsRuntimeException() {
final Throwable ex = catchThrowable(() -> credentials.doAuthenticate(goodUrl, null, null));

assertThat(ex).isInstanceOf(RuntimeException.class)
.hasMessageContaining("AZURE_CLIENT_ID and AZURE_CLIENT_SECRET environment variables must be set");
.hasMessageContaining(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.junit.Before;
import org.junit.Test;

import static com.quorum.tessera.config.util.EnvironmentVariables.AZURE_CLIENT_ID;
import static com.quorum.tessera.config.util.EnvironmentVariables.AZURE_CLIENT_SECRET;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -41,33 +43,33 @@ public void nullEnvVarProviderThrowsException() {
public void clientIdEnvironmentVariableNotSetThrowsException() {
Throwable ex = catchThrowable(() -> azureKeyVaultServiceFactory.create(config, envProvider));

when(envProvider.getEnv("AZURE_CLIENT_ID")).thenReturn(null);
when(envProvider.getEnv("AZURE_CLIENT_SECRET")).thenReturn("secret");
when(envProvider.getEnv(AZURE_CLIENT_ID)).thenReturn(null);
when(envProvider.getEnv(AZURE_CLIENT_SECRET)).thenReturn("secret");

assertThat(ex).isInstanceOf(AzureCredentialNotSetException.class);
assertThat(ex.getMessage()).isEqualTo("AZURE_CLIENT_ID and AZURE_CLIENT_SECRET environment variables must be set");
assertThat(ex.getMessage()).isEqualTo(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set");
}

@Test
public void clientSecretEnvironmentVariableNotSetThrowsException() {
Throwable ex = catchThrowable(() -> azureKeyVaultServiceFactory.create(config, envProvider));

when(envProvider.getEnv("AZURE_CLIENT_ID")).thenReturn("id");
when(envProvider.getEnv("AZURE_CLIENT_SECRET")).thenReturn(null);
when(envProvider.getEnv(AZURE_CLIENT_ID)).thenReturn("id");
when(envProvider.getEnv(AZURE_CLIENT_SECRET)).thenReturn(null);

assertThat(ex).isInstanceOf(AzureCredentialNotSetException.class);
assertThat(ex.getMessage()).isEqualTo("AZURE_CLIENT_ID and AZURE_CLIENT_SECRET environment variables must be set");
assertThat(ex.getMessage()).isEqualTo(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set");
}

@Test
public void bothClientIdAndClientSecretEnvironmentVariablesNotSetThrowsException() {
Throwable ex = catchThrowable(() -> azureKeyVaultServiceFactory.create(config, envProvider));

when(envProvider.getEnv("AZURE_CLIENT_ID")).thenReturn(null);
when(envProvider.getEnv("AZURE_CLIENT_SECRET")).thenReturn(null);
when(envProvider.getEnv(AZURE_CLIENT_ID)).thenReturn(null);
when(envProvider.getEnv(AZURE_CLIENT_SECRET)).thenReturn(null);

assertThat(ex).isInstanceOf(AzureCredentialNotSetException.class);
assertThat(ex.getMessage()).isEqualTo("AZURE_CLIENT_ID and AZURE_CLIENT_SECRET environment variables must be set");
assertThat(ex.getMessage()).isEqualTo(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set");
}

@Test
Expand Down
Loading