-
-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
86 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
serenity-report-resources/src/main/resources/freemarker/text-summary.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
serenity-rest-assured/src/main/java/net/serenitybdd/rest/filters/BlacklistFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "=") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...est-assured/src/test/java/net/serenitybdd/rest/filters/WhenMaskingBlacklistedHeaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=*/*"); | ||
} | ||
|
||
} |
be0a46b
There was a problem hiding this comment.
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](https://user-images.githubusercontent.com/99406982/171199713-d9abf002-f9e7-4318-94a1-8e9ffef2d978.png)
Reports->
![image](https://user-images.githubusercontent.com/99406982/171199933-e4de3fbc-db18-42c7-850e-ce5ab4cc3752.png)
Please could you help.
Thanks in advance.
be0a46b
There was a problem hiding this comment.
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.
be0a46b
There was a problem hiding this comment.
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;
be0a46b
There was a problem hiding this comment.
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