简体   繁体   中英

pyinstaller of python daemon that uses cryptography throws exception: 'No module named '_hmacopenssl'

Hi Stack Overflow Team,

I have a daemon written in python, using daemon runner. ie

from daemon import runner
import random
import time

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/null'
        self.stderr_path = '/dev/null'
        self.pidfile_path = '/var/run/my_daemon.pid'
        self.pidfile_timeout = 5
    def run(self):
        seed = random.seed()
        rtime = random.randrange(21600,42300)
        count = 0
        while True:
            count += 1
            main()
            time.sleep(rtime)

app = App()
daemon_runner = runner.DaemonRunner(app)

In this same daemon, I use: from cryptography.fernet import Fernet to decrypt our DB password when the daemon sees a need to write to our remote database. ie

from cryptography.fernet import Fernet

def getTheIngredients(recipe):
    cipher_suite = Fernet(recipe['lock_smith'].encode())
    og_kush = cipher_suite.decrypt(recipe['og_kush_repro'].encode())
    raw_bacon = recipe['bacon'].encode()
    the_son_of_anton = Fernet(og_kush)
    bacon = the_son_of_anton.decrypt(raw_bacon)

    return bacon

I deploy this daemon using pyinstaller --onefile my_daemon.py and build an rpm using: rpmbuild -ba my_custom_daemon.spec

what the spec file looks like:

Name: my_daemon
Version: 1.1.1
Release:    0%{?dist}
Summary: my little daemon

Group: Miscellaneous
License: GPL
#URL:
Source0: my_daemon-1.1.1.tar.gz

BuildArch: x86_64
#BuildRequires: systemd
#Requires:

%description
my little daemon that does stuff :-)

%prep
%setup -q

%build

%install
rm -rf $RPM_BUILD_ROOT
install -d -m 0755 $RPM_BUILD_ROOT/usr/local/bin/
install -m 0755 my_daemon $RPM_BUILD_ROOT/usr/local/bin

mkdir -p %{buildroot}%{_unitdir}/
install -m 0644 %{name}.service %{buildroot}%{_unitdir}/

%post
%systemd_post %{name}.service

%clean
rm -rf $RPM_BUILD_ROOT

%files
%doc
/usr/local/bin/my_daemon
%{_unitdir}/my_daemon.service

%changelog
* Tue Jun 08 2021 M E <me@me.com> - 1.1.1-0
- stuff
- more stuff

when my daemon runs, the below exception gets caught:

'No module named '_hmacopenssl'

And I'm very certain that the exception has to do with the cryptography.fernet that I added to do the decryption for me. As my daemon was running perfectly fine until I made those changes.

So running my daemon outside of the built rpm works fine. But as soon as I package it and try run it via the installed rpm, it throws that exception.

Any assistance / guidance in this regard is greatly appreciated.

You have to add

Requires: python3-fernet

But I guess that during installation you will hit "No such package python3-fernet". So you will have to package that one first.

But it is on PyPI, so it should be easy using pyp2rpm --srpm fernet

Side note: you have several other issues in your spec and I recommend you reading https://docs.fedoraproject.org/en-US/packaging-guidelines/Python/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM