Heroku 中的多行配置变量

发布于 2024-11-27 16:23:47 字数 302 浏览 1 评论 0原文

我有一个 Rails 应用程序,可以在使用 Paypal 进行交易之前加载许多 RSA 证书。在我的开发机器上,这些证书是从文件系统中的文件读取的,但由于 Heroku(我用于部署)基本上是只读的,我无法上传这些文件,所以我猜我必须从配置变量中读取证书(请参阅 Heroku 配置变量)。

因为证书由多行数据组成,所以我不确定如何将它们设置为变量,或者即使这是可能的。有谁知道我该如何做到这一点或能够提出替代方法?

非常感谢, 艾迪

I've have a Rails app that loads a number of RSA certificates before a transaction is made with Paypal. On my development machine, these certificates are read from files in the file system but because Heroku (which I'm using for delpoyment) is largely read-only, I can't upload these files so I'm guessing I'll have to read the certificates from config variables (see Heroku Config Vars).

Because the certificates consist of multiple lines of data, I'm not sure how to set them as variables or even if this is possible. Does anyone know how I could do this or be able to suggest an alternative approach?

Many thanks,
Eddie

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

黯然 2024-12-04 16:23:47

我发现添加多行配置的一个简单方法是双引号它们,然后从我的本地环境中回显它们

heroku config:add EC2_PRIVATE_KEY="$EC2_PRIVATE_KEY"

I found that a easy way to add multi-line configs is to double quote them and then echo them from my local environment

heroku config:add EC2_PRIVATE_KEY="$EC2_PRIVATE_KEY"
囍孤女 2024-12-04 16:23:47

如果您想从文件内容设置 Heroku 配置值,您可以使用以下 shell 技巧:

$ heroku config:set SECRET_KEY="$(cat path/to/secret.key)"

可以通过在值周围加上引号来直接设置多行值:

$ heroku config:set SECRET_KEY='first line
> second line'

如果您使用 Foreman 在本地运行(现在 heroku local),它不支持多行变量。您必须首先使用某些东西将它们注入到环境中,例如 envdir

$ envdir my-env-dir heroku local

If you want to set Heroku config values from your file contents, you can use the following shell trick:

$ heroku config:set SECRET_KEY="$(cat path/to/secret.key)"

Multi-line values can be set directly by putting quotes around the value:

$ heroku config:set SECRET_KEY='first line
> second line'

If you're using Foreman to run locally (now heroku local), it doesn't support multi-line variables. You must use something to inject them into the environment first, such as envdir:

$ envdir my-env-dir heroku local
挥剑断情 2024-12-04 16:23:47

或者,您可以转到 Heroku 仪表板的“设置”选项卡,打开“配置变量”并将其粘贴进去。

Heroku 设置

简单。

Or you can just go to the Settings tab of the Heroku dashboard, open Config Vars and paste it in.

Heroku Settings

Easy peasy.

月亮是我掰弯的 2024-12-04 16:23:47

我们需要做同样的事情。

您可以将变量值括在双引号中:

bobvila@bobuntu:~/svnroot/app/myapp$ heroku config:add woodchuck="How much wood
> could a woodchuck chuck
> if a woodchuck could chuck wood"
Adding config vars and restarting myapp... done, v25
woodchuck: How much wood
could a woodchuck chuck
if a woodchuck could chuck wood
bobvila@bobuntu:~/svnroot/app/myapp$ heroku config
=== Config Vars for myapp
woodchuck:                       How much wood
could a woodchuck chuck
if a woodchuck could chuck wood
bobvila@bobuntu:~/svnroot/app/myapp$ 

如果您使用 Foreman 进行本地主机开发,则 .env 文件不支持多行变量,因此您需要在启动 Foreman 之前将其导出到 shell

We needed to do the same thing.

You can wrap the variable value in double quotes:

bobvila@bobuntu:~/svnroot/app/myapp$ heroku config:add woodchuck="How much wood
> could a woodchuck chuck
> if a woodchuck could chuck wood"
Adding config vars and restarting myapp... done, v25
woodchuck: How much wood
could a woodchuck chuck
if a woodchuck could chuck wood
bobvila@bobuntu:~/svnroot/app/myapp$ heroku config
=== Config Vars for myapp
woodchuck:                       How much wood
could a woodchuck chuck
if a woodchuck could chuck wood
bobvila@bobuntu:~/svnroot/app/myapp$ 

If you're using Foreman for localhost development, the .env file doesn't support multi-line variables so you'll need to export it to the shell before launching Foreman

执笔绘流年 2024-12-04 16:23:47

我的回答有点晚了,但我最近在多行环境中遇到了同样的问题。 Heroku 上的变量。
我的解决方案是使用 strict_encode64

encoded_secret = Base64.strict_encode64("my_multi_line_secret")

添加密钥:

$ heroku config:set SECRET_KEY='the encoded_secret string here'

在代码中,然后使用 Base64.strict_decode64(ENV['SECRET_KEY']) 对其进行解码

My answer comes a bit late but I had the same issue recently with multi-line env. variables on Heroku.
My solution was to use strict_encode64:

encoded_secret = Base64.strict_encode64("my_multi_line_secret")

add the key:

$ heroku config:set SECRET_KEY='the encoded_secret string here'

In the code, you then decode it using Base64.strict_decode64(ENV['SECRET_KEY'])

梦冥 2024-12-04 16:23:47

如何使用 NodeJS 处理此问题的示例。通过用 \n 替换 \\n 字符来清理该值:

process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n')

取自:
使用 Firebase privateKey 作为 Heroku 配置变量来转义问题

An example of how to deal with this problem using NodeJS. Sanitize the value by replacing \\n characters with \n:

process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n')

Taken from:
Escaping issue with firebase privateKey as a Heroku config variable

错々过的事 2024-12-04 16:23:47

我知道这是一种非常手动的方法,但对我有用的是将私钥粘贴到文本编辑器中,找到并用实际的换行符替换 \n 并将其粘贴为Heroku 中环境变量的值。

I know this is a very manual way of doing it, but what worked for me is to paste the private key into a text editor, find and replace \n with an actual line break and paste that as the value for the env var in Heroku.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文