简体   繁体   English

如何从浏览器中的凭证文件中访问 aws 凭证信息以从秘密管理器-赛普拉斯检索秘密

[英]how to access aws credentials info from credentials file within the browser to retrieve secrets from secrets manager -Cypress

I have aws credentials file and config file in my local windows machine.我的本地 windows 机器中有 aws 凭据文件和配置文件。 With below javascript code, I can get the secrets from aws secret manager containing username and password.But the problem is, My code runs within the browser through cypress and It cannot access the aws credentials file from my local.Hence, as you can see, I have hardcoded my aws credentials info to get it.使用下面的 javascript 代码,我可以从包含用户名和密码的 aws 秘密管理器获取秘密。但问题是,我的代码通过 cypress 在浏览器中运行,它无法从我的本地访问 aws 凭证文件。因此,如您所见,我已经硬编码了我的 aws 凭据信息来获取它。

My question is, How do I access the aws credentials file from my local so that I can get rid of hardcodes tokens?Please advise我的问题是,如何从本地访问 aws 凭据文件,以便摆脱硬编码令牌?请告知

\.aws\credentials 
[default] 
aws_access_key_id=<access key>
aws_secret_access_key= <secret key> 
aws_session_token=<session token>
aws_expiration=<datetime>

awssecretmanager.ts: awssecretmanager.ts:

 const AWS = require('aws-sdk'); const region = 'eu-central-1'; const secretName = 'secretname of my application'; const secretManager = new AWS.SecretsManager({ region, accessKeyId: 'qbxx1234556', //hardcoded secretAccessKey: 'axyx124545', //hardcoded sessionToken: 'x1234; //hardcoded }); const getSecrets = async () => { return await new Promise((resolve, reject) => { secretManager.getSecretValue({ SecretId: secretName }, (err, result) => { if (err) { reject(err); } else { resolve(JSON.parse(result.SecretString)); } }); }); }; const getSecret = async () => { const secret = await getSecrets(); return secret; //secret contains username and password }; export {getSecret};

How to access it from my test file?如何从我的测试文件中访问它? test.spec.ts测试规范.ts

 const secret = await getSecret(); if (secret;== undefined){ username = secret['username']; password = secret['password'];

I tried using below code, but it does not work as the browser cannot access the local credentials file我尝试使用下面的代码,但它不起作用,因为浏览器无法访问本地凭据文件

const credentials = new AWS.SharedIniFileCredentials({ profile: "default" });
AWS.config.credentials = credentials;

Had a workaround solution which was much more simpler.有一个更简单的解决方法。

1.As we are using 'Azure devops' for CI, it was easier for us to get the secrets from AWS Secret manager through the Azure devops pipeline tasks. 1.由于我们使用“Azure devops”进行 CI,我们更容易通过 Azure devops 管道任务从 AWS Secret Manager 获取机密。 2.Once you get the secrets, we set the environment variables like CYPRESS_USERNAME, CYPRESS_PASSWORD 3. In the test, we are able to refer the environment variable by using Cypress.env('USERNAME') and Cypress.env('PASSWORD') 2.一旦你得到秘密,我们设置环境变量,如CYPRESS_USERNAME,CYPRESS_PASSWORD 3.在测试中,我们可以通过使用Cypress.env('USERNAME')和Cypress.env('PASSWORD')来引用环境变量

Step1 and Step2: azure-pipeline.yml file below containing tasks Step1 和 Step2:下面的 azure-pipeline.yml 文件包含任务

 jobs: - job: run_e2e_tests steps: - task: SecretsManagerGetSecret@1 displayName: Get AWS secrets inputs: awsCredentials: 'aws credentials' regionName: 'eu-central-1' secretIdOrName: 'secretname' variableName: 'variable-containing-secrets' - powershell: | $variablecontainingsecrets ='$(variable-containing-secrets)' | ConvertFrom-Json echo "##vso[task.setvariable variable=TestUsername;isOutput=true;issecret=true;]$($variablecontainingsecrets.'username')" echo "##vso[task.setvariable variable=TestPassword;isOutput=true;issecret=true;]$($variablecontainingsecrets.'password')" name: secrets displayName: Set environment variables from secrets JSON - task: PowerShell@2 displayName: 'Setup environment variables for Cypress tests' inputs: targetType: 'inline' script: | Write-Host "##vso[task.setvariable variable=CYPRESS_USERNAME;]$(secrets.TestUsername)" Write-Host "About to set environment variable for username ($env:CYPRESS_USERNAME)" Write-Host "##vso[task.setvariable variable=CYPRESS_PASSWORD;]$(secrets.TestPassword)" Write-Host "About to set environment variable for password ($env:CYPRESS_PASSWORD)"

Step3: In the cypress tests, we then referenced the env variable like this Step3:在 cypress 测试中,我们然后像这样引用 env 变量

  cy.get('input#signInFormUsername').type(Cypress.env('USERNAME'));

Please note that if you are running it in local, you may need to set up the environment variables.请注意,如果您在本地运行它,您可能需要设置环境变量。 eg, I need to set the environment variables like CYPRESS_USERNAME=username , CYPRESS_PASSWORD=password例如,我需要设置环境变量,如CYPRESS_USERNAME=usernameCYPRESS_PASSWORD=password

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

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