Skip to content

Commit

Permalink
Merge pull request #8 from coveo/fix/missing-validations
Browse files Browse the repository at this point in the history
Added more validations on the result received from AWS.
  • Loading branch information
jebeaudet authored Jun 10, 2019
2 parents 8ac86f2 + d8b385e commit 180f71a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.coveo</groupId>
<artifactId>spring-boot-parameter-store-integration</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>

<name>Spring Boot Parameter Store Integration</name>
<description>An integration of Amazon Web Services' Systems Manager Parameter Store for Spring Boot's properties injection.</description>
Expand Down Expand Up @@ -41,7 +41,7 @@
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.9.8</version>
<version>2.9.9</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand All @@ -52,19 +52,19 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.5.14.RELEASE</version>
<version>1.5.21.RELEASE</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ssm</artifactId>
<version>1.11.351</version>
<version>1.11.566</version>
</dependency>

<!-- Test libraries -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.14.RELEASE</version>
<version>1.5.21.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest;
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult;
import com.amazonaws.services.simplesystemsmanagement.model.ParameterNotFoundException;
import com.coveo.configuration.parameterstore.exception.ParameterStoreParameterNotFoundError;
import com.coveo.configuration.parameterstore.exception.ParameterStoreError;
import com.coveo.configuration.parameterstore.exception.ParameterStoreParameterNotFoundError;

public class ParameterStoreSource
{
Expand All @@ -20,9 +21,10 @@ public ParameterStoreSource(AWSSimpleSystemsManagement ssmClient, boolean haltBo
public Object getProperty(String propertyName)
{
try {
return ssmClient.getParameter(new GetParameterRequest().withName(propertyName).withWithDecryption(true))
.getParameter()
.getValue();
GetParameterResult getParameterResult = ssmClient.getParameter(new GetParameterRequest().withName(propertyName)
.withWithDecryption(true));
validate(propertyName, getParameterResult);
return getParameterResult.getParameter().getValue();
} catch (ParameterNotFoundException e) {
if (haltBoot) {
throw new ParameterStoreParameterNotFoundError(propertyName, e);
Expand All @@ -32,4 +34,28 @@ public Object getProperty(String propertyName)
}
return null;
}

private void validate(String propertyName, GetParameterResult getParameterResult)
{
String requestId = getParameterResult.getSdkResponseMetadata().getRequestId();
int statusCode = getParameterResult.getSdkHttpMetadata().getHttpStatusCode();
if (statusCode != 200) {
throw new ParameterStoreError(propertyName,
String.format("Invalid response code '%s' received from AWS. AWS Request ID : '%s'.",
statusCode,
requestId));
}

if (getParameterResult.getParameter() == null) {
throw new ParameterStoreError(propertyName,
String.format("A null Parameter was received from AWS. AWS Request ID : '%s'.",
requestId));
}

if (getParameterResult.getParameter().getValue() == null) {
throw new ParameterStoreError(propertyName,
String.format("A null Parameter value was received from AWS. AWS Request ID : '%s'.",
requestId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ public class ParameterStoreError extends Error
{
private static final long serialVersionUID = 1L;

public ParameterStoreError(String propertyName, String reason)
{
super(String.format("Accessing Parameter Store for parameter '%s' failed. %s", propertyName, reason));
}

public ParameterStoreError(String propertyName, Exception e)
{
super(String.format("Accessing Parameter Store for parameter '%s' failed.", propertyName), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.amazonaws.ResponseMetadata;
import com.amazonaws.http.SdkHttpMetadata;
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest;
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult;
Expand All @@ -28,19 +30,25 @@ public class ParameterStoreSourceTest

@Mock
private AWSSimpleSystemsManagement ssmClientMock;
@Mock
private SdkHttpMetadata sdkHttpMetadataMock;
@Mock
private ResponseMetadata responseMetadataMock;

private ParameterStoreSource parameterStoreSource;

@Before
public void setUp()
{
when(sdkHttpMetadataMock.getHttpStatusCode()).thenReturn(200);

parameterStoreSource = new ParameterStoreSource(ssmClientMock, false);
}

@Test
public void testGetProperty()
{
when(ssmClientMock.getParameter(getParameterRequest(VALID_PROPERTY_NAME))).thenReturn(new GetParameterResult().withParameter(new Parameter().withValue(VALID_PROPERTY_VALUE)));
when(ssmClientMock.getParameter(getParameterRequest(VALID_PROPERTY_NAME))).thenReturn(getGetParameterResult().withParameter(new Parameter().withValue(VALID_PROPERTY_VALUE)));

Object value = parameterStoreSource.getProperty(VALID_PROPERTY_NAME);

Expand Down Expand Up @@ -74,6 +82,42 @@ public void shouldThrowOnGetPropertyWhenNotFoundAndHaltBootIsTrue()
parameterStoreSourceHaltingBoot.getProperty(INVALID_PROPERTY_NAME);
}

@Test(expected = ParameterStoreError.class)
public void shouldThrowWhenStatusCodeIsNot200()
{
when(sdkHttpMetadataMock.getHttpStatusCode()).thenReturn(503);
when(ssmClientMock.getParameter(getParameterRequest(VALID_PROPERTY_NAME))).thenReturn(getGetParameterResult());
ParameterStoreSource parameterStoreSourceHaltingBoot = new ParameterStoreSource(ssmClientMock, true);

parameterStoreSourceHaltingBoot.getProperty(VALID_PROPERTY_NAME);
}

@Test(expected = ParameterStoreError.class)
public void shouldThrowWhenParameterIsNull()
{
when(ssmClientMock.getParameter(getParameterRequest(VALID_PROPERTY_NAME))).thenReturn(getGetParameterResult());
ParameterStoreSource parameterStoreSourceHaltingBoot = new ParameterStoreSource(ssmClientMock, true);

parameterStoreSourceHaltingBoot.getProperty(VALID_PROPERTY_NAME);
}

@Test(expected = ParameterStoreError.class)
public void shouldThrowWhenParameterValueIsNull()
{
when(ssmClientMock.getParameter(getParameterRequest(VALID_PROPERTY_NAME))).thenReturn(getGetParameterResult().withParameter(new Parameter()));
ParameterStoreSource parameterStoreSourceHaltingBoot = new ParameterStoreSource(ssmClientMock, true);

parameterStoreSourceHaltingBoot.getProperty(VALID_PROPERTY_NAME);
}

private GetParameterResult getGetParameterResult()
{
GetParameterResult getParameterResult = new GetParameterResult();
getParameterResult.setSdkHttpMetadata(sdkHttpMetadataMock);
getParameterResult.setSdkResponseMetadata(responseMetadataMock);
return getParameterResult;
}

private GetParameterRequest getParameterRequest(String parameterName)
{
return new GetParameterRequest().withName(parameterName).withWithDecryption(true);
Expand Down

0 comments on commit 180f71a

Please sign in to comment.