gpg error There is no assurance this key belongs to the named user

318 Views Asked by At

I have the below which I'm running in Airflow. Im fetching the public pgp key file, that's coming from an ftp server, and saving it to a namedtemporaryfile to read to then use to encrypt a file.

However, I'm seeing the error:

There is no assurance this key belongs to the named user

I checked that the best way to avoid this is to set the gpg file to -- trust-model always which I have but since the pgp key file is a namedtemporary file the command throws an error:

FileNotFoundError: [Errno 2] No such file or directory 'gpg --encrypt --trust-model always /tmp/tmp51sdmyg5'

       # fetching public pgp key used for encryption
        gpg = gnupg.GPG()

        with tempfile.NamedTemporaryFile(delete=False) as public_pgp_file:
            remote_file_path = 'public_key.asc'
            ftp_hook.retrieve_file(remote_full_path=remote_file_path, local_full_path=public_pgp_file.name)

            public_pgp_file.seek(0)

            # Open the public pgp temporary file for reading

            with open(public_pgp_file.name, 'r', encoding="utf-8") as key_data:

                contents = key_data.read()

                import_result = gpg.import_keys(contents)

                public_keys = gpg.list_keys()

                # fetch the fingerprint for the key we want to use, in case there are multiple
                fingerprint = public_keys[0]['fingerprint']

                subprocess.run(f"gpg --encrypt --trust-model always {public_pgp_file.name}")
0

There are 0 best solutions below