def test_cache_checks_type():
    cache = InMemoryCache()

    async def foo():
        pass

    with pytest.raises(TypeError):
        cache.add("x", foo())
예제 #2
0
def test_cache_checks_type():
    cache = InMemoryCache()

    async def foo():
        pass

    with pytest.raises(TypeError):
        cache.add('x', foo())
def test_load_cache(event_loop):
    cache = InMemoryCache()
    transport = asyncio.AsyncTransport(loop=event_loop, cache=cache)

    with aioresponses() as m:
        m.get("http://tests.python-zeep.org/test.xml", body="x")
        result = transport.load("http://tests.python-zeep.org/test.xml")
        assert result == b"x"

    assert cache.get("http://tests.python-zeep.org/test.xml") == b"x"
예제 #4
0
def test_load_cache(event_loop):
    cache = InMemoryCache()
    transport = asyncio.AsyncTransport(loop=event_loop, cache=cache)

    with aioresponses() as m:
        m.get('http://tests.python-zeep.org/test.xml', body='x')
        result = transport.load('http://tests.python-zeep.org/test.xml')
        assert result == b'x'

    assert cache.get('http://tests.python-zeep.org/test.xml') == b'x'
def test_load_cache(httpx_mock):
    cache = InMemoryCache()
    transport = AsyncTransport(cache=cache)

    httpx_mock.add_response(url="http://tests.python-zeep.org/test.xml",
                            data="x")
    result = transport.load("http://tests.python-zeep.org/test.xml")
    assert result == b"x"

    assert cache.get("http://tests.python-zeep.org/test.xml") == b"x"
예제 #6
0
async def main():
    client = AsyncClient(
        wsdl="http://localhost:8080/say_hello/?WSDL",
        transport=AsyncTransport(cache=InMemoryCache(timeout=None)),
    )
    await send_messages(client)
    await client.transport.aclose()
예제 #7
0
 def __init__(self, timeout=None):
     self.client = Zeep(
         wsdl=self.url,
         transport=Transport(cache=InMemoryCache()),
     )
     if timeout is not None:
         self.client.operation_timeout = self.client.timeout = timeout
예제 #8
0
def get_SOAP_client(url, verify=True):
    # http://docs.python-zeep.org/en/master/transport.html#ssl-verification
    # Disable SSL cert verification meanwhile a better way is found
    session = requests.Session()
    session.verify = verify
    transport = Transport(cache=InMemoryCache(), session=session)
    return Client(url, transport=transport)
예제 #9
0
 def __init__(self):
     session = Session()
     session.auth = HTTPBasicAuth(settings.COA_USERNAME, settings.COA_PASSWORD)
     self.client = Client(
         settings.COA_SEARCH_ENDPOINT,
         transport=Transport(cache=InMemoryCache(), session=session),
     )
예제 #10
0
def setup_zeepclient(wsdl, serviceurl, rootcert, localstore, key):
    session = Session()
    session.verify = rootcert
    transport = Transport(cache=InMemoryCache(), session=session)
    client = Client(wsdl=wsdl, transport=transport)
    #   Zeep takes the service url from wsdl by default, hence static wsdl demands the below override
    client.service._binding_options["address"] = serviceurl
    localstore[key] = client
예제 #11
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        session = Session()
        session.auth = HTTPBasicAuth(settings.NAV_USER, settings.NAV_PASS)

        self.client = zeep.Client(settings.NAV_HOST,
                                  transport=Transport(session=session,
                                                      cache=InMemoryCache()))
예제 #12
0
def sessionend_handler(sender, **kwargs):
    # Ensure that if user logs out of FireCARES, then then his/her IMIS session
    # token is destroyed
    inst = kwargs.get('instance').get_decoded()
    if 'ibcToken' in inst:
        token = inst.get('ibcToken')
        transport = Transport(cache=InMemoryCache())
        imis = Client(settings.IMIS_SSO_SERVICE_URL, transport=transport)
        imis.service.DisposeSessionByUserToken(applicationInstance=1, userToken=token)
예제 #13
0
def main(args):
    if args.verbose:
        logging.config.dictConfig({
            'version': 1,
            'formatters': {
                'verbose': {
                    'format': '%(name)20s: %(message)s'
                }
            },
            'handlers': {
                'console': {
                    'level': 'DEBUG',
                    'class': 'logging.StreamHandler',
                    'formatter': 'verbose',
                },
            },
            'loggers': {
                'zeep': {
                    'level': 'DEBUG',
                    'propagate': True,
                    'handlers': ['console'],
                },
            }
        })

    if args.profile:
        import cProfile
        profile = cProfile.Profile()
        profile.enable()

    cache = SqliteCache() if args.cache else InMemoryCache()
    transport_kwargs = {'cache': cache}

    if args.no_verify:
        transport_kwargs['verify'] = False

    result = urlparse(args.wsdl_file)
    if result.username or result.password:
        transport_kwargs['http_auth'] = (result.username, result.password)

    transport = Transport(**transport_kwargs)
    st = time.time()
    client = Client(args.wsdl_file, transport=transport)
    logger.debug("Loading WSDL took %sms", (time.time() - st) * 1000)

    if args.profile:
        profile.disable()
        profile.dump_stats(args.profile)
    client.wsdl.dump()
예제 #14
0
    def __init__(self, ssu, client, service, transport=None, *args, **kwargs):
        self.response = None

        if not service:
            raise Exception('service - required')
        if not client:
            raise Exception('client - required')

        client = {
            ADDR_FIELDS[i]: val
            for i, val in enumerate(client.split('/'))
        }
        client.update({'objectType': 'SUBSYSTEM'})

        service = {
            ADDR_FIELDS[i]: val
            for i, val in enumerate(service.split('/'))
        }
        service.update({'objectType': 'SERVICE'})

        super().__init__(_get_wsdl_url(ssu, service),
                         transport=transport if transport else Transport(
                             InMemoryCache(timeout=60)),
                         *args,
                         **kwargs)

        self.transport.session.proxies.update({
            'http': ssu,
        })

        self.set_ns_prefix('xro', "http://x-road.eu/xsd/xroad.xsd")
        self.set_ns_prefix('iden', "http://x-road.eu/xsd/identifiers")

        self.set_default_soapheaders({
            'client': client,
            'service': service,
            'userId': client.get('subsystemCode'),
            'id': uuid.uuid4().hex,
            'protocolVersion': self._version,
        })
        _logger.debug('Default header (%s)', self._default_soapheaders)
예제 #15
0
    def get_client(self, url):

        # Si ya se inicializó el cliente especificado en client_name, lo devuelve. Si no, lo inicializa, setea y devuelve.
        # ej. -> https://<ITIMURL>/.../WSSessionService?wsdl -> wssessionservice
        client_name = url.split("/")[-1][:-5].lower()
        client = getattr(self, client_name, None)

        if client is None:
            settings = Settings(strict=False)
            s = requests.Session()
            s.verify = self.cert_path
            client = Client(
                url,
                settings=settings,
                transport=Transport(session=s, cache=InMemoryCache()),
            )
            # necesario porque los WSDL de SIM queman el puerto y no funciona con balanceador
            client.service._binding_options["address"] = url[:-5]
            setattr(self, client_name, client)

        return client
    def load_class_variables(cls, app_configs):
        """load_class_variables loads in the app_configs dict
        and assigned each value as a class variable.
        After loading, a zeep SOAP Client is created with our credentials

        :param app_configs: a dictionary containing key-value pairs used for
        setting up a client with Symantec DLP.
        :type app_configs: dict
        """
        cls.is_connected = False # Set is_connected to false initially
        
        validate_fields(['sdlp_host', 'sdlp_wsdl', 'sdlp_username', 'sdlp_password', 'sdlp_savedreportid', 'sdlp_incident_endpoint'], kwargs=app_configs)

        LOG.debug("Validated Mandatory app.configs for DLPSoapClient")

        cls.host = app_configs.get('sdlp_host')
        cls.wsdl = app_configs.get('sdlp_wsdl')
        # Gather the DLP User Name
        cls.dlp_username = app_configs.get('sdlp_username')
        # Gather the DLP User Password
        cls.dlp_password = app_configs.get('sdlp_password')

        # Gather the DLP Cert
        cls.dlp_cert = app_configs.get('sdlp_cafile', False)

        # Gather the DLP Saved Report ID
        cls.dlp_saved_report_id = app_configs.get('sdlp_savedreportid')

        # Gather the DLP Incident Endpoint
        cls.sdlp_incident_endpoint = app_configs.get('sdlp_incident_endpoint')

        cls.session = Session()
        # Use DLP Cert if provided or if None, set verify to false
        cls.session.verify = cls.dlp_cert
        cls.session.auth = SymantecAuth(cls.dlp_username, cls.dlp_password, cls.host)


        mimefile_abs_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            os.path.pardir, "data", "xmlmime.xml")
        # If the Xmlmime file was provided
        if os.path.isfile(mimefile_abs_path):
            LOG.info("A Local XML file was found in the data directory, %s \n Loading this into the cache", mimefile_abs_path)
            # Open it and add it to a Cache
            with open(mimefile_abs_path, mode="rb") as f:
                filecontent = f.read()
                dlp_cache = InMemoryCache()
                dlp_cache.add("http://www.w3.org/2005/05/xmlmime", filecontent)
                dlp_cache.add("https://www.w3.org/2005/05/xmlmime", filecontent)
            # Setup Transport with credentials and the cached mimefile
            cls.transport = zeep.Transport(session=cls.session, cache=dlp_cache)
        else:
            # Setup Transport with our credentials
            cls.transport = zeep.Transport(session=cls.session)

        try: # Try to create a soap_client from the wsdl and transport
            cls.soap_client = zeep.Client(wsdl=cls.wsdl, transport=cls.transport)
        except Exception as caught_exc: # We got an error when setting up a client, catch and release the error in logs so circuits doesn't stop
            # Put the traceback into DEBUG
            LOG.debug(traceback.format_exc())
            # Log the Connection error to the user
            LOG.error(u"Problem: %s", repr(caught_exc))
            LOG.error(u"[Symantec DLP] Encountered an exception when setting up the SOAP Client")
            
        else: # No connection error, client is setup with the URL. Allow the poller to be setup
            cls.is_connected = True
        cls.class_vars_loaded = True
예제 #17
0
import plotly.offline as py
import numpy as np
import simplejson as json
from itertools import chain
import csv
import warnings
import time
from lib.awdbToolsJson import padMissingData, getBasinSites, getSaturation, calcSMSAvg, fillMissingData

py.init_notebook_mode(connected=True)

this_dir = path.dirname(path.abspath(__file__))
master_dir = path.dirname(this_dir)

wsdl = r"https://wcc.sc.egov.usda.gov/awdbWebService/services?WSDL"
transport = Transport(timeout=300, cache=InMemoryCache())
awdb = Client(wsdl=wsdl, transport=transport, strict=False)

dt = datetime.datetime
date = datetime.date
today = dt.utcnow() - datetime.timedelta(hours=8)


def updtChart(basinName, basinSites):
    basin = basinName
    print('Working on STO POR Chart for ' + basinName)
    statsData = []
    minData = []
    maxData = []
    meanData = []
    lowestData = []
예제 #18
0
    def get(self, request):
        # Looking to start a new session
        if 'ibcToken' in request.GET:
            transport = Transport(cache=InMemoryCache())
            # Do service reflection on demand vs on middleware init
            self.imis = Client(
                settings.IMIS_SSO_SERVICE_URL,
                transport=transport) if not self.imis else self.imis
            # Verify token and create user if none exists
            token = request.GET['ibcToken']

            if self.imis.service.ValidateSession(
                    applicationInstance=self.application_instance,
                    userToken=token):
                request.session['ibcToken'] = token
                info = self.imis.service.FetchUserInfo(
                    applicationInstance=self.application_instance,
                    userToken=token)
                user_info = self._extract_user_info(info)

                logging.info(str(user_info))

                if not self._is_whitelisted(user_info):
                    if not self._should_be_department_admin(user_info):
                        messages.add_message(
                            request, messages.ERROR,
                            'Must be approved by a local officer to login to FireCARES using IMIS'
                        )
                        return redirect(reverse('login'))

                    if not self._is_member(user_info):
                        messages.add_message(
                            request, messages.ERROR,
                            'Must be an approved IAFF member to login to FireCARES using IMIS'
                        )
                        return redirect(reverse('login'))

                user = auth.authenticate(
                    remote_user=self._create_username(user_info))
                # Sync user information on every login
                if user:
                    user.email = user_info.get('EmailAddress', user.email)
                    user.first_name = user_info.get('FirstName',
                                                    user.first_name)
                    user.last_name = user_info.get('LastName', user.last_name)
                    user.save()

                    deptid = self._get_firecares_id(user_info)
                    dept = FireDepartment.objects.filter(id=deptid).first()
                    user.userprofile.department = dept
                    user.userprofile.save()

                    wht = RegistrationWhitelist.get_for_email(user.email)
                    if wht:
                        wht.process_permission_assignment(user)

                    if self._should_be_department_admin(user_info) and deptid:
                        # Remove existing department permissions
                        departments = get_objects_for_user(
                            user, 'firestation.admin_firedepartment')
                        for department in departments:
                            if department.id != deptid:
                                department.remove_admin(user)
                                department.remove_curator(user)
                        # Make this user an admin on the their "firecares_id" department
                        fd = FireDepartment.objects.get(id=deptid)
                        fd.add_admin(user)
                        fd.add_curator(user)
                    auth.login(request, user)
                    return redirect(
                        request.GET.get('next') or reverse('firestation_home'))

        messages.add_message(request, messages.ERROR,
                             'Invalid or missing IMIS session token')
        return redirect(reverse('login'))
예제 #19
0
 def _generate_inmemory_cache(self) -> zeep.cache.Base:
     return InMemoryCache(timeout=60 * 60 * 24 * 365)
예제 #20
0
def process_units(queue: multiprocessing.Queue, process_no, api_key,
                  mastr_number, output):
    logging.getLogger('zeep').setLevel(logging.CRITICAL)
    logging.basicConfig(stream=sys.stderr, level=logging.INFO)
    output_exists = output.exists()

    force_termination = False

    def terminate(*args):
        nonlocal force_termination

        if force_termination:
            logger.warning(f'Process {process_no}: Force termination')
            raise KeyboardInterrupt('Force exit')
        else:
            logger.info(f'Process {process_no}: Set to gracefully terminate')
            force_termination = True

    signal.signal(signal.SIGINT, terminate)

    wsdl = 'https://www.marktstammdatenregister.de/MaStRAPI/wsdl/mastr.wsdl'
    transport = Transport(cache=InMemoryCache(), operation_timeout=60)
    settings = Settings(strict=False, xml_huge_tree=True)
    client = Client(wsdl=wsdl, transport=transport, settings=settings)
    client_bind = client.bind('Marktstammdatenregister', 'Anlage')

    with output.open('a') as f:
        writer = csv.DictWriter(f, field_names)

        if not output_exists:
            writer.writeheader()

        while True:
            unit_mastr_numbers = queue.get(block=True)
            logger.info(f'Process {process_no}: Processing next batch')

            if unit_mastr_numbers is None:
                logger.info(
                    f'Process {process_no}: Received termination sentinel -> no more data to process.'
                )
                return

            errors_count = 0
            for unit_number in unit_mastr_numbers:
                if force_termination:
                    logger.info(
                        f'Process {process_no}: Gracefully terminating')
                    return

                if errors_count > ERRORS_LIMIT:
                    logger.warning(
                        f'Process {process_no}: Reached errors limit, discarding this batch'
                    )
                    break

                try:
                    c = fetch_unit(client_bind, api_key, mastr_number,
                                   unit_number)
                    respond = serialize_object(c)
                    writer.writerow({
                        k: (v.get('Wert', '<<unknown structure>>') if hasattr(
                            v, 'get') else v)
                        for k, v in respond.items()
                    })

                    # We got successful reply, the previous errors might not be related ==> continue
                    errors_count = 0
                except Fault as e:
                    logger.warning(
                        f'Process {process_no}: Got error, but continuing: {e.message}'
                    )
                    errors_count += 1
예제 #21
0
import logging

import zeep
from zeep.cache import InMemoryCache
from zeep.transports import Transport
"""
set of function all related to shahparak bank payment gateway
"""

errors = logging.getLogger('errors')
transport = Transport(cache=InMemoryCache())


def verify_saman(wsdl, ref_number, mid, real_amount):
    result = False
    try:
        client = zeep.Client(wsdl=wsdl, transport=transport)

        res = client.service.verifyTransaction(str(ref_number), str(mid))
        if int(res) == real_amount:
            result = True
    except Exception as e:
        errors.error(str(e))

    return result
예제 #22
0
def get_SOAP_client(url):
    transport = Transport(cache=InMemoryCache())
    return Client(url, transport=transport)
예제 #23
0
 def service(self):
     if not hasattr(self, "_service"):
         client = Zeep(wsdl=self.wsdl,
                       transport=Transport(cache=InMemoryCache()))
         self._service = client.service
     return self._service
예제 #24
0
    def __init__(self, *args, **kwargs):
        """Intialize the varible that are used in establishing connection to the ACS and\
           Intialize an HTTP SOAP client which will authenticate with the ACS server.

        :param ``*args``: the arguments to be used if any
        :type ``*args``: tuple
        :param ``**kwargs``: extra args to be used if any (mainly contains username, password, ipadress and port)
        :type ``**kwargs``: dict
        """
        self.args = args
        self.kwargs = kwargs
        self.username = self.kwargs['username']
        self.password = self.kwargs['password']
        self.ipaddr = self.kwargs['ipaddr']
        self.port = self.kwargs.get('port', None)
        self.cli_port = self.kwargs.pop('cli_port', '22')
        self.cli_username = self.kwargs.pop('cli_username', None)
        self.cli_password = self.kwargs.pop('cli_password', None)
        self.color = self.kwargs.pop('color', None)
        self.options = self.kwargs.pop('options', None)
        AxirosACS.CPE_wait_time = self.kwargs.pop('wait_time',
                                                  AxirosACS.CPE_wait_time)

        if self.options:
            options = [x.strip() for x in self.options.split(',')]
            for opt in options:
                if opt.startswith('wan-static-ipv6:'):
                    ipv6_address = opt.replace('wan-static-ipv6:', '').strip()
                    if "/" not in opt:
                        ipv6_address += "/64"
                    self.ipv6_interface = ipaddress.IPv6Interface(ipv6_address)
                    self.gwv6 = self.ipv6_interface.ip

        if self.port is not None:
            target = self.ipaddr + ":" + self.port
        else:
            target = self.ipaddr

        self.wsdl = "http://" + target + "/live/CPEManager/DMInterfaces/soap/getWSDL"

        session = Session()
        session.auth = HTTPBasicAuth(self.username, self.password)

        self.client = Client(
            wsdl=self.wsdl,
            transport=Transport(session=session,
                                cache=InMemoryCache(timeout=3600 * 3)),
            wsse=UsernameToken(self.username, self.password),
        )

        # to spawn pexpect on cli
        if all([self.ipaddr, self.cli_username, self.cli_password]):
            bft_pexpect_helper.spawn.__init__(
                self,
                command="ssh",
                args=[
                    '%s@%s' % (self.cli_username, self.ipaddr), '-p',
                    self.cli_port, '-o', 'StrictHostKeyChecking=no', '-o',
                    'UserKnownHostsFile=/dev/null', '-o',
                    'ServerAliveInterval=60', '-o', 'ServerAliveCountMax=5'
                ])
            self.check_connection(self.cli_username, self.name,
                                  self.cli_password)
            self.print_connected_console_msg(self.ipaddr, self.cli_port,
                                             self.color, self.name)
        # this should be populater ONLY when using __main__
        self.cpeid = self.kwargs.pop('cpeid', None)
예제 #25
0
"""
Definition of Soap Client used by all methods
Handles caching of wsdl globally in memory
A requests session exists through the lifetime of the client
"""
import zeep
from fenixlib import exc
from requests import Session
from zeep.exceptions import Fault
from zeep.cache import InMemoryCache
from fenixlib.schemas.context import AdminUserSchema

_cache = InMemoryCache(timeout=86400)
admin_schema = AdminUserSchema()


class ContextClient(object):
    def __init__(self, wsdl=None, auth=(None, None), cert_verify=None):
        session = Session()
        session.verify = cert_verify

        self.client = zeep.Client(wsdl,
                                  transport=zeep.transports.Transport(
                                      cache=_cache, session=session))
        self.auth = self.client.get_type("ns5:Credentials")(login=auth[0],
                                                            password=auth[1])
        self.service = self.client.create_service(
            self.client.service._binding.name,
            self.client.service._binding_options['address'].replace(
                ":80", "", 1))