Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
rakuy0 committed Sep 17, 2024
1 parent 86266ce commit 19dd6b3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
5 changes: 3 additions & 2 deletions synapse/cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4395,8 +4395,9 @@ async def addHttpExtApi(self, adef):
if adef.get('iden') is None:
adef['iden'] = s_common.guid()

if self._exthttpapis.get(adef['iden']) is not None:
raise s_exc.DupIden(mesg=f'Duplicate iden specified for Extended HTTP API: {adef["iden"]}')
iden = adef['iden']
if self._exthttpapis.get(iden) is not None:
raise s_exc.DupIden(mesg=f'Duplicate iden specified for Extended HTTP API: {iden}', iden=iden)

adef['created'] = s_common.now()
adef['updated'] = adef['created']
Expand Down
2 changes: 1 addition & 1 deletion synapse/lib/stormlib/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ class User(s_stormtypes.Prim):
''',
'type': {'type': 'function', '_funcname': '_methUserSetRoles',
'args': (
{'name': 'idens', 'type': 'list', 'desc': 'The idens to of the Role.', },
{'name': 'idens', 'type': 'list', 'desc': 'The idens of the Roles to set on the User.', },
),
'returns': {'type': 'null', }}},
{'name': 'revoke', 'desc': 'Remove a Role from the User',
Expand Down
5 changes: 4 additions & 1 deletion synapse/lib/stormlib/cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,8 @@ class CortexHttpApi(s_stormtypes.Lib):
'desc': 'Require the API endpoint to be authenticated.', 'default': True},
{'name': 'readonly', 'type': 'boolean',
'desc': 'Run the Extended HTTP Storm methods in readonly mode.', 'default': False},
{'name': 'iden', 'type': 'str',
'desc': 'An iden for the new Extended HTTP API', 'default': None},
),
'returns': {'type': 'http:api', 'desc': 'A new ``http:api`` object.'}}},
{'name': 'del', 'desc': 'Delete an Extended HTTP API endpoint.',
Expand Down Expand Up @@ -1196,7 +1198,7 @@ async def listHttpApis(self):
apis = [HttpApi(self.runt, adef) for adef in adefs]
return apis

async def addHttpApi(self, path, name='', desc='', runas='owner', authenticated=True, readonly=False):
async def addHttpApi(self, path, name='', desc='', runas='owner', authenticated=True, readonly=False, iden=None):
s_stormtypes.confirm(('storm', 'lib', 'cortex', 'httpapi', 'add'))

path = await s_stormtypes.tostr(path)
Expand All @@ -1207,6 +1209,7 @@ async def addHttpApi(self, path, name='', desc='', runas='owner', authenticated=
authenticated = await s_stormtypes.tobool(authenticated)

adef = {
'iden': iden,
'path': path,
'view': self.runt.snap.view.iden,
'runas': runas,
Expand Down
3 changes: 2 additions & 1 deletion synapse/tests/test_cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -7969,13 +7969,14 @@ async def test_cortex_ext_httpapi(self):
'view': view.iden
})

with self.raises(s_exc.DupIden):
with self.raises(s_exc.DupIden) as ectx:
await core.addHttpExtApi({
'iden': othr,
'path': 'bad/dup',
'owner': unfo.get('iden'),
'view': view.iden
})
self.eq(ectx.exception.get('iden'), othr)

with self.raises(s_exc.SynErr):
await core.setHttpApiIndx(newp, 0)
Expand Down
25 changes: 21 additions & 4 deletions synapse/tests/test_lib_stormlib_cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import synapse.tests.utils as s_test

from pprint import pprint

class CortexLibTest(s_test.SynTest):

async def test_libcortex_httpapi_methods(self):
Expand Down Expand Up @@ -63,11 +61,16 @@ async def test_libcortex_httpapi_methods(self):
'''
testpath00 = await core.callStorm(q)

iden = s_common.guid()
q = '''
$api = $lib.cortex.httpapi.add(nomeths)
$api = $lib.cortex.httpapi.add(nomeths, iden=$iden)
return ( $api.iden )
'''
nomeths = await core.callStorm(q)
nomeths = await core.callStorm(q, opts={'vars': {'iden': iden}})
self.eq(iden, nomeths)

adef = await core.getHttpExtApi(iden)
self.nn(adef)

info = await core.callStorm('return( $lib.cortex.httpapi.get($iden).pack() )',
opts={'vars': {'iden': testpath00}})
Expand All @@ -89,6 +92,20 @@ async def test_libcortex_httpapi_methods(self):
q = '$api = $lib.cortex.httpapi.get($iden) $api.methods.wildycustomverb = ${}'
await core.callStorm(q, opts={'vars': {'iden': testpath00}})

with self.raises(s_exc.DupIden):
q = '''
$api = $lib.cortex.httpapi.add(duplicate, iden=$iden)
return ( $api.iden )
'''
await core.callStorm(q, opts={'vars': {'iden': iden}})

with self.raises(s_exc.SchemaViolation):
q = '''
$api = $lib.cortex.httpapi.add(duplicate, iden="trollolololol")
return ( $api.iden )
'''
await core.callStorm(q, opts={'vars': {'iden': iden}})

async with self.getHttpSess(auth=('root', 'root'), port=hport) as sess: # type: aiohttp.ClientSession
resp = await sess.get(f'https://localhost:{hport}/api/ext/testpath00')
self.eq(resp.status, 200)
Expand Down

0 comments on commit 19dd6b3

Please sign in to comment.