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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
package s3api
import (
"time"
"github.com/seaweedfs/seaweedfs/weed/iam/policy"
)
// S3PolicyTemplates provides pre-built IAM policy templates for common S3 use cases
type S3PolicyTemplates struct{}
// NewS3PolicyTemplates creates a new policy templates provider
func NewS3PolicyTemplates() *S3PolicyTemplates {
return &S3PolicyTemplates{}
}
// GetS3ReadOnlyPolicy returns a policy that allows read-only access to all S3 resources
func (t *S3PolicyTemplates) GetS3ReadOnlyPolicy() *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "S3ReadOnlyAccess",
Effect: "Allow",
Action: []string{
"s3:GetObject",
"s3:GetObjectVersion",
"s3:ListBucket",
"s3:ListBucketVersions",
"s3:GetBucketLocation",
"s3:GetBucketVersioning",
"s3:ListAllMyBuckets",
},
Resource: []string{
"arn:seaweed:s3:::*",
"arn:seaweed:s3:::*/*",
},
},
},
}
}
// GetS3WriteOnlyPolicy returns a policy that allows write-only access to all S3 resources
func (t *S3PolicyTemplates) GetS3WriteOnlyPolicy() *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "S3WriteOnlyAccess",
Effect: "Allow",
Action: []string{
"s3:PutObject",
"s3:PutObjectAcl",
"s3:CreateMultipartUpload",
"s3:UploadPart",
"s3:CompleteMultipartUpload",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploads",
"s3:ListParts",
},
Resource: []string{
"arn:seaweed:s3:::*",
"arn:seaweed:s3:::*/*",
},
},
},
}
}
// GetS3AdminPolicy returns a policy that allows full admin access to all S3 resources
func (t *S3PolicyTemplates) GetS3AdminPolicy() *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "S3FullAccess",
Effect: "Allow",
Action: []string{
"s3:*",
},
Resource: []string{
"arn:seaweed:s3:::*",
"arn:seaweed:s3:::*/*",
},
},
},
}
}
// GetBucketSpecificReadPolicy returns a policy for read-only access to a specific bucket
func (t *S3PolicyTemplates) GetBucketSpecificReadPolicy(bucketName string) *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "BucketSpecificReadAccess",
Effect: "Allow",
Action: []string{
"s3:GetObject",
"s3:GetObjectVersion",
"s3:ListBucket",
"s3:ListBucketVersions",
"s3:GetBucketLocation",
},
Resource: []string{
"arn:seaweed:s3:::" + bucketName,
"arn:seaweed:s3:::" + bucketName + "/*",
},
},
},
}
}
// GetBucketSpecificWritePolicy returns a policy for write-only access to a specific bucket
func (t *S3PolicyTemplates) GetBucketSpecificWritePolicy(bucketName string) *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "BucketSpecificWriteAccess",
Effect: "Allow",
Action: []string{
"s3:PutObject",
"s3:PutObjectAcl",
"s3:CreateMultipartUpload",
"s3:UploadPart",
"s3:CompleteMultipartUpload",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploads",
"s3:ListParts",
},
Resource: []string{
"arn:seaweed:s3:::" + bucketName,
"arn:seaweed:s3:::" + bucketName + "/*",
},
},
},
}
}
// GetPathBasedAccessPolicy returns a policy that restricts access to a specific path within a bucket
func (t *S3PolicyTemplates) GetPathBasedAccessPolicy(bucketName, pathPrefix string) *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "ListBucketPermission",
Effect: "Allow",
Action: []string{
"s3:ListBucket",
},
Resource: []string{
"arn:seaweed:s3:::" + bucketName,
},
Condition: map[string]map[string]interface{}{
"StringLike": map[string]interface{}{
"s3:prefix": []string{pathPrefix + "/*"},
},
},
},
{
Sid: "PathBasedObjectAccess",
Effect: "Allow",
Action: []string{
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:CreateMultipartUpload",
"s3:UploadPart",
"s3:CompleteMultipartUpload",
"s3:AbortMultipartUpload",
},
Resource: []string{
"arn:seaweed:s3:::" + bucketName + "/" + pathPrefix + "/*",
},
},
},
}
}
// GetIPRestrictedPolicy returns a policy that restricts access based on source IP
func (t *S3PolicyTemplates) GetIPRestrictedPolicy(allowedCIDRs []string) *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "IPRestrictedS3Access",
Effect: "Allow",
Action: []string{
"s3:*",
},
Resource: []string{
"arn:seaweed:s3:::*",
"arn:seaweed:s3:::*/*",
},
Condition: map[string]map[string]interface{}{
"IpAddress": map[string]interface{}{
"aws:SourceIp": allowedCIDRs,
},
},
},
},
}
}
// GetTimeBasedAccessPolicy returns a policy that allows access only during specific hours
func (t *S3PolicyTemplates) GetTimeBasedAccessPolicy(startHour, endHour int) *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "TimeBasedS3Access",
Effect: "Allow",
Action: []string{
"s3:GetObject",
"s3:PutObject",
"s3:ListBucket",
},
Resource: []string{
"arn:seaweed:s3:::*",
"arn:seaweed:s3:::*/*",
},
Condition: map[string]map[string]interface{}{
"DateGreaterThan": map[string]interface{}{
"aws:CurrentTime": time.Now().Format("2006-01-02") + "T" +
formatHour(startHour) + ":00:00Z",
},
"DateLessThan": map[string]interface{}{
"aws:CurrentTime": time.Now().Format("2006-01-02") + "T" +
formatHour(endHour) + ":00:00Z",
},
},
},
},
}
}
// GetMultipartUploadPolicy returns a policy specifically for multipart upload operations
func (t *S3PolicyTemplates) GetMultipartUploadPolicy(bucketName string) *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "MultipartUploadOperations",
Effect: "Allow",
Action: []string{
"s3:CreateMultipartUpload",
"s3:UploadPart",
"s3:CompleteMultipartUpload",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploads",
"s3:ListParts",
},
Resource: []string{
"arn:seaweed:s3:::" + bucketName + "/*",
},
},
{
Sid: "ListBucketForMultipart",
Effect: "Allow",
Action: []string{
"s3:ListBucket",
},
Resource: []string{
"arn:seaweed:s3:::" + bucketName,
},
},
},
}
}
// GetPresignedURLPolicy returns a policy for generating and using presigned URLs
func (t *S3PolicyTemplates) GetPresignedURLPolicy(bucketName string) *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "PresignedURLAccess",
Effect: "Allow",
Action: []string{
"s3:GetObject",
"s3:PutObject",
},
Resource: []string{
"arn:seaweed:s3:::" + bucketName + "/*",
},
Condition: map[string]map[string]interface{}{
"StringEquals": map[string]interface{}{
"s3:x-amz-signature-version": "AWS4-HMAC-SHA256",
},
},
},
},
}
}
// GetTemporaryAccessPolicy returns a policy for temporary access with expiration
func (t *S3PolicyTemplates) GetTemporaryAccessPolicy(bucketName string, expirationHours int) *policy.PolicyDocument {
expirationTime := time.Now().Add(time.Duration(expirationHours) * time.Hour)
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "TemporaryS3Access",
Effect: "Allow",
Action: []string{
"s3:GetObject",
"s3:PutObject",
"s3:ListBucket",
},
Resource: []string{
"arn:seaweed:s3:::" + bucketName,
"arn:seaweed:s3:::" + bucketName + "/*",
},
Condition: map[string]map[string]interface{}{
"DateLessThan": map[string]interface{}{
"aws:CurrentTime": expirationTime.UTC().Format("2006-01-02T15:04:05Z"),
},
},
},
},
}
}
// GetContentTypeRestrictedPolicy returns a policy that restricts uploads to specific content types
func (t *S3PolicyTemplates) GetContentTypeRestrictedPolicy(bucketName string, allowedContentTypes []string) *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "ContentTypeRestrictedUpload",
Effect: "Allow",
Action: []string{
"s3:PutObject",
"s3:CreateMultipartUpload",
"s3:UploadPart",
"s3:CompleteMultipartUpload",
},
Resource: []string{
"arn:seaweed:s3:::" + bucketName + "/*",
},
Condition: map[string]map[string]interface{}{
"StringEquals": map[string]interface{}{
"s3:content-type": allowedContentTypes,
},
},
},
{
Sid: "ReadAccess",
Effect: "Allow",
Action: []string{
"s3:GetObject",
"s3:ListBucket",
},
Resource: []string{
"arn:seaweed:s3:::" + bucketName,
"arn:seaweed:s3:::" + bucketName + "/*",
},
},
},
}
}
// GetDenyDeletePolicy returns a policy that allows all operations except delete
func (t *S3PolicyTemplates) GetDenyDeletePolicy() *policy.PolicyDocument {
return &policy.PolicyDocument{
Version: "2012-10-17",
Statement: []policy.Statement{
{
Sid: "AllowAllExceptDelete",
Effect: "Allow",
Action: []string{
"s3:GetObject",
"s3:GetObjectVersion",
"s3:PutObject",
"s3:PutObjectAcl",
"s3:ListBucket",
"s3:ListBucketVersions",
"s3:CreateMultipartUpload",
"s3:UploadPart",
"s3:CompleteMultipartUpload",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploads",
"s3:ListParts",
},
Resource: []string{
"arn:seaweed:s3:::*",
"arn:seaweed:s3:::*/*",
},
},
{
Sid: "DenyDeleteOperations",
Effect: "Deny",
Action: []string{
"s3:DeleteObject",
"s3:DeleteObjectVersion",
"s3:DeleteBucket",
},
Resource: []string{
"arn:seaweed:s3:::*",
"arn:seaweed:s3:::*/*",
},
},
},
}
}
// Helper function to format hour with leading zero
func formatHour(hour int) string {
if hour < 10 {
return "0" + string(rune('0'+hour))
}
return string(rune('0'+hour/10)) + string(rune('0'+hour%10))
}
// PolicyTemplateDefinition represents metadata about a policy template
type PolicyTemplateDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Category string `json:"category"`
UseCase string `json:"use_case"`
Parameters []PolicyTemplateParam `json:"parameters,omitempty"`
Policy *policy.PolicyDocument `json:"policy"`
}
// PolicyTemplateParam represents a parameter for customizing policy templates
type PolicyTemplateParam struct {
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Required bool `json:"required"`
DefaultValue string `json:"default_value,omitempty"`
Example string `json:"example,omitempty"`
}
// GetAllPolicyTemplates returns all available policy templates with metadata
func (t *S3PolicyTemplates) GetAllPolicyTemplates() []PolicyTemplateDefinition {
return []PolicyTemplateDefinition{
{
Name: "S3ReadOnlyAccess",
Description: "Provides read-only access to all S3 buckets and objects",
Category: "Basic Access",
UseCase: "Data consumers, backup services, monitoring applications",
Policy: t.GetS3ReadOnlyPolicy(),
},
{
Name: "S3WriteOnlyAccess",
Description: "Provides write-only access to all S3 buckets and objects",
Category: "Basic Access",
UseCase: "Data ingestion services, backup applications",
Policy: t.GetS3WriteOnlyPolicy(),
},
{
Name: "S3AdminAccess",
Description: "Provides full administrative access to all S3 resources",
Category: "Administrative",
UseCase: "S3 administrators, service accounts with full control",
Policy: t.GetS3AdminPolicy(),
},
{
Name: "BucketSpecificRead",
Description: "Provides read-only access to a specific bucket",
Category: "Bucket-Specific",
UseCase: "Applications that need access to specific data sets",
Parameters: []PolicyTemplateParam{
{
Name: "bucketName",
Type: "string",
Description: "Name of the S3 bucket to grant access to",
Required: true,
Example: "my-data-bucket",
},
},
Policy: t.GetBucketSpecificReadPolicy("${bucketName}"),
},
{
Name: "BucketSpecificWrite",
Description: "Provides write-only access to a specific bucket",
Category: "Bucket-Specific",
UseCase: "Upload services, data ingestion for specific datasets",
Parameters: []PolicyTemplateParam{
{
Name: "bucketName",
Type: "string",
Description: "Name of the S3 bucket to grant access to",
Required: true,
Example: "my-upload-bucket",
},
},
Policy: t.GetBucketSpecificWritePolicy("${bucketName}"),
},
{
Name: "PathBasedAccess",
Description: "Restricts access to a specific path/prefix within a bucket",
Category: "Path-Restricted",
UseCase: "Multi-tenant applications, user-specific directories",
Parameters: []PolicyTemplateParam{
{
Name: "bucketName",
Type: "string",
Description: "Name of the S3 bucket",
Required: true,
Example: "shared-bucket",
},
{
Name: "pathPrefix",
Type: "string",
Description: "Path prefix to restrict access to",
Required: true,
Example: "user123/documents",
},
},
Policy: t.GetPathBasedAccessPolicy("${bucketName}", "${pathPrefix}"),
},
{
Name: "IPRestrictedAccess",
Description: "Allows access only from specific IP addresses or ranges",
Category: "Security",
UseCase: "Corporate networks, office-based access, VPN restrictions",
Parameters: []PolicyTemplateParam{
{
Name: "allowedCIDRs",
Type: "array",
Description: "List of allowed IP addresses or CIDR ranges",
Required: true,
Example: "[\"192.168.1.0/24\", \"10.0.0.0/8\"]",
},
},
Policy: t.GetIPRestrictedPolicy([]string{"${allowedCIDRs}"}),
},
{
Name: "MultipartUploadOnly",
Description: "Allows only multipart upload operations on a specific bucket",
Category: "Upload-Specific",
UseCase: "Large file upload services, streaming applications",
Parameters: []PolicyTemplateParam{
{
Name: "bucketName",
Type: "string",
Description: "Name of the S3 bucket for multipart uploads",
Required: true,
Example: "large-files-bucket",
},
},
Policy: t.GetMultipartUploadPolicy("${bucketName}"),
},
{
Name: "PresignedURLAccess",
Description: "Policy for generating and using presigned URLs",
Category: "Presigned URLs",
UseCase: "Frontend applications, temporary file sharing",
Parameters: []PolicyTemplateParam{
{
Name: "bucketName",
Type: "string",
Description: "Name of the S3 bucket for presigned URL access",
Required: true,
Example: "shared-files-bucket",
},
},
Policy: t.GetPresignedURLPolicy("${bucketName}"),
},
{
Name: "ContentTypeRestricted",
Description: "Restricts uploads to specific content types",
Category: "Content Control",
UseCase: "Image galleries, document repositories, media libraries",
Parameters: []PolicyTemplateParam{
{
Name: "bucketName",
Type: "string",
Description: "Name of the S3 bucket",
Required: true,
Example: "media-bucket",
},
{
Name: "allowedContentTypes",
Type: "array",
Description: "List of allowed MIME content types",
Required: true,
Example: "[\"image/jpeg\", \"image/png\", \"video/mp4\"]",
},
},
Policy: t.GetContentTypeRestrictedPolicy("${bucketName}", []string{"${allowedContentTypes}"}),
},
{
Name: "DenyDeleteAccess",
Description: "Allows all operations except delete (immutable storage)",
Category: "Data Protection",
UseCase: "Compliance storage, audit logs, backup retention",
Policy: t.GetDenyDeletePolicy(),
},
}
}
// GetPolicyTemplateByName returns a specific policy template by name
func (t *S3PolicyTemplates) GetPolicyTemplateByName(name string) *PolicyTemplateDefinition {
templates := t.GetAllPolicyTemplates()
for _, template := range templates {
if template.Name == name {
return &template
}
}
return nil
}
// GetPolicyTemplatesByCategory returns all policy templates in a specific category
func (t *S3PolicyTemplates) GetPolicyTemplatesByCategory(category string) []PolicyTemplateDefinition {
var result []PolicyTemplateDefinition
templates := t.GetAllPolicyTemplates()
for _, template := range templates {
if template.Category == category {
result = append(result, template)
}
}
return result
}
|