Skip to content

Commit

Permalink
feat(label-config): added ability to specify label config (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
maira-samtek authored Jul 15, 2024
1 parent 2c907eb commit da07afd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libs/security-hub-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface SecurityHubFinding {
remediation?: Remediation;
ProductName?: string;
Resources?: Resource[];
[key: string]: string | unknown;
}

export class SecurityHub {
Expand Down
52 changes: 52 additions & 0 deletions src/macpro-security-hub-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ interface UpdateForReturn {
summary: string;
}

interface LabelConfig {
labelField: string;
labelPrefix?: string;
labelDelimiter?: string;
}

export class SecurityHubJiraSync {
private readonly jira: Jira;
private readonly securityHub: SecurityHub;
Expand Down Expand Up @@ -296,7 +302,41 @@ export class SecurityHubJiraSync {
throw new Error(`Invalid severity: ${severity}`);
}
};
createLabels(
finding: SecurityHubFinding,
identifyingLabels: string[],
config: LabelConfig[]
): string[] {
const labels: string[] = [];
const fields = ["accountId", "region", "identify"];
const values = [...identifyingLabels, "security-hub"];

config.forEach(
({ labelField: field, labelDelimiter: delim, labelPrefix: prefix }) => {
const delimiter = delim ?? "";
const labelPrefix = prefix ?? "";

if (fields.includes(field)) {
const index = fields.indexOf(field);
if (index >= 0) {
labels.push(
`${labelPrefix}${delimiter}${values[index]
?.trim()
.replace(/ /g, "")}`
);
}
} else {
const value = (finding[field] ?? "")
.toString()
.trim()
.replace(/ /g, "");
labels.push(`${labelPrefix}${delimiter}${value}`);
}
}
);

return labels;
}
async createJiraIssueFromFinding(
finding: SecurityHubFinding,
identifyingLabels: string[]
Expand All @@ -323,6 +363,18 @@ export class SecurityHubJiraSync {
...this.customJiraFields,
},
};
if (process.env.LABELS_CONFIG) {
try {
const config = JSON.parse(process.env.LABELS_CONFIG);
newIssueData.fields.labels = this.createLabels(
finding,
identifyingLabels,
config
);
} catch (e) {
console.log("Invalid labels config - going with default labels");
}
}
if (finding.severity && process.env.JIRA_HOST?.includes("jiraent")) {
newIssueData.fields.priority = {
name: this.getPriorityNumber(finding.severity, true),
Expand Down

0 comments on commit da07afd

Please sign in to comment.