Skip to content

Commit e9aaf4f

Browse files
authored
fix(migrations): Added migration to remove admin_rights_count property from User nodes (#1339)
* fix(migrations): Adds a migration to remove the unused admin_rights_count property This creates a graph migration for version 7.3.0 that removes adminrightscount from all user nodes where adminrightscount is set Closes BED-4886
1 parent 19ee6c8 commit e9aaf4f

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

cmd/api/src/migrations/manifest.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ func RequiresMigration(ctx context.Context, db graph.Database) (bool, error) {
4747
}
4848
}
4949

50+
// Version_730_Migration removes the leftover 'adminrightscount' property from User nodes
51+
func Version_730_Migration(ctx context.Context, db graph.Database) error {
52+
const adminRightsCount = "adminrightscount"
53+
54+
defer measure.LogAndMeasure(slog.LevelInfo, "Migration to remove admin_rights_count property from user nodes")
55+
56+
return db.WriteTransaction(ctx, func(tx graph.Transaction) error {
57+
// MATCH(n:User) WHERE n.adminrightscount <> null
58+
if nodes, err := ops.FetchNodes(tx.Nodes().Filterf(func() graph.Criteria {
59+
return query.And(
60+
query.Kind(query.Node(), ad.User),
61+
query.IsNotNull(query.NodeProperty(adminRightsCount)),
62+
)
63+
})); err != nil {
64+
return err
65+
} else {
66+
for _, node := range nodes {
67+
node.Properties.Delete(adminRightsCount)
68+
if err := tx.UpdateNode(node); err != nil {
69+
return err
70+
}
71+
}
72+
73+
return nil
74+
}
75+
})
76+
}
77+
5078
// Version_620_Migration is intended to rename the RemoteInteractiveLogonPrivilege edge to RemoteInteractiveLogonRight
5179
// See: https://specterops.atlassian.net/browse/BED-4428
5280
func Version_620_Migration(ctx context.Context, db graph.Database) error {
@@ -62,7 +90,7 @@ func Version_620_Migration(ctx context.Context, db graph.Database) error {
6290
edgeProperties := graph.NewProperties()
6391
edgeProperties.Set(common.LastSeen.String(), time.Now().UTC())
6492

65-
//Get all RemoteInteractiveLogonPrivilege edges, use the start/end ids to insert new edges, and delete the old ones
93+
// Get all RemoteInteractiveLogonPrivilege edges, use the start/end ids to insert new edges, and delete the old ones
6694
return db.BatchOperation(ctx, func(batch graph.Batch) error {
6795
rels, err := ops.FetchRelationships(batch.Relationships().Filter(targetCriteria))
6896
if err != nil {
@@ -200,7 +228,7 @@ func Version_277_Migration(ctx context.Context, db graph.Database) error {
200228
node.Properties.Set(common.ObjectID.String(), strings.ToUpper(objectId))
201229
}
202230

203-
//We may not always get these properties, so ignore errors
231+
// We may not always get these properties, so ignore errors
204232
if operatingSystem, err := node.Properties.Get(common.OperatingSystem.String()).String(); err == nil && operatingSystem != "" && operatingSystem != strings.ToUpper(operatingSystem) {
205233
dirty = true
206234
node.Properties.Set(common.OperatingSystem.String(), strings.ToUpper(operatingSystem))
@@ -284,7 +312,7 @@ var Manifest = []Migration{
284312
}
285313

286314
return db.WriteTransaction(ctx, func(tx graph.Transaction) error {
287-
//Remove ADLocalGroup label from all nodes that also have the group label
315+
// Remove ADLocalGroup label from all nodes that also have the group label
288316
if nodes, err := ops.FetchNodes(tx.Nodes().Filterf(func() graph.Criteria {
289317
return query.And(
290318
query.Kind(query.Node(), ad.LocalGroup),
@@ -301,7 +329,7 @@ var Manifest = []Migration{
301329
}
302330
}
303331

304-
//Delete all local group edges
332+
// Delete all local group edges
305333
if err := tx.Relationships().Filterf(func() graph.Criteria {
306334
return query.And(
307335
query.Or(
@@ -335,6 +363,10 @@ var Manifest = []Migration{
335363
Version: version.Version{Major: 6, Minor: 2, Patch: 0},
336364
Execute: Version_620_Migration,
337365
},
366+
{
367+
Version: version.Version{Major: 7, Minor: 3, Patch: 0},
368+
Execute: Version_730_Migration,
369+
},
338370
}
339371

340372
func LatestGraphMigrationVersion() version.Version {

0 commit comments

Comments
 (0)