Skip to content

Commit ee4c2c0

Browse files
committed
Obfuscate credentials logged via FailedRequestException
1 parent 09134a1 commit ee4c2c0

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

DEVELOPMENT.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,23 @@ Beyond the unit tests, ad hoc "integration testing" can be done by running via G
66
./gradlew run --args="assert fileExists build.gradle"
77
```
88

9+
### Using IntelliJ
10+
11+
Create an "Application" run configuration, such as shown here:
12+
13+
![intellij-run-config](docs/intellij-run-config.png)
14+
15+
### Build and use application script
16+
17+
Build and install the distribution script
18+
19+
```shell
20+
./gradlew installDist
21+
```
22+
23+
Run the script using:
24+
25+
```shell
26+
./build/install/mc-image-helper/bin/mc-image-helper ...args...
27+
```
28+

docs/intellij-run-config.png

69.2 KB
Loading

src/main/java/me/itzg/helpers/http/FailedRequestException.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.netty.handler.codec.http.HttpHeaders;
44
import io.netty.handler.codec.http.HttpResponseStatus;
55
import java.net.URI;
6+
import java.net.URISyntaxException;
67
import lombok.Getter;
78
import lombok.ToString;
89

@@ -19,14 +20,23 @@ public class FailedRequestException extends RuntimeException {
1920
*/
2021
public FailedRequestException(HttpResponseStatus status, URI uri, String body, String msg, HttpHeaders headers) {
2122
super(
22-
String.format("HTTP request of %s failed with %s: %s", uri, status, msg)
23+
String.format("HTTP request of %s failed with %s: %s", obfuscate(uri), status, msg)
2324
);
2425
this.uri = uri;
2526
this.statusCode = status.code();
2627
this.body = body;
2728
this.headers = headers;
2829
}
2930

31+
public static String obfuscate(URI uri) {
32+
try {
33+
return new URI(uri.getScheme(), uri.getUserInfo() != null ? "*:*" : null, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment())
34+
.toString();
35+
} catch (URISyntaxException e) {
36+
return "???";
37+
}
38+
}
39+
3040
@SuppressWarnings("unused")
3141
public static boolean isNotFound(Throwable throwable) {
3242
return isStatus(throwable, HttpResponseStatus.NOT_FOUND);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package me.itzg.helpers.http;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.net.URI;
6+
import org.junit.jupiter.api.Test;
7+
8+
class FailedRequestExceptionTest {
9+
10+
@Test
11+
void obfuscate() {
12+
assertThat(FailedRequestException.obfuscate(
13+
URI.create("https://example.com/path?query=value")
14+
))
15+
.isEqualTo("https://example.com/path?query=value");
16+
assertThat(FailedRequestException.obfuscate(
17+
URI.create("https://user:[email protected]/path?query=value")
18+
))
19+
.isEqualTo("https://*:*@example.com/path?query=value");
20+
}
21+
}

0 commit comments

Comments
 (0)