Skip to content

Commit 01340b0

Browse files
createUser func fixed ad part of Security tests (opensearch-project#1623)
* gradle and jdk upgrade Signed-off-by: Riya Saxena <riysaxen@amazon.com> * gradle and jdk upgrade Signed-off-by: Riya Saxena <riysaxen@amazon.com> * updating build tools in maven Signed-off-by: Riya Saxena <riysaxen@amazon.com> * updating build tools in maven Signed-off-by: Riya Saxena <riysaxen@amazon.com> * fix security tests Signed-off-by: Riya Saxena <riysaxen@amazon.com> --------- Signed-off-by: Riya Saxena <riysaxen@amazon.com>
1 parent 2113bd2 commit 01340b0

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/test/java/org/opensearch/securityanalytics/SecurityAnalyticsRestTestCase.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,11 +1395,26 @@ protected void createCustomRole(String name, String clusterPermissions) throws I
13951395

13961396
public void createUser(String name, String[] backendRoles) throws IOException {
13971397
Request request = new Request("PUT", String.format(Locale.getDefault(), "/_plugins/_security/api/internalusers/%s", name));
1398-
String broles = String.join(",", backendRoles);
1399-
//String roles = String.join(",", customRoles);
1398+
1399+
// Filter out null and blank backend roles to avoid security plugin rejection
1400+
String[] filteredRoles = backendRoles == null ? new String[0] :
1401+
Arrays.stream(backendRoles)
1402+
.filter(role -> role != null && !role.trim().isEmpty())
1403+
.toArray(String[]::new);
1404+
1405+
// Build the backend_roles JSON array properly
1406+
String backendRolesJson;
1407+
if (filteredRoles.length == 0) {
1408+
backendRolesJson = "[]";
1409+
} else {
1410+
backendRolesJson = Arrays.stream(filteredRoles)
1411+
.map(role -> "\"" + role + "\"")
1412+
.collect(Collectors.joining(",", "[", "]"));
1413+
}
1414+
14001415
String entity = " {\n" +
14011416
"\"password\": \"" + password + "\",\n" +
1402-
"\"backend_roles\": [\"" + broles + "\"],\n" +
1417+
"\"backend_roles\": " + backendRolesJson + ",\n" +
14031418
"\"attributes\": {\n" +
14041419
"}} ";
14051420
request.setJsonEntity(entity);

0 commit comments

Comments
 (0)