-
Notifications
You must be signed in to change notification settings - Fork 10
/
legacyiac.go
228 lines (209 loc) · 6.58 KB
/
legacyiac.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
// Copyright 2022 Snyk Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package policy
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/open-policy-agent/opa/rego"
"github.com/snyk/policy-engine/pkg/input"
"github.com/snyk/policy-engine/pkg/logging"
"github.com/snyk/policy-engine/pkg/models"
"github.com/snyk/policy-engine/pkg/policy/legacyiac"
)
// This file contains code for backwards compatibility with legacy Snyk IaC rules
type LegacyIaCPolicy struct {
*BasePolicy
}
func (p *LegacyIaCPolicy) inputType() *input.Type {
for _, ele := range strings.Split(p.pkg, ".") {
switch ele {
case "arm":
return input.Arm
case "cloudformation":
return input.CloudFormation
case "kubernetes":
return input.Kubernetes
case "terraform":
return input.Terraform
}
}
return p.BasePolicy.inputType
}
func (p *LegacyIaCPolicy) InputType() string {
return p.inputType().Name
}
func (p *LegacyIaCPolicy) InputTypeMatches(inputType string) bool {
return p.inputType().Matches(inputType)
}
func (p *LegacyIaCPolicy) Eval(
ctx context.Context,
options EvalOptions,
) ([]models.RuleResults, error) {
logger := options.Logger
if logger == nil {
logger = logging.DefaultLogger
}
logger = logger.WithField(logging.POLICY_TYPE, "legacy_iac")
inputs, err := legacyIaCInput(options.Input)
if err != nil {
logger.Error(ctx, "Failed to transform input")
err = fmt.Errorf("%w: %v", FailedToEvaluateRule, err)
return p.errorOutput(err)
}
defaultResourceNamespace := ""
if filepath, ok := options.Input.Meta["filepath"].(string); ok {
defaultResourceNamespace = filepath
} else {
logger.Warn(ctx, "No filepath found in meta, using empty namespace")
}
ruleResults := []models.RuleResults{}
for _, input := range inputs {
raw := input.Raw()
opts := append(
options.RegoOptions,
rego.Query(p.judgementRule.query()),
rego.Input(raw),
rego.StrictBuiltinErrors(false),
)
builtins := NewBuiltins(options.Input, options.ResourcesResolver)
opts = append(opts, builtins.Rego()...)
query, err := rego.New(opts...).PrepareForEval(ctx)
if err != nil {
logger.Error(ctx, "Failed to prepare for eval")
return p.errorOutput(err)
}
resultSet, err := query.Eval(ctx)
if err != nil {
logger.Error(ctx, "Failed to evaluate query")
return p.errorOutput(err)
}
lirs := legacyIaCResults{}
if err := unmarshalResultSet(resultSet, &lirs); err != nil {
logger.Error(ctx, "Failed to unmarshal result set")
return p.errorOutput(err)
}
ruleResults = append(
ruleResults,
lirs.toRuleResults(p.pkg, input, defaultResourceNamespace, options.Input.InputType)...,
)
}
return ruleResults, nil
}
func (p *LegacyIaCPolicy) errorOutput(err error) ([]models.RuleResults, error) {
return []models.RuleResults{
{
Package_: p.pkg,
Errors: []string{err.Error()},
},
}, err
}
type legacyIaCResults []*legacyIaCResult
func (r legacyIaCResults) toRuleResults(pkg string, input legacyiac.Input, defaultResourceNamespace string, inputType string) []models.RuleResults {
resultsByRuleID := map[string]models.RuleResults{}
for _, ir := range r {
id := ir.PublicID
ruleResults, ok := resultsByRuleID[id]
if !ok {
refs := make([]models.RuleResultsReference, len(ir.References))
for i, r := range ir.References {
refs[i] = models.RuleResultsReference{Url: r}
}
ruleResults = models.RuleResults{
Id: id,
Title: ir.Title,
Description: ir.Impact,
References: refs,
Package_: pkg,
}
}
ruleResults.Results = append(ruleResults.Results, *ir.toRuleResult(input, defaultResourceNamespace, inputType))
resultsByRuleID[id] = ruleResults
}
output := make([]models.RuleResults, 0, len(resultsByRuleID))
for _, r := range resultsByRuleID {
output = append(output, r)
}
return output
}
type legacyIaCRemediation struct {
Default string
ByInputType map[string]string
}
func (r *legacyIaCRemediation) UnmarshalJSON(data []byte) error {
var remediationString string
if err := json.Unmarshal(data, &remediationString); err == nil {
r.Default = remediationString
return nil
}
var remediationMap map[string]string
err := json.Unmarshal(data, &remediationMap)
if err != nil {
return err
}
r.ByInputType = remediationMap
return nil
}
type legacyIaCResult struct {
PublicID string `json:"publicId"`
Title string `json:"title"`
Severity string `json:"severity"`
Msg string `json:"msg"`
Issue string `json:"issue"`
Impact string `json:"impact"`
Remediation legacyIaCRemediation `json:"remediation"`
References []string `json:"references"`
}
func (r *legacyIaCResult) toRuleResult(input legacyiac.Input, defaultResourceNamespace string, inputType string) *models.RuleResult {
parsedMsg := input.ParseMsg(r.Msg)
builder := newRuleResultBuilder()
key := ResourceKey{
ID: parsedMsg.ResourceID,
Type: parsedMsg.ResourceType,
Namespace: defaultResourceNamespace,
}
if parsedMsg.ResourceNamespace != "" {
key.Namespace = parsedMsg.ResourceNamespace
}
builder.setPrimaryResource(key)
if parsedMsg.ResourceID != "" {
if len(parsedMsg.Path) > 0 {
builder.addResourceAttribute(key, parsedMsg.Path)
}
}
result := builder.toRuleResult()
result.Passed = false
result.Severity = r.Severity
if r.Remediation.Default != "" {
result.Remediation = r.Remediation.Default
} else if key, ok := remediationKeys[inputType]; ok {
result.Remediation = r.Remediation.ByInputType[key]
}
return &result
}
func legacyIaCInput(state *models.State) ([]legacyiac.Input, error) {
switch {
case input.Terraform.Matches(state.InputType):
return []legacyiac.Input{legacyiac.NewTfInput(state)}, nil
case input.CloudFormation.Matches(state.InputType):
return []legacyiac.Input{legacyiac.NewCfnInput(state)}, nil
case input.Arm.Matches(state.InputType):
return []legacyiac.Input{legacyiac.NewArmInput(state)}, nil
case input.Kubernetes.Matches(state.InputType):
return legacyiac.NewK8sInputs(state), nil
default:
return nil, fmt.Errorf("unsupported input type for this type of rule")
}
}