Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate log when permissions is .Read for write operations #299

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions ApiDoctor.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2846,9 +2846,10 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
case PermissionsInsertionState.InsertPermissionBlock:
var includeFileMetadata = "---\r\ndescription: \"Automatically generated file. DO NOT MODIFY\"\r\nms.topic: include\r\nms.localizationpriority: medium\r\n---\r\n\r\n";
var permissionFileContents = string.Empty;
var existingPermissionsTable = originalFileContents.Skip(insertionStartLine + 2).Take(insertionEndLine - insertionStartLine - 1);

if (!isBootstrapped)
{
var existingPermissionsTable = originalFileContents.Skip(insertionStartLine + 2).Take(insertionEndLine - insertionStartLine - 1);
{
permissionFileContents = $"{includeFileMetadata}{ConvertToThreeColumnPermissionsTable(existingPermissionsTable)}";
}

Expand All @@ -2866,8 +2867,14 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
var httpRequests = (requestUrlsForPermissions?.Length ?? 0) != 0
? requestUrlsForPermissions
: originalFileContents.Skip(httpRequestStartLine + 1).Take(httpRequestEndLine - httpRequestStartLine - 1).Where(x => !string.IsNullOrWhiteSpace(x));



var newPermissionFileContents = GetPermissionsMarkdownTableForHttpRequestBlock(permissionsDocument, httpRequests,
mergePermissions, docFile.DisplayName, foundPermissionTablesOrBlocks); // get from Kibali

CheckForPermissionsAccuracy(newPermissionFileContents.Split(Environment.NewLine), httpRequests, docFile.DisplayName);

if (!string.IsNullOrWhiteSpace(newPermissionFileContents))
{
permissionFileContents = $"{includeFileMetadata}{newPermissionFileContents}";
Expand Down Expand Up @@ -3039,6 +3046,51 @@ private static CodeBlockAnnotation ExtractCodeBlockAnnotationForPermissionsTable
return null;
}

private static void CheckForPermissionsAccuracy(IEnumerable<string> tableRows, IEnumerable<string> httpRequests, string fileName)
{
try
{
var methods = new string[] { "PUT", "PATCH", "PUT", "POST", "DELETE" };
foreach (var r in httpRequests)
{
var method = r.Split(" ").First();
if (methods.Contains(method))
{
foreach (var row in tableRows)
{
string[] cells = Regex.Split(row.Trim(), @"\s*\|\s*").Where(static x => !string.IsNullOrWhiteSpace(x)).ToArray();
if (cells.Length == 3)
{
var permissionType = cells[0];

var perms = cells[1].Split(",").ToList();
var higher = cells[2].Split(",");
foreach (var h in higher)
{
perms.Add(h);
}

foreach (var perm in perms)
{
if (perm.EndsWith("Read") || perm.Contains(".Read."))
{
Console.WriteLine($"Incorrect permissions for HTTPRequest {string.Join(" ", httpRequests)}; docFike {fileName}; permissions {string.Join(",", perms.Where(x => x.EndsWith("Read") || x.Contains(".Read.")))}; Scope {permissionType}");
}
}

}
}

break;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error checking permissions accuracy: {ex.Message} for {fileName}");
}
}

private static string ConvertToThreeColumnPermissionsTable(IEnumerable<string> tableRows)
{
var tableString = new StringBuilder("|Permission type|Least privileged permissions|Higher privileged permissions|");
Expand Down
Loading