/
opa_bundle.go
206 lines (169 loc) · 5.21 KB
/
opa_bundle.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
/*
Copyright 2021 The KodeRover 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.
*/
package bundle
import (
"net/http"
"sort"
"github.com/koderover/zadig/pkg/config"
"github.com/koderover/zadig/pkg/microservice/policy/core/repository/models"
"github.com/koderover/zadig/pkg/microservice/policy/core/yamlconfig"
"github.com/koderover/zadig/pkg/tool/log"
"github.com/koderover/zadig/pkg/tool/opa"
"github.com/koderover/zadig/pkg/types"
)
const (
manifestPath = ".manifest"
policyRegoPath = "authz.rego"
exemptionsPath = "exemptions/data.json"
policyRoot = "rbac"
rolesRoot = "roles"
rolebindingsRoot = "bindings"
exemptionsRoot = "exemptions"
resourcesRoot = "resources"
policiesRoot = "policies"
)
type expressionOperator string
const (
MethodGet = http.MethodGet
MethodPost = http.MethodPost
MethodPut = http.MethodPut
MethodPatch = http.MethodPatch
MethodDelete = http.MethodDelete
)
var AllMethods = []string{MethodGet, MethodPost, MethodPut, MethodPatch, MethodDelete}
var revision string
type Rule struct {
Method string `json:"method"`
Endpoint string `json:"endpoint"`
ResourceType string `json:"resourceType,omitempty"`
IDRegex string `json:"idRegex,omitempty"`
MatchAttributes Attributes `json:"matchAttributes,omitempty"`
MatchExpressions []expression `json:"matchExpressions,omitempty"`
}
type Attribute struct {
Key string `json:"key"`
Value string `json:"value"`
}
func NewFrom(attrs []models.MatchAttribute) Attributes {
var att Attributes
for _, v := range attrs {
a := Attribute{
Key: v.Key,
Value: v.Value,
}
att = append(att, &a)
}
return att
}
type Attributes []*Attribute
func (a Attributes) LessOrEqual(other Attributes) bool {
if len(a) == 0 {
return true
}
if len(other) == 0 {
return false
}
for i, attr := range a {
if i > len(other)-1 {
return false
}
ki := attr.Key
kj := other[i].Key
vi := attr.Value
vj := other[i].Value
if ki == kj {
if vi == vj {
continue
}
return vi < vj
}
return ki < kj
}
return true
}
type expression struct {
Key string `json:"key"`
Values []string `json:"values"`
Operator expressionOperator `json:"operator"`
}
type Rules []*Rule
func (o Rules) Len() int { return len(o) }
func (o Rules) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o Rules) Less(i, j int) bool {
if o[i].Endpoint == o[j].Endpoint {
if o[i].Method == o[j].Method {
return o[i].MatchAttributes.LessOrEqual(o[j].MatchAttributes)
}
return o[i].Method < o[j].Method
}
return o[i].Endpoint < o[j].Endpoint
}
type ExemptionURLs struct {
Public Rules `json:"public"` // public urls are not controlled by AuthN and AuthZ
Privileged Rules `json:"privileged"` // privileged urls can only be visited by system admins
Registered Rules `json:"registered"` // registered urls are the entire list of urls which are controlled by AuthZ, which means that if an url is not in this list, it is not controlled by AuthZ
}
func generateOPAExemptionURLs(policies []*types.PolicyMeta) *ExemptionURLs {
data := &ExemptionURLs{}
for _, r := range yamlconfig.GetExemptionsUrls().Public {
if len(r.Methods) == 1 && r.Methods[0] == models.MethodAll {
r.Methods = AllMethods
}
for _, method := range r.Methods {
data.Public = append(data.Public, &Rule{Method: method, Endpoint: r.Endpoint})
}
}
sort.Sort(data.Public)
resourceMappings := getResourceActionMappings(false, policies)
for _, resourceMappings := range resourceMappings {
for _, rs := range resourceMappings {
for _, r := range rs {
data.Registered = append(data.Registered, r)
}
}
}
adminURLs := append(yamlconfig.GetExemptionsUrls().SystemAdmin, yamlconfig.GetExemptionsUrls().ProjectAdmin...)
for _, r := range adminURLs {
if len(r.Methods) == 1 && r.Methods[0] == models.MethodAll {
r.Methods = AllMethods
}
for _, method := range r.Methods {
data.Registered = append(data.Registered, &Rule{Method: method, Endpoint: r.Endpoint})
}
}
sort.Sort(data.Registered)
return data
}
func generateOPAPolicyRego() []byte {
return authz
}
func GenerateOPABundle() error {
policieMetas := yamlconfig.DefaultPolicyMetasConfig().Policies()
bundle := &opa.Bundle{
Data: []*opa.DataSpec{
{Data: generateOPAPolicyRego(), Path: policyRegoPath},
{Data: generateOPAExemptionURLs(policieMetas), Path: exemptionsPath},
},
Roots: []string{policyRoot, rolesRoot, rolebindingsRoot, exemptionsRoot, resourcesRoot, policiesRoot},
}
hash, err := bundle.Rehash()
if err != nil {
log.Errorf("Failed to calculate bundle hash, err: %s", err)
return err
}
revision = hash
return bundle.Save(config.DataPath())
}
func GetRevision() string {
return revision
}