forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacl.go
311 lines (271 loc) · 8.51 KB
/
acl.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2019 The ChubaoFS 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 objectnode
// https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/S3_ACLs_UsingACLs.html
import (
"encoding/xml"
"errors"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/util/log"
)
const (
maxGrantCount = 100 //ACL 可以拥有最多 100 个授权。
bucketRootPath = "/"
)
const (
//Permission Value
ReadPermission Permission = "READ"
WritePermission = "WRITE"
ReadACPPermission = "READ_ACP"
WriteACPPermission = "WRITE_ACP"
FullControlPermission = "FULL_CONTROL"
)
// Mapping of ACL Permissions and Access Policy Permissions
// Reference: https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/acl-overview.html
var (
aclBucketPermissionActions = map[Permission]proto.Actions{
ReadPermission: {
proto.OSSListObjectsAction,
proto.OSSListObjectVersionsAction,
proto.OSSListMultipartUploadsAction,
},
WritePermission: {
proto.OSSPutObjectAction,
proto.OSSDeleteObjectAction,
proto.OSSDeleteObjectsAction,
},
ReadACPPermission: {
proto.OSSGetBucketAclAction,
},
WriteACPPermission: {
proto.OSSPutBucketAclAction,
},
FullControlPermission: {
proto.OSSListObjectsAction,
proto.OSSListObjectVersionsAction,
proto.OSSListMultipartUploadsAction,
proto.OSSPutObjectAction,
proto.OSSDeleteObjectAction,
proto.OSSDeleteObjectsAction,
proto.OSSGetBucketAclAction,
proto.OSSPutBucketAclAction,
},
}
aclObjectPermissionActions = map[Permission]proto.Actions{
ReadPermission: {
proto.OSSGetObjectAction,
proto.OSSGetObjectTorrentAction,
},
WritePermission: {},
ReadACPPermission: {
proto.OSSGetObjectAclAction,
},
WriteACPPermission: {
proto.OSSPutObjectAclAction},
FullControlPermission: {
proto.OSSGetObjectAction,
proto.OSSGetObjectTorrentAction,
proto.OSSGetObjectAclAction,
proto.OSSPutObjectAclAction,
},
}
)
type StandardACL string
const (
PrivateACL StandardACL = "private"
PublicReadACL = "public-read"
PubliceReadWriteACL = "public-read-write"
AwsExecReadACL = "aws-exec-read"
AuthenticatedReadACL = "authenticated-read"
BucketOwnerReadACL = "bucket-owner-read"
BucketOwnerFullControlACL = "bucket-owner-full-control"
LogDeliveryWriteACL = "log-delivery-write"
)
type ResourceType string
const (
bucketResource ResourceType = "bucket"
objectResource = "object"
)
type AclRole = string
const (
objectOwnerRole AclRole = "owner"
bucketOwnerRole = "bucket-owner"
allUsersRole = "AllUsers"
LogDeliveryRole = "LogDelivery"
)
var (
aclPermissions = map[StandardACL]map[ResourceType]map[string][]Permission{
PrivateACL: {"bucket": {"owner": {FullControlPermission}}, "object": {"owner": {FullControlPermission}}},
PublicReadACL: {"bucket": {"owner": {FullControlPermission}, "AllUsers": {ReadPermission}}, "object": {"owner": {FullControlPermission}, "AllUsers": {ReadPermission}}},
PubliceReadWriteACL: {"bucket": {"owner": {FullControlPermission}, "AllUsers": {ReadPermission, WritePermission}}, "object": {"owner": {FullControlPermission}, "AllUsers": {ReadPermission, WritePermission}}},
AwsExecReadACL: {"bucket": {"owner": {FullControlPermission}}, "object": {"owner": {FullControlPermission}}},
AuthenticatedReadACL: {"bucket": {"owner": {FullControlPermission}}, "object": {"owner": {FullControlPermission}}},
BucketOwnerReadACL: {"object": {"owner": {FullControlPermission}, "bucket-owner": {ReadPermission}}},
BucketOwnerFullControlACL: {"object": {"owner": {FullControlPermission}, "bucket-owner": {FullControlPermission}}},
LogDeliveryWriteACL: {"bucket": {"LogDelivery": {WriteACPPermission, ReadACPPermission}}},
}
)
//grant permission
type Permission string
// grantee
type Grantee struct {
Xmlns string `xml:"xmlns:xsi,attr,omitempty"`
Xmlsi string `xml:"xsi:type,attr,omitempty"`
Id string `xml:"ID,omitempty"`
URI string `xml:"URI,omitempty"`
Type string `xml:"Type,omitempty"`
DisplayName string `xml:"DisplayName,omitempty"`
EmailAddress string `xml:"EmailAddress,omitempty"`
}
// grant
type Grant struct {
Grantee Grantee `xml:"Grantee,omitempty"`
Permission Permission `xml:"Permission,omitempty"`
}
// access control list
type AccessControlList struct {
Grants []Grant `xml:"Grant,omitempty"`
}
func (acl *AccessControlList) IsEmpty() bool {
return len(acl.Grants) == 0
}
// owner
type Owner struct {
Id string `xml:"ID"`
DisplayName string `xml:"DisplayName"`
}
// access control policy
type AccessControlPolicy struct {
Xmlns string `xml:"xmlns:xsi,attr"`
Owner Owner `xml:"Owner,omitempty"`
Acl AccessControlList `xml:"AccessControlList,omitempty"`
}
func (acp *AccessControlPolicy) IsAclEmpty() bool {
return acp.Acl.IsEmpty()
}
func (acp *AccessControlPolicy) Validate(bucket string) (bool, error) {
for _, grant := range acp.Acl.Grants {
if !grant.Validate() {
return false, nil
}
}
return true, nil
}
func (acp *AccessControlPolicy) IsAllowed(param *RequestParam) bool {
log.LogInfof("acl is allowed ?")
if len(acp.Acl.Grants) == 0 {
return true
}
for _, grant := range acp.Acl.Grants {
if grant.IsAllowed(param) {
return true
}
}
return false
}
var (
aclGrantKeyPermissionMap = map[string]Permission{
"x-amz-grant-full-control": FullControlPermission,
"x-amz-grant-read": ReadPermission,
"x-amz-grant-read-acp": ReadACPPermission,
"x-amz-grant-write": WritePermission,
"x-amz-grant-write-acp": WriteACPPermission,
}
aclRoleURIMap = map[string]string{
"AllUsers": "http://acs.amazonaws.com/groups/global/AllUsers",
"LogDelivery": "http://acs.amazonaws.com/groups/s3/LogDelivery",
}
)
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketAcl.html
func (acp *AccessControlPolicy) SetBucketStandardACL(param *RequestParam, acl string) {
sacl := StandardACL(acl)
var (
rolePermissionsMap map[string][]Permission
ok bool
)
if rolePermissionsMap, ok = aclPermissions[sacl]["bucket"]; !ok {
return
}
for role, permissions := range rolePermissionsMap {
grantee := Grantee{}
if uri, ok := aclRoleURIMap[role]; ok {
grantee.URI = uri
} else {
grantee.Id = param.accessKey
grantee.DisplayName = param.accessKey
}
for _, p := range permissions {
grant := Grant{
Grantee: grantee,
Permission: p,
}
acp.Acl.Grants = append(acp.Acl.Grants, grant)
}
}
}
func (acp *AccessControlPolicy) SetBucketGrantACL(param *RequestParam, permission Permission) {
grantee := Grantee{
Id: param.accessKey,
DisplayName: param.accessKey,
}
grant := Grant{
Grantee: grantee,
Permission: permission,
}
acp.Acl.Grants = append(acp.Acl.Grants, grant)
}
func (acp *AccessControlPolicy) Marshal() ([]byte, error) {
data, err := xml.Marshal(acp)
if err != nil {
return nil, err
}
return append([]byte(xml.Header), data...), nil
}
func ParseACL(bytes []byte, bucket string) (*AccessControlPolicy, error) {
acl := &AccessControlPolicy{}
err2 := xml.Unmarshal(bytes, acl)
if err2 != nil {
return nil, err2
}
ok, err3 := acl.Validate(bucket)
if err3 != nil {
return nil, err3
}
if !ok {
return nil, errors.New("")
}
return acl, nil
}
func storeBucketACL(bytes []byte, vol *Volume) (*AccessControlPolicy, error) {
acl, err3 := ParseACL(bytes, vol.name)
if err3 != nil {
return nil, err3
}
err4 := vol.store.Put(vol.name, bucketRootPath, OSS_ACL_KEY, bytes)
if err4 != nil {
return nil, err4
}
vol.storeACL(acl)
return acl, nil
}
func (g Grant) Validate() bool {
return true
}
func (g *Grant) IsAllowed(param *RequestParam) bool {
if param.accessKey != g.Grantee.Id {
return false
}
actions := aclBucketPermissionActions[g.Permission]
return IsIntersectionActions(actions, param.Action())
}