From 92662a4f795ce0f1f5f65204b6fc57c36fac77b5 Mon Sep 17 00:00:00 2001 From: baubakg Date: Sat, 4 May 2024 17:14:36 +0200 Subject: [PATCH] Issue #79 Adding rules for errors when a secret has the same name as a callContent --- docs/Technical.md | 10 +++++----- .../tests/bridge/service/IntegroAPI.java | 9 +++++++++ .../tests/bridge/service/JavaCalls.java | 14 +++++++++---- .../exceptions/IBSPayloadException.java | 20 +++++++++++++++++++ .../tests/bridge/service/E2ETests.java | 17 ++++++++++++++++ .../tests/bridge/service/TestFetchCalls.java | 19 ++++++++++++++++-- 6 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/exceptions/IBSPayloadException.java diff --git a/docs/Technical.md b/docs/Technical.md index ca6efe86..0cbce4b0 100644 --- a/docs/Technical.md +++ b/docs/Technical.md @@ -4,11 +4,11 @@ This document deals with the general technical elements of the project. ## Error Handling We currently manage the following error types. -| Error Code | Exceptions | Description | -|------------|----------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------| -| 404 | NonExistentJavaObjectException , JsonProcessingException, AmbiguousMethodException, JavaObjectInaccessibleException | Errors due to client side manipulations, and payloads. | -| 408 | IBSTimeOutException | When the underlying java call takes too long to execute. | -| 500 | IBSConfigurationException, IBSRunTimeException, TargetJavaMethodCallException | Errors happening when calling the underlying Java classes. | +| Error Code | Exceptions | Description | +|------------|------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------| +| 404 | NonExistentJavaObjectException , JsonProcessingException, AmbiguousMethodException, JavaObjectInaccessibleException, IBSPayloadException | Errors due to client side manipulations, and payloads. | +| 408 | IBSTimeOutException | When the underlying java call takes too long to execute. | +| 500 | IBSConfigurationException, IBSRunTimeException, TargetJavaMethodCallException | Errors happening when calling the underlying Java classes. | ## Managing Contexts and Static Variables We have three modes of Integrity management : diff --git a/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/IntegroAPI.java b/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/IntegroAPI.java index 50aea4b0..0a4d617a 100644 --- a/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/IntegroAPI.java +++ b/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/IntegroAPI.java @@ -26,6 +26,7 @@ public class IntegroAPI { public static final String ERROR_CONTENT_TYPE = "application/problem+json"; public static final String SYSTEM_UP_MESSAGE = "All systems up"; public static final String ERROR_IBS_INTERNAL = "Internal IBS error. Please file a bug report with the project and provide this JSON in the report."; + public static final String ERROR_PAYLOAD_INCONSISTENCY = "We detected stored header ames that match a callContent."; protected static final String ERROR_JSON_TRANSFORMATION = "JSON Transformation issue : Problem processing request. The given json could not be mapped to a Java Call"; protected static final String ERROR_CALLING_JAVA_METHOD = "Error during call of target Java Class and Method."; protected static final String ERROR_JAVA_OBJECT_NOT_FOUND = "Could not find the given class or method."; @@ -93,6 +94,14 @@ public static void startServices(int port) { new ErrorObject(e, ERROR_JSON_TRANSFORMATION, statusCode))); }); + exception(IBSPayloadException.class, (e, req, res) -> { + int statusCode = 404; + res.status(statusCode); + res.type(ERROR_CONTENT_TYPE); + res.body(BridgeServiceFactory.createExceptionPayLoad( + new ErrorObject(e, ERROR_PAYLOAD_INCONSISTENCY, statusCode))); + }); + exception(AmbiguousMethodException.class, (e, req, res) -> { int statusCode = 404; res.status(statusCode); diff --git a/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/JavaCalls.java b/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/JavaCalls.java index 0259d91f..2d166cfc 100644 --- a/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/JavaCalls.java +++ b/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/JavaCalls.java @@ -206,9 +206,15 @@ public void setAssertions(Map assertions) { * @param in_mapOHeaders A map containing header values coming from the request */ public void addSecrets(Map in_mapOHeaders) { - in_mapOHeaders.keySet().stream().filter(i -> i.startsWith(ConfigValueHandlerIBS.SECRETS_FILTER_PREFIX.fetchValue())).forEach(fk -> { - this.getLocalClassLoader().getCallResultCache().put(fk, in_mapOHeaders.get(fk)); - this.getLocalClassLoader().getSecretsSet().add(fk); - }); + in_mapOHeaders.keySet().stream() + .filter(i -> i.startsWith(ConfigValueHandlerIBS.SECRETS_FILTER_PREFIX.fetchValue())).forEach(fk -> { + this.getLocalClassLoader().getCallResultCache().put(fk, in_mapOHeaders.get(fk)); + this.getLocalClassLoader().getSecretsSet().add(fk); + }); + + this.getLocalClassLoader().getSecretsSet().stream().filter(s -> this.getCallContent().keySet().contains(s)) + .anyMatch(t -> { + throw new IBSPayloadException("There is a duplicate key between the Secrets and the CallContents"); + }); } } diff --git a/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/exceptions/IBSPayloadException.java b/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/exceptions/IBSPayloadException.java new file mode 100644 index 00000000..ee006b32 --- /dev/null +++ b/integroBridgeService/src/main/java/com/adobe/campaign/tests/bridge/service/exceptions/IBSPayloadException.java @@ -0,0 +1,20 @@ +/* + * Copyright 2022 Adobe + * All Rights Reserved. + * + * NOTICE: Adobe permits you to use, modify, and distribute this file in + * accordance with the terms of the Adobe license agreement accompanying + * it. + */ +package com.adobe.campaign.tests.bridge.service.exceptions; + +/** + * This exception is thrown whenever there is an inconsistency in the payload + */ +public class IBSPayloadException extends RuntimeException { + private static final long serialVersionUID = -6489221630558571708L; + + public IBSPayloadException(String message) { + super(message); + } +} diff --git a/integroBridgeService/src/test/java/com/adobe/campaign/tests/bridge/service/E2ETests.java b/integroBridgeService/src/test/java/com/adobe/campaign/tests/bridge/service/E2ETests.java index ff8e495e..717f8f19 100644 --- a/integroBridgeService/src/test/java/com/adobe/campaign/tests/bridge/service/E2ETests.java +++ b/integroBridgeService/src/test/java/com/adobe/campaign/tests/bridge/service/E2ETests.java @@ -779,6 +779,23 @@ public void testFetchHeaders() { } + @Test(description = "Issue #78 accepting header secrets - where the header has the same name as a call content", groups = "E2E") + public void testFetchHeaders_negative() { + JavaCalls l_myJavaCall = new JavaCalls(); + + CallContent l_cc = new CallContent(); + l_cc.setClassName(SimpleStaticMethods.class.getTypeName()); + l_cc.setMethodName("methodAcceptingStringArgument"); + l_cc.setArgs(new Object[] { "IBS_HEADER_1" }); + + l_myJavaCall.getCallContent().put("IBS_HEADER_1", l_cc); + + given().body(l_myJavaCall).header("IBS_HEADER_1","REAL").post(EndPointURL + "call").then().statusCode(404) + .assertThat() + .body("title", Matchers.equalTo(IntegroAPI.ERROR_PAYLOAD_INCONSISTENCY)); + + } + @AfterGroups(groups = "E2E", alwaysRun = true) diff --git a/integroBridgeService/src/test/java/com/adobe/campaign/tests/bridge/service/TestFetchCalls.java b/integroBridgeService/src/test/java/com/adobe/campaign/tests/bridge/service/TestFetchCalls.java index 1f6e5a07..17ee6edc 100644 --- a/integroBridgeService/src/test/java/com/adobe/campaign/tests/bridge/service/TestFetchCalls.java +++ b/integroBridgeService/src/test/java/com/adobe/campaign/tests/bridge/service/TestFetchCalls.java @@ -2058,7 +2058,7 @@ public void testWithErrorAtEnvironmentValue() { //Managing headers @Test - public void testUsingHeaders() { + public void testUsingHeadersAsVariables() { Map l_headerMap = Map.of("key1", "value1", "key2", "value2"); JavaCalls l_myJavaCalls = new JavaCalls(); @@ -2077,7 +2077,7 @@ public void testUsingHeaders() { } @Test - public void testUsingHeaders_filter() { + public void testUsingHeaders_filterWhichHeadersToInclude() { ConfigValueHandlerIBS.SECRETS_FILTER_PREFIX.activate("ibs-header-"); Map l_secretsMap = Map.of("key1", "value1", "ibs-header-key2", "value2"); @@ -2090,5 +2090,20 @@ public void testUsingHeaders_filter() { assertThat("We should not have any call results yet", l_myJavaCalls.getLocalClassLoader().getSecretsSet().size(), Matchers.equalTo(1)); } + + @Test + public void testUsingHeaders_negative() { + Map l_secretsMap = Map.of("key1", "value1", "ibs-header-key2", "value2"); + + JavaCalls l_myJavaCalls = new JavaCalls(); + CallContent l_cc = new CallContent(); + l_cc.setClassName(SimpleStaticMethods.class.getTypeName()); + l_cc.setMethodName("methodReturningString"); + + l_myJavaCalls.getCallContent().put("ibs-header-key2", l_cc); + + Assert.assertThrows(IBSPayloadException.class, () -> l_myJavaCalls.addSecrets(l_secretsMap)); + + } }