Skip to content

Commit

Permalink
Fixed #2660
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Jan 23, 2022
1 parent e5501e1 commit be0a46b
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.thucydides.core.util.EnvironmentVariables;
import org.openqa.selenium.WebDriver;

import static net.thucydides.core.ThucydidesSystemProperty.WEBDRIVER_REMOTE_URL;


public class AfterABrowserStackScenario implements AfterAWebdriverScenario {

Expand All @@ -17,7 +19,8 @@ public void apply(EnvironmentVariables environmentVariables, TestOutcome testOut
if ((driver == null) || (!RemoteDriver.isARemoteDriver(driver)) || RemoteDriver.isStubbed(driver)) {
return;
}
if (!ThucydidesSystemProperty.WEBDRIVER_REMOTE_URL.from(environmentVariables,"").contains("browserstack")) {
String remoteUrl = EnvironmentSpecificConfiguration.from(environmentVariables).getOptionalProperty(WEBDRIVER_REMOTE_URL).orElse("");
if (!remoteUrl.contains("browserstack")) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.*;

import static net.thucydides.core.ThucydidesSystemProperty.WEBDRIVER_REMOTE_URL;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

public class BeforeABrowserStackScenario implements BeforeAWebdriverScenario {
Expand Down Expand Up @@ -54,7 +55,9 @@ public MutableCapabilities apply(EnvironmentVariables environmentVariables,
if (driver != SupportedWebDriver.REMOTE) {
return capabilities;
}
if (!ThucydidesSystemProperty.WEBDRIVER_REMOTE_URL.from(environmentVariables,"").contains("browserstack")) {

String remoteUrl = EnvironmentSpecificConfiguration.from(environmentVariables).getOptionalProperty(WEBDRIVER_REMOTE_URL).orElse("");
if (!remoteUrl.contains("browserstack")) {
return capabilities;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import net.serenitybdd.core.exceptions.SerenityManagedException;

import java.io.Serializable;

public class ErrorConvertor {

public static Throwable convertToAssertion(final Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,6 @@ private void assureTestSuiteFinished() {
}

private void cleanupTestResourcesForURI(URI uri) {
LOGGER.info("Cleanup test resources for URI " + uri);
getStepEventBus(uri).testSuiteFinished();
getStepEventBus(uri).dropAllListeners();
getStepEventBus(uri).clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ private void logOutcomesFrom(TestOutcomes testOutcomes) {
logger.info(white("-----------------------------------------"));
logger.info(white(" SERENITY TESTS: ") + colored(testOutcomes.getResult(), testOutcomes.getResult().toString()));
logger.info(white("-----------------------------------------"));
logger.info(
resultLine(white(
"Test cases executed "), white(Integer.toString(testOutcomes.getOutcomes().size()))));
logger.info(
resultLine(white(
"Tests executed "), white(Integer.toString(testOutcomes.getTotal()))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
<td width="375px" valign="top">
<div class="test-count-summary">
<div class="test-count-title">
${testOutcomes.total} test scenarios <#if (testOutcomes.hasDataDrivenTests())>
${testCount} test cases <#if (testOutcomes.hasDataDrivenTests())>
(including ${testOutcomes.totalDataRows} rows of test data)</#if>
<#if (csvReport! != '')> |
<a href="${csvReport}" title="Download CSV"> <i class="bi bi-cloud-arrow-down"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Serenity report generated ${timestamp}
${testOutcomes.totalTestScenarios} test scenarios <#if (testOutcomes.hasDataDrivenTests())>(${testOutcomes.total} tests in all, including ${testOutcomes.totalDataRows} rows of test data)</#if>

Test Cases: ${testCount} <#if (testOutcomes.hasDataDrivenTests())> (including ${testOutcomes.totalDataRows} rows of test data)</#if>
Passed: ${testOutcomes.totalScenarios.withResult("success")}
Failed: ${testOutcomes.totalScenarios.withResult("failure")}
Failed with errors: ${testOutcomes.totalScenarios.withResult("error")}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.serenitybdd.rest.filters;

import java.util.Set;

public class BlacklistFilter {
private final Set<String> blacklistedHeaders;

public BlacklistFilter(Set<String> blacklistedHeaders) {
this.blacklistedHeaders = blacklistedHeaders;
}

public String filter(String headers) {
StringBuilder filteredHeaders = new StringBuilder();
for(String headerEntry : headers.split("\n")) {
if (isBlacklisted(headerEntry.trim())) {
filteredHeaders.append(masked(headerEntry)).append("\n");
} else {
filteredHeaders.append(headerEntry).append("\n");
}
}
return filteredHeaders.toString().trim();
}

private String masked(String headerEntry) {
int headerSize = headerEntry.indexOf("=");
return headerEntry.substring(0, headerSize + 1) + "****";
}

private boolean isBlacklisted(String headerEntry) {
return blacklistedHeaders.stream().anyMatch(
header -> headerEntry.startsWith(header + "=")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ public FieldsRecordingFilter(final boolean shouldPrettyPrint, final LogDetail de
public Response filter(final FilterableRequestSpecification requestSpec,
final FilterableResponseSpecification responseSpec, final FilterContext ctx) {

BlacklistFilter blacklistFilter = new BlacklistFilter(requestSpec.getConfig().getLogConfig().blacklistedHeaders());
try (ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream recordingStream = new PrintStream(output, true, StandardCharsets.UTF_8.toString())) {
final RequestLoggingFilter filter = new RequestLoggingFilter(this.logDetail,
shouldPrettyPrint, recordingStream);
final RequestLoggingFilter filter = new RequestLoggingFilter(this.logDetail, shouldPrettyPrint, recordingStream);
final Response response = filter.filter(requestSpec, responseSpec, ctx);
recordingStream.flush();
this.recorded = new String(output.toByteArray(), StandardCharsets.UTF_8);
this.recorded = this.recorded.replaceAll("^(" +
"(Proxy:)|(Body:)|(Cookies:)|(Headers:)|(Multiparts:)|(Request path:)" +
")\\s*\\n*", "");
this.recorded = this.recorded.replaceAll("^(<none>)", "");
this.recorded = blacklistFilter.filter(this.recorded);
this.recorded = this.recorded.replaceAll("\n$", "");
return response;
} catch (UnsupportedEncodingException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.serenitybdd.rest.filters;

import org.junit.Before;
import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

public class WhenMaskingBlacklistedHeaders {

BlacklistFilter blacklistFilter;

@Before
public void setupFilter() {
Set<String> blacklist = new HashSet<>();
blacklist.add("api-key");
blacklistFilter = new BlacklistFilter(blacklist);
}

@Test
public void shouldMaskSpecifiedHeaders() {
String filtered = blacklistFilter.filter("api-key=my-api-key-value\n other=value\n Accept=*/*");

assertThat(filtered).isEqualTo("api-key=****\n other=value\n Accept=*/*");
}

@Test
public void shouldNotModifyUnmaskedHeaders() {
String filtered = blacklistFilter.filter("other=value\n Accept=*/*");

assertThat(filtered).isEqualTo("other=value\n Accept=*/*");
}

}

4 comments on commit be0a46b

@hthakursalt
Copy link

@hthakursalt hthakursalt commented on be0a46b May 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi John @wakaleo ,
I have pulled the latest serenity framework. And tried to mask blacklisted headers. It is masked in the html reports but still not masked in the logs.

//Added blacklist as below to the conf
RestAssuredConfig config = RestAssuredConfig.config().logConfig(new LogConfig().blacklistHeaders(blacklist));
request.config(config);

Logs->
image

Reports->
image

Please could you help.
Thanks in advance.

@wakaleo
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you propose a PR to make it behave the way you want? I doubt I will have time to look into this (outside of any commercial support packages of course) any time soon.

@hthakursalt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will raise a PR.
On further debugging I found the issue.

The request call for logging doesn't have blacklist. So empty list is being used as blacklist while logging. incorrect constructor is being called. Need to call the one with blacklist.

FieldsRecordingFilter.class
try {
RequestLoggingFilter filter = new RequestLoggingFilter(this.logDetail, this.shouldPrettyPrint, recordingStream);
Response response = filter.filter(requestSpec, responseSpec, ctx);
recordingStream.flush();
this.recorded = new String(output.toByteArray(), StandardCharsets.UTF_8);
this.recorded = this.recorded.replaceAll("^((Proxy:)|(Body:)|(Cookies:)|(Headers:)|(Multiparts:)|(Request path:))\s*\n*", "");
this.recorded = this.recorded.replaceAll("^()", "");
this.recorded = blacklistFilter.filter(this.recorded);
this.recorded = this.recorded.replaceAll("\n$", "");
var11 = response;

@hthakursalt
Copy link

@hthakursalt hthakursalt commented on be0a46b Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a PR have a look. @wakaleo.
#2838

Please sign in to comment.