Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Fixes #13
Browse files Browse the repository at this point in the history
Added code to filter more non-CSV results returned by SHOW GRANTS TO and SHOW GRANTS ON commands
  • Loading branch information
sfc-gh-dodievich committed Apr 22, 2022
1 parent 06998ef commit 46c2bc5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net5.0/SFGrantReport.dll",
//"args": ["-l", "C:\\snowflake\\SnowflakeGrantReport\\Reports\\aws_cas1", "-r", "C:\\snowflake\\SnowflakeGrantReport\\Reports\\aws_cas1.offline.08192021", "-o", "C:\\snowflake\\SnowflakeGrantReport\\Reports\\aws_cas1.offline.08192021.compare" ],
"args": ["-i", "C:\\snowflake\\SnowflakeGrantReport\\ACCOUNT_USAGE_INPUT\\COINBASE_Snowhouse", "-o", "C:\\snowflake\\SnowflakeGrantReport\\Reports\\COINBASE.20220314"],
//"args": ["-c", "sfgrantreport", "-o", "C:\\snowflake\\SnowflakeGrantReport\\Reports\\Avalera\\myReports", "-s"],
//"args": ["-i", "C:\\snowflake\\SnowflakeGrantReport\\ACCOUNT_USAGE_INPUT\\COINBASE_Snowhouse", "-o", "C:\\snowflake\\SnowflakeGrantReport\\Reports\\COINBASE.20220314"],
"args": ["-c", "aws_cas2.dodievich", "-o", "C:\\snowflake\\SnowflakeGrantReport\\Reports\\aws_cas2.20220421"],
//"args": ["-i", "C:\\snowflake\\SnowflakeGrantReport\\ACCOUNT_USAGE_INPUT\\COINBASE_Snowhouse", "-o", "C:\\snowflake\\SnowflakeGrantReport\\Reports\\COINBASE.20220314"],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "externalTerminal",
Expand Down
23 changes: 19 additions & 4 deletions Helpers/FileIOHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,10 @@ public static MemoryStream WriteListToMemoryStream<T>(List<T> listToWrite, Class

public static List<T> ReadListFromCSVFile<T>(string csvFilePath, ClassMap<T> classMap)
{
return ReadListFromCSVFile<T>(csvFilePath, classMap, String.Empty);
return ReadListFromCSVFile<T>(csvFilePath, classMap, null);
}

public static List<T> ReadListFromCSVFile<T>(string csvFilePath, ClassMap<T> classMap, string skipRecordPrefix)
public static List<T> ReadListFromCSVFile<T>(string csvFilePath, ClassMap<T> classMap, string[] skipRecordValues)
{
try
{
Expand All @@ -583,9 +583,10 @@ public static List<T> ReadListFromCSVFile<T>(string csvFilePath, ClassMap<T> cla
logger.Warn("Bad thing on row {0}, char {1}, field '{2}'", rc.Row, rc.CharPosition, rc.Field);
logger.Warn(rc.RawRecord);
};
if (skipRecordPrefix.Length > 0)
if (skipRecordValues != null && skipRecordValues.Length > 0)
{
csvReader.Configuration.ShouldSkipRecord = record => record.FirstOrDefault()?.StartsWith(skipRecordPrefix) ?? false;
//csvReader.Configuration.ShouldSkipRecord = record => record.FirstOrDefault()?.StartsWith(skipRecordPrefix) ?? false;
csvReader.Configuration.ShouldSkipRecord = record => ShouldSkipRecordFunction(record, skipRecordValues);
}
return csvReader.GetRecords<T>().ToList();
}
Expand All @@ -600,6 +601,20 @@ public static List<T> ReadListFromCSVFile<T>(string csvFilePath, ClassMap<T> cla
return null;
}

private static bool ShouldSkipRecordFunction(string[] record, string[] skipRecordValues)
{
if (record == null) return true;

if (record.Length == 0) return true;

for (int i = 0; i < skipRecordValues.Length; i++)
{
if (record[0].Contains(skipRecordValues[i]) == true) return true;
}

return false;
}

public static bool AppendTwoCSVFiles(string csvToAppendToFilePath, string csvFromWhichToAppendFilePath)
{
string folderPath = Path.GetDirectoryName(csvToAppendToFilePath);
Expand Down
2 changes: 1 addition & 1 deletion ProcessingSteps/Extract/ExtractUsersRolesGrants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public override bool Execute(ProgramOptions programOptions)

#region Future Grants

List<Grant> grantsToRolesList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Data_RoleShowGrantsTo_FilePath(), new GrantShowGrantsMap(), "No data returned");
List<Grant> grantsToRolesList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Data_RoleShowGrantsTo_FilePath(), new GrantShowGrantsMap(), new string[] {"No data returned", "SQL compilation error", "does not exist"});
if (grantsToRolesList != null)
{
// Get list of all databases and process them
Expand Down
10 changes: 5 additions & 5 deletions ProcessingSteps/Index/IndexGrantDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override bool Execute(ProgramOptions programOptions)

loggerConsole.Info("Process Grants OF");

List<RoleMember> grantsOfRolesAndUsersList = FileIOHelper.ReadListFromCSVFile<RoleMember>(FilePathMap.Data_RoleShowGrantsOf_FilePath(), new RoleMemberShowGrantsMap(), "No data returned");
List<RoleMember> grantsOfRolesAndUsersList = FileIOHelper.ReadListFromCSVFile<RoleMember>(FilePathMap.Data_RoleShowGrantsOf_FilePath(), new RoleMemberShowGrantsMap(), new string[] {"No data returned", "SQL compilation error", "does not exist"});
if (grantsOfRolesAndUsersList != null)
{
foreach (RoleMember roleMember in grantsOfRolesAndUsersList)
Expand All @@ -73,28 +73,28 @@ public override bool Execute(ProgramOptions programOptions)

List<Grant> grantsNonUniqueList = new List<Grant>();

List<Grant> grantsOnRolesList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Data_RoleShowGrantsOn_FilePath(), new GrantShowGrantsMap(), "No data returned");
List<Grant> grantsOnRolesList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Data_RoleShowGrantsOn_FilePath(), new GrantShowGrantsMap(), new string[] {"No data returned", "SQL compilation error", "does not exist"});
if (grantsOnRolesList != null)
{
loggerConsole.Info("Granted ON {0} grants", grantsOnRolesList.Count);
grantsNonUniqueList.AddRange(grantsOnRolesList);
}

List<Grant> grantsToRolesList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Data_RoleShowGrantsTo_FilePath(), new GrantShowGrantsMap(), "No data returned");
List<Grant> grantsToRolesList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Data_RoleShowGrantsTo_FilePath(), new GrantShowGrantsMap(), new string[] {"No data returned", "SQL compilation error", "does not exist"});
if (grantsToRolesList != null)
{
loggerConsole.Info("Granted TO {0} grants", grantsToRolesList.Count);
grantsNonUniqueList.AddRange(grantsToRolesList);
}

List<Grant> grantsFutureDatabasesList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Data_FutureGrantsInDatabases_FilePath(), new GrantShowFutureGrantsMap(), "No data returned");
List<Grant> grantsFutureDatabasesList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Data_FutureGrantsInDatabases_FilePath(), new GrantShowFutureGrantsMap(), new string[] {"No data returned", "SQL compilation error", "does not exist"});
if (grantsFutureDatabasesList != null)
{
loggerConsole.Info("Future Grants on Databases {0} grants", grantsFutureDatabasesList.Count);
grantsNonUniqueList.AddRange(grantsFutureDatabasesList);
}

List<Grant> grantsFutureSchemasList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Data_FutureGrantsInSchemas_FilePath(), new GrantShowFutureGrantsMap(), "No data returned");
List<Grant> grantsFutureSchemasList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Data_FutureGrantsInSchemas_FilePath(), new GrantShowFutureGrantsMap(), new string[] {"No data returned", "SQL compilation error", "does not exist"});
if (grantsFutureSchemasList != null)
{
loggerConsole.Info("Future Grants on Schemas {0} grants", grantsFutureSchemasList.Count);
Expand Down
4 changes: 2 additions & 2 deletions ProcessingSteps/Index/IndexGrantDetailsAccountUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override bool Execute(ProgramOptions programOptions)

List<RoleMember> grantsOfRolesList = new List<RoleMember>();

List<Grant> grantsOnRolesList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Input_RoleShowGrantsToAndOn_FilePath(), new GrantGrantToRolesMap(), "Initiating login request with your identity provider");
List<Grant> grantsOnRolesList = FileIOHelper.ReadListFromCSVFile<Grant>(FilePathMap.Input_RoleShowGrantsToAndOn_FilePath(), new GrantGrantToRolesMap(), new string[] {"Initiating login request with your identity provider"});

if (grantsOnRolesList != null)
{
Expand Down Expand Up @@ -344,7 +344,7 @@ public override bool Execute(ProgramOptions programOptions)

loggerConsole.Info("Process Grants OF Users");

List<RoleMember> grantsOfUsersList = FileIOHelper.ReadListFromCSVFile<RoleMember>(FilePathMap.Input_RoleShowGrantsOf_FilePath(), new RoleMemberGrantsToUsersMap(), "Initiating login request with your identity provider");
List<RoleMember> grantsOfUsersList = FileIOHelper.ReadListFromCSVFile<RoleMember>(FilePathMap.Input_RoleShowGrantsOf_FilePath(), new RoleMemberGrantsToUsersMap(), new string[] {"Initiating login request with your identity provider"});
if (grantsOfUsersList != null)
{
foreach (RoleMember roleMember in grantsOfUsersList)
Expand Down
10 changes: 9 additions & 1 deletion ProcessingSteps/JobStepBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ public static string quoteObjectIdentifier(string objectName)
'{',
'}',
'~',
'`' };
'`',
'@' };
foreach (var c in roleNameSpecialChars)
{
// Escape the embedded "
Expand Down Expand Up @@ -251,6 +252,13 @@ public static string quoteObjectIdentifier(string objectName)
{
return String.Format("\"{0}\"", objectName);
}

// Finally check for lowercase of the objects. If lowercase exists, quote them
if (objectName.Any(char.IsLower) == true)
{
return String.Format("\"{0}\"", objectName);
}

return objectName;
}

Expand Down

0 comments on commit 46c2bc5

Please sign in to comment.