|
| 1 | +package io.prometheus.client.exporter; |
| 2 | + |
| 3 | +import com.sun.net.httpserver.Authenticator; |
| 4 | +import com.sun.net.httpserver.BasicAuthenticator; |
| 5 | +import com.sun.net.httpserver.HttpServer; |
| 6 | +import io.prometheus.client.CollectorRegistry; |
| 7 | +import io.prometheus.client.Gauge; |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import javax.net.ssl.SSLContext; |
| 13 | +import javax.xml.bind.DatatypeConverter; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.UnsupportedEncodingException; |
| 16 | +import java.net.InetSocketAddress; |
| 17 | +import java.net.URL; |
| 18 | +import java.net.URLConnection; |
| 19 | +import java.util.Scanner; |
| 20 | +import java.util.zip.GZIPInputStream; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import static org.assertj.core.api.Assertions.fail; |
| 24 | + |
| 25 | +public class TestHTTPServerBasicAuthentication { |
| 26 | + |
| 27 | + private static final String HTTP_USER = "prometheus"; |
| 28 | + private static final String HTTP_PASSWORD = "some_password"; |
| 29 | + |
| 30 | + HTTPServer s; |
| 31 | + |
| 32 | + @Before |
| 33 | + public void init() throws IOException { |
| 34 | + CollectorRegistry registry = new CollectorRegistry(); |
| 35 | + Gauge.build("a", "a help").register(registry); |
| 36 | + Gauge.build("b", "a help").register(registry); |
| 37 | + Gauge.build("c", "a help").register(registry); |
| 38 | + |
| 39 | + Authenticator authenticator = new BasicAuthenticator("/") { |
| 40 | + @Override |
| 41 | + public boolean checkCredentials(String user, String password) { |
| 42 | + return HTTP_USER.equals(user) && HTTP_PASSWORD.equals(password); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + SSLContext sslContext = null; |
| 47 | + |
| 48 | + HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 3); |
| 49 | + s = new HTTPServer(httpServer, authenticator, registry, false); |
| 50 | + } |
| 51 | + |
| 52 | + @After |
| 53 | + public void cleanup() { |
| 54 | + s.stop(); |
| 55 | + } |
| 56 | + |
| 57 | + String request(String context, String suffix) throws IOException { |
| 58 | + return request(context, suffix, HTTP_USER, HTTP_PASSWORD); |
| 59 | + } |
| 60 | + |
| 61 | + String request(String context, String suffix, String user, String password) throws IOException { |
| 62 | + String url = "http://localhost:" + s.server.getAddress().getPort() + context + suffix; |
| 63 | + URLConnection connection = new URL(url).openConnection(); |
| 64 | + connection.setDoOutput(true); |
| 65 | + if (user != null && password != null) { |
| 66 | + connection.setRequestProperty("Authorization", encodeCredentials(user, password)); |
| 67 | + } |
| 68 | + connection.connect(); |
| 69 | + Scanner s = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\\A"); |
| 70 | + return s.hasNext() ? s.next() : ""; |
| 71 | + } |
| 72 | + |
| 73 | + String request(String suffix) throws IOException { |
| 74 | + return request("/metrics", suffix); |
| 75 | + } |
| 76 | + |
| 77 | + String requestWithCompression(String suffix) throws IOException { |
| 78 | + return requestWithCompression("/metrics", suffix); |
| 79 | + } |
| 80 | + |
| 81 | + String requestWithCompression(String context, String suffix) throws IOException { |
| 82 | + return requestWithCompression(context, suffix, HTTP_USER, HTTP_PASSWORD); |
| 83 | + } |
| 84 | + |
| 85 | + String requestWithCompression(String context, String suffix, String user, String password) throws IOException { |
| 86 | + String url = "http://localhost:" + s.server.getAddress().getPort() + context + suffix; |
| 87 | + URLConnection connection = new URL(url).openConnection(); |
| 88 | + connection.setDoOutput(true); |
| 89 | + connection.setDoInput(true); |
| 90 | + if (user != null && password != null) { |
| 91 | + connection.setRequestProperty("Authorization", encodeCredentials(user, password)); |
| 92 | + } |
| 93 | + connection.setRequestProperty("Accept-Encoding", "gzip, deflate"); |
| 94 | + connection.connect(); |
| 95 | + GZIPInputStream gzs = new GZIPInputStream(connection.getInputStream()); |
| 96 | + Scanner s = new Scanner(gzs).useDelimiter("\\A"); |
| 97 | + return s.hasNext() ? s.next() : ""; |
| 98 | + } |
| 99 | + |
| 100 | + String requestWithAccept(String accept) throws IOException { |
| 101 | + return requestWithAccept(accept, HTTP_USER, HTTP_PASSWORD); |
| 102 | + } |
| 103 | + |
| 104 | + String requestWithAccept(String accept, String user, String password) throws IOException { |
| 105 | + String url = "http://localhost:" + s.server.getAddress().getPort(); |
| 106 | + URLConnection connection = new URL(url).openConnection(); |
| 107 | + connection.setDoOutput(true); |
| 108 | + connection.setDoInput(true); |
| 109 | + if (user != null && password != null) { |
| 110 | + connection.setRequestProperty("Authorization", encodeCredentials(user, password)); |
| 111 | + } |
| 112 | + connection.setRequestProperty("Accept", accept); |
| 113 | + Scanner s = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\\A"); |
| 114 | + return s.hasNext() ? s.next() : ""; |
| 115 | + } |
| 116 | + |
| 117 | + private static String encodeCredentials(String user, String password) { |
| 118 | + // Per RFC4648 table 2. We support Java 6, and java.util.Base64 was only added in Java 8, |
| 119 | + try { |
| 120 | + byte[] credentialsBytes = (user + ":" + password).getBytes("UTF-8"); |
| 121 | + String encoded = DatatypeConverter.printBase64Binary(credentialsBytes); |
| 122 | + encoded = String.format("Basic %s", encoded); |
| 123 | + return encoded; |
| 124 | + } catch (UnsupportedEncodingException e) { |
| 125 | + throw new IllegalArgumentException(e); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Test(expected = IllegalArgumentException.class) |
| 130 | + public void testRefuseUsingUnbound() throws IOException { |
| 131 | + CollectorRegistry registry = new CollectorRegistry(); |
| 132 | + HTTPServer s = new HTTPServer(HttpServer.create(), registry, true); |
| 133 | + s.stop(); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testSimpleRequest() throws IOException { |
| 138 | + String response = request(""); |
| 139 | + assertThat(response).contains("a 0.0"); |
| 140 | + assertThat(response).contains("b 0.0"); |
| 141 | + assertThat(response).contains("c 0.0"); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testBadParams() throws IOException { |
| 146 | + String response = request("?x"); |
| 147 | + assertThat(response).contains("a 0.0"); |
| 148 | + assertThat(response).contains("b 0.0"); |
| 149 | + assertThat(response).contains("c 0.0"); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testSingleName() throws IOException { |
| 154 | + String response = request("?name[]=a"); |
| 155 | + assertThat(response).contains("a 0.0"); |
| 156 | + assertThat(response).doesNotContain("b 0.0"); |
| 157 | + assertThat(response).doesNotContain("c 0.0"); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testMultiName() throws IOException { |
| 162 | + String response = request("?name[]=a&name[]=b"); |
| 163 | + assertThat(response).contains("a 0.0"); |
| 164 | + assertThat(response).contains("b 0.0"); |
| 165 | + assertThat(response).doesNotContain("c 0.0"); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testDecoding() throws IOException { |
| 170 | + String response = request("?n%61me[]=%61"); |
| 171 | + assertThat(response).contains("a 0.0"); |
| 172 | + assertThat(response).doesNotContain("b 0.0"); |
| 173 | + assertThat(response).doesNotContain("c 0.0"); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void testGzipCompression() throws IOException { |
| 178 | + String response = requestWithCompression(""); |
| 179 | + assertThat(response).contains("a 0.0"); |
| 180 | + assertThat(response).contains("b 0.0"); |
| 181 | + assertThat(response).contains("c 0.0"); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testOpenMetrics() throws IOException { |
| 186 | + String response = requestWithAccept("application/openmetrics-text; version=0.0.1,text/plain;version=0.0.4;q=0.5,*/*;q=0.1"); |
| 187 | + assertThat(response).contains("# EOF"); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testHealth() throws IOException { |
| 192 | + String response = request("/-/healthy", ""); |
| 193 | + assertThat(response).contains("Exporter is Healthy"); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void testHealthGzipCompression() throws IOException { |
| 198 | + String response = requestWithCompression("/-/healthy", ""); |
| 199 | + assertThat(response).contains("Exporter is Healthy"); |
| 200 | + } |
| 201 | + |
| 202 | + @Test(expected = IOException.class) |
| 203 | + public void testHealthyBasicAuthenticationFailure() throws IOException { |
| 204 | + String response = request("/-/healthy", "", null, null); |
| 205 | + fail("Expected IOException"); |
| 206 | + } |
| 207 | +} |
0 commit comments