-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix(logs): Redacted secret data in logs. * fix(logs): Redacted secret data in logs. * fix(logs): Redacted secret data in logs. * fix(logs): Redacted secret data in logs. * fix(logs): Redacted secret data in logs. (cherry picked from commit 092eff2) Co-authored-by: DanielaS12 <111055962+DanielaS12@users.noreply.github.com>
- Loading branch information
1 parent
a52e5a9
commit 19e4ef2
Showing
3 changed files
with
95 additions
and
17 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
49 changes: 49 additions & 0 deletions
49
fiat-api/src/main/java/com/netflix/spinnaker/fiat/shared/HeadersRedactor.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,49 @@ | ||
/* | ||
* Copyright 2023 Armory, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.fiat.shared; | ||
|
||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
class HeadersRedactor { | ||
|
||
private static final String WHITELIST_HEADER_PREFIX = "X-"; | ||
private static final String SECRET_DATA_VALUE = "**REDACTED**"; | ||
|
||
public Map<String, String> getRedactedHeaders(HttpServletRequest request) { | ||
Map<String, String> headers = new HashMap<>(); | ||
|
||
if (request.getHeaderNames() != null) { | ||
for (Enumeration<String> h = request.getHeaderNames(); h.hasMoreElements(); ) { | ||
String headerName = h.nextElement(); | ||
String headerValue = getRedactedHeaderValue(headerName, request.getHeader(headerName)); | ||
headers.put(headerName, headerValue); | ||
} | ||
} | ||
return headers; | ||
} | ||
|
||
private String getRedactedHeaderValue(String headerName, String headerValue) { | ||
if (!headerName.startsWith(WHITELIST_HEADER_PREFIX)) { | ||
return SECRET_DATA_VALUE; | ||
} else { | ||
return headerValue; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
fiat-api/src/test/java/com/netflix/spinnaker/fiat/shared/HeadersRedactorTest.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,44 @@ | ||
/* | ||
* Copyright 2023 Armory, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.fiat.shared; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
|
||
class HeadersRedactorTest { | ||
|
||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
HeadersRedactor unit = new HeadersRedactor(); | ||
|
||
@Test | ||
public void verifyNoSecretDataIsShownTest() { | ||
request.addHeader("Content-Type", "text/html"); | ||
request.addHeader("Authorization", "bearer token"); | ||
request.addHeader("Proxy-Authorization", "bearer token"); | ||
request.addHeader("X-Frame-Options", "DENY"); | ||
|
||
Map<String, String> result = unit.getRedactedHeaders(request); | ||
|
||
assertEquals("**REDACTED**", result.get("Content-Type")); | ||
assertEquals("**REDACTED**", result.get("Authorization")); | ||
assertEquals("**REDACTED**", result.get("Proxy-Authorization")); | ||
assertEquals("DENY", result.get("X-Frame-Options")); | ||
} | ||
} |