|
18 | 18 | import static org.mockito.ArgumentMatchers.any;
|
19 | 19 | import static org.mockito.Mockito.lenient;
|
20 | 20 | import static org.mockito.Mockito.mock;
|
| 21 | +import static org.mockito.Mockito.times; |
21 | 22 | import static org.mockito.Mockito.verify;
|
22 | 23 | import com.amazonaws.services.lambda.runtime.Context;
|
23 | 24 | import com.amazonaws.services.lambda.runtime.LambdaLogger;
|
|
42 | 43 | import software.amazon.cloudformation.loggers.LogPublisher;
|
43 | 44 | import software.amazon.cloudformation.metrics.MetricsPublisher;
|
44 | 45 | import software.amazon.cloudformation.proxy.Credentials;
|
| 46 | +import software.amazon.cloudformation.proxy.HandlerErrorCode; |
45 | 47 | import software.amazon.cloudformation.proxy.OperationStatus;
|
46 | 48 | import software.amazon.cloudformation.proxy.ProgressEvent;
|
47 | 49 | import software.amazon.cloudformation.proxy.hook.HookHandlerRequest;
|
@@ -83,11 +85,16 @@ public class HookLambdaWrapperTest {
|
83 | 85 | private KMSCipher cipher;
|
84 | 86 |
|
85 | 87 | private HookLambdaWrapperOverride wrapper;
|
| 88 | + private HookLambdaWrapperOverride wrapperStrictDeserialize; |
86 | 89 |
|
87 | 90 | @BeforeEach
|
88 | 91 | public void initWrapper() {
|
89 | 92 | wrapper = new HookLambdaWrapperOverride(providerLoggingCredentialsProvider, platformEventsLogger, providerEventsLogger,
|
90 |
| - providerMetricsPublisher, validator, httpClient, cipher); |
| 93 | + providerMetricsPublisher, validator, httpClient, cipher, false); |
| 94 | + |
| 95 | + wrapperStrictDeserialize = new HookLambdaWrapperOverride(providerLoggingCredentialsProvider, platformEventsLogger, |
| 96 | + providerEventsLogger, providerMetricsPublisher, validator, |
| 97 | + httpClient, cipher, true); |
91 | 98 | }
|
92 | 99 |
|
93 | 100 | private static InputStream loadRequestStream(final String fileName) {
|
@@ -166,4 +173,161 @@ public void invokeHandler_CompleteSynchronously_returnsSuccess(final String requ
|
166 | 173 | assertThat(wrapper.callbackContext).isNull();
|
167 | 174 | }
|
168 | 175 | }
|
| 176 | + |
| 177 | + @ParameterizedTest |
| 178 | + @CsvSource({ "preCreate.request.with-resource-properties.json,CREATE_PRE_PROVISION" }) |
| 179 | + public void invokeHandler_WithResourceProperties_returnsSuccess(final String requestDataPath, |
| 180 | + final String invocationPointString) |
| 181 | + throws IOException { |
| 182 | + final HookInvocationPoint invocationPoint = HookInvocationPoint.valueOf(invocationPointString); |
| 183 | + |
| 184 | + // if the handler responds Complete, this is treated as a successful synchronous |
| 185 | + // completion |
| 186 | + final ProgressEvent<TestModel, |
| 187 | + TestContext> pe = ProgressEvent.<TestModel, TestContext>builder().status(OperationStatus.SUCCESS).build(); |
| 188 | + wrapper.setInvokeHandlerResponse(pe); |
| 189 | + |
| 190 | + lenient().when(cipher.decryptCredentials(any())).thenReturn(new Credentials("123", "123", "123")); |
| 191 | + |
| 192 | + wrapper.setTransformResponse(hookHandlerRequest); |
| 193 | + |
| 194 | + try (final InputStream in = loadRequestStream(requestDataPath); final OutputStream out = new ByteArrayOutputStream()) { |
| 195 | + final Context context = getLambdaContext(); |
| 196 | + |
| 197 | + wrapper.handleRequest(in, out, context); |
| 198 | + |
| 199 | + // verify initialiseRuntime was called and initialised dependencies |
| 200 | + verifyInitialiseRuntime(); |
| 201 | + |
| 202 | + // verify output response |
| 203 | + verifyHandlerResponse(out, |
| 204 | + HookProgressEvent.<TestContext>builder().clientRequestToken("123456").hookStatus(HookStatus.SUCCESS).build()); |
| 205 | + |
| 206 | + // assert handler receives correct injections |
| 207 | + assertThat(wrapper.awsClientProxy).isNotNull(); |
| 208 | + assertThat(wrapper.getRequest()).isEqualTo(hookHandlerRequest); |
| 209 | + assertThat(wrapper.invocationPoint).isEqualTo(invocationPoint); |
| 210 | + assertThat(wrapper.callbackContext).isNull(); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + @ParameterizedTest |
| 215 | + @CsvSource({ "preCreate.request.with-resource-properties-and-extraneous-fields.json,CREATE_PRE_PROVISION" }) |
| 216 | + public void invokeHandler_WithResourcePropertiesAndExtraneousFields_returnsSuccess(final String requestDataPath, |
| 217 | + final String invocationPointString) |
| 218 | + throws IOException { |
| 219 | + final HookInvocationPoint invocationPoint = HookInvocationPoint.valueOf(invocationPointString); |
| 220 | + |
| 221 | + // if the handler responds Complete, this is treated as a successful synchronous |
| 222 | + // completion |
| 223 | + final ProgressEvent<TestModel, |
| 224 | + TestContext> pe = ProgressEvent.<TestModel, TestContext>builder().status(OperationStatus.SUCCESS).build(); |
| 225 | + wrapper.setInvokeHandlerResponse(pe); |
| 226 | + |
| 227 | + lenient().when(cipher.decryptCredentials(any())).thenReturn(new Credentials("123", "123", "123")); |
| 228 | + |
| 229 | + wrapper.setTransformResponse(hookHandlerRequest); |
| 230 | + |
| 231 | + try (final InputStream in = loadRequestStream(requestDataPath); final OutputStream out = new ByteArrayOutputStream()) { |
| 232 | + final Context context = getLambdaContext(); |
| 233 | + |
| 234 | + wrapper.handleRequest(in, out, context); |
| 235 | + |
| 236 | + // verify initialiseRuntime was called and initialised dependencies |
| 237 | + verifyInitialiseRuntime(); |
| 238 | + |
| 239 | + // verify output response |
| 240 | + verifyHandlerResponse(out, |
| 241 | + HookProgressEvent.<TestContext>builder().clientRequestToken("123456").hookStatus(HookStatus.SUCCESS).build()); |
| 242 | + |
| 243 | + // assert handler receives correct injections |
| 244 | + assertThat(wrapper.awsClientProxy).isNotNull(); |
| 245 | + assertThat(wrapper.getRequest()).isEqualTo(hookHandlerRequest); |
| 246 | + assertThat(wrapper.invocationPoint).isEqualTo(invocationPoint); |
| 247 | + assertThat(wrapper.callbackContext).isNull(); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + @ParameterizedTest |
| 252 | + @CsvSource({ "preCreate.request.with-resource-properties.json,CREATE_PRE_PROVISION" }) |
| 253 | + public void invokeHandler_StrictDeserializer_WithResourceProperties_returnsSuccess(final String requestDataPath, |
| 254 | + final String invocationPointString) |
| 255 | + throws IOException { |
| 256 | + final HookInvocationPoint invocationPoint = HookInvocationPoint.valueOf(invocationPointString); |
| 257 | + |
| 258 | + // if the handler responds Complete, this is treated as a successful synchronous |
| 259 | + // completion |
| 260 | + final ProgressEvent<TestModel, |
| 261 | + TestContext> pe = ProgressEvent.<TestModel, TestContext>builder().status(OperationStatus.SUCCESS).build(); |
| 262 | + wrapperStrictDeserialize.setInvokeHandlerResponse(pe); |
| 263 | + |
| 264 | + lenient().when(cipher.decryptCredentials(any())).thenReturn(new Credentials("123", "123", "123")); |
| 265 | + |
| 266 | + wrapperStrictDeserialize.setTransformResponse(hookHandlerRequest); |
| 267 | + |
| 268 | + try (final InputStream in = loadRequestStream(requestDataPath); final OutputStream out = new ByteArrayOutputStream()) { |
| 269 | + final Context context = getLambdaContext(); |
| 270 | + |
| 271 | + wrapperStrictDeserialize.handleRequest(in, out, context); |
| 272 | + |
| 273 | + // verify initialiseRuntime was called and initialised dependencies |
| 274 | + verifyInitialiseRuntime(); |
| 275 | + |
| 276 | + // verify output response |
| 277 | + verifyHandlerResponse(out, |
| 278 | + HookProgressEvent.<TestContext>builder().clientRequestToken("123456").hookStatus(HookStatus.SUCCESS).build()); |
| 279 | + |
| 280 | + // assert handler receives correct injections |
| 281 | + assertThat(wrapperStrictDeserialize.awsClientProxy).isNotNull(); |
| 282 | + assertThat(wrapperStrictDeserialize.getRequest()).isEqualTo(hookHandlerRequest); |
| 283 | + assertThat(wrapperStrictDeserialize.invocationPoint).isEqualTo(invocationPoint); |
| 284 | + assertThat(wrapperStrictDeserialize.callbackContext).isNull(); |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + @ParameterizedTest |
| 289 | + @CsvSource({ "preCreate.request.with-resource-properties-and-extraneous-fields.json" }) |
| 290 | + public void |
| 291 | + invokeHandler_StrictDeserializer_WithResourcePropertiesAndExtraneousFields_returnsFailure(final String requestDataPath) |
| 292 | + throws IOException { |
| 293 | + // if the handler responds Complete, this is treated as a successful synchronous |
| 294 | + // completion |
| 295 | + final ProgressEvent<TestModel, |
| 296 | + TestContext> pe = ProgressEvent.<TestModel, TestContext>builder().status(OperationStatus.SUCCESS).build(); |
| 297 | + wrapperStrictDeserialize.setInvokeHandlerResponse(pe); |
| 298 | + |
| 299 | + lenient().when(cipher.decryptCredentials(any())).thenReturn(new Credentials("123", "123", "123")); |
| 300 | + |
| 301 | + wrapperStrictDeserialize.setTransformResponse(hookHandlerRequest); |
| 302 | + |
| 303 | + try (final InputStream in = loadRequestStream(requestDataPath); final OutputStream out = new ByteArrayOutputStream()) { |
| 304 | + final Context context = getLambdaContext(); |
| 305 | + |
| 306 | + wrapperStrictDeserialize.handleRequest(in, out, context); |
| 307 | + |
| 308 | + // verify initialiseRuntime was called and initialised dependencies |
| 309 | + verify(providerLoggingCredentialsProvider, times(0)).setCredentials(any(Credentials.class)); |
| 310 | + verify(providerMetricsPublisher, times(0)).refreshClient(); |
| 311 | + |
| 312 | + // verify output response |
| 313 | + verifyHandlerResponse(out, |
| 314 | + HookProgressEvent.<TestContext>builder().clientRequestToken(null).hookStatus(HookStatus.FAILED) |
| 315 | + .errorCode(HandlerErrorCode.InternalFailure).callbackContext(null) |
| 316 | + .message(expectedStringWhenStrictDeserializingWithExtraneousFields).build()); |
| 317 | + |
| 318 | + // assert handler receives correct injections |
| 319 | + assertThat(wrapperStrictDeserialize.awsClientProxy).isNull(); |
| 320 | + assertThat(wrapperStrictDeserialize.getRequest()).isEqualTo(null); |
| 321 | + assertThat(wrapperStrictDeserialize.invocationPoint).isEqualTo(null); |
| 322 | + assertThat(wrapperStrictDeserialize.callbackContext).isNull(); |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + private final String expectedStringWhenStrictDeserializingWithExtraneousFields = "Unrecognized field \"targetName\" (class software.amazon.cloudformation.proxy.hook.HookInvocationRequest), not marked as ignorable (10 known properties: \"requestContext\", \"stackId\", \"clientRequestToken\", \"hookModel\", \"hookTypeName\", \"requestData\", \"actionInvocationPoint\", \"awsAccountId\", \"changeSetId\", \"hookTypeVersion\"])\n" |
| 327 | + + " at [Source: (String)\"{\n" + " \"clientRequestToken\": \"123456\",\n" + " \"awsAccountId\": \"123456789012\",\n" |
| 328 | + + " \"stackId\": \"arn:aws:cloudformation:us-east-1:123456789012:stack/SampleStack/e722ae60-fe62-11e8-9a0e-0ae8cc519968\",\n" |
| 329 | + + " \"changeSetId\": \"arn:aws:cloudformation:us-east-1:123456789012:changeSet/SampleChangeSet-conditional/1a2345b6-0000-00a0-a123-00abc0abc000\",\n" |
| 330 | + + " \"hookTypeName\": \"AWS::Test::TestModel\",\n" + " \"hookTypeVersion\": \"1.0\",\n" + " \"hookModel\": {\n" |
| 331 | + + " \"property1\": \"abc\",\n" + " \"property2\": 123\n" + " },\n" |
| 332 | + + " \"action\"[truncated 1935 chars]; line: 40, column: 20] (through reference chain: software.amazon.cloudformation.proxy.hook.HookInvocationRequest[\"targetName\"])"; |
169 | 333 | }
|
0 commit comments