Skip to content

Commit

Permalink
🐛 Pass all associated rule labels to analyzer (#1085)
Browse files Browse the repository at this point in the history
- Passes a list of all labels that are associated with rule files along
to the analyzer.

Signed-off-by: ibolton336 <[email protected]>
  • Loading branch information
ibolton336 committed Jul 5, 2023
1 parent 452cd57 commit 904efd3
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,
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();
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);
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)
)
) {
const newOtherLabels = otherLabels.filter(
(otherLabel) => !formOtherLabels.includes(otherLabel)
);

setValue("formOtherLabels", [
...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");

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(
(formRuleset) => selectedRuleset.id !== formRuleset.id
);

const otherSelectedOtherLabels = formOtherLabels.filter(
(label) =>
!selectedRuleset.rules
.flatMap((rule) => rule?.metadata?.otherLabels)
.includes(label)
);

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

setValue("formOtherLabels", [
...otherSelectedOtherLabels,
...definedSelectedOtherLabels,
]);

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

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(
new Set(
selectedRuleset.kind === "category"
? [selectedRuleTarget]
: selectedRuleset.rules
.map((rulesets) => rulesets?.metadata?.target || "")
.filter((target) => !!target)
)
);

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

setValue("formRulesets", [...otherSelectedrulesets, selectedRuleset]);
setValue("formRulesets", [...otherSelectedRulesets, selectedRuleset]);
} else {
setValue("formSources", otherSelectedRuleSources);
setValue("formTargets", otherSelectedRuleTargets);
setValue("formRulesets", otherSelectedrulesets);
setValue("formRulesets", otherSelectedRulesets);
setValue("formOtherLabels", otherSelectedOtherLabels);
}
};
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 904efd3

Please sign in to comment.