Skip to content

Commit 180f71a

Browse files
authored
Merge pull request #8 from coveo/fix/missing-validations
Added more validations on the result received from AWS.
2 parents 8ac86f2 + d8b385e commit 180f71a

File tree

4 files changed

+85
-10
lines changed

4 files changed

+85
-10
lines changed

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.coveo</groupId>
77
<artifactId>spring-boot-parameter-store-integration</artifactId>
8-
<version>1.1.1</version>
8+
<version>1.1.2</version>
99

1010
<name>Spring Boot Parameter Store Integration</name>
1111
<description>An integration of Amazon Web Services' Systems Manager Parameter Store for Spring Boot's properties injection.</description>
@@ -41,7 +41,7 @@
4141
<dependency>
4242
<groupId>com.fasterxml.jackson</groupId>
4343
<artifactId>jackson-bom</artifactId>
44-
<version>2.9.8</version>
44+
<version>2.9.9</version>
4545
<scope>import</scope>
4646
<type>pom</type>
4747
</dependency>
@@ -52,19 +52,19 @@
5252
<dependency>
5353
<groupId>org.springframework.boot</groupId>
5454
<artifactId>spring-boot</artifactId>
55-
<version>1.5.14.RELEASE</version>
55+
<version>1.5.21.RELEASE</version>
5656
</dependency>
5757
<dependency>
5858
<groupId>com.amazonaws</groupId>
5959
<artifactId>aws-java-sdk-ssm</artifactId>
60-
<version>1.11.351</version>
60+
<version>1.11.566</version>
6161
</dependency>
6262

6363
<!-- Test libraries -->
6464
<dependency>
6565
<groupId>org.springframework.boot</groupId>
6666
<artifactId>spring-boot-starter-test</artifactId>
67-
<version>1.5.14.RELEASE</version>
67+
<version>1.5.21.RELEASE</version>
6868
<scope>test</scope>
6969
</dependency>
7070
</dependencies>

src/main/java/com/coveo/configuration/parameterstore/ParameterStoreSource.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
44
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest;
5+
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult;
56
import com.amazonaws.services.simplesystemsmanagement.model.ParameterNotFoundException;
6-
import com.coveo.configuration.parameterstore.exception.ParameterStoreParameterNotFoundError;
77
import com.coveo.configuration.parameterstore.exception.ParameterStoreError;
8+
import com.coveo.configuration.parameterstore.exception.ParameterStoreParameterNotFoundError;
89

910
public class ParameterStoreSource
1011
{
@@ -20,9 +21,10 @@ public ParameterStoreSource(AWSSimpleSystemsManagement ssmClient, boolean haltBo
2021
public Object getProperty(String propertyName)
2122
{
2223
try {
23-
return ssmClient.getParameter(new GetParameterRequest().withName(propertyName).withWithDecryption(true))
24-
.getParameter()
25-
.getValue();
24+
GetParameterResult getParameterResult = ssmClient.getParameter(new GetParameterRequest().withName(propertyName)
25+
.withWithDecryption(true));
26+
validate(propertyName, getParameterResult);
27+
return getParameterResult.getParameter().getValue();
2628
} catch (ParameterNotFoundException e) {
2729
if (haltBoot) {
2830
throw new ParameterStoreParameterNotFoundError(propertyName, e);
@@ -32,4 +34,28 @@ public Object getProperty(String propertyName)
3234
}
3335
return null;
3436
}
37+
38+
private void validate(String propertyName, GetParameterResult getParameterResult)
39+
{
40+
String requestId = getParameterResult.getSdkResponseMetadata().getRequestId();
41+
int statusCode = getParameterResult.getSdkHttpMetadata().getHttpStatusCode();
42+
if (statusCode != 200) {
43+
throw new ParameterStoreError(propertyName,
44+
String.format("Invalid response code '%s' received from AWS. AWS Request ID : '%s'.",
45+
statusCode,
46+
requestId));
47+
}
48+
49+
if (getParameterResult.getParameter() == null) {
50+
throw new ParameterStoreError(propertyName,
51+
String.format("A null Parameter was received from AWS. AWS Request ID : '%s'.",
52+
requestId));
53+
}
54+
55+
if (getParameterResult.getParameter().getValue() == null) {
56+
throw new ParameterStoreError(propertyName,
57+
String.format("A null Parameter value was received from AWS. AWS Request ID : '%s'.",
58+
requestId));
59+
}
60+
}
3561
}

src/main/java/com/coveo/configuration/parameterstore/exception/ParameterStoreError.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ public class ParameterStoreError extends Error
44
{
55
private static final long serialVersionUID = 1L;
66

7+
public ParameterStoreError(String propertyName, String reason)
8+
{
9+
super(String.format("Accessing Parameter Store for parameter '%s' failed. %s", propertyName, reason));
10+
}
11+
712
public ParameterStoreError(String propertyName, Exception e)
813
{
914
super(String.format("Accessing Parameter Store for parameter '%s' failed.", propertyName), e);

src/test/java/com/coveo/configuration/parameterstore/ParameterStoreSourceTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.mockito.Mock;
1111
import org.mockito.runners.MockitoJUnitRunner;
1212

13+
import com.amazonaws.ResponseMetadata;
14+
import com.amazonaws.http.SdkHttpMetadata;
1315
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
1416
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest;
1517
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult;
@@ -28,19 +30,25 @@ public class ParameterStoreSourceTest
2830

2931
@Mock
3032
private AWSSimpleSystemsManagement ssmClientMock;
33+
@Mock
34+
private SdkHttpMetadata sdkHttpMetadataMock;
35+
@Mock
36+
private ResponseMetadata responseMetadataMock;
3137

3238
private ParameterStoreSource parameterStoreSource;
3339

3440
@Before
3541
public void setUp()
3642
{
43+
when(sdkHttpMetadataMock.getHttpStatusCode()).thenReturn(200);
44+
3745
parameterStoreSource = new ParameterStoreSource(ssmClientMock, false);
3846
}
3947

4048
@Test
4149
public void testGetProperty()
4250
{
43-
when(ssmClientMock.getParameter(getParameterRequest(VALID_PROPERTY_NAME))).thenReturn(new GetParameterResult().withParameter(new Parameter().withValue(VALID_PROPERTY_VALUE)));
51+
when(ssmClientMock.getParameter(getParameterRequest(VALID_PROPERTY_NAME))).thenReturn(getGetParameterResult().withParameter(new Parameter().withValue(VALID_PROPERTY_VALUE)));
4452

4553
Object value = parameterStoreSource.getProperty(VALID_PROPERTY_NAME);
4654

@@ -74,6 +82,42 @@ public void shouldThrowOnGetPropertyWhenNotFoundAndHaltBootIsTrue()
7482
parameterStoreSourceHaltingBoot.getProperty(INVALID_PROPERTY_NAME);
7583
}
7684

85+
@Test(expected = ParameterStoreError.class)
86+
public void shouldThrowWhenStatusCodeIsNot200()
87+
{
88+
when(sdkHttpMetadataMock.getHttpStatusCode()).thenReturn(503);
89+
when(ssmClientMock.getParameter(getParameterRequest(VALID_PROPERTY_NAME))).thenReturn(getGetParameterResult());
90+
ParameterStoreSource parameterStoreSourceHaltingBoot = new ParameterStoreSource(ssmClientMock, true);
91+
92+
parameterStoreSourceHaltingBoot.getProperty(VALID_PROPERTY_NAME);
93+
}
94+
95+
@Test(expected = ParameterStoreError.class)
96+
public void shouldThrowWhenParameterIsNull()
97+
{
98+
when(ssmClientMock.getParameter(getParameterRequest(VALID_PROPERTY_NAME))).thenReturn(getGetParameterResult());
99+
ParameterStoreSource parameterStoreSourceHaltingBoot = new ParameterStoreSource(ssmClientMock, true);
100+
101+
parameterStoreSourceHaltingBoot.getProperty(VALID_PROPERTY_NAME);
102+
}
103+
104+
@Test(expected = ParameterStoreError.class)
105+
public void shouldThrowWhenParameterValueIsNull()
106+
{
107+
when(ssmClientMock.getParameter(getParameterRequest(VALID_PROPERTY_NAME))).thenReturn(getGetParameterResult().withParameter(new Parameter()));
108+
ParameterStoreSource parameterStoreSourceHaltingBoot = new ParameterStoreSource(ssmClientMock, true);
109+
110+
parameterStoreSourceHaltingBoot.getProperty(VALID_PROPERTY_NAME);
111+
}
112+
113+
private GetParameterResult getGetParameterResult()
114+
{
115+
GetParameterResult getParameterResult = new GetParameterResult();
116+
getParameterResult.setSdkHttpMetadata(sdkHttpMetadataMock);
117+
getParameterResult.setSdkResponseMetadata(responseMetadataMock);
118+
return getParameterResult;
119+
}
120+
77121
private GetParameterRequest getParameterRequest(String parameterName)
78122
{
79123
return new GetParameterRequest().withName(parameterName).withWithDecryption(true);

0 commit comments

Comments
 (0)