-
Notifications
You must be signed in to change notification settings - Fork 805
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http server, add subject.doAs handler wrapper for exchange attribute …
…configured via authenticatedSubjectAttributeName #1088 (#1089) Signed-off-by: Gary Tully <[email protected]> Co-authored-by: Gregor Zeitlinger <[email protected]>
- Loading branch information
1 parent
5973270
commit d376a59
Showing
2 changed files
with
153 additions
and
6 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
79 changes: 79 additions & 0 deletions
79
...er-httpserver/src/test/java/io/prometheus/metrics/exporter/httpserver/HTTPServerTest.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,79 @@ | ||
package io.prometheus.metrics.exporter.httpserver; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.sun.net.httpserver.Authenticator; | ||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpPrincipal; | ||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.Socket; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.AccessController; | ||
import java.security.Principal; | ||
import javax.security.auth.Subject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class HTTPServerTest { | ||
|
||
@Test | ||
public void testSubjectDoAs() throws Exception { | ||
|
||
final String user = "joe"; | ||
final Subject subject = new Subject(); | ||
subject.getPrincipals().add(() -> (user)); | ||
|
||
Authenticator authenticator = | ||
new Authenticator() { | ||
@Override | ||
public Result authenticate(HttpExchange exchange) { | ||
exchange.setAttribute("aa", subject); | ||
return new Success(new HttpPrincipal(user, "/")); | ||
} | ||
}; | ||
|
||
HttpHandler handler = | ||
exchange -> { | ||
boolean found = false; | ||
Subject current = Subject.getSubject(AccessController.getContext()); | ||
for (Principal p : current.getPrincipals()) { | ||
if (user.equals(p.getName())) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
if (!found) { | ||
throw new IOException("Expected validated user joe!"); | ||
} | ||
exchange.sendResponseHeaders(204, -1); | ||
}; | ||
HTTPServer server = | ||
HTTPServer.builder() | ||
.port(0) | ||
.authenticator(authenticator) | ||
.defaultHandler(handler) | ||
.authenticatedSubjectAttributeName("aa") | ||
.buildAndStart(); | ||
|
||
Socket socket = new Socket(); | ||
try { | ||
socket.connect(new InetSocketAddress("localhost", server.getPort())); | ||
|
||
socket.getOutputStream().write("GET / HTTP/1.1 \r\n".getBytes(StandardCharsets.UTF_8)); | ||
socket.getOutputStream().write("HOST: localhost \r\n\r\n".getBytes(StandardCharsets.UTF_8)); | ||
socket.getOutputStream().flush(); | ||
|
||
String actualResponse = ""; | ||
byte[] resp = new byte[500]; | ||
int read = socket.getInputStream().read(resp, 0, resp.length); | ||
if (read > 0) { | ||
actualResponse = new String(resp, 0, read); | ||
} | ||
assertThat(actualResponse).contains("204"); | ||
|
||
} finally { | ||
socket.close(); | ||
} | ||
} | ||
} |