Skip to content

Commit 109856d

Browse files
henrybarretogustavosbarreto
authored andcommitted
fix(api,ssh): remove direct-tcpip ssh event
1 parent e267025 commit 109856d

File tree

5 files changed

+508
-125
lines changed

5 files changed

+508
-125
lines changed

api/store/mongo/migrations/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func GenerateMigrations() []migrate.Migration {
109109
migration97,
110110
migration98,
111111
migration99,
112+
migration100,
112113
}
113114
}
114115

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
"github.com/sirupsen/logrus"
7+
migrate "github.com/xakep666/mongo-migrate"
8+
"go.mongodb.org/mongo-driver/bson"
9+
"go.mongodb.org/mongo-driver/mongo"
10+
)
11+
12+
var migration100 = migrate.Migration{
13+
Version: 100,
14+
Description: "Remove direct-tcpip events",
15+
Up: migrate.MigrationFunc(func(ctx context.Context, db *mongo.Database) error {
16+
logrus.WithFields(logrus.Fields{
17+
"component": "migration",
18+
"version": 100,
19+
"action": "Up",
20+
}).Info("Applying migration: 100")
21+
22+
pipeline := mongo.Pipeline{
23+
{{Key: "$match", Value: bson.M{"type": "direct-tcpip"}}},
24+
{{Key: "$group", Value: bson.M{
25+
"_id": "$session",
26+
"seats": bson.M{"$addToSet": "$seat"},
27+
}}},
28+
}
29+
30+
cursor, err := db.Collection("sessions_events").Aggregate(ctx, pipeline)
31+
if err != nil {
32+
logrus.WithError(err).Error("Failed to aggregate direct-tcpip seats")
33+
34+
return err
35+
}
36+
37+
defer cursor.Close(ctx)
38+
39+
for cursor.Next(ctx) {
40+
var result struct {
41+
ID string `bson:"_id"`
42+
Seats []int `bson:"seats"`
43+
}
44+
45+
if err := cursor.Decode(&result); err != nil {
46+
logrus.WithError(err).Error("Failed to decode aggregation result")
47+
48+
return err
49+
}
50+
51+
_, err := db.Collection("sessions").UpdateOne(
52+
ctx,
53+
bson.M{"uid": result.ID},
54+
bson.M{"$pullAll": bson.M{
55+
"events.seats": result.Seats,
56+
}},
57+
)
58+
if err != nil {
59+
logrus.WithError(err).WithField("session_uid", result.ID).Error("Failed to remove seats from session")
60+
61+
return err
62+
}
63+
}
64+
65+
if err := cursor.Err(); err != nil {
66+
logrus.WithError(err).Error("Cursor error during seat removal")
67+
68+
return err
69+
}
70+
71+
_, err = db.Collection("sessions").UpdateMany(
72+
ctx,
73+
bson.M{"events.types": "direct-tcpip"},
74+
bson.M{"$pull": bson.M{
75+
"events.types": "direct-tcpip",
76+
}},
77+
)
78+
if err != nil {
79+
logrus.WithError(err).Error("Failed to remove direct-tcpip from events.types in sessions")
80+
81+
return err
82+
}
83+
84+
_, err = db.Collection("sessions_events").DeleteMany(
85+
ctx,
86+
bson.M{"type": "direct-tcpip"},
87+
)
88+
if err != nil {
89+
logrus.WithError(err).Error("Failed to remove direct-tcpip events from sessions_events")
90+
91+
return err
92+
}
93+
94+
_, err = db.Collection("sessions").UpdateMany(
95+
ctx,
96+
bson.M{
97+
"recorded": true,
98+
"events.types": bson.M{"$size": 0},
99+
},
100+
bson.M{"$set": bson.M{"recorded": false}},
101+
)
102+
if err != nil {
103+
logrus.WithError(err).Error("Failed to update recorded flag for sessions with empty events.types")
104+
105+
return err
106+
}
107+
108+
return nil
109+
}),
110+
Down: migrate.MigrationFunc(func(ctx context.Context, db *mongo.Database) error {
111+
logrus.WithFields(logrus.Fields{
112+
"component": "migration",
113+
"version": 100,
114+
"action": "Down",
115+
}).Info("Reverting migration: 100")
116+
117+
_, err := db.Collection("sessions").UpdateMany(
118+
ctx,
119+
bson.M{
120+
"recorded": false,
121+
"events.types": bson.M{"$size": 0},
122+
},
123+
bson.M{"$set": bson.M{"recorded": true}},
124+
)
125+
if err != nil {
126+
logrus.WithError(err).Error("Failed to revert recorded flag changes")
127+
128+
return err
129+
}
130+
131+
logrus.WithFields(logrus.Fields{
132+
"component": "migration",
133+
"version": 100,
134+
}).Warn("Cannot restore deleted direct-tcpip events and seats - data loss is permanent")
135+
136+
return nil
137+
}),
138+
}

0 commit comments

Comments
 (0)