Skip to content

Commit cee84f9

Browse files
authored
Merge pull request #1421 from marsteg/ADD_listacl_listpolicy
Add getacl getpolicy
2 parents d427537 + 2bfb73f commit cee84f9

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

s3cmd

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,32 @@ def cmd_sync(args):
21712171
return cmd_sync_remote2remote(args)
21722172
raise ParameterError("Invalid source/destination: '%s'" % "' '".join(args))
21732173

2174+
def cmd_getacl(args):
2175+
cfg = Config()
2176+
s3 = S3(cfg)
2177+
uri = S3Uri(args[0])
2178+
2179+
if uri.type != "s3" or not uri.has_bucket():
2180+
raise ParameterError("Expecting S3 URI instead of '%s'" % args[0])
2181+
2182+
acl = s3.get_acl(uri)
2183+
acl_grant_list = acl.getGrantList()
2184+
owner = acl.getOwner()
2185+
if uri.has_object():
2186+
uri_type = "(object)"
2187+
else:
2188+
uri_type = "(bucket)"
2189+
output(u"%s %s:" % (uri, uri_type))
2190+
if not owner['nick']:
2191+
output(u" Owner: %s" % (owner['id']))
2192+
else:
2193+
output(u" Owner: %s (Nick: %s)" % (owner['id'], owner['nick']))
2194+
for grant in acl_grant_list:
2195+
output(u" ACL: %s: %s" % (grant['grantee'], grant['permission']))
2196+
if acl.isAnonRead():
2197+
output(u" URL: %s" % uri.public_url())
2198+
return EX_OK
2199+
21742200
def cmd_setacl(args):
21752201
cfg = Config()
21762202
s3 = S3(cfg)
@@ -2346,6 +2372,32 @@ def cmd_setpolicy(args):
23462372
output(u"%s: Policy updated" % uri)
23472373
return EX_OK
23482374

2375+
def cmd_getpolicy(args):
2376+
cfg = Config()
2377+
s3 = S3(cfg)
2378+
uri = S3Uri(args[0])
2379+
2380+
if uri.type != "s3" or not uri.has_bucket():
2381+
raise ParameterError("Expecting S3 URI instead of '%s'" % args[0])
2382+
2383+
try:
2384+
policy = s3.get_policy(uri)
2385+
output(u" Policy: %s" % policy)
2386+
except S3Error as exc:
2387+
# Ignore the exception and don't fail the info
2388+
# if the server doesn't support setting ACLs
2389+
if exc.status == 403:
2390+
output(u" Policy: Not available: GetPolicy permission is needed to read the policy")
2391+
return EX_ACCESSDENIED
2392+
elif exc.status == 405:
2393+
output(u" Policy: Not available: Only the bucket owner can read the policy")
2394+
return EX_ACCESSDENIED
2395+
elif exc.status not in [404, 501]:
2396+
raise exc
2397+
else:
2398+
output(u" Policy: none")
2399+
return EX_OK
2400+
23492401
def cmd_delpolicy(args):
23502402
cfg = Config()
23512403
s3 = S3(cfg)
@@ -2980,6 +3032,7 @@ def get_commands_list():
29803032
{"cmd":"cp", "label":"Copy object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_cp, "argc":2},
29813033
{"cmd":"modify", "label":"Modify object metadata", "param":"s3://BUCKET1/OBJECT", "func":cmd_modify, "argc":1},
29823034
{"cmd":"mv", "label":"Move object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_mv, "argc":2},
3035+
{"cmd":"getacl", "label":"List Access control list for Bucket or Files", "param":"s3://BUCKET[/OBJECT]", "func":cmd_getacl, "argc":1},
29833036
{"cmd":"setacl", "label":"Modify Access control list for Bucket or Files", "param":"s3://BUCKET[/OBJECT]", "func":cmd_setacl, "argc":1},
29843037
{"cmd":"setversioning", "label":"Modify Bucket Versioning", "param":"s3://BUCKET enable|disable", "func":cmd_setversioning, "argc":2},
29853038
{"cmd":"setownership", "label":"Modify Bucket Object Ownership", "param":"s3://BUCKET BucketOwnerPreferred|BucketOwnerEnforced|ObjectWriter", "func":cmd_setownership, "argc":2},
@@ -2989,6 +3042,7 @@ def get_commands_list():
29893042
{"cmd":"setobjectretention", "label":"Modify Object Retention", "param":"MODE RETAIN_UNTIL_DATE s3://BUCKET/OBJECT", "func":cmd_setobjectretention, "argc":3},
29903043

29913044
{"cmd":"setpolicy", "label":"Modify Bucket Policy", "param":"FILE s3://BUCKET", "func":cmd_setpolicy, "argc":2},
3045+
{"cmd":"getpolicy", "label":"Get Bucket Policy", "param":"FILE s3://BUCKET", "func":cmd_getpolicy, "argc":1},
29923046
{"cmd":"delpolicy", "label":"Delete Bucket Policy", "param":"s3://BUCKET", "func":cmd_delpolicy, "argc":1},
29933047
{"cmd":"setcors", "label":"Modify Bucket CORS", "param":"FILE s3://BUCKET", "func":cmd_setcors, "argc":2},
29943048
{"cmd":"delcors", "label":"Delete Bucket CORS", "param":"s3://BUCKET", "func":cmd_delcors, "argc":1},
@@ -3041,7 +3095,6 @@ def format_commands(progname, commands_list):
30413095
help += " %s\n %s %s %s\n" % (cmd["label"], progname, cmd["cmd"], cmd["param"])
30423096
return help
30433097

3044-
30453098
def update_acl(s3, uri, seq_label=""):
30463099
cfg = Config()
30473100
something_changed = False

0 commit comments

Comments
 (0)