繁体   English   中英

获取没有 session 的 aws-sdk 参数

[英]get aws-sdk parameters without session

我想知道这是否可能 - 我需要从 AWS 商店获取参数以在每个环境中使用某些值,而无需每次重新部署 Angular 应用程序以获取 appSettings.json 中的更改

我有以下代码在 SessionToken 过期之前运行良好。 这是一个公共站点,因此没有经过身份验证的用户。

import { Injectable } from '@angular/core';
import { SSMClient, GetParameterCommand, GetParameterCommandInput } from '@aws-sdk/client-ssm';
import { fromTemporaryCredentials } from '@aws-sdk/credential-providers';

@Injectable({
  providedIn: 'root'
})
export class SsmService {

  constructor() {}

  creds = {
    accessKeyId: 'accessKeyId',
    secretAccessKey: 'secretAccessKey',
    sessionToken: 'sessionToken'
  }

  tempCreds = fromTemporaryCredentials({
    masterCredentials: this.creds,
    params: {
      RoleArn: 'arn:RoleArn',
      RoleSessionName: 'RoleSessionName',
      DurationSeconds: 3600
    },
    clientConfig: {
      region: 'eu-west-2'
    }
  });

  client: SSMClient = new SSMSClient(
  {
    region: 'eu-west-2',
    credentials: this.tempCreds
  });

  async getParameterValue(name: string): Promise<string> {
    const input: GetParameterCommandInput = {
      Name: name
    }

    const command = new GetParameterCommand(input);

    return await this.client.send(command).Parameter?.Value ?? '';
  }
}

是否可以在没有 SessionToken 的情况下获取参数?

不可以。如果没有有效的 session 令牌,则无法调用 AWS API。

我有以下代码在 SessionToken 过期之前运行良好。

为什么不在旧令牌过期之前从 STS 获取新的 session 令牌?

在没有凭据和身份验证的情况下获取配置数据的一种方法是从本地配置文件加载它们(例如,参见https://jooooel.com/angular-config-from-an-external-source/ )。 当然,这不能用于加载凭据或其他机密数据。 想法是将非机密配置和设置数据存储在 json 文件中,并在应用程序启动之前动态加载。 每个环境都可以有不同的配置文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM