From a2d8a3c1c9d95c244472ee9f2cb35f0cc136d572 Mon Sep 17 00:00:00 2001 From: Ryan Jones Date: Wed, 9 Feb 2022 15:42:53 -0600 Subject: [PATCH] ObjectCache --- service/src/DockerScanService.js | 7 ++++++- service/src/ObjectCache.js | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 service/src/ObjectCache.js diff --git a/service/src/DockerScanService.js b/service/src/DockerScanService.js index 71316e8..6be931e 100644 --- a/service/src/DockerScanService.js +++ b/service/src/DockerScanService.js @@ -1,6 +1,7 @@ const { spawn, execSync } = require('child_process'); const fs = require('fs'); const path = require('path'); +const ObjectCache = require('./ObjectCache'); const DATA_PATH = process.env.DATA_PATH || '/docker-swarm-snyk/data'; const SEVERITY = process.env.SEVERITY || 'high'; @@ -32,9 +33,13 @@ class DockerScanService { } static getSnykUri = (image) => { + const cache = ObjectCache.readCache(image).snyk; + if (cache) return cache; try { const cmd = `jq -r .uri ${DockerScanService.getFilename(image)}`; - return execSync(cmd).toString().trim(); + const snyk = execSync(cmd).toString().trim(); + if (snyk) ObjectCache.setCache(image, {snyk}); + return snyk; } catch (error) { console.error(error); } diff --git a/service/src/ObjectCache.js b/service/src/ObjectCache.js new file mode 100644 index 0000000..df2b347 --- /dev/null +++ b/service/src/ObjectCache.js @@ -0,0 +1,21 @@ +class ObjectCache { + static cache = {}; + static setCache = (key, obj, overwrite=false) => { + if (overwrite) { + ObjectCache[key] = obj; + return; + } + if (!Object.prototype.hasOwnProperty.call(ObjectCache.cache, key)) { + ObjectCache.cache[key] = {}; + } + Object.assign(ObjectCache.cache[key], obj); + } + static readCache = (key) => { + if (Object.prototype.hasOwnProperty.call(ObjectCache.cache, key)) { + return ObjectCache.cache[key]; + } + return {}; + } +} + +module.exports = ObjectCache; \ No newline at end of file