Skip to content

Commit bbda051

Browse files
authored
Fixed custom authenticator test for ibmjava:8 (#1012)
Signed-off-by: dhoard <doug.hoard@gmail.com>
1 parent d31583b commit bbda051

File tree

2 files changed

+72
-17
lines changed

2 files changed

+72
-17
lines changed

integration_test_suite/integration_tests/src/test/java/io/prometheus/jmx/test/http/authentication/AuthenticatorClassTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import io.prometheus.jmx.test.common.ExporterTestEnvironment;
2121
import io.prometheus.jmx.test.support.JmxExporterMode;
2222
import java.util.stream.Stream;
23-
2423
import org.verifyica.api.ArgumentContext;
2524
import org.verifyica.api.Verifyica;
2625

integration_test_suite/jmx_example_application/src/main/java/io/prometheus/jmx/CustomAuthenticator.java

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,95 @@
1818
import com.sun.net.httpserver.Headers;
1919
import com.sun.net.httpserver.HttpExchange;
2020
import com.sun.net.httpserver.HttpPrincipal;
21-
import com.sun.security.auth.UserPrincipal;
21+
import java.io.Serializable;
2222
import java.nio.charset.StandardCharsets;
23+
import java.security.Principal;
2324
import java.util.Base64;
25+
import java.util.Objects;
2426
import javax.security.auth.Subject;
2527

28+
/** Example custom authenticator */
2629
public class CustomAuthenticator extends Authenticator {
2730

31+
private static final String AUTHORIZATION = "Authorization";
32+
private static final String BASIC = "Basic";
33+
34+
private static final String USERNAME = "Prometheus";
35+
private static final String PASSWORD = "secret";
36+
2837
@Override
29-
public Result authenticate(HttpExchange exch) {
38+
public Result authenticate(HttpExchange httpExchange) {
3039
// nothing too custom, so the test works, just to demonstrate that it is plug-able
31-
Headers rmap = exch.getRequestHeaders();
32-
String auth = rmap.getFirst("Authorization");
33-
if (auth == null) {
40+
Headers headers = httpExchange.getRequestHeaders();
41+
42+
String authorization = headers.getFirst(AUTHORIZATION);
43+
if (authorization == null) {
3444
return new Authenticator.Retry(401);
3545
}
36-
int sp = auth.indexOf(' ');
37-
if (sp == -1 || !auth.substring(0, sp).equals("Basic")) {
46+
47+
int space = authorization.indexOf(' ');
48+
if (space == -1 || !authorization.substring(0, space).equals(BASIC)) {
3849
return new Authenticator.Failure(401);
3950
}
40-
byte[] b = Base64.getDecoder().decode(auth.substring(sp + 1));
41-
String userpass = new String(b, StandardCharsets.UTF_8);
42-
int colon = userpass.indexOf(':');
43-
String uname = userpass.substring(0, colon);
44-
String pass = userpass.substring(colon + 1);
4551

46-
if ("Prometheus".equals(uname) && "secret".equals(pass)) {
52+
byte[] usernamePasswordBytes =
53+
Base64.getDecoder().decode(authorization.substring(space + 1));
54+
String usernamePassword = new String(usernamePasswordBytes, StandardCharsets.UTF_8);
55+
int colon = usernamePassword.indexOf(':');
56+
String username = usernamePassword.substring(0, colon);
57+
String password = usernamePassword.substring(colon + 1);
58+
59+
if (USERNAME.equals(username) && PASSWORD.equals(password)) {
4760
Subject subject = new Subject();
48-
subject.getPrincipals().add(new UserPrincipal(uname));
61+
subject.getPrincipals().add(new UserPrincipal(username));
4962
// to communicate an authenticated subject for subsequent handler calls via Subject.doAs
50-
exch.setAttribute("io.prometheus.jmx.CustomAuthenticatorSubjectAttribute", subject);
51-
return new Authenticator.Success(new HttpPrincipal(uname, "/"));
63+
httpExchange.setAttribute(
64+
"io.prometheus.jmx.CustomAuthenticatorSubjectAttribute", subject);
65+
return new Authenticator.Success(new HttpPrincipal(username, "/"));
5266
} else {
5367
return new Authenticator.Failure(401);
5468
}
5569
}
70+
71+
/**
72+
* Class to implement Principal
73+
*
74+
* <p>Required for ibmjava:8 since it doesn't provide com.sun.security.auth.UserPrincipal
75+
*/
76+
public static class UserPrincipal implements Principal, Serializable {
77+
78+
private final String name;
79+
80+
/**
81+
* Constructor
82+
*
83+
* @param name name
84+
*/
85+
public UserPrincipal(String name) {
86+
this.name = Objects.requireNonNull(name, "Name cannot be null");
87+
}
88+
89+
@Override
90+
public String getName() {
91+
return name;
92+
}
93+
94+
@Override
95+
public String toString() {
96+
return "UserPrincipal{" + "name='" + name + '\'' + '}';
97+
}
98+
99+
@Override
100+
public boolean equals(Object object) {
101+
if (this == object) return true;
102+
if (object == null || getClass() != object.getClass()) return false;
103+
UserPrincipal that = (UserPrincipal) object;
104+
return Objects.equals(name, that.name);
105+
}
106+
107+
@Override
108+
public int hashCode() {
109+
return Objects.hashCode(name);
110+
}
111+
}
56112
}

0 commit comments

Comments
 (0)