213213import java .util .Set ;
214214import java .util .StringTokenizer ;
215215import java .util .TreeSet ;
216+ import java .util .regex .Pattern ;
216217import java .util .stream .Collectors ;
217218
218219import static org .apache .ranger .db .XXGlobalStateDao .RANGER_GLOBAL_STATE_NAME_GDS ;
@@ -255,6 +256,9 @@ public class ServiceDBStore extends AbstractServiceStore {
255256 private static final String FILE_HEADER = "ID|Name|Resources|Roles|Groups|Users|Accesses|Service Type|Status|Policy Type|Delegate Admin|isRecursive|isExcludes|Service Name|Description|isAuditEnabled|Policy Conditions|Policy Condition Type|Masking Options|Row Filter Expr|Policy Label Name" ;
256257 private static final String COMMA_DELIMITER = "|" ;
257258
259+ private static final String DEFAULT_CSV_SANITIZATION_PATTERN = "^[=+\\ -@\\ t\\ r]" ;
260+ private static final Pattern CSV_SANITIZATION_PATTERN = Pattern .compile (PropertiesUtil .getProperty ("ranger.admin.csv.sanitization.pattern" , DEFAULT_CSV_SANITIZATION_PATTERN ));
261+
258262 private static final Comparator <RangerPolicyDelta > POLICY_DELTA_ID_COMPARATOR = new RangerPolicyDeltaComparator ();
259263
260264 public static boolean SUPPORTS_POLICY_DELTAS ;
@@ -4731,7 +4735,6 @@ private void writeCSVForPolicyItems(Map<String, String> svcNameToSvcType, Ranger
47314735 String userNames = "" ;
47324736 String policyLabelName = "" ;
47334737 String accessType = "" ;
4734- String policyType = "" ;
47354738 Boolean delegateAdmin = false ;
47364739 String isExcludesValue = "" ;
47374740 String maskingInfo = "" ;
@@ -4898,28 +4901,6 @@ private void writeCSVForPolicyItems(Map<String, String> svcNameToSvcType, Ranger
48984901 policyConditionTypeValue = "" ;
48994902 }
49004903
4901- String policyStatus ;
4902-
4903- if (policy .getIsEnabled ()) {
4904- policyStatus = "Enabled" ;
4905- } else {
4906- policyStatus = "Disabled" ;
4907- }
4908-
4909- int policyTypeInt = policy .getPolicyType ();
4910-
4911- switch (policyTypeInt ) {
4912- case RangerPolicy .POLICY_TYPE_ACCESS :
4913- policyType = POLICY_TYPE_ACCESS ;
4914- break ;
4915- case RangerPolicy .POLICY_TYPE_DATAMASK :
4916- policyType = POLICY_TYPE_DATAMASK ;
4917- break ;
4918- case RangerPolicy .POLICY_TYPE_ROWFILTER :
4919- policyType = POLICY_TYPE_ROWFILTER ;
4920- break ;
4921- }
4922-
49234904 if (CollectionUtils .isNotEmpty (policyLabels )) {
49244905 for (String policyLabel : policyLabels ) {
49254906 if (StringUtils .isNotBlank (policyLabel )) {
@@ -4936,49 +4917,53 @@ private void writeCSVForPolicyItems(Map<String, String> svcNameToSvcType, Ranger
49364917
49374918 csvBuffer .append (policy .getId ());
49384919 csvBuffer .append (COMMA_DELIMITER );
4939- csvBuffer .append (policyName );
4920+ csvBuffer .append (sanitizeCell ( policyName ) );
49404921 csvBuffer .append (COMMA_DELIMITER );
4941- csvBuffer .append (resourceKeyVal );
4922+ csvBuffer .append (sanitizeCell ( resourceKeyVal ) );
49424923 csvBuffer .append (COMMA_DELIMITER );
4943- csvBuffer .append (roleNames );
4924+ csvBuffer .append (sanitizeCell ( roleNames ) );
49444925 csvBuffer .append (COMMA_DELIMITER );
4945- csvBuffer .append (groupNames );
4926+ csvBuffer .append (sanitizeCell ( groupNames ) );
49464927 csvBuffer .append (COMMA_DELIMITER );
4947- csvBuffer .append (userNames );
4928+ csvBuffer .append (sanitizeCell ( userNames ) );
49484929 csvBuffer .append (COMMA_DELIMITER );
49494930 csvBuffer .append (accessType .trim ());
49504931 csvBuffer .append (COMMA_DELIMITER );
4951- csvBuffer .append (serviceType );
4932+ csvBuffer .append (sanitizeCell ( serviceType ) );
49524933 csvBuffer .append (COMMA_DELIMITER );
4953- csvBuffer .append (policyStatus );
4934+ csvBuffer .append (policy . getIsEnabled () ? "Enabled" : "Disabled" );
49544935 csvBuffer .append (COMMA_DELIMITER );
4955- csvBuffer .append (policyType );
4936+ csvBuffer .append (getPolicyTypeString ( policy . getPolicyType ()) );
49564937 csvBuffer .append (COMMA_DELIMITER );
49574938 csvBuffer .append (delegateAdmin .toString ().toUpperCase ());
49584939 csvBuffer .append (COMMA_DELIMITER );
49594940 csvBuffer .append (isRecursiveValue );
49604941 csvBuffer .append (COMMA_DELIMITER );
49614942 csvBuffer .append (isExcludesValue );
49624943 csvBuffer .append (COMMA_DELIMITER );
4963- csvBuffer .append (serviceName );
4944+ csvBuffer .append (sanitizeCell ( serviceName ) );
49644945 csvBuffer .append (COMMA_DELIMITER );
4965- csvBuffer .append (description );
4946+ csvBuffer .append (sanitizeCell ( description ) );
49664947 csvBuffer .append (COMMA_DELIMITER );
49674948 csvBuffer .append (isAuditEnabled .toString ().toUpperCase ());
49684949 csvBuffer .append (COMMA_DELIMITER );
4969- csvBuffer .append (conditionKeyValue .trim ());
4950+ csvBuffer .append (sanitizeCell ( conditionKeyValue .trim () ));
49704951 csvBuffer .append (COMMA_DELIMITER );
4971- csvBuffer .append (policyConditionTypeValue );
4952+ csvBuffer .append (sanitizeCell ( policyConditionTypeValue ) );
49724953 csvBuffer .append (COMMA_DELIMITER );
4973- csvBuffer .append (maskingInfo );
4954+ csvBuffer .append (sanitizeCell ( maskingInfo ) );
49744955 csvBuffer .append (COMMA_DELIMITER );
4975- csvBuffer .append (filterExpr );
4956+ csvBuffer .append (sanitizeCell ( filterExpr ) );
49764957 csvBuffer .append (COMMA_DELIMITER );
4977- csvBuffer .append (policyLabelName );
4958+ csvBuffer .append (sanitizeCell ( policyLabelName ) );
49784959 csvBuffer .append (COMMA_DELIMITER );
49794960 csvBuffer .append (LINE_SEPARATOR );
49804961 }
49814962
4963+ private String sanitizeCell (String value ) {
4964+ return (value != null && !value .isEmpty () && CSV_SANITIZATION_PATTERN .matcher (value ).find ()) ? " " + value : value ;
4965+ }
4966+
49824967 private <T > void writeJson (List <T > objList , String jsonFileName , HttpServletResponse response , JSON_FILE_NAME_TYPE type ) {
49834968 response .setContentType ("text/json" );
49844969 response .setHeader ("Content-Disposition" , "attachment; filename=" + jsonFileName );
@@ -5044,18 +5029,11 @@ private void writeBookForPolicyItems(Map<String, String> svcNameToSvcType, Range
50445029 String userNames = "" ;
50455030 String policyLabelNames = "" ;
50465031 String accessType = "" ;
5047- String policyStatus ;
5048- String policyType = "" ;
50495032 Boolean delegateAdmin = false ;
50505033 String isRecursive ;
50515034 String isExcludes ;
5052- String serviceName ;
5053- String description ;
50545035 Boolean isAuditEnabled = policy .getIsAuditEnabled ();
50555036 String isExcludesValue = "" ;
5056- Cell cell = row .createCell (0 );
5057-
5058- cell .setCellValue (policy .getId ());
50595037
50605038 List <RangerPolicyItemAccess > accesses = new ArrayList <>();
50615039 List <RangerPolicyItemCondition > conditionsList = new ArrayList <>();
@@ -5072,11 +5050,8 @@ private void writeBookForPolicyItems(Map<String, String> svcNameToSvcType, Range
50725050 RangerPolicy .RangerPolicyItemDataMaskInfo dataMaskInfo ;
50735051 RangerPolicy .RangerPolicyItemRowFilterInfo filterInfo ;
50745052
5075- cell = row .createCell (1 );
5076-
5077- cell .setCellValue (policy .getName ());
5078-
5079- cell = row .createCell (2 );
5053+ row .createCell (0 ).setCellValue (policy .getId ());
5054+ row .createCell (1 ).setCellValue (sanitizeCell (policy .getName ()));
50805055
50815056 if (resources != null ) {
50825057 for (Entry <String , RangerPolicyResource > resource : resources .entrySet ()) {
@@ -5101,7 +5076,7 @@ private void writeBookForPolicyItems(Map<String, String> svcNameToSvcType, Range
51015076 resourceKeyVal = sb .toString ();
51025077 resourceKeyVal = resourceKeyVal .substring (1 );
51035078
5104- cell . setCellValue (resourceKeyVal );
5079+ row . createCell ( 2 ). setCellValue (sanitizeCell ( resourceKeyVal ) );
51055080
51065081 if (policyItem != null && dataMaskPolicyItem == null && rowFilterPolicyItem == null ) {
51075082 roles = policyItem .getRoles ();
@@ -5128,8 +5103,7 @@ private void writeBookForPolicyItems(Map<String, String> svcNameToSvcType, Range
51285103 maskingInfo = maskingInfo + "; conditionExpr=[" + conditionExpr + "]" ;
51295104 }
51305105
5131- cell = row .createCell (18 );
5132- cell .setCellValue (maskingInfo );
5106+ row .createCell (18 ).setCellValue (sanitizeCell (maskingInfo ));
51335107 } else if (rowFilterPolicyItem != null && policyItem == null && dataMaskPolicyItem == null ) {
51345108 roles = rowFilterPolicyItem .getRoles ();
51355109 groups = rowFilterPolicyItem .getGroups ();
@@ -5141,9 +5115,7 @@ private void writeBookForPolicyItems(Map<String, String> svcNameToSvcType, Range
51415115
51425116 String filterExpr = filterInfo .getFilterExpr ();
51435117
5144- cell = row .createCell (19 );
5145-
5146- cell .setCellValue (filterExpr );
5118+ row .createCell (19 ).setCellValue (sanitizeCell (filterExpr ));
51475119 }
51485120
51495121 if (CollectionUtils .isNotEmpty (accesses )) {
@@ -5194,15 +5166,10 @@ private void writeBookForPolicyItems(Map<String, String> svcNameToSvcType, Range
51945166 conditionKeyValue = conditionType + "=" + conditionValue ;
51955167 }
51965168
5197- cell = row .createCell (3 );
5198- cell .setCellValue (roleNames );
5199- cell = row .createCell (4 );
5200- cell .setCellValue (groupNames );
5201- cell = row .createCell (5 );
5202- cell .setCellValue (userNames );
5203- cell = row .createCell (6 );
5204- cell .setCellValue (accessType .trim ());
5205- cell = row .createCell (7 );
5169+ row .createCell (3 ).setCellValue (sanitizeCell (roleNames ));
5170+ row .createCell (4 ).setCellValue (sanitizeCell (groupNames ));
5171+ row .createCell (5 ).setCellValue (sanitizeCell (userNames ));
5172+ row .createCell (6 ).setCellValue (accessType .trim ());
52065173
52075174 String serviceType = policy .getServiceType ();
52085175
@@ -5224,16 +5191,19 @@ private void writeBookForPolicyItems(Map<String, String> svcNameToSvcType, Range
52245191 policyConditionTypeValue = "" ;
52255192 }
52265193
5227- cell .setCellValue (serviceType );
5228-
5229- cell = row .createCell (8 );
5194+ row .createCell (7 ).setCellValue (sanitizeCell (serviceType ));
52305195 }
52315196
5232- if (policy .getIsEnabled ()) {
5233- policyStatus = "Enabled" ;
5234- } else {
5235- policyStatus = "Disabled" ;
5236- }
5197+ row .createCell (8 ).setCellValue (policy .getIsEnabled () ? "Enabled" : "Disabled" );
5198+ row .createCell (9 ).setCellValue (getPolicyTypeString (policy .getPolicyType ()));
5199+ row .createCell (10 ).setCellValue (delegateAdmin .toString ().toUpperCase ());
5200+ row .createCell (11 ).setCellValue (isRecursiveValue );
5201+ row .createCell (12 ).setCellValue (isExcludesValue );
5202+ row .createCell (13 ).setCellValue (sanitizeCell (policy .getService ()));
5203+ row .createCell (14 ).setCellValue (sanitizeCell (policy .getDescription ()));
5204+ row .createCell (15 ).setCellValue (isAuditEnabled .toString ().toUpperCase ());
5205+ row .createCell (16 ).setCellValue (sanitizeCell (conditionKeyValue .trim ()));
5206+ row .createCell (17 ).setCellValue (sanitizeCell (policyConditionTypeValue ));
52375207
52385208 policyLabels = policy .getPolicyLabels ();
52395209
@@ -5247,47 +5217,20 @@ private void writeBookForPolicyItems(Map<String, String> svcNameToSvcType, Range
52475217 }
52485218 }
52495219
5250- cell .setCellValue (policyStatus );
5251-
5252- cell = row .createCell (9 );
5253-
5254- int policyTypeInt = policy .getPolicyType ();
5220+ row .createCell (20 ).setCellValue (sanitizeCell (policyLabelNames ));
5221+ }
52555222
5256- switch (policyTypeInt ) {
5223+ private String getPolicyTypeString (int policyType ) {
5224+ switch (policyType ) {
52575225 case RangerPolicy .POLICY_TYPE_ACCESS :
5258- policyType = POLICY_TYPE_ACCESS ;
5259- break ;
5260-
5226+ return POLICY_TYPE_ACCESS ;
52615227 case RangerPolicy .POLICY_TYPE_DATAMASK :
5262- policyType = POLICY_TYPE_DATAMASK ;
5263- break ;
5264-
5228+ return POLICY_TYPE_DATAMASK ;
52655229 case RangerPolicy .POLICY_TYPE_ROWFILTER :
5266- policyType = POLICY_TYPE_ROWFILTER ;
5267- break ;
5230+ return POLICY_TYPE_ROWFILTER ;
5231+ default :
5232+ return "" ;
52685233 }
5269-
5270- cell .setCellValue (policyType );
5271- cell = row .createCell (10 );
5272- cell .setCellValue (delegateAdmin .toString ().toUpperCase ());
5273- cell = row .createCell (11 );
5274- cell .setCellValue (isRecursiveValue );
5275- cell = row .createCell (12 );
5276- cell .setCellValue (isExcludesValue );
5277- cell = row .createCell (13 );
5278- serviceName = policy .getService ();
5279- cell .setCellValue (serviceName );
5280- cell = row .createCell (14 );
5281- description = policy .getDescription ();
5282- cell .setCellValue (description );
5283- cell = row .createCell (15 );
5284- cell .setCellValue (isAuditEnabled .toString ().toUpperCase ());
5285- cell = row .createCell (16 );
5286- cell .setCellValue (conditionKeyValue .trim ());
5287- cell = row .createCell (17 );
5288- cell .setCellValue (policyConditionTypeValue );
5289- cell = row .createCell (20 );
5290- cell .setCellValue (policyLabelNames );
52915234 }
52925235
52935236 private void createHeaderRow (Sheet sheet ) {
0 commit comments