-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorization.go
250 lines (222 loc) · 6.4 KB
/
authorization.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
package driplimit
import (
"context"
"errors"
"fmt"
)
// PolicyAction represents an action that can be performed. eg. read, write
type PolicyAction string
const (
Read PolicyAction = "read"
Write PolicyAction = "write"
)
// Policy gives the read and write permissions.
type Policy struct {
Read bool `json:"read" description:"Read permission"`
Write bool `json:"write" description:"Write permission"`
}
// all is a wildcard for all ids. This can be found in items policies
const all = "*"
// Can checks if the policy allows the action.
func (k Policy) Can(action PolicyAction) bool {
switch action {
case "read":
return k.Read
case "write":
return k.Write
default:
return false
}
}
// Policies is a map of policies.
type Policies map[string]Policy
// Can checks if the action can be performed on item id.
func (policies Policies) Can(action PolicyAction, id string) bool {
policy, found := policies[all]
if found {
if policy.Can(action) {
return true
}
}
policy, found = policies[id]
if !found {
return false
}
return policy.Can(action)
}
// Authorizer is an authorization wrapper. It implements the Service interface.
type Authorizer struct {
driplimit Service
}
// NewAuthorizer wraps a Driplimit ServiceWithToken with an authorizer.
func NewAuthorizer(driplimit Service) *Authorizer {
return &Authorizer{
driplimit: driplimit,
}
}
// caller gets the service key from the context.
func (a *Authorizer) caller(ctx context.Context, payload Payload) (sk *ServiceKey, err error) {
sk, err = a.driplimit.ServiceKeyGet(ctx, ServiceKeyGetPayload{Token: payload.ServiceToken()})
if err != nil {
if errors.Is(err, ErrNotFound) {
return nil, ErrUnauthorized
}
return nil, fmt.Errorf("failed to get service key: %w", err)
}
return sk, nil
}
func (a *Authorizer) KeyCheck(ctx context.Context, payload KeysCheckPayload) (key *Key, err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return nil, err
}
if sk.Admin || sk.KeyspacesPolicies.Can(Read, payload.KSID) {
return a.driplimit.KeyCheck(ctx, payload)
}
return nil, ErrUnauthorized
}
func (a *Authorizer) KeyCreate(ctx context.Context, payload KeyCreatePayload) (key *Key, err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return nil, err
}
if sk.Admin || sk.KeyspacesPolicies.Can(Write, payload.KSID) {
return a.driplimit.KeyCreate(ctx, payload)
}
return nil, ErrUnauthorized
}
func (a *Authorizer) KeyGet(ctx context.Context, payload KeyGetPayload) (key *Key, err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return nil, err
}
if sk.Admin || sk.KeyspacesPolicies.Can(Read, payload.KSID) {
return a.driplimit.KeyGet(ctx, payload)
}
return nil, ErrUnauthorized
}
func (a *Authorizer) KeyList(ctx context.Context, payload KeyListPayload) (klist *KeyList, err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return nil, err
}
if sk.Admin || sk.KeyspacesPolicies.Can(Read, payload.KSID) {
return a.driplimit.KeyList(ctx, payload)
}
return nil, ErrUnauthorized
}
func (a *Authorizer) KeyDelete(ctx context.Context, payload KeyDeletePayload) (err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return err
}
if sk.Admin || sk.KeyspacesPolicies.Can("write", payload.KSID) {
return a.driplimit.KeyDelete(ctx, payload)
}
return ErrUnauthorized
}
func (a *Authorizer) KeyspaceGet(ctx context.Context, payload KeyspaceGetPayload) (keyspace *Keyspace, err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return nil, err
}
if sk.Admin || sk.KeyspacesPolicies.Can(Read, payload.KSID) {
return a.driplimit.KeyspaceGet(ctx, payload)
}
return nil, ErrUnauthorized
}
func (a *Authorizer) KeyspaceCreate(ctx context.Context, payload KeyspaceCreatePayload) (keyspace *Keyspace, err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return nil, err
}
if sk.Admin {
return a.driplimit.KeyspaceCreate(ctx, payload)
}
return nil, ErrUnauthorized
}
func (a *Authorizer) KeyspaceList(ctx context.Context, payload KeyspaceListPayload) (kslist *KeyspaceList, err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return nil, err
}
if sk.Admin || sk.KeyspacesPolicies.Can(Read, all) {
return a.driplimit.KeyspaceList(ctx, payload)
}
// if the service key is not allowed to read all keyspaces, then filter by SKID
payload.FilterBySKIDKeyspacesPolicies = sk.SKID
return a.driplimit.KeyspaceList(ctx, payload)
}
func (a *Authorizer) KeyspaceDelete(ctx context.Context, payload KeyspaceDeletePayload) (err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return err
}
if sk.Admin {
return a.driplimit.KeyspaceDelete(ctx, payload)
}
return ErrUnauthorized
}
func (a *Authorizer) ServiceKeyGet(ctx context.Context, payload ServiceKeyGetPayload) (sk *ServiceKey, err error) {
sk, err = a.caller(ctx, payload)
if err != nil {
return nil, err
}
if sk.Admin {
return a.driplimit.ServiceKeyGet(ctx, payload)
}
requestedServiceKey, err := a.driplimit.ServiceKeyGet(ctx, payload)
if err != nil {
if errors.Is(err, ErrNotFound) {
return nil, ErrUnauthorized
}
return nil, fmt.Errorf("failed to get service key: %w", err)
}
if requestedServiceKey.SKID != sk.SKID {
return nil, ErrUnauthorized
}
return requestedServiceKey, nil
}
func (a *Authorizer) ServiceKeyCreate(ctx context.Context, payload ServiceKeyCreatePayload) (sk *ServiceKey, err error) {
sk, err = a.caller(ctx, payload)
if err != nil {
return nil, err
}
if sk.Admin {
return a.driplimit.ServiceKeyCreate(ctx, payload)
}
return nil, ErrUnauthorized
}
func (a *Authorizer) ServiceKeyList(ctx context.Context, payload ServiceKeyListPayload) (sklist *ServiceKeyList, err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return nil, err
}
if sk.Admin {
return a.driplimit.ServiceKeyList(ctx, payload)
}
return nil, ErrUnauthorized
}
func (a *Authorizer) ServiceKeyDelete(ctx context.Context, payload ServiceKeyDeletePayload) (err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return err
}
if sk.SKID == payload.SKID {
return ErrCannotDeleteItself
}
if sk.Admin {
return a.driplimit.ServiceKeyDelete(ctx, payload)
}
return ErrUnauthorized
}
func (a *Authorizer) ServiceKeySetToken(ctx context.Context, payload ServiceKeySetTokenPayload) (err error) {
sk, err := a.caller(ctx, payload)
if err != nil {
return err
}
if sk.Admin || sk.SKID == payload.SKID {
return a.driplimit.ServiceKeySetToken(ctx, payload)
}
return ErrUnauthorized
}