make forbiddenApisTest work for client and client-sniffer

This commit is contained in:
javanna 2016-06-09 23:44:34 +02:00 committed by Luca Cavanna
parent cf6e713d77
commit f38ce72004
5 changed files with 66 additions and 86 deletions

View File

@ -51,14 +51,10 @@ forbiddenApisMain {
signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')]
} }
//TODO would be nice to just exclude the classes where we use com.sun.net.httpserver.* classes forbiddenApisTest {
//excludes don't seem to work though and we don't want to have our own @SuppressForbidden
forbiddenApisTest.enabled=false
//forbiddenApisTest {
//client does not depend on core, so only jdk signatures should be checked //client does not depend on core, so only jdk signatures should be checked
//signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')]
//} }
//JarHell is part of es core, which we don't want to pull in //JarHell is part of es core, which we don't want to pull in
jarHell.enabled=false jarHell.enabled=false

View File

@ -31,6 +31,7 @@ import org.apache.http.Consts;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.SuppressForbidden;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
@ -54,6 +55,7 @@ import java.util.Set;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
@SuppressForbidden(reason = "uses sun HttpServer")
public class HostsSnifferTests extends LuceneTestCase { public class HostsSnifferTests extends LuceneTestCase {
private int sniffRequestTimeout; private int sniffRequestTimeout;
@ -109,26 +111,37 @@ public class HostsSnifferTests extends LuceneTestCase {
} }
} }
private static HttpServer createHttpServer(final SniffResponse sniffResponse, final int sniffTimeout) throws IOException { private static HttpServer createHttpServer(final SniffResponse sniffResponse, final int sniffTimeoutMillis) throws IOException {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
httpServer.createContext("/_nodes/http", new HttpHandler() { httpServer.createContext("/_nodes/http", new ResponseHandler(sniffTimeoutMillis, sniffResponse));
@Override return httpServer;
public void handle(HttpExchange httpExchange) throws IOException { }
if (httpExchange.getRequestMethod().equals(HttpGet.METHOD_NAME)) {
if (httpExchange.getRequestURI().getRawQuery().equals("timeout=" + sniffTimeout + "ms")) { @SuppressForbidden(reason = "uses sun HttpServer")
String nodesInfoBody = sniffResponse.nodesInfoBody; private static class ResponseHandler implements HttpHandler {
httpExchange.sendResponseHeaders(sniffResponse.nodesInfoResponseCode, nodesInfoBody.length()); private final int sniffTimeoutMillis;
try (OutputStream out = httpExchange.getResponseBody()) { private final SniffResponse sniffResponse;
out.write(nodesInfoBody.getBytes(Consts.UTF_8));
return; ResponseHandler(int sniffTimeoutMillis, SniffResponse sniffResponse) {
} this.sniffTimeoutMillis = sniffTimeoutMillis;
this.sniffResponse = sniffResponse;
}
@Override
public void handle(HttpExchange httpExchange) throws IOException {
if (httpExchange.getRequestMethod().equals(HttpGet.METHOD_NAME)) {
if (httpExchange.getRequestURI().getRawQuery().equals("timeout=" + sniffTimeoutMillis + "ms")) {
String nodesInfoBody = sniffResponse.nodesInfoBody;
httpExchange.sendResponseHeaders(sniffResponse.nodesInfoResponseCode, nodesInfoBody.length());
try (OutputStream out = httpExchange.getResponseBody()) {
out.write(nodesInfoBody.getBytes(Consts.UTF_8));
return;
} }
} }
httpExchange.sendResponseHeaders(404, 0);
httpExchange.close();
} }
}); httpExchange.sendResponseHeaders(404, 0);
return httpServer; httpExchange.close();
}
} }
private static SniffResponse buildSniffResponse(HostsSniffer.Scheme scheme) throws IOException { private static SniffResponse buildSniffResponse(HostsSniffer.Scheme scheme) throws IOException {

View File

@ -49,14 +49,9 @@ forbiddenApisMain {
signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')]
} }
//TODO would be nice to just exclude the classes where we use com.sun.net.httpserver.* classes
//excludes don't seem to work though and we don't want to have our own @SuppressForbidden
forbiddenApisTest.enabled=false
forbiddenApisTest { forbiddenApisTest {
//client does not depend on core, so only jdk signatures should be checked //client does not depend on core, so only jdk signatures should be checked
signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')]
suppressAnnotations = ['**.SuppressForbidden']
} }
//JarHell is part of es core, which we don't want to pull in //JarHell is part of es core, which we don't want to pull in

View File

@ -32,6 +32,7 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.SuppressForbidden;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -83,32 +84,41 @@ public class RestClientIntegTests extends LuceneTestCase {
} }
private static void createStatusCodeContext(HttpServer httpServer, final int statusCode) { private static void createStatusCodeContext(HttpServer httpServer, final int statusCode) {
httpServer.createContext("/" + statusCode, new HttpHandler() { httpServer.createContext("/" + statusCode, new ResponseHandler(statusCode));
@Override }
public void handle(HttpExchange httpExchange) throws IOException {
StringBuilder body = new StringBuilder(); @SuppressForbidden(reason = "uses sun HttpServer")
try (InputStreamReader reader = new InputStreamReader(httpExchange.getRequestBody(), Consts.UTF_8)) { private static class ResponseHandler implements HttpHandler {
char[] buffer = new char[256]; private final int statusCode;
int read;
while ((read = reader.read(buffer)) != -1) { ResponseHandler(int statusCode) {
body.append(buffer, 0, read); this.statusCode = statusCode;
} }
@Override
public void handle(HttpExchange httpExchange) throws IOException {
StringBuilder body = new StringBuilder();
try (InputStreamReader reader = new InputStreamReader(httpExchange.getRequestBody(), Consts.UTF_8)) {
char[] buffer = new char[256];
int read;
while ((read = reader.read(buffer)) != -1) {
body.append(buffer, 0, read);
} }
Headers requestHeaders = httpExchange.getRequestHeaders();
Headers responseHeaders = httpExchange.getResponseHeaders();
for (Map.Entry<String, List<String>> header : requestHeaders.entrySet()) {
responseHeaders.put(header.getKey(), header.getValue());
}
httpExchange.getRequestBody().close();
httpExchange.sendResponseHeaders(statusCode, body.length() == 0 ? -1 : body.length());
if (body.length() > 0) {
try (OutputStream out = httpExchange.getResponseBody()) {
out.write(body.toString().getBytes(Consts.UTF_8));
}
}
httpExchange.close();
} }
}); Headers requestHeaders = httpExchange.getRequestHeaders();
Headers responseHeaders = httpExchange.getResponseHeaders();
for (Map.Entry<String, List<String>> header : requestHeaders.entrySet()) {
responseHeaders.put(header.getKey(), header.getValue());
}
httpExchange.getRequestBody().close();
httpExchange.sendResponseHeaders(statusCode, body.length() == 0 ? -1 : body.length());
if (body.length() > 0) {
try (OutputStream out = httpExchange.getResponseBody()) {
out.write(body.toString().getBytes(Consts.UTF_8));
}
}
httpExchange.close();
}
} }
@AfterClass @AfterClass

View File

@ -1,34 +0,0 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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
*
* 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 org.elasticsearch.client;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation to suppress forbidden-apis errors inside a whole class, a method, or a field.
*/
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE })
public @interface SuppressForbidden {
String reason();
}