-
Notifications
You must be signed in to change notification settings - Fork 21
/
parser_constraints.go
88 lines (73 loc) · 2.25 KB
/
parser_constraints.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
package spec
import (
"fmt"
version "github.com/hashicorp/go-version"
)
type hcltmConstraint interface {
verConstraint() string
msg() string
asOf() string
tmCheck(tm *Threatmodel) bool
}
type controlStringToBlock struct{}
func (c *controlStringToBlock) asOf() string {
return "0.1.5"
}
func (c *controlStringToBlock) verConstraint() string {
return ">= 0.0.1"
}
func (c *controlStringToBlock) msg() string {
return "Deprecation warning: This threat model has defined `control` strings inside of `threat` blocks. As of v0.1.5 It's recommended that you update these to `expanded_control` blocks, as they may be cause errors in future versions of hcltm."
}
func (c *controlStringToBlock) tmCheck(tm *Threatmodel) bool {
for _, t := range tm.Threats {
if t.Control != "" {
return true
}
}
return false
}
type proposedControlToBlock struct{}
func (c *proposedControlToBlock) asOf() string {
return "0.1.5"
}
func (c *proposedControlToBlock) verConstraint() string {
return ">= 0.0.1"
}
func (c *proposedControlToBlock) msg() string {
return "Deprecation warning: This threat model has defined `proposed_control` block(s) inside of `threat` blocks. As of v0.1.5 It's recommended that you update these to `expanded_control` blocks, as they may be cause errors in future versions of hcltm."
}
func (c *proposedControlToBlock) tmCheck(tm *Threatmodel) bool {
for _, t := range tm.Threats {
if len(t.ProposedControls) > 0 {
return true
}
}
return false
}
func VersionConstraints(tmw *ThreatmodelWrapped, emit bool) (string, error) {
hcltmConstraints := make(map[string]hcltmConstraint)
hcltmConstraints["control_string_to_block"] = &controlStringToBlock{}
hcltmConstraints["proposed_control_to_block"] = &proposedControlToBlock{}
for _, cval := range hcltmConstraints {
newConst, err := version.NewConstraint(cval.verConstraint())
if err != nil {
return "", err
}
currVer, err := version.NewVersion(tmw.SpecVersion)
if err != nil {
return "", err
}
if newConst.Check(currVer) {
for _, tm := range tmw.Threatmodels {
if cval.tmCheck(&tm) {
if emit {
fmt.Printf("[threatmodel: %s] %s\n", tm.Name, cval.msg())
}
return fmt.Sprintf("[threatmodel: %s] %s", tm.Name, cval.msg()), nil
}
}
}
}
return "", nil
}