Skip to content

Commit 664862f

Browse files
authored
build: update S3 policy configuration (#2022)
1 parent 83d48b7 commit 664862f

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

app/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@
291291
S3_BUCKET_REGION = os.getenv("S3_BUCKET_REGION", "set-s3-bucket-region")
292292
S3_ACCESS_KEY = os.getenv("S3_ACCESS_KEY", "set-s3-access-key")
293293
S3_SECRET_KEY = os.getenv("S3_SECRET_KEY", "set-s3-secret-key")
294+
S3_USER_ID = os.getenv("S3_USER_ID", "set-s3-user-id")
294295

295296
QUESTION_FOLDER_NAME = "questions"
296297
QUIZ_FOLDER_NAME = "quizs"

core/utils/s3.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
"""
2+
boto3 doc
3+
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/collections.html
4+
5+
bucket = s3.get_bucket()
6+
s3.get_bucket_cors(bucket)
7+
s3.get_bucket_policy(bucket)
8+
"""
9+
110
import json
211

312
import boto3
413
from botocore.client import Config
514
from django.conf import settings
615

716

8-
# boto3 doc
9-
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/collections.html
10-
11-
1217
API_CONNECTION_DICT = {
1318
"endpoint_url": settings.S3_ENDPOINT,
1419
"aws_access_key_id": settings.S3_ACCESS_KEY,
@@ -32,7 +37,7 @@
3237
}
3338

3439
DEFAULT_POLICY_CONFIGURATION = {
35-
"Version": "2012-10-17",
40+
"Version": "2023-04-17",
3641
"Statement": [
3742
{
3843
"Sid": "AllowPublicRead",
@@ -42,18 +47,11 @@
4247
"Resource": f"{settings.S3_BUCKET_NAME}/*",
4348
},
4449
{
45-
"Sid": "DenyPublicUpdate",
46-
"Effect": "Deny",
47-
"Principal": "*",
48-
"Action": "s3:PutObject",
49-
"Resource": f"{settings.S3_BUCKET_NAME}/*",
50-
},
51-
{
52-
"Sid": "DenyPublicDelete",
53-
"Effect": "Deny",
54-
"Principal": "*",
55-
"Action": "s3:DeleteObject",
56-
"Resource": f"{settings.S3_BUCKET_NAME}/*",
50+
"Sid": "AllowPrivateReadAndUpdate",
51+
"Effect": "Allow",
52+
"Principal": {"SCW": f"user_id:{settings.S3_USER_ID}"},
53+
"Action": "*",
54+
"Resource": [f"{settings.S3_BUCKET_NAME}", f"{settings.S3_BUCKET_NAME}/*"],
5755
},
5856
],
5957
}

0 commit comments

Comments
 (0)