Skip to content

Commit

Permalink
GH zatosource#282 - Made SFTP connections available under self.out.sftp.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuch committed Mar 2, 2019
1 parent 48485aa commit fff0f7b
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ def text_func(config):

# ################################################################################################################################
# ################################################################################################################################

60 changes: 48 additions & 12 deletions code/zato-server/src/zato/server/base/worker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from __future__ import absolute_import, division, print_function, unicode_literals

# stdlib
import logging, inspect, os, sys
import logging
import inspect
import os
import sys
from copy import deepcopy
from datetime import datetime
from errno import ENOENT
from inspect import isclass
Expand Down Expand Up @@ -71,6 +75,7 @@
from zato.server.connection.sap import SAPWrapper
from zato.server.connection.search.es import ElasticSearchAPI, ElasticSearchConnStore
from zato.server.connection.search.solr import SolrAPI, SolrConnStore
from zato.server.connection.sftp import SFTPIPCFacade
from zato.server.connection.sms.twilio import TwilioAPI, TwilioConnStore
from zato.server.connection.stomp import ChannelSTOMPConnStore, STOMPAPI, channel_main_loop as stomp_channel_main_loop, \
OutconnSTOMPConnStore
Expand Down Expand Up @@ -219,9 +224,6 @@ def init(self):
# Generic connections - WSX outconns
self.outconn_wsx = {}

# Generic connections - SFTP
self.outconn_sftp = {}

# Maps generic connection types to their API handler objects
self.generic_conn_api = {
COMMON_GENERIC.CONNECTION.TYPE.OUTCONN_WSX: self.outconn_wsx,
Expand Down Expand Up @@ -285,9 +287,10 @@ def init(self):
# API keys
self.update_apikeys()

# Request dispatcher - matches URLs, checks security and dispatches HTTP
# requests to services.
# SFTP - attach handles to connections to each ConfigDict now that all their configuration is ready
self.init_sftp()

# Request dispatcher - matches URLs, checks security and dispatches HTTP requests to services.
self.request_dispatcher = RequestDispatcher(simple_io_config=self.worker_config.simple_io,
return_tracebacks=self.server.return_tracebacks, default_error_message=self.server.default_error_message)
self.request_dispatcher.url_data = URLData(
Expand Down Expand Up @@ -477,6 +480,14 @@ def init_ftp(self):
self.worker_config.out_ftp = FTPStore()
self.worker_config.out_ftp.add_params(config_list)


def init_sftp(self):
""" Each outgoing SFTP connection requires a connection handle to be attached here,
later, in run-time, this is the 'conn' parameter available via self.out[name].conn.
"""
for value in self.worker_config.out_sftp.values():
value['conn'] = SFTPIPCFacade(self.server, value['config'])

def init_http_soap(self):
""" Initializes plain HTTP/SOAP connections.
"""
Expand Down Expand Up @@ -930,10 +941,36 @@ def init_generic_connections_config(self):
outconn_wsx_map[_generic_msg.delete] = self._delete_generic_connection

# Outgoing SFTP connections require for a different API to be called (provided by ParallelServer)
outconn_sftp_map[_generic_msg.create] = self.server.connector_sftp.invoke_connector
outconn_sftp_map[_generic_msg.edit] = self.server.connector_sftp.invoke_connector
outconn_sftp_map[_generic_msg.delete] = self.server.connector_sftp.invoke_connector
outconn_sftp_map[_generic_msg.change_password] = self.server.connector_sftp.invoke_connector
outconn_sftp_map[_generic_msg.create] = self._on_outconn_sftp_create
outconn_sftp_map[_generic_msg.edit] = self._on_outconn_sftp_edit
outconn_sftp_map[_generic_msg.delete] = self._on_outconn_sftp_delete

# ################################################################################################################################

def _on_outconn_sftp_create(self, msg):
connector_msg = deepcopy(msg)
self.worker_config.out_sftp[msg.name] = msg
self.worker_config.out_sftp[msg.name].conn = SFTPIPCFacade(self.server, msg)
return self.server.connector_sftp.invoke_connector(connector_msg)

# ################################################################################################################################

def _on_outconn_sftp_edit(self, msg):
connector_msg = deepcopy(msg)
del self.worker_config.out_sftp[msg.old_name]
return self._on_outconn_sftp_create(connector_msg)

# ################################################################################################################################

def _on_outconn_sftp_delete(self, msg):
connector_msg = deepcopy(msg)
del self.worker_config.out_sftp[msg.name]
return self.server.connector_sftp.invoke_connector(connector_msg)

# ################################################################################################################################

def _on_outconn_sftp_change_password(self, msg):
raise NotImplementedError('No password for SFTP connections can be set')

# ################################################################################################################################

Expand All @@ -943,8 +980,7 @@ def _get_generic_impl_func(self, msg, *args, **kwargs):
for instance, in the case of SFTP, it uses subprocesses and a different management API.
"""
func_map = self.generic_impl_func_map[msg['type_']]
func = func_map[msg['action']]
return func#(msg, *args, **kwargs)
return func_map[msg['action']]

# ################################################################################################################################

Expand Down
36 changes: 36 additions & 0 deletions code/zato-server/src/zato/server/connection/sftp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-

"""
Copyright (C) 2019, Zato Source s.r.o. https://zato.io
Licensed under LGPLv3, see LICENSE.txt for terms and conditions.
"""

from __future__ import absolute_import, division, print_function, unicode_literals

# ################################################################################################################################
# ################################################################################################################################

class SFTPIPCFacade(object):
""" Provides servers and services with access to SFTP resources.
"""
def __init__(self, server, config):
# type: (object, dict) -> None
self.server = server
self.config = config

# ################################################################################################################################

def ping(self, msg):
# type: (dict) -> None

print()
print()

print('111 SFTPIPCFacade', msg)

print()
print()

# ################################################################################################################################
# ################################################################################################################################
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def handle(self):
else:
model = self._new_zato_instance_with_cluster(ModelGenericConn)

# This will be needed in case this is a rename
old_name = model.name

for key, value in conn_dict.items():
setattr(model, key, value)

Expand All @@ -94,6 +97,7 @@ def handle(self):
self.response.payload.id = instance.id
self.response.payload.name = instance.name

data['old_name'] = old_name
data['action'] = GENERIC.CONNECTION_EDIT.value if self.is_edit else GENERIC.CONNECTION_CREATE.value
data['id'] = instance.id
self.broker_client.publish(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,10 @@

from __future__ import absolute_import, division, print_function, unicode_literals

# stdlib
from contextlib import closing
from time import time
from uuid import uuid4

# Python 2/3 compatibility
from six import add_metaclass

# Zato
from zato.common.broker_message import OUTGOING
from zato.common.odb.model import OutgoingSAP
from zato.common.odb.query import out_sap_list
from zato.common.util import ping_sap
from zato.server.service.internal import AdminService, AdminSIO, ChangePasswordBase
from zato.server.service.meta import CreateEditMeta, DeleteMeta, GetListMeta
from zato.server.service.internal import AdminService, AdminSIO

# ################################################################################################################################
# ################################################################################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
# stdlib
import logging

# Django
from django.http import HttpResponse, HttpResponseServerError

# Zato
from zato.admin.web.forms import ChangePasswordForm
from zato.admin.web.forms.definition.jms_wmq import CreateForm, EditForm
Expand Down

0 comments on commit fff0f7b

Please sign in to comment.