policies.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. def read_write(bucket, prefix="*", extra_statements=None):
  2. statements = read_write_statements(bucket, prefix=prefix)
  3. if extra_statements:
  4. statements.extend(extra_statements)
  5. return wrap_policy(statements)
  6. def read_write_statements(bucket, prefix="*"):
  7. # https://github.com/simonw/s3-credentials/issues/24
  8. if not prefix.endswith("*"):
  9. prefix += "*"
  10. return read_only_statements(bucket, prefix) + [
  11. {
  12. "Effect": "Allow",
  13. "Action": ["s3:PutObject", "s3:DeleteObject"],
  14. "Resource": ["arn:aws:s3:::{}/{}".format(bucket, prefix)],
  15. }
  16. ]
  17. def read_only(bucket, prefix="*", extra_statements=None):
  18. statements = read_only_statements(bucket, prefix=prefix)
  19. if extra_statements:
  20. statements.extend(extra_statements)
  21. return wrap_policy(statements)
  22. def read_only_statements(bucket, prefix="*"):
  23. # https://github.com/simonw/s3-credentials/issues/23
  24. statements = []
  25. if not prefix.endswith("*"):
  26. prefix += "*"
  27. if prefix != "*":
  28. statements.append(
  29. {
  30. "Effect": "Allow",
  31. "Action": ["s3:GetBucketLocation"],
  32. "Resource": ["arn:aws:s3:::{}".format(bucket)],
  33. }
  34. )
  35. statements.append(
  36. {
  37. "Effect": "Allow",
  38. "Action": ["s3:ListBucket"],
  39. "Resource": ["arn:aws:s3:::{}".format(bucket)],
  40. "Condition": {
  41. "StringLike": {
  42. # Note that prefix must end in / if user wants to limit to a folder
  43. "s3:prefix": [prefix]
  44. }
  45. },
  46. }
  47. )
  48. else:
  49. # We can combine s3:GetBucketLocation and s3:ListBucket into one
  50. statements.append(
  51. {
  52. "Effect": "Allow",
  53. "Action": ["s3:ListBucket", "s3:GetBucketLocation"],
  54. "Resource": ["arn:aws:s3:::{}".format(bucket)],
  55. }
  56. )
  57. return statements + [
  58. {
  59. "Effect": "Allow",
  60. "Action": [
  61. "s3:GetObject",
  62. "s3:GetObjectAcl",
  63. "s3:GetObjectLegalHold",
  64. "s3:GetObjectRetention",
  65. "s3:GetObjectTagging",
  66. ],
  67. "Resource": ["arn:aws:s3:::{}/{}".format(bucket, prefix)],
  68. },
  69. ]
  70. def write_only(bucket, prefix="*", extra_statements=None):
  71. statements = write_only_statements(bucket, prefix=prefix)
  72. if extra_statements:
  73. statements.extend(extra_statements)
  74. return wrap_policy(statements)
  75. def write_only_statements(bucket, prefix="*"):
  76. # https://github.com/simonw/s3-credentials/issues/25
  77. if not prefix.endswith("*"):
  78. prefix += "*"
  79. return [
  80. {
  81. "Effect": "Allow",
  82. "Action": ["s3:PutObject"],
  83. "Resource": ["arn:aws:s3:::{}/{}".format(bucket, prefix)],
  84. }
  85. ]
  86. def wrap_policy(statements):
  87. return {"Version": "2012-10-17", "Statement": statements}
  88. def bucket_policy_allow_all_get(bucket):
  89. return {
  90. "Version": "2012-10-17",
  91. "Statement": [
  92. {
  93. "Sid": "AllowAllGetObject",
  94. "Effect": "Allow",
  95. "Principal": "*",
  96. "Action": ["s3:GetObject"],
  97. "Resource": ["arn:aws:s3:::{}/*".format(bucket)],
  98. }
  99. ],
  100. }