Пример #1
0
    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))
Пример #2
0
    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
Пример #3
0
    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))
Пример #4
0
    def handle(self):

        # Let's prepare as much as we can upfront.
        sec_def = self.server.worker_store.basic_auth_get(
            'admin.invoke').config
        channel = self.server.worker_store.get_channel_rest(
            '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']
                        response = b64decode(response)
                        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)
# -*- 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

Пример #6
0
    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))
 def process_request(self, req):
     req.zato_client = AnyServiceInvoker('http://localhost:17010',
                                         '/django/sample',
                                         ('django-app', 'django-password'))