use of com.yahoo.elide.core.security.PermissionExecutor in project elide by yahoo.
the class VerifyFieldAccessFilterExpressionVisitorTest method testShortCircuitPass.
@Test
public void testShortCircuitPass() throws Exception {
RSQLFilterDialect dialect = RSQLFilterDialect.builder().dictionary(scope.getDictionary()).build();
FilterExpression expression = dialect.parseFilterExpression("authors.name==foo", ClassType.of(Book.class), true);
Book book = new Book();
PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
DataStoreTransaction tx = scope.getTransaction();
when(permissionExecutor.checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, AUTHORS)).thenReturn(ExpressionResult.PASS);
when(permissionExecutor.checkUserPermissions(ClassType.of(Author.class), ReadPermission.class, NAME)).thenReturn(ExpressionResult.PASS);
VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
// restricted HOME field
assertTrue(expression.accept(visitor));
verify(permissionExecutor, times(1)).evaluateFilterJoinUserChecks(any(), any());
verify(permissionExecutor, times(1)).checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, AUTHORS);
verify(permissionExecutor, times(1)).checkUserPermissions(ClassType.of(Author.class), ReadPermission.class, NAME);
verify(permissionExecutor, never()).checkSpecificFieldPermissions(resource, null, ReadPermission.class, GENRE);
verify(permissionExecutor, times(2)).checkUserPermissions(any(), any(), isA(String.class));
verify(permissionExecutor, never()).handleFilterJoinReject(any(), any(), any());
verify(tx, never()).getToManyRelation(any(), any(), any(), any());
}
use of com.yahoo.elide.core.security.PermissionExecutor in project elide by yahoo.
the class VerifyFieldAccessFilterExpressionVisitor method evaluateUserChecks.
/**
* Scan the Path for user checks.
* <ol>
* <li>If all are PASS, return PASS
* <li>If any FAIL, return FAIL
* <li>Otherwise return DEFERRED
* </ol>
* @param filterPredicate filterPredicate
* @param permissionExecutor permissionExecutor
* @return ExpressionResult
*/
private ExpressionResult evaluateUserChecks(FilterPredicate filterPredicate, PermissionExecutor permissionExecutor) {
PermissionExecutor executor = resource.getRequestScope().getPermissionExecutor();
ExpressionResult ret = ExpressionResult.PASS;
for (PathElement element : filterPredicate.getPath().getPathElements()) {
ExpressionResult result;
try {
result = executor.checkUserPermissions(element.getType(), ReadPermission.class, element.getFieldName());
} catch (ForbiddenAccessException e) {
result = permissionExecutor.handleFilterJoinReject(filterPredicate, element, e);
}
if (result == ExpressionResult.FAIL) {
return ExpressionResult.FAIL;
}
if (result != ExpressionResult.PASS) {
ret = ExpressionResult.DEFERRED;
}
}
return ret;
}
use of com.yahoo.elide.core.security.PermissionExecutor in project elide by yahoo.
the class VerifyFieldAccessFilterExpressionVisitorTest method setupMocks.
@BeforeEach
public void setupMocks() {
// this will test with the default interface implementation
scope = mock(RequestScope.class);
PermissionExecutor permissionExecutor = mock(PermissionExecutor.class);
DataStoreTransaction transaction = mock(DataStoreTransaction.class);
EntityDictionary dictionary = EntityDictionary.builder().build();
dictionary.bindEntity(Book.class);
dictionary.bindEntity(Author.class);
when(scope.getDictionary()).thenReturn(dictionary);
when(scope.getPermissionExecutor()).thenReturn(permissionExecutor);
when(scope.getTransaction()).thenReturn(transaction);
when(permissionExecutor.evaluateFilterJoinUserChecks(any(), any())).thenCallRealMethod();
when(permissionExecutor.handleFilterJoinReject(any(), any(), any())).thenCallRealMethod();
}
use of com.yahoo.elide.core.security.PermissionExecutor in project elide by yahoo.
the class VerifyFieldAccessFilterExpressionVisitorTest method testShortCircuitDeferred.
@Test
public void testShortCircuitDeferred() throws Exception {
RSQLFilterDialect dialect = RSQLFilterDialect.builder().dictionary(scope.getDictionary()).build();
FilterExpression expression = dialect.parseFilterExpression("genre==foo", ClassType.of(Book.class), true);
Book book = new Book();
PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
when(permissionExecutor.checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, GENRE)).thenReturn(ExpressionResult.DEFERRED);
when(permissionExecutor.checkSpecificFieldPermissions(resource, null, ReadPermission.class, GENRE)).thenThrow(ForbiddenAccessException.class);
VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
// restricted HOME field
assertFalse(expression.accept(visitor));
verify(permissionExecutor, times(1)).evaluateFilterJoinUserChecks(any(), any());
verify(permissionExecutor, times(1)).checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, GENRE);
verify(permissionExecutor, times(1)).checkSpecificFieldPermissions(resource, null, ReadPermission.class, GENRE);
verify(permissionExecutor, times(1)).checkUserPermissions(any(), any(), isA(String.class));
verify(permissionExecutor, times(1)).handleFilterJoinReject(any(), any(), any());
}
use of com.yahoo.elide.core.security.PermissionExecutor in project elide by yahoo.
the class VerifyFieldAccessFilterExpressionVisitorTest method testShortCircuitReject.
@Test
public void testShortCircuitReject() throws Exception {
RSQLFilterDialect dialect = RSQLFilterDialect.builder().dictionary(scope.getDictionary()).build();
FilterExpression expression = dialect.parseFilterExpression("genre==foo", ClassType.of(Book.class), true);
Book book = new Book();
PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
when(permissionExecutor.checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, GENRE)).thenThrow(ForbiddenAccessException.class);
VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
// restricted HOME field
assertFalse(expression.accept(visitor));
verify(permissionExecutor, times(1)).evaluateFilterJoinUserChecks(any(), any());
verify(permissionExecutor, times(1)).checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, GENRE);
verify(permissionExecutor, never()).checkSpecificFieldPermissions(resource, null, ReadPermission.class, GENRE);
verify(permissionExecutor, times(1)).checkUserPermissions(any(), any(), isA(String.class));
verify(permissionExecutor, times(1)).handleFilterJoinReject(any(), any(), any());
}
Aggregations