【问题标题】:How can I query UserId for AWS SSO Users using Boto3如何使用 Boto3 查询 AWS SSO 用户的 UserId
【发布时间】:2022-12-15 07:18:59
【问题描述】:

如何使用 Boto3 为 AWS SSO 用户获取 UserId

我想使用它为使用以下代码的特定 aws 帐户的用户分配权限,但是,这需要 PrincipalId 这是与每个用户关联的一些 16-20 位数字,在 AWS 控制台中称为 User ID

你可以阅读它 - here

response = client.create_account_assignment(
    InstanceArn='string',
    TargetId='string',
    TargetType='AWS_ACCOUNT',
    PermissionSetArn='string',
    PrincipalType='USER'|'GROUP',
    PrincipalId='string'
)

【问题讨论】:

    标签: python amazon-web-services boto3 botocore aws-sso


    【解决方案1】:

    如果您有要为其分配权限的用户的 UserName,则可以 programmatically use IAM 确定该用户的 UserId:

    import boto3
    
    # Get the UserId.
    user_name = 'the user name here'
    iam_client = boto3.client('iam')
    result = iam_client.get_user(UserName=user_name)
    user_id = result['User']['UserId']
    
    # Assign permissions to the UserId.
    sso_admin_client = boto3.client('sso-admin')
    response = sso_admin_client.create_account_assignment(
        InstanceArn='string',
        TargetId='string',
        TargetType='AWS_ACCOUNT',
        PermissionSetArn='string',
        PrincipalType='USER',
        PrincipalId=user_id
    )
    

    【讨论】:

    • sso 用户不是 IAM 用户
    【解决方案2】:

    您还需要使用“identitystore”来获取用户或组 ID。从文档中尝试这个 -

    import boto3
    
    client = boto3.client('identitystore')
    
    response = client.get_user_id(
        IdentityStoreId='string',
        AlternateIdentifier={
            'ExternalId': {
                'Issuer': 'string',
                'Id': 'string'
            },
            'UniqueAttribute': {
                'AttributePath': 'string',
                'AttributeValue': {...}|[...]|123|123.4|'string'|True|None
            }
        }
    )
    

    虽然我个人发现上述方法对我不起作用,因为它在我安装的 Boto3 版本中不可用,所以我改为这样做,效果很好 -

    import boto3
    
    client = boto3.client('identitystore')
    
    response = client.list_users(
        IdentityStoreId='string',
        Filters=[
            {
                'AttributePath': 'UserName',
                'AttributeValue': 'string'
            },
        ]
    )
    
    print(response["Users"][0]["UserId"])
    

    资料来源:

    【讨论】:

      猜你喜欢
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 2021-09-14
      • 1970-01-01
      相关资源
      最近更新 更多