Skip to content

Commit 5fda905

Browse files
committed
Add task to prepopulate/pre-cache the NVD database
This makes it easier to use in a CI pipeline, especially if multiple jobs use the NVD database. Takes care of #162
1 parent e4d0ad5 commit 5fda905

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

src/nvd/task.clj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
(ns nvd.task
2424
"Clojure CLI tool entry points: `check`."
2525
(:require
26-
[nvd.task.check :refer [-main]]))
26+
[nvd.task.check :as check]
27+
[nvd.task.prepopulate-db :as prepopulate-db]))
2728

2829
(defn check
2930
"Arguments: `:config-filename` (optional), `:classpath` (required)."
3031
[{:keys [config-filename classpath]}]
31-
(-main (or config-filename "") classpath))
32+
(check/-main (or config-filename "") classpath))
33+
34+
(defn prepopulate-db
35+
"Arguments: `:config-filename` (optional)."
36+
[{:keys [config-filename]}]
37+
(prepopulate-db/-main (or config-filename "")))

src/nvd/task/prepopulate_db.clj

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
;; The MIT License (MIT)
2+
;;
3+
;; Copyright (c) 2026- bevuta IT GmbH
4+
;;
5+
;; Permission is hereby granted, free of charge, to any person obtaining a copy
6+
;; of this software and associated documentation files (the "Software"), to deal
7+
;; in the Software without restriction, including without limitation the rights
8+
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
;; copies of the Software, and to permit persons to whom the Software is
10+
;; furnished to do so, subject to the following conditions:
11+
;;
12+
;; The above copyright notice and this permission notice shall be included in all
13+
;; copies or substantial portions of the Software.
14+
;;
15+
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
;; SOFTWARE.
22+
23+
(ns nvd.task.prepopulate-db
24+
(:require
25+
[clansi :refer [style]]
26+
[clojure.string :as s]
27+
[nvd.config :refer [default-edn-config-filename with-config]]
28+
[trptcolin.versioneer.core :refer [get-version]])
29+
(:import
30+
(org.owasp.dependencycheck Engine)
31+
(org.owasp.dependencycheck.exception ExceptionCollection)))
32+
33+
(def version
34+
(delay {:nvd-clojure (get-version "nvd-clojure" "nvd-clojure")
35+
:dependency-check (.getImplementationVersion (.getPackage Engine))}))
36+
37+
(defn do-updates [project]
38+
(let [^Engine engine (:engine project)]
39+
(try
40+
(.doUpdates engine)
41+
(catch ExceptionCollection e
42+
(println "Encountered errors while analyzing:" (.getMessage e))
43+
(doseq [exc (.getExceptions e)]
44+
(println exc))
45+
(let [exception-info (ex-info (str `ExceptionCollection)
46+
{:exceptions (.getExceptions e)})]
47+
(throw exception-info))))
48+
project))
49+
50+
(defn impl [config-filename]
51+
(with-config [project config-filename]
52+
(println "Prepulating database for" (-> project
53+
:title
54+
(s/trim)
55+
(str "...")
56+
(style :bright :yellow)))
57+
(println " using nvd-clojure:" (:nvd-clojure @version) "and dependency-check:" (:dependency-check @version))
58+
(-> project
59+
do-updates)
60+
;; If we got here, it's all good. Otherwise, we'd be throwing an exception
61+
(System/exit 0)))
62+
63+
(defn -main [& [config-filename]]
64+
;; specifically handle blank strings (in addition to nil)
65+
;; so that CLI callers can skip the first argument by simply passing an empty string:
66+
(let [config-filename (if (s/blank? config-filename)
67+
default-edn-config-filename
68+
config-filename)]
69+
(impl config-filename)))
70+

0 commit comments

Comments
 (0)