简体   繁体   English

AWS Secrets Manager 异常访问被拒绝

[英]AWS Secrets Manager Exception AccessDenied

The issue briefly explained:该问题简要说明:

I am getting this AWSSecretsManagerException AccessDenied when my web application makes API calls to AWS Secrets Manager to get secret keys/data.当我的 Web 应用程序对 AWS Secrets Manager 进行 API 调用以获取密钥/数据时,我收到此 AWSSecretsManagerException AccessDenied。

在此处输入图片说明

The issue in a little more detail:更详细一点的问题:

I have a Web Application war file deployed to AWS ElasticBeanstalk.我有一个部署到 AWS ElasticBeanstalk 的 Web 应用程序 war 文件。 I have a front end React application on AWS Amplify that makes a HTTPS Request to the WAR file.我在 AWS Amplify 上有一个前端 React 应用程序,它向 WAR 文件发出 HTTPS 请求。

As part of handling this request the Web Application interfaces to PayPal's API.作为处理此请求的一部分,Web 应用程序与 PayPal 的 API 接口。 To make a successful request to use PayPal's API I need a token .要成功请求使用 PayPal 的 API,我需要一个令牌 To get that token my web app needs to make a request to PayPal to get that token by sending my Pay Pal Client ID and Secret key .要获取该令牌,我的 Web 应用程序需要向 PayPal 发出请求,以通过发送我的 PayPal Client IDSecret key来获取该令牌

To not hardcode that into the app or to have a local config file with that client id and key in plain text, I decided to store those in AWS using AWS Secrets Manager.为了不将其硬编码到应用程序中或使用纯文本格式的客户端 ID 和密钥拥有本地配置文件,我决定使用 AWS Secrets Manager 将它们存储在 AWS 中。 To access the keys stored there in my application I need to use the AWS Secrets Manager SDK.要访问存储在我的应用程序中的密钥,我需要使用 AWS Secrets Manager SDK。 I did that by including this dependency on my Web Applications pom file.我通过在我的 Web 应用程序 pom 文件中包含这个依赖来做到这一点。

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-secretsmanager</artifactId>
    <version>1.11.700</version>
</dependency>

I then added code to retrieve the client id and secret using the AWS Secrets Manager SDK API.然后,我添加了使用 AWS Secrets Manager SDK API 检索客户端 ID 和密钥的代码。

When I run my Web Application locally on my machine on a Payara Server it works.当我在 Payara 服务器上的机器上本地运行我的 Web 应用程序时,它可以工作。 Meaning I have a local React app on my machine that has the HTTP Request go to my local Web app on Payara (instead of on AWS), and the Web app successfully extracts the Client ID and Secrets from AWS Secrets Manager.这意味着我的机器上有一个本地 React 应用程序,该应用程序将 HTTP 请求发送到 Payara(而不是 AWS)上的本地 Web 应用程序,并且该 Web 应用程序成功地从 AWS Secrets Manager 中提取了客户端 ID 和机密。

However when I deploy my Web Application to Elastic Beanstalk EC2 instance, I get the Exception above from my front end React app on AWS Amplify.但是,当我将 Web 应用程序部署到 Elastic Beanstalk EC2 实例时,我从 AWS Amplify 上的前端 React 应用程序中收到了上述异常。

After a couple of hours I got the issue resolved.几个小时后,我解决了问题。

First you have to notice the Role listed in the Exception (look at the bolded text):首先,您必须注意 Exception 中列出的 Role(查看粗体文本):

com.amazonaws.services.secretsmanager.model.AWSSecretsManagerException: User: arn:aws:sts::<......>:assumed-role/ aws-elasticbeanstalk-ec2-role /<.....> is not authorized to perform: secretsmanager:GetSecretValue on resource: arn:aws:secretsmanager:us-east-1:<......>:secret:<.....> (Service: AWSSecretsManager; Status Code: 400; Error Code: AccessDeniedException; Request ID: <.......>) com.amazonaws.services.secretsmanager.model.AWSSecretsManagerException:用户:arn:aws:sts::<......>:assumed-role/ aws-elasticbeanstalk-ec2-role /<...>未授权执行:secretsmanager:GetSecretValue 资源:arn:aws:secretsmanager:us-east-1:<......>:secret:<.....>(服务:AWSSecretsManager;状态代码:400 ; 错误代码: AccessDeniedException; 请求 ID: <......>)

When you're in the AWS Console go to AWS->IAM .当您在 AWS 控制台中时,转到AWS->IAM From there click on the roles link on the left side.从那里单击左侧的角色链接。 Notice the role from the bolded text above is listed there (text circled in red).请注意上面粗体文本中的角色在此处列出(文本以红色圈出)。

在此处输入图片说明

Double click on that.双击那个。

In this screenshot I already did it, but initially the SecretsManagerReadWrite policy (circled in red) wasn't there.在此屏幕截图中,我已经这样做了,但最初没有SecretsManagerReadWrite策略(以红色圈出)。

在此处输入图片说明

To get that policy, click on "Attach Policies".要获得该政策,请单击“附加政策”。 Then on the next page search for SecretsManagerReadWrite , and click on the checkbox.然后在下一页搜索SecretsManagerReadWrite ,然后单击复选框。 Click on "Attach Policy" button and now that role has it!单击“附加策略”按钮,现在该角色已拥有!

Now my HTTPS request from my React app on AWS Amplify makes the request to my Web App on ElasticBeanstalk and I no longer get that AWSSecretsManagerException AccessDenied message!现在,来自 AWS Amplify 上的 React 应用程序的 HTTPS 请求向 ElasticBeanstalk 上的 Web 应用程序发出请求,并且我不再收到AWSSecretsManagerException AccessDenied消息!

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

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