K8s 部署上的 Cognito Go SDK 问题

问题描述 投票:0回答:1

我遇到一个问题,AWS Cognito 问题在本地工作,但一旦部署到 K8s 中,它就无法工作。我希望得到一些关于原因的帮助。

以下简化的 Go 功能在我的本地开发机器(Fedora 39)上可以运行,但是一旦放入 K8s 中就无法运行。我已删除所有 ENV 变量并对它们进行硬编码以确保这不是问题。

func GetUserEmail(username string) string {
    // userPoolID := os.Getenv("AWS_USER_POOL_ID")
    // awsRegion := os.Getenv("AWS_REGION_ID")

    userPoolID := "us-east-2_xxxxxxxxx"
    awsRegion := "us-east-2"
    username = "xxxxxxxx-xxxx-xxxx-xxxx-bc4d2c17e7c8"

    verbose := true

    mySession := session.Must(session.NewSession())

    // Create a CognitoIdentityProvider client with additional configuration
    svc := cognitoidentityprovider.New(mySession, aws.NewConfig().WithRegion(awsRegion).WithCredentialsChainVerboseErrors(verbose))

    userInput := &cognitoidentityprovider.AdminGetUserInput{}
    userInput.SetUserPoolId(userPoolID)
    userInput.SetUsername(username)

    log.Printf("userInput: %+v\n", userInput)

    userOutput, err := svc.AdminGetUser(userInput)
    if err != nil {
        log.Printf("cognito client admin get user error: %s", err.Error())
        return ""
    }

    att := userOutput.UserAttributes
    for _, attr := range att {
        if *attr.Name == "email" {
            return *attr.Value
        }
    }

    return ""
}

当我在本地运行此代码时,它会按预期返回电子邮件,但是,然后我在 K8s 中运行此代码时出现以下错误:

2023/12/30 21:58:57 Cognito 客户端管理员收到用户错误:NoCredentialProviders:链中没有有效的提供程序。已弃用。

部分详细错误是:

2023/12/30 21:43:10 Cognito 客户端管理员收到用户错误:NoCredentialProviders:链中没有有效的提供者 原因如下:EnvAccessKeyNotFound:未能在环境中找到凭据。 SharedCredsLoad:无法加载配置文件,. EC2RoleRequestError:找不到 EC2 实例角色 导致:RequestError:发送请求失败 原因:获取“http://169.254.169.254/latest/meta-data/iam/security-credentials/”:超出上下文截止日期(等待标头时超出 Client.Timeout)

非常感谢任何帮助。

go amazon-cognito aws-sdk-go
1个回答
0
投票

AWS SDK GO 要求您向系统提供凭证以对操作进行身份验证。文档指定您需要将它们作为环境变量传递到系统中,系统将使用标头中的那些变量。

本文档解释了所需的信息:https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials

我的本地计算机安装了 AWS CLI,它会在您的主目录中创建一个 .aws 文件夹,本地版本在本地运行时会使用该文件夹。

添加所需的两个凭据后,该应用程序运行良好。感谢 Ermiya Eskandary 提供的链接。

© www.soinside.com 2019 - 2024. All rights reserved.