Skip to content

Commit

Permalink
Merge branch 'main' into eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
gammelalf committed Mar 27, 2024
2 parents e12ba47 + 7f62242 commit 394b225
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 89 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## v0.1.0

Initial release of kraken.
21 changes: 20 additions & 1 deletion kraken/src/api/handler/findings/handler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use actix_web::delete;
use actix_web::get;
use actix_web::post;
Expand Down Expand Up @@ -98,6 +100,22 @@ pub async fn get_all_findings(
return Err(ApiError::NotFound);
}

let mut affected_lookup = HashMap::new();
let affected = query!(
&mut tx,
(FindingAffected::F.uuid, FindingAffected::F.finding)
)
.condition(FindingAffected::F.workspace.equals(workspace_uuid))
.all()
.await?;

for (_, finding) in affected {
affected_lookup
.entry(*finding.key())
.and_modify(|x| *x += 1)
.or_insert(1);
}

let findings = query!(
&mut tx,
(
Expand All @@ -106,7 +124,7 @@ pub async fn get_all_findings(
Finding::F.definition.name,
Finding::F.definition.cve,
Finding::F.severity,
Finding::F.created_at
Finding::F.created_at,
)
)
.condition(Finding::F.workspace.equals(workspace_uuid))
Expand All @@ -119,6 +137,7 @@ pub async fn get_all_findings(
cve,
severity,
created_at,
affected_count: *affected_lookup.get(&uuid).unwrap_or(&0),
},
)
.try_collect()
Expand Down
3 changes: 3 additions & 0 deletions kraken/src/api/handler/findings/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub struct SimpleFinding {
/// The severity of the finding
pub severity: FindingSeverity,

/// The count of affected aggregations
pub affected_count: u64,

/// The point in time this finding definition was created
pub created_at: DateTime<Utc>,
}
Expand Down
61 changes: 41 additions & 20 deletions kraken/src/api/handler/findings/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use futures::TryStreamExt;
use std::collections::HashMap;

use rorm::and;
use rorm::conditions::Condition;
use rorm::db::Executor;
use rorm::db::transaction::Transaction;
use rorm::prelude::*;
use rorm::query;
use uuid::Uuid;
Expand Down Expand Up @@ -66,39 +67,59 @@ pub fn finding_affected_into_simple(affected: FindingAffected) -> ApiResult<Simp

impl ListFindings {
/// Query all findings affecting an object
pub async fn query_through_affected<'ex: 'co, 'co>(
executor: impl Executor<'ex>,
pub async fn query_through_affected<'exe: 'co, 'co>(
tx: &'exe mut Transaction,
workspace: Uuid,
condition: impl Condition<'co>,
) -> Result<ListFindings, rorm::Error> {
query!(
executor,
let mut affected_lookup = HashMap::new();

let affected = query!(&mut *tx, (FindingAffected::F.finding,))
.condition(FindingAffected::F.workspace.equals(workspace))
.all()
.await?;
for (finding,) in affected {
affected_lookup
.entry(*finding.key())
.and_modify(|x| *x += 1)
.or_insert(1);
}

let findings = query!(
&mut *tx,
(
FindingAffected::F.finding.uuid,
FindingAffected::F.finding.definition.uuid,
FindingAffected::F.finding.definition.name,
FindingAffected::F.finding.definition.cve,
FindingAffected::F.finding.severity,
FindingAffected::F.finding.created_at
FindingAffected::F.finding.created_at,
)
)
.condition(and![
condition,
FindingAffected::F.workspace.equals(workspace)
])
.stream()
.and_then(|(uuid, definition, name, cve, severity, created_at)| {
std::future::ready(Ok(SimpleFinding {
uuid,
definition,
name,
cve,
severity,
created_at,
}))
.all()
.await?;

let simple_findings = findings
.into_iter()
.map(
|(uuid, definition, name, cve, severity, created_at)| SimpleFinding {
uuid,
definition,
name,
cve,
severity,
created_at,
affected_count: affected_lookup.get(&uuid).copied().unwrap_or(0),
},
)
.collect();

Ok(ListFindings {
findings: simple_findings,
})
.try_collect()
.await
.map(|findings| ListFindings { findings })
}
}
8 changes: 7 additions & 1 deletion kraken_frontend/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -12732,7 +12732,7 @@
"SimpleFinding": {
"type": "object",
"description": "A simple finding",
"required": ["uuid", "definition", "name", "severity", "created_at"],
"required": ["uuid", "definition", "name", "severity", "affected_count", "created_at"],
"properties": {
"uuid": {
"type": "string",
Expand All @@ -12756,6 +12756,12 @@
"severity": {
"$ref": "#/components/schemas/FindingSeverity"
},
"affected_count": {
"type": "integer",
"format": "int64",
"description": "The count of affected",
"minimum": 0
},
"created_at": {
"type": "string",
"format": "date-time",
Expand Down
14 changes: 13 additions & 1 deletion kraken_frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@
"prettier": {
"useTabs": false,
"tabWidth": 4,
"printWidth": 120
"printWidth": 120,
"plugins": [
"prettier-plugin-organize-imports"
],
"overrides": [
{
"files": "*.json",
"options": {
"parser": "json-stringify"
}
}
]
},
"scripts": {
"dev": "vite",
Expand Down Expand Up @@ -41,6 +52,7 @@
"eslint": "^8.57.0",
"eslint-plugin-jsdoc": "^48.2.1",
"prettier": "3.2.5",
"prettier-plugin-organize-imports": "^3.2.4",
"typescript": "^5.4.2",
"typescript-eslint": "^7.4.0",
"vite": ">=4.5.2"
Expand Down
9 changes: 9 additions & 0 deletions kraken_frontend/src/api/generated/models/SimpleFinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export interface SimpleFinding {
* @memberof SimpleFinding
*/
severity: FindingSeverity;
/**
* The count of affected
* @type {number}
* @memberof SimpleFinding
*/
affectedCount: number;
/**
* The point in time this finding definition was created
* @type {Date}
Expand All @@ -73,6 +79,7 @@ export function instanceOfSimpleFinding(value: object): boolean {
isInstance = isInstance && "definition" in value;
isInstance = isInstance && "name" in value;
isInstance = isInstance && "severity" in value;
isInstance = isInstance && "affectedCount" in value;
isInstance = isInstance && "createdAt" in value;

return isInstance;
Expand All @@ -93,6 +100,7 @@ export function SimpleFindingFromJSONTyped(json: any, ignoreDiscriminator: boole
'name': json['name'],
'cve': !exists(json, 'cve') ? undefined : json['cve'],
'severity': FindingSeverityFromJSON(json['severity']),
'affectedCount': json['affected_count'],
'createdAt': (new Date(json['created_at'])),
};
}
Expand All @@ -111,6 +119,7 @@ export function SimpleFindingToJSON(value?: SimpleFinding | null): any {
'name': value.name,
'cve': value.cve,
'severity': FindingSeverityToJSON(value.severity),
'affected_count': value.affectedCount,
'created_at': (value.createdAt.toISOString()),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function MarkdownEditorPopup(props: MarkdownEditorPopupProps) {
<GithubMarkdown>{content}</GithubMarkdown>
<Editor
className={"knowledge-base-editor"}
theme={"custom"}
theme={"kraken"}
beforeMount={configureMonaco}
language={"markdown"}
value={content}
Expand Down
Loading

0 comments on commit 394b225

Please sign in to comment.