在Java中扮演Web Indidentity请求的角色

发布于 2025-01-27 06:18:18 字数 1786 浏览 4 评论 0原文

Kubernetes中的微服务需要访问私有AWS API网关中的API。身份验证和授权是AWS_IAM。

Kubernetes具有适当的环境变量 aws_web_identity_token_file AWS_ROLE_ARN

KUBERNETES中的Pods使用服务帐户运行。对于AWS_IAM,我们需要生成会话令牌。

  1. WebIdentityTokenFileCredentialSprovider.Create()有助于仅生成访问密钥和访问秘密。这不会生成会话令牌。

  2. 基本上,我们需要将STS CLI命令转换为Java

    在 - -olole-arn $ aws_role_arn \ - 角色 - 会议名称Mysession \ -web-nidentity-token file:// $ aws_web_identity_token_file \ - 持续时间1000> /tmp/irp-cred.txt
  3. 我在Java代码中传递了正确的内容

      ausherolewithwebidentityrequindityrequest asherolewithwebidentityRequindityRequest = ausheroleleolewithwebidentityrequest.builder()
                    。
                            。
                                    .Rolesessessname(“用户”)
                                            。建造();
    log.info(“ asherolewithwebidentityRequest:{}”,asherolewithwebidentityRequest.toString());
    
    stsclient stsclient = stsclient.builder()
              .Build();
    asherolewithwebidentityResponse asherolewithwebidentityResponse = stsclient.assumerolewithwebidentity(asherolewithwebidentityRequest);
    log.info(“ asherolewithwebidentityResponse:{}”,asherolewithwebidentityResponse.toString());
    
    凭证凭证= asherolewithwebidentityResponse.credentials();
    log.info(“凭据,accessKey:{},accessSecret:{},accessToken:{}”,recretentials.accesskeyid(),recretentials.secretaccesskey(),recretentials.sessiontoken();
     
  4. 我正在使用以下依赖关系

     < depentency>
        < groupId> software.amazon.awssdk</groupid>
        < artifactid> acm</artifactid>
        <版本> 2.17.102</version>
    </depentency>
     

,但这不起作用。

有人可以帮忙吗?

The microservice in Kubernetes needs to access the API's hosted in a private AWS API gateway. The authentication and authorization is AWS_IAM.

The Kubernetes has the proper environment variables
AWS_WEB_IDENTITY_TOKEN_FILE
AWS_ROLE_ARN

The pods in Kubernetes are running with service account. For AWS_IAM, we need the session token to be generated.

  1. WebIdentityTokenFileCredentialsProvider.create() helps to generate only access key and access secret. This does not generate the session token.

  2. So basically, we need to convert the sts cli command to Java

    aws sts assume-role-with-web-identity \
     --role-arn $AWS_ROLE_ARN \
     --role-session-name mysession \
     --web-identity-token file://$AWS_WEB_IDENTITY_TOKEN_FILE \
     --duration-seconds 1000 > /tmp/irp-cred.txt
    
  3. I am passing the correct things in the Java code

    AssumeRoleWithWebIdentityRequest assumeRoleWithWebIdentityRequest = AssumeRoleWithWebIdentityRequest.builder()
                    .webIdentityToken("file:///var/run/secrets/eks.amazonaws.com/serviceaccount/token")
                            .roleArn("arn:aws:iam::*:role/eks/test")
                                    .roleSessionName("user")
                                            .build();
    log.info("AssumeRoleWithWebIdentityRequest:{}",assumeRoleWithWebIdentityRequest.toString());
    
    StsClient stsClient =  StsClient.builder()
              .region(region).build();
    AssumeRoleWithWebIdentityResponse assumeRoleWithWebIdentityResponse = stsClient.assumeRoleWithWebIdentity(assumeRoleWithWebIdentityRequest);
    log.info("AssumeRoleWithWebIdentityResponse:{}",assumeRoleWithWebIdentityResponse.toString());
    
    Credentials credentials = assumeRoleWithWebIdentityResponse.credentials();
    log.info("credentials, AccessKey:{},AccessSecret:{},AccessToken:{}",credentials.accessKeyId(),credentials.secretAccessKey(),credentials.sessionToken();
    
  4. I am using below dependency

    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>acm</artifactId>
        <version>2.17.102</version>
    </dependency>
    

But this is not working.

Can someone please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文