-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for loading a customAuthenticator class #1001 - needs cli…
…ent_java 1.4 (#1002) Signed-off-by: Gary Tully <[email protected]>
- Loading branch information
Showing
10 changed files
with
557 additions
and
87 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
37 changes: 37 additions & 0 deletions
37
...ests/src/test/java/io/prometheus/jmx/test/http/authentication/AuthenticatorClassTest.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,37 @@ | ||
/* | ||
* Copyright (C) 2023 The Prometheus jmx_exporter Authors | ||
* | ||
* 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 io.prometheus.jmx.test.http.authentication; | ||
|
||
import io.prometheus.jmx.test.common.AbstractExporterTest; | ||
import io.prometheus.jmx.test.common.ExporterTestEnvironment; | ||
import io.prometheus.jmx.test.support.JmxExporterMode; | ||
import java.util.stream.Stream; | ||
import org.antublue.verifyica.api.Verifyica; | ||
|
||
public class AuthenticatorClassTest extends BasicAuthenticationPlaintextTest { | ||
|
||
@Verifyica.ArgumentSupplier | ||
public static Stream<ExporterTestEnvironment> arguments() { | ||
// custom authenticator class is only on the agent classpath | ||
return AbstractExporterTest.arguments() | ||
.filter( | ||
exporterTestEnvironment -> | ||
exporterTestEnvironment | ||
.getName() | ||
.contains(JmxExporterMode.JavaAgent.name())); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...o/prometheus/jmx/test/http/authentication/AuthenticatorClassTest/JavaAgent/application.sh
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,6 @@ | ||
#!/bin/bash | ||
|
||
java \ | ||
-Xmx512M \ | ||
-javaagent:jmx_prometheus_javaagent.jar=8888:exporter.yaml \ | ||
-jar jmx_example_application.jar |
7 changes: 7 additions & 0 deletions
7
...io/prometheus/jmx/test/http/authentication/AuthenticatorClassTest/JavaAgent/exporter.yaml
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,7 @@ | ||
httpServer: | ||
authentication: | ||
customAuthenticator: | ||
authenticatorClass: io.prometheus.jmx.CustomAuthenticator | ||
subjectAttributeName: io.prometheus.jmx.CustomAuthenticatorSubjectAttribute | ||
rules: | ||
- pattern: ".*" |
56 changes: 56 additions & 0 deletions
56
...st_suite/jmx_example_application/src/main/java/io/prometheus/jmx/CustomAuthenticator.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,56 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional information regarding | ||
* copyright ownership. The ASF licenses this file to You 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 | ||
* | ||
* <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p>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 io.prometheus.jmx; | ||
|
||
import com.sun.net.httpserver.Authenticator; | ||
import com.sun.net.httpserver.Headers; | ||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpPrincipal; | ||
import com.sun.security.auth.UserPrincipal; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import javax.security.auth.Subject; | ||
|
||
public class CustomAuthenticator extends Authenticator { | ||
|
||
@Override | ||
public Result authenticate(HttpExchange exch) { | ||
// nothing too custom, so the test works, just to demonstrate that it is plug-able | ||
Headers rmap = exch.getRequestHeaders(); | ||
String auth = rmap.getFirst("Authorization"); | ||
if (auth == null) { | ||
return new Authenticator.Retry(401); | ||
} | ||
int sp = auth.indexOf(' '); | ||
if (sp == -1 || !auth.substring(0, sp).equals("Basic")) { | ||
return new Authenticator.Failure(401); | ||
} | ||
byte[] b = Base64.getDecoder().decode(auth.substring(sp + 1)); | ||
String userpass = new String(b, StandardCharsets.UTF_8); | ||
int colon = userpass.indexOf(':'); | ||
String uname = userpass.substring(0, colon); | ||
String pass = userpass.substring(colon + 1); | ||
|
||
if ("Prometheus".equals(uname) && "secret".equals(pass)) { | ||
Subject subject = new Subject(); | ||
subject.getPrincipals().add(new UserPrincipal(uname)); | ||
// to communicate an authenticated subject for subsequent handler calls via Subject.doAs | ||
exch.setAttribute("io.prometheus.jmx.CustomAuthenticatorSubjectAttribute", subject); | ||
return new Authenticator.Success(new HttpPrincipal(uname, "/")); | ||
} else { | ||
return new Authenticator.Failure(401); | ||
} | ||
} | ||
} |
Oops, something went wrong.