이번 블로그에서는 S3 Bucket — Cross Account Access 실습을 해보도록 하겠습니다. 실습내용은 아래 AWS 문서를 참고하였습니다.
글을 읽는 것만으로는 정확히 이해가 되지 않는 경우가 있습니다. 따라서 직접 실습을 하면 내용을 파악하는 데 도움이 많이 되는 것 같습니다.
A Account에 Admin 유저 생성
A Account에 Admin 유저를 생성합니다. Admin 유저명은 AccountAAdmin 입니다. AWS CLI로 접속을 해야 하기 때문에 Programmatic access도 체크했습니다.
AWS Managed Policy 인 AdministratorAccess Permission을 적용합니다.
B Account에 테스트 유저 생성
계정을 하나 더 생성하여 AccountBAdmin IAM 유저를 생성합니다.
AWS CLI 에 Security Credentials 정보 입력
aws configure 명령어를 통해 profile 별로 Access Key정보를 입력해줍니다.
MackBook-Pro:~ redwood$ aws configure --profile AccountAAdminAWS Access Key ID [None]: *****AWS Secret Access Key [None]: *********
Default region name [None]: ap-northeast-2
Default output format [None]:MackBook-Pro:~ redwood$ aws configure --profile AccountBAdmin
AWS Access Key ID [None]: *****
AWS Secret Access Key [None]: **********
Default region name [None]: ap-northeast-2
Default output format [None]:
MackBook-Pro:~ redwood$
~/.aws/ 디렉토리 아래에 config, credentials 파일이 생기고 Access Key 정보는 credentials 파일에, Region 정보는 config 파일에 저장이 됩니다.
Account A 에서 작업 수행
AccountAAdmin 사용자로 접속하여 Bucket을 생성합니다. Bucket 명을 testbucket-1630하고 나머지 값은 모두 default 값을 사용했습니다.
test.txt 파일을 업로드합니다.
Bucket Policy 생성하기
AccountAAdmin이 생성한 testbucket-1630 bucket에 아래와 같이 bucket policy를 적용합니다.
아래 bucket policy의 의미는 Account B의 root 계정은 Account A에 생성된 testbucket-1630 에 대해서 s3:ListBucket, s3:GetBucketLocation API를 사용할 수 있게 허용하는 것입니다.
{"Version": "2012-10-17",
"Statement": [
{
"Sid": "Example permissions",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::6775990419456:root"
},
"Action": [
"s3:GetBucketLocation",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::testbucket-1630"
]
}
]
}
특이한 점은 Bucket Policy를 AccountBAdmin IAM유저에 적용하지 않고 Root Account에 적용한다는 점입니다.
AccountBAdmin 사용자로 s3:ListBucket, s3:GetBucketLocation API가 호출되는지 테스트해보겠습니다.
MackBook-Pro:~ redwood$ aws s3 ls s3://testbucket-1630 --profile AccountBAdmin2019-02-07 22:28:30 26 test.txtMackBook-Pro:~ redwood$ aws s3api get-bucket-location --bucket testbucket-1630 --profile AccountBAdmin
{
"LocationConstraint": "ap-northeast-2"
}
MackBook-Pro:~ redwood$
Account B에서 Dave 사용자 생성
Account B 계정에 Dave 사용자를 생성합니다. AWS CLI 에서 접속해야 하므로 Programmatic access를 체크합니다.
AWS CLI 에 Dave에 대한 Access Key를 등록합니다.
MackBook-Pro:~ redwood$ aws configure --profile UserDaveAWS Access Key ID [None]: *****
AWS Secret Access Key [None]: *********
Default region name [None]: ap-northeast-2
Default output format [None]:
MackBook-Pro:~ redwood$
사용자 Dave에 권한 위임
AccountBAdmin 사용자 자격 증명을 사용하여 콘솔에 로그인한 후 다음 정책을 사용하여 사용자 Dave에 대한 인라인 정책을 만듭니다. AccountAAdmin가 AccountBAdmin 에게 위임한 권한을 Dave에게 위임한 것입니다.
{s3:ListBucket만 허용하였고, s3:GetBucketLocation는 제외가 되었습니다.
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Example",
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::testbucket-1630"
]
}
]
}
Users에서 Dave를 선택하고 Add inline policy를 클릭합니다.
Dave 유저로 테스트한 결과입니다.
s3 ls 명령어 성공하였지만, get-bucket-location은 에러가 발생하였습니다.
2019-02-07 22:28:30 26 test.txtMackBook-Pro:~ redwood$
MackBook-Pro:~ redwood$ aws s3api get-bucket-location --bucket testbucket-1630 --profile UserDave
An error occurred (AccessDenied) when calling the GetBucketLocation operation: Access Denied
정리하면
AWS 문서에 있는 그림이 잘 이해가 안 돼서 지금까지 테스트한 내용을 제가 이해한 내용으로 약간 변경해서 다시 그렸습니다.
AccountAAdmin이 만든 Bucket과 Bucket Policy를 통해 AccountBAdmin에게 bucket에 대한 API 호출 권한을 위임하였고, 권한을 위임받은 AccountBAdmin는 IAM Policy를 통해 Dave 유저에 bucket에 대한 권한을 다시 위임하였습니다. 결과적으로 Dave 유저가 AccountAAdmin이 만든 Bucket에 대해서 ListBucket을 하게끔 설정하였습니다.