class _RemoteServer(_Server): """ API through which it is possible to invoke services directly on other remote servers or clusters. """ def __init__(self, cluster_id, cluster_name, name, preferred_address, port, crypto_use_tls, api_password): self.cluster_id = cluster_id self.cluster_name = cluster_name self.name = name self.preferred_address = preferred_address self.port = port self.crypto_use_tls = crypto_use_tls self.api_password = api_password self.address = 'http{}://{}:{}'.format( 's' if self.crypto_use_tls else '', self.preferred_address, self.port) self.invoker = AnyServiceInvoker(self.address, '/zato/internal/invoke', (api_user, self.api_password)) def invoke(self, service, request=None, *args, **kwargs): response = self.invoker.invoke(service, request, *args, **kwargs) if response.ok: return response.data else: raise Exception(response.details) def invoke_async(self, service, request=None, *args, **kwargs): return self.invoker.invoke_async(service, request, *args, **kwargs)
def handle(self): # Let's prepare as much as we can upfront. sec_def = self.worker_store.basic_auth_get('admin.invoke').config channel = self.worker_store.get_channel_plain_http('admin.invoke.json') out = {} with closing(self.odb.session()) as session: for item in server_list(session, self.server.cluster_id, False): server_info = out.setdefault(item.name, {}) server_info['cluster_name'] = item.cluster_name server_info['up_mod_date'] = item.up_mod_date.isoformat() if item.up_status == SERVER_UP_STATUS.RUNNING else None server_info['last_join_mod_date'] = item.last_join_mod_date.isoformat() if \ item.last_join_status == SERVER_JOIN_STATUS.ACCEPTED else None for name in 'id', 'name', 'bind_host', 'bind_port', 'last_join_status', 'last_join_mod_by', 'up_status': server_info[name] = getattr(item, name) if item.up_status == SERVER_UP_STATUS.RUNNING: client = AnyServiceInvoker( 'http://{}:{}'.format(item.bind_host, item.bind_port), channel.url_path, (sec_def.username, sec_def.password)) response = client.invoke('zato.info.get-server-info') if response.ok: response = loads(response.inner.text)['zato_service_invoke_response']['response'].decode('base64') response = loads(response)['response'] server_info['info'] = loads(response['info']) else: self.logger.warn(response) self.response.content_type = 'application/json' self.response.payload = dumps(out)
def handle(self): # Let's prepare as much as we can upfront. sec_def = self.worker_store.basic_auth_get('admin.invoke').config channel = self.worker_store.get_channel_plain_http('admin.invoke.json') out = {} with closing(self.odb.session()) as session: for item in server_list(session, self.server.cluster_id, None, None, False): server_info = out.setdefault(item.name, {}) server_info['cluster_name'] = item.cluster_name server_info['up_mod_date'] = item.up_mod_date.isoformat() if item.up_status == SERVER_UP_STATUS.RUNNING else None server_info['last_join_mod_date'] = item.last_join_mod_date.isoformat() if \ item.last_join_status == SERVER_JOIN_STATUS.ACCEPTED else None for name in 'id', 'name', 'bind_host', 'bind_port', 'last_join_status', 'last_join_mod_by', 'up_status': server_info[name] = getattr(item, name) if item.up_status == SERVER_UP_STATUS.RUNNING: client = AnyServiceInvoker( 'http://{}:{}'.format(item.bind_host, item.bind_port), channel.url_path, (sec_def.username, sec_def.password)) response = client.invoke('zato.info.get-server-info') if response.ok: response = loads(response.inner.text)['zato_service_invoke_response']['response'].decode('base64') response = loads(response)['response'] server_info['info'] = loads(response['info']) else: self.logger.warn(response) self.response.content_type = 'application/json' self.response.payload = dumps(out)
class _RemoteServer(_Server): """ API through which it is possible to invoke services directly on other remote servers or clusters. """ repr_to_avoid = ['api_password'] def __init__(self, cluster_id, cluster_name, name, preferred_address, port, crypto_use_tls, api_password, up_status): self.cluster_id = cluster_id self.cluster_name = cluster_name self.name = name self.preferred_address = preferred_address self.port = port self.crypto_use_tls = crypto_use_tls self.api_password = api_password self.up_status = up_status self.address = 'http{}://{}:{}'.format( 's' if self.crypto_use_tls else '', self.preferred_address, self.port) self.invoker = AnyServiceInvoker(self.address, '/zato/internal/invoke', (api_user, self.api_password)) self.ping_address = '{}/zato/ping'.format(self.address) self.ping_timeout = 2 # ################################################################################################################################ def invoke(self, service, request=None, *args, **kwargs): # Ping the remote server to quickly find out if it is still available requests_get(self.ping_address, timeout=self.ping_timeout) response = self.invoker.invoke(service, request, *args, **kwargs) if response.ok: return response.data else: raise Exception(response.details) # ################################################################################################################################ def invoke_all_pids(self, service, request=None, *args, **kwargs): # Ping the remote server to quickly find out if it is still available requests_get(self.ping_address, timeout=self.ping_timeout) return self.invoker.invoke(service, request, all_pids=True, *args, **kwargs) # ################################################################################################################################ def invoke_async(self, service, request=None, *args, **kwargs): return self.invoker.invoke_async(service, request, *args, **kwargs)
def __init__(self, cluster_id, cluster_name, name, preferred_address, port, crypto_use_tls, api_password): self.cluster_id = cluster_id self.cluster_name = cluster_name self.name = name self.preferred_address = preferred_address self.port = port self.crypto_use_tls = crypto_use_tls self.api_password = api_password self.address = 'http{}://{}:{}'.format( 's' if self.crypto_use_tls else '', self.preferred_address, self.port) self.invoker = AnyServiceInvoker(self.address, '/zato/internal/invoke', (api_user, self.api_password))
def __init__(self, logger=None): try: self.address = 'http://localhost:11223' self.path = '/zato/admin/invoke' self.auth = ('admin.invoke', os.environ["ZATOPASS"]) self.client = AnyServiceInvoker(self.address, self.path, self.auth) self.channeldict = None self.loggerobj = logger except KeyError: print "Please set the environment variable ZATOPASS" self.log('Please set the environment variable ZATOPASS') sys.exit(1)
def process_request(self, req): for dont_require_login in self.dont_require_login: if req.path.startswith(dont_require_login): return None if req.path != self.require_login_path and req.user.is_anonymous(): if req.POST: return login(req) else: return HttpResponseRedirect('{}?next={}'.format( self.require_login_path, req.path)) # Makes each Django view have an access to a 'zato.odb' attribute of the # request object. The attribute is an SQLAlchemy session to the database # defined in app's settings.py req.zato = Bunch() req.zato.odb = SASession() req.zato.args = Bunch() # Arguments read from URL resolved_kwargs = resolve(req.path).kwargs req.zato.id = resolved_kwargs.get('id') req.zato.cluster_id = req.GET.get('cluster') or req.POST.get('cluster_id') or \ resolved_kwargs.get('cluster_id') or resolved_kwargs.get('cluster') if req.zato.cluster_id: req.zato.cluster = req.zato.odb.query(Cluster).filter_by( id=req.zato.cluster_id).one() url = 'http://{}:{}'.format(req.zato.cluster.lb_host, req.zato.cluster.lb_port) auth = (ADMIN_INVOKE_NAME, ADMIN_INVOKE_PASSWORD) req.zato.client = AnyServiceInvoker(url, ADMIN_INVOKE_PATH, auth, to_bunch=True) req.zato.clusters = req.zato.odb.query(Cluster).order_by('name').all() req.zato.choose_cluster_form = ChooseClusterForm( req.zato.clusters, req.GET) if not req.user.is_anonymous(): try: user_profile = UserProfile.objects.get(user=req.user) except UserProfile.DoesNotExist: user_profile = UserProfile(user=req.user) user_profile.save() req.zato.user_profile = user_profile else: req.zato.user_profile = None
def execute(self, args): repo_dir = os.path.join( os.path.abspath(os.path.join(self.original_dir, args.path)), 'config', 'repo') config = get_config(repo_dir, 'server.conf') headers = {} if args.headers: for pair in args.headers.strip().split(';'): k, v = pair.strip().split('=', 1) headers[k] = v repo_dir = os.path.join(os.path.abspath(os.path.join(args.path)), 'config', 'repo') config = get_config(repo_dir, 'server.conf') client = AnyServiceInvoker( 'http://{}'.format(config.main.gunicorn_bind), args.url_path, self.get_server_client_auth(config, repo_dir), max_response_repr=int(args.max_response_repr), max_cid_repr=int(args.max_cid_repr)) # Prevents attempts to convert/escape XML into JSON to_json = True if args.data_format == DATA_FORMAT.JSON else False func = client.invoke_async if args. async else client.invoke response = func(args.name, args.payload, headers, args.channel, args.data_format, args.transport, to_json=to_json) if response.ok: self.logger.info(response.data or '(None)') else: self.logger.error(response.details) if args.verbose: self.logger.debug('inner.text:[{}]'.format(response.inner.text)) self.logger.debug('response:[{}]'.format(response))
# -*- coding: utf-8 -*- # Zato from zato.client import AnyServiceInvoker, JSONClient # Our app from settings import CLIENT_ADDRESS, CLIENT_CREDENTIALS, CLIENT_PATH_ANY, CLIENT_PATH_JSON client_any = AnyServiceInvoker(CLIENT_ADDRESS, CLIENT_PATH_ANY, CLIENT_CREDENTIALS) client_json = JSONClient(CLIENT_ADDRESS, CLIENT_PATH_JSON, CLIENT_CREDENTIALS) class ZatoMiddleware(object): def process_request(self, req): req.client_any = client_any req.client_json = client_json
def execute(self, args): repo_dir = os.path.join( os.path.abspath(os.path.join(self.original_dir, args.path)), 'config', 'repo') config = get_config(repo_dir, 'server.conf') priv_key_location = os.path.abspath( os.path.join(repo_dir, config.crypto.priv_key_location)) cm = CryptoManager(priv_key_location=priv_key_location) cm.load_keys() engine_args = Bunch() engine_args.odb_type = config.odb.engine engine_args.odb_user = config.odb.username engine_args.odb_password = cm.decrypt(config.odb.password) engine_args.odb_host = config.odb.host engine_args.odb_db_name = config.odb.db_name engine = self._get_engine(engine_args) session = self._get_session(engine) auth = None headers = {} with closing(session) as session: cluster = session.query(Server).\ filter(Server.token == config.main.token).\ one().cluster channel = session.query(HTTPSOAP).\ filter(HTTPSOAP.cluster_id == cluster.id).\ filter(HTTPSOAP.url_path == args.url_path).\ one() if channel.security_id: security = session.query(HTTPBasicAuth).\ filter(HTTPBasicAuth.id == channel.security_id).\ first() if security: auth = (security.username, security.password) if args.headers: for pair in args.headers.strip().split(';'): k, v = pair.strip().split('=', 1) headers[k] = v client = AnyServiceInvoker( 'http://{}'.format(config.main.gunicorn_bind), args.url_path, auth, max_response_repr=int(args.max_response_repr), max_cid_repr=int(args.max_cid_repr)) func = client.invoke_async if args. async else client.invoke response = func(args.name, args.payload, headers, args.channel, args.data_format, args.transport) if response.ok: self.logger.info(response.data or '(None)') else: self.logger.error(response.details) if args.verbose: self.logger.debug('inner.text:[{}]'.format(response.inner.text)) self.logger.debug('response:[{}]'.format(response))
def process_request(self, req): req.zato_client = AnyServiceInvoker( 'http://%s:%s' % (ZATO_HOST, ZATO_PORT), ZATO_PATH, (ZATO_USER, ZATO_PASSWD))
class ChannelUtils: def __init__(self, logger=None): try: self.address = 'http://localhost:11223' self.path = '/zato/admin/invoke' self.auth = ('admin.invoke', os.environ["ZATOPASS"]) self.client = AnyServiceInvoker(self.address, self.path, self.auth) self.channeldict = None self.loggerobj = logger except KeyError: print "Please set the environment variable ZATOPASS" self.log('Please set the environment variable ZATOPASS') sys.exit(1) def get_channels(self): self.channeldict = {} payload = {"cluster_id": 1, "connection": "channel", "transport": "plain_http"} response = self.client.invoke('zato.http-soap.get-list', payload=payload) if response.ok: d = response.data for c in d: self.channeldict[c[ u'name']]=c[ u'id'] return self.channeldict else: self.log('ERROR getting channel dictionary') return {} def delete_channel(self, id): self.channeldict = {} payload = {"id": id} response = self.client.invoke('zato.http-soap.delete', payload=payload) if response.ok: pass else: print(response.details) return {} def add_channel(self, channelname, servicename, url, method, skipifexists=False): if self.channeldict is None: self.get_channels() if skipifexists == False and channelname in self.channeldict: self.delete_channel(id=self.channeldict[channelname]) payload = {} payload[u'connection'] = u'channel' payload[u'name'] = channelname payload[u'cluster_id'] = 1 payload[u'transport'] = u'plain_http' payload[u'url_path'] = url payload[u'service'] = servicename payload[u'method'] = method payload[u'data_format'] = u'json' payload[u'is_active']=True payload[u'is_internal']=False response = self.client.invoke('zato.http-soap.create', payload=payload) if response.ok: pass else: print(response.details) return {} def log(self, msg): if self.loggerobj is not None: self.loggerobj.info(msg) else: print msg def configure_service_file(self, filepath): source = '' self.log('Processing {}'.format(filepath)) with open(filepath, 'r') as f: source = f.read() p = ast.parse(source) classes = [node.name for node in ast.walk(p) if isinstance(node, ast.ClassDef)] for classname in classes: if classname != 'SimpleIO': classobj = load_from_file(filepath, classname) namemethod = getattr(classobj, 'get_name') servicename = namemethod() channelmethod = getattr(classobj, 'get_channels') channels = channelmethod() for channel in channels: self.log('Adding channel {}'.format(channel['name'])) self.add_channel(channel['name'], servicename, channel['path'], channel['method'])
def process_request(self, req): req.zato_client = AnyServiceInvoker('http://localhost:17010', '/django/sample', ('django-app', 'django-password'))