[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

parsing encrypted netrc file

On Tue, 23 Jun 2020 00:40:28 +0100,
MRAB <python at> 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,
>> info = netrc.netrc(

>> 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 login user at password mypasswd port 587

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