-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.go
231 lines (204 loc) · 6.85 KB
/
evaluator.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
package internal
import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"os"
"github.com/mjec/docker-socket-authorizer/config"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
"github.com/open-policy-agent/opa/topdown"
"github.com/open-policy-agent/opa/types"
"golang.org/x/exp/slog"
)
type RegoEvaluator struct {
authorizer *rego.PreparedEvalQuery
store *storage.Store
policyList []string
}
func NewEvaluator(policyLoader func(*rego.Rego)) (*RegoEvaluator, error) {
cfg := config.ConfigurationPointer.Load()
// TODO: @CONFIG store in files instead of inmem? A lot of extra complexity, especially on reloads
store := inmem.NewFromObject(map[string]interface{}{
"docker_socket_authorizer_storage": map[string]interface{}{},
})
transaction, err := store.NewTransaction(context.Background(), storage.WriteParams)
if err != nil {
return nil, err
}
// We have to do this nonsense to avoid aborting a stale transaction; though doing that is preferable to returning with a hanging transaction
transactionIsCommitted := false
defer func() {
if !transactionIsCommitted {
store.Abort(context.Background(), transaction)
}
}()
function_dns_a := rego.Function1(
®o.Function{
Name: "dns.a",
Decl: types.NewFunction(types.Args(types.S), types.NewArray(make([]types.Type, 0), types.S)),
Memoize: true,
Nondeterministic: true,
},
func(_ rego.BuiltinContext, nameArgument *ast.Term) (*ast.Term, error) {
var name string
if err := ast.As(nameArgument.Value, &name); err != nil {
return nil, fmt.Errorf("dns.a: invalid argument (string required): %s", err)
}
forwardIps, err := net.LookupHost(name)
if err != nil {
return nil, fmt.Errorf("dns.a: error: %s", err)
}
ipTerms := make([]*ast.Term, len(forwardIps))
for i, name := range forwardIps {
ipTerms[i] = ast.StringTerm(name)
}
return ast.ArrayTerm(ipTerms...), nil
},
)
function_dns_ptr := rego.Function1(
®o.Function{
Name: "dns.ptr",
Decl: types.NewFunction(types.Args(types.S), types.NewArray(make([]types.Type, 0), types.S)),
Memoize: true,
Nondeterministic: true,
},
func(_ rego.BuiltinContext, ipArgument *ast.Term) (*ast.Term, error) {
var ip string
if err := ast.As(ipArgument.Value, &ip); err != nil {
return nil, fmt.Errorf("dns.ptr: invalid argument (string required): %s", err)
}
if ip == "" || ip == "@" {
return ast.ArrayTerm(), nil
}
names, err := net.LookupAddr(ip)
if err != nil {
return nil, fmt.Errorf("dns.ptr: invalid argument (IP address required): %s", err)
}
nameTerms := make([]*ast.Term, len(names))
for i, name := range names {
nameTerms[i] = ast.StringTerm(name)
}
return ast.ArrayTerm(nameTerms...), nil
},
)
policyMetaRego := rego.New(
function_dns_ptr,
function_dns_a,
rego.Strict(cfg.Policy.StrictMode),
rego.Module("docker_socket_meta_policy", META_POLICY),
rego.Query(QUERY),
)
policyLoader(policyMetaRego)
policyMetaQuery, err := policyMetaRego.PrepareForEval(context.Background())
if err != nil {
return nil, err
}
policyMetaResult, err := policyMetaQuery.Eval(context.Background())
if err != nil {
return nil, err
}
if !policyMetaResult[0].Bindings["meta_policy_ok"].(bool) {
if prettyOutput, err := json.Marshal(policyMetaResult[0].Bindings); err != nil {
return nil, fmt.Errorf("meta-policy validation failed and unable to serialize output to JSON (%s): %v", err, policyMetaResult[0].Bindings)
} else {
return nil, fmt.Errorf("meta-policy validation failed: %s", prettyOutput)
}
}
policyList := make([]string, 0, len(policyMetaResult[0].Bindings["all_policies"].([]interface{})))
for _, policy := range policyMetaResult[0].Bindings["all_policies"].([]interface{}) {
switch value := policy.(type) {
case string:
policyList = append(policyList, value)
default:
return nil, fmt.Errorf("invalid policy name of type %T (%v) in all_policies list returned by meta-policy; likely a bug", value, value)
}
}
for _, policy := range policyList {
path, ok := storage.ParsePath("/docker_socket_authorizer_storage/" + policy)
if !ok {
return nil, fmt.Errorf("unable to parse path to policy %s in store", policy)
}
store.Write(context.Background(), transaction, storage.AddOp, path, map[string]interface{}{})
}
newRegoObject := rego.New(
function_dns_ptr,
function_dns_a,
rego.Strict(cfg.Policy.StrictMode),
rego.Store(store),
rego.Transaction(transaction),
rego.Module("docker_socket_meta_policy", META_POLICY),
rego.Query(QUERY),
)
var printTo io.Writer = os.Stdout
switch cfg.Policy.PrintTo {
case "stdout":
printTo = os.Stdout
case "stderr":
printTo = os.Stderr
case "":
fallthrough
case "none":
printTo = nil
default:
slog.Warn("Unsupported policy.print_to configuration value; defaulting to stdout", slog.String("print_to", cfg.Policy.PrintTo))
}
if printTo != nil {
rego.EnablePrintStatements(true)(newRegoObject)
rego.PrintHook(topdown.NewPrintHook(printTo))(newRegoObject)
}
policyLoader(newRegoObject)
query, err := newRegoObject.PrepareForEval(context.Background())
if err != nil {
return nil, err
}
if err := store.Commit(context.Background(), transaction); err != nil {
return nil, err
}
transactionIsCommitted = true
return &RegoEvaluator{
authorizer: &query,
store: &store,
policyList: policyList,
}, nil
}
func (r *RegoEvaluator) EvaluateQuery(ctx context.Context, options ...rego.EvalOption) (rego.ResultSet, error) {
return r.authorizer.Eval(ctx, options...)
}
func (r *RegoEvaluator) WriteToStorage(ctx context.Context, toStore map[string]interface{}) error {
// Store has gone stale, so we're free to ignore the write (if we execute it no harm, just wasted effort)
// WARNING: if we stop using inmem or stop resetting storage on reload, this will break.
if r.isStale() {
return nil
}
transaction, err := (*r.store).NewTransaction(ctx, storage.WriteParams)
if err != nil {
return err
}
// We have to do this nonsense to avoid aborting a stale transaction; though doing that is preferable to returning with a hanging transaction
transactionIsCommitted := false
defer func() {
if !transactionIsCommitted {
(*r.store).Abort(context.Background(), transaction)
}
}()
for policy, toStore := range toStore {
if path, ok := storage.ParsePath("/docker_socket_authorizer_storage/" + policy); !ok {
return fmt.Errorf("unable to parse path to policy %s in store", policy)
} else if err := (*r.store).Write(ctx, transaction, storage.AddOp, path, toStore); err != nil {
return err
}
}
if err := (*r.store).Commit(ctx, transaction); err != nil {
return err
}
transactionIsCommitted = true
return nil
}
func (r *RegoEvaluator) isStale() bool {
return Evaluator.Load() != r
}