Skip to content

Commit a7cf418

Browse files
authored
Merge branch 'opensearch-project:main' into main
2 parents b7ebb43 + 10ee3c0 commit a7cf418

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
88
### Enhancements
99
### Bug Fixes
1010
- Fix substitution templates for OpenSearch 3.x compatibility by removing `_doc` mapping ([#1301](https://github.com/opensearch-project/flow-framework/pull/1301))
11+
- Fixed user permission validation to correctly handle null users and backend roles ([#1292](https://github.com/opensearch-project/flow-framework/pull/1292))
1112

1213
### Infrastructure
1314
### Documentation

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ buildscript {
6161

6262
dependencies {
6363
classpath "org.opensearch.gradle:build-tools:${opensearch_version}"
64-
classpath "com.diffplug.spotless:spotless-plugin-gradle:8.2.0"
64+
classpath "com.diffplug.spotless:spotless-plugin-gradle:8.2.1"
6565
}
6666
}
6767

src/main/java/org/opensearch/flowframework/util/ParseUtils.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@
5555
import java.io.InputStream;
5656
import java.time.Instant;
5757
import java.util.ArrayList;
58+
import java.util.Arrays;
5859
import java.util.HashMap;
5960
import java.util.HashSet;
6061
import java.util.List;
6162
import java.util.Map;
6263
import java.util.Map.Entry;
64+
import java.util.Objects;
6365
import java.util.Optional;
6466
import java.util.Set;
6567
import java.util.regex.Matcher;
@@ -366,6 +368,10 @@ public static void resolveUserAndExecute(
366368
}
367369
}
368370

371+
private static boolean anyNullUserOrRoles(User... users) {
372+
return Arrays.stream(users).anyMatch(u -> Objects.isNull(u) || Objects.isNull(u.getBackendRoles()));
373+
}
374+
369375
/**
370376
* Check if requested user has backend role required to access the resource
371377
* @param requestedUser the user to execute the request
@@ -374,10 +380,10 @@ public static void resolveUserAndExecute(
374380
* @return boolean if the requested user has backend role required to access the resource
375381
* @throws Exception exception
376382
*/
377-
private static boolean checkUserPermissions(User requestedUser, User resourceUser, String workflowId) throws Exception {
378-
if (resourceUser.getBackendRoles() == null || requestedUser.getBackendRoles() == null) {
379-
return false;
380-
}
383+
static boolean checkUserPermissions(User requestedUser, User resourceUser, String workflowId) throws Exception {
384+
385+
if (anyNullUserOrRoles(requestedUser, resourceUser)) return false;
386+
381387
// Check if requested user has backend role required to access the resource
382388
for (String backendRole : requestedUser.getBackendRoles()) {
383389
if (resourceUser.getBackendRoles().contains(backendRole)) {
@@ -533,8 +539,8 @@ public static void onGetWorkflowResponse(
533539
}
534540
if (shouldUseResourceAuthz(CommonValue.WORKFLOW_RESOURCE_TYPE)
535541
|| !filterByEnabled
536-
|| checkUserPermissions(requestUser, resourceUser, workflowId)
537-
|| isAdmin(requestUser)) {
542+
|| isAdmin(requestUser)
543+
|| checkUserPermissions(requestUser, resourceUser, workflowId)) {
538544
function.run();
539545
} else {
540546
logger.debug("User: " + requestUser.getName() + " does not have permissions to access workflow: " + workflowId);

src/test/java/org/opensearch/flowframework/util/ParseUtilsTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,36 @@ public void testIsAdminBackendRoleIsAllAccess() {
444444
public void testIsAdminNull() {
445445
assertFalse(isAdmin(null));
446446
}
447+
448+
public void testCheckUserPermissionsWithNullUsers() throws Exception {
449+
String workflowId = "testWorkflowId";
450+
User validUser = new User("user", ImmutableList.of("role"), ImmutableList.of(), ImmutableList.of());
451+
User userWithNullRoles = new User("user", null, ImmutableList.of(), ImmutableList.of());
452+
453+
// requestedUser is null
454+
assertFalse(ParseUtils.checkUserPermissions(null, validUser, workflowId));
455+
// resourceUser is null
456+
assertFalse(ParseUtils.checkUserPermissions(validUser, null, workflowId));
457+
// resourceUser.getBackendRoles() is null
458+
assertFalse(ParseUtils.checkUserPermissions(validUser, userWithNullRoles, workflowId));
459+
// requestedUser.getBackendRoles() is null
460+
assertFalse(ParseUtils.checkUserPermissions(userWithNullRoles, validUser, workflowId));
461+
}
462+
463+
public void testCheckUserPermissionsWithMatchingRoles() throws Exception {
464+
String workflowId = "testWorkflowId";
465+
User requestedUser = new User("user1", ImmutableList.of("roleA", "roleB"), ImmutableList.of(), ImmutableList.of());
466+
User resourceUser = new User("user2", ImmutableList.of("roleB", "roleC"), ImmutableList.of(), ImmutableList.of());
467+
468+
assertTrue(ParseUtils.checkUserPermissions(requestedUser, resourceUser, workflowId));
469+
}
470+
471+
public void testCheckUserPermissionsWithNoMatchingRoles() throws Exception {
472+
String workflowId = "testWorkflowId";
473+
User requestedUser = new User("user1", ImmutableList.of("roleA"), ImmutableList.of(), ImmutableList.of());
474+
User resourceUser = new User("user2", ImmutableList.of("roleB"), ImmutableList.of(), ImmutableList.of());
475+
476+
assertFalse(ParseUtils.checkUserPermissions(requestedUser, resourceUser, workflowId));
477+
}
478+
447479
}

src/test/java/org/opensearch/flowframework/workflow/CreateConnectorStepTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void setUp() throws Exception {
7171
Map.entry(CommonValue.NAME_FIELD, "test"),
7272
Map.entry(CommonValue.DESCRIPTION_FIELD, "description"),
7373
Map.entry(CommonValue.VERSION_FIELD, "1"),
74-
Map.entry(CommonValue.PROTOCOL_FIELD, "test"),
74+
Map.entry(CommonValue.PROTOCOL_FIELD, "http"),
7575
Map.entry(CommonValue.PARAMETERS_FIELD, params),
7676
Map.entry(CommonValue.CREDENTIAL_FIELD, credentials),
7777
Map.entry(CommonValue.ACTIONS_FIELD, actions)

0 commit comments

Comments
 (0)