Skip to content

Commit

Permalink
fix: correct status log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nielm committed Dec 4, 2024
1 parent 2e647b2 commit 02da621
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cloudrun-malware-scanner/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ Log INFO main "Starting clamav services"
CLAMAV_NO_CLAMD=false
CLAMAV_NO_FRESHCLAMD=false
CLAMAV_NO_MILTERD=true
FRESHCLAM_CHECKS=48
FRESHCLAM_CHECKS=48 # 48/day = every half hour.
export CLAMAV_NO_CLAMD CLAMAV_NO_FRESHCLAMD CLAMAV_NO_MILTERD FRESHCLAM_CHECKS
bash -x /init &
/init &

sleep 30

Expand Down
2 changes: 1 addition & 1 deletion cloudrun-malware-scanner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prettier": "prettier --config .prettierrc.js --write ..",
"prettier-check": "prettier --config .prettierrc.js --check --log-level=warn ..",
"start-proxy": "node gcs-proxy-server.js",
"test": "jasmine",
"test": "env NODE_ENV=test jasmine",
"eslint": "eslint *.js",
"eslint-fix": "eslint --fix *.js",
"prepare": "{ git rev-parse --is-inside-work-tree >/dev/null 2>/dev/null && test \"$NODE_ENV\" != production -a \"$CI\" != true && cd .. && husky cloudrun-malware-scanner/.husky ; } || echo 'skipping husky setup'",
Expand Down
27 changes: 12 additions & 15 deletions cloudrun-malware-scanner/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {logger} = require('./logger.js');
/** @typedef {import('node:stream').Readable} Readable */
/** @typedef {typeof import('./metrics.js')} MetricsClient */
/** @typedef {import('@google-cloud/storage').Storage} Storage */
/** @typedef {import('@google-cloud/storage').File} File */

// @ts-ignore -- TS7016: Could not find a declaration file
/** @typedef {typeof import('clamdjs')} ClamdClient */
Expand Down Expand Up @@ -207,7 +208,7 @@ class Scanner {
if (!(await gcsFile.exists())[0]) {
// Warn in logs, but return successful to client.
logger.warn(
`Ignoring no longer existing file: gs://${gcsFile.bucket}/${gcsFile.name}`,
`Ignoring no longer existing file: ${gcsFile.cloudStorageURI.href}`,
);
return {status: 'ignored', message: 'file deleted'};
}
Expand All @@ -219,7 +220,7 @@ class Scanner {
const metadataSize = parseInt(String(metadata.size));
if (fileSize !== metadataSize) {
logger.info(
`Scan status for gs://${gcsFile.bucket}/${gcsFile.name}: IGNORED (File size mismatch (reported: ${fileSize}, metadata: ${metadataSize}). File upload may not be complete).`,
`Scan status for ${gcsFile.cloudStorageURI.href}: IGNORED (File size mismatch (reported: ${fileSize}, metadata: ${metadataSize}). File upload may not be complete).`,
);
this.metricsClient.writeScanIgnored(
bucketDefs.unscanned,
Expand All @@ -232,7 +233,7 @@ class Scanner {

const clamdVersion = await this.getClamVersion();
logger.info(
`Scan request for gs://${gcsFile.bucket}/${gcsFile.name}, (${fileSize} bytes) scanning with clam ${clamdVersion}`,
`Scan request for ${gcsFile.cloudStorageURI.href}, (${fileSize} bytes) scanning with clam ${clamdVersion}`,
);
const startTime = Date.now();
const readStream = await gcsFile.createReadStream();
Expand All @@ -248,7 +249,7 @@ class Scanner {

if (this.clamdClient.isCleanReply(result)) {
logger.info(
`Scan status for gs://${gcsFile.bucket}/${gcsFile.name}: CLEAN (${fileSize} bytes in ${scanDuration} ms)`,
`Scan status for ${gcsFile.cloudStorageURI.href}: CLEAN (${fileSize} bytes in ${scanDuration} ms)`,
);
this.metricsClient.writeScanClean(
bucketDefs.unscanned,
Expand All @@ -260,7 +261,7 @@ class Scanner {

// Move document to the bucket that holds clean documents. This can
// fail due to permissions or if the file has been deleted.
await this.moveProcessedFile(gcsFile.name, true, bucketDefs);
await this.moveProcessedFile(gcsFile, bucketDefs.clean);

// Respond to API client.
return {
Expand All @@ -270,7 +271,7 @@ class Scanner {
};
} else {
logger.warn(
`Scan status for gs://${gcsFile.bucket}/${gcsFile.name}: INFECTED ${result} (${fileSize} bytes in ${scanDuration} ms)`,
`Scan status for ${gcsFile.cloudStorageURI.href}: INFECTED ${result} (${fileSize} bytes in ${scanDuration} ms)`,
);
this.metricsClient.writeScanInfected(
bucketDefs.unscanned,
Expand All @@ -282,7 +283,7 @@ class Scanner {

// Move document to the bucket that holds infected documents. This can
// fail due to permissions or if the file has been deleted.
await this.moveProcessedFile(gcsFile.name, false, bucketDefs);
await this.moveProcessedFile(gcsFile, bucketDefs.quarantined);

// Respond to API client.
return {
Expand Down Expand Up @@ -315,19 +316,15 @@ class Scanner {
/**
* Move the file to the appropriate bucket.
* @async
* @param {string} filename
* @param {boolean} isClean
* @param {!import('./config.js').BucketDefs} config
* @param {File} srcfile
* @param {string} destinationBucketName
*/
async moveProcessedFile(filename, isClean, config) {
const srcBucketName = config.unscanned;
const srcfile = this.storageClient.bucket(srcBucketName).file(filename);
const destinationBucketName = isClean ? config.clean : config.quarantined;
async moveProcessedFile(srcfile, destinationBucketName) {
const destinationBucket = this.storageClient.bucket(destinationBucketName);

await srcfile.move(destinationBucket);
logger.info(
`Successfully moved file gs://${srcBucketName}/${filename} to gs://${destinationBucketName}/${filename}`,
`Successfully moved file ${srcfile.cloudStorageURI.href} to gs://${destinationBucketName}/${srcfile.name}`,
);
}
}
Expand Down
7 changes: 6 additions & 1 deletion cloudrun-malware-scanner/spec/scanner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ describe('Scanner', () => {
mockFile = jasmine.createSpyObj(
'testFile',
['exists', 'getMetadata', 'createReadStream', 'move'],
{name: TEST_FILE_NAME},
{
name: TEST_FILE_NAME,
cloudStorageURI: new URL(
`gs://${CONFIG.buckets[0].clean}/${TEST_FILE_NAME}`,
),
},
);
mockUnscannedBucket.file.withArgs(TEST_FILE_NAME).and.returnValue(mockFile);

Expand Down

0 comments on commit 02da621

Please sign in to comment.