2626import org .apache .ranger .plugin .model .RangerPolicy .RangerDataMaskPolicyItem ;
2727import org .apache .ranger .plugin .model .RangerPolicy .RangerPolicyItem ;
2828import org .apache .ranger .plugin .model .RangerPolicy .RangerPolicyItemAccess ;
29+ import org .apache .ranger .plugin .model .RangerPolicy .RangerPolicyItemDataMaskInfo ;
2930import org .apache .ranger .plugin .model .RangerPolicy .RangerPolicyResource ;
3031import org .apache .ranger .plugin .model .RangerPolicy .RangerRowFilterPolicyItem ;
3132import org .apache .ranger .plugin .model .RangerPolicyResourceSignature ;
5051public class RangerPolicyValidator extends RangerValidator {
5152 private static final Logger LOG = LoggerFactory .getLogger (RangerPolicyValidator .class );
5253
53- private static final List <String > INVALID_ITEMS = new ArrayList <>(Arrays .asList ("null" , "NULL" , "Null" , null ));
54+ private static final Set <String > INVALID_POLICY_ITEM_VALUES = new HashSet <>(Arrays .asList ("null" , "NULL" , "Null" , null , "" ));
5455
5556 public RangerPolicyValidator (ServiceStore store ) {
5657 super (store );
@@ -430,6 +431,14 @@ boolean isValid(RangerPolicy policy, Action action, boolean isAdmin, List<Valida
430431 valid = isValidPolicyItems (policy .getDenyPolicyItems (), failures , serviceDef ) && valid ;
431432 valid = isValidPolicyItems (policy .getAllowExceptions (), failures , serviceDef ) && valid ;
432433 valid = isValidPolicyItems (policy .getDenyExceptions (), failures , serviceDef ) && valid ;
434+
435+ @ SuppressWarnings ("unchecked" )
436+ List <RangerPolicyItem > dataMaskPolicyItems = (List <RangerPolicyItem >) (List <?>) policy .getDataMaskPolicyItems ();
437+ valid = isValidPolicyItems (dataMaskPolicyItems , failures , serviceDef ) && valid ;
438+
439+ @ SuppressWarnings ("unchecked" )
440+ List <RangerPolicyItem > rowFilterPolicyItems = (List <RangerPolicyItem >) (List <?>) policy .getRowFilterPolicyItems ();
441+ valid = isValidPolicyItems (rowFilterPolicyItems , failures , serviceDef ) && valid ;
433442 }
434443 }
435444
@@ -1052,6 +1061,20 @@ boolean isValidPolicyItem(RangerPolicyItem policyItem, List<ValidationFailureDet
10521061 if (policyItem == null ) {
10531062 LOG .debug ("policy item was null!" );
10541063 } else {
1064+ if (policyItem instanceof RangerDataMaskPolicyItem ) {
1065+ RangerPolicyItemDataMaskInfo dataMaskInfo = ((RangerDataMaskPolicyItem ) policyItem ).getDataMaskInfo ();
1066+ if (StringUtils .isBlank (dataMaskInfo .getDataMaskType ())) {
1067+ ValidationErrorCode error = ValidationErrorCode .POLICY_VALIDATION_ERR_NULL_POLICY_ITEM ;
1068+ failures .add (new ValidationFailureDetailsBuilder ()
1069+ .field ("policy item datamask-type" )
1070+ .isMissing ()
1071+ .becauseOf (error .getMessage ("policy item datamask-type" ))
1072+ .errorCode (error .getErrorCode ())
1073+ .build ());
1074+
1075+ valid = false ;
1076+ }
1077+ }
10551078 // access items collection can't be empty (unless delegated admin is true) and should be otherwise valid
10561079 if (CollectionUtils .isEmpty (policyItem .getAccesses ())) {
10571080 if (!Boolean .TRUE .equals (policyItem .getDelegateAdmin ())) {
@@ -1089,7 +1112,7 @@ boolean isValidPolicyItem(RangerPolicyItem policyItem, List<ValidationFailureDet
10891112 removeDuplicates (policyItem .getGroups ());
10901113 removeDuplicates (policyItem .getRoles ());
10911114
1092- if (CollectionUtils .isNotEmpty (policyItem .getUsers ()) && CollectionUtils .containsAny (policyItem .getUsers (), INVALID_ITEMS )) {
1115+ if (CollectionUtils .isNotEmpty (policyItem .getUsers ()) && CollectionUtils .containsAny (policyItem .getUsers (), INVALID_POLICY_ITEM_VALUES )) {
10931116 ValidationErrorCode error = ValidationErrorCode .POLICY_VALIDATION_ERR_NULL_POLICY_ITEM_USER ;
10941117
10951118 failures .add (new ValidationFailureDetailsBuilder ()
@@ -1102,7 +1125,7 @@ boolean isValidPolicyItem(RangerPolicyItem policyItem, List<ValidationFailureDet
11021125 valid = false ;
11031126 }
11041127
1105- if (CollectionUtils .isNotEmpty (policyItem .getGroups ()) && CollectionUtils .containsAny (policyItem .getGroups (), INVALID_ITEMS )) {
1128+ if (CollectionUtils .isNotEmpty (policyItem .getGroups ()) && CollectionUtils .containsAny (policyItem .getGroups (), INVALID_POLICY_ITEM_VALUES )) {
11061129 ValidationErrorCode error = ValidationErrorCode .POLICY_VALIDATION_ERR_NULL_POLICY_ITEM_GROUP ;
11071130
11081131 failures .add (new ValidationFailureDetailsBuilder ()
@@ -1115,7 +1138,7 @@ boolean isValidPolicyItem(RangerPolicyItem policyItem, List<ValidationFailureDet
11151138 valid = false ;
11161139 }
11171140
1118- if (CollectionUtils .isNotEmpty (policyItem .getRoles ()) && CollectionUtils .containsAny (policyItem .getRoles (), INVALID_ITEMS )) {
1141+ if (CollectionUtils .isNotEmpty (policyItem .getRoles ()) && CollectionUtils .containsAny (policyItem .getRoles (), INVALID_POLICY_ITEM_VALUES )) {
11191142 ValidationErrorCode error = ValidationErrorCode .POLICY_VALIDATION_ERR_NULL_POLICY_ITEM_ROLE ;
11201143
11211144 failures .add (new ValidationFailureDetailsBuilder ()
@@ -1281,6 +1304,7 @@ private static void removeDuplicates(List<String> values) {
12811304
12821305 HashSet <String > uniqueElements = new HashSet <>();
12831306
1307+ values .replaceAll (e -> e == null ? null : e .trim ());
12841308 values .removeIf (e -> !uniqueElements .add (e ));
12851309 }
12861310}
0 commit comments