diff --git a/python/rpdk/java/templates/generate/hook/BaseHookConfiguration.java b/python/rpdk/java/templates/generate/hook/BaseHookConfiguration.java index ad960492..797ee057 100644 --- a/python/rpdk/java/templates/generate/hook/BaseHookConfiguration.java +++ b/python/rpdk/java/templates/generate/hook/BaseHookConfiguration.java @@ -44,7 +44,7 @@ public JSONObject targetSchemaJSONObject(final String targetName) { new JSONTokener( this.targetSchemas.computeIfAbsent( targetName, - tn -> this.getClass().getClassLoader().getResourceAsStream(tn) + tn -> this.getClass().getClassLoader().getResourceAsStream(this.targetSchemaPaths.get(tn)) ) ) ); diff --git a/src/main/java/software/amazon/cloudformation/HookAbstractWrapper.java b/src/main/java/software/amazon/cloudformation/HookAbstractWrapper.java index 64892918..8a9bcbbd 100644 --- a/src/main/java/software/amazon/cloudformation/HookAbstractWrapper.java +++ b/src/main/java/software/amazon/cloudformation/HookAbstractWrapper.java @@ -35,7 +35,6 @@ import software.amazon.awssdk.http.HttpStatusFamily; import software.amazon.awssdk.http.SdkHttpClient; import software.amazon.awssdk.http.apache.ApacheHttpClient; -import software.amazon.awssdk.utils.StringUtils; import software.amazon.cloudformation.encryption.Cipher; import software.amazon.cloudformation.encryption.KMSCipher; import software.amazon.cloudformation.exceptions.BaseHandlerException; @@ -67,7 +66,6 @@ import software.amazon.cloudformation.resource.SchemaValidator; import software.amazon.cloudformation.resource.Serializer; import software.amazon.cloudformation.resource.Validator; -import software.amazon.cloudformation.resource.exceptions.ValidationException; public abstract class HookAbstractWrapper { @@ -90,7 +88,6 @@ public abstract class HookAbstractWrapper { final SchemaValidator validator; final TypeReference> typeReference; - private MetricsPublisher platformMetricsPublisher; private MetricsPublisher providerMetricsPublisher; private CloudWatchLogHelper cloudWatchLogHelper; @@ -112,7 +109,6 @@ protected HookAbstractWrapper() { public HookAbstractWrapper(final CredentialsProvider providerCredentialsProvider, final CloudWatchLogPublisher providerEventsLogger, final LogPublisher platformEventsLogger, - final MetricsPublisher platformMetricsPublisher, final MetricsPublisher providerMetricsPublisher, final SchemaValidator validator, final Serializer serializer, @@ -123,7 +119,6 @@ public HookAbstractWrapper(final CredentialsProvider providerCredentialsProvider this.cloudWatchLogsProvider = new CloudWatchLogsProvider(this.providerCredentialsProvider, httpClient); this.providerEventsLogger = providerEventsLogger; this.platformLogPublisher = platformEventsLogger; - this.platformMetricsPublisher = platformMetricsPublisher; this.providerMetricsPublisher = providerMetricsPublisher; this.serializer = serializer; this.validator = validator; @@ -147,22 +142,14 @@ private void initialiseRuntime(final String hookTypeName, this.loggerProxy = new LoggerProxy(); this.loggerProxy.addLogPublisher(this.platformLogPublisher); + // Initialisation skipped if dependencies were set during injection (in unit + // tests). + // Initialize a KMS cipher to decrypt customer credentials in HookRequestData if (this.cipher == null && hookEncryptionKeyArn != null && hookEncryptionKeyRole != null) { this.cipher = new KMSCipher(hookEncryptionKeyArn, hookEncryptionKeyRole); } - // Initialisation skipped if dependencies were set during injection (in unit - // tests). - // e.g. "if (this.platformMetricsPublisher == null)" - if (this.platformMetricsPublisher == null) { - // platformMetricsPublisher needs aws account id to differentiate metrics - // namespace - this.platformMetricsPublisher = new HookMetricsPublisherImpl(this.platformLoggerProxy, awsAccountId, hookTypeName); - } - this.metricsPublisherProxy.addMetricsPublisher(this.platformMetricsPublisher); - this.platformMetricsPublisher.refreshClient(); - // NOTE: providerCredentials and providerLogGroupName are null/not null in // sync. // Both are required parameters when LoggingConfig (optional) is provided when @@ -210,19 +197,6 @@ public void processRequest(final InputStream inputStream, final OutputStream out // deserialize incoming payload to modeled request request = this.serializer.deserialize(input, typeReference); handlerResponse = processInvocation(rawInput, request); - } catch (final ValidationException e) { - String message; - String fullExceptionMessage = ValidationException.buildFullExceptionMessage(e); - if (!StringUtils.isEmpty(fullExceptionMessage)) { - message = String.format("Model validation failed (%s)", fullExceptionMessage); - } else { - message = "Model validation failed with unknown cause."; - } - - handlerResponse = ProgressEvent.defaultFailureHandler(new TerminalException(message, e), - HandlerErrorCode.InvalidRequest); - publishExceptionMetric(request != null ? request.getActionInvocationPoint() : null, e, - HandlerErrorCode.InvalidRequest); } catch (final Throwable e) { // Exceptions are wrapped as a consistent error response to the caller (i.e; // CloudFormation) diff --git a/src/main/java/software/amazon/cloudformation/HookExecutableWrapper.java b/src/main/java/software/amazon/cloudformation/HookExecutableWrapper.java index bd389d52..87e7ddf9 100644 --- a/src/main/java/software/amazon/cloudformation/HookExecutableWrapper.java +++ b/src/main/java/software/amazon/cloudformation/HookExecutableWrapper.java @@ -44,14 +44,13 @@ public HookExecutableWrapper() { public HookExecutableWrapper(final CredentialsProvider providerCredentialsProvider, final CloudWatchLogPublisher providerEventsLogger, final LogPublisher platformEventsLogger, - final MetricsPublisher platformMetricsPublisher, final MetricsPublisher providerMetricsPublisher, final SchemaValidator validator, final Serializer serializer, final SdkHttpClient httpClient, final Cipher cipher) { - super(providerCredentialsProvider, providerEventsLogger, platformEventsLogger, platformMetricsPublisher, - providerMetricsPublisher, validator, serializer, httpClient, cipher); + super(providerCredentialsProvider, providerEventsLogger, platformEventsLogger, providerMetricsPublisher, validator, + serializer, httpClient, cipher); } public void handleRequest(final InputStream inputStream, final OutputStream outputStream) throws IOException, diff --git a/src/main/java/software/amazon/cloudformation/HookLambdaWrapper.java b/src/main/java/software/amazon/cloudformation/HookLambdaWrapper.java index ab106848..d43c20bf 100644 --- a/src/main/java/software/amazon/cloudformation/HookLambdaWrapper.java +++ b/src/main/java/software/amazon/cloudformation/HookLambdaWrapper.java @@ -43,14 +43,13 @@ public HookLambdaWrapper() { public HookLambdaWrapper(final CredentialsProvider providerCredentialsProvider, final CloudWatchLogPublisher providerEventsLogger, final LogPublisher platformEventsLogger, - final MetricsPublisher platformMetricsPublisher, final MetricsPublisher providerMetricsPublisher, final SchemaValidator validator, final Serializer serializer, final SdkHttpClient httpClient, final Cipher cipher) { - super(providerCredentialsProvider, providerEventsLogger, platformEventsLogger, platformMetricsPublisher, - providerMetricsPublisher, validator, serializer, httpClient, cipher); + super(providerCredentialsProvider, providerEventsLogger, platformEventsLogger, providerMetricsPublisher, validator, + serializer, httpClient, cipher); } @Override diff --git a/src/main/java/software/amazon/cloudformation/metrics/HookMetricsPublisherImpl.java b/src/main/java/software/amazon/cloudformation/metrics/HookMetricsPublisherImpl.java index 6f50da41..f6e17005 100644 --- a/src/main/java/software/amazon/cloudformation/metrics/HookMetricsPublisherImpl.java +++ b/src/main/java/software/amazon/cloudformation/metrics/HookMetricsPublisherImpl.java @@ -19,8 +19,6 @@ import java.util.Collection; import java.util.EnumSet; import java.util.HashSet; -import software.amazon.awssdk.core.SdkSystemSetting; -import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.Dimension; import software.amazon.awssdk.services.cloudwatch.model.MetricDatum; @@ -32,9 +30,7 @@ import software.amazon.cloudformation.proxy.Logger; public class HookMetricsPublisherImpl extends MetricsPublisher { - private static final String DEFAULT_REGION = "us-east-1"; - - private CloudWatchProvider cloudWatchProvider; + private final CloudWatchProvider cloudWatchProvider; private Logger loggerProxy; private String awsAccountId; private CloudWatchClient cloudWatchClient; @@ -49,20 +45,9 @@ public HookMetricsPublisherImpl(final CloudWatchProvider cloudWatchProvider, this.awsAccountId = awsAccountId; } - public HookMetricsPublisherImpl(final Logger loggerProxy, - final String awsAccountId, - final String hookTypeName) { - super(hookTypeName); - this.loggerProxy = loggerProxy; - this.awsAccountId = awsAccountId; - this.cloudWatchClient = createClient(); - } - @Override public void refreshClient() { - if (cloudWatchProvider != null) { - this.cloudWatchClient = cloudWatchProvider.get(); - } + this.cloudWatchClient = cloudWatchProvider.get(); } private String getHookTypeName() { @@ -166,9 +151,4 @@ private void log(final String message) { } } - private CloudWatchClient createClient() { - final String region = SdkSystemSetting.AWS_REGION.getStringValue().map(Object::toString).orElse(DEFAULT_REGION); - return CloudWatchClient.builder().region(Region.of(region)).build(); - } - } diff --git a/src/test/java/software/amazon/cloudformation/HookExecutableWrapperOverride.java b/src/test/java/software/amazon/cloudformation/HookExecutableWrapperOverride.java index d22f73bd..32c624eb 100644 --- a/src/test/java/software/amazon/cloudformation/HookExecutableWrapperOverride.java +++ b/src/test/java/software/amazon/cloudformation/HookExecutableWrapperOverride.java @@ -50,13 +50,12 @@ public class HookExecutableWrapperOverride extends HookExecutableWrapperbuilder().clientRequestToken("123456").hookStatus(HookStatus.SUCCESS).build()); diff --git a/src/test/java/software/amazon/cloudformation/HookLambdaWrapperOverride.java b/src/test/java/software/amazon/cloudformation/HookLambdaWrapperOverride.java index cc748458..d4d07d37 100644 --- a/src/test/java/software/amazon/cloudformation/HookLambdaWrapperOverride.java +++ b/src/test/java/software/amazon/cloudformation/HookLambdaWrapperOverride.java @@ -50,13 +50,12 @@ public class HookLambdaWrapperOverride extends HookLambdaWrapperbuilder().clientRequestToken("123456").hookStatus(HookStatus.SUCCESS).build()); diff --git a/src/test/java/software/amazon/cloudformation/HookWrapperOverride.java b/src/test/java/software/amazon/cloudformation/HookWrapperOverride.java index 90c7873b..7565fb3a 100644 --- a/src/test/java/software/amazon/cloudformation/HookWrapperOverride.java +++ b/src/test/java/software/amazon/cloudformation/HookWrapperOverride.java @@ -66,13 +66,12 @@ public HookWrapperOverride(final LogPublisher platformEventsLogger, public HookWrapperOverride(final CredentialsProvider providerLoggingCredentialsProvider, final LogPublisher platformEventsLogger, final CloudWatchLogPublisher providerEventsLogger, - final MetricsPublisher platformMetricsPublisher, final MetricsPublisher providerMetricsPublisher, final SchemaValidator validator, final SdkHttpClient httpClient, final Cipher cipher) { - super(providerLoggingCredentialsProvider, providerEventsLogger, platformEventsLogger, platformMetricsPublisher, - providerMetricsPublisher, validator, new Serializer(), httpClient, cipher); + super(providerLoggingCredentialsProvider, providerEventsLogger, platformEventsLogger, providerMetricsPublisher, validator, + new Serializer(), httpClient, cipher); } @Override diff --git a/src/test/java/software/amazon/cloudformation/HookWrapperTest.java b/src/test/java/software/amazon/cloudformation/HookWrapperTest.java index 7a69b15d..512a1416 100644 --- a/src/test/java/software/amazon/cloudformation/HookWrapperTest.java +++ b/src/test/java/software/amazon/cloudformation/HookWrapperTest.java @@ -73,9 +73,6 @@ public class HookWrapperTest { @Mock private CredentialsProvider providerLoggingCredentialsProvider; - @Mock - private MetricsPublisher platformMetricsPublisher; - @Mock private MetricsPublisher providerMetricsPublisher; @@ -102,7 +99,7 @@ public class HookWrapperTest { @BeforeEach public void initWrapper() { wrapper = new HookWrapperOverride(providerLoggingCredentialsProvider, platformEventsLogger, providerEventsLogger, - platformMetricsPublisher, providerMetricsPublisher, validator, httpClient, cipher); + providerMetricsPublisher, validator, httpClient, cipher); } private static InputStream loadRequestStream(final String fileName) { @@ -118,7 +115,6 @@ private static InputStream loadRequestStream(final String fileName) { private void verifyInitialiseRuntime() { verify(providerLoggingCredentialsProvider).setCredentials(any(Credentials.class)); - verify(platformMetricsPublisher).refreshClient(); verify(providerMetricsPublisher).refreshClient(); } @@ -156,14 +152,10 @@ public void invokeHandler_nullResponse_returnsFailure(final String requestDataPa verifyInitialiseRuntime(); // validation failure metric should be published for final error handling - verify(platformMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), - any(TerminalException.class), any(HandlerErrorCode.class)); verify(providerMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), any(TerminalException.class), any(HandlerErrorCode.class)); // all metrics should be published even on terminal failure - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); @@ -200,20 +192,17 @@ public void invokeHandler_without_customerLoggingCredentials(final String reques wrapper.processRequest(in, out); - // verify initialiseRuntime was called and initialised dependencies + // verify initialiseRuntime was called and provider dependencies not setup as no + // credentials provided verify(providerLoggingCredentialsProvider, times(0)).setCredentials(any(Credentials.class)); - verify(platformMetricsPublisher).refreshClient(); verify(providerMetricsPublisher, times(0)).refreshClient(); - // validation failure metric should be published for final error handling - verify(platformMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), - any(TerminalException.class), any(HandlerErrorCode.class)); + // validation failure metric should not be published since provider metric + // publisher is not setup verify(providerMetricsPublisher, times(0)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), any(TerminalException.class), any(HandlerErrorCode.class)); - // all metrics should be published even on terminal failure - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); + // no metrics should be published since provider metric publisher is not setup verify(providerMetricsPublisher, times(0)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); verify(providerMetricsPublisher, times(0)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); @@ -249,15 +238,6 @@ public void invokeHandler_handlerFailed_returnsFailure(final String requestDataP // verify initialiseRuntime was called and initialised dependencies verifyInitialiseRuntime(); - // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); - verify(platformMetricsPublisher, times(1)).publishExceptionByErrorCodeAndCountBulkMetrics(any(Instant.class), - any(HookInvocationPoint.class), eq(HandlerErrorCode.InternalFailure)); - - // validation failure metric should not be published - verifyNoMoreInteractions(platformMetricsPublisher); - // verify output response verifyHandlerResponse(out, HookProgressEvent.builder().clientRequestToken("123456") .errorCode(HandlerErrorCode.InternalFailure).hookStatus(HookStatus.FAILED).message("Custom Fault").build()); @@ -282,8 +262,7 @@ public void invokeHandler_withNullInput() throws IOException { try (final InputStream in = null; final OutputStream out = new ByteArrayOutputStream()) { wrapper.processRequest(in, out); - verifyNoMoreInteractions(platformMetricsPublisher, platformEventsLogger, providerMetricsPublisher, - providerEventsLogger); + verifyNoMoreInteractions(platformEventsLogger, providerMetricsPublisher, providerEventsLogger); } } @@ -312,15 +291,6 @@ public void invokeHandler_CompleteSynchronously_returnsSuccess(final String requ // verify initialiseRuntime was called and initialised dependencies verifyInitialiseRuntime(); - // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); - verify(platformMetricsPublisher, times(1)).publishExceptionByErrorCodeAndCountBulkMetrics(any(Instant.class), - any(HookInvocationPoint.class), isNull()); - - // validation failure metric should not be published - verifyNoMoreInteractions(platformMetricsPublisher); - // verify output response verifyHandlerResponse(out, HookProgressEvent.builder().clientRequestToken("123456").hookStatus(HookStatus.SUCCESS).build()); @@ -389,14 +359,10 @@ public void invokeHandler_InProgress_returnsInProgress(final String requestDataP verifyInitialiseRuntime(); // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); // verify exception and error code count published - verify(platformMetricsPublisher, times(1)).publishExceptionByErrorCodeAndCountBulkMetrics(any(Instant.class), - any(HookInvocationPoint.class), isNull()); verify(providerMetricsPublisher, times(1)).publishExceptionByErrorCodeAndCountBulkMetrics(any(Instant.class), any(HookInvocationPoint.class), isNull()); @@ -405,7 +371,6 @@ public void invokeHandler_InProgress_returnsInProgress(final String requestDataP HookProgressEvent.builder().clientRequestToken("123456").hookStatus(HookStatus.IN_PROGRESS).build()); // validation failure metric should not be published - verifyNoMoreInteractions(platformMetricsPublisher); verifyNoMoreInteractions(providerMetricsPublisher); // assert handler receives correct injections @@ -445,20 +410,14 @@ public void reInvokeHandler_InProgress_returnsInProgress(final String requestDat verifyInitialiseRuntime(); // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); - verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); // verify exception and error code count published - verify(platformMetricsPublisher, times(1)).publishExceptionByErrorCodeAndCountBulkMetrics(any(Instant.class), - any(HookInvocationPoint.class), isNull()); verify(providerMetricsPublisher, times(1)).publishExceptionByErrorCodeAndCountBulkMetrics(any(Instant.class), any(HookInvocationPoint.class), isNull()); // validation failure metric should not be published - verifyNoMoreInteractions(platformMetricsPublisher); verifyNoMoreInteractions(providerMetricsPublisher); // verify output response @@ -551,22 +510,6 @@ public void invokeHandler_withDefaultInjection_returnsInProgress() throws IOExce } } - @Test - public void invokeHandler_clientsRefreshedOnEveryInvoke() throws IOException { - try (InputStream in = loadRequestStream("preCreate.request.json"); OutputStream out = new ByteArrayOutputStream()) { - wrapper.processRequest(in, out); - } - - verify(platformMetricsPublisher, times(1)).refreshClient(); - - // invoke the same wrapper instance again to ensure client is refreshed - try (InputStream in = loadRequestStream("preCreate.request.json"); OutputStream out = new ByteArrayOutputStream()) { - wrapper.processRequest(in, out); - } - - verify(platformMetricsPublisher, times(2)).refreshClient(); - } - @Test public void invokeHandler_platformCredentialsRefreshedOnEveryInvoke() throws IOException { try (InputStream in = loadRequestStream("preCreate.request.json"); OutputStream out = new ByteArrayOutputStream()) { @@ -603,20 +546,12 @@ public void invokeHandler_throwsAmazonServiceException_returnsServiceException() verifyInitialiseRuntime(); // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); - verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION)); verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); // failure metric should be published - verify(platformMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), - any(AmazonServiceException.class), any(HandlerErrorCode.class)); - verify(providerMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), any(AmazonServiceException.class), any(HandlerErrorCode.class)); @@ -648,20 +583,12 @@ public void invokeHandler_throwsSDK2ServiceException_returnsServiceException() t verifyInitialiseRuntime(); // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); - verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION)); verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); // failure metric should be published - verify(platformMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), - any(AwsServiceException.class), any(HandlerErrorCode.class)); - verify(providerMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), any(AwsServiceException.class), any(HandlerErrorCode.class)); @@ -692,20 +619,12 @@ public void invokeHandler_throwsThrottlingException_returnsCFNThrottlingExceptio verifyInitialiseRuntime(); // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); - verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION)); verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); // failure metric should be published - verify(platformMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), - any(AmazonServiceException.class), any(HandlerErrorCode.class)); - verify(providerMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), any(AmazonServiceException.class), any(HandlerErrorCode.class)); @@ -736,20 +655,12 @@ public void invokeHandler_throwsServiceInternalError_returnsCFNServiceInternalEr verifyInitialiseRuntime(); // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); - verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION)); verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); // failure metric should be published - verify(platformMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), - any(AmazonServiceException.class), any(HandlerErrorCode.class)); - verify(providerMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), any(AmazonServiceException.class), any(HandlerErrorCode.class)); @@ -780,20 +691,12 @@ public void invokeHandler_throwsInvalidRequestError_returnsCFNInvalidRequestExce verifyInitialiseRuntime(); // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); - verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION)); verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); // failure metric should be published - verify(platformMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), - any(AmazonServiceException.class), any(HandlerErrorCode.class)); - verify(providerMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), any(AmazonServiceException.class), any(HandlerErrorCode.class)); @@ -823,20 +726,12 @@ public void invokeHandler_throwsHookNotFoundException_returnsNotFound() throws I verifyInitialiseRuntime(); // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), - eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); - verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION)); verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(HookInvocationPoint.CREATE_PRE_PROVISION), anyLong()); // failure metric should be published - verify(platformMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), - any(ResourceNotFoundException.class), any(HandlerErrorCode.class)); - verify(providerMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), any(ResourceNotFoundException.class), any(HandlerErrorCode.class)); @@ -852,9 +747,7 @@ public void invokeHandler_throwsHookNotFoundException_returnsNotFound() throws I public void invokeHandler_metricPublisherThrowable_returnsFailureResponse() throws IOException { // simulate runtime Errors in the metrics publisher (such as dependency // resolution conflicts) - doThrow(new Error("not an Exception")).when(platformMetricsPublisher).publishInvocationMetric(any(), - any(HookInvocationPoint.class)); - doThrow(new Error("not an Exception")).when(platformMetricsPublisher).publishExceptionMetric(any(), + doThrow(new Error("not an Exception")).when(providerMetricsPublisher).publishExceptionMetric(any(), any(HookInvocationPoint.class), any(), any()); lenient().when(cipher.decryptCredentials(any())).thenReturn(new Credentials("123", "123", "123")); @@ -872,15 +765,9 @@ public void invokeHandler_metricPublisherThrowable_returnsFailureResponse() thro verifyInitialiseRuntime(); // verify exception and error code count published - verify(platformMetricsPublisher, times(1)).publishExceptionByErrorCodeAndCountBulkMetrics(any(Instant.class), - any(HookInvocationPoint.class), eq(HandlerErrorCode.InternalFailure)); verify(providerMetricsPublisher, times(1)).publishExceptionByErrorCodeAndCountBulkMetrics(any(Instant.class), any(HookInvocationPoint.class), eq(HandlerErrorCode.InternalFailure)); - // no further calls to metrics publisher should occur - verifyNoMoreInteractions(platformMetricsPublisher); - verifyNoMoreInteractions(providerMetricsPublisher); - // verify output response verifyHandlerResponse(out, HookProgressEvent.builder().clientRequestToken("123456") .errorCode(HandlerErrorCode.InternalFailure).hookStatus(HookStatus.FAILED).message("not an Exception").build()); @@ -980,8 +867,8 @@ public void stringifiedPayload_validation_successful() throws IOException { // construct a valid POJO SchemaValidator validator = new Validator(); final HookWrapperOverride wrapper = new HookWrapperOverride(providerLoggingCredentialsProvider, platformEventsLogger, - providerEventsLogger, platformMetricsPublisher, - providerMetricsPublisher, validator, httpClient, cipher); + providerEventsLogger, providerMetricsPublisher, validator, + httpClient, cipher); // explicit fault response is treated as an unsuccessful synchronous completion final ProgressEvent pe = ProgressEvent.builder() @@ -1009,8 +896,8 @@ public void invokeHandler_without_encryptionKeyARN(final String requestDataPath, throws IOException { final HookInvocationPoint invocationPoint = HookInvocationPoint.valueOf(invocationPointString); final HookWrapperOverride wrapper = new HookWrapperOverride(providerLoggingCredentialsProvider, platformEventsLogger, - providerEventsLogger, platformMetricsPublisher, - providerMetricsPublisher, validator, httpClient, null); + providerEventsLogger, providerMetricsPublisher, validator, + httpClient, null); // a null response is a terminal fault wrapper.setInvokeHandlerResponse(null); @@ -1021,19 +908,16 @@ public void invokeHandler_without_encryptionKeyARN(final String requestDataPath, wrapper.processRequest(in, out); - // verify initialiseRuntime was called and initialised dependencies + // verify initialiseRuntime was called and provider dependencies not setup since + // no credentials' decryption key provided verify(providerLoggingCredentialsProvider, times(0)).setCredentials(any(Credentials.class)); - verify(platformMetricsPublisher).refreshClient(); - // validation failure metric should be published for final error handling - verify(platformMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), - any(TerminalException.class), any(HandlerErrorCode.class)); + // validation failure metric should not be published since provider metric + // publisher is not setup verify(providerMetricsPublisher, times(0)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), any(TerminalException.class), any(HandlerErrorCode.class)); - // all metrics should be published even on terminal failure - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); + // no metrics should be published since provider metric publisher is not setup verify(providerMetricsPublisher, times(0)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); verify(providerMetricsPublisher, times(0)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); @@ -1050,8 +934,8 @@ public void invokeHandler_without_encryptionKeyRole(final String requestDataPath throws IOException { final HookInvocationPoint invocationPoint = HookInvocationPoint.valueOf(invocationPointString); final HookWrapperOverride wrapper = new HookWrapperOverride(providerLoggingCredentialsProvider, platformEventsLogger, - providerEventsLogger, platformMetricsPublisher, - providerMetricsPublisher, validator, httpClient, null); + providerEventsLogger, providerMetricsPublisher, validator, + httpClient, null); // a null response is a terminal fault wrapper.setInvokeHandlerResponse(null); @@ -1062,19 +946,16 @@ public void invokeHandler_without_encryptionKeyRole(final String requestDataPath wrapper.processRequest(in, out); - // verify initialiseRuntime was called and initialised dependencies + // verify initialiseRuntime was called and provider dependencies not setup since + // no credentials' decryption key role provided verify(providerLoggingCredentialsProvider, times(0)).setCredentials(any(Credentials.class)); - verify(platformMetricsPublisher).refreshClient(); - // validation failure metric should be published for final error handling - verify(platformMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), - any(TerminalException.class), any(HandlerErrorCode.class)); + // validation failure metric should not be published since provider metric + // publisher is not setup verify(providerMetricsPublisher, times(0)).publishExceptionMetric(any(Instant.class), any(HookInvocationPoint.class), any(TerminalException.class), any(HandlerErrorCode.class)); - // all metrics should be published even on terminal failure - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); + // no metrics should be published since provider metric publisher is not setup verify(providerMetricsPublisher, times(0)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); verify(providerMetricsPublisher, times(0)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); @@ -1092,7 +973,7 @@ public void invokeHandler_with_unencrypted_credentials(final String requestDataP final HookInvocationPoint invocationPoint = HookInvocationPoint.valueOf(invocationPointString); wrapper = new HookWrapperOverride(providerLoggingCredentialsProvider, platformEventsLogger, providerEventsLogger, - platformMetricsPublisher, providerMetricsPublisher, validator, httpClient, null); + providerMetricsPublisher, validator, httpClient, null); // if the handler responds Complete, this is treated as a successful synchronous // completion @@ -1109,21 +990,8 @@ public void invokeHandler_with_unencrypted_credentials(final String requestDataP verify(cipher, times(0)).decryptCredentials(anyString()); verify(providerLoggingCredentialsProvider) .setCredentials(eq((new Credentials("providerAccessKeyId", "providerSecretAccessKey", "providerSessionToken")))); - verify(platformMetricsPublisher).refreshClient(); verify(providerMetricsPublisher).refreshClient(); - // verify initialiseRuntime was called and initialised dependencies - // verifyInitialiseRuntime(); - - // all metrics should be published, once for a single invocation - verify(platformMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(invocationPoint)); - verify(platformMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(invocationPoint), anyLong()); - verify(platformMetricsPublisher, times(1)).publishExceptionByErrorCodeAndCountBulkMetrics(any(Instant.class), - any(HookInvocationPoint.class), isNull()); - - // validation failure metric should not be published - verifyNoMoreInteractions(platformMetricsPublisher); - // verify output response verifyHandlerResponse(out, HookProgressEvent.builder().clientRequestToken("123456").hookStatus(HookStatus.SUCCESS).build()); diff --git a/src/test/java/software/amazon/cloudformation/proxy/handler/hook/HookServiceHandlerWrapper.java b/src/test/java/software/amazon/cloudformation/proxy/handler/hook/HookServiceHandlerWrapper.java index 3436aad6..cd030b7f 100644 --- a/src/test/java/software/amazon/cloudformation/proxy/handler/hook/HookServiceHandlerWrapper.java +++ b/src/test/java/software/amazon/cloudformation/proxy/handler/hook/HookServiceHandlerWrapper.java @@ -48,15 +48,14 @@ public class HookServiceHandlerWrapper extends HookLambdaWrapper