要對S3的訪問權限作控制,既能夠使用基於身份的策略(IAM用戶策略),也能夠使用基於資源的策略(ACL和存儲桶策略)。html
訪問一個存儲桶的權限控制流程如圖所示:
json
訪問存儲桶中的對象的權限控制流程如圖所示:
瀏覽器
當 Amazon S3 收到對象操做請求時,它會將基於資源的全部相關權限(對象訪問控制列表 (ACL)、存儲桶策略、存儲桶 ACL)和IAM用戶策略轉換爲將在運行時進行評估的策略集。而後它會經過一系列步驟評估生成的策略集。在每一個步驟中,它會在三個特定上下文 (用戶上下文、存儲桶上下文和對象上下文) 中評估一個策略子集。
簡單來講就是先判斷用戶權限,而後判斷存儲桶權限,最後判斷對象權限。安全
目前咱們都是經過同一個AWS帳戶下的多個IAM用戶去訪問S3的,所以能夠只使用IAM用戶策略去作訪問權限控制,這也基本能知足絕大部分常規的權限控制需求,若是沒法知足的狀況再考慮使用存儲桶處理和ACL。ide
Harp
)登陸AWS管理控制檯,選擇IAM服務進入,建立一個名爲Administrators
的組,並向其附加AdministratorAccess
權限;Harp-Admin
),並將其添加到Administrators
組;Harp-Admin
,點擊安全憑證;
Harp-Admin
已經開啓了MFA驗證,使用該用戶登陸控制檯時,須要在驗證密碼以後,再驗證MFA(即輸入Authy中的實時CODE);
Harp-Admin
來處理的;
s3_common_policy
;{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListAllMyBuckets", "s3:GetBucketLocation" ], "Resource": "arn:aws:s3:::*" }, { "Effect": "Allow", "Action": [ "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::derek-public*" ] }, { "Sid": "AllowRootLevelListingOfTheBucket", "Action": [ "s3:ListBucket" ], "Effect": "Allow", "Resource": [ "arn:aws:s3:::derek-bucket3" ], "Condition": { "StringEquals": { "s3:prefix": [ "" ], "s3:delimiter": [ "/" ] } } }, { "Sid": "AllowListBucketOfASpecificUserPrefix", "Action": [ "s3:ListBucket" ], "Effect": "Allow", "Resource": [ "arn:aws:s3:::derek-bucket3" ], "Condition": { "StringLike": { "s3:prefix": [ "public/*" ] } } }, { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl", "s3:GetObject", "s3:GetObjectAcl", "s3:DeleteObject" ], "Resource": [ "arn:aws:s3:::derek-public1/*", "arn:aws:s3:::derek-bucket3/public/*" ] }, { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": "arn:aws:s3:::derek-public2/*" } ] }
解釋一下各條statements:
statement1: 對全部的S3資源賦予s3:ListAllMyBuckets和s3:GetBucketLocation,這兩個權限是給控制檯使用的,經過控制檯訪問S3時,須要這兩個權限來正常列出全部存儲桶,若是經過CLI訪問則不須要;
statement2: 賦予全部derek-public開頭的存儲桶列出存儲桶對象的權限(s3:ListBucket);
statement3: 容許列出derek-bucket3第一層路徑下的對象,這個寫法能夠在S3的官方文檔中找到;
statement4: 容許列出derek-bucket3/public下的全部對象;
statement5: 容許對derek-public1和derek-bucket3/public中的對象讀寫、複製操做;
statement6: 容許對derek-public2中的對象讀取操做;測試
s3_common_group
,並將全部用戶添加到其中,將s3_common_policy
附加到這個組;derek-public1
、derek-public2
和derek-bucket3/public
,其中derek-public2
只有可讀權限,若是用戶嘗試寫入,例如建立一個文件夾,會顯示「沒法使用名稱XXX建立文件夾」;
derek-bucket1
或derek-bucket3/user1
,會顯示「Access Denied」;
user1_policy
;{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::derek-bucket1", "arn:aws:s3:::derek-bucket2" ] }, { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl", "s3:GetObject", "s3:GetObjectAcl", "s3:DeleteObject" ], "Resource": [ "arn:aws:s3:::derek-bucket1/*", "arn:aws:s3:::derek-bucket3/user1/*" ] }, { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": "arn:aws:s3:::derek-bucket2" }, { "Sid": "AllowListBucketOfASpecificUserPrefix", "Action": [ "s3:ListBucket" ], "Effect": "Allow", "Resource": [ "arn:aws:s3:::derek-bucket3" ], "Condition": { "StringLike": { "s3:prefix": [ "user1/*" ] } } } ] }
解釋一下各條statements:
statement1: 容許列出derek-bucket1和derek-bucket2中的全部對象;
statement2: 容許對derek-bucket1和derek-bucket3/user1中的對象讀寫、複製操做;
statement3: 容許對derek-bucket2中的對象讀取操做;
statement4: 容許列出derek-bucket3/user1下的全部對象;ui
user1_policy
,審覈,添加權限;user2_policy
;{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::derek-bucket2" ] }, { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl", "s3:GetObject", "s3:GetObjectAcl", "s3:DeleteObject" ], "Resource": [ "arn:aws:s3:::derek-bucket2/*", "arn:aws:s3:::derek-bucket3/user2/*" ] }, { "Sid": "AllowListBucketOfASpecificUserPrefix", "Action": [ "s3:ListBucket" ], "Effect": "Allow", "Resource": [ "arn:aws:s3:::derek-bucket3" ], "Condition": { "StringLike": { "s3:prefix": [ "user2/*" ] } } } ] }
解釋一下各條statements:
statement1: 容許列出derek-bucket2
中的全部對象;
statement2: 容許對derek-bucket2
和derek-bucket3/user2
中的對象讀寫、複製操做;
statement3: 容許列出derek-bucket3/user2
下的全部對象;spa
user3_policy
;{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::derek-bucket3" ] }, { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": "arn:aws:s3:::derek-bucket3" } ] }
Harp-Admin
爲user4建立虛擬MFA;user4_policy
;{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::derek-bucket4" ] }, { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl", "s3:GetObject", "s3:GetObjectAcl", "s3:DeleteObject" ], "Resource": [ "arn:aws:s3:::derek-bucket4/*" ] }, { "Effect": "Deny", "Action": [ "s3:PutObject", "s3:PutObjectAcl", "s3:GetObjectAcl", "s3:DeleteObject" ], "Resource": [ "arn:aws:s3:::derek-bucket4/mfa/*" ], "Condition": { "BoolIfExists": { "aws:MultiFactorAuthPresent": false } } }, { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": [ "arn:aws:s3:::derek-bucket4/mfa/*" ] } ] }
解釋一下各條statements:
statement1: 容許列出derek-bucket4中的全部對象;
statement2: 容許對derek-bucket4中的對象讀寫、複製操做;
statement3: 未經過MFA驗證時禁止寫derek-bucket4/mfa下的全部對象;
statement4: 容許讀取derek-bucket4/mfa下的全部對象;插件
MFA ARN
和MFA當前的6位token,會返回一個json格式的臨時憑證,該憑證默認有效期12h,更多關於CLI和MFA的內容可參考 如何使用 MFA 令牌對經過 AWS CLI 進行的 AWS 資源訪問執行身份驗證?:
mfa-user
,將上一步的結果複製到這裏;
mfa-user
複製文件到mfa文件夾,提示覆製成功;
在使用user1時,在derek-bucket1
中創建文件夾沒有問題,可是上傳文件卻失敗,提示OptionsRequestDenied
,經谷歌查詢猜想可能和瀏覽器的AdBlock相關插件有關,因而換火狐瀏覽器、或者使用CLI上傳,都沒有問題。code