CredStashHelper { @Retryable(maxAttempts = 3, value = CredStashGetCredentialFailedException.class, backoff = @Backoff(delay = 5000, multiplier = 2)) @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) public String getCredentialFromCredStash(String credStashEncryptionContext, String credentialName) throws CredStashGetCredentialFailedException { String credStashAwsRegion = configurationHelper.getProperty(ConfigurationValue.CREDSTASH_AWS_REGION_NAME); String credStashTableName = configurationHelper.getProperty(ConfigurationValue.CREDSTASH_TABLE_NAME); LOGGER.info("credStashAwsRegion={} credStashTableName={} credStashEncryptionContext={} credentialName={}", credStashAwsRegion, credStashTableName, credStashEncryptionContext, credentialName); ClientConfiguration clientConfiguration = awsHelper.getClientConfiguration(awsHelper.getAwsParamsDto()); CredStash credstash = credStashFactory.getCredStash(credStashAwsRegion, credStashTableName, clientConfiguration); String password = null; String errorMessage = null; try { @SuppressWarnings("unchecked") Map<String, String> credstashEncryptionContextMap = jsonHelper.unmarshallJsonToObject(Map.class, credStashEncryptionContext); password = credstash.getCredential(credentialName, credstashEncryptionContextMap); } catch (Exception exception) { LOGGER.error("Caught exception when attempting to get a credential value from CredStash.", exception); errorMessage = exception.getMessage(); } if (StringUtils.isEmpty(password)) { throw new CredStashGetCredentialFailedException(String.format("Failed to obtain credential from credstash.%s " + "credStashAwsRegion=%s credStashTableName=%s credStashEncryptionContext=%s credentialName=%s", StringUtils.isNotBlank(errorMessage) ? " Reason: " + errorMessage : "", credStashAwsRegion, credStashTableName, credStashEncryptionContext, credentialName)); } return password; }  @Retryable(maxAttempts = 3, value = CredStashGetCredentialFailedException.class, backoff = @Backoff(delay = 5000, multiplier = 2)) @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) String getCredentialFromCredStash(String credStashEncryptionContext, String credentialName);  }
@Test public void testGetCredentialFromCredStash() throws Exception { AwsParamsDto awsParamsDto = new AwsParamsDto(NO_AWS_ACCESS_KEY, NO_AWS_SECRET_KEY, NO_SESSION_TOKEN, HTTP_PROXY_HOST, HTTP_PROXY_PORT, NO_AWS_REGION_NAME); ClientConfiguration clientConfiguration = new ClientConfiguration(); Map<String, String> credStashEncryptionContextMap = new HashMap<>(); credStashEncryptionContextMap.put(KEY, VALUE); CredStash credStash = mock(CredStash.class); when(credStash.getCredential(USER_CREDENTIAL_NAME, credStashEncryptionContextMap)).thenReturn(PASSWORD); when(configurationHelper.getProperty(ConfigurationValue.CREDSTASH_AWS_REGION_NAME)).thenReturn(AWS_REGION_NAME); when(configurationHelper.getProperty(ConfigurationValue.CREDSTASH_TABLE_NAME)).thenReturn(TABLE_NAME); when(awsHelper.getAwsParamsDto()).thenReturn(awsParamsDto); when(awsHelper.getClientConfiguration(awsParamsDto)).thenReturn(clientConfiguration); when(credStashFactory.getCredStash(AWS_REGION_NAME, TABLE_NAME, clientConfiguration)).thenReturn(credStash); when(jsonHelper.unmarshallJsonToObject(Map.class, CREDSTASH_ENCRYPTION_CONTEXT)).thenReturn(credStashEncryptionContextMap); String result = credStashHelper.getCredentialFromCredStash(CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME); verify(configurationHelper).getProperty(ConfigurationValue.CREDSTASH_AWS_REGION_NAME); verify(configurationHelper).getProperty(ConfigurationValue.CREDSTASH_TABLE_NAME); verify(awsHelper).getAwsParamsDto(); verify(awsHelper).getClientConfiguration(awsParamsDto); verify(credStashFactory).getCredStash(AWS_REGION_NAME, TABLE_NAME, clientConfiguration); verify(jsonHelper).unmarshallJsonToObject(Map.class, CREDSTASH_ENCRYPTION_CONTEXT); verify(credStash).getCredential(USER_CREDENTIAL_NAME, credStashEncryptionContextMap); verifyNoMoreInteractions(credStash); verifyNoMoreInteractionsHelper(); assertEquals(PASSWORD, result); }
@Test public void testGetCredentialFromCredStashEmptyPasswordValue() throws Exception { AwsParamsDto awsParamsDto = new AwsParamsDto(NO_AWS_ACCESS_KEY, NO_AWS_SECRET_KEY, NO_SESSION_TOKEN, HTTP_PROXY_HOST, HTTP_PROXY_PORT, NO_AWS_REGION_NAME); ClientConfiguration clientConfiguration = new ClientConfiguration(); Map<String, String> credStashEncryptionContextMap = new HashMap<>(); credStashEncryptionContextMap.put(KEY, VALUE); CredStash credStash = mock(CredStash.class); when(credStash.getCredential(USER_CREDENTIAL_NAME, credStashEncryptionContextMap)).thenReturn(EMPTY_STRING); when(configurationHelper.getProperty(ConfigurationValue.CREDSTASH_AWS_REGION_NAME)).thenReturn(AWS_REGION_NAME); when(configurationHelper.getProperty(ConfigurationValue.CREDSTASH_TABLE_NAME)).thenReturn(TABLE_NAME); when(awsHelper.getAwsParamsDto()).thenReturn(awsParamsDto); when(awsHelper.getClientConfiguration(awsParamsDto)).thenReturn(clientConfiguration); when(credStashFactory.getCredStash(AWS_REGION_NAME, TABLE_NAME, clientConfiguration)).thenReturn(credStash); when(jsonHelper.unmarshallJsonToObject(Map.class, CREDSTASH_ENCRYPTION_CONTEXT)).thenReturn(credStashEncryptionContextMap); expectedException.expect(CredStashGetCredentialFailedException.class); expectedException.expectMessage(String .format("Failed to obtain credential from credstash. credStashAwsRegion=%s credStashTableName=%s credStashEncryptionContext=%s credentialName=%s", AWS_REGION_NAME, TABLE_NAME, CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME)); credStashHelper.getCredentialFromCredStash(CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME); verify(configurationHelper).getProperty(ConfigurationValue.CREDSTASH_AWS_REGION_NAME); verify(configurationHelper).getProperty(ConfigurationValue.CREDSTASH_TABLE_NAME); verify(awsHelper).getAwsParamsDto(); verify(awsHelper).getClientConfiguration(awsParamsDto); verify(credStashFactory).getCredStash(AWS_REGION_NAME, TABLE_NAME, clientConfiguration); verify(jsonHelper).unmarshallJsonToObject(Map.class, CREDSTASH_ENCRYPTION_CONTEXT); verify(credStash).getCredential(USER_CREDENTIAL_NAME, credStashEncryptionContextMap); verifyNoMoreInteractions(credStash); verifyNoMoreInteractionsHelper(); }
@Test public void testGetCredentialFromCredStashException() throws Exception { AwsParamsDto awsParamsDto = new AwsParamsDto(NO_AWS_ACCESS_KEY, NO_AWS_SECRET_KEY, NO_SESSION_TOKEN, HTTP_PROXY_HOST, HTTP_PROXY_PORT, NO_AWS_REGION_NAME); ClientConfiguration clientConfiguration = new ClientConfiguration(); Map<String, String> credStashEncryptionContextMap = new HashMap<>(); credStashEncryptionContextMap.put(KEY, VALUE); CredStash credStash = mock(CredStash.class); when(credStash.getCredential(USER_CREDENTIAL_NAME, credStashEncryptionContextMap)).thenThrow(new Exception(ERROR_MESSAGE)); when(configurationHelper.getProperty(ConfigurationValue.CREDSTASH_AWS_REGION_NAME)).thenReturn(AWS_REGION_NAME); when(configurationHelper.getProperty(ConfigurationValue.CREDSTASH_TABLE_NAME)).thenReturn(TABLE_NAME); when(awsHelper.getAwsParamsDto()).thenReturn(awsParamsDto); when(awsHelper.getClientConfiguration(awsParamsDto)).thenReturn(clientConfiguration); when(credStashFactory.getCredStash(AWS_REGION_NAME, TABLE_NAME, clientConfiguration)).thenReturn(credStash); when(jsonHelper.unmarshallJsonToObject(Map.class, CREDSTASH_ENCRYPTION_CONTEXT)).thenReturn(credStashEncryptionContextMap); expectedException.expect(CredStashGetCredentialFailedException.class); expectedException.expectMessage(String.format("Failed to obtain credential from credstash. Reason: %s " + "credStashAwsRegion=%s credStashTableName=%s credStashEncryptionContext=%s credentialName=%s", ERROR_MESSAGE, AWS_REGION_NAME, TABLE_NAME, CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME)); credStashHelper.getCredentialFromCredStash(CREDSTASH_ENCRYPTION_CONTEXT, USER_CREDENTIAL_NAME); verify(configurationHelper).getProperty(ConfigurationValue.CREDSTASH_AWS_REGION_NAME); verify(configurationHelper).getProperty(ConfigurationValue.CREDSTASH_TABLE_NAME); verify(awsHelper).getAwsParamsDto(); verify(awsHelper).getClientConfiguration(awsParamsDto); verify(credStashFactory).getCredStash(AWS_REGION_NAME, TABLE_NAME, clientConfiguration); verify(jsonHelper).unmarshallJsonToObject(Map.class, CREDSTASH_ENCRYPTION_CONTEXT); verify(credStash).getCredential(USER_CREDENTIAL_NAME, credStashEncryptionContextMap); verifyNoMoreInteractions(credStash); verifyNoMoreInteractionsHelper(); }