Exemple #1
0
 def openLogFd(self, opts):
     opath = self.locs.get('log:fp')
     if opath:
         self.printf(
             'Must call --off to disable current file before starting a new file.'
         )
         return
     fmt = opts.format
     path = opts.path
     nodes_only = opts.nodes_only
     edits_only = opts.edits_only
     if not path:
         ts = s_time.repr(s_common.now(), True)
         fn = f'storm_{ts}.{fmt}'
         path = s_common.getSynPath('stormlogs', fn)
     self.printf(f'Starting logfile at [{path}]')
     q = queue.Queue()
     fd = s_common.genfile(path)
     # Seek to the end of the file. Allows a user to append to a file.
     fd.seek(0, 2)
     self.locs['log:fp'] = path
     self.locs['log:fd'] = fd
     self.locs['log:fmt'] = fmt
     self.locs['log:queue'] = q
     self.locs['log:thr'] = self.queueLoop()
     self.locs['log:nodesonly'] = nodes_only
     self.locs['log:editsonly'] = edits_only
     self._cmd_cli.on('storm:mesg', self.onStormMesg)
Exemple #2
0
 def openLogFd(self, opts):
     opath = self.locs.get('log:fp')
     if opath:
         self.printf('Must call --off to disable current file before starting a new file.')
         return
     fmt = opts.format
     path = opts.path
     nodes_only = opts.nodes_only
     splice_only = opts.splices_only
     if not path:
         ts = s_time.repr(s_common.now(), True)
         fn = f'storm_{ts}.{fmt}'
         path = s_common.getSynPath('stormlogs', fn)
     self.printf(f'Starting logfile at [{path}]')
     q = queue.Queue()
     fd = s_common.genfile(path)
     # Seek to the end of the file. Allows a user to append to a file.
     fd.seek(0, 2)
     self.locs['log:fp'] = path
     self.locs['log:fd'] = fd
     self.locs['log:fmt'] = fmt
     self.locs['log:queue'] = q
     self.locs['log:thr'] = self.queueLoop()
     self.locs['log:nodesonly'] = nodes_only
     self.locs['log:splicesonly'] = splice_only
     self._cmd_cli.on('storm:mesg', self.onStormMesg)
Exemple #3
0
async def main(argv, outp=s_output.stdout):

    pars = getArgParser()
    opts = pars.parse_args(argv)

    path = s_common.getSynPath('telepath.yaml')
    telefini = await s_telepath.loadTeleEnv(path)

    async with await s_telepath.openurl(opts.cortex) as proxy:

        if telefini is not None:
            proxy.onfini(telefini)

        async with await StormCli.anit(proxy, outp=outp, opts=opts) as cli:

            if opts.onecmd:
                await cli.runCmdLine(opts.onecmd)
                return

            # pragma: no cover
            cli.colorsenabled = True
            cli.printf(welcome)

            await cli.addSignalHandlers()
            await cli.runCmdLoop()
Exemple #4
0
async def main(argv):  # pragma: no cover

    if len(argv) not in (1, 2):
        print(
            'usage: python -m synapse.tools.cmdr <url> [<single quoted command>]'
        )
        return 1

    s_common.setlogging(logger, 'WARNING')

    path = s_common.getSynPath('telepath.yaml')
    telefini = await s_telepath.loadTeleEnv(path)

    # Ensure that SYN_DIR is available
    _ = s_common.getSynDir()

    async with await s_telepath.openurl(argv[0]) as item:
        try:
            s_version.reqVersion(item._getSynVers(), reqver)
        except s_exc.BadVersion as e:
            valu = s_version.fmtVersion(*e.get('valu'))
            print(
                f'Proxy version {valu} is outside of the cmdr supported range ({reqver}).'
            )
            print(
                f'Please use a version of Synapse which supports {valu}; current version is {s_version.verstring}.'
            )
            return 1
        await runcmdr(argv, item)

    if telefini is not None:
        await telefini()

    return 0
Exemple #5
0
    async def prompt(self, text=None):
        '''
        Prompt for user input from stdin.
        '''
        if self.sess is None:
            hist = FileHistory(s_common.getSynPath('cmdr_history'))
            self.sess = PromptSession(history=hist)

        if text is None:
            text = self.cmdprompt

        with patch_stdout():
            retn = await self.sess.prompt(text, async_=True, vi_mode=self.vi_mode, enable_open_in_editor=True)
            return retn
Exemple #6
0
    async def prompt(self, text=None):
        '''
        Prompt for user input from stdin.
        '''
        if self.sess is None:
            hist = FileHistory(s_common.getSynPath('cmdr_history'))
            self.sess = PromptSession(history=hist)

        if text is None:
            text = self.cmdprompt

        with patch_stdout():
            retn = await self.sess.prompt_async(text,
                                                vi_mode=self.vi_mode,
                                                enable_open_in_editor=True)
            return retn
Exemple #7
0
async def main(argv, outp=None):

    pars = setup()
    opts = pars.parse_args(argv)

    path = s_common.getSynPath('telepath.yaml')
    telefini = await s_telepath.loadTeleEnv(path)

    if outp is None:  # pragma: no cover
        outp = s_output.OutPut()

    if opts.output is None:
        opts.output = '.'

    outdir = pathlib.Path(opts.output)

    s_common.gendir(opts.output)

    async with await s_telepath.openurl(opts.axon) as axon:

        # reminder: these are the hashes *not* available

        awants = await axon.wants([s_common.uhex(h) for h in opts.hashes])
        for a in awants:
            outp.printf(f'{s_common.ehex(a)} not in axon store')

        exists = [h for h in opts.hashes if s_common.uhex(h) not in awants]

        for h in exists:

            try:
                outp.printf(f'Fetching {h} to file')

                with open(outdir.joinpath(h), 'wb') as fd:
                    async for b in axon.get(s_common.uhex(h)):
                        fd.write(b)

                outp.printf(f'Fetched {h} to file')

            except Exception as e:
                outp.printf('Error: Hit Exception: %s' % (str(e), ))
                continue

    if telefini:  # pragma: no cover
        await telefini()

    return 0
Exemple #8
0
async def main(argv, outp=None):  # pragma: no cover

    if outp is None:
        outp = s_output.stdout

    s_common.setlogging(logger, 'WARNING')

    path = s_common.getSynPath('telepath.yaml')
    async with contextlib.AsyncExitStack() as ctx:

        telefini = await s_telepath.loadTeleEnv(path)
        if telefini is not None:
            ctx.push_async_callback(telefini)

        await _main(argv, outp)

    return 0
Exemple #9
0
async def main(argv):  # pragma: no cover

    if len(argv) not in (1, 2):
        print(
            'usage: python -m synapse.tools.cmdr <url> [<single quoted command>]'
        )
        return 1

    s_common.setlogging(logger, 'WARNING')

    path = s_common.getSynPath('telepath.yaml')
    async with contextlib.AsyncExitStack() as ctx:

        telefini = await s_telepath.loadTeleEnv(path)
        if telefini is not None:
            ctx.push_async_callback(telefini)

        await _main(argv)

    return 0
Exemple #10
0
async def main(argv, outp=None):  # pragma: no cover

    if outp is None:
        outp = s_output.stdout

    if len(argv) not in (1, 2):
        outp.printf('usage: python -m synapse.tools.aha.list <url> [network name]')
        return 1

    s_common.setlogging(logger, 'WARNING')

    path = s_common.getSynPath('telepath.yaml')
    async with contextlib.AsyncExitStack() as ctx:

        telefini = await s_telepath.loadTeleEnv(path)
        if telefini is not None:
            ctx.push_async_callback(telefini)

        await _main(argv, outp)

    return 0
Exemple #11
0
def alias(name):
    '''
    Resolve a telepath alias via ~/.syn/aliases.yaml

    Args:
        name (str): Name of the alias to resolve.

    Notes:
        An exact match against the aliases will always be returned first.
        If no exact match is found and the name contains a '/' in it, the
        value before the slash is looked up and the remainder of the path
        is joined to any result. This is done to support dynamic Telepath
        share names.

    Returns:
        str: The url string, if present in the alias.  None will be returned
        if there are no matches.
    '''
    path = s_common.getSynPath('aliases.yaml')
    if not os.path.isfile(path):
        return None

    conf = s_common.yamlload(path)

    # Is there an exact match - if so, return it.
    url = conf.get(name)
    if url:
        return url

    # Since telepath supports dynamic shared object access,
    # slice a name at the first '/', look up using that value
    # and then append the second value to it.
    dynname = None
    if '/' in name:
        name, dynname = name.split('/', 1)
    url = conf.get(name)
    if url and dynname:
        url = '/'.join([url, dynname])

    return url
Exemple #12
0
def alias(name):
    '''
    Resolve a telepath alias via ~/.syn/aliases.yaml

    Args:
        name (str): Name of the alias to resolve.

    Notes:
        An exact match against the aliases will always be returned first.
        If no exact match is found and the name contains a '/' in it, the
        value before the slash is looked up and the remainder of the path
        is joined to any result. This is done to support dynamic Telepath
        share names.

    Returns:
        str: The url string, if present in the alias.  None will be returned
        if there are no matches.
    '''
    path = s_common.getSynPath('aliases.yaml')
    if not os.path.isfile(path):
        return None

    conf = s_common.yamlload(path)

    # Is there an exact match - if so, return it.
    url = conf.get(name)
    if url:
        return url

    # Since telepath supports dynamic shared object access,
    # slice a name at the first '/', look up using that value
    # and then append the second value to it.
    dynname = None
    if '/' in name:
        name, dynname = name.split('/', 1)
    url = conf.get(name)
    if url and dynname:
        url = '/'.join([url, dynname])

    return url
Exemple #13
0
async def main(argv, outprint=None):
    if outprint is None:  # pragma: no cover
        outprint = s_output.OutPut()
    global outp
    outp = outprint

    async with contextlib.AsyncExitStack() as cm:

        teleyaml = s_common.getSynPath('telepath.yaml')
        if os.path.isfile(teleyaml):
            fini = await s_telepath.loadTeleEnv(teleyaml)
            cm.push_async_callback(fini)

        pars = makeargparser()
        try:
            opts = pars.parse_args(argv)
        except s_exc.ParserExit:
            return -1

        retn = await opts.func(opts)

    return retn
Exemple #14
0
    async def test_alias(self):

        item = TeleAware()
        name = 'item'

        async with self.getTestDmon() as dmon:

            host, port = dmon.addr
            dmon.share(name, item)

            with self.getTestDir() as dirn:

                url = f'tcp://{host}:{port}/{name}'
                beepbeep_alias = url + '/beepbeep'
                aliases = {name: url, f'{name}/borp': beepbeep_alias}

                with self.setSynDir(dirn):

                    fp = s_common.getSynPath('aliases.yaml')
                    s_common.yamlsave(aliases, fp)

                    # None existent aliases return None
                    self.none(s_telepath.alias('newp'))
                    self.none(s_telepath.alias('newp/path'))

                    # An exact match wins
                    self.eq(s_telepath.alias(name), url)
                    self.eq(s_telepath.alias(f'{name}/borp'), beepbeep_alias)
                    # Dynamic aliases are valid.
                    self.eq(s_telepath.alias(f'{name}/beepbeep'),
                            beepbeep_alias)

                    async with await s_telepath.openurl(name) as prox:
                        self.eq(10, await prox.getFooBar(20, 10))

                    # Check to see that we can connect to an aliased name
                    # with a dynamic share attached to it.
                    async with await s_telepath.openurl(f'{name}/bar') as prox:
                        self.eq('bar: beep', await prox.beep())
Exemple #15
0
async def main(argv, outp=None):

    if outp is None:  # pragma: no cover
        outp = s_output.OutPut()

    path = s_common.getSynPath('telepath.yaml')
    telefini = await s_telepath.loadTeleEnv(path)

    pars = makeargparser()
    opts = pars.parse_args(argv)

    axon = await s_telepath.openurl(opts.axon)

    core = None
    if opts.cortex:
        core = await s_telepath.openurl(opts.cortex)

        tags = set()
        if opts.tags:
            for tag in opts.tags.split(','):
                tags.add(tag)

        tags = tuple(tags)
        if tags:
            outp.printf(f'adding tags: {tags}')

    filepaths = set()
    for item in opts.filenames:
        paths = glob.glob(item, recursive=opts.recursive)

        if not paths:
            outp.printf(f'filepath does not contain any files: {item}')
            continue

        filepaths.update([path for path in paths if os.path.isfile(path)])

    for path in filepaths:

        bname = os.path.basename(path)

        hset = s_hashset.HashSet()
        with s_common.reqfile(path) as fd:
            hset.eatfd(fd)

        fhashes = {htyp: hasher.hexdigest() for htyp, hasher in hset.hashes}

        sha256 = fhashes.get('sha256')
        bsha256 = s_common.uhex(sha256)

        if not await axon.has(bsha256):

            async with await axon.upload() as upfd:

                with s_common.genfile(path) as fd:
                    for byts in s_common.iterfd(fd):
                        await upfd.write(byts)

                size, hashval = await upfd.save()

            if hashval != bsha256:  # pragma: no cover
                raise s_exc.SynErr(mesg='hashes do not match',
                                   ehash=s_common.ehex(hashval),
                                   ahash=hashval)

            outp.printf(f'Uploaded [{bname}] to axon')
        else:
            outp.printf(f'Axon already had [{bname}]')

        if core:
            opts = {
                'vars': {
                    'md5': fhashes.get('md5'),
                    'sha1': fhashes.get('sha1'),
                    'sha256': fhashes.get('sha256'),
                    'size': hset.size,
                    'name': bname,
                    'tags': tags,
                }
            }

            q = '[file:bytes=$sha256 :md5=$md5 :sha1=$sha1 :size=$size :name=$name] ' \
                '{ for $tag in $tags { [+#$tag] } }'

            msgs = await core.storm(q, opts=opts).list()
            node = [m[1] for m in msgs if m[0] == 'node'][0]

            iden = node[0][1]
            size = node[1]['props']['size']
            name = node[1]['props']['name']
            mesg = f'file: {bname} ({size}) added to core ({iden}) as {name}'
            outp.printf(mesg)

    await axon.fini()
    if core:
        await core.fini()

    if telefini:  # pragma: no cover
        await telefini()

    return 0