gpt4 book ai didi

python - 无法通过 lambda 函数承担角色 - Python

转载 作者:行者123 更新时间:2023-12-03 20:17:34 24 4
gpt4 key购买 nike

您好,我从昨天开始就遇到了这个奇怪的问题。当我尝试在 pycharm 上手动运行它并打印 request_url 时,我有一个 python 模块 web_token.py,它完全可以正常工作并输出 requested_url。但是,当我将我的 web_token.py 和 fetch_accounts.py 压缩在一起并将其上传到 lambda 函数时,它给了我以下错误 -

An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:sts::5398XXXXXXX:assumed-role/sandbox-amp_sandbox-dev/sandbox-dev-amp_sandbox is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::4540XXXXXXXX:role/AMPSandbox

我什至尝试给它 FullAdministrativeAccess 但它似乎仍然有效。虽然我能够在 pycharm 中以 stanalone 模式运行 web_token.py 时生成 requested_url。有人可以提供一些指导,将不胜感激。

代码片段来自

retrieve_accounts.py

import boto3

import web_token


def get_account(event, context):
client = boto3.client('dynamodb')
NameID = "test@orgz.com"
ManagerEmail = "test1@orgaz.com"
response = client.scan(
TableName='Sandbox-Users',
ScanFilter={
'NameID': {
'AttributeValueList': [
{
'S': NameID,
},
],
'ComparisonOperator': 'EQ'
}
}
)
return web_token.request_url

web_token.py

import httplib
import urllib, json
import boto3

client = boto3.client('sts')
assumed_role_object = client.assume_role(
RoleArn="arn:aws:iam::4540XXXXXXXX:role/AMPSandboxRole",
RoleSessionName="AssumeRoleSession"
)

# Step 3: Format resulting temporary credentials into JSON
json_string_with_temp_credentials = '{'
json_string_with_temp_credentials += '"sessionId":"' + assumed_role_object.get("Credentials").get("AccessKeyId") + '",'
json_string_with_temp_credentials += '"sessionKey":"' + assumed_role_object.get("Credentials").get("SecretAccessKey") + '",'
json_string_with_temp_credentials += '"sessionToken":"' + assumed_role_object.get("Credentials").get("SessionToken") + '"'
json_string_with_temp_credentials += '}'

# Step 4. Make request to AWS federation endpoint to get sign-in token. Construct the parameter string with
# the sign-in action request, a 12-hour session duration, and the JSON document with temporary credentials
# as parameters.
request_parameters = "?Action=getSigninToken"
request_parameters += "&SessionDuration=43200"
request_parameters += "&Session=" + urllib.quote_plus(json_string_with_temp_credentials)
request_url = "/federation" + request_parameters

conn = httplib.HTTPSConnection("signin.aws.amazon.com")
conn.request("GET", request_url)
r = conn.getresponse()
# Returns a JSON document with a single element named SigninToken.
signin_token = json.loads(r.read())

request_parameters = "?Action=login"
request_parameters += "&Issuer=sandbox.com"
request_parameters += "&Destination=" + urllib.quote_plus("https://console.aws.amazon.com/")
request_parameters += "&SigninToken=" + signin_token["SigninToken"]
request_url = "https://signin.aws.amazon.com/federation" + request_parameters

更新:我有两个策略附加到 sandbox-amp_sandbox-dev 角色 -

InfraLoggingPolicy[in 5398XXXXXXX]

    {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sqs:SendMessage",
"sqs:SendMessageBatch"
],
"Resource": "arn:aws:sqs:*:131703196249:org-logging-prod",
"Effect": "Allow"
},
{
"Action": [
"ec2:DescribeInstances",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::em-log-intake-us-east-1-prod/*",
"arn:aws:s3:::em-log-intake-us-west-2-prod/*"
]
}
]
}

sandbox-amp_sandbox-policy-dev[在 5398XXXXXXX]

    {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Action": "dynamodb:*",
"Resource": "arn:aws:dynamodb:*:*:*"
}
]
}

更新 2.0以上政策来 self 的账号5398XXXXXXX。我在 4540XXXXXXXX 帐户 AMPSandboxRole 中有以下角色,我在该角色下有以下政策

AssumeRole[在 4540XXXXXXXX]

    {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Resource": "arn:aws:iam::*:role/AMPSandboxRole",
"Action": "sts:AssumeRole"
}
]
}

[4540XXXXXXXX] 中的组织访问权限

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"organizations:Describe*",
"organizations:List*",
"organizations:CreateAccount",
"organizations:MoveAccount"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"organizations:MoveAccount"
],
"Resource": "arn:aws:organizations::454084028794:root/o-eyec2h6qr0/r-ekzh"
},
{
"Effect": "Allow",
"Action": [
"organizations:*"
],
"Resource": "arn:aws:organizations::45xxxxxxxxxx:ou/o-eyec2h6qr0/ou-ekzh-x2xcsupl"
}
]
}

更新 3.045xxxxxxxxxx中的信任关系

 {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::53xxxxxxxxxx:root"
},
"Action": "sts:AssumeRole"
}
]
}

最佳答案

错误说:

User: arn:aws:sts::5398XXXXXXX:assumed-role/sandbox-amp_sandbox-dev/sandbox-dev-amp_sandbox is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::4540XXXXXXXX:role/AMPSandbox

AWS Lambda 函数正在您上面列出的角色下执行。它只有调用 dynamodb:* 的权限。 它还需要调用 AssumeRole 的权限。

您的政策应更新为:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PermitDynamoDB",
"Action": "dynamodb:*",
"Effect": "Allow",
"Resource": "*"
},
{
"Sid": "PermitAssumeRole",
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Resource": "arn:aws:iam::4540XXXXXXXX:role/AMPSandboxRole"
}
]
}

关于python - 无法通过 lambda 函数承担角色 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46135413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com