Skip to content

Commit

Permalink
Merge pull request #1407 from CompassionCH/devel-jelastic
Browse files Browse the repository at this point in the history
Switch SAMBA by SFTP to connect to nas
  • Loading branch information
eicher31 authored Sep 9, 2021
2 parents cfd63ab + 920c9e3 commit 6abb905
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 371 deletions.
2 changes: 1 addition & 1 deletion partner_compassion/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"l10n_ch_zip", # oca_addon/l10n_switzerland
"web_view_google_map" # oca_addon/web_view_google_map
],
"external_dependencies": {"python": ["pandas", "pyminizip", "magic"]},
"external_dependencies": {"python": ["pandas", "pyminizip", "magic", "pysftp"]},
"data": [
"security/ir.model.access.csv",
"security/criminal_record_groups.xml",
Expand Down
61 changes: 37 additions & 24 deletions partner_compassion/models/partner_compassion.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
MAGIC_INSTALLED = True
import pyminizip
import csv
from smb.SMBConnection import SMBConnection
from smb.smb_structs import OperationFailure
import pysftp
from pysftp import RSAKey
except ImportError:
logger.warning("Please install python dependencies.", exc_info=True)

Expand Down Expand Up @@ -644,18 +644,16 @@ def _secure_save_data(self):
inside a password-protected ZIP file.
:return: None
"""
smb_conn = self._get_smb_connection()
if smb_conn and smb_conn.connect(SmbConfig.smb_ip, SmbConfig.smb_port):
sftp = self._get_sftp_connection()
if sftp:
config_obj = self.env["ir.config_parameter"].sudo()
share_nas = config_obj.get_param("partner_compassion.share_on_nas")
store_path = config_obj.get_param("partner_compassion.store_path")
src_zip_file = tempfile.NamedTemporaryFile()
attrs = smb_conn.retrieveFile(share_nas, store_path, src_zip_file)
file_size = attrs[1]
file_size = sftp.getfo(store_path, src_zip_file)
if file_size:
src_zip_file.flush()
zip_dir = tempfile.mkdtemp()
pyminizip.uncompress(src_zip_file.name, SmbConfig.file_pw, zip_dir, 0)
pyminizip.uncompress(src_zip_file.name, SftpConfig.file_pw, zip_dir, 0)
csv_path = zip_dir + "/partner_data.csv"
with open(csv_path, "a", newline="", encoding="utf-8") as csv_file:
csv_writer = csv.writer(csv_file)
Expand All @@ -668,29 +666,44 @@ def _secure_save_data(self):
]
)
dst_zip_file = tempfile.NamedTemporaryFile()
pyminizip.compress(
csv_path, "", dst_zip_file.name, SmbConfig.file_pw, 5
)
pyminizip.compress(csv_path, "", dst_zip_file.name, SftpConfig.file_pw, 5)
try:
smb_conn.storeFile(share_nas, store_path, dst_zip_file)
except OperationFailure:
sftp.putfo(dst_zip_file, store_path)
except Exception:
logger.error(
"Couldn't store secure partner data on NAS. "
"Please do it manually by replicating the following "
"file: " + dst_zip_file.name
)
finally:
src_zip_file.close()
dst_zip_file.close()

def _get_smb_connection(self):
def _get_sftp_connection(self):
"""" Retrieve configuration SMB """
if not (
SmbConfig.smb_user
and SmbConfig.smb_pass
and SmbConfig.smb_ip
and SmbConfig.smb_port
SftpConfig.username
and SftpConfig.password
and SftpConfig.host
and SftpConfig.port
):
return False
else:
return SMBConnection(SmbConfig.smb_user, SmbConfig.smb_pass, "odoo", "nas")

cnopts = pysftp.CnOpts()

try:
key_data = self.env.ref("sbc_switzerland.nas_ssh_key").value
key = RSAKey(data=base64.decodebytes(key_data.encode('utf-8')))
cnopts.hostkeys.add(SftpConfig.host, "ssh-rsa", key)
except:
cnopts.hostkeys = None
logger.warning(
"No hostkeys defined in StfpConnection. Connection will be unsecured. "
"Please configure parameter sbc_switzerland.nas_ssh_key with ssh_key data.")

return pysftp.Connection(username=SftpConfig.username, password=SftpConfig.password, port=SftpConfig.port,
host=SftpConfig.host, cnopts=cnopts)

def _get_active_sponsorships_domain(self):
"""
Expand Down Expand Up @@ -730,11 +743,11 @@ def _notify_prepare_email_values(self, message):
return mail_values


class SmbConfig:
class SftpConfig:
"""" Little class who contains SMB configuration """

smb_user = config.get("smb_user")
smb_pass = config.get("smb_pwd")
smb_ip = config.get("smb_ip")
smb_port = int(config.get("smb_port", 0))
username = config.get("sftp_user")
password = config.get("sftp_pwd")
host = config.get("sftp_ip")
port = int(config.get("sftp_port", 22))
file_pw = config.get("partner_data_password")
8 changes: 4 additions & 4 deletions sbc_switzerland/data/import_config_templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
<!-- production config -->
<record id="standard" model="import.letter.config">
<field name="name">Standard</field>
<field name="import_folder_path">\Scan Lettres S2B\Standard</field>
<field name="import_folder_path">Standard</field>
</record>
<record id="standard_with_attachments" model="import.letter.config">
<field name="name">Standard with attachments</field>
<field name="physical_attachments">sent_by_mail</field>
<field name="import_folder_path">\Scan Lettres S2B\Standard_With_Attachments</field>
<field name="import_folder_path">Standard_With_Attachments</field>
</record>
<record id="no_standard" model="import.letter.config">
<field name="name">No Standard</field>
<field name="template_id" ref="sbc_compassion.default_template"/>
<field name="import_folder_path">\Scan Lettres S2B\No_Standard</field>
<field name="import_folder_path">No_Standard</field>
</record>
<record id="no_standard_with_attachments" model="import.letter.config">
<field name="name">No Standard with attachment</field>
<field name="template_id" ref="sbc_compassion.default_template"/>
<field name="physical_attachments">sent_by_mail</field>
<field name="import_folder_path">\Scan Lettres S2B\No_Standard_With_Attachments</field>
<field name="import_folder_path">No_Standard_With_Attachments</field>
</record>
<record id="web_letter" model="import.letter.config">
<field name="name">Web Letter</field>
Expand Down
6 changes: 6 additions & 0 deletions sbc_switzerland/data/nas_parameters.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<odoo>
<data noupdate="1">

<record id="nas_ssh_key" model="ir.config_parameter">
<field name="key">sbc_switzerland.nas_ssh_key</field>
<field name="value">TO_CHANGE</field>
</record>

<record id="nas_share_name" model="ir.config_parameter">
<field name="key">sbc_switzerland.nas_share_name</field>
<field name="value">GP</field>
Expand Down
4 changes: 2 additions & 2 deletions sbc_switzerland/data/scan_letter_params.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<!-- production config -->
<record id="share_on_nas" model="ir.config_parameter">
<field name="key">sbc_switzerland.share_on_nas</field>
<field name="value">Public</field>
<field name="value">/share/Scan</field>
</record>
<record id="scan_letter_imported" model="ir.config_parameter">
<field name="key">sbc_switzerland.scan_letter_imported</field>
<field name="value">\Scan Lettres S2B\Imports\</field>
<field name="value">Imports</field>
</record>
<record id="scan_letter_done" model="ir.config_parameter">
<field name="key">sbc_switzerland.scan_letter_done</field>
Expand Down
47 changes: 28 additions & 19 deletions sbc_switzerland/models/correspondence.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
from odoo.exceptions import UserError
from odoo.addons.sbc_compassion.models.correspondence_page import BOX_SEPARATOR


logger = logging.getLogger(__name__)

try:
from PyPDF2 import PdfFileReader, PdfFileWriter
from smb.SMBConnection import SMBConnection
import pysftp
except ImportError:
logger.warning("Please install pyPdf and smb.")

Expand Down Expand Up @@ -84,7 +84,7 @@ def create(self, vals):
correspondence = super(
Correspondence, self.with_context(no_comm_kit=True)
).create(vals)
correspondence.send_local_translate()
# correspondence.send_local_translate()
else:
correspondence = super().create(vals)

Expand Down Expand Up @@ -412,26 +412,35 @@ def _transfer_file_on_nas(self, file_name):
"""
self.ensure_one()
# Retrieve configuration
smb_user = config.get("smb_user")
smb_pass = config.get("smb_pwd")
smb_ip = config.get("smb_ip")
smb_port = int(config.get("smb_port", 0))
if not (smb_user and smb_pass and smb_ip and smb_port):
raise Exception("No config SMB in file .conf")
sftp_user = config.get("sftp_user")
sftp_pass = config.get("sftp_pwd")
sftp_ip = config.get("sftp_ip")
sftp_port = int(config.get("sftp_port", 0))
if not (sftp_user and sftp_pass and sftp_ip and sftp_port):
raise Exception("No config SFTP in file .conf")

cnopts = pysftp.CnOpts()
# TODO import hostkey for compassion nas
cnopts.hostkeys = None

if cnopts.hostkeys is None:
logger.warning("No hostkeys defined in StfpConnection. Connection will be unsecured.")

# Copy file in the imported letter folder
smb_conn = SMBConnection(smb_user, smb_pass, "openerp", "nas")
if smb_conn.connect(smb_ip, smb_port):
file_ = BytesIO(self.get_image())
nas_share_name = self.env.ref("sbc_switzerland.nas_share_name").value
try:
sftp_conn = pysftp.Connection(host=sftp_ip, password=sftp_pass, username=sftp_user,
port=sftp_port, cnopts=cnopts)
except Exception:
raise UserError(_("Connection to NA failed."))

nas_letters_store_path = (
self.env.ref(
"sbc_switzerland.nas_letters_store_path").value + file_name
)
smb_conn.storeFile(nas_share_name, nas_letters_store_path, file_)
else:
raise UserError(_("Connection to NAS failed"))
with sftp_conn as sftp:
file_ = BytesIO(self.get_image())
with sftp.cd(self.env.ref("sbc_switzerland.nas_share_name").value):
nas_letters_store_path = (
self.env.ref(
"sbc_switzerland.nas_letters_store_path").value + file_name
)
sftp.putfo(file_, nas_letters_store_path)

# CRON Methods
##############
Expand Down
Loading

0 comments on commit 6abb905

Please sign in to comment.