Skip to content

Commit

Permalink
Fix FullClusterRestartIT
Browse files Browse the repository at this point in the history
  • Loading branch information
jkakavas committed Dec 6, 2018
1 parent a77dcd8 commit ff242e8
Showing 1 changed file with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public void testSingleDoc() throws IOException {
@SuppressWarnings("unchecked")
public void testSecurityNativeRealm() throws Exception {
if (isRunningAgainstOldCluster()) {
createUser("preupgrade_user");
createRole("preupgrade_role");
createUser(true);
createRole(true);
} else {
waitForYellow(".security");
Response settingsResponse = client().performRequest(new Request("GET", "/.security/_settings/index.format"));
Expand Down Expand Up @@ -126,7 +126,7 @@ public void testSecurityNativeRealm() throws Exception {
logger.info("upgrading security index {}", concreteSecurityIndex);
// without upgrade, an error should be thrown
try {
createUser("postupgrade_user");
createUser(false);
fail("should not be able to add a user when upgrade hasn't taken place");
} catch (ResponseException e) {
assertThat(e.getMessage(), containsString("Security index is not on the current version - " +
Expand All @@ -139,16 +139,12 @@ public void testSecurityNativeRealm() throws Exception {
}

// create additional user and role
createUser("postupgrade_user");
createRole("postupgrade_role");
createUser(false);
createRole(false);
}

assertUserInfo("preupgrade_user");
assertRoleInfo("preupgrade_role");
if (isRunningAgainstOldCluster() == false) {
assertUserInfo("postupgrade_user");
assertRoleInfo("postupgrade_role");
}
assertUserInfo(isRunningAgainstOldCluster());
assertRoleInfo(isRunningAgainstOldCluster());
}

public void testWatcher() throws Exception {
Expand Down Expand Up @@ -578,8 +574,14 @@ static String toStr(Response response) throws IOException {
return EntityUtils.toString(response.getEntity());
}

private void createUser(final String id) throws Exception {
Request request = new Request("PUT", "/_security/user/" + id);
private void createUser(final boolean oldCluster) throws Exception {
final String id = oldCluster ? "preupgrade_user" : "postupgrade_user";
Request request;
if (oldCluster) {
request = new Request("PUT", "/_xpack/security/user/" + id);
} else {
request = new Request("PUT", "/_security/user/" + id);
}
request.setJsonEntity(
"{\n" +
" \"password\" : \"j@rV1s\",\n" +
Expand All @@ -591,8 +593,14 @@ private void createUser(final String id) throws Exception {
client().performRequest(request);
}

private void createRole(final String id) throws Exception {
Request request = new Request("PUT", "/_security/role/" + id);
private void createRole(final boolean oldCluster) throws Exception {
final String id = oldCluster ? "preupgrade_role" : "postupgrade_role";
Request request;
if (oldCluster) {
request = new Request("PUT", "/_xpack/security/role/" + id);
} else {
request = new Request("PUT", "/_security/role/" + id);
}
request.setJsonEntity(
"{\n" +
" \"run_as\": [ \"abc\" ],\n" +
Expand All @@ -611,17 +619,22 @@ private void createRole(final String id) throws Exception {
client().performRequest(request);
}

private void assertUserInfo(final String user) throws Exception {
Map<String, Object> response = entityAsMap(client().performRequest(new Request("GET", "/_security/user/" + user)));
private void assertUserInfo(final boolean oldCluster) throws Exception {
final String user = oldCluster ? "preupgrade_user" : "postupgrade_user";
Map<String, Object> response = oldCluster ?
entityAsMap(client().performRequest(new Request("GET", "/_xpack/security/user/" + user))) :
entityAsMap(client().performRequest(new Request("GET", "/_security/user/" + user)));
@SuppressWarnings("unchecked") Map<String, Object> userInfo = (Map<String, Object>) response.get(user);
assertEquals(user + "@example.com", userInfo.get("email"));
assertNotNull(userInfo.get("full_name"));
assertNotNull(userInfo.get("roles"));
}

private void assertRoleInfo(final String role) throws Exception {
@SuppressWarnings("unchecked") Map<String, Object> response = (Map<String, Object>)
entityAsMap(client().performRequest(new Request("GET", "/_security/role/" + role))).get(role);
private void assertRoleInfo(final boolean oldCluster) throws Exception {
final String role = oldCluster ? "preupgrade_role" : "postupgrade_role";
@SuppressWarnings("unchecked") Map<String, Object> response = oldCluster ?
(Map<String, Object>) entityAsMap(client().performRequest(new Request("GET", "/_xpack/security/role/" + role))).get(role) :
(Map<String, Object>) entityAsMap(client().performRequest(new Request("GET", "/_security/role/" + role))).get(role);
assertNotNull(response.get("run_as"));
assertNotNull(response.get("cluster"));
assertNotNull(response.get("indices"));
Expand Down

0 comments on commit ff242e8

Please sign in to comment.