Skip to content

Commit

Permalink
ISSUES-603 Mask applied for logging n removed for API exec
Browse files Browse the repository at this point in the history
  • Loading branch information
nirmalchandra committed Jan 28, 2024
1 parent 721aeba commit 153a06d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public interface ZeroCodeAssertionsProcessor {

Step resolveJsonContent(Step thisStep, ScenarioExecutionState scenarioExecutionState);

String fieldMasksRemoved(String resolvedRequestJson);

String fieldMasksApplied(String resolvedRequestJson);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.jayway.jsonpath.JsonPath;
import net.minidev.json.JSONArray;
import org.apache.commons.lang.text.StrSubstitutor;
import org.jsmart.zerocode.core.domain.Step;
import org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher;
import org.jsmart.zerocode.core.engine.assertion.JsonAsserter;
import org.jsmart.zerocode.core.engine.assertion.array.ArrayIsEmptyAsserterImpl;
import org.jsmart.zerocode.core.engine.assertion.array.ArraySizeAsserterImpl;
import org.jsmart.zerocode.core.engine.assertion.field.FieldContainsStringAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldContainsStringIgnoreCaseAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldHasDateAfterValueAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldHasDateBeforeValueAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldHasEqualNumberValueAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldHasExactValueAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldHasGreaterThanValueAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldHasInEqualNumberValueAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldHasLesserThanValueAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldIsNotNullAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldIsNullAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldIsOneOfValueAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldMatchesCustomAsserter;
import org.jsmart.zerocode.core.engine.assertion.field.FieldMatchesRegexPatternAsserter;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
Expand All @@ -19,37 +41,47 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import net.minidev.json.JSONArray;
import org.apache.commons.lang.text.StrSubstitutor;
import org.jsmart.zerocode.core.domain.Step;
import org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher;
import org.jsmart.zerocode.core.engine.assertion.JsonAsserter;
import org.jsmart.zerocode.core.engine.assertion.array.ArrayIsEmptyAsserterImpl;
import org.jsmart.zerocode.core.engine.assertion.array.ArraySizeAsserterImpl;
import org.jsmart.zerocode.core.engine.assertion.field.*;
import org.jsmart.zerocode.core.utils.SmartUtils;

import static java.lang.Integer.valueOf;
import static java.lang.String.format;
import static org.apache.commons.lang.StringEscapeUtils.escapeJava;
import static org.apache.commons.lang.StringUtils.substringBetween;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.*;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_LOCAL_DATETIME_AFTER;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_LOCAL_DATETIME_BEFORE;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_PATH_SIZE;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_CONTAINS_STRING;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_CONTAINS_STRING_IGNORE_CASE;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_CUSTOM_ASSERT;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_EMPTY_ARRAY;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_EQUAL_TO_NUMBER;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_GREATER_THAN;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_IS_NOT_NULL;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_IS_NULL;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_IS_ONE_OF;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_LESSER_THAN;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_MATCHES_STRING;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_NOT_EQUAL_TO_NUMBER;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_NOT_NULL;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_NULL;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_ONE_OF;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.RAW_BODY;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.$VALUE;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.JSON_CONTENT;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.JSON_PAYLOAD_FILE;
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.YAML_PAYLOAD_FILE;
import static org.jsmart.zerocode.core.utils.FieldTypeConversionUtils.deepTypeCast;
import static org.jsmart.zerocode.core.utils.FieldTypeConversionUtils.fieldTypes;
import static org.jsmart.zerocode.core.utils.PropertiesProviderUtils.loadAbsoluteProperties;
import static org.jsmart.zerocode.core.utils.SmartUtils.checkDigNeeded;
import static org.jsmart.zerocode.core.utils.SmartUtils.getJsonFilePhToken;
import static org.jsmart.zerocode.core.utils.SmartUtils.isValidAbsolutePath;
import static org.jsmart.zerocode.core.utils.SmartUtils.readJsonAsString;
import static org.jsmart.zerocode.core.utils.SmartUtils.readYamlAsString;
import static org.jsmart.zerocode.core.utils.SmartUtils.checkDigNeeded;;
import static org.jsmart.zerocode.core.utils.SmartUtils.getJsonFilePhToken;;
import static org.jsmart.zerocode.core.utils.TokenUtils.getMasksRemoved;
import static org.jsmart.zerocode.core.utils.TokenUtils.getMasksReplaced;
import static org.jsmart.zerocode.core.utils.TokenUtils.getTestCaseTokens;
import static org.jsmart.zerocode.core.utils.TokenUtils.populateParamMap;
import static org.slf4j.LoggerFactory.getLogger;

;
;

public class ZeroCodeAssertionsProcessorImpl implements ZeroCodeAssertionsProcessor {

private static final org.slf4j.Logger LOGGER = getLogger(ZeroCodeAssertionsProcessorImpl.class);
Expand Down Expand Up @@ -380,6 +412,16 @@ public Step resolveJsonContent(Step thisStep, ScenarioExecutionState scenarioExe
}
}

@Override
public String fieldMasksRemoved(String resolvedRequestJson) {
return getMasksRemoved(resolvedRequestJson);
}

@Override
public String fieldMasksApplied(String resolvedRequestJson) {
return getMasksReplaced(resolvedRequestJson);
}

private void loadAnnotatedHostProperties() {
try {
if(isValidAbsolutePath(hostFileName)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ private String executeApi(String logPrefixRelationshipId,
// --------------------------------
url = zeroCodeAssertionsProcessor.resolveStringJson(url, scenarioExecutionState.getResolvedScenarioState());

// ------------------------------------------------
// 1) Removed the MASKED wrapper for API execution (For logging)
// 2) Replace the MASKED field with masked content (For API executions)
// ------------------------------------------------
String resolvedRequestJsonMaskRemoved = zeroCodeAssertionsProcessor.fieldMasksRemoved(resolvedRequestJson);
String resolvedRequestJsonMaskApplied = zeroCodeAssertionsProcessor.fieldMasksApplied(resolvedRequestJson);

final LocalDateTime requestTimeStamp = LocalDateTime.now();

String executionResult;
Expand All @@ -428,9 +435,9 @@ private String executeApi(String logPrefixRelationshipId,
.url(url)
.method(operationName)
.id(stepId)
.request(prettyPrintJson(resolvedRequestJson));
.request(prettyPrintJson(resolvedRequestJsonMaskApplied));

executionResult = apiExecutor.executeHttpApi(url, operationName, resolvedRequestJson);
executionResult = apiExecutor.executeHttpApi(url, operationName, resolvedRequestJsonMaskRemoved);
break;

case JAVA_CALL:
Expand All @@ -441,10 +448,10 @@ private String executeApi(String logPrefixRelationshipId,
.id(stepId)
.url(url)
.method(operationName)
.request(prettyPrintJson(resolvedRequestJson));
.request(prettyPrintJson(resolvedRequestJsonMaskApplied));

url = apiTypeUtils.getQualifiedJavaApi(url);
executionResult = apiExecutor.executeJavaOperation(url, operationName, resolvedRequestJson);
executionResult = apiExecutor.executeJavaOperation(url, operationName, resolvedRequestJsonMaskRemoved);
break;

case KAFKA_CALL:
Expand All @@ -459,10 +466,10 @@ private String executeApi(String logPrefixRelationshipId,
.url(url)
.method(operationName.toUpperCase())
.id(stepId)
.request(prettyPrintJson(resolvedRequestJson));
.request(prettyPrintJson(resolvedRequestJsonMaskApplied));

String topicName = url.substring(KAFKA_TOPIC.length());
executionResult = apiExecutor.executeKafkaService(kafkaServers, topicName, operationName, resolvedRequestJson, scenarioExecutionState);
executionResult = apiExecutor.executeKafkaService(kafkaServers, topicName, operationName, resolvedRequestJsonMaskRemoved, scenarioExecutionState);
break;

case NONE:
Expand All @@ -473,14 +480,14 @@ private String executeApi(String logPrefixRelationshipId,
.id(stepId)
.url(url)
.method(operationName)
.request(prettyPrintJson(resolvedRequestJson));
.request(prettyPrintJson(resolvedRequestJsonMaskApplied));

executionResult = prettyPrintJson(resolvedRequestJson);
break;

default:
throw new RuntimeException("Oops! API Type Undecided. If it is intentional, " +
"then keep the value as empty to receive the request in the response");
"then keep the value as empty to receive the request as response");
}

return executionResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static List<String> getTestCaseTokens(String aString) {
return keyTokens;
}

public static String getMaskedTokensReplaced(String aString) {
public static String getMasksReplaced(String aString) {
String regex = "\\$\\{MASKED:([^\\}]*)\\}";
Matcher maskMatcher = Pattern.compile(regex).matcher(aString);
while(maskMatcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.jsmart.zerocode.core.utils.TokenUtils.absolutePathOf;
import static org.jsmart.zerocode.core.utils.TokenUtils.getMaskedTokensReplaced;
import static org.jsmart.zerocode.core.utils.TokenUtils.getMasksReplaced;
import static org.jsmart.zerocode.core.utils.TokenUtils.getMasksRemoved;
import static org.jsmart.zerocode.core.utils.TokenUtils.resolveKnownTokens;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -175,22 +175,22 @@ public void testAbsolutePathOf() {

@Test
public void testGetMaskedTokensReplaced_multipleOccurrences(){
assertEquals("This is a ***masked*** message with ***masked*** tokens.", getMaskedTokensReplaced("This is a ${MASKED:secret} message with ${MASKED:masked} tokens."));
assertEquals("This is a ***masked*** message with ***masked*** tokens.", getMasksReplaced("This is a ${MASKED:secret} message with ${MASKED:masked} tokens."));
}

@Test
public void testGetMaskedTokensReplaced_noOccurrences(){
assertEquals("This string has no masked tokens.", getMaskedTokensReplaced("This string has no masked tokens."));
assertEquals("This string has no masked tokens.", getMasksReplaced("This string has no masked tokens."));
}

@Test
public void testGetMaskedTokensReplaced_emptyString(){
assertEquals("", getMaskedTokensReplaced(""));
assertEquals("", getMasksReplaced(""));
}

@Test
public void testGetMaskedTokensReplaced_specialCharacters(){
assertEquals("***masked*** and ***masked***", getMaskedTokensReplaced("${MASKED:abc@123} and ${MASKED:!@#$%^}"));
assertEquals("***masked*** and ***masked***", getMasksReplaced("${MASKED:abc@123} and ${MASKED:!@#$%^}"));
}

@Test
Expand Down

0 comments on commit 153a06d

Please sign in to comment.