Skip to content

Commit fc19301

Browse files
committed
Use 'nil' instead of empty map in 'catcher' calls system. Improve code readability and reduce memory allocation overhead.
1 parent 9af3754 commit fc19301

File tree

8 files changed

+51
-51
lines changed

8 files changed

+51
-51
lines changed

agent-manager/agent/agent_command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (s *Grpc) ListAgentCommands(ctx context.Context, req *ListRequest) (*ListAg
1616

1717
commands, total, err := agentCommandService.ListAgentCommands(page, filter)
1818
if err != nil {
19-
catcher.Error("failed to fetch agents", err, map[string]any{})
19+
catcher.Error("failed to fetch agents", err, nil)
2020
return nil, status.Errorf(codes.Internal, "failed to fetch agents: %v", err)
2121
}
2222

agent-manager/agent/agent_imp.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (s *Grpc) RegisterAgent(ctx context.Context, req *AgentRequest) (*AuthRespo
4949
agent.AgentKey = key
5050
err = agentService.Create(agent)
5151
if err != nil {
52-
catcher.Error("Failed to create agent", err, map[string]any{})
52+
catcher.Error("Failed to create agent", err, nil)
5353
return nil, err
5454
}
5555

@@ -59,7 +59,7 @@ func (s *Grpc) RegisterAgent(ctx context.Context, req *AgentRequest) (*AuthRespo
5959

6060
err = lastSeenService.Set(key, time.Now())
6161
if err != nil {
62-
catcher.Error("Failed to set last seen", err, map[string]any{})
62+
catcher.Error("Failed to set last seen", err, nil)
6363
return nil, err
6464
}
6565
res := &AuthResponse{
@@ -89,7 +89,7 @@ func (s *Grpc) UpdateAgent(ctx context.Context, req *AgentRequest) (*AuthRespons
8989

9090
agent, err := agentService.FindByID(uint(id))
9191
if err != nil {
92-
catcher.Error("Failed to find agent", err, map[string]any{})
92+
catcher.Error("Failed to find agent", err, nil)
9393
return nil, err
9494
}
9595

@@ -120,7 +120,7 @@ func (s *Grpc) UpdateAgent(ctx context.Context, req *AgentRequest) (*AuthRespons
120120

121121
err = agentService.Update(agent)
122122
if err != nil {
123-
catcher.Error("Failed to update agent", err, map[string]any{})
123+
catcher.Error("Failed to update agent", err, nil)
124124
return nil, err
125125
}
126126

@@ -147,7 +147,7 @@ func (s *Grpc) DeleteAgent(ctx context.Context, req *AgentDelete) (*AuthResponse
147147

148148
id, err := agentService.Delete(uuid.MustParse(key), req.DeletedBy)
149149
if err != nil {
150-
catcher.Error("Unable to delete agent", err, map[string]any{})
150+
catcher.Error("Unable to delete agent", err, nil)
151151
return &AuthResponse{}, status.Error(codes.Internal, fmt.Sprintf("unable to delete agent: %v", err.Error()))
152152
}
153153

@@ -174,7 +174,7 @@ func (s *Grpc) ListAgents(ctx context.Context, req *ListRequest) (*ListAgentsRes
174174

175175
agents, total, err := agentService.ListAgents(page, filter)
176176
if err != nil {
177-
catcher.Error("failed to fetch agents", err, map[string]any{})
177+
catcher.Error("failed to fetch agents", err, nil)
178178
return nil, status.Errorf(codes.Internal, "failed to fetch agents: %v", err)
179179
}
180180
return convertToAgentResponse(agents, total)
@@ -203,7 +203,7 @@ func (s *Grpc) AgentStream(stream AgentService_AgentStreamServer) error {
203203
delete(s.AgentStreamMap, agentKey)
204204
s.agentStreamMutex.Unlock()
205205

206-
catcher.Error("failed to reconnect to client", err, map[string]any{})
206+
catcher.Error("failed to reconnect to client", err, nil)
207207
return fmt.Errorf("failed to reconnect to client: %v", err)
208208
}
209209

@@ -245,7 +245,7 @@ func (s *Grpc) AgentStream(stream AgentService_AgentStreamServer) error {
245245
},
246246
},
247247
}); err != nil {
248-
catcher.Error("Failed to send result to server", err, map[string]any{})
248+
catcher.Error("Failed to send result to server", err, nil)
249249
}
250250
s.resultChannelM.Lock()
251251
if resultChan, ok := s.ResultChannel[cmdID]; ok {
@@ -347,25 +347,25 @@ func (s *Grpc) ProcessCommand(stream PanelService_ProcessCommandServer) error {
347347

348348
func (s *Grpc) UpdateAgentGroup(ctx context.Context, req *AgentGroupUpdate) (*Agent, error) {
349349
if req.AgentId == 0 || req.AgentGroup == 0 {
350-
catcher.Error("Error in req", nil, map[string]any{})
350+
catcher.Error("Error in req", nil, nil)
351351
return nil, status.Errorf(codes.FailedPrecondition, "error in req")
352352
}
353353
agent, err := agentService.UpdateAgentGroup(uint(req.AgentId), uint(req.AgentGroup))
354354
if err != nil {
355-
catcher.Error("Unable to update group", err, map[string]any{})
355+
catcher.Error("Unable to update group", err, nil)
356356
return nil, status.Errorf(codes.Internal, "unable to update group: %v", err)
357357
}
358358
return parseAgentToProto(agent), nil
359359
}
360360

361361
func (s *Grpc) GetAgentByHostname(ctx context.Context, req *Hostname) (*Agent, error) {
362362
if req.Hostname == "" {
363-
catcher.Error("Error in req", nil, map[string]any{})
363+
catcher.Error("Error in req", nil, nil)
364364
return nil, status.Errorf(codes.FailedPrecondition, "error in req")
365365
}
366366
agent, err := agentService.FindByHostname(req.Hostname)
367367
if err != nil {
368-
catcher.Error("Unable to find agent with hostname", err, map[string]any{})
368+
catcher.Error("Unable to find agent with hostname", err, nil)
369369
return nil, status.Errorf(codes.NotFound, "unable to find agent with hostname: %v", err)
370370
}
371371
return parseAgentToProto(*agent), nil
@@ -377,7 +377,7 @@ func (s *Grpc) UpdateAgentType(ctx context.Context, req *AgentTypeUpdate) (*Agen
377377
}
378378
agent, err := agentService.UpdateAgentType(uint(req.AgentId), uint(req.AgentType))
379379
if err != nil {
380-
catcher.Error("Unable to update type", err, map[string]any{})
380+
catcher.Error("Unable to update type", err, nil)
381381
return nil, status.Errorf(codes.Internal, "unable to update type: %v", err)
382382
}
383383
return parseAgentToProto(agent), nil
@@ -388,7 +388,7 @@ func (s *Grpc) LoadAgentCacheFromDatabase() error {
388388
// Fill the agentCache map with agentID and agentToken pairs
389389
agents, err := agentService.FindAll()
390390
if err != nil {
391-
catcher.Error("Failed to fetch agents from database", err, map[string]any{})
391+
catcher.Error("Failed to fetch agents from database", err, nil)
392392
return err
393393
}
394394
for _, agent := range agents {
@@ -404,7 +404,7 @@ func (s *Grpc) ListAgentsWithCommands(ctx context.Context, req *ListRequest) (*L
404404

405405
agents, total, err := agentService.ListAgentWithCommands(page, filter)
406406
if err != nil {
407-
catcher.Error("failed to fetch agents", err, map[string]any{})
407+
catcher.Error("failed to fetch agents", err, nil)
408408
return nil, status.Errorf(codes.Internal, "failed to fetch agents: %v", err)
409409
}
410410

@@ -460,14 +460,14 @@ func createHistoryCommand(cmd *UtmCommand, cmdID string) {
460460
}
461461
err := agentCommandService.Create(cmdHistory)
462462
if err != nil {
463-
catcher.Error("Unable to create a new command history", err, map[string]any{})
463+
catcher.Error("Unable to create a new command history", err, nil)
464464
}
465465
}
466466

467467
func updateHistoryCommand(cmdResult *CommandResult, cmdID string) {
468468
err := agentCommandService.UpdateCommandStatusAndResult(findAgentIdByKey(CacheAgent, cmdResult.AgentKey), cmdID, models.Executed, cmdResult.Result)
469469
if err != nil {
470-
catcher.Error("Failed to update command status", nil, map[string]any{})
470+
catcher.Error("Failed to update command status", nil, nil)
471471
}
472472
}
473473

agent-manager/agent/collector_imp.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (s *Grpc) RegisterCollector(ctx context.Context, req *RegisterRequest) (*Au
4040
collector.CollectorKey = key
4141
err = collectorService.Create(collector)
4242
if err != nil {
43-
catcher.Error("Failed to create collector", err, map[string]any{})
43+
catcher.Error("Failed to create collector", err, nil)
4444
return nil, err
4545
}
4646

@@ -50,7 +50,7 @@ func (s *Grpc) RegisterCollector(ctx context.Context, req *RegisterRequest) (*Au
5050

5151
err = lastSeenService.Set(key, time.Now())
5252
if err != nil {
53-
catcher.Error("Failed to set last seen", err, map[string]any{})
53+
catcher.Error("Failed to set last seen", err, nil)
5454
return nil, err
5555
}
5656
res := &AuthResponse{
@@ -76,7 +76,7 @@ func (s *Grpc) DeleteCollector(ctx context.Context, req *CollectorDelete) (*Auth
7676

7777
id, err := collectorService.Delete(uuid.MustParse(key), req.DeletedBy)
7878
if err != nil {
79-
catcher.Error("unable to delete collector", err, map[string]any{})
79+
catcher.Error("unable to delete collector", err, nil)
8080
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to delete collector: %v", err.Error()))
8181
}
8282

@@ -103,7 +103,7 @@ func (s *Grpc) ListCollector(ctx context.Context, req *ListRequest) (*ListCollec
103103

104104
collectors, total, err := collectorService.ListCollectors(page, filter)
105105
if err != nil {
106-
catcher.Error("failed to fetch collectors", err, map[string]any{})
106+
catcher.Error("failed to fetch collectors", err, nil)
107107
return nil, status.Errorf(codes.Internal, "failed to fetch collectors: %v", err)
108108
}
109109
return convertToCollectorResponse(collectors, total)
@@ -131,7 +131,7 @@ func (s *Grpc) ProcessPendingConfigs() {
131131
if ok {
132132
collector, err := collectorService.GetByKey(key)
133133
if err != nil {
134-
catcher.Error("unable to get collector config to send config to stream", err, map[string]any{})
134+
catcher.Error("unable to get collector config to send config to stream", err, nil)
135135
continue
136136
}
137137

@@ -141,7 +141,7 @@ func (s *Grpc) ProcessPendingConfigs() {
141141
},
142142
})
143143
if err != nil {
144-
catcher.Error("failed to send config to collector", err, map[string]any{})
144+
catcher.Error("failed to send config to collector", err, nil)
145145
}
146146
}
147147
}
@@ -172,7 +172,7 @@ func (s *Grpc) CollectorStream(stream CollectorService_CollectorStreamServer) er
172172
delete(s.CollectorStreamMap, collectorKey)
173173
s.collectorStreamMutex.Unlock()
174174

175-
catcher.Error("failed to reconnect to client", err, map[string]any{})
175+
catcher.Error("failed to reconnect to client", err, nil)
176176
return fmt.Errorf("failed to reconnect to client: %v", err)
177177
}
178178

@@ -224,7 +224,7 @@ func (s *Grpc) GetCollectorConfig(ctx context.Context, in *ConfigRequest) (*Coll
224224

225225
collector, err := collectorService.GetByKey(key)
226226
if err != nil {
227-
catcher.Error("unable to get collector config", err, map[string]any{})
227+
catcher.Error("unable to get collector config", err, nil)
228228
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to get collector config: %v", err.Error()))
229229
}
230230

@@ -252,7 +252,7 @@ func (s *Grpc) RegisterCollectorConfig(ctx context.Context, in *CollectorConfig)
252252

253253
err = collectorService.SaveCollectorConfigs(collectorConf, collector.ID)
254254
if err != nil {
255-
catcher.Error("error saving collector configuration", err, map[string]any{})
255+
catcher.Error("error saving collector configuration", err, nil)
256256
return nil, status.Errorf(codes.Internal, "error saving collector configuration: %v", err.Error())
257257
}
258258

@@ -272,7 +272,7 @@ func (s *Grpc) ListCollectorHostnames(ctx context.Context, req *ListRequest) (*C
272272

273273
hostnames, _, err := collectorService.GetHostnames(page, filter)
274274
if err != nil {
275-
catcher.Error("failed to fetch hostnames", err, map[string]any{})
275+
catcher.Error("failed to fetch hostnames", err, nil)
276276
return nil, status.Errorf(codes.NotFound, "failed to fetch hostnames: %v", err)
277277
}
278278

@@ -284,7 +284,7 @@ func (s *Grpc) ListCollectorHostnames(ctx context.Context, req *ListRequest) (*C
284284
func (s *Grpc) GetCollectorsByHostnameAndModule(ctx context.Context, filter *FilterByHostAndModule) (*ListCollectorResponse, error) {
285285
collectors, err := collectorService.GetCollectorByHostnameAndModule(filter.GetHostname(), filter.GetModule().String())
286286
if err != nil {
287-
catcher.Error("unable to get hostname", err, map[string]any{})
287+
catcher.Error("unable to get hostname", err, nil)
288288
return nil, status.Errorf(codes.NotFound, "unable to get hostname: %v", err)
289289
}
290290

@@ -294,7 +294,7 @@ func (s *Grpc) GetCollectorsByHostnameAndModule(ctx context.Context, filter *Fil
294294
func (s *Grpc) LoadCollectorsCacheFromDatabase() error {
295295
collectors, err := collectorService.FindAll()
296296
if err != nil {
297-
catcher.Error("Failed to fetch collectors from database", err, map[string]any{})
297+
catcher.Error("Failed to fetch collectors from database", err, nil)
298298
return err
299299
}
300300
for _, colect := range collectors {

agent-manager/auth/interceptor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func StreamInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamS
9898
func checkKeyAuth(token string, id uint64, fullMethod string) error {
9999
authCache := getAuthCache(fullMethod)
100100
if authCache == nil {
101-
catcher.Error("unable to resolve auth cache", nil, map[string]any{})
101+
catcher.Error("unable to resolve auth cache", nil, nil)
102102
return status.Error(codes.Unauthenticated, "unable to resolve auth cache")
103103
}
104104

@@ -154,13 +154,13 @@ func authenticateRequest(md metadata.MD, authName string) error {
154154

155155
if authName == "connection-key" && authHeader[0] != "" {
156156
if !validateToken(authHeader[0]) {
157-
catcher.Error("unable to connect with the panel to check the connection-key", nil, map[string]any{})
157+
catcher.Error("unable to connect with the panel to check the connection-key", nil, nil)
158158
return status.Error(codes.Unauthenticated, "unable to connect with the panel to check the connection-key")
159159
}
160160
} else if authName == "internal-key" && authHeader[0] != "" {
161161
internalKey := os.Getenv(config.UTMSharedKeyEnv)
162162
if authHeader[0] != internalKey {
163-
catcher.Error("internal key does not match", nil, map[string]any{})
163+
catcher.Error("internal key does not match", nil, nil)
164164
return status.Error(codes.Unauthenticated, "internal key does not match")
165165
}
166166
} else {

agent-manager/main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
func main() {
26-
catcher.Info("Starting UTMStack Agent Manager", map[string]any{})
26+
catcher.Info("Starting UTMStack Agent Manager", nil)
2727

2828
defer func() {
2929
if r := recover(); r != nil {
@@ -32,20 +32,20 @@ func main() {
3232
}
3333
}()
3434

35-
catcher.Info("Initializing database...", map[string]any{})
35+
catcher.Info("Initializing database...", nil)
3636
config.InitDb()
3737
migration.MigrateDatabase()
38-
catcher.Info("[OK] Database initialized", map[string]any{})
38+
catcher.Info("[OK] Database initialized", nil)
3939

4040
s, err := pb.InitGrpc()
4141
if err != nil {
42-
catcher.Error("Failed to initialize gRPC", err, map[string]any{})
42+
catcher.Error("Failed to initialize gRPC", err, nil)
4343
os.Exit(1)
4444
}
4545

4646
cert, err := tls.LoadX509KeyPair("/cert/utm.crt", "/cert/utm.key")
4747
if err != nil {
48-
catcher.Error("failed to load server certificates", err, map[string]any{})
48+
catcher.Error("failed to load server certificates", err, nil)
4949
os.Exit(1)
5050
}
5151

@@ -84,13 +84,13 @@ func main() {
8484

8585
lis, err := net.Listen("tcp", "0.0.0.0:50051")
8686
if err != nil {
87-
catcher.Error("Failed to listen", err, map[string]any{})
87+
catcher.Error("Failed to listen", err, nil)
8888
os.Exit(1)
8989
}
9090

91-
catcher.Info("Starting gRPC server on 0.0.0.0:50051", map[string]any{})
91+
catcher.Info("Starting gRPC server on 0.0.0.0:50051", nil)
9292
if err := grpcServer.Serve(lis); err != nil {
93-
catcher.Error("Failed to serve", err, map[string]any{})
93+
catcher.Error("Failed to serve", err, nil)
9494
os.Exit(1)
9595
}
9696
}

agent-manager/migration/migrations.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func MigrateDatabase() {
2121
db := config.GetDB()
2222
err := db.AutoMigrate(&Changeset{})
2323
if err != nil {
24-
catcher.Error("failed to auto-migrate MigrationRecord table", err, map[string]any{})
24+
catcher.Error("failed to auto-migrate MigrationRecord table", err, nil)
2525
return
2626
}
2727
performMigration(db, "performInitialMigrations_15022024_001", "jdieguez89", performInitialMigrations)
@@ -61,7 +61,7 @@ func performMigration(db *gorm.DB, migrationName string, executedBy string, migr
6161
func executeSQLCommands(db *gorm.DB, sqlCommands []string) error {
6262
for _, sql := range sqlCommands {
6363
if err := db.Exec(sql).Error; err != nil {
64-
catcher.Error("Failed to execute SQL command", err, map[string]any{})
64+
catcher.Error("Failed to execute SQL command", err, nil)
6565
return err
6666
}
6767
}
@@ -118,11 +118,11 @@ func renameLastSeenTableAndColumnOrCreateTable(db *gorm.DB) error {
118118
oldName := "agent_last_seens"
119119
if db.Migrator().HasTable(oldName) {
120120
if err := db.Migrator().RenameTable("agent_last_seens", newName); err != nil {
121-
catcher.Error("Failed to rename table", err, map[string]any{})
121+
catcher.Error("Failed to rename table", err, nil)
122122
return err
123123
}
124124
if err := db.Migrator().RenameColumn(&models.LastSeen{}, "agent_key", "key"); err != nil {
125-
catcher.Error("Failed to rename column", err, map[string]any{})
125+
catcher.Error("Failed to rename column", err, nil)
126126
return err
127127
}
128128
sqlCommands := []string{
@@ -132,7 +132,7 @@ func renameLastSeenTableAndColumnOrCreateTable(db *gorm.DB) error {
132132
}
133133
err := executeSQLCommands(db, sqlCommands)
134134
if err == nil {
135-
catcher.Info("Renamed table and column successfully", map[string]any{})
135+
catcher.Info("Renamed table and column successfully", nil)
136136
}
137137
return err
138138

agent-manager/service/last_seen_service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func NewLastSeenService() *LastSeenService {
3131
func (s *LastSeenService) Start() {
3232
pings, err := s.repo.GetAll()
3333
if err != nil {
34-
catcher.Error("Failed to populate LastSeen cache", err, map[string]any{})
34+
catcher.Error("Failed to populate LastSeen cache", err, nil)
3535
} else {
3636
s.Populate(pings)
3737
}
@@ -71,7 +71,7 @@ func (s *LastSeenService) flushCachePeriodically() {
7171
// Flush the cache to the database
7272
err := s.flushCacheToDB()
7373
if err != nil {
74-
catcher.Error("Failed to flush LastSeen cache to database", err, map[string]any{})
74+
catcher.Error("Failed to flush LastSeen cache to database", err, nil)
7575
}
7676
case <-s.stopCh:
7777
return

0 commit comments

Comments
 (0)