Search in sources :

Example 1 with ReadPermission

use of com.yahoo.elide.annotation.ReadPermission in project elide by yahoo.

the class VerifyFieldAccessFilterExpressionVisitor method visitPredicate.

/**
 * Enforce ReadPermission on provided query filter.
 *
 * @return true if allowed, false if rejected
 */
@Override
public Boolean visitPredicate(FilterPredicate filterPredicate) {
    RequestScope requestScope = resource.getRequestScope();
    Set<PersistentResource> val = Collections.singleton(resource);
    PermissionExecutor permissionExecutor = requestScope.getPermissionExecutor();
    ExpressionResult result = permissionExecutor.evaluateFilterJoinUserChecks(resource, filterPredicate);
    if (result == ExpressionResult.UNEVALUATED) {
        result = evaluateUserChecks(filterPredicate, permissionExecutor);
    }
    if (result == ExpressionResult.PASS) {
        return true;
    }
    if (result == ExpressionResult.FAIL) {
        return false;
    }
    for (PathElement element : filterPredicate.getPath().getPathElements()) {
        String fieldName = element.getFieldName();
        if ("this".equals(fieldName)) {
            continue;
        }
        try {
            val = val.stream().filter(Objects::nonNull).flatMap(x -> getValueChecked(x, fieldName, requestScope).toList(LinkedHashSet::new).blockingGet().stream()).filter(Objects::nonNull).collect(Collectors.toSet());
        } catch (ForbiddenAccessException e) {
            result = permissionExecutor.handleFilterJoinReject(filterPredicate, element, e);
            if (result == ExpressionResult.DEFERRED) {
                continue;
            }
            // pass or fail
            return result == ExpressionResult.PASS;
        }
    }
    return true;
}
Also used : FilterExpressionVisitor(com.yahoo.elide.core.filter.expression.FilterExpressionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) Set(java.util.Set) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Objects(java.util.Objects) ForbiddenAccessException(com.yahoo.elide.core.exceptions.ForbiddenAccessException) ExpressionResult(com.yahoo.elide.core.security.permissions.ExpressionResult) ReadPermission(com.yahoo.elide.annotation.ReadPermission) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) PersistentResource(com.yahoo.elide.core.PersistentResource) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Relationship(com.yahoo.elide.core.request.Relationship) Observable(io.reactivex.Observable) PathElement(com.yahoo.elide.core.Path.PathElement) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) RequestScope(com.yahoo.elide.core.RequestScope) LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) PathElement(com.yahoo.elide.core.Path.PathElement) ExpressionResult(com.yahoo.elide.core.security.permissions.ExpressionResult) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) Objects(java.util.Objects) RequestScope(com.yahoo.elide.core.RequestScope) ForbiddenAccessException(com.yahoo.elide.core.exceptions.ForbiddenAccessException)

Example 2 with ReadPermission

use of com.yahoo.elide.annotation.ReadPermission in project elide by yahoo.

the class CanPaginateVisitorTest method testSparseFields.

@Test
public void testSparseFields() throws Exception {
    @Entity
    @Include(rootLevel = false)
    @ReadPermission(expression = "In Memory Check")
    class Book {

        @Id
        private long id;

        @ReadPermission(expression = "Filter Expression Check")
        private String title;

        @ReadPermission(expression = "Filter Expression Check")
        private Date publicationDate;

        private boolean outOfPrint;
    }
    EntityDictionary dictionary = TestDictionary.getTestDictionary(checkMappings);
    dictionary.bindEntity(Book.class);
    RequestScope scope = mock(RequestScope.class);
    Set<String> sparseFields = new HashSet<>();
    assertFalse(CanPaginateVisitor.canPaginate(ClassType.of(Book.class), dictionary, scope, sparseFields));
    sparseFields.add("title");
    sparseFields.add("publicationDate");
    assertTrue(CanPaginateVisitor.canPaginate(ClassType.of(Book.class), dictionary, scope, sparseFields));
    sparseFields.add("outOfPrint");
    assertFalse(CanPaginateVisitor.canPaginate(ClassType.of(Book.class), dictionary, scope, sparseFields));
}
Also used : Entity(javax.persistence.Entity) Include(com.yahoo.elide.annotation.Include) ReadPermission(com.yahoo.elide.annotation.ReadPermission) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RequestScope(com.yahoo.elide.core.RequestScope) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with ReadPermission

use of com.yahoo.elide.annotation.ReadPermission in project elide by yahoo.

the class CanPaginateVisitorTest method testMultipleFieldsPagination.

@Test
public void testMultipleFieldsPagination() throws Exception {
    @Entity
    @Include(rootLevel = false)
    @ReadPermission(expression = "In Memory Check")
    class Book {

        @Id
        private long id;

        @ReadPermission(expression = "Filter Expression Check")
        private String title;

        @ReadPermission(expression = "Filter Expression Check")
        private Date publicationDate;
    }
    EntityDictionary dictionary = TestDictionary.getTestDictionary(checkMappings);
    dictionary.bindEntity(Book.class);
    RequestScope scope = mock(RequestScope.class);
    assertTrue(CanPaginateVisitor.canPaginate(ClassType.of(Book.class), dictionary, scope, new HashSet<>()));
}
Also used : Entity(javax.persistence.Entity) Include(com.yahoo.elide.annotation.Include) ReadPermission(com.yahoo.elide.annotation.ReadPermission) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RequestScope(com.yahoo.elide.core.RequestScope) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with ReadPermission

use of com.yahoo.elide.annotation.ReadPermission in project elide by yahoo.

the class TableType method buildAnnotations.

private static Map<Class<? extends Annotation>, Annotation> buildAnnotations(Dimension dimension) {
    Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<>();
    annotations.put(ColumnMeta.class, new ColumnMeta() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return ColumnMeta.class;
        }

        @Override
        public String friendlyName() {
            return dimension.getFriendlyName();
        }

        @Override
        public String description() {
            return dimension.getDescription();
        }

        @Override
        public String category() {
            return dimension.getCategory();
        }

        @Override
        public TableSource tableSource() {
            return buildTableSource(dimension.getTableSource());
        }

        @Override
        public String[] tags() {
            return dimension.getTags().toArray(new String[0]);
        }

        @Override
        public String[] values() {
            return dimension.getValues().toArray(new String[0]);
        }

        @Override
        public boolean isHidden() {
            return dimension.getHidden() != null && dimension.getHidden();
        }

        @Override
        public String filterTemplate() {
            return dimension.getFilterTemplate();
        }

        @Override
        public CardinalitySize size() {
            if (dimension.getCardinality() == null || dimension.getCardinality().isEmpty()) {
                return CardinalitySize.UNKNOWN;
            }
            return CardinalitySize.valueOf(dimension.getCardinality().toUpperCase(Locale.ENGLISH));
        }
    });
    annotations.put(DimensionFormula.class, new DimensionFormula() {

        @Override
        public ArgumentDefinition[] arguments() {
            return getArgumentDefinitions(dimension.getArguments());
        }

        @Override
        public Class<? extends Annotation> annotationType() {
            return DimensionFormula.class;
        }

        @Override
        public String value() {
            return trimColumnReferences(dimension.getDefinition());
        }
    });
    String readPermission = dimension.getReadAccess();
    if (StringUtils.isNotEmpty(readPermission)) {
        annotations.put(ReadPermission.class, new ReadPermission() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return ReadPermission.class;
            }

            @Override
            public String expression() {
                return readPermission;
            }
        });
    }
    if (dimension.getType().toUpperCase(Locale.ROOT).equals(ENUM_ORDINAL)) {
        annotations.put(Enumerated.class, getEnumeratedAnnotation(EnumType.ORDINAL));
    }
    if (dimension.getType().toUpperCase(Locale.ROOT).equals(TIME)) {
        annotations.put(Temporal.class, new Temporal() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return Temporal.class;
            }

            @Override
            public TimeGrainDefinition[] grains() {
                int numGrains = dimension.getGrains() == null ? 0 : dimension.getGrains().size();
                TimeGrainDefinition[] definitions = new TimeGrainDefinition[numGrains];
                for (int idx = 0; idx < numGrains; idx++) {
                    Grain grain = dimension.getGrains().get(idx);
                    definitions[idx] = new TimeGrainDefinition() {

                        @Override
                        public Class<? extends Annotation> annotationType() {
                            return TimeGrainDefinition.class;
                        }

                        @Override
                        public TimeGrain grain() {
                            if (grain.getType() == null) {
                                return TimeGrain.DAY;
                            }
                            return TimeGrain.valueOf(grain.getType().name());
                        }

                        @Override
                        public String expression() {
                            String sql = grain.getSql();
                            if (StringUtils.isEmpty(sql)) {
                                return "{{$$column.expr}}";
                            }
                            return grain.getSql();
                        }
                    };
                }
                return definitions;
            }

            @Override
            public String timeZone() {
                return "UTC";
            }
        });
    }
    return annotations;
}
Also used : CardinalitySize(com.yahoo.elide.datastores.aggregation.annotation.CardinalitySize) HashMap(java.util.HashMap) Grain(com.yahoo.elide.modelconfig.model.Grain) TimeGrain(com.yahoo.elide.datastores.aggregation.metadata.enums.TimeGrain) Annotation(java.lang.annotation.Annotation) TableSource(com.yahoo.elide.datastores.aggregation.annotation.TableSource) ColumnMeta(com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta) DimensionFormula(com.yahoo.elide.datastores.aggregation.annotation.DimensionFormula) Temporal(com.yahoo.elide.datastores.aggregation.annotation.Temporal) ReadPermission(com.yahoo.elide.annotation.ReadPermission) TimeGrainDefinition(com.yahoo.elide.datastores.aggregation.annotation.TimeGrainDefinition)

Example 5 with ReadPermission

use of com.yahoo.elide.annotation.ReadPermission in project elide by yahoo.

the class TableType method buildAnnotations.

private static Map<Class<? extends Annotation>, Annotation> buildAnnotations(Measure measure) {
    Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<>();
    annotations.put(ColumnMeta.class, new ColumnMeta() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return ColumnMeta.class;
        }

        @Override
        public String friendlyName() {
            return measure.getFriendlyName();
        }

        @Override
        public String description() {
            return measure.getDescription();
        }

        @Override
        public String category() {
            return measure.getCategory();
        }

        @Override
        public TableSource tableSource() {
            return buildTableSource(null);
        }

        @Override
        public String[] tags() {
            return measure.getTags().toArray(new String[0]);
        }

        @Override
        public String[] values() {
            return new String[0];
        }

        @Override
        public boolean isHidden() {
            return measure.getHidden() != null && measure.getHidden();
        }

        @Override
        public String filterTemplate() {
            return measure.getFilterTemplate();
        }

        @Override
        public CardinalitySize size() {
            return CardinalitySize.UNKNOWN;
        }
    });
    annotations.put(MetricFormula.class, new MetricFormula() {

        @Override
        public ArgumentDefinition[] arguments() {
            return getArgumentDefinitions(measure.getArguments());
        }

        @Override
        public Class<? extends Annotation> annotationType() {
            return MetricFormula.class;
        }

        @Override
        public String value() {
            if (measure.getDefinition() != null) {
                return trimColumnReferences(measure.getDefinition());
            } else {
                return "";
            }
        }

        @Override
        public Class<? extends MetricProjectionMaker> maker() {
            if (measure.getMaker() == null || measure.getMaker().isEmpty()) {
                return DefaultMetricProjectionMaker.class;
            }
            try {
                return (Class<? extends MetricProjectionMaker>) Class.forName(measure.getMaker());
            } catch (ClassNotFoundException e) {
                throw new IllegalStateException(e);
            }
        }
    });
    String readPermission = measure.getReadAccess();
    if (StringUtils.isNotEmpty(readPermission)) {
        annotations.put(ReadPermission.class, new ReadPermission() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return ReadPermission.class;
            }

            @Override
            public String expression() {
                return readPermission;
            }
        });
    }
    return annotations;
}
Also used : CardinalitySize(com.yahoo.elide.datastores.aggregation.annotation.CardinalitySize) MetricFormula(com.yahoo.elide.datastores.aggregation.annotation.MetricFormula) MetricProjectionMaker(com.yahoo.elide.datastores.aggregation.query.MetricProjectionMaker) DefaultMetricProjectionMaker(com.yahoo.elide.datastores.aggregation.query.DefaultMetricProjectionMaker) HashMap(java.util.HashMap) Annotation(java.lang.annotation.Annotation) TableSource(com.yahoo.elide.datastores.aggregation.annotation.TableSource) ColumnMeta(com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta) ReadPermission(com.yahoo.elide.annotation.ReadPermission)

Aggregations

ReadPermission (com.yahoo.elide.annotation.ReadPermission)21 Test (org.junit.jupiter.api.Test)15 Include (com.yahoo.elide.annotation.Include)11 HashSet (java.util.HashSet)10 Entity (javax.persistence.Entity)8 RequestScope (com.yahoo.elide.core.RequestScope)6 PersistentResource (com.yahoo.elide.core.PersistentResource)5 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)5 ColumnMeta (com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta)5 FromTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable)5 Annotation (java.lang.annotation.Annotation)5 Table (com.yahoo.elide.modelconfig.model.Table)4 HashMap (java.util.HashMap)4 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)3 Field (com.yahoo.elide.core.type.Field)3 CardinalitySize (com.yahoo.elide.datastores.aggregation.annotation.CardinalitySize)3 DimensionFormula (com.yahoo.elide.datastores.aggregation.annotation.DimensionFormula)3 ApiVersion (com.yahoo.elide.annotation.ApiVersion)2 PathElement (com.yahoo.elide.core.Path.PathElement)2 ForbiddenAccessException (com.yahoo.elide.core.exceptions.ForbiddenAccessException)2