Search in sources :

Example 1 with RangerValidityScheduleEvaluator

use of org.apache.ranger.plugin.policyevaluator.RangerValidityScheduleEvaluator in project ranger by apache.

the class RangerTagForEval method isApplicable.

public boolean isApplicable(Date accessTime) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerTagForEval.isApplicable(type=" + type + ", " + accessTime + ")");
    }
    boolean ret = false;
    List<RangerValidityScheduleEvaluator> validityPeriodEvaluators = this.validityPeriodEvaluators;
    // Specifically for unit-testing using TestPolicyEngine
    if (MapUtils.isNotEmpty(options) && CollectionUtils.isEmpty(validityPeriodEvaluators)) {
        Object value = getOption(RangerTag.OPTION_TAG_VALIDITY_PERIODS);
        if (value != null && value instanceof String) {
            this.validityPeriods = JsonUtils.jsonToRangerValiditySchedule((String) value);
            validityPeriodEvaluators = createValidityPeriodEvaluators();
        } else {
            validityPeriodEvaluators = Collections.emptyList();
        }
        this.validityPeriodEvaluators = validityPeriodEvaluators;
    }
    if (accessTime != null && CollectionUtils.isNotEmpty(validityPeriodEvaluators)) {
        for (RangerValidityScheduleEvaluator evaluator : validityPeriodEvaluators) {
            if (evaluator.isApplicable(accessTime.getTime())) {
                ret = true;
                break;
            }
        }
    } else {
        ret = true;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerTagForEval.isApplicable(type=" + type + ", " + accessTime + ") : " + ret);
    }
    return ret;
}
Also used : RangerValidityScheduleEvaluator(org.apache.ranger.plugin.policyevaluator.RangerValidityScheduleEvaluator)

Example 2 with RangerValidityScheduleEvaluator

use of org.apache.ranger.plugin.policyevaluator.RangerValidityScheduleEvaluator in project ranger by apache.

the class TestPolicyEngine method runValiditySchedulerTests.

private void runValiditySchedulerTests(String resourceName) {
    TimeZone defaultTZ = TimeZone.getDefault();
    TimeZone.setDefault(TimeZone.getTimeZone("PST"));
    List<ValiditySchedulerTestCase> testCases = null;
    InputStream inStream = this.getClass().getResourceAsStream(resourceName);
    InputStreamReader reader = new InputStreamReader(inStream);
    try {
        Type listType = new TypeToken<List<ValiditySchedulerTestCase>>() {
        }.getType();
        testCases = gsonBuilder.fromJson(reader, listType);
    } catch (Exception e) {
        assertFalse("Exception in reading validity-scheduler test cases.", true);
    }
    assertNotNull("TestCases are null!", testCases);
    if (CollectionUtils.isNotEmpty(testCases)) {
        for (ValiditySchedulerTestCase testCase : testCases) {
            boolean isValid = true;
            List<ValidationFailureDetails> validationFailures = new ArrayList<>();
            boolean isApplicable = false;
            List<RangerValiditySchedule> validatedSchedules = new ArrayList<>();
            for (RangerValiditySchedule validitySchedule : testCase.validitySchedules) {
                RangerValidityScheduleValidator validator = new RangerValidityScheduleValidator(validitySchedule);
                RangerValiditySchedule validatedSchedule = validator.validate(validationFailures);
                isValid = isValid && validatedSchedule != null;
                if (isValid) {
                    validatedSchedules.add(validatedSchedule);
                }
            }
            if (isValid) {
                for (RangerValiditySchedule validSchedule : validatedSchedules) {
                    isApplicable = new RangerValidityScheduleEvaluator(validSchedule).isApplicable(testCase.accessTime.getTime());
                    if (isApplicable) {
                        break;
                    }
                }
            }
            assertTrue(testCase.name, isValid == testCase.result.isValid);
            assertTrue(testCase.name, isApplicable == testCase.result.isApplicable);
            assertTrue(testCase.name + ", [" + validationFailures + "]", validationFailures.size() == testCase.result.validationFailureCount);
        }
    }
    TimeZone.setDefault(defaultTZ);
}
Also used : InputStreamReader(java.io.InputStreamReader) RangerValiditySchedule(org.apache.ranger.plugin.model.RangerValiditySchedule) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) JsonParseException(com.google.gson.JsonParseException) ValidationFailureDetails(org.apache.ranger.plugin.model.validation.ValidationFailureDetails) TimeZone(java.util.TimeZone) Type(java.lang.reflect.Type) RangerValidityScheduleEvaluator(org.apache.ranger.plugin.policyevaluator.RangerValidityScheduleEvaluator) RangerValidityScheduleValidator(org.apache.ranger.plugin.model.validation.RangerValidityScheduleValidator) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

RangerValidityScheduleEvaluator (org.apache.ranger.plugin.policyevaluator.RangerValidityScheduleEvaluator)2 JsonParseException (com.google.gson.JsonParseException)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TimeZone (java.util.TimeZone)1 RangerValiditySchedule (org.apache.ranger.plugin.model.RangerValiditySchedule)1 RangerValidityScheduleValidator (org.apache.ranger.plugin.model.validation.RangerValidityScheduleValidator)1 ValidationFailureDetails (org.apache.ranger.plugin.model.validation.ValidationFailureDetails)1