简体   繁体   English

节点和 github 秘密。 如何在本地测试

[英]node and github secrets. how to test locally

i'm using node and github actions for a simple twitter api script.我正在为一个简单的 twitter api 脚本使用节点和 github 操作。

twitter credentials are stored in github secrets twitter 凭据存储在 github 秘密中

other than hard coding the twitter credentials and then changing the code to use secrets before pushing to github how do i test locally?除了硬编码 twitter 凭据,然后在推送到 github 之前更改代码以使用机密之外,我如何在本地进行测试?

I need some way to retrieve the credentials locally if running locally and from secrets if running on github.如果在本地运行,我需要一些方法在本地检索凭据;如果在 github 上运行,我需要一些方法从机密中检索凭据。

what is best practice way to do this please?请问这样做的最佳做法是什么?

Essentially, what David is trying to say is that you'll need to store your credentials in two separate places:本质上,David 想说的是您需要将您的凭据存储在两个不同的地方:

  1. Inside GitHub Actions Secrets内部 GitHub 动作秘密
  2. Your local filesystem inside a gitignored.env file gitignored.env 文件中的本地文件系统

GitHub Actions Secrets GitHub 动作秘密

GitHub Actions Secrets uses encryption to store the secret value, and this information is only decrypted while the workflow is running. GitHub Actions Secrets 使用加密来存储秘密值,并且此信息仅在工作流运行时解密。 Note that the secrets are not stored in the codebase.请注意,机密未存储在代码库中。 Instead, this is managed entirely via the GitHub web interface, and perhaps the GitHub API. I do want to emphasise again that none of this is stored inside your git repository as that would essentially be considered a security leak.相反,这完全是通过 GitHub web 接口管理的,也许还有 GitHub API 接口。我想再次强调,这些都没有存储在你的 git 存储库中,因为这基本上被认为是安全漏洞。

Now, this works fine when running in GitHub Actions, but the question you have is how do you run locally.现在,这在 GitHub Actions 中运行时运行良好,但您的问题是如何在本地运行。 This is where the.env file comes into play.这是 .env 文件发挥作用的地方。

Running Locally with.env and dotenv使用 .env 和 dotenv 在本地运行

Create the following file, and add the same value you used as the secret in GitHub Actions secrets inside the.env file as a key/value pair.创建以下文件,并在 .env 文件中添加您在 GitHub Actions secrets 中用作 secret 的相同值作为键/值对。 Let's assume the secret name is MY_SECRET_CRED and the value is abcdefg:假设秘密名称为 MY_SECRET_CRED,值为 abcdefg:

.env:环境:

MY_SECRET_CRED=abcdefg

Next, add this file to.gitignore.接下来,将此文件添加到 .gitignore。 You do not want to commit this, because it would violate your security, and you also don't need it to be committed, since GitHub has stored the credential as a secret.你不想提交这个,因为它会违反你的安全,你也不需要提交它,因为 GitHub 已经将凭证存储为秘密。 You only need this file locally .您只需要在本地使用此文件。

.gitignore: .gitignore:

.env

The purpose of adding the file to gitignore is because you do not want to commit this file.将文件添加到 gitignore 的目的是因为你不想提交这个文件。 This file is local to your workstation only.此文件仅在您的工作站本地。

Next, install the dotenv npm module:接下来,安装 dotenv npm 模块:

$ npm i dotenv

In your main file, be sure to require the dotenv module.在您的主文件中,确保需要 dotenv 模块。 This module reads the contents of.env and loads the key value pairs up as environment variables:该模块读取 .env 的内容并将键值对加载为环境变量:

require('dotenv').config()

Tying it all together把它们绑在一起

In your application code, you're probably already using the secret in GitHub Actions as some kind of environment variable.在您的应用程序代码中,您可能已经将 GitHub Actions 中的秘密用作某种环境变量。 If running in GitHub Actions, you can load the secret as an environment variable with the same name you used in the.env file:如果在 GitHub Actions 中运行,您可以将 secret 作为环境变量加载,其名称与您在 .env 文件中使用的名称相同:

- name: Test application
  env:
    MY_SECRET_CRED: ${{ secrets.MY_SECRET_CRED }}
  run: npm run test

In the above example, the Node.js process can access the secret as the environment variable MY_SECRET_CRED在上面的示例中,Node.js 进程可以访问作为环境变量 MY_SECRET_CRED 的秘密

doSomethingWithSecret(process.env.MY_SECRET_CRED);

But do note that, if you try to print it, GitHub will mask it, since it knows it's a secret that shouldn't be leaked.但请注意,如果您尝试打印它,GitHub 将掩盖它,因为它知道这是不应该泄露的秘密。

When running locally, the environment variable is loaded into the Node.js process via the dotenv module.在本地运行时,环境变量通过dotenv模块加载到Node.js进程中。

Downside to.env files .env 文件的缺点

The downside to.env files is that the secret is stored in plain text in your workstation, and it's difficult to share with other developers. .env 文件的缺点是秘密以纯文本形式存储在您的工作站中,并且很难与其他开发人员共享。 Ideally, we don't want to share secrets through chat systems or email for the same reasons we don't want to commit and push them.理想情况下,我们不想通过聊天系统或 email 共享秘密,原因与我们不想提交和推送它们的原因相同。

One solution is to use a secret manager or key management store to encrypt secrets locally.一种解决方案是使用秘密管理器或密钥管理存储在本地加密秘密。 Your local application then decrypts them when running the application.然后,您的本地应用程序会在运行应用程序时解密它们。 This also means you can commit the encrypted data directly to the GitHub repository.这也意味着您可以将加密数据直接提交到 GitHub 存储库。 One example of this is Google KMS. Google KMS 就是其中一个例子。

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

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