简体   繁体   English

AWS Boto3-用户无权在资源上执行sts :: AssumeRole吗?

[英]AWS Boto3 - User is not authorized to perform sts::AssumeRole on resource?

I am Creating a URL that Enables Federated Users to Access the AWS Management Console by sts:AssumeRole with below reference by using python Boto3 AWS SDK - 我正在创建一个URL,该链接使sted:AssumeRole可以使联合用户使用python Boto3 AWS开发工具包通过sts:AssumeRole访问具有以下引用的AWS管理控制台-

https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_enable-console-custom-url.html#STSConsoleLink_programPython https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_enable-console-custom-url.html#STSConsoleLink_programPython

I can successfully assume a role via AWS CLI. 我可以通过AWS CLI成功承担角色。 But, I do not understand at all why when I use the Boto library and calling the python script by PHP and shell_exe I receive the following error: 但是,我根本不理解为什么当我使用Boto库并通过PHP和shell_exe调用python脚本时收到以下错误:

botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the AssumeRole operation: User arn:aws:iam::xxx:user/admin is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::xxx/(role-name) botocore.exceptions.ClientError:调用AssumeRole操作时发生错误(AccessDenied):用户arn:aws:iam :: xxx:user / admin无权执行:资源上的sts:AssumeRole:arn:aws:iam :: xxx /(角色名称)

Here admin user has full administrative access, and the same script running successfully and giving login link by CLI :- 此处admin用户具有完全的管理访问权限,并且同一脚本成功运行并通过CLI提供登录链接:-

aws sts assume-role --role-arn arn:aws:iam::123456789012:role/role-name --role-session-name "RoleSession1" aws sts假设角色--role-arn arn:aws:iam :: 123456789012:role / role-name --role-session-name“ RoleSession1”

This is the python sample: 这是python示例:

#!/usr/bin/python35
import urllib, json
import requests 
import boto3 
import urllib.parse
import sys
import cgi
import json

sts_connection = boto3.client('sts',aws_access_key_id='my_access_key',aws_secret_access_key='my_secret_key')
assumed_role_object = sts_connection.assume_role(
    RoleArn=my_role_arn,
    RoleSessionName="AssumeRoleSession"
)    

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 += '}'

request_parameters = "?Action=getSigninToken"
request_parameters += "&SessionDuration=1800"
request_parameters += "&Session=" + urllib.parse.quote(json_string_with_temp_credentials)
request_url = "https://signin.aws.amazon.com/federation" + request_parameters
r = requests.get(request_url)

signin_token = json.loads(r.text)


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

print (request_url)

Problem with calling the same script by webserver using PHP shell_exec as follows -: 通过网络服务器使用PHP shell_exec调用同一脚本的问题如下:

$output = shell_exec("python3 get_link.py arn=".$arn." 2>&1");

I do not think that the problem is with IAM roles and policies; 我认为问题不在于IAM的角色和政策。 if it was, AWS CLI would not connect as well. 如果是的话,AWS CLI也将无法连接。

Any suggestion? 有什么建议吗?

Finally I found the solution, I have updated the connection object as follows and it works form me - 最终,我找到了解决方案,并按如下所示更新了连接对象,该连接对象对我有效:

sts_connection = boto3.client('sts')
assumed_role_object = 
sts_connection.assume_role(RoleArn=my_role_arn,RoleSessionName=str(rolid))

We need to pass "sts" in boto3 client object and in sts connection object need to pass your role arn and roleId for you want to create Federated login link. 我们需要在boto3客户端对象中传递“ sts” ,而在sts连接对象中则需要传递您的角色arn和roleId才能创建联合登录链接。

I also faced the same issue which is mentioned above and i tried the above mentioned suggestions as well but no luck. 我也遇到了与上述相同的问题,我也尝试了上述建议,但是没有运气。 Then i realized that the AWS account which i used for testing is not authorized. 然后,我意识到用于测试的AWS账户未获得授权。 So i added the below in the Trusted Relationship under role summary page in AWS console to authenticate the AWS account. 因此,我在AWS控制台的“角色摘要”下的“可信关系下”页面中添加了以下内容,以对AWS账户进行身份验证。

{
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::123456789012:root"
      },
      "Action": "sts:AssumeRole"
}

Its started working. 它开始工作了。 I believe it may helpful to new guys to AWS like me. 我相信这可能对像我这样的AWS新手有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS AssumeRole - 用户无权对资源执行:sts:AssumeRole - AWS AssumeRole - User is not authorized to perform: sts:AssumeRole on resource AWS cli:无权执行:sts:AssumeRole 对资源 - AWS cli: not authorized to perform: sts:AssumeRole on resource 无权执行:资源上的 sts:AssumeRole - Not authorized to perform: sts:AssumeRole on resource AWS Amplify - CognitoIdentityCredentials 无权执行:sts:AssumeRole on resource - AWS Amplify - CognitoIdentityCredentials is not authorized to perform: sts:AssumeRole on resource AWS boto3 用户:arn:aws:iam::xxxx:root 无权执行:lambda:AddLayerVersionPermission 对资源 - AWS boto3 User: arn:aws:iam::xxxx:root is not authorized to perform: lambda:AddLayerVersionPermission on resource 用户:arn:aws:sts::{account_id}:assumed-role/* 无权执行:sts:AssumeRole on resource:arn:aws:iam::{account_id}:role/* - User: arn:aws:sts::{account_id}:assumed-role/* is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::{account_id}:role/* CodeBuild 无权执行: sts:AssumeRole on arn:aws:iam/ … InvalidInputException; 为什么? - CodeBuild is not authorized to perform: sts:AssumeRole on arn:aws:iam/ … InvalidInputException; why? EKS 无法使用 Kubectl 向 Kubernetes 进行身份验证 - “用户:无权执行:sts:AssumeRole” - EKS not able to authenticate to Kubernetes with Kubectl - “User: is not authorized to perform: sts:AssumeRole” AWSSecurityTokenServiceException:加入被拒绝。 用户无权执行 sts:AssumeRole - AWSSecurityTokenServiceException: Acced denied. User is not authorized to perform sts:AssumeRole Lambda function 无权执行 sts:AssumeRole - Lambda function is not authorized to perform sts:AssumeRole
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM