forked from prometheus/client_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# This is a combination of 3 commits.
# This is the 1st commit message: Added HTTP authentication to HTTPServer Signed-off-by: Doug Hoard <[email protected]> # This is the commit message #2: Added BasicAuthenticator for HTTPServer Signed-off-by: Doug Hoard <[email protected]> # This is the commit message #3: Simplified code
- Loading branch information
Showing
5 changed files
with
226 additions
and
7 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
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
38 changes: 38 additions & 0 deletions
38
..._httpserver/src/main/java/io/prometheus/client/exporter/HTTPServerBasicAuthenticator.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,38 @@ | ||
package io.prometheus.client.exporter; | ||
|
||
/** | ||
* Basic HTTP authenticator | ||
*/ | ||
public class HTTPServerBasicAuthenticator extends com.sun.net.httpserver.BasicAuthenticator { | ||
|
||
private static final String REALM = "/"; | ||
|
||
private String username; | ||
private String password; | ||
|
||
public HTTPServerBasicAuthenticator(String username, String password) { | ||
super(REALM); | ||
|
||
if ((username == null) || (username.trim().length() == 0)) { | ||
throw new IllegalArgumentException("username is null or empty"); | ||
} | ||
|
||
if ((password == null) || (password.trim().length() == 0)) { | ||
throw new IllegalArgumentException("password is null or empty"); | ||
} | ||
|
||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
/** | ||
* Check username and password | ||
* @param username | ||
* @param password | ||
* @return | ||
*/ | ||
@Override | ||
public boolean checkCredentials(String username, String password) { | ||
return (this.username.equals(username) && this.password.equals(password)); | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
...pserver/src/test/java/io/prometheus/client/exporter/TestHTTPServerBasicAuthenticator.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,68 @@ | ||
package io.prometheus.client.exporter; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Java6Assertions.assertThat; | ||
|
||
public class TestHTTPServerBasicAuthenticator { | ||
|
||
@Test | ||
public void testUsernameAndPassword() { | ||
HTTPServerBasicAuthenticator authenticator = new HTTPServerBasicAuthenticator("test", "secret"); | ||
Assert.assertTrue(authenticator.checkCredentials("test", "secret")); | ||
} | ||
|
||
@Test | ||
public void testWrongUsername() { | ||
HTTPServerBasicAuthenticator authenticator = new HTTPServerBasicAuthenticator("test", "secret"); | ||
|
||
Assert.assertFalse(authenticator.checkCredentials("wrong", "secret")); | ||
} | ||
|
||
@Test | ||
public void testWrongPassword() { | ||
HTTPServerBasicAuthenticator authenticator = new HTTPServerBasicAuthenticator("test", "secret"); | ||
Assert.assertFalse(authenticator.checkCredentials("test", "wrong")); | ||
} | ||
|
||
@Test | ||
public void testNullUsername() { | ||
try { | ||
new HTTPServerBasicAuthenticator(null, "secret"); | ||
Assert.fail("Expected IllegalArgumentException"); | ||
} catch (IllegalArgumentException e) { | ||
assertThat(e.getMessage().contains("username is null or empty")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testEmptyUsername() { | ||
try { | ||
new HTTPServerBasicAuthenticator("", "secret"); | ||
Assert.fail("Expected IllegalArgumentException"); | ||
} catch (IllegalArgumentException e) { | ||
assertThat(e.getMessage().contains("username is null or empty")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNullPassword() { | ||
try { | ||
new HTTPServerBasicAuthenticator("test", null); | ||
Assert.fail("Expected IllegalArgumentException"); | ||
} catch (IllegalArgumentException e) { | ||
assertThat(e.getMessage().contains("password is null or empty")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testEmptyPassword() { | ||
try { | ||
new HTTPServerBasicAuthenticator("test", ""); | ||
Assert.fail("Expected IllegalArgumentException"); | ||
} catch (IllegalArgumentException e) { | ||
assertThat(e.getMessage().contains("password is null or empty")); | ||
} | ||
} | ||
} |