Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSquires committed Sep 16, 2024
1 parent 1dab5fe commit 14dbcf3
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
32 changes: 32 additions & 0 deletions synapse/assets/storm/migrations/model-0.2.28.storm
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,38 @@ function migrateCpeNode(oldcpe, newcpe) {
// scoped to the whole script.
function main() {

$views = $lib.view.list(deporder=(true))
$views.reverse()

// Iterate views in reverse deporder
for $view in $views {
if (not $layers.has($view.layers.0.iden)) { continue }

view.exec $view.iden {
$layer = $lib.layer.get()

// Get sodes by :v2_2. We're looking for :v2_2 props set incorrectly
// in upper views
for $sode in $layer.getStorNodesByForm(it:sec:cpe) {
($buid, $info) = $sode

// If the valu is present on the sode, this is a full node in
// this view, go around
if $info.valu { continue }

// This is a floating :v2_2 prop. Remove it because we don't
// know if the underlying node will eventually be deleted.
// There are two options:
// - The underlying node is deleted in which case, this floating
// :v2_2 prop will be dangling in the layer (very bad)
// - The underlying node is fixed and migrated in which case,
// the prop being set in this layer shouldn't matter
yield $sode
[ -:v2_2 ]
}
}
}

for $view in $lib.view.list(deporder=$lib.true) {

if (not $layers.has($view.layers.0.iden)) { continue }
Expand Down
60 changes: 60 additions & 0 deletions synapse/lib/stormlib/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,59 @@ class LibModelMigration(s_stormtypes.Lib, MigrationEditorMixin):
'returns': {'type': 'null', }}},
)
_storm_lib_path = ('model', 'migration')
# _storm_query = ''' // storm
# _cpeData = ({})

# function _loadQueueData() {
# $nodesq = $lib.queue.get("model_0_2_28:nodes")
# $edgesq = $lib.queue.get("model_0_2_28:nodes:edges")
# $refsq = $lib.queue.get("model_0_2_28:nodes:refs")

# for $ii in $lib.range($nodesq.size()) {
# $item = $nodesq.get($ii, cull=(false))
# $nidn = $item.1.iden
# if ($iden and $iden != $nidn) { continue }
# $nodes.$nidn = $item.1
# }

# for $ii in $lib.range($edgesq.size()) {
# $item = $edgesq.get($ii, cull=(false))
# $nidn = $item.1.iden
# if ($iden and $iden != $nidn) { continue }
# if (not $nodes.$nidn.edges) { $nodes.$nidn.edges = ([]) }
# $nodes.$nidn.edges.append($item.1)
# }

# for $ii in $lib.range($refsq.size()) {
# $item = $refsq.get($ii, cull=(false))
# $nidn = $item.1.iden
# if ($iden and $iden != $nidn) { continue }
# if (not $nodes.$nidn.refs) { $nodes.$nidn.refs = ([]) }
# $nodes.$nidn.refs.append($item.1)
# }
# }

# function repairNode(iden, newval) {
# $oldcpe = $getNode($iden)

# try {
# $view = $lib.view.get($oldcpe.view)
# } except NoSuchView as exc {
# $lib.warn(`Cannot restore node {$iden}, view {$oldcpe.view} does not exist.`)
# return()
# }

# $wlyr = $view.layers.0
# if (not $lib.user.allowed("node", gateiden=$wlyr.iden)) {
# $lib.warn(`Cannot restore node, user {$lib.user.name()} does not have write access to view {$oldcpe.view}.`)
# return()
# }



# }

# '''

def getObjLocals(self):
return {
Expand Down Expand Up @@ -1268,3 +1321,10 @@ async def _riskHasVulnToVulnerable(self, n, nodata=False):
await self.copyData(n, proto, overwrite=False)

return retidens

# @s_stormtypes.registry.registerLib
# class LibModelMigrations_0_2_28(s_stormtypes.Lib):
# '''
# A Storm library with helper functions for the 0.2.28 model it:sec:cpe migration.
# '''
# _storm_lib_path = ('model', 'migration', 's', 'model_0_2_28')
46 changes: 43 additions & 3 deletions synapse/tests/test_lib_modelrev.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ async def test_modelrev_0_2_28(self):
# some lifting/pivoting won't work right.

views = await core.callStorm('return($lib.view.list(deporder=$lib.true))')
self.len(2, views)
self.len(3, views)

fork00 = views[1].get('iden')
infork00 = {'view': fork00}
Expand Down Expand Up @@ -642,7 +642,7 @@ async def test_modelrev_0_2_28(self):
async with self.getRegrCore('model-0.2.28') as core:

views = await core.callStorm('return($lib.view.list(deporder=$lib.true))')
self.len(2, views)
self.len(3, views)

fork00 = views[1].get('iden')
infork00 = {'view': fork00}
Expand Down Expand Up @@ -838,7 +838,7 @@ async def test_modelrev_0_2_28(self):
async with self.getRegrCore('model-0.2.28') as core:

views = await core.callStorm('return($lib.view.list(deporder=$lib.true))')
self.len(2, views)
self.len(3, views)

fork00 = views[1].get('iden') # forked view
forklayr = views[1].get('layers')[0].get('iden')
Expand Down Expand Up @@ -1167,6 +1167,46 @@ async def test_modelrev_0_2_28(self):
'view': fork00}),
])

async with self.getRegrCore('model-0.2.28') as core:

views = await core.callStorm('return($lib.view.list(deporder=$lib.true))')
self.len(3, views)

fork01 = views[2].get('iden') # forked view
infork01 = {'view': fork01}

# Normal lift will go through the views
nodes = await core.nodes('it:sec:cpe:vendor=01generator', opts=infork01)
self.len(1, nodes)
self.eq(nodes[0].get('v2_2'), 'cpe:/a:01generator:pireospay:-::~~~prestashop~~')

# Lift by prop from this layer should return nothing
q = '''
$nodes = ([])
for $n in $lib.view.get().layers.0.liftByProp("it:sec:cpe:v2_2") {
$nodes.append($n)
}
return($nodes)
'''
nodes = await core.callStorm(q, opts=infork01)
self.len(0, nodes)

# Lift by sodes should return nothing
# Lift by prop from this layer should return nothing
q = '''
$nodes = ([])
for $n in $lib.view.get().layers.0.getStorNodesByForm("it:sec:cpe") {
$nodes.append($n)
}
return($nodes)
'''
nodes = await core.callStorm(q, opts=infork01)
self.len(0, nodes)

async def test_modelrev_0_2_29(self):
async with self.getRegrCore('model-0.2.29') as core:
self.len(2, await core.nodes('ou:industry:type:taxonomy'))

0 comments on commit 14dbcf3

Please sign in to comment.