在 Azure Python 函数中读取私钥 (asc) 时遇到问题

问题描述 投票:0回答:1

我在Azure函数中有这段Python代码。 Secret.asc 文件纯粹是一个带有私钥的文件,但是此代码仅读取公钥信息,没有成功导入私钥信息。

错误一直显示: gpg:运行“/usr/bin/gpg-agent”时出错:退出状态 2 gpg:无法启动代理“/usr/bin/gpg-agent”:一般错误 gpg:无法连接到代理:一般错误 gpg:获取 KEK 时出错:没有正在运行的代理 gpg:读取“[stdin]”时出错:没有正在运行的代理 gpg:从“[stdin]”导入失败:没有正在运行的代理 gpg:已处理总数:0 GPG:导入:1 gpg:秘密密钥读取:1 [GNUPG:] IMPORT_RES 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0

#DECRYPTION
    private_key_path = os.path.join("/home/site/wwwroot", "secret.asc")
    encrypted_file_path = os.path.join("/home/site/wwwroot", "Encrypted.txt")
    gpg_home = '/home/site/wwwroot/gpg_home'
    if not os.path.exists(gpg_home):
        os.makedirs(gpg_home)
    gpg = gnupg.GPG(gnupghome=gpg_home,use_agent=False)


    with open(private_key_path, "r") as private_key_file:
        key=gpg.import_keys(private_key_file.read())


我尝试了各种方法来在Azure功能中的SSH中启动代理,但没有任何效果。请帮忙,谢谢!

python azure azure-functions gnupg
1个回答
0
投票

要解决

gpg: can't connect to the agent
错误,请删除已安装的
gnupg
,然后重新安装。

apt-get remove gnupg -y
apt-get install -y gnupg gnupg1
cp -a /usr/bin/gpg /usr/bin/gpg2
ln -sf /usr/bin/gpg1 /usr/bin/gpg

当我尝试在 Azure 函数应用程序中生成密钥时,遇到了同样的错误。

root@5f970a08ed49:~/site/wwwroot# gpg --full-generate-key

gpg: starting migration from earlier GnuPG versions
gpg: error running '/usr/bin/gpg-agent': exit status 2
gpg: failed to start agent '/usr/bin/gpg-agent': General error
gpg: can't connect to the agent: General error
gpg: error: GnuPG agent unusable. Please check that a GnuPG agent can be started.
gpg: migration aborted

我可以通过在 Azure 函数应用程序的 SSH 中运行以下命令来解决此问题:

cp -a /usr/bin/gpg /usr/bin/gpg2
ln -sf /usr/bin/gpg1 /usr/bin/gpg

能够成功创建私钥:

回复:

root@5f970a08ed49:~/site/wwwroot# gpg --gen-key

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 1
Key expires at Wed Jan 22 07:46:45 2025 UTC
Is this correct? (y/N) y
//Removed few logs

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   2  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 2u
gpg: next trustdb check due at 2025-01-22
pub   2048R/5AB538D9 2025-01-21 [expires: 2025-01-22]
      Key fingerprint = 5F1F 8C40 0A4D A302 BB4F  8C81 2E79 1321 5AB5 38D9
uid                  Pravu (NA) <[email protected]>
sub   2048R/2C0ACE19 2025-01-21 [expires: 2025-01-22]

同时将功能代码中的

use_agent
更新为
true

 gpg = gnupg.GPG(gnupghome=gpg_home,use_agent=True) (or)

 gpg = gnupg.GPG(gnupghome=gpg_home) //use_agent is True by default
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.