{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "CreateLogGroup",
"Effect": "Allow",
"Action": "logs:CreateLogGroup",
"Resource": "arn:aws:logs:us-east-1:<ACCOUNT_ID>:*"
},
{
"Sid": "CreateLogStreamandEvents",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:us-east-1:<ACCOUNT_ID>:log-group:/aws/lambda/*:*"
},
{
"Sid": "AllowS3GetObject",
"Effect": "Allow",
"Action": [
"s3:List*"
],
"Resource": "arn:aws:s3:::shared-logging-<ACCOUNT_ID>-us-east-1-data",
"Condition": {
"StringEquals": {
"s3:prefix": "webadmins"
}
}
},
{
"Sid": "OtherPermissionsNeeded",
"Effect": "Allow",
"Action": [
"dynamodb:PutItem",
"dynamodb:UpdateItem",
"dynamodb:DeleteItem"
],
"Resource": "*",
"Condition": {
"StringEquals": {
"aws:RequestedRegion": "us-east-1"
}
}
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "CreateCustomerManagedPolicies",
"Effect": "Allow",
"Action": [
"iam:CreatePolicy",
"iam:DeletePolicy",
"iam:CreatePolicyVersion",
"iam:DeletePolicyVersion",
"iam:SetDefaultPolicyVersion"
],
"Resource": "arn:aws:iam::<ACCOUNT_ID>:policy/webadmins/???*"
},
{
"Sid": "RoleandPolicyActionswithnoPermissionBoundarySupport",
"Effect": "Allow",
"Action": [
"iam:UpdateRole",
"iam:DeleteRole"
],
"Resource": [
"arn:aws:iam::<ACCOUNT_ID>:role/webadmins/???*"
]
},
{
"Sid": "CreateRoles",
"Effect": "Allow",
"Action": [
"iam:CreateRole",
"iam:AttachRolePolicy",
"iam:DetachRolePolicy"
],
"Resource": [
"arn:aws:iam::<ACCOUNT_ID>:role/webadmins/???*"
],
"Condition": {"StringEquals":
{"iam:PermissionsBoundary": "arn:aws:iam::<ACCOUNT_ID>:policy/webadminspermissionsboundary"}
}
},
{
"Sid": "LambdaFullAccess",
"Effect": "Allow",
"Action": "lambda:*",
"Resource": "arn:aws:lambda:us-east-1:<ACCOUNT_ID>:function:*"
},
{
"Sid": "PassRoletoLambda",
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "arn:aws:iam::<ACCOUNT_ID>:role/webadmins/???*",
"Condition": {
"StringLikeIfExists": {
"iam:PassedToService": "lambda.amazonaws.com"
}
}
},
{
"Sid": "AdditionalPermissionsforLambda",
"Effect": "Allow",
"Action": ["kms:ListAliases", "logs:Describe*", "logs:ListTagsLogGroup", "logs:FilterLogEvents", "logs:GetLogEvents"],
"Resource": "*"
},
{
"Sid": "DenyPermissionBoundaryandPolicyDeleteModify",
"Effect": "Deny",
"Action": [
"iam:CreatePolicyVersion",
"iam:DeletePolicy",
"iam:DeletePolicyVersion",
"iam:SetDefaultPolicyVersion"
],
"Resource": [
"arn:aws:iam::<ACCOUNT_ID>:policy/webadminspermissionsboundary",
"arn:aws:iam::<ACCOUNT_ID>:policy/webadminspermissionpolicy"
]
},
{
"Sid": "DenyRolePermissionBoundaryDelete",
"Effect": "Deny",
"Action": "iam:DeleteRolePermissionsBoundary",
"Resource": "*"
}
]
}
작업을 확인하고 웹 관리자가 제대로 설정되었는지 확인
테스트 계정 하나로 진행하므로 다른 어카운트로 assume하는것은 생략.
aws cli setting
[profile webadmins]
role_arn = arn:aws:iam::YOUR_TEAMS_ACCOUNT_ID:role/webadmins
source_profile = SecAdmin
aws iam create-policy --policy-name webpolicy --policy-document file://verifypolicy.json --profile webadmins —path /webadmins/
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"s3:*"
],
"Resource": "*"
}
]
}
위에서 생성했던 정책에서 ???를 제거하고 로 한부분이 Arn을 가리키는걸로 보임
- 좀더 강한 정책을 두고싶다면, 이 아닌 web이나 *policy 등으로 진행하면 될듯
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
}
아래와같은 명령어를 입력했을때 에러가 발생함.
- assume role에 iam:createRole이 없다고함.
aws iam create-role --role-name NAME_OF_ROLE --path /webadmins/ --assume-role-policy-document file://verifytrustpolicy.json --profile webadmins
앞서 만든 정책을 역할에 적용
aws iam attach-role-policy --policy-arn arn:aws:iam::<accountid>:policy/webadmins/webpolicy --role-name weblambdarole --profile webadmins
파일명 : index.js
getKeys
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.handler = async (event) => {
console.log('Loading function');
const allKeys = [];
await getKeys({ Bucket: 'shared-logging-<accountid>-us-east-1-data' , Prefix: 'test'}, allKeys);
return allKeys;
};
async function getKeys(params, keys){
const response = await s3.listObjectsV2(params).promise();
response.Contents.forEach(obj => keys.push(obj.Key));
if (response.IsTruncated) {
const newParams = Object.assign({}, params);
newParams.ContinuationToken = response.NextContinuationToken;
await getKeys(newParams, keys);
}
}
lambda 생성
aws lambda create-function --function-name verifyfunction --runtime nodejs14.x --role arn:aws:iam::<accountid>:role/webadmins/weblambdarole --handler index.handler --region us-east-1 --zip-file fileb://lambdafunction.zip --profile webadmins
labmda 생성 결과
outfile.txt
{"errorType":"AccessDenied","errorMessage":"Access Denied","trace":["AccessDenied: Access Denied"," at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/services/s3.js:711:35)"," at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)"," at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)"," at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:686:14)"," at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)"," at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)"," at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10"," at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)"," at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:688:12)"," at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:116:18)"]}
webadminspermissionsboundary의 s3 정책 조건을 살펴보자
- prefix 확인가능
["webadmins/you-should-SEE-this-file--webadmins_logging1","webadmins/you-should-SEE-this-file--webadmins_logging2","webadmins/you-should-SEE-this-file--webadmins_logging3","webadmins/you-should-SEE-this-file--webadmins_logging4"]