parsing encrypted netrc file

Seb spluque at gmail.com
Mon Jun 22 20:47:04 EDT 2020


On Tue, 23 Jun 2020 00:40:28 +0100,
MRAB <python at mrabarnett.plus.com> wrote:

> On 2020-06-22 23:38, Seb wrote:
>> Hello,

>> What's the pythonic way to do this without polluting the user's
>> directory with the decrypted file?  I wrongly thought this should do
>> it:

>> import os.path as osp import gnupg import netrc import tempfile

>> gpg = gnupg.GPG()

>> with open(osp.expanduser("~/.authinfo.gpg"), "rb") as f: with
>> tempfile.NamedTemporaryFile("w+") as tf: status = gpg.decrypt_file(f,
>> output=tf.name) info = netrc.netrc(tf.name)

>> which fails as the temporary file doesn't even get created.

> Are you sure it doesn't get created?

Without using tempfile:

with open(osp.expanduser("~/.authinfo.gpg"), "rb") as f:
    status = gpg.decrypt_file(f, output=".authinfo.txt")
    info = netrc.netrc(".authinfo.txt")

I get the error:

NetrcParseError: bad follower token 'port' (.authinfo.txt, line 1)

which is interesting.  The structure of ~/.authinfo.gpg is:

machine my.server.com login user at foo.com password mypasswd port 587

so it seems this is not what netrc.netrc expects.

-- 
Seb


More information about the Python-list mailing list