如何在 Bash/终端中导出多行环境变量,例如: RSA 私钥

我们的一个应用程序 github-backup要求使用一个 RSA 私钥作为环境变量。

只是尝试在终端中导出密钥,例如: 短信 Export PRIVATE _ KEY = ——-开始 RSA PRIVATE KEY ——- MIIEpAIBAAKCAQEA04up8hoqzS1 + ... L48DlnUtMdMrWvBlRFPzU + hU9wDhb3F0CATQdvYo2mhzyUs8B1ZSQz2Vy = = ——结束 RSA 私人密钥——

不工作... 因为线断了。

我谷歌了一下,但没有找到一个可行的解决方案..。
例如: < a href = “ https://stackoverflow. com/questions/43082918/How-to-sett-multiline-RSA-private-key-Environment-variable-for-AWS-厅弹性豆”> 如何为 AWS 弹性豆设置多线 RSA 私钥环境变量

image

错误: ——-END RSA PRIVATE KEY ——-“ : 不是有效的标识符

遵照下列指示: Http://blog.vawter.com/2016/02/10/create-an-environment-variable-from-a-private-key

创建了一个名为 keytoenvar.sh的文件,其代码行如下:

#!/usr/bin/env bash
file=$2
name=$1
export $name="$(awk 'BEGIN{}{out=out$0"\n"}END{print out}' $file| sed 's/\n$//')"

image 然后运行以下命令:

source keytoenvar.sh PRIVATE_KEY ./gitbu.2018-03-23.private-key.pem

那个 工程但它看起来像一个“ 长篇大论”的方法..

有人知道 更简单做这件事的方法吗?
(我希望有一个“强”的初学者友好的解决方案,没有太多的“步骤”..。)

99766 次浏览

export the key

export PRIVATE_KEY=`cat ./gitbu.2018-03-23.private-key.pem`

test.sh

#!/bin/bash


echo "$PRIVATE_KEY";

If you want to save the key to a .env file with the rest of your environment variables, all you needed to do is "wrap" the private key string in single quotes in the .env file ... e.g: sh exports HELLO_WORLD='-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEA04up8hoqzS1+APIB0RhjXyObwHQnOzhAk5Bd7mhkSbPkyhP1 ... iWlX9HNavcydATJc1f0DpzF0u4zY8PY24RVoW8vk+bJANPp1o2IAkeajCaF3w9nf q/SyqAWVmvwYuIhDiHDaV2A== -----END RSA PRIVATE KEY-----' So the following command will work:

echo "export PRIVATE_KEY='`cat ./gitbu.2018-03-23.private-key.pem`'" >> .env

Followed by:

source .env

Now the key will be in your .env file and whenever you source .env it will be exported.

NOTE: For me to get the output to work correctly, I had to wrap the environment variable in double quotes. Otherwise it replaced newlines with spaces.

In:

export PRIVATE_KEY=$(cat ./gitbu.2018-03-23.private-key.pem)

Out:

echo "$PRIVATE_KEY"

If you want to export direct value (not from *.pem) then use " after equals sign. The terminal will let you finish with another ".

export PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA04up8hoqzS1+
...
l48DlnUtMdMrWvBlRFPzU+hU9wDhb3F0CATQdvYo2mhzyUs8B1ZSQz2Vy==
-----END RSA PRIVATE KEY-----"

What I wanted is one and only one executable shell script containing it all, and not 1 script and 1 .pem file and then doing some gymnastics in between, like what I am seeing in the existing answers so far.

To achieve this unification, all that is needed is the following. Preparation phase:

cat id_rsa | base64 -w0
# assign the converted 1-liner string wrap in single quote into a shell variable, for example
pk='xxxxxxxxxxxyyyyyyyyyyzzzzzzzzzzz......'

The rest is walk-in-the park. To ssh using variable pk you will convert back the 1-liner string into its original posture and write to a temporary file.

t=$(mktemp ~/temp.XXXXXXXXXX)
printf $pk | base64 --decode > $t
ssh -i $t smeagol@192.143.69.69

To clean-up the temporary file when your shell script exits, add a shell trap handler:

trap cleanup 1 2 3 6
cleanup () {
rm -f $t
}

To improve security, notice the use of mktemp ~/temp.XXXXXXXXXX so the temporary file is written somewhere within your $HOME folder where only you can read, rather than in a system wide /tmp folder where other users in the same server can read.

You can also use a bash heredoc:

export MY_CERTIFICATE=$(cat <<EOF
-----BEGIN CERTIFICATE-----
qiuwiuwoejqododhIOOISOIIOiiSNIDNIDINDIONDIND
DDHDHUDHDUHUhudhHQhhqoohooiiohihiohihhihhihi
dhdiodhioho...
-----END CERTIFICATE-----
EOF
)

Once you set it you can access it as a regular env variable echo "$MY_CERTIFICATE".

Adding a RSA key to an .env file.

Step 1.

echo "PRIVATE_KEY=\"`sed -E 's/$/\\\n/g' my_rsa_2048_priv.pem`\"" >> .env

Your key in the .env file will look something like this:

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\n
dasdasdadasdasdasdasdasdasdasdadasdasdadasa\n
huehuauhhuauhahuauhauahuauhehuehuauheuhahue\n
-----END RSA PRIVATE KEY-----\n"

Step 2. Printing PRIVATE_KEY only show the first line. Change the variable to a single line. Like this:

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\ndasdasdadasdasdasdasdasdasdasdadasdasdadasa\nhuehuauhhuauhahuauhauahuauhehuehuauheuhahue\n-----END RSA PRIVATE KEY-----\n"

If using the key inside an app e.g. node.
process.env.PRIVATE_KEY will be outputted correctly.

I'll add that a more elegant fool-proof way is to encode the env var as base64 and then decode it when you access it.

const base64 = process.env.GITHUB_PRIVATE_KEY
const privateKey = Buffer.from(base64, 'base64')