Skip to content

Commit

Permalink
🐛 Pass all associated rule labels to analyzer
Browse files Browse the repository at this point in the history
Signed-off-by: ibolton336 <[email protected]>
  • Loading branch information
ibolton336 committed Jul 3, 2023
1 parent cd9ecaf commit 3f9ed1f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 20 deletions.
1 change: 1 addition & 0 deletions client/src/app/api/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ export interface Ruleset {
export interface Metadata {
target: string;
source?: string;
otherLabels?: string[];
}
export interface Rule {
name: string;
Expand Down
5 changes: 4 additions & 1 deletion client/src/app/common/CustomRules/rules-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ export const getLabels = (labels: string[]) =>
{
sourceLabel: sourceValue ? label : map.sourceLabel,
targetLabel: targetValue ? label : map.targetLabel,
otherLabels: [...map.otherLabels, label],
otherLabels:
!sourceValue && !targetValue
? [...map.otherLabels, label]
: map.otherLabels,

Check warning on line 132 in client/src/app/common/CustomRules/rules-utils.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/common/CustomRules/rules-utils.tsx#L131-L132

Added lines #L131 - L132 were not covered by tests
allLabels: [...map.allLabels, label],
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const AnalysisWizard: React.FC<IAnalysisWizard> = ({
artifact: null,
mode: "binary",
formTargets: [],
formOtherLabels: [],
selectedFormSources: [],
formSources: defaultSources,
formRulesets: [],
Expand Down Expand Up @@ -256,10 +257,13 @@ export const AnalysisWizard: React.FC<IAnalysisWizard> = ({
},
rules: {
labels: {
included: [
...fieldValues.formTargets,
...fieldValues.selectedFormSources,
],
included: Array.from(
new Set<string>([
...fieldValues.formTargets,
...fieldValues.selectedFormSources,
...fieldValues.formOtherLabels,
])
),
excluded: [],
},
path: fieldValues.customRulesFiles.length > 0 ? "/rules" : "",
Expand Down
25 changes: 23 additions & 2 deletions client/src/app/pages/applications/analysis-wizard/custom-rules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ export const CustomRules: React.FC<CustomRulesProps> = (props) => {
useFormContext<AnalysisWizardFormValues>();
const values = getValues();

const { formSources, formTargets, customRulesFiles, rulesKind } = watch();
const {
formSources,
formTargets,
formOtherLabels,
customRulesFiles,
rulesKind,
} = watch();

Check warning on line 76 in client/src/app/pages/applications/analysis-wizard/custom-rules.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/custom-rules.tsx#L76

Added line #L76 was not covered by tests
const initialActiveTabKeyValue = (value: string): number =>
value === "manual" ? 0 : value === "repository" ? 1 : 0;

Expand Down Expand Up @@ -440,13 +446,28 @@ export const CustomRules: React.FC<CustomRulesProps> = (props) => {
shouldDirty: true,
});
updatedCustomRulesFiles.forEach((file) => {
const { source, target } = parseRules(file);
const { source, target, otherLabels } = parseRules(file);

Check warning on line 449 in client/src/app/pages/applications/analysis-wizard/custom-rules.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/custom-rules.tsx#L449

Added line #L449 was not covered by tests
if (source && !formSources.includes(source)) {
setValue("formSources", [...formSources, source]);
}
if (target && !formTargets.includes(target)) {
setValue("formTargets", [...formTargets, target]);
}
if (
otherLabels?.length &&
formOtherLabels.some(
(otherLabel) => !otherLabels.includes(otherLabel)

Check warning on line 459 in client/src/app/pages/applications/analysis-wizard/custom-rules.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/custom-rules.tsx#L458-L459

Added lines #L458 - L459 were not covered by tests
)
) {
const newOtherLabels = otherLabels.filter(
(otherLabel) => !formOtherLabels.includes(otherLabel)

Check warning on line 463 in client/src/app/pages/applications/analysis-wizard/custom-rules.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/custom-rules.tsx#L462-L463

Added lines #L462 - L463 were not covered by tests
);

setValue("formOtherLabels", [

Check warning on line 466 in client/src/app/pages/applications/analysis-wizard/custom-rules.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/custom-rules.tsx#L466

Added line #L466 was not covered by tests
...formOtherLabels,
...newOtherLabels,
]);
}
});
setRuleFiles([]);
setUploadError("");
Expand Down
2 changes: 2 additions & 0 deletions client/src/app/pages/applications/analysis-wizard/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const useModeStepSchema = ({

export interface TargetsStepValues {
formTargets: string[];
formOtherLabels: string[];
formRulesets: Ruleset[];
}
export const rulesetSchema: yup.SchemaOf<Ruleset> = yup.object({
Expand All @@ -79,6 +80,7 @@ const useTargetsStepSchema = (): yup.SchemaOf<TargetsStepValues> => {
const { t } = useTranslation();
return yup.object({
formTargets: yup.array(),
formOtherLabels: yup.array(),
formRulesets: yup.array().of(rulesetSchema),
});
};
Expand Down
53 changes: 41 additions & 12 deletions client/src/app/pages/applications/analysis-wizard/set-targets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const SetTargets: React.FC = () => {
const formTargets = watch("formTargets");
const formRulesets = watch("formRulesets");
const formSources = watch("formSources");
const formOtherLabels = watch("formOtherLabels");

Check warning on line 33 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L33

Added line #L33 was not covered by tests

const handleOnSelectedCardTargetChange = (
selectedRuleTarget: string,
Expand Down Expand Up @@ -72,37 +73,65 @@ export const SetTargets: React.FC = () => {
.includes(formTarget)
);

const otherSelectedrulesets = formRulesets.filter(
const otherSelectedRulesets = formRulesets.filter(

Check warning on line 76 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L76

Added line #L76 was not covered by tests
(formRuleset) => selectedRuleset.id !== formRuleset.id
);

const otherSelectedOtherLabels = formOtherLabels.filter(

Check warning on line 80 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L80

Added line #L80 was not covered by tests
(label) =>
!selectedRuleset.rules
.flatMap((rule) => rule?.metadata?.otherLabels)

Check warning on line 83 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L82-L83

Added lines #L82 - L83 were not covered by tests
.includes(label)
);

if (isSelecting) {
const definedSelectedSources: string[] = selectedRuleset.rules
.map((rulesets) => rulesets?.metadata?.source || "")
.filter((source) => !!source);
const definedSelectedOtherLabels: string[] = Array.from(

Check warning on line 88 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L88

Added line #L88 was not covered by tests
new Set(
selectedRuleset.rules
.flatMap((rulesets) => rulesets?.metadata?.otherLabels || "")
.filter((otherLabel) => otherLabel!)

Check warning on line 92 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L92

Added line #L92 was not covered by tests
)
);

setValue("formOtherLabels", [

Check warning on line 96 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L96

Added line #L96 was not covered by tests
...otherSelectedOtherLabels,
...definedSelectedOtherLabels,
]);

const definedSelectedSources: string[] = Array.from(

Check warning on line 101 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L101

Added line #L101 was not covered by tests
new Set(
selectedRuleset.rules
.map((rulesets) => rulesets?.metadata?.source || "")
.filter((source) => !!source)

Check warning on line 105 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L105

Added line #L105 was not covered by tests
)
);

setValue("formSources", [
...otherSelectedRuleSources,
...definedSelectedSources,
]);

const definedSelectedTargets: string[] =
selectedRuleset.kind === "category"
? [selectedRuleTarget]
: selectedRuleset.rules
.map((rulesets) => rulesets?.metadata?.target || "")
.filter((target) => !!target);
const definedSelectedTargets: string[] = Array.from(

Check warning on line 114 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L114

Added line #L114 was not covered by tests
new Set(
selectedRuleset.kind === "category"
? [selectedRuleTarget]
: selectedRuleset.rules

Check warning on line 118 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L117-L118

Added lines #L117 - L118 were not covered by tests
.map((rulesets) => rulesets?.metadata?.target || "")
.filter((target) => !!target)

Check warning on line 120 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L120

Added line #L120 was not covered by tests
)
);

setValue("formTargets", [
...otherSelectedRuleTargets,
...definedSelectedTargets,
]);

setValue("formRulesets", [...otherSelectedrulesets, selectedRuleset]);
setValue("formRulesets", [...otherSelectedRulesets, selectedRuleset]);

Check warning on line 129 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L129

Added line #L129 was not covered by tests
} else {
setValue("formSources", otherSelectedRuleSources);
setValue("formTargets", otherSelectedRuleTargets);
setValue("formRulesets", otherSelectedrulesets);
setValue("formRulesets", otherSelectedRulesets);
setValue("formOtherLabels", otherSelectedOtherLabels);

Check warning on line 134 in client/src/app/pages/applications/analysis-wizard/set-targets.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/analysis-wizard/set-targets.tsx#L133-L134

Added lines #L133 - L134 were not covered by tests
}
};
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export const CustomTargetForm: React.FC<CustomTargetFormProps> = ({

ruleFiles.forEach((file) => {
if (file.data && file?.fullFile?.type !== "placeholder") {
const { source, target, fileID, allLabels } = parseRules(file);
const { fileID, allLabels } = parseRules(file);
const newRule: Rule = {
name: file.fileName,
labels: allLabels,
Expand All @@ -268,6 +268,7 @@ export const CustomTargetForm: React.FC<CustomTargetFormProps> = ({
}
}
});

const matchingSourceCredential = identities.find(
(identity) => identity.name === formValues.associatedCredentials
);
Expand Down
1 change: 1 addition & 0 deletions client/src/app/queries/rulesets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const useFetchRulesets = () => {
const transformedMetadata: Metadata = {
source: labels.sourceLabel,
target: labels.targetLabel,
otherLabels: labels.otherLabels,
};
return {
...rule,
Expand Down

0 comments on commit 3f9ed1f

Please sign in to comment.