pythonazureazure-functionsgnupgpython-gnupgp

Azure Functions in Python and GnuPG invocation


I have an Azure Function in Python and I am trying to use the python-gnupg wrapper to invoke a GnuPG binary while doing local development.

This is the code I am trying out inside of the Azure Function with a HTTP Trigger.

import gnupg
import tempfile
import subprocess
import azure.functions as func
import logging

@app.route(route="PGPOne")
def PGPOne(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

# Correctly obtaining the GPG binary path
gpg_path = r'C:\PGPD\dependencies\gpg.exe'

# Testing the GPG binary works
result = subprocess.run([gpg_path, '--version'], capture_output=True, text=True)
print(result.stdout)

# Creating a temporary directory for GPG home
temp_dir = tempfile.mkdtemp()
print(f"Temporary GPG home directory: {temp_dir}")

# Initializing GPG with the temporary home directory
gpg = gnupg.GPG(homedir=temp_dir, binary=gpg_path)

name = req.params.get('name')
if not name:
    try:
        req_body = req.get_json()
    except ValueError:
        pass
    else:
        name = req_body.get('name')

if name:
    return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
    return func.HttpResponse(
         "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
         status_code=200

The code blocks [Testing the GPG binary works] and [Creating a temporary directory for GPG home] both work as expected and I get the following outputs for the respective print statements.

Temporary directory: C:\Users\<myusername>\AppData\Local\Temp\tmpxacvv8_i
GPG binary path: C:\PGPD\dependencies\gpg.exe

But the invocation of

gnupg.GPG(homedir=temp_dir, binary=gpg_path)

results in an error starting with -

Python HTTP trigger function processed a request.
[2024-03-18T04:13:19.620Z] Creating directory: C:\PGPD\'C:\Users\<myusername>\AppData\Local\Temp\tmpxacvv8_i'
[2024-03-18T04:13:19.658Z] [WinError 123] The filename, directory name, or volume label syntax is incorrect: "C:\\PGPD\\'C:"

Why is this part being prefixed in the invocation while Creating directory:

 C:\PGPD\'

What am I doing wrong and how to correct this?


This is while debugging the function locally using Function Core Tools and using Python 3.10 in a virtual env setting within VS Code.

And I have brought in the GnuPG binary dependency into the code folder structure as recommended by Microsoft docs.


Solution

  • The code which worked for me is below and you can integrate into azure functions and I followed Document:

    import gnupg
    import tempfile
    import os
    import subprocess as rithsb
    
    rithgpg_path = r'C:\Program Files (x86)\GnuPG\bin\gpg.exe'
    oute = rithsb.run([rithgpg_path, '--version'], capture_output=True, text=True)
    print(oute.stdout)
    rith_temp_dir = tempfile.mkdtemp()
    print(f"Temporary GPG home directory: {rith_temp_dir}")
    gpg = gnupg.GPG(gnupghome=os.path.abspath(rith_temp_dir))
    print(gpg)
    

    Output:

    enter image description here