【问题标题】:How to authenticate gocql to AWS如何向 AWS 验证 gocql
【发布时间】:2022-02-08 15:42:25
【问题描述】:

我有一个需要连接 AWS 上的 Keyspaces 的 Go 服务。我的 pod 有一个角色和 AWS_SECRET_ACCESS_KEYAWS_ACCESS_KEY_IDAWS_SESSION_TOKEN 环境变量。

我想使用 aws SDK v2。我应该使用什么凭据提供程序? ec2rolecreds 或其他一个(可能是stscreds)?

我尝试实现来自here 的示例。但我得到一个错误

no EC2 IMDS role found, operation error ec2imds: GetMetadata, request canceled, context deadline exceeded

我在哪里可以找到工作示例和更多解释?

更新

现在我的代码如下所示:

awsCfg, err := awsConfig.LoadDefaultConfig(ctx)
if err != nil {
    return nil, fmt.Errorf("failed to load AWS config: %w", err)
}

imdsClient := imds.NewFromConfig(awsCfg)

appCreds := aws.NewCredentialsCache(ec2rolecreds.New(func(options *ec2rolecreds.Options) {
        options.Client = imdsClient
}))
cluster := gocql.NewCluster(cassandraHost)

auth := sigv4.NewAwsAuthenticator()
auth.SessionToken = value.SessionToken
auth.AccessKeyId = value.AccessKeyID
auth.SecretAccessKey = value.SecretAccessKey

cluster.Authenticator = auth

session, err := cluster.CreateSession()

Retrieve() 返回错误

no EC2 IMDS role found, operation error ec2imds: GetMetadata, exceeded maximum number of attempts, 3, request send failed, Get \"http://169.254.169.254/latest/meta-data/iam/security-credentials/\": dial tcp 169.254.169.254:80: connect: host is down

更新 2

在相同的环境中,AWS SDK v1 工作正常。所以物理上 AWS 端点是可用的。


session, err := aws_sess.NewSession()
if err != nil {
    return fmt.Errorf("failed to create AWS session: %w", err)
}

creds := credentialsV1.NewChainCredentials(
    []credentialsV1.Provider{
        &credentialsV1.EnvProvider{},
        &ec2rolecredsV1.EC2RoleProvider{
            Client: ec2metadataV1.New(session),
    }})

value, getErr := creds.Get()

【问题讨论】:

  • 你的代码是什么样的?
  • 我用代码更新了帖子

标签: amazon-web-services go authentication aws-sdk-go aws-sdk-go-v2


【解决方案1】:

no EC2 IMDS role found, operation error ec2imds: GetMetadata, exceeded maximum number of attempts, 3, request send failed, Get \"http://169.254.169.254/latest/meta-data/iam/security-credentials/\": dial tcp 169.254.169.254:80: connect: host is down

您的代码 sn-p 正在尝试使用 EC2 实例元数据服务来读取要使用的 IAM 角色。为此,您需要能够与之通信,并且角色必须附加到实例。

您的 Go 服务是否在 EC2 实例上运行?如果不是,那将解释您的错误。如果是,请确保进程或容器具有适当的网络访问权限(例如:网络命名空间)以与 169.254.169.254 通信。

【讨论】:

  • 当我在同一环境中使用 SDK v1 时,它可以得到一切。这是一个代码示例:``` session, err := aws_sess.NewSession() if err != nil { return fmt.Errorf("failed to create AWS session: %w", err) } creds := credentialsV1.NewChainCredentials( []credentialsV1.Provider{ &credentialsV1.EnvProvider{}, &ec2rolecredsV1.EC2RoleProvider{ 客户端:ec2metadataV1.New(session), }, }, ) 值,getErr := creds.Get() ```
  • 请看更新2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 2019-10-04
  • 2017-06-29
  • 2016-05-24
  • 2021-11-22
  • 2019-08-31
  • 1970-01-01
相关资源
最近更新 更多