def test_cmds_ask_showcols(self): with self.getDmonCore() as core: core.formTufoByProp('inet:email', '*****@*****.**') core.formTufoByProp('inet:email', '*****@*****.**') core.formTufoByProp('inet:email', '*****@*****.**') core.formTufoByProp('inet:email', '*****@*****.**') outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(core, outp=outp) line = 'ask inet:email="*****@*****.**" show:cols(inet:email:fqdn,inet:email:user,node:ndef)' resp = cmdr.runCmdLine(line) self.len(1, resp['data']) self.true( outp.expect( 'vertex.link visi a20979f71b90cf2ae1c53933675b5c3c')) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(core, outp=outp) line = 'ask inet:email show:cols(inet:email, order=inet:email:fqdn)' resp = cmdr.runCmdLine(line) self.len(4, resp['data']) result = [mesg.strip() for mesg in outp.mesgs] self.eq(result, [ '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '(4 results)' ])
def test_cmds_help(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) cmdr.runCmdLine('help') self.true( str(outp).find('List commands and display help output.') != -1)
def test_cmds_ask(self): with self.getDmonCore() as core: outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(core, outp=outp) core.formTufoByProp('inet:email', '*****@*****.**') resp = cmdr.runCmdLine('ask inet:email="*****@*****.**"') self.len(1, resp['data']) self.true(outp.expect('*****@*****.**'))
def test_cmds_ask_null_response(self): with self.getDmonCore() as core: outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(core, outp=outp) core.formTufoByProp('inet:email', '*****@*****.**') resp = cmdr.runCmdLine('ask inet:email="*****@*****.**"') self.none(resp) self.true(outp.expect('(0 results)'))
def test_cmds_ask(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) core.formTufoByProp('inet:email', '*****@*****.**') resp = cmdr.runCmdLine('ask inet:email="*****@*****.**"') self.eq(len(resp['data']), 1) self.ne(str(outp).strip().find('*****@*****.**'), -1)
def test_cmds_ask_mutual_exclusive(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) core.formTufoByProp('inet:email', '*****@*****.**') resp = cmdr.runCmdLine( 'ask --raw --props inet:email="*****@*****.**"') self.none(resp) outp = str(outp) self.true('Cannot specify --raw and --props together.' in outp)
def main(argv): if len(argv) != 2: print('usage: python -m synapse.tools.cmdr <url>') return -1 item = s_telepath.openurl(argv[1]) cmdr = s_cmdr.getItemCmdr(item) cmdr.runCmdLoop()
def test_cmds_ask_raw(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) core.formTufoByProp('inet:email', '*****@*****.**') resp = cmdr.runCmdLine('ask --raw inet:email="*****@*****.**"') self.eq(len(resp['data']), 1) outp = str(outp) terms = ('"tufo:form": "inet:email"', '"inet:email:user": "******"') for term in terms: self.nn(re.search(term, outp))
def test_cmds_help(self): with self.getDmonCore() as core: outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(core, outp=outp) cmdr.runCmdLine('help') self.true(outp.expect('List commands and display help output.')) cmdr.runCmdLine('help quit') self.true(outp.expect('Quit the current command line interpreter.')) cmdr.runCmdLine('help notacommand') self.true(outp.expect('=== NOT FOUND: notacommand'))
def test_cmds_ask_debug(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) core.formTufoByProp('inet:email', '*****@*****.**') resp = cmdr.runCmdLine('ask --debug inet:email="*****@*****.**"') self.eq(len(resp['data']), 1) outp = str(outp) terms = ('oplog', 'took', 'options', 'limits') for term in terms: self.nn(re.search(term, outp))
def test_cmds_ask_props(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) core.formTufoByProp('inet:email', '*****@*****.**') resp = cmdr.runCmdLine('ask --props inet:email="*****@*****.**"') self.eq(len(resp['data']), 1) outp = str(outp) terms = ('fqdn = vertex.link', 'user = visi') for term in terms: self.nn(re.search(term, outp))
def test_cmds_ask_exc_response(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) core.formTufoByProp('inet:email', '*****@*****.**') resp = cmdr.runCmdLine( 'ask inet:dns:a:ipv4*inet:cidr=192.168.0.0/100') self.none(resp) outp = str(outp) terms = ('\(0 results\)', 'oplog:', 'options:', 'limits:') for term in terms: self.nn(re.search(term, outp))
def test_cmds_ask_multilift(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) core.formTufoByProp('str', 'hehe') core.formTufoByProp('inet:ipv4', 0) resp = cmdr.runCmdLine('ask str inet:ipv4') self.eq(len(resp['data']), 2) outp = str(outp) terms = ('0.0.0.0', 'hehe') for term in terms: self.nn(re.search(term, outp))
def main(argv): # pragma: no cover if len(argv) != 2: print('usage: python -m synapse.tools.cmdr <url>') return -1 with s_telepath.openurl(argv[1]) as item: cmdr = s_glob.sync(s_cmdr.getItemCmdr(item)) # This causes a dropped connection to the cmdr'd item to # cause cmdr to exit. We can't safely test this in CI since # the fini handler sends a SIGINT to mainthread; which can # be problematic for test runners. cmdr.finikill = True cmdr.runCmdLoop() cmdr.finikill = False
def test_cmds_ask_mesgs(self): with self.getDmonCore() as core: real_core = s_scope.get('syn:core') real_core.setOperFunc('test:mesg', mesg_cmd) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(core, outp=outp) resp = cmdr.runCmdLine('ask [inet:ipv4=1.2.3.4] test:mesg()') self.len(1, resp['data']) self.len(2, resp['mesgs']) outp.expect('Storm Status Messages:') outp.expect('Log test messages') outp.expect('Query has [1] nodes') print('cli> ask [inet:ipv4=1.2.3.4] test:mesg()') print(outp)
def test_cmds_ask_tagtime(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) resp = cmdr.runCmdLine( 'ask [ inet:ipv4=1.2.3.4 #foo.bar@2011-2016 #baz.faz ]') self.eq(len(resp['data']), 1) lines = [s.strip() for s in str(outp).split('\n')] self.true( any([ re.search('^#baz.faz \(added [0-9/: \.]+\)$', l) for l in lines ])) self.true( any([ re.search( '^#foo.bar \(added [0-9/: \.]+\) 2011/01/01 00:00:00.000 - 2016/01/01 00:00:00.000$', l) for l in lines ]))
def choptag(tag): ''' Chop a tag into hierarchal levels. ''' parts = tag.split('.') return ['.'.join(parts[:x + 1]) for x in range(len(parts))] def _ctor_cortex(conf): url = conf.pop('url', None) if url is None: raise s_common.BadInfoValu(name='url', valu=None, mesg='cortex ctor requires "url":<url> option') core = openurl(url) core.setConfOpts(conf) return core s_dyndeps.addDynAlias('syn:cortex', _ctor_cortex) if __name__ == '__main__': # pragma: no cover import sys import synapse.lib.cmdr as s_cmdr core = openurl(sys.argv[1]) cmdr = s_cmdr.getItemCmdr(core) cmdr.runCmdLoop()
def test_cmds_auth(self): with self.getSslCore() as proxies: uprox, rprox = proxies # type: s_cores_common.CoreApi, s_cores_common.CoreApi outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) cmdr.runCmdLine('auth --type user') self.true(outp.expect('root@localhost')) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) cmdr.runCmdLine('auth --type role') outp.expect("'roles': ()") # Get our user outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) cmdr.runCmdLine('auth --act get --name root@localhost') self.true(outp.expect('root@localhost')) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine('auth --act add --name creator --type role') self.istufo(retn) rolefo = retn[1].get('role') self.eq(rolefo[0], 'creator') outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --name creator --type role --rule node:add --form *' ) self.istufo(retn) self.true(outp.expect("('node:add', {'form': '*'})")) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) cmdr.runCmdLine('ask [strform="giggles"]') self.true(outp.expect('AuthDeny')) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --name root@localhost --role creator') self.true(outp.expect("'roles': ('creator',)")) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) cmdr.runCmdLine('ask [strform="giggles"]') self.true(outp.expect('strform = giggles')) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine('auth --name user@localhost --act add') self.true(outp.expect('user@localhost')) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --name user@localhost --role creator') self.true(outp.expect('user@localhost')) self.true(outp.expect("'roles': ('creator',)")) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --type role --name creator --rule node:tag:add --tag *' ) self.true(outp.expect("('node:tag:add', {'tag': '*'})")) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(uprox, outp=outp) retn = cmdr.runCmdLine( 'ask [strform=hehe] addtag(yes.no) addtag(hehe.haha)') self.true(outp.expect('strform = hehe')) self.true(outp.expect('#yes.no')) self.true(outp.expect('#hehe.haha')) # uprox is not admin and cannot do auth things outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(uprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --type role --name creator --rule node:tag:del --tag yes' ) self.none(retn) self.true(outp.expect('AuthDeny')) # rprox is an admin so he can add the tag deletion rule outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --type role --name creator --rule node:tag:del --tag yes' ) self.true(outp.expect("('node:tag:del', {'tag': 'yes'})")) # Uprox can now delete tags outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(uprox, outp=outp) retn = cmdr.runCmdLine('ask strform=hehe deltag(yes.no)') self.true(outp.expect('strform = hehe')) self.true(outp.expect('#yes')) self.false(outp.expect('#yes.no', throw=False)) # delete a rule outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act del --type role --name creator --rule node:tag:add --tag *' ) self.false( outp.expect("('node:tag:add', {'tag': '*'})", throw=False)) # Add a node:prop:set rule outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --type role --name creator --rule node:prop:set --prop ' 'foo --form strform') self.true( outp.expect( "('node:prop:set', {'form': 'strform', 'prop': 'foo'})")) # We can elevate user to admin outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --name user@localhost --admin') self.true(outp.expect("('user@localhost', {'admin': True")) # Uprox can now do admin things like remove his admin bit outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(uprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act del --name user@localhost --admin') self.true(outp.expect("('user@localhost', {'admin': False")) # We can have json output outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine('auth --name root@localhost --json') self.true(outp.expect(json.dumps(retn, sort_keys=True, indent=2))) # Cannot make rules with invalid combinations outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --type role --name creator --rule node:add --tag yes --form ' 'intform') self.true( outp.expect('Cannot form rulefo with tag and (form OR prop)')) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --type role --name creator --rule node:add --tag yes --prop ' 'intform:foo') self.true( outp.expect('Cannot form rulefo with tag and (form OR prop)')) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --type role --name creator --rule node:add --prop clown:foo' ) self.true(outp.expect('Unable to form rulefo')) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine( 'auth --act add --type role --name creator --rule node:add') self.true(outp.expect('Unable to form rulefo')) outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(rprox, outp=outp) retn = cmdr.runCmdLine('auth --act add --type role') self.true(outp.expect('Action requires a name'))
def test_cmds_quit(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) cmdr.runCmdLine('quit') self.true(str(outp).find('o/') != -1)
def test_cmds_quit(self): with self.getDmonCore() as core: outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(core, outp=outp) cmdr.runCmdLine('quit') self.true(outp.expect('o/'))
def test_cmds_guid(self): with self.getDmonCore() as core: outp = self.getTestOutp() cmdr = s_cmdr.getItemCmdr(core, outp=outp) cmdr.runCmdLine('guid') self.true(outp.expect('new guid:'))
def getCoreCmdr(self, core): outp = s_output.OutPutStr() return s_cmdr.getItemCmdr(core, outp=outp)
def test_cmds_py(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) cmdr.runCmdLine('py 20 + 20') self.ne(str(outp).find('40'), -1)
def test_cmds_guid(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) cmdr.runCmdLine('guid') self.ne(str(outp).find('new guid:'), -1)
def test_cmds_ask_noopts(self): with self.getDmonCore() as core: outp = s_output.OutPutStr() cmdr = s_cmdr.getItemCmdr(core, outp=outp) cmdr.runCmdLine('ask') self.nn(re.search('Examples:', str(outp)))