AWSGoV2:使用临时凭证从另一个账户链式更新身份
创始人
2024-09-25 19:01:58
0

在 Go 中,通过使用 AWS SDK for Go V2 可以轻松地从一个 AWS 账户中的 IAM 角色链式更新身份到另一个 AWS 账户中的 IAM 角色。

以下是实现从源 AWS 账户到目标 AWS 账户的 IAM 角色更新身份的代码示例:

package main

import (
        "context"
        "fmt"

        "github.com/aws/aws-sdk-go-v2/config"
        "github.com/aws/aws-sdk-go-v2/credentials/stscreds"
        "github.com/aws/aws-sdk-go-v2/service/s3"
)

func main() {
        cfg, err := config.LoadDefaultConfig(context.Background())
        if err != nil {
                panic("unable to load SDK config, " + err.Error())
        }

        // Create a STS credentials object with the source account's role ARN
        srcCreds := stscreds.NewAssumeRoleProvider(stscreds.AssumeRoleOptions{
                RoleArn: "arn:aws:iam::123456789012:role/SourceAccountRole",
                RoleSessionName: "SourceAccountRoleSession",
        })

        // Create a S3 client with the source account STS credentials
        s3Client := s3.NewFromConfig(cfg, func(o *s3.Options) {
                o.Credentials = srcCreds
        })

        // Create the target account credentials for the desired role
        targetCreds := stscreds.NewAssumeRoleCredentials(stscreds.AssumeRoleOptions{
                RoleArn: "arn:aws:iam::234567890123:role/TargetAccountRole",
                RoleSessionName: "TargetAccountRoleSession",
                ExternalID: "123456789012",
        }, func(o *stscreds.AssumeRoleProvider) {
                o.Credentials = srcCreds.Retrieve(context.Background())
        })

        // Create a S3 client with the target account STS credentials
        s3Client = s3.NewFromConfig(cfg, func(o *s3.Options) {
                o.Credentials = targetCreds
        })

        // Use the target account STS credentials to make S3 API calls

相关内容

热门资讯

第7分钟辅助!wepoker可... 第7分钟辅助!wepoker可以开透视吗,epoker免费透视脚本,指南教程(有挂教学)1、首先打开...
三分钟辅助!wepoker免费... 三分钟辅助!wepoker免费脚本咨询,aapoker如何设置胜率,手册教程(果真有挂)1、很好的工...
第7分钟辅助!wepoker私... 第7分钟辅助!wepoker私人局辅助,hhpoker是真的还是假的,法子教程(有挂透明挂)hhpo...
四分钟辅助!wepoker分析... 四分钟辅助!wepoker分析,werplan怎么透视,模板教程(有挂方式)该软件可以轻松地帮助玩家...
第6分钟辅助!哈糖大菠萝软件下... 第6分钟辅助!哈糖大菠萝软件下载,aapoker怎么开辅助器,法子教程(证实有挂)1、哈糖大菠萝软件...
七分钟辅助!hhpoker透视... 七分钟辅助!hhpoker透视方法,we poker辅助器下载,机巧教程(有挂存在)1、we pok...
三分钟辅助!werplan免费... 三分钟辅助!werplan免费挂下载,拱趴大菠萝挂怎么安装,项目教程(有挂方针)1、完成拱趴大菠萝挂...
第9分钟辅助!wpk透视辅助靠... 第9分钟辅助!wpk透视辅助靠谱吗,wepoker透视脚本视频,机巧教程(有挂教学)wepoker透...
第十分钟辅助!werplan辅... 您好,wepoker游戏安装教程这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
一分钟辅助!哈糖大菠萝怎么开挂... 一分钟辅助!哈糖大菠萝怎么开挂,wepoker透视脚本免费,阶段教程(有挂规律)1、哈糖大菠萝怎么开...