-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ui button to allow manual file re scan and included migration s…
…cript
- Loading branch information
Showing
35 changed files
with
685 additions
and
55 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,81 @@ | ||
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 { modelscanVersion } = await getModelScanInfo() | ||
|
||
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, | ||
scannerVersion: modelscanVersion, | ||
isInfected, | ||
viruses, | ||
lastRunAt: new Date(), | ||
}, | ||
] | ||
} catch (error) { | ||
log.error({ error, modelId: file.modelId, fileId: file._id, name: file.name }, 'Scan errored.') | ||
return [ | ||
{ | ||
toolName: modelScanToolName, | ||
state: ScanState.Error, | ||
lastRunAt: new Date(), | ||
}, | ||
] | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
backend/src/migrations/011_find_and_remove_invalid_users.ts
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,44 @@ | ||
import authentication from '../connectors/authentication/index.js' | ||
import { MigrationMetadata } from '../models/Migration.js' | ||
import ModelModel from '../models/Model.js' | ||
|
||
/** | ||
* As we now do backend validation for users being added to model access lists, we | ||
* added this script to find and remove all existing users that do not pass the | ||
* "getUserInformation" call in the authentication connector. You can find a | ||
* list of removed users for all affected models by looking at the "metadata" | ||
* property of this migration's database object. | ||
**/ | ||
|
||
export async function up() { | ||
const models = await ModelModel.find({}) | ||
const metadata: MigrationMetadata[] = [] | ||
for (const model of models) { | ||
const invalidUsers: string[] = [] | ||
await Promise.all( | ||
model.collaborators.map(async (collaborator) => { | ||
if (collaborator.entity !== '') { | ||
try { | ||
await authentication.getUserInformation(collaborator.entity) | ||
} catch (err) { | ||
invalidUsers.push(collaborator.entity) | ||
} | ||
} | ||
}), | ||
) | ||
if (invalidUsers.length > 0) { | ||
const invalidUsersForModel = { modelId: model.id, invalidUsers: invalidUsers } | ||
const invalidUsersRemoved = model.collaborators.filter( | ||
(collaborator) => !invalidUsers.includes(collaborator.entity), | ||
) | ||
model.collaborators = invalidUsersRemoved | ||
await model.save() | ||
metadata.push(invalidUsersForModel) | ||
} | ||
} | ||
return metadata | ||
} | ||
|
||
export async function down() { | ||
/* NOOP */ | ||
} |
17 changes: 17 additions & 0 deletions
17
backend/src/migrations/012_add_avscan_lastRanAt_property.ts
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,17 @@ | ||
import FileModel from '../models/File.js' | ||
|
||
export async function up() { | ||
const files = await FileModel.find({}) | ||
for (const file of files) { | ||
for (const avResult of file.avScan) { | ||
if (avResult.lastRunAt === undefined) { | ||
avResult.lastRunAt = file.createdAt | ||
} | ||
} | ||
await file.save() | ||
} | ||
} | ||
|
||
export async function down() { | ||
/* NOOP */ | ||
} |
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
Oops, something went wrong.