[TEST] add a lot of forgotten try with resources to wrap ElasticsearchResponses
Original commit: elastic/x-pack-elasticsearch@e4634ea599
This commit is contained in:
parent
4e2766df11
commit
ad9a64e854
|
@ -39,11 +39,12 @@ public class IndexAuditIT extends ESIntegTestCase {
|
|||
|
||||
public void testShieldIndexAuditTrailWorking() throws Exception {
|
||||
try (RestClient restClient = restClient()) {
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/_cluster/health", Collections.emptyMap(), null,
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/_cluster/health", Collections.emptyMap(), null,
|
||||
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
||||
UsernamePasswordToken.basicAuthHeaderValue(USER, new SecuredString(PASS.toCharArray()))));
|
||||
UsernamePasswordToken.basicAuthHeaderValue(USER, new SecuredString(PASS.toCharArray()))))) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||
}
|
||||
}
|
||||
|
||||
final AtomicReference<ClusterState> lastClusterState = new AtomicReference<>();
|
||||
final AtomicBoolean indexExists = new AtomicBoolean(false);
|
||||
|
|
|
@ -59,12 +59,13 @@ public class CustomRealmIT extends ESIntegTestCase {
|
|||
|
||||
public void testHttpAuthentication() throws Exception {
|
||||
try (RestClient restClient = restClient()) {
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null,
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null,
|
||||
new BasicHeader(CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER),
|
||||
new BasicHeader(CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW));
|
||||
new BasicHeader(CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW))) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testTransportClient() throws Exception {
|
||||
NodesInfoResponse nodeInfos = client().admin().cluster().prepareNodesInfo().get();
|
||||
|
|
|
@ -54,7 +54,8 @@ public class MarvelSettingsFilterTests extends MarvelIntegTestCase {
|
|||
} else {
|
||||
headers = new Header[0];
|
||||
}
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/_nodes/settings", Collections.emptyMap(), null, headers);
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/_nodes/settings",
|
||||
Collections.emptyMap(), null, headers)) {
|
||||
Map<String, Object> responseMap = JsonXContent.jsonXContent.createParser(response.getEntity().getContent()).map();
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> nodes = (Map<String, Object>) responseMap.get("nodes");
|
||||
|
@ -71,6 +72,7 @@ public class MarvelSettingsFilterTests extends MarvelIntegTestCase {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void assertNullSetting(Map<String, Object> settings, String setting) {
|
||||
assertThat(extractValue(setting, settings), nullValue());
|
||||
|
|
|
@ -37,15 +37,16 @@ public abstract class AbstractPrivilegeTestCase extends ShieldIntegTestCase {
|
|||
protected void assertAccessIsAllowed(String user, String method, String uri, String body,
|
||||
Map<String, String> params) throws IOException {
|
||||
try (RestClient restClient = restClient()) {
|
||||
ElasticsearchResponse response = restClient.performRequest(method, uri, params, entityOrNull(body),
|
||||
try (ElasticsearchResponse response = restClient.performRequest(method, uri, params, entityOrNull(body),
|
||||
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
||||
UsernamePasswordToken.basicAuthHeaderValue(user, new SecuredString("passwd".toCharArray()))));
|
||||
UsernamePasswordToken.basicAuthHeaderValue(user, new SecuredString("passwd".toCharArray()))))) {
|
||||
StatusLine statusLine = response.getStatusLine();
|
||||
String message = String.format(Locale.ROOT, "%s %s: Expected no error got %s %s with body %s", method, uri,
|
||||
statusLine.getStatusCode(), statusLine.getReasonPhrase(), EntityUtils.toString(response.getEntity()));
|
||||
assertThat(message, statusLine.getStatusCode(), is(not(greaterThanOrEqualTo(400))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void assertAccessIsAllowed(String user, String method, String uri, String body) throws IOException {
|
||||
assertAccessIsAllowed(user, method, uri, body, new HashMap<>());
|
||||
|
|
|
@ -81,12 +81,15 @@ public class BulkUpdateTests extends ShieldIntegTestCase {
|
|||
|
||||
try (RestClient restClient = restClient()) {
|
||||
StringEntity body = new StringEntity("{\"test\":\"test\"}", RestClient.JSON_CONTENT_TYPE);
|
||||
ElasticsearchResponse response = restClient.performRequest("PUT", path, Collections.emptyMap(), body, basicAuthHeader);
|
||||
try (ElasticsearchResponse response = restClient.performRequest("PUT", path, Collections.emptyMap(), body, basicAuthHeader)) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(201));
|
||||
}
|
||||
|
||||
response = restClient.performRequest("GET", path, Collections.emptyMap(), null, basicAuthHeader);
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", path, Collections.emptyMap(), null, basicAuthHeader)) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
assertThat(EntityUtils.toString(response.getEntity()), containsString("\"test\":\"test\""));
|
||||
}
|
||||
|
||||
|
||||
if (randomBoolean()) {
|
||||
flushAndRefresh();
|
||||
|
@ -94,14 +97,17 @@ public class BulkUpdateTests extends ShieldIntegTestCase {
|
|||
|
||||
//update with new field
|
||||
body = new StringEntity("{\"doc\": {\"not test\": \"not test\"}}", RestClient.JSON_CONTENT_TYPE);
|
||||
response = restClient.performRequest("POST", path + "/_update", Collections.emptyMap(), body, basicAuthHeader);
|
||||
try (ElasticsearchResponse response = restClient.performRequest("POST", path + "/_update",
|
||||
Collections.emptyMap(), body, basicAuthHeader)) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
}
|
||||
|
||||
response = restClient.performRequest("GET", path, Collections.emptyMap(), null, basicAuthHeader);
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", path, Collections.emptyMap(), null, basicAuthHeader)) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
String responseBody = EntityUtils.toString(response.getEntity());
|
||||
assertThat(responseBody, containsString("\"test\":\"test\""));
|
||||
assertThat(responseBody, containsString("\"not test\":\"not test\""));
|
||||
}
|
||||
|
||||
// this part is important. Without this, the document may be read from the translog which would bypass the bug where
|
||||
// FLS kicks in because the request can't be found and only returns meta fields
|
||||
|
@ -109,14 +115,17 @@ public class BulkUpdateTests extends ShieldIntegTestCase {
|
|||
|
||||
body = new StringEntity("{\"update\": {\"_index\": \"index1\", \"_type\": \"type\", \"_id\": \"1\"}}\n" +
|
||||
"{\"doc\": {\"bulk updated\":\"bulk updated\"}}\n", RestClient.JSON_CONTENT_TYPE);
|
||||
response = restClient.performRequest("POST", "/_bulk", Collections.emptyMap(), body, basicAuthHeader);
|
||||
try (ElasticsearchResponse response = restClient.performRequest("POST", "/_bulk",
|
||||
Collections.emptyMap(), body, basicAuthHeader)) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
}
|
||||
|
||||
response = restClient.performRequest("GET", path, Collections.emptyMap(), null, basicAuthHeader);
|
||||
responseBody = EntityUtils.toString(response.getEntity());
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", path, Collections.emptyMap(), null, basicAuthHeader)) {
|
||||
String responseBody = EntityUtils.toString(response.getEntity());
|
||||
assertThat(responseBody, containsString("\"test\":\"test\""));
|
||||
assertThat(responseBody, containsString("\"not test\":\"not test\""));
|
||||
assertThat(responseBody, containsString("\"bulk updated\":\"bulk updated\""));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -167,15 +167,16 @@ public class ClearRealmsCacheTests extends ShieldIntegTestCase {
|
|||
|
||||
static void executeHttpRequest(String path, Map<String, String> params) throws Exception {
|
||||
try (RestClient restClient = restClient()) {
|
||||
ElasticsearchResponse response = restClient.performRequest("POST", path, params, null,
|
||||
try (ElasticsearchResponse response = restClient.performRequest("POST", path, params, null,
|
||||
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
||||
UsernamePasswordToken.basicAuthHeaderValue(ShieldSettingsSource.DEFAULT_USER_NAME,
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))));
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))))) {
|
||||
assertNotNull(response.getEntity());
|
||||
assertTrue(EntityUtils.toString(response.getEntity()).contains("cluster_name"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Settings nodeSettings(int nodeOrdinal) {
|
||||
|
|
|
@ -138,12 +138,13 @@ public class ClearRolesCacheTests extends NativeRealmIntegTestCase {
|
|||
path = "/_xpack/security/role/" + Strings.arrayToCommaDelimitedString(rolesToClear) + "/_clear_cache";
|
||||
}
|
||||
try (RestClient restClient = restClient()) {
|
||||
ElasticsearchResponse response = restClient.performRequest("POST", path, Collections.emptyMap(), null,
|
||||
try (ElasticsearchResponse response = restClient.performRequest("POST", path, Collections.emptyMap(), null,
|
||||
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
||||
UsernamePasswordToken.basicAuthHeaderValue(ShieldSettingsSource.DEFAULT_USER_NAME,
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))));
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))))) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(RestStatus.OK.getStatus()));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
securityClient.prepareClearRolesCache().names(rolesToClear).get();
|
||||
}
|
||||
|
|
|
@ -198,9 +198,10 @@ public class LicensingTests extends ShieldIntegTestCase {
|
|||
|
||||
public void testRestAuthenticationByLicenseType() throws Exception {
|
||||
try (RestClient restClient = restClient()) {
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null);
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null)) {
|
||||
// the default of the licensing tests is basic
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||
}
|
||||
|
||||
// generate a new license with a mode that enables auth
|
||||
OperationMode mode = randomFrom(OperationMode.GOLD, OperationMode.TRIAL, OperationMode.PLATINUM, OperationMode.STANDARD);
|
||||
|
|
|
@ -44,11 +44,12 @@ public class ShieldPluginTests extends ShieldIntegTestCase {
|
|||
}
|
||||
|
||||
logger.info("executing authorized request to /_xpack infos");
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/_xpack", Collections.emptyMap(), null,
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/_xpack", Collections.emptyMap(), null,
|
||||
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
||||
basicAuthHeaderValue(ShieldSettingsSource.DEFAULT_USER_NAME,
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))));
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))))) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(OK.getStatus()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -142,14 +142,15 @@ public class RunAsIntegTests extends ShieldIntegTestCase {
|
|||
}
|
||||
|
||||
// but when running as a different user it should work
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/_nodes", Collections.emptyMap(), null,
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/_nodes", Collections.emptyMap(), null,
|
||||
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
||||
UsernamePasswordToken.basicAuthHeaderValue(RUN_AS_USER,
|
||||
SecuredStringTests.build(ShieldSettingsSource.DEFAULT_PASSWORD))),
|
||||
new BasicHeader(InternalAuthenticationService.RUN_AS_USER_HEADER, ShieldSettingsSource.DEFAULT_USER_NAME));
|
||||
new BasicHeader(InternalAuthenticationService.RUN_AS_USER_HEADER, ShieldSettingsSource.DEFAULT_USER_NAME))) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testEmptyUserImpersonationHeader() throws Exception {
|
||||
try (TransportClient client = getTransportClient(Settings.builder()
|
||||
|
|
|
@ -89,13 +89,14 @@ public class PkiOptionalClientAuthTests extends ShieldIntegTestCase {
|
|||
assertThat(e.getElasticsearchResponse().getStatusLine().getStatusCode(), is(401));
|
||||
}
|
||||
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "_nodes", Collections.emptyMap(), null,
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "_nodes", Collections.emptyMap(), null,
|
||||
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
||||
UsernamePasswordToken.basicAuthHeaderValue(ShieldSettingsSource.DEFAULT_USER_NAME,
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))));
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))))) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testTransportClientWithoutClientCertificate() {
|
||||
Transport transport = internalCluster().getDataNodeInstance(Transport.class);
|
||||
|
|
|
@ -81,11 +81,12 @@ public class PkiWithoutClientAuthenticationTests extends ShieldIntegTestCase {
|
|||
sc.init(null, trustAllCerts, new SecureRandom());
|
||||
CloseableHttpClient httpClient = HttpClients.custom().setSslcontext(sc).build();
|
||||
try (RestClient restClient = restClient(httpClient, "https")) {
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/_nodes", Collections.emptyMap(), null,
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/_nodes", Collections.emptyMap(), null,
|
||||
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
||||
UsernamePasswordToken.basicAuthHeaderValue(ShieldSettingsSource.DEFAULT_USER_NAME,
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))));
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))))) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,11 +45,12 @@ public class PkiWithoutSSLTests extends ShieldIntegTestCase {
|
|||
|
||||
public void testThatHttpWorks() throws Exception {
|
||||
try (RestClient restClient = restClient()) {
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/_nodes", Collections.emptyMap(), null,
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/_nodes", Collections.emptyMap(), null,
|
||||
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
||||
UsernamePasswordToken.basicAuthHeaderValue(ShieldSettingsSource.DEFAULT_USER_NAME,
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))));
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))))) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,9 +53,9 @@ public class RestAuthenticateActionTests extends ShieldIntegTestCase {
|
|||
|
||||
public void testAuthenticateApi() throws Exception {
|
||||
try (RestClient restClient = restClient()) {
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/_xpack/security/_authenticate", Collections.emptyMap(),
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/_xpack/security/_authenticate", Collections.emptyMap(),
|
||||
null, new BasicHeader("Authorization", basicAuthHeaderValue(ShieldSettingsSource.DEFAULT_USER_NAME,
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))));
|
||||
new SecuredString(ShieldSettingsSource.DEFAULT_PASSWORD.toCharArray()))))) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||
JsonPath jsonPath = new JsonPath(EntityUtils.toString(response.getEntity()));
|
||||
assertThat(jsonPath.evaluate("username").toString(), equalTo(ShieldSettingsSource.DEFAULT_USER_NAME));
|
||||
|
@ -65,11 +65,12 @@ public class RestAuthenticateActionTests extends ShieldIntegTestCase {
|
|||
assertThat(roles, contains(ShieldSettingsSource.DEFAULT_ROLE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testAuthenticateApiWithoutAuthentication() throws Exception {
|
||||
try (RestClient restClient = restClient()) {
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/_xpack/security/_authenticate",
|
||||
Collections.emptyMap(), null);
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/_xpack/security/_authenticate",
|
||||
Collections.emptyMap(), null)) {
|
||||
if (anonymousEnabled) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
||||
JsonPath jsonPath = new JsonPath(EntityUtils.toString(response.getEntity()));
|
||||
|
@ -89,4 +90,5 @@ public class RestAuthenticateActionTests extends ShieldIntegTestCase {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,12 +83,13 @@ public class SslClientAuthTests extends ShieldIntegTestCase {
|
|||
CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
|
||||
|
||||
try (RestClient restClient = restClient(client, "https")) {
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null,
|
||||
new BasicHeader("Authorization", basicAuthHeaderValue(transportClientUsername(), transportClientPassword())));
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/", Collections.emptyMap(), null,
|
||||
new BasicHeader("Authorization", basicAuthHeaderValue(transportClientUsername(), transportClientPassword())))) {
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
assertThat(EntityUtils.toString(response.getEntity()), containsString("You Know, for Search"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testThatTransportWorksWithoutSslClientAuth() throws Exception {
|
||||
// specify an arbitrary keystore, that does not include the certs needed to connect to the transport protocol
|
||||
|
|
|
@ -63,7 +63,8 @@ public class WatcherSettingsFilterTests extends AbstractWatcherIntegrationTestCa
|
|||
} else {
|
||||
headers = new Header[0];
|
||||
}
|
||||
ElasticsearchResponse response = restClient.performRequest("GET", "/_nodes/settings", Collections.emptyMap(), null, headers);
|
||||
try (ElasticsearchResponse response = restClient.performRequest("GET", "/_nodes/settings",
|
||||
Collections.emptyMap(), null, headers)) {
|
||||
Map<String, Object> responseMap = JsonXContent.jsonXContent.createParser(response.getEntity().getContent()).map();
|
||||
Map<String, Object> nodes = (Map<String, Object>) responseMap.get("nodes");
|
||||
for (Object node : nodes.values()) {
|
||||
|
@ -75,4 +76,5 @@ public class WatcherSettingsFilterTests extends AbstractWatcherIntegrationTestCa
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue