forked from tenable/cnappgoat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenario.go
258 lines (229 loc) · 5.73 KB
/
scenario.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package cnappgoat
import (
"errors"
"fmt"
"strings"
"gopkg.in/yaml.v3"
)
type Scenario struct {
Name string `yaml:"name"`
Runtime runtime `yaml:"runtime"`
Description string `yaml:"description,omitempty"`
ScenarioParams scenarioParams `yaml:"cnappgoat-params"`
State State `yaml:"-"`
Hash string `yaml:"-"`
SrcDir string `yaml:"-"`
}
type State struct {
State string `yaml:"state"`
Msg string `yaml:"msg"`
}
type runtime struct {
Name string
Options map[string]interface{}
}
type scenarioParams struct {
Module Module `yaml:"module"`
Platform Platform `yaml:"platform"`
ID string `yaml:"id"`
FriendlyName string `yaml:"friendlyName"`
Description string `yaml:"description"`
ScenarioType string `yaml:"scenarioType"`
Config map[string]string `yaml:"config"`
}
const (
Deployed = "deployed"
Destroyed = "destroyed"
Error = "error"
NotDeployed = "not-deployed"
)
type Module string
const (
CIEM Module = "CIEM"
CSPM Module = "CSPM"
CWPP Module = "CWPP"
DSPM Module = "DSPM"
IAC Module = "IAC"
KSPM Module = "KSPM"
)
type Platform string
const (
AWS Platform = "AWS"
Azure Platform = "AZURE"
GCP Platform = "GCP"
)
func (m Module) Equals(other Module) bool {
return strings.EqualFold(string(m), string(other))
}
func (m Module) MarshalYAML() (interface{}, error) {
return m.String(), nil
}
func (m Module) String() string {
switch m {
case CIEM:
return "CIEM"
case CSPM:
return "CSPM"
case CWPP:
return "CWPP"
case DSPM:
return "DSPM"
case IAC:
return "IAC"
case KSPM:
return "KSPM"
default:
return ""
}
}
func (m *Module) UnmarshalYAML(node *yaml.Node) error {
name := node.Value
module, err := ModuleFromString(name)
if err != nil {
return err
}
*m = module
return nil
}
func (p Platform) Equals(other Platform) bool {
return strings.EqualFold(string(p), string(other))
}
func (p Platform) String() string {
switch p {
case AWS:
return "AWS"
case Azure:
return "AZURE"
case GCP:
return "GCP"
default:
return ""
}
}
func (p Platform) MarshalYAML() (interface{}, error) {
return p.String(), nil
}
func (p *Platform) UnmarshalYAML(node *yaml.Node) error {
platform, err := PlatformFromString(node.Value)
if err != nil {
return err
}
*p = platform
return nil
}
func (r *runtime) UnmarshalYAML(unmarshal func(interface{}) error) error {
var runtimeStr string
err := unmarshal(&runtimeStr)
if err == nil {
r.Name = runtimeStr
return nil
}
var runtimeObj map[string]interface{}
if err = unmarshal(&runtimeObj); err != nil {
return err
}
if name, ok := runtimeObj["name"].(string); ok {
r.Name = name
}
if options, ok := runtimeObj["options"].(map[string]interface{}); ok {
r.Options = options
}
return nil
}
func (s State) String() string {
return s.State
}
func (s State) Equals(state State) bool {
return strings.EqualFold(s.State, state.State)
}
func ModuleFromString(name string) (Module, error) {
switch strings.ToLower(name) {
case strings.ToLower(CIEM.String()):
return CIEM, nil
case strings.ToLower(CSPM.String()):
return CSPM, nil
case strings.ToLower(CWPP.String()):
return CWPP, nil
case strings.ToLower(DSPM.String()):
return DSPM, nil
case strings.ToLower(IAC.String()):
return IAC, nil
case strings.ToLower(KSPM.String()):
return KSPM, nil
default:
return "", errors.New("unknown module name: " + name)
}
}
func PlatformFromString(name string) (Platform, error) {
switch strings.ToLower(name) {
case strings.ToLower(AWS.String()):
return AWS, nil
case strings.ToLower(Azure.String()):
return Azure, nil
case strings.ToLower(GCP.String()):
return GCP, nil
default:
return "", errors.New("unknown platform name: " + name)
}
}
func StateFromString(state string) (State, error) {
switch strings.ToLower(state) {
case strings.ToLower(Deployed):
return State{State: Deployed}, nil
case strings.ToLower(Destroyed):
return State{State: Destroyed}, nil
case strings.ToLower(Error):
return State{State: Error}, nil
case strings.ToLower(NotDeployed):
return State{State: NotDeployed}, nil
default:
return State{}, errors.New("unknown state: " + state)
}
}
func (p scenarioParams) IsValid() error {
if p.Module == "" && p.Platform == "" && p.ID == "" && p.FriendlyName == "" && p.Description == "" && p.ScenarioType == "" {
return errors.New("scenarioParams is empty, yaml is missing value cnappgoat-params")
}
if p.Module == "" {
return errors.New("module is required")
}
if p.Platform == "" {
return errors.New("platform is required")
}
if p.ID == "" {
return errors.New("id is required")
}
if len(strings.Split(p.ID, "-")) < 3 {
return fmt.Errorf("id must be in the format of <module>-<platform>-<id>, id is: %s", p.ID)
}
// module must be valid
if _, err := ModuleFromString(string(p.Module)); err != nil {
return err
}
// platform must be valid
if _, err := PlatformFromString(string(p.Platform)); err != nil {
return err
}
if !p.Module.Equals(Module(strings.Split(p.ID, "-")[0])) {
return fmt.Errorf("id must start with matching module name: %s, id is: %v", p.Module, p.ID)
}
if !p.Platform.Equals(Platform(strings.Split(p.ID, "-")[1])) {
return fmt.Errorf("id must include matching platform name, id is: %v", p.ID)
}
if p.FriendlyName == "" {
return errors.New("friendlyName is required")
}
if p.Description == "" {
return errors.New("description is required")
}
if p.ScenarioType == "" {
return errors.New("scenarioType is required")
}
if _, err := ModuleFromString(string(p.Module)); err != nil {
return err
}
if _, err := PlatformFromString(string(p.Platform)); err != nil {
return err
}
return nil
}