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

NEW @W-16360876@ Implemented describeRules() functionality for FlowTest #93

Merged
merged 11 commits into from
Oct 2, 2024

Conversation

jfeingold35
Copy link
Contributor

@jfeingold35 jfeingold35 commented Sep 25, 2024

This PR does the following:

  • Deletes a bunch of files from the locally stored FlowTest folder that never should have been committed in the first place.
  • Updates several files within the locally stored FlowTest folder to provide the new API for rule description.
  • Utilizes that API to get descriptions for the FlowTest queries, and converts those into the CodeAnalyzer house style.

return flowTestRules.map(flowTestRule => {
return {
// The name maps directly over.
name: flowTestRule.query_name,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query_name field has spaces in it, and the query_id field has periods in it. So I chose to use the one with spaces in it for now. We can have a discussion about what postprocessing might be needed to bring the names more into line with our best practices.

];

let stdout: string = '';
const processStdout = (stdoutMsg: string) => {
stdout += stdoutMsg;
}
await this.pythonCommandExecutor.exec(pythonArgs, processStdout);
return stdout;
return (JSON.parse(stdout) as FlowTestRuleDescriptor[]).sort((a, b) => a.query_name.localeCompare(b.query_name));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sorting is necessary because the output printed to stdout isn't sorted at this time. Robert intends to update it with sorting, and when he does I will change it here too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that code coverage for this flowtest package is no longer 100% like all other packages

 code-analyzer-flowtest-engine/src        |     100 |      100 |     100 |     100 |                   
  config.ts                               |     100 |      100 |     100 |     100 |                   
  engine.ts                               |     100 |      100 |     100 |     100 |                   
  messages.ts                             |     100 |      100 |     100 |     100 |                   
  plugin.ts                               |     100 |      100 |     100 |     100 |                   
 code-analyzer-flowtest-engine/src/python |   90.56 |    44.44 |   77.77 |   92.15 |                   
  FlowTestCommandWrapper.ts               |     100 |      100 |     100 |     100 |                   
  PythonCommandExecutor.ts                |   78.26 |    28.57 |      70 |   80.95 | 35-45             
  PythonVersionIdentifier.ts              |     100 |      100 |      80 |     100 |                     

If possible, can we either add in code coverage for anything to be submitted or add in the ignore comments where code coverage isn't easily able to be covered?

Or will these be hit when the run method is implemented?

Copy link
Collaborator

@stephen-carter-at-sf stephen-carter-at-sf Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved: Now I see

------------------------------------------|---------|----------|---------|---------|-------------------
File                                      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------------------------------------|---------|----------|---------|---------|-------------------
  PythonVersionIdentifier.ts              |     100 |      100 |      80 |     100 |           

Maybe there is 1 function that isn't fully hit.

@@ -23,24 +24,60 @@ export class FlowTestEngine extends Engine {

public async describeRules(_describeOptions: DescribeOptions): Promise<RuleDescription[]> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The describe command should be looking at the workspace if it is provided to see if the workspace even contains relevant files (in this case, flow based xml files). If there is a workspace and there are no relevant files, then no rules should be returned. That is, we shouldn't be doing any work if there are no flow files in the workspace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the extensions the FlowTest tool looks for are .flow-meta.xml and .flow. So I'll make the plugin look for those too.

this.emitRunRulesProgressEvent(100);
return {
violations: []
};
}

private convertFlowTestRulesToCodeAnalyzerRules(flowTestRules: FlowTestRuleDescriptor[]): RuleDescription[] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Can these private methods instead be functions?

Also maybe call this function toRuleDescription(flowTestRule: FlowTestRuleDiscriptor) and then simply make your line 29 to be

const ruleDescriptions: RuleDescription[] = flowTestRules.map(toRuleDescription);

return [];
const flowTestRules: FlowTestRuleDescriptor[] = await this.commandWrapper.getFlowTestRuleDescriptions();
this.emitDescribeRulesProgressEvent(75);
const convertedRules = flowTestRules.map(r => toRuleDescription(r));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is now a function (and not tied to a class instance), I believe you could have also just done flowTestRules.map(toRuleDescription)

function toRuleDescription(flowTestRule: FlowTestRuleDescriptor): RuleDescription {
return {
// The name maps directly over.
name: flowTestRule.query_name,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like in the catalog.goldfile.json that an example query_name is Flow: SystemModeWithSharing recordUpdates data and here it looks like we are using this as the rule name. This does not seem good to me.

Robert closed the Enhancement Request for rules having shorter names (see https://git.soma.salesforce.com/SecurityTools/FlowSecurityLinter/issues/39) ... but it looks like he maintained spaces and colons in the names. For example I see

query_name = f"Flow Security: User access to {elem_type} {check_labels_val} in mode {run_mode}"
got turned into

query_name = f"Flow: {run_mode} {elem_type} {check_labels_val}"

which is still too long.

So either we need to go back to him and make these names even shorter without spaces and colons, or we'll need to create a mapping on our end (either fixed or dynamic).

For example on our end we could probably just remove the prefix "Flow: " and then convert all spaces to dashes for now or something. So instead of
Flow: SystemModeWithSharing recordUpdates data maybe the name could for now just be SystemModeWithSharing-recordUpdates-data

But I created https://git.soma.salesforce.com/SecurityTools/FlowSecurityLinter/issues/47 to ask that we may receive better names.

const workspaceFiles: string[] = await describeOptions.workspace.getExpandedFiles();
// If a workspace is provided but it contains no flow files, then return no rules.
if (!workspaceFiles.some(fileIsFlowFile)) {
this.emitLogEvent(LogLevel.Fine, 'Workspace contains no Flow files; returning no rules');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems relevant to possible user debugging... so maybe make this to LogLevel.Debug instead of LogLevel.Fine.

@jfeingold35 jfeingold35 marked this pull request as ready for review October 2, 2024 16:29
@jfeingold35 jfeingold35 merged commit abf6cd8 into dev Oct 2, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants