Skip to content

Commit

Permalink
Merge branch 'master' into visi-model-20201113
Browse files Browse the repository at this point in the history
  • Loading branch information
invisig0th committed Nov 25, 2020
2 parents 383bc64 + 57f222c commit cbf518d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions synapse/cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,7 @@ def _initCortexHttpApi(self):
'''
self.addHttpApi('/api/v1/storm', s_httpapi.StormV1, {'cell': self})
self.addHttpApi('/api/v1/watch', s_httpapi.WatchSockV1, {'cell': self})
self.addHttpApi('/api/v1/storm/call', s_httpapi.StormCallV1, {'cell': self})
self.addHttpApi('/api/v1/storm/nodes', s_httpapi.StormNodesV1, {'cell': self})
self.addHttpApi('/api/v1/reqvalidstorm', s_httpapi.ReqValidStormV1, {'cell': self})

Expand Down
30 changes: 30 additions & 0 deletions synapse/lib/httpapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,36 @@ async def get(self):
self.write(json.dumps(mesg))
await self.flush()

class StormCallV1(Handler):

async def post(self):
return await self.get()

async def get(self):

user, body = await self.getUserBody()
if body is s_common.novalu:
return

# dont allow a user to be specified
opts = body.get('opts')
query = body.get('query')

opts = await self._reqValidOpts(opts)

try:
ret = await self.cell.callStorm(query, opts=opts)
except s_exc.SynErr as e:
mesg = e.get('mesg', str(e))
return self.sendRestErr(e.__class__.__name__, mesg)
except asyncio.CancelledError: # pragma: no cover
raise
except Exception as e:
mesg = str(e)
return self.sendRestErr(e.__class__.__name__, mesg)
else:
return self.sendRestRetn(ret)

class ReqValidStormV1(Handler):

async def post(self):
Expand Down
37 changes: 36 additions & 1 deletion synapse/tests/test_cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ async def test_cortex_edges(self):
'walk operation expected a string or list. got: 0.')

async def test_cortex_callstorm(self):
async with self.getTestCore() as core:
async with self.getTestCore(conf={'auth:passwd': 'root'}) as core:
self.eq('asdf', await core.callStorm('return (asdf)'))
async with core.getLocalProxy() as proxy:
self.eq('qwer', await proxy.callStorm('return (qwer)'))
Expand All @@ -236,6 +236,41 @@ async def test_cortex_callstorm(self):
await proxy.callStorm(q)
self.eq(cm.exception.get('errx'), 'StormExit')

host, port = await core.addHttpsPort(0, host='127.0.0.1')

async with self.getHttpSess() as sess:
async with sess.post(f'https://localhost:{port}/api/v1/login',
json={'user': 'root', 'passwd': 'root'}) as resp:
retn = await resp.json()
self.eq('ok', retn.get('status'))
self.eq('root', retn['result']['name'])

body = {'query': 'return (asdf)'}
async with sess.get(f'https://localhost:{port}/api/v1/storm/call', json=body) as resp:
retn = await resp.json()
self.eq('ok', retn.get('status'))
self.eq('asdf', retn['result'])

body = {'query': '$foo=$lib.list() $bar=$foo.index(10) return ( $bar )'}
async with sess.get(f'https://localhost:{port}/api/v1/storm/call', json=body) as resp:
retn = await resp.json()
self.eq('err', retn.get('status'))
self.eq('StormRuntimeError', retn.get('code'))
self.eq('list index out of range', retn.get('mesg'))

body = {'query': 'return ( $lib.exit() )'}
async with sess.post(f'https://localhost:{port}/api/v1/storm/call', json=body) as resp:
retn = await resp.json()
self.eq('err', retn.get('status'))
self.eq('StormExit', retn.get('code'))
self.eq('', retn.get('mesg'))

# No body
async with sess.get(f'https://localhost:{port}/api/v1/storm/call') as resp:
retn = await resp.json()
self.eq('err', retn.get('status'))
self.eq('SchemaViolation', retn.get('code'))

async def test_cortex_storm_dmon_log(self):

async with self.getTestCore() as core:
Expand Down

0 comments on commit cbf518d

Please sign in to comment.