-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1628 from gchq/feature/BAI-1459-create-a-new-file…
…-scan-connector-for-modelscan Feature/bai 1459 create a new file scan connector for modelscan
- Loading branch information
Showing
17 changed files
with
376 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import fetch, { Response } from 'node-fetch' | ||
|
||
import config from '../utils/config.js' | ||
import { BadReq, InternalError } from '../utils/error.js' | ||
|
||
interface ModelScanInfoResponse { | ||
apiName: string | ||
apiVersion: string | ||
scannerName: string | ||
modelscanVersion: string | ||
} | ||
|
||
interface ModelScanResponse { | ||
summary: { | ||
total_issues: number | ||
total_issues_by_severity: { | ||
LOW: number | ||
MEDIUM: number | ||
HIGH: number | ||
CRITICAL: number | ||
} | ||
input_path: string | ||
absolute_path: string | ||
modelscan_version: string | ||
timestamp: string | ||
scanned: { | ||
total_scanned: number | ||
scanned_files: string[] | ||
} | ||
skipped: { | ||
total_skipped: number | ||
skipped_files: string[] | ||
} | ||
} | ||
issues: [ | ||
{ | ||
description: string | ||
operator: string | ||
module: string | ||
source: string | ||
scanner: string | ||
severity: string | ||
}, | ||
] | ||
// TODO: currently unknown what this might look like | ||
errors: object[] | ||
} | ||
|
||
export async function getModelScanInfo() { | ||
const url = `${config.avScanning.modelscan.protocol}://${config.avScanning.modelscan.host}:${config.avScanning.modelscan.port}` | ||
let res: Response | ||
|
||
try { | ||
res = await fetch(`${url}/info`, { | ||
method: 'GET', | ||
headers: { 'Content-Type': 'application/json' }, | ||
}) | ||
} catch (err) { | ||
throw InternalError('Unable to communicate with the ModelScan service.', { err }) | ||
} | ||
if (!res.ok) { | ||
throw BadReq('Unrecognised response returned by the ModelScan service.') | ||
} | ||
|
||
return (await res.json()) as ModelScanInfoResponse | ||
} | ||
|
||
export async function scanFile(file: Blob, file_name: string) { | ||
const url = `${config.avScanning.modelscan.protocol}://${config.avScanning.modelscan.host}:${config.avScanning.modelscan.port}` | ||
let res: Response | ||
|
||
try { | ||
const formData = new FormData() | ||
formData.append('in_file', file, file_name) | ||
|
||
res = await fetch(`${url}/scan/file`, { | ||
method: 'POST', | ||
headers: { | ||
accept: 'application/json', | ||
}, | ||
body: formData, | ||
}) | ||
} catch (err) { | ||
throw InternalError('Unable to communicate with the ModelScan service.', { err }) | ||
} | ||
if (!res.ok) { | ||
throw BadReq('Unrecognised response returned by the ModelScan service.', { | ||
body: JSON.stringify(await res.json()), | ||
}) | ||
} | ||
|
||
return (await res.json()) as ModelScanResponse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Response } from 'node-fetch' | ||
import { Readable } from 'stream' | ||
|
||
import { getModelScanInfo, scanFile } from '../../clients/modelScan.js' | ||
import { getObjectStream } from '../../clients/s3.js' | ||
import { FileInterfaceDoc, ScanState } from '../../models/File.js' | ||
import log from '../../services/log.js' | ||
import config from '../../utils/config.js' | ||
import { ConfigurationError } from '../../utils/error.js' | ||
import { BaseFileScanningConnector, FileScanResult } from './Base.js' | ||
|
||
export const modelScanToolName = 'ModelScan' | ||
|
||
export class ModelScanFileScanningConnector extends BaseFileScanningConnector { | ||
constructor() { | ||
super() | ||
} | ||
|
||
info() { | ||
return [modelScanToolName] | ||
} | ||
|
||
async ping() { | ||
try { | ||
// discard the results as we only want to know if the endpoint is reachable | ||
await getModelScanInfo() | ||
} catch (error) { | ||
throw ConfigurationError( | ||
'ModelScan does not look like it is running. Check that the service configuration is correct.', | ||
{ | ||
modelScanConfig: config.avScanning.modelscan, | ||
}, | ||
) | ||
} | ||
} | ||
|
||
async scan(file: FileInterfaceDoc): Promise<FileScanResult[]> { | ||
this.ping() | ||
|
||
const s3Stream = (await getObjectStream(file.bucket, file.path)).Body as Readable | ||
try { | ||
// TODO: see if it's possible to directly send the Readable stream rather than a blob | ||
const fileBlob = await new Response(s3Stream).blob() | ||
const scanResults = await scanFile(fileBlob, file.name) | ||
|
||
const issues = scanResults.summary.total_issues | ||
const isInfected = issues > 0 | ||
const viruses: string[] = [] | ||
if (isInfected) { | ||
for (const issue of scanResults.issues) { | ||
viruses.push(`${issue.severity}: ${issue.description}. ${issue.scanner}`) | ||
} | ||
} | ||
log.info( | ||
{ modelId: file.modelId, fileId: file._id, name: file.name, result: { isInfected, viruses } }, | ||
'Scan complete.', | ||
) | ||
return [ | ||
{ | ||
toolName: modelScanToolName, | ||
state: ScanState.Complete, | ||
isInfected, | ||
viruses, | ||
}, | ||
] | ||
} catch (error) { | ||
log.error({ error, modelId: file.modelId, fileId: file._id, name: file.name }, 'Scan errored.') | ||
return [ | ||
{ | ||
toolName: modelScanToolName, | ||
state: ScanState.Error, | ||
}, | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`clients > modelScan > getModelScanInfo > success 1`] = ` | ||
[ | ||
[ | ||
"undefined://undefined:undefined/info", | ||
{ | ||
"headers": { | ||
"Content-Type": "application/json", | ||
}, | ||
"method": "GET", | ||
}, | ||
], | ||
] | ||
`; | ||
|
||
exports[`clients > modelScan > scanFile > success 1`] = ` | ||
[ | ||
[ | ||
"undefined://undefined:undefined/scan/file", | ||
{ | ||
"body": FormData { | ||
Symbol(state): [ | ||
{ | ||
"name": "in_file", | ||
"value": File { | ||
Symbol(kHandle): Blob {}, | ||
Symbol(kLength): 0, | ||
Symbol(kType): "application/x-hdf5", | ||
}, | ||
}, | ||
], | ||
}, | ||
"headers": { | ||
"accept": "application/json", | ||
}, | ||
"method": "POST", | ||
}, | ||
], | ||
] | ||
`; |
Oops, something went wrong.