Search in sources :

Example 1 with ValidationFailureDetails

use of org.apache.ranger.plugin.model.validation.ValidationFailureDetails in project ranger by apache.

the class TagDBStore method validateTag.

private RangerTag validateTag(RangerTag tag) throws Exception {
    List<RangerValiditySchedule> validityPeriods = tag.getValidityPeriods();
    if (CollectionUtils.isNotEmpty(validityPeriods)) {
        List<RangerValiditySchedule> normalizedValidityPeriods = new ArrayList<>();
        List<ValidationFailureDetails> failures = new ArrayList<>();
        for (RangerValiditySchedule validityPeriod : validityPeriods) {
            RangerValidityScheduleValidator validator = new RangerValidityScheduleValidator(validityPeriod);
            RangerValiditySchedule normalizedValidityPeriod = validator.validate(failures);
            if (normalizedValidityPeriod != null && CollectionUtils.isEmpty(failures)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Normalized ValidityPeriod:[" + normalizedValidityPeriod + "]");
                }
                normalizedValidityPeriods.add(normalizedValidityPeriod);
            } else {
                String error = "Incorrect time-specification:[" + Arrays.asList(failures) + "]";
                LOG.error(error);
                throw new Exception(error);
            }
        }
        tag.setValidityPeriods(normalizedValidityPeriods);
    }
    return tag;
}
Also used : ValidationFailureDetails(org.apache.ranger.plugin.model.validation.ValidationFailureDetails) ArrayList(java.util.ArrayList) RangerValidityScheduleValidator(org.apache.ranger.plugin.model.validation.RangerValidityScheduleValidator) RangerServiceNotFoundException(org.apache.ranger.plugin.util.RangerServiceNotFoundException)

Example 2 with ValidationFailureDetails

use of org.apache.ranger.plugin.model.validation.ValidationFailureDetails 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

ArrayList (java.util.ArrayList)2 RangerValidityScheduleValidator (org.apache.ranger.plugin.model.validation.RangerValidityScheduleValidator)2 ValidationFailureDetails (org.apache.ranger.plugin.model.validation.ValidationFailureDetails)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 List (java.util.List)1 TimeZone (java.util.TimeZone)1 RangerValiditySchedule (org.apache.ranger.plugin.model.RangerValiditySchedule)1 RangerValidityScheduleEvaluator (org.apache.ranger.plugin.policyevaluator.RangerValidityScheduleEvaluator)1 RangerServiceNotFoundException (org.apache.ranger.plugin.util.RangerServiceNotFoundException)1