/
opa.go
277 lines (237 loc) · 6.92 KB
/
opa.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2017 Istio Authors
//
// 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.
// nolint: lll
//go:generate $GOPATH/src/istio.io/istio/bin/mixer_codegen.sh -a mixer/adapter/opa/config/config.proto -x "-n opa -t authorization"
package opa // import "istio.io/istio/mixer/adapter/opa"
import (
"context"
"fmt"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"istio.io/istio/mixer/adapter/opa/config"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/status"
"istio.io/istio/mixer/template/authorization"
)
type (
builder struct {
adapterConfig *config.Params
configErrors []error
compiler *ast.Compiler
}
handler struct {
checkMethod string
compiler *ast.Compiler
logger adapter.Logger
hasConfigError bool
failClose bool
}
)
const (
policyFileNamePrefix = "opa_policy"
)
///////////////// Configuration Methods ///////////////
func (b *builder) SetAuthorizationTypes(types map[string]*authorization.Type) {}
func (b *builder) SetAdapterConfig(cfg adapter.Config) {
b.adapterConfig = cfg.(*config.Params)
}
// To support fail close, Validate will not append errors to ce but will be appended to the configErrors.
// HandleAuthorization handle request based on configErrors and failClose option
func (b *builder) Validate() (ce *adapter.ConfigErrors) {
if len(b.adapterConfig.CheckMethod) == 0 {
msg := "check method is not configured"
if b.adapterConfig.FailClose {
b.configErrors = append(b.configErrors, fmt.Errorf(msg))
} else {
ce = ce.Appendf("CheckMethod", msg)
}
}
parsedModuleCount := 0
moduleParseErrorCount := 0
modules := map[string]*ast.Module{}
for index, policy := range b.adapterConfig.Policy {
filename := fmt.Sprintf("%v.%v", policyFileNamePrefix, index)
parsed, err := ast.ParseModule(filename, policy)
if err != nil {
if b.adapterConfig.FailClose {
b.configErrors = append(b.configErrors, err)
} else {
ce = ce.Append("Policy", err)
}
moduleParseErrorCount++
} else {
modules[filename] = parsed
parsedModuleCount++
}
}
if moduleParseErrorCount > 0 {
return
}
if parsedModuleCount == 0 {
msg := "policies are not configured"
if b.adapterConfig.FailClose {
b.configErrors = append(b.configErrors, fmt.Errorf(msg))
} else {
ce = ce.Appendf("Policy", msg)
}
return
}
compiler := ast.NewCompiler()
compiler.Compile(modules)
if compiler.Failed() {
for _, err := range compiler.Errors {
if b.adapterConfig.FailClose {
b.configErrors = append(b.configErrors, fmt.Errorf("%v", err.Error()))
} else {
ce = ce.Appendf("Policy", err.Error())
}
}
} else {
b.compiler = compiler
}
return
}
func (b *builder) Build(context context.Context, env adapter.Env) (adapter.Handler, error) {
if len(b.configErrors) >= 0 {
for _, err := range b.configErrors {
_ = env.Logger().Errorf("%v", err)
}
}
return &handler{
compiler: b.compiler,
checkMethod: b.adapterConfig.CheckMethod,
failClose: b.adapterConfig.FailClose,
logger: env.Logger(),
hasConfigError: len(b.configErrors) > 0,
}, nil
}
////////////////// Runtime Methods //////////////////////////
func convertActionObjectToMap(action *authorization.Action) map[string]interface{} {
result := map[string]interface{}{}
if len(action.Namespace) > 0 {
result["namespace"] = action.Namespace
}
if len(action.Service) > 0 {
result["service"] = action.Service
}
if len(action.Method) > 0 {
result["method"] = action.Method
}
if len(action.Path) > 0 {
result["path"] = action.Path
}
if action.Properties != nil {
properties := map[string]interface{}{}
count := 0
for key, val := range action.Properties {
properties[key] = val
count++
}
if count > 0 {
result["properties"] = properties
}
}
return result
}
func convertSubjectObjectToMap(subject *authorization.Subject) map[string]interface{} {
result := map[string]interface{}{}
if len(subject.User) > 0 {
result["user"] = subject.User
}
// TODO(jaebong): repeated string is not supported yet.
if len(subject.Groups) > 0 {
result["groups"] = []interface{}{subject.Groups}
}
/*
if subject.Groups != nil && len(subject.Groups) > 0 {
groups := []interface{}{}
for _, group := range subject.Groups {
groups = append(groups, group)
}
result["groups"] = groups
}
*/
if subject.Properties != nil {
properties := map[string]interface{}{}
count := 0
for key, val := range subject.Properties {
properties[key] = val
count++
}
if count > 0 {
result["properties"] = properties
}
}
return result
}
func (h *handler) handleFailClose(err error) (adapter.CheckResult, error) {
retStatus := status.OK
if h.failClose {
retStatus = status.WithPermissionDenied(err.Error())
}
return adapter.CheckResult{
Status: retStatus,
}, nil
}
func (h *handler) HandleAuthorization(context context.Context, instance *authorization.Instance) (adapter.CheckResult, error) {
// Handle configuration error
if h.hasConfigError {
return h.handleFailClose(fmt.Errorf("opa: request was rejected"))
}
// eval rego policy scripts
rs, err := rego.New(
rego.Compiler(h.compiler),
rego.Query(h.checkMethod),
rego.Input(map[string]interface{}{
"action": convertActionObjectToMap(instance.Action),
"subject": convertSubjectObjectToMap(instance.Subject),
}),
).Eval(context)
// Handle errors from OPA engine, policy scripts
if err != nil {
return h.handleFailClose(fmt.Errorf("opa: request was rejected. err: %v", err))
}
if len(rs) != 1 {
return h.handleFailClose(fmt.Errorf("opa: request was rejected"))
}
result, ok := rs[0].Expressions[0].Value.(bool)
if !ok {
return h.handleFailClose(fmt.Errorf("opa: request was rejected"))
}
// rejected by policy scripts
if !result {
return adapter.CheckResult{
Status: status.WithPermissionDenied("opa: request was rejected"),
}, nil
}
// Accepted
return adapter.CheckResult{Status: status.OK}, nil
}
func (h *handler) Close() error {
return nil
}
////////////////// Bootstrap //////////////////////////
// GetInfo returns the Info associated with this adapter implementation.
func GetInfo() adapter.Info {
return adapter.Info{
Name: "opa",
Impl: "istio.io/istio/mixer/adapter/opa",
Description: "Istio Authorization with Open Policy Agent engine",
SupportedTemplates: []string{
authorization.TemplateName,
},
DefaultConfig: &config.Params{},
NewBuilder: func() adapter.HandlerBuilder { return &builder{} },
}
}