Skip to content

Commit ed040f5

Browse files
committed
RANGER-3888: fixed unit tests for earlier commit in RANGER-3888 - #2
(cherry picked from commit b6e4324)
1 parent b5dd2e2 commit ed040f5

File tree

3 files changed

+256
-18
lines changed

3 files changed

+256
-18
lines changed

agents-common/src/main/java/org/apache/ranger/plugin/model/validation/RangerValidityScheduleValidator.java

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public class RangerValidityScheduleValidator {
4949
private static final ThreadLocal<DateFormat> DATE_FORMATTER = new ThreadLocal<DateFormat>() {
5050
@Override
5151
protected DateFormat initialValue() {
52-
return new SimpleDateFormat(RangerValiditySchedule.VALIDITY_SCHEDULE_DATE_STRING_SPECIFICATION);
52+
SimpleDateFormat sd = new SimpleDateFormat(RangerValiditySchedule.VALIDITY_SCHEDULE_DATE_STRING_SPECIFICATION);
53+
sd.setLenient(false);
54+
return sd;
5355
}
5456
};
5557

@@ -183,7 +185,8 @@ private boolean validateValidityInterval(RangerValidityRecurrence recurrence, Li
183185

184186
if (validityInterval.getDays() < 0
185187
|| (validityInterval.getHours() < 0 || validityInterval.getHours() > 23)
186-
|| (validityInterval.getMinutes() < 0 || validityInterval.getMinutes() > 59)) {
188+
|| (validityInterval.getMinutes() < 0 || validityInterval.getMinutes() > 59)
189+
|| (validityInterval.getDays() == 0 && validityInterval.getHours() == 0 && validityInterval.getMinutes() == 0 )) {
187190
validationFailures.add(new ValidationFailureDetails(0, "interval", "", false, true, false, "invalid interval"));
188191
ret = false;
189192
}
@@ -226,6 +229,26 @@ private boolean validateFieldSpec(RangerValidityRecurrence recurrence, RangerVal
226229
int maximum = field == RangerValidityRecurrence.RecurrenceSchedule.ScheduleFieldSpec.month ? field.maximum + 1 : field.maximum;
227230
ret = validateRanges(recurrence, field, minimum, maximum, validationFailures);
228231
}
232+
233+
if(ret) {
234+
final int minimum;
235+
final int maximum;
236+
237+
if (field == RecurrenceSchedule.ScheduleFieldSpec.year) {
238+
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
239+
240+
minimum = Integer.valueOf(formatter.format(startTime));
241+
maximum = Integer.valueOf(formatter.format(endTime));
242+
} else if (field == RecurrenceSchedule.ScheduleFieldSpec.month) {
243+
minimum = field.minimum + 1;
244+
maximum = field.maximum + 1;
245+
} else {
246+
minimum = field.minimum;
247+
maximum = field.maximum;
248+
}
249+
250+
ret = validateRanges(recurrence, field, minimum, maximum, validationFailures);
251+
}
229252
return ret;
230253
}
231254

@@ -340,36 +363,47 @@ public int compare(Range me, Range other) {
340363
if (StringUtils.isNotEmpty(spec)) {
341364
// Range
342365
if (spec.startsWith("-") || spec.endsWith("-")) {
343-
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range spec"));
366+
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range spec: " + spec));
344367
ret = false;
345368
} else {
346369
String[] ranges = StringUtils.split(spec, "-");
347370
if (ranges.length > 2) {
348-
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range spec"));
371+
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range spec: " + spec));
349372
ret = false;
350373
} else if (ranges.length == 2) {
351374
int val1 = minValidValue, val2 = maxValidValue;
352375
if (!StringUtils.equals(ranges[0], RangerValidityRecurrence.RecurrenceSchedule.WILDCARD)) {
353376
val1 = Integer.valueOf(ranges[0]);
354-
if (val1 < minValidValue || val1 > maxValidValue) {
355-
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect lower range value"));
356-
ret = false;
357-
}
358377
} else {
359378
value = RangerValidityRecurrence.RecurrenceSchedule.WILDCARD;
360379
}
380+
361381
if (!StringUtils.equals(ranges[1], RangerValidityRecurrence.RecurrenceSchedule.WILDCARD)) {
362382
val2 = Integer.valueOf(ranges[1]);
363-
if (val1 < minValidValue || val2 > maxValidValue) {
364-
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect upper range value"));
365-
ret = false;
366-
}
367383
} else {
368384
value = RangerValidityRecurrence.RecurrenceSchedule.WILDCARD;
369385
}
386+
387+
if (field == RecurrenceSchedule.ScheduleFieldSpec.year) { // for year, one bound (lower or upper) can be outside the range
388+
if (val1 < minValidValue && val2 > maxValidValue) {
389+
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range: (" + val1 + ", " + val2 + "). valid range: (" + minValidValue + ", " + maxValidValue + ")"));
390+
ret = false;
391+
}
392+
} else { // for month/dayOfMonth/dayOfWeek/hour/minute both bounds (lower and upper) must be within range
393+
if (val1 < minValidValue || val1 > maxValidValue) {
394+
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect lower range: " + val1 + ". valid range: (" + minValidValue + ", " + maxValidValue + ")"));
395+
ret = false;
396+
}
397+
398+
if (val2 < minValidValue || val2 > maxValidValue) {
399+
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect upper range: " + val2 + ". valid range: (" + minValidValue + ", " + maxValidValue + ")"));
400+
ret = false;
401+
}
402+
}
403+
370404
if (ret) {
371405
if (val1 >= val2) {
372-
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range"));
406+
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range: min=" + val1 + ", max=" + val2));
373407
ret = false;
374408
} else {
375409
value = RangerValidityRecurrence.RecurrenceSchedule.WILDCARD;
@@ -389,7 +423,7 @@ public int compare(Range me, Range other) {
389423
if (!StringUtils.equals(ranges[0], RangerValidityRecurrence.RecurrenceSchedule.WILDCARD)) {
390424
int val = Integer.valueOf(ranges[0]);
391425
if (val < minValidValue || val > maxValidValue) {
392-
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect value"));
426+
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect value: " + val + ". Valid range: (" + minValidValue + "-" + maxValidValue + ")"));
393427
ret = false;
394428
} else {
395429
if (!StringUtils.equals(value, RangerValidityRecurrence.RecurrenceSchedule.WILDCARD)) {
@@ -414,7 +448,7 @@ public int compare(Range me, Range other) {
414448
int upper = range.upper;
415449
for (int j = i+1; j < rangeOfValues.size(); j++) {
416450
Range r = rangeOfValues.get(j);
417-
if (upper < r.upper) {
451+
if (upper > r.lower) {
418452
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "overlapping range value"));
419453
ret = false;
420454
}

agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestPolicyEngine.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -905,9 +905,9 @@ private void runValiditySchedulerTests(String resourceName) {
905905
}
906906
}
907907

908-
assertTrue(testCase.name, isValid == testCase.result.isValid);
909-
assertTrue(testCase.name, isApplicable == testCase.result.isApplicable);
910-
assertTrue(testCase.name + ", [" + validationFailures +"]", validationFailures.size() == testCase.result.validationFailureCount);
908+
assertEquals(testCase.name + " - isValid (validationFailures: " + validationFailures + ")", testCase.result.isValid, isValid);
909+
assertEquals(testCase.name + " - isApplicable (validationFailures: " + validationFailures + ")", testCase.result.isApplicable, isApplicable);
910+
assertEquals(testCase.name + " - validationFailureCount (validationFailures: " + validationFailures +")", testCase.result.validationFailureCount, validationFailures.size());
911911
}
912912
}
913913
TimeZone.setDefault(defaultTZ);

agents-common/src/test/resources/policyengine/validityscheduler/test-validity-schedules-invalid.json

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,209 @@
210210
"isApplicable": false,
211211
"validationFailureCount": 2
212212
}
213+
},
214+
{
215+
"name": "invalid lower bound - minute",
216+
"validitySchedules": [
217+
{
218+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
219+
"recurrences": [
220+
{
221+
"schedule": { "minute": "-1", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": "1", "year": "2018" },
222+
"interval": { "minutes": 10 }
223+
}
224+
]
225+
}
226+
],
227+
"accessTime": "20180112-18:32:27.000-0800",
228+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
229+
}
230+
},
231+
{
232+
"name": "invalid upper bound - minute",
233+
"validitySchedules": [
234+
{
235+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
236+
"recurrences": [
237+
{
238+
"schedule": { "minute": "60", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": "1", "year": "2018" },
239+
"interval": { "minutes": 10 }
240+
}
241+
]
242+
}
243+
],
244+
"accessTime": "20180112-18:32:27.000-0800",
245+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
246+
}
247+
},
248+
{
249+
"name": "invalid lower bound - hour",
250+
"validitySchedules": [
251+
{
252+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
253+
"recurrences": [
254+
{
255+
"schedule": { "minute": "0", "hour": "-1", "dayOfMonth": "1", "dayOfWeek": "1", "month": "1", "year": "2018" },
256+
"interval": { "minutes": 10 }
257+
}
258+
]
259+
}
260+
],
261+
"accessTime": "20180112-18:32:27.000-0800",
262+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
263+
}
264+
},
265+
{
266+
"name": "invalid upper bound - hour",
267+
"validitySchedules": [
268+
{
269+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
270+
"recurrences": [
271+
{
272+
"schedule": { "minute": "0", "hour": "24", "dayOfMonth": "1", "dayOfWeek": "1", "month": "1", "year": "2018" },
273+
"interval": { "minutes": 10 }
274+
}
275+
]
276+
}
277+
],
278+
"accessTime": "20180112-18:32:27.000-0800",
279+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
280+
}
281+
},
282+
{
283+
"name": "invalid lower bound - dayOfMonth",
284+
"validitySchedules": [
285+
{
286+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
287+
"recurrences": [
288+
{
289+
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "0", "dayOfWeek": "1", "month": "1", "year": "2018" },
290+
"interval": { "minutes": 10 }
291+
}
292+
]
293+
}
294+
],
295+
"accessTime": "20180112-18:32:27.000-0800",
296+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
297+
}
298+
},
299+
{
300+
"name": "invalid upper bound - dayOfMonth",
301+
"validitySchedules": [
302+
{
303+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
304+
"recurrences": [
305+
{
306+
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "32", "dayOfWeek": "1", "month": "1", "year": "2018" },
307+
"interval": { "minutes": 10 }
308+
}
309+
]
310+
}
311+
],
312+
"accessTime": "20180112-18:32:27.000-0800",
313+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
314+
}
315+
},
316+
{
317+
"name": "invalid lower bound - dayOfWeek",
318+
"validitySchedules": [
319+
{
320+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
321+
"recurrences": [
322+
{
323+
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "0", "month": "1", "year": "2018" },
324+
"interval": { "minutes": 10 }
325+
}
326+
]
327+
}
328+
],
329+
"accessTime": "20180112-18:32:27.000-0800",
330+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
331+
}
332+
},
333+
{
334+
"name": "invalid upper bound - dayOfWeek",
335+
"validitySchedules": [
336+
{
337+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
338+
"recurrences": [
339+
{
340+
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "8", "month": "1", "year": "2018" },
341+
"interval": { "minutes": 10 }
342+
}
343+
]
344+
}
345+
],
346+
"accessTime": "20180112-18:32:27.000-0800",
347+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
348+
}
349+
},
350+
{
351+
"name": "invalid lower bound - month",
352+
"validitySchedules": [
353+
{
354+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
355+
"recurrences": [
356+
{
357+
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": "0", "year": "2018" },
358+
"interval": { "minutes": 10 }
359+
}
360+
]
361+
}
362+
],
363+
"accessTime": "20180112-18:32:27.000-0800",
364+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
365+
}
366+
},
367+
{
368+
"name": "invalid upper bound - month",
369+
"validitySchedules": [
370+
{
371+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
372+
"recurrences": [
373+
{
374+
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": "13", "year": "2018" },
375+
"interval": { "minutes": 10 }
376+
}
377+
]
378+
}
379+
],
380+
"accessTime": "20180112-18:32:27.000-0800",
381+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
382+
}
383+
},
384+
{
385+
"name": "invalid lower bound - year",
386+
"validitySchedules": [
387+
{
388+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
389+
"recurrences": [
390+
{
391+
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": 1, "year": "2016" },
392+
"interval": { "minutes": 10 }
393+
}
394+
]
395+
}
396+
],
397+
"accessTime": "20180112-18:32:27.000-0800",
398+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
399+
}
400+
},
401+
{
402+
"name": "invalid upper bound - year",
403+
"validitySchedules": [
404+
{
405+
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
406+
"recurrences": [
407+
{
408+
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": "1", "year": "2101" },
409+
"interval": { "minutes": 10 }
410+
}
411+
]
412+
}
413+
],
414+
"accessTime": "20180112-18:32:27.000-0800",
415+
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
416+
}
213417
}
214418
]

0 commit comments

Comments
 (0)