-
Notifications
You must be signed in to change notification settings - Fork 33
/
generate.clj
125 lines (101 loc) · 3.82 KB
/
generate.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(ns ctim.generate
(:gen-class)
(:require
[cheshire.core :as json]
[clojure.spec.alpha :as cs]
[clojure.spec.gen.alpha :as csg]
[clojure.string :as str]
[ctim.schemas
[actor :refer [Actor NewActor]]
[asset :refer [Asset NewAsset]]
[asset-mapping :refer [AssetMapping NewAssetMapping]]
[asset-properties :refer [AssetProperties NewAssetProperties]]
[attack-pattern :refer [AttackPattern NewAttackPattern]]
[campaign :refer [Campaign NewCampaign]]
[coa :refer [COA NewCOA]]
[feedback :refer [Feedback NewFeedback]]
[identity-assertion :refer [IdentityAssertion NewIdentityAssertion]]
[incident :refer [Incident NewIncident]]
[indicator :refer [Indicator NewIndicator]]
[judgement :refer [Judgement NewJudgement]]
[malware :refer [Malware NewMalware]]
[note :refer [Note NewNote]]
[relationship :refer [Relationship NewRelationship]]
[sighting :refer [Sighting NewSighting]]
[target-record :refer [TargetRecord NewTargetRecord]]
[tool :refer [Tool NewTool]]
[vulnerability :refer [Vulnerability NewVulnerability]]
[weakness :refer [Weakness NewWeakness]]]
[flanders.spec :as fs]
[flanders.utils :as fu])
(:import java.util.UUID))
(defn ->gen [ddl]
(cs/gen (fs/->spec (fu/require-all ddl)
(str (UUID/randomUUID)))))
(def generators
{:actor (->gen Actor)
:new-actor (->gen NewActor)
:asset (->gen Asset)
:new-asset (->gen NewAsset)
:asset-mapping (->gen AssetMapping)
:new-asset-mapping (->gen NewAssetMapping)
:asset-properties (->gen AssetProperties)
:new-asset-properties (->gen NewAssetProperties)
:attack-pattern (->gen AttackPattern)
:new-attack-pattern (->gen NewAttackPattern)
:campaign (->gen Campaign)
:new-campaign (->gen NewCampaign)
:coa (->gen COA)
:new-coa (->gen NewCOA)
:feedback (->gen Feedback)
:new-feedback (->gen NewFeedback)
:incident (->gen Incident)
:new-incident (->gen NewIncident)
:indicator (->gen Indicator)
:new-indicator (->gen NewIndicator)
:judgement (->gen Judgement)
:new-judgement (->gen NewJudgement)
:malware (->gen Malware)
:new-malware (->gen NewMalware)
:relationship (->gen Relationship)
:new-relationship (->gen NewRelationship)
:sighting (->gen Sighting)
:new-sighting (->gen NewSighting)
:identity-assertion (->gen IdentityAssertion)
:new-identity-assertion (->gen NewIdentityAssertion)
:target-record (->gen TargetRecord)
:new-target-record (->gen NewTargetRecord)
:tool (->gen Tool)
:new-tool (->gen NewTool)
:weakness (->gen Weakness)
:new-weakness (->gen NewWeakness)
:vulnerability (->gen Vulnerability)
:new-vulnerability (->gen NewVulnerability)
:note (->gen Note)
:new-note (->gen NewNote)})
(defn- sample [gen-kw sample-num take-num]
(take-last take-num
(csg/sample (get generators gen-kw)
sample-num)))
(defn- usage []
(str "Usage: lein gen <entity>\n"
" lein gen <entity> <amount> <complexity>\n"
"\n"
"Entity must be one of " (->> (keys generators)
(map name)
sort
(str/join ", "))
".\n"
"Amount and Complexity may be positive integers.\n"))
(defn -main [& [entity amount complexity]]
(if (nil? entity)
(println (usage))
(let [amount (when amount (bigint amount))
complexity (when complexity (bigint complexity))
sample-num (apply max (filter integer? [10 amount complexity]))
take-num (apply max (filter integer? [1 amount]))]
(doseq [example (sample (keyword entity) sample-num take-num)]
(println (str "**** Example " entity " *****"))
(-> example
(json/generate-string {:pretty true})
println)))))