예제 #1
0
 async def test_lib_scope_task(self):
     s_scope.set('test:foo', 10)
     self.eq(s_scope.get('test:foo'), 10)
     self.eq(s_scope.pop('test:foo'), 10)
     self.none(s_scope.get('test:foo'))
     s_scope.update([('test:hehe', 1), ('test:haha', 'wow')])
     self.eq(s_scope.get('test:hehe'), 1)
     self.eq(s_scope.get('test:haha'), 'wow')
예제 #2
0
    def test_lib_scope_enter(self):

        with s_scope.enter({'woot': 10}):
            self.eq(s_scope.get('woot'), 10)
            self.assertIsNone(s_scope.get('newp'))

        self.assertIsNone(s_scope.get('woot'))
        self.assertIsNone(s_scope.get('newp'))
예제 #3
0
 async def test_lib_scope_task(self):
     s_scope.set('test:foo', 10)
     self.eq(s_scope.get('test:foo'), 10)
     self.eq(s_scope.pop('test:foo'), 10)
     self.none(s_scope.get('test:foo'))
     s_scope.update([('test:hehe', 1), ('test:haha', 'wow')])
     self.eq(s_scope.get('test:hehe'), 1)
     self.eq(s_scope.get('test:haha'), 'wow')
예제 #4
0
    def test_socket_glob_plex(self):
        plex0 = s_scope.get('plex')

        self.nn(plex0)

        with s_scope.enter():
            plex1 = s_socket.Plex()
            s_scope.set('plex', plex1)
            self.ne(id(plex0), id(s_scope.get('plex')))
            plex1.fini()

        self.eq(id(plex0), id(s_scope.get('plex')))
예제 #5
0
    def iAmSynSvc(self, iden, props):
        '''
        API used by synapse service to register with the bus.

        Example:

            sbus.iAmSynSvc('syn.blah', foo='bar', baz=10)

        '''
        props['iden'] = iden

        svcfo = (iden, props)

        sock = s_scope.get('sock')
        if sock != None:

            def onfini():
                oldsvc = self.services.pop(iden, None)
                self.bytag.pop(iden)
                self.fire('syn:svc:fini', svcfo=oldsvc)

            sock.onfini(onfini)

        self.services[iden] = svcfo

        tags = props.get('tags', ())

        self.bytag.put(iden, tags)

        self.fire('syn:svc:init', svcfo=svcfo)
예제 #6
0
def openlink(link):
    '''
    Construct a telepath proxy from a link tufo.

    Example:

        foo = openlink(link)
        foo.bar(20)

    '''
    # special case for dmon://fooname/ which refers to a
    # named object within whatever dmon is currently in scope
    if link[0] == 'dmon':

        dmon = s_scope.get('dmon')
        if dmon is None:
            raise s_common.NoSuchName(name='dmon', link=link, mesg='no dmon instance in current scope')

        # the "host" part is really a dmon local
        host = link[1].get('host')
        item = dmon.locs.get(host)
        if item is None:
            raise s_common.NoSuchName(name=host, link=link, mesg='dmon instance has no local with that name')

        return item

    relay = s_link.getLinkRelay(link)
    name = link[1].get('path')[1:]

    sock = relay.connect()

    synack = teleSynAck(sock, name=name)
    bases = ()

    return Proxy(relay, sock=sock)
예제 #7
0
    def wait(self, jid, timeout=None):
        '''
        Wait for a job to complete by id.

        Example:

            boss.wait( jid, timeout=10 )

        '''
        s_threads.iWillWait()

        if timeout is None:
            timeout = s_scope.get('syntimeout')

        with self.joblock:
            job = self._boss_jobs.get(jid)
            if job is None:
                return True

            joblocal = self.joblocal.get(jid)
            evt = joblocal.get('waitevt')
            if evt is None:
                evt = threading.Event()
                joblocal['waitevt'] = evt

        evt.wait(timeout=timeout)
        return evt.is_set()
예제 #8
0
    async def test_lib_scope_enter(self):

        with s_scope.enter({'woot': 10}):
            self.eq(s_scope.get('woot'), 10)
            self.none(s_scope.get('newp'))

        self.none(s_scope.get('woot'))
        self.none(s_scope.get('newp'))

        scope = s_scope.Scope()
        scope.enter({'yes': 1})
        self.eq(scope.get('yes'), 1)
        scope.set('no', 0)
        frame = scope.leave()
        self.eq(frame, {'yes': 1, 'no': 0})
        self.none(scope.get('yes'))
        self.none(scope.get('no'))
        self.raises(IndexError, scope.leave)
예제 #9
0
파일: db.py 프로젝트: thpatel/synapse
    def xact(self):

        xact = s_scope.get(self.iden)
        if xact is not None:
            return xact

        xact = self.dbque.get()
        s_scope.set(self.iden, xact)
        return xact
예제 #10
0
    async def test_lib_scope_enter(self):

        with s_scope.enter({'woot': 10}):
            self.eq(s_scope.get('woot'), 10)
            self.none(s_scope.get('newp'))

        self.none(s_scope.get('woot'))
        self.none(s_scope.get('newp'))

        scope = s_scope.Scope()
        scope.enter({'yes': 1})
        self.eq(scope.get('yes'), 1)
        scope.set('no', 0)
        frame = scope.leave()
        self.eq(frame, {'yes': 1, 'no': 0})
        self.none(scope.get('yes'))
        self.none(scope.get('no'))
        self.raises(IndexError, scope.leave)
예제 #11
0
def whoami():
    '''
    Return the name of the current synapse user for this thread.

    Example:

        name = s_auth.whoami()

    '''
    return s_scope.get('syn:user', 'root@localhost')
예제 #12
0
def getSynAuth():
    '''
    Return the current UserAuth object for the current thread.

    Example:

        auth = s_userauth.getSynAuth()

    '''
    return s_scope.get('syn:auth')
예제 #13
0
def getSynUser():
    '''
    Return the name of the current synapse user for this thread.

    Example:

        name = s_userauth.getSynUser()

    '''
    return s_scope.get('syn:user')
예제 #14
0
    def iAmHive(self, iden, **info):
        hive = (iden, info)
        self.hives[iden] = hive

        sock = s_scope.get('sock')
        def onfini():
            self.fireHiveFini(iden)
        sock.onfini(onfini)

        self.fire('hive:hive:init', hive=hive)
예제 #15
0
    def iAmDrone(self, iden, **info):

        drone = (iden, info)
        self.drones[iden] = drone

        sock = s_scope.get('sock')
        def onfini():
            self.fireDroneFini(iden)
        sock.onfini(onfini)

        self.fire('hive:drone:init', iden=iden)
예제 #16
0
파일: telepath.py 프로젝트: mari0d/synapse
def openlink(link):
    '''
    Construct a Telepath Proxy from a link tufo.

    Args:
        link ((str, dict)): A link dictionary.

    Examples:
        Get a proxy object, call a function then close the object::

            foo = openlink(link)
            foo.bar(20)
            foo.fini()

        Get a proxy object as a context manager, which will result in the object being automatically closed::

            with openlink as foo:
                foo.dostuff(30)

    Returns:
        Proxy: A Proxy object for calling remote tasks.
    '''
    # special case for dmon://fooname/ which refers to a
    # named object within whatever dmon is currently in scope
    if link[0] == 'dmon':

        dmon = s_scope.get('dmon')
        if dmon is None:
            raise s_common.NoSuchName(name='dmon',
                                      link=link,
                                      mesg='no dmon instance in current scope')

        # the "host" part is really a dmon local
        host = link[1].get('host')
        item = dmon.locs.get(host)
        if item is None:
            raise s_common.NoSuchName(
                name=host,
                link=link,
                mesg='dmon instance has no local with that name')

        return item

    relay = s_link.getLinkRelay(link)
    name = link[1].get('path')[1:]

    sock = relay.connect()

    synack = teleSynAck(sock, name=name)
    bases = ()

    return Proxy(relay, sock=sock)
예제 #17
0
    def getLiftLimit(self, limit):
        userlim = s_scope.get('storm:limit:lift')
        if userlim != None:
            if limit == None:
                return userlim

            return min(userlim, limit)

        if self.limlift != None:
            if limit == None:
                return self.limlift
            return min(self.limlift, limit)

        return limit
예제 #18
0
    def getLiftLimit(self, *limits):
        '''
        Return the lift() result limit for the current user/runtime.
        Callers may specify additional limit values, which if not None
        will be included in the min() calculation.

        Args:
            *limits:    Optional list of additional int limit values

        Returns:
            (int): The lift limit value or None

        '''
        limt0 = s_scope.get('storm:limit:lift')
        return minlim(self.limlift, limt0, *limits)
예제 #19
0
    async def test_pullfile(self):

        async with self.getTestDmonCortexAxon():

            axonurl = s_scope.get('axonurl')
            async with await s_telepath.openurl(s_scope.get('blobstorurl')) as blob:
                await blob.putmany([b'visi', b'test'])

                def pullfile():

                    testhash = hashlib.sha256(b'test').hexdigest()
                    visihash = hashlib.sha256(b'visi').hexdigest()
                    nonehash = hashlib.sha256(b'none').hexdigest()

                    with self.getTestDir() as wdir:
                        outp = self.getTestOutp()
                        self.eq(0, s_pullfile.main(['-a', axonurl,
                                                    '-o', wdir,
                                                    '-l', testhash,
                                                    '-l', nonehash], outp))
                        oldcwd = os.getcwd()
                        os.chdir(wdir)
                        self.eq(0, s_pullfile.main(['-a', axonurl,
                                                    '-l', visihash], outp))
                        os.chdir(oldcwd)
                        with open(pathlib.Path(wdir, testhash), 'rb') as fd:
                            self.eq(b'test', fd.read())
                        with open(pathlib.Path(wdir, visihash), 'rb') as fd:
                            self.eq(b'visi', fd.read())

                        self.true(outp.expect(f'b\'{nonehash}\' not in axon store'))
                        self.true(outp.expect(f'Fetching {testhash} to file'))
                        self.true(outp.expect(f'Fetching {visihash} to file'))

                loop = asyncio.get_running_loop()
                await loop.run_in_executor(None, pullfile)
예제 #20
0
def reminder(evnt, **info):
    '''
    May be used on server side to set a telepath reconnect reminder.

    Args:
        name (str): The event name to fire on reconnect
        **info: The event metadata

    Returns:
        (bool): True if the telepath caller was notified.

    NOTE:
        This requests that the current telepath caller fire the given
        event back to us in the event of a socket reconnect.
    '''
    sock = s_scope.get('sock')
    if sock is None:
        return False

    mesg = (evnt, info)
    sock.tx(('tele:reminder', {'mesg': mesg}))
    return True
예제 #21
0
import synapse.link as s_link
import synapse.compat as s_compat
import synapse.cortex as s_cortex
import synapse.eventbus as s_eventbus

import synapse.lib.scope as s_scope
import synapse.lib.ingest as s_ingest
import synapse.lib.output as s_output
import synapse.lib.thishost as s_thishost

from synapse.common import *

# create the global multi-plexor *not* within a test
# to avoid "leaked resource" when a test triggers creation
s_scope.get('plex')


class TooFewEvents(Exception):
    pass


# Py2/3 SSL Exception Compat
if s_compat.version >= (3, 0, 0):
    TestSSLInvalidClientCertErr = ssl.SSLError
    TestSSLConnectionResetErr = ConnectionResetError
else:
    TestSSLInvalidClientCertErr = socket.error
    TestSSLConnectionResetErr = socket.error

예제 #22
0
    def getSslCore(self, conf=None, configure_roles=False):
        dconf = {
            'auth:admin': 'root@localhost',
            'auth:en': 1,
        }
        if conf:
            conf.update(dconf)
        conf = dconf

        amesgs = (
            ('auth:add:user', {
                'user': '******'
            }),
            ('auth:add:role', {
                'role': 'creator'
            }),
            ('auth:add:rrule', {
                'role': 'creator',
                'rule': ('node:add', {
                    'form': '*'
                })
            }),
            ('auth:add:rrule', {
                'role': 'creator',
                'rule': ('node:tag:add', {
                    'tag': '*'
                })
            }),
            ('auth:add:rrule', {
                'role': 'creator',
                'rule': ('node:prop:set', {
                    'form': '*',
                    'prop': '*'
                })
            }),
            ('auth:add:role', {
                'role': 'deleter'
            }),
            ('auth:add:rrule', {
                'role': 'deleter',
                'rule': ('node:del', {
                    'form': '*'
                })
            }),
            ('auth:add:rrule', {
                'role': 'deleter',
                'rule': ('node:del', {
                    'form': '*'
                })
            }),
            ('auth:add:rrule', {
                'role': 'deleter',
                'rule': ('node:tag:del', {
                    'tag': '*'
                })
            }),
            ('auth:add:rrule', {
                'role': 'deleter',
                'rule': ('node:prop:set', {
                    'form': '*',
                    'prop': '*'
                })
            }),
            ('auth:add:urole', {
                'user': '******',
                'role': 'creator'
            }),
            ('auth:add:urole', {
                'user': '******',
                'role': 'deleter'
            }),
        )

        with self.getDirCore(conf=conf) as core:
            s_scope.set('syn:core', core)
            dirn = s_scope.get('dirn')
            writeCerts(dirn)
            cafile = os.path.join(dirn, 'ca.crt')
            keyfile = os.path.join(dirn, 'server.key')
            certfile = os.path.join(dirn, 'server.crt')
            userkey = os.path.join(dirn, 'user.key')
            usercrt = os.path.join(dirn, 'user.crt')
            rootkey = os.path.join(dirn, 'root.key')
            rootcrt = os.path.join(dirn, 'root.crt')
            with s_daemon.Daemon() as dmon:
                s_scope.set('syn:dmon', dmon)
                dmon.share('core', core)
                link = dmon.listen(
                    'ssl://*****:*****@localhost/core'
                user_prox = s_telepath.openurl(
                    url,
                    port=port,
                    cafile=cafile,
                    keyfile=userkey,
                    certfile=usercrt)  # type: s_cores_common.CoreApi
                root_prox = s_telepath.openurl(
                    url,
                    port=port,
                    cafile=cafile,
                    keyfile=rootkey,
                    certfile=rootcrt)  # type: s_cores_common.CoreApi

                if configure_roles:
                    for mesg in amesgs:
                        isok, retn = root_prox.authReact(mesg)
                        s_common.reqok(isok, retn)

                try:
                    yield user_prox, root_prox
                finally:
                    user_prox.fini()
                    root_prox.fini()
예제 #23
0
    async def test_pushfile(self):

        async with self.getTestDmonCortexAxon() as dmon:

            coreurl = s_scope.get('coreurl')
            axonurl = s_scope.get('axonurl')

            def pushfile():

                with self.getTestDir() as dirn:

                    nullpath = os.path.join(dirn, 'null.txt')
                    visipath = os.path.join(dirn, 'visi.txt')

                    with s_common.genfile(visipath) as fd:
                        fd.write(b'visi')

                    with self.getTestProxy(dmon, 'axon00') as axon:
                        self.len(1, axon.wants([visihash]))

                    outp = self.getTestOutp()
                    args = ['-a', axonurl,
                            '-c', coreurl,
                            '-t', 'foo.bar,baz.faz',
                            visipath]

                    self.eq(0, s_pushfile.main(args, outp))
                    self.true(outp.expect('Uploaded [visi.txt] to axon'))
                    self.true(outp.expect('file: visi.txt (4) added to core'))

                    with self.getTestProxy(dmon, 'axon00') as axon:
                        self.len(0, axon.wants([visihash]))
                        self.eq(b'visi', b''.join([buf for buf in axon.get(visihash)]))

                    outp = self.getTestOutp()
                    self.eq(0, s_pushfile.main(args, outp))
                    self.true(outp.expect('Axon already had [visi.txt]'))

                    with self.getTestProxy(dmon, 'core', user='******', passwd='root') as core:
                        self.len(1, core.eval(f'file:bytes={s_common.ehex(visihash)}'))
                        self.len(1, core.eval('file:bytes:size=4'))
                        self.len(1, core.eval('#foo.bar'))
                        self.len(1, core.eval('#baz.faz'))

                    # Ensure user can't push a non-existant file and that it won't exist
                    args = ['-a', axonurl, nullpath]
                    self.raises(s_exc.NoSuchFile, s_pushfile.main, args, outp=outp)

                    with self.getTestProxy(dmon, 'axon00') as axon:
                        self.len(1, axon.wants([nullhash]))

                    with s_common.genfile(nullpath) as fd:
                        fd.write(b'')

                    outp = self.getTestOutp()
                    args = ['-a', axonurl,
                            '-c', coreurl,
                            '-t', 'empty',
                            nullpath]

                    self.eq(0, s_pushfile.main(args, outp))

                    with self.getTestProxy(dmon, 'axon00') as axon:
                        self.len(0, axon.wants([nullhash]))
                        self.eq(b'', b''.join([buf for buf in axon.get(nullhash)]))

            loop = asyncio.get_running_loop()
            await loop.run_in_executor(None, pushfile)