reflex-dev / reflex

🕸️ Web apps in pure Python 🐍
https://reflex.dev
Apache License 2.0
19.87k stars 1.14k forks source link

Cannot deploy app with "paramiko" as dependency #3277

Open ali-zakaria opened 5 months ago

ali-zakaria commented 5 months ago

Describe the bug Cannot deploy app with "paramiko" as dependency.

Getting this error when running the application

2024-05-12 01:30:04.498689 Romance Daylight Time | rxh-prod-botmanagerwebapp |      StockPickler.save_dict(pickler, obj) | None | None
2024-05-12 01:30:04.498689 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/usr/local/lib/python3.8/pickle.py", line 971, in save_dict | None | None
2024-05-12 01:30:04.498690 Romance Daylight Time | rxh-prod-botmanagerwebapp |      self._batch_setitems(obj.items()) | None | None
2024-05-12 01:30:04.499024 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/usr/local/lib/python3.8/pickle.py", line 997, in _batch_setitems | None | None
2024-05-12 01:30:04.499029 Romance Daylight Time | rxh-prod-botmanagerwebapp |      save(v) | None | None
2024-05-12 01:30:04.499032 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/home/reflexuser/venv/lib/python3.8/site-packages/dill/_dill.py", line 414, in save | None | None
2024-05-12 01:30:04.499033 Romance Daylight Time | rxh-prod-botmanagerwebapp |      StockPickler.save(self, obj, save_persistent_id) | None | None
2024-05-12 01:30:04.499034 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/usr/local/lib/python3.8/pickle.py", line 603, in save | None | None
2024-05-12 01:30:04.499035 Romance Daylight Time | rxh-prod-botmanagerwebapp |      self.save_reduce(obj=obj, *rv) | None | None
2024-05-12 01:30:04.499036 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/usr/local/lib/python3.8/pickle.py", line 717, in save_reduce | None | None
2024-05-12 01:30:04.499036 Romance Daylight Time | rxh-prod-botmanagerwebapp |      save(state) | None | None
2024-05-12 01:30:04.499036 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/home/reflexuser/venv/lib/python3.8/site-packages/dill/_dill.py", line 414, in save | None | None
2024-05-12 01:30:04.499037 Romance Daylight Time | rxh-prod-botmanagerwebapp |      StockPickler.save(self, obj, save_persistent_id) | None | None
2024-05-12 01:30:04.499037 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/usr/local/lib/python3.8/pickle.py", line 560, in save | None | None
2024-05-12 01:30:04.499038 Romance Daylight Time | rxh-prod-botmanagerwebapp |      f(self, obj)  # Call unbound method with explicit self | None | None
2024-05-12 01:30:04.499038 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/home/reflexuser/venv/lib/python3.8/site-packages/dill/_dill.py", line 1217, in save_module_dict | None | None
2024-05-12 01:30:04.499039 Romance Daylight Time | rxh-prod-botmanagerwebapp |      StockPickler.save_dict(pickler, obj) | None | None
2024-05-12 01:30:04.499039 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/usr/local/lib/python3.8/pickle.py", line 971, in save_dict | None | None
2024-05-12 01:30:04.499040 Romance Daylight Time | rxh-prod-botmanagerwebapp |      self._batch_setitems(obj.items()) | None | None
2024-05-12 01:30:04.499040 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/usr/local/lib/python3.8/pickle.py", line 997, in _batch_setitems | None | None
2024-05-12 01:30:04.499041 Romance Daylight Time | rxh-prod-botmanagerwebapp |      save(v) | None | None
2024-05-12 01:30:04.499041 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/home/reflexuser/venv/lib/python3.8/site-packages/dill/_dill.py", line 414, in save | None | None
2024-05-12 01:30:04.499042 Romance Daylight Time | rxh-prod-botmanagerwebapp |      StockPickler.save(self, obj, save_persistent_id) | None | None
2024-05-12 01:30:04.499042 Romance Daylight Time | rxh-prod-botmanagerwebapp |    File "/usr/local/lib/python3.8/pickle.py", line 578, in save | None | None
2024-05-12 01:30:04.499043 Romance Daylight Time | rxh-prod-botmanagerwebapp |      rv = reduce(self.proto) | None | None
2024-05-12 01:30:04.499043 Romance Daylight Time | rxh-prod-botmanagerwebapp |  TypeError: cannot pickle 'cryptography.hazmat.bindings._rust.openssl.rsa.RSAPrivateKey' object | None | None

To Reproduce

Deploy application with this requirement.txt

reflex==0.4.9
cryptography==42.0.7
paramiko==3.4.0

Expected behavior Application should run like in localhost:3000 mode

Specifics (please complete the following information):

ali-zakaria commented 5 months ago

Work-around: Use subprocess and native ssh instead of paramiko

        cmd = 'ssh -o StrictHostKeyChecking=accept-new -i "%s" %s@%s "%s"' % (ssh.server.pkeyPath, ssh.server.username, ssh.server.hostname, cmd_to_execute)
        p = subprocess.Popen(cmd,
                           shell=True,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
        ssh_stdout, ssh_stderr = p.communicate()
masenf commented 5 months ago

Are you keeping any ssh related objects in the State? That pickling error is coming from reflex trying to pickle the state for redis (prod mode)

ali-zakaria commented 5 months ago

Yes I am reading a bench of ssh keys stored in text files and creating paramiko.RSAKey objects at initialization and keeping it in a dict

masenf commented 5 months ago

Are the SSH keys per user of the app or are they shared among users of the app?

ali-zakaria commented 5 months ago

The goal of the app is to manage ~100 servers. Each server RSA key is stored as plain text in App's server uploaded_files/ folder (self hosting)

Users (administrators of the servers) log to the app and choose the server they want to configure from a dropdown menu, at this point, the app reads the RSA key and opens an SSH connection to the chosen server