-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.go
138 lines (112 loc) · 3.5 KB
/
main.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
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"strings"
"github.com/ghodss/yaml"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
)
func main() {
ctx := context.Background()
preparedRegoQuery, err := createRegoPreparedQueryFromTemplateFile(ctx, "opa-template.yaml")
if err != nil {
log.Panic(err)
}
opaConstraintsParams, err := getConstraintParamsFromConstraintFile("opa-policy.yaml")
if err != nil {
log.Panic(err)
}
resource, err := getResource("deployment.yaml")
if err != nil {
log.Panic(err)
}
err = evaluateRego(ctx, preparedRegoQuery, opaConstraintsParams, resource)
if err != nil {
log.Panic(err)
}
}
func createRegoPreparedQueryFromTemplateFile(ctx context.Context, filename string) (*RegoStruct, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed reading data from file: %s", err)
}
resourceObject := map[string]interface{}{}
if err := yaml.Unmarshal(data, &resourceObject); err != nil {
return nil, fmt.Errorf("failed reading data from file: %s", err)
}
regoQuery := resourceObject["spec"].(map[string]interface{})["targets"].([]interface{})[0].(map[string]interface{})["rego"].(string)
moduleName := resourceObject["metadata"].(map[string]interface{})["name"].(string)
compiler, err := ast.CompileModules(map[string]string{moduleName + ".rego": regoQuery})
if err != nil {
return nil, err
}
r := rego.New(
rego.Query("data[_].violation"),
rego.Compiler(compiler),
)
query, err := r.PrepareForEval(ctx)
if err != nil {
return nil, err
}
res := RegoStruct{
Query: query,
Name: moduleName,
}
return &res, nil
}
func getConstraintParamsFromConstraintFile(filename string) (interface{}, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed reading data from file: %s", err)
}
resourceObject := K8sConstraintSpecObject{}
if err := yaml.Unmarshal(data, &resourceObject); err != nil {
return nil, fmt.Errorf("failed reading data from file: %s", err)
}
return resourceObject.Spec.Parameters, nil
}
func getResource(filename string) (map[string]interface{}, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed reading data from file: %s", err)
}
resourceObject := map[string]interface{}{}
if err := yaml.Unmarshal(data, &resourceObject); err != nil {
return nil, fmt.Errorf("failed reading data from file: %s", err)
}
return resourceObject, nil
}
func evaluateRego(ctx context.Context, preparedRego *RegoStruct, params interface{}, resource map[string]interface{}) error {
resourceAPIGroup, resourceVersion := prepareK8sKind(resource["apiVersion"].(string))
input := map[string]interface{}{
"review": map[string]interface{}{
"object": resource,
"kind": map[string]interface{}{
"kind": resource["kind"],
"group": resourceAPIGroup,
"version": resourceVersion,
},
},
"parameters": params,
}
rs, err := preparedRego.Query.Eval(ctx, rego.EvalInput(input))
if err != nil {
return err
}
for _, violations := range rs[0].Expressions[0].Value.([]interface{}) {
fmt.Println("Violation:", violations.(map[string]interface{})["msg"].(string))
}
return nil
}
func prepareK8sKind(resourceAPI string) (string, string) {
resourceAPIGroup := ""
resourceVersion := resourceAPI
if strings.Contains(resourceAPI, "/") {
resourceAPIGroup = strings.Split(resourceAPI, "/")[0]
resourceVersion = strings.Split(resourceAPI, "/")[1]
}
return resourceAPIGroup, resourceVersion
}