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;
}
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);
}
Aggregations