From a593879e4babbb4d72b225f25333ee26c8386bc8 Mon Sep 17 00:00:00 2001 From: Anton Galitsyn Date: Tue, 17 Jan 2023 22:52:28 +0700 Subject: [PATCH] rework logger --- cmd/api/main.go | 78 ++-- pkg/app/api/client/agent_server.go | 2 +- pkg/app/api/server/middleware.go | 30 ++ pkg/app/api/server/private/agents.go | 115 +++--- pkg/app/api/server/private/binaries.go | 19 +- pkg/app/api/server/private/events.go | 29 +- pkg/app/api/server/private/groups.go | 103 ++--- pkg/app/api/server/private/modules.go | 495 +++++++++++++------------ pkg/app/api/server/private/options.go | 7 +- pkg/app/api/server/private/policies.go | 119 +++--- pkg/app/api/server/private/porting.go | 37 +- pkg/app/api/server/private/proto.go | 9 +- pkg/app/api/server/private/roles.go | 7 +- pkg/app/api/server/private/services.go | 39 +- pkg/app/api/server/private/tags.go | 11 +- pkg/app/api/server/private/tenants.go | 33 +- pkg/app/api/server/private/upgrades.go | 71 ++-- pkg/app/api/server/private/users.go | 77 ++-- pkg/app/api/server/private/versions.go | 11 +- pkg/app/api/server/proto/proto.go | 26 +- pkg/app/api/server/public/auth.go | 36 +- pkg/app/api/server/public/info.go | 12 +- pkg/app/api/server/router.go | 22 +- pkg/app/api/user_action/log.go | 12 +- pkg/app/api/utils/utils.go | 14 +- pkg/log/context.go | 32 -- pkg/log/log.go | 73 ---- pkg/logger/gorm.go | 25 ++ pkg/logger/log.go | 28 ++ pkg/storage/mysql/mysql.go | 11 +- 30 files changed, 765 insertions(+), 818 deletions(-) delete mode 100644 pkg/log/context.go delete mode 100644 pkg/log/log.go create mode 100644 pkg/logger/gorm.go create mode 100644 pkg/logger/log.go diff --git a/cmd/api/main.go b/cmd/api/main.go index df07c3ef..51cf5e1d 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -4,16 +4,13 @@ import ( "context" "flag" "fmt" - "io" "os" "os/signal" - "path" "syscall" "time" "github.com/heetch/confita" "github.com/heetch/confita/backend/env" - "github.com/natefinch/lumberjack" "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "golang.org/x/sync/errgroup" @@ -24,7 +21,7 @@ import ( useraction "soldr/pkg/app/api/user_action" "soldr/pkg/app/api/utils/meter" "soldr/pkg/app/api/worker" - "soldr/pkg/log" + "soldr/pkg/logger" "soldr/pkg/observability" "soldr/pkg/secret" "soldr/pkg/storage/mysql" @@ -57,7 +54,6 @@ type DBConfig struct { Port int `config:"db_port,required"` } -// TODO: refactor old env names type PublicAPIConfig struct { Addr string `config:"api_listen_http"` AddrHTTPS string `config:"api_listen_https"` @@ -121,32 +117,22 @@ func main() { version.IsDevelop = "true" } + logLevels := []logrus.Level{ + logrus.PanicLevel, + logrus.FatalLevel, + logrus.ErrorLevel, + logrus.WarnLevel, + logrus.InfoLevel, + } if cfg.Debug { + logLevels = append(logLevels, logrus.DebugLevel) cfg.Log.Level = "debug" + cfg.Log.Format = "text" } - logLevel, err := log.ParseLevel(cfg.Log.Level) - if err != nil { - fmt.Fprintf(os.Stderr, "could not parse log level: %s", err) - return - } - logFormat, err := log.ParseFormat(cfg.Log.Format) - if err != nil { - fmt.Fprintf(os.Stderr, "could not parse log format: %s", err) - return - } - logDir := "logs" - if dir, ok := os.LookupEnv("LOG_DIR"); ok { - logDir = dir - } - logFile := &lumberjack.Logger{ - Filename: path.Join(logDir, "app.log"), - MaxSize: 100, - MaxBackups: 7, - MaxAge: 14, - Compress: true, - } - logger := log.New(log.Config{Level: logLevel, Format: logFormat}, io.MultiWriter(os.Stdout, logFile)) - ctx = log.AttachToContext(ctx, logger) + + logrus.SetLevel(logger.ParseLevel(cfg.Log.Level)) + logrus.SetFormatter(logger.ParseFormat(cfg.Log.Format)) + logrus.SetOutput(os.Stdout) dsn := fmt.Sprintf("%s:%s@%s/%s?parseTime=true", cfg.DB.User, @@ -156,11 +142,11 @@ func main() { ) db, err := mysql.New(&mysql.Config{DSN: secret.NewString(dsn)}) if err != nil { - logger.WithError(err).Error("could not create DB instance") + logrus.WithError(err).Error("could not create DB instance") return } if err = db.RetryConnect(ctx, 10, 100*time.Millisecond); err != nil { - logger.WithError(err).Error("could not connect to database") + logrus.WithError(err).Error("could not connect to database") return } @@ -169,12 +155,12 @@ func main() { migrationDir = dir } if err = db.Migrate(migrationDir); err != nil { - logger.WithError(err).Error("could not apply migrations") + logrus.WithError(err).Error("could not apply migrations") return } - dbWithORM, err := db.WithORM(ctx) + dbWithORM, err := db.WithORM() if err != nil { - logger.WithError(err).Error("could not create ORM") + logrus.WithError(err).Error("could not create ORM") return } if cfg.Debug { @@ -202,7 +188,7 @@ func main() { attr, ) if err != nil { - logger.WithError(err).Error("could not create tracer provider") + logrus.WithError(err).Error("could not create tracer provider") return } meterClient := observability.NewProxyMeterClient( @@ -214,7 +200,7 @@ func main() { }), ) if err != nil { - logger.WithError(err).Error("could not create meter client") + logrus.WithError(err).Error("could not create meter client") return } meterProvider, err := observability.NewMeterProvider( @@ -225,20 +211,10 @@ func main() { attr, ) if err != nil { - logger.WithError(err).Error("could not create meter provider") + logrus.WithError(err).Error("could not create meter provider") return } - logLevels := []logrus.Level{ - logrus.PanicLevel, - logrus.FatalLevel, - logrus.ErrorLevel, - logrus.WarnLevel, - logrus.InfoLevel, - } - if cfg.Debug { - logLevels = append(logLevels, logrus.DebugLevel) - } observability.InitObserver( ctx, tracerProvider, @@ -252,7 +228,7 @@ func main() { gormMeter := meterProvider.Meter("vxapi-meter") if err = meter.InitGormMetrics(gormMeter); err != nil { - logger.WithError(err).Error("could not initialize vxapi-meter") + logrus.WithError(err).Error("could not initialize vxapi-meter") return } @@ -265,7 +241,7 @@ func main() { eventWorker := srvevents.NewEventPoller(exchanger, cfg.EventWorker.PollInterval, dbWithORM) go func() { if err = eventWorker.Run(ctx); err != nil { - logger.WithError(err).Error("could not start event worker") + logrus.WithError(err).Error("could not start event worker") } }() @@ -278,7 +254,7 @@ func main() { // run worker to synchronize events retention policy to all instance DB go worker.SyncRetentionEvents(ctx, dbWithORM) - userActionWriter := useraction.NewLogWriter(logger) + userActionWriter := useraction.NewLogWriter() router := server.NewRouter( dbWithORM, @@ -305,7 +281,7 @@ func main() { }.ListenAndServeTLS(ctx, router) }) } - if err := srvg.Wait(); err != nil { - logger.WithError(err).Error("failed to start server") + if err = srvg.Wait(); err != nil { + logrus.WithError(err).Error("failed to start server") } } diff --git a/pkg/app/api/client/agent_server.go b/pkg/app/api/client/agent_server.go index a4557baf..7d4e5b3b 100644 --- a/pkg/app/api/client/agent_server.go +++ b/pkg/app/api/client/agent_server.go @@ -57,7 +57,7 @@ func (c *AgentServerClient) GetDB(ctx context.Context, hash string) (*gorm.DB, e return nil, fmt.Errorf("could not connect to database: %w", err) } - dbWithORM, err := dbConn.WithORM(ctx) + dbWithORM, err := dbConn.WithORM() if err != nil { return nil, fmt.Errorf("could not create ORM: %w", err) } diff --git a/pkg/app/api/server/middleware.go b/pkg/app/api/server/middleware.go index 2bb63658..b25aaa25 100644 --- a/pkg/app/api/server/middleware.go +++ b/pkg/app/api/server/middleware.go @@ -6,10 +6,12 @@ import ( "regexp" "strings" "sync" + "time" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/models" "soldr/pkg/app/api/server/context" @@ -245,3 +247,31 @@ func setServiceInfo(db *gorm.DB) gin.HandlerFunc { c.Next() } } + +func WithLogger(skipPaths []string) gin.HandlerFunc { + skip := make(map[string]struct{}, len(skipPaths)) + for _, path := range skipPaths { + skip[path] = struct{}{} + } + + return func(c *gin.Context) { + start := time.Now() + path := c.Request.URL.Path + raw := c.Request.URL.RawQuery + + c.Next() + + if _, ok := skip[path]; !ok { + if raw != "" { + path = path + "?" + raw + } + logrus.WithFields(logrus.Fields{ + "client_ip": c.ClientIP(), + "latency": time.Now().Sub(start), + "path": path, + "method": c.Request.Method, + "status_code": c.Writer.Status(), + }).Info() + } + } +} diff --git a/pkg/app/api/server/private/agents.go b/pkg/app/api/server/private/agents.go index 16f8d22b..185f734c 100644 --- a/pkg/app/api/server/private/agents.go +++ b/pkg/app/api/server/private/agents.go @@ -9,6 +9,7 @@ import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/client" "soldr/pkg/app/api/models" @@ -277,26 +278,26 @@ func (s *AgentService) GetAgents(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrGetAgentsInvalidRequest, err) return } serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = query.Init("agents", agentsSQLMappers); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrGetAgentsInvalidRequest, err) return } @@ -343,13 +344,13 @@ func (s *AgentService) GetAgents(c *gin.Context) { if query.Group == "" { if resp.Total, err = query.Query(iDB, &resp.Agents, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agents") + logrus.WithError(err).Errorf("error finding agents") response.Error(c, response.ErrGetAgentsInvalidQuery, err) return } } else { if groupedResp.Total, err = query.QueryGrouped(iDB, &groupedResp.Grouped, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding grouped agents") + logrus.WithError(err).Errorf("error finding grouped agents") response.Error(c, response.ErrGetAgentsInvalidQuery, err) return } @@ -361,19 +362,19 @@ func (s *AgentService) GetAgents(c *gin.Context) { aids = append(aids, resp.Agents[i].ID) gids = append(gids, resp.Agents[i].GroupID) if err = resp.Agents[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent data '%s'", resp.Agents[i].Hash) + logrus.WithError(err).Errorf("error validating agent data '%s'", resp.Agents[i].Hash) response.Error(c, response.ErrAgentsInvalidData, err) return } } if err = iDB.Where("id IN (?)", gids).Find(&groupsa).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding linked groups") + logrus.WithError(err).Errorf("error finding linked groups") response.Error(c, response.ErrGetAgentsInvalidQuery, err) return } for i := 0; i < len(groupsa); i++ { if err = groupsa[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group data '%s'", groupsa[i].Hash) + logrus.WithError(err).Errorf("error validating group data '%s'", groupsa[i].Hash) response.Error(c, response.ErrAgentsInvalidData, err) return } @@ -382,7 +383,7 @@ func (s *AgentService) GetAgents(c *gin.Context) { sqlQuery := sqlAgentDetails + ` WHERE a.id IN (?) AND a.deleted_at IS NULL` if err = iDB.Raw(sqlQuery, aids).Scan(&resp.Details).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading details agents") + logrus.WithError(err).Errorf("error loading details agents") response.Error(c, response.ErrGetAgentsInvalidQuery, err) return } @@ -397,7 +398,7 @@ func (s *AgentService) GetAgents(c *gin.Context) { QueryExpr()). Find(&tasks).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding linked agent upgrade tasks") + logrus.WithError(err).Errorf("error finding linked agent upgrade tasks") response.Error(c, response.ErrGetAgentsInvalidQuery, err) return } @@ -408,7 +409,7 @@ func (s *AgentService) GetAgents(c *gin.Context) { Joins(`LEFT JOIN groups_to_policies gtp ON gtp.policy_id = modules.policy_id`). Find(&modulesa, "gtp.group_id IN (?) AND status = 'joined'", gids).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group modules") + logrus.WithError(err).Errorf("error finding group modules") response.Error(c, response.ErrGetAgentsInvalidQuery, err) return } else { @@ -417,7 +418,7 @@ func (s *AgentService) GetAgents(c *gin.Context) { name := modulesa[i].Info.Name policy_id := modulesa[i].PolicyID if err = modulesa[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group module data '%d' '%s'", id, name) + logrus.WithError(err).Errorf("error validating group module data '%d' '%s'", id, name) response.Error(c, response.ErrAgentsInvalidData, err) return } @@ -430,7 +431,7 @@ func (s *AgentService) GetAgents(c *gin.Context) { } if err = iDB.Find(&gpss, "group_id IN (?)", gids).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy to groups links") + logrus.WithError(err).Errorf("error finding policy to groups links") response.Error(c, response.ErrGroupPolicyGroupsNotFound, err) return } @@ -441,7 +442,7 @@ func (s *AgentService) GetAgents(c *gin.Context) { Joins(`LEFT JOIN groups_to_policies gtp ON gtp.policy_id = policies.id AND gtp.group_id IN (?)`, gids). Find(&policiesa).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group policies") + logrus.WithError(err).Errorf("error finding group policies") response.Error(c, response.ErrGroupPolicyPoliciesNotFound, err) return } else { @@ -449,7 +450,7 @@ func (s *AgentService) GetAgents(c *gin.Context) { id := policiesa[i].ID name := policiesa[i].Info.Name if err = policiesa[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%d' '%s'", id, name) + logrus.WithError(err).Errorf("error validating policy data '%d' '%s'", id, name) response.Error(c, response.ErrGetAgentsInvalidAgentModuleData, err) return } @@ -533,7 +534,7 @@ func (s *AgentService) PatchAgents(c *gin.Context) { }() if err := c.ShouldBindBodyWith(&action, binding.JSON); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrPatchAgentsInvalidAction, err) return } @@ -541,13 +542,13 @@ func (s *AgentService) PatchAgents(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -559,7 +560,7 @@ func (s *AgentService) PatchAgents(c *gin.Context) { var agents []models.Agent if err = iDB.Scopes(scope).Model(&models.Agent{}).Count(&resp.Total).Find(&agents).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error collecting agents by filter") + logrus.WithError(err).Errorf("error collecting agents by filter") response.Error(c, response.ErrPatchAgentsInvalidQuery, err) return } @@ -585,7 +586,7 @@ func (s *AgentService) PatchAgents(c *gin.Context) { "reason": "Canceled.By.User", }).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating tasks by filter") + logrus.WithError(err).Errorf("error updating tasks by filter") response.Error(c, response.ErrPatchAgentsUpdateTasksFail, err) return false } @@ -593,7 +594,7 @@ func (s *AgentService) PatchAgents(c *gin.Context) { err = iDB.Scopes(scope).Model(&models.Agent{}). Count(&resp.Total).UpdateColumns(update_fields).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating agents by filter") + logrus.WithError(err).Errorf("error updating agents by filter") response.Error(c, response.ErrPatchAgentsUpdateAgentsFail, err) return false } @@ -604,7 +605,7 @@ func (s *AgentService) PatchAgents(c *gin.Context) { case "delete": for _, agent := range agents { if err = iDB.Delete(&agent).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting agents by filter") + logrus.WithError(err).Errorf("error deleting agents by filter") response.Error(c, response.ErrPatchAgentsDeleteAgentsFail, err) return } @@ -626,7 +627,7 @@ func (s *AgentService) PatchAgents(c *gin.Context) { var group models.Group err = iDB.Where("id = ?", action.To).Take(&group).Error if err != nil || group.ID == 0 { - utils.FromContext(c).WithError(err).Errorf("error getting agents group") + logrus.WithError(err).Errorf("error getting agents group") response.Error(c, response.ErrPatchAgentsMoveFail, err) return } @@ -647,7 +648,7 @@ func (s *AgentService) PatchAgents(c *gin.Context) { err = iDB.Where("agent_id IN (?)", agentIds). Delete(&models.Event{}).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting agents on moving") + logrus.WithError(err).Errorf("error deleting agents on moving") response.Error(c, response.ErrPatchAgentsMoveFail, err) return } @@ -659,7 +660,7 @@ func (s *AgentService) PatchAgents(c *gin.Context) { err = iDB.Scopes(query.DataFilter()).Model(&models.Agent{}).Count(&resp.Total). UpdateColumns(update_fields).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating agents on moving") + logrus.WithError(err).Errorf("error updating agents on moving") response.Error(c, response.ErrPatchAgentsMoveFail, err) return } @@ -689,19 +690,19 @@ func (s *AgentService) GetAgent(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = iDB.Take(&resp.Agent, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent by hash") + logrus.WithError(err).Errorf("error finding agent by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrAgentsNotFound, err) } else { @@ -709,14 +710,14 @@ func (s *AgentService) GetAgent(c *gin.Context) { } return } else if err = resp.Agent.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent data '%s'", resp.Agent.Hash) + logrus.WithError(err).Errorf("error validating agent data '%s'", resp.Agent.Hash) response.Error(c, response.ErrAgentsInvalidData, err) return } sqlQuery := sqlAgentDetails + ` WHERE a.hash = ? AND a.deleted_at IS NULL` if err = iDB.Raw(sqlQuery, hash).Scan(&resp.Details).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading details by agent hash '%s'", hash) + logrus.WithError(err).Errorf("error loading details by agent hash '%s'", hash) response.Error(c, response.ErrGetAgentDetailsNotFound, err) return } @@ -733,7 +734,7 @@ func (s *AgentService) GetAgent(c *gin.Context) { if resp.Agent.GroupID != 0 { if err = iDB.Take(&group, "id = ?", resp.Agent.GroupID).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group by id") + logrus.WithError(err).Errorf("error finding group by id") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGetAgentGroupNotFound, err) } else { @@ -741,7 +742,7 @@ func (s *AgentService) GetAgent(c *gin.Context) { } return } else if err = group.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group data '%s'", group.Hash) + logrus.WithError(err).Errorf("error validating group data '%s'", group.Hash) response.Error(c, response.ErrGetAgentInvalidGroupData, err) return } @@ -753,7 +754,7 @@ func (s *AgentService) GetAgent(c *gin.Context) { Joins(`LEFT JOIN groups_to_policies gtp ON gtp.policy_id = modules.policy_id`). Find(&resp.Details.Modules, "gtp.group_id = ? AND status = 'joined'", resp.Agent.GroupID).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group modules by group ID '%d'", resp.Agent.GroupID) + logrus.WithError(err).Errorf("error finding group modules by group ID '%d'", resp.Agent.GroupID) response.Error(c, response.ErrGetAgentGroupModulesNotFound, err) return } else { @@ -761,7 +762,7 @@ func (s *AgentService) GetAgent(c *gin.Context) { if err = resp.Details.Modules[i].Valid(); err != nil { id := resp.Details.Modules[i].ID name := resp.Details.Modules[i].Info.Name - utils.FromContext(c).WithError(err).Errorf("error validating group module data '%d' '%s'", id, name) + logrus.WithError(err).Errorf("error validating group module data '%d' '%s'", id, name) response.Error(c, response.ErrGetAgentInvalidAgentModuleData, err) return } @@ -773,7 +774,7 @@ func (s *AgentService) GetAgent(c *gin.Context) { Group: *resp.Details.Group, } if err = iDB.Model(gps).Association("policies").Find(&gps.Policies).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group policies by group model") + logrus.WithError(err).Errorf("error finding group policies by group model") response.Error(c, response.ErrGetAgentPoliciesNotFound, err) return } @@ -804,13 +805,13 @@ func (s *AgentService) PatchAgent(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -824,7 +825,7 @@ func (s *AgentService) PatchAgent(c *gin.Context) { if nameErr == nil { uaf.ObjectDisplayName = name } - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrPatchAgentValidationError, err) return } @@ -833,14 +834,14 @@ func (s *AgentService) PatchAgent(c *gin.Context) { uaf.ObjectDisplayName = action.Agent.Description if hash != action.Agent.Hash { - utils.FromContext(c).WithError(nil).Errorf("mismatch agent hash to requested one") + logrus.WithError(nil).Errorf("mismatch agent hash to requested one") response.Error(c, response.ErrPatchAgentValidationError, nil) return } var count int64 if err = iDB.Model(&action.Agent).Count(&count).Error; err != nil || count == 0 { - utils.FromContext(c).WithError(nil).Errorf("error updating agent by hash '%s', agent not found", hash) + logrus.WithError(nil).Errorf("error updating agent by hash '%s', agent not found", hash) response.Error(c, response.ErrAgentsNotFound, err) return } @@ -855,7 +856,7 @@ func (s *AgentService) PatchAgent(c *gin.Context) { "reason": "Canceled.By.User", }).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating tasks by agent") + logrus.WithError(err).Errorf("error updating tasks by agent") response.Error(c, response.ErrPatchAgentTaskUpdateFail, err) return } @@ -865,11 +866,11 @@ func (s *AgentService) PatchAgent(c *gin.Context) { err = iDB.Select("", public_info...).Save(&action.Agent).Error if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - utils.FromContext(c).WithError(nil).Errorf("error updating agent by hash '%s', agent not found", hash) + logrus.WithError(nil).Errorf("error updating agent by hash '%s', agent not found", hash) response.Error(c, response.ErrAgentsNotFound, err) return } else if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating agent by hash '%s'", hash) + logrus.WithError(err).Errorf("error updating agent by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } @@ -892,11 +893,9 @@ func (s *AgentService) CreateAgent(c *gin.Context) { uaf := useraction.NewFields(c, "agent", "agent", "creation", "", useraction.UnknownObjectDisplayName) defer s.userActionWriter.WriteUserAction(uaf) - logger := utils.FromContext(c) - var info agentInfo if err := c.ShouldBindJSON(&info); err != nil { - logger.WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrCreateAgentValidationError, err) return } @@ -904,13 +903,13 @@ func (s *AgentService) CreateAgent(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -947,7 +946,7 @@ func (s *AgentService) CreateAgent(c *gin.Context) { uaf.ObjectID = newAgent.Hash if err = iDB.Create(&newAgent).Error; err != nil { - logger.WithError(err).Errorf("error creating agent") + logrus.WithError(err).Errorf("error creating agent") response.Error(c, response.ErrCreateAgentCreateError, err) return } @@ -976,19 +975,19 @@ func (s *AgentService) DeleteAgent(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = iDB.Take(&agent, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent by hash") + logrus.WithError(err).Errorf("error finding agent by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrAgentsNotFound, err) } else { @@ -996,14 +995,14 @@ func (s *AgentService) DeleteAgent(c *gin.Context) { } return } else if err = agent.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent data '%s'", agent.Hash) + logrus.WithError(err).Errorf("error validating agent data '%s'", agent.Hash) response.Error(c, response.ErrAgentsInvalidData, err) return } uaf.ObjectDisplayName = agent.Description if err = iDB.Delete(&agent).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting agent by hash '%s'", hash) + logrus.WithError(err).Errorf("error deleting agent by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } @@ -1029,13 +1028,13 @@ func (s *AgentService) GetAgentsCount(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -1055,7 +1054,7 @@ func (s *AgentService) GetAgentsCount(c *gin.Context) { Scan(&resp). Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("could not count agents") + logrus.WithError(err).Errorf("could not count agents") response.Error(c, response.ErrInternal, err) return } diff --git a/pkg/app/api/server/private/binaries.go b/pkg/app/api/server/private/binaries.go index 407b7d47..cbe05be2 100644 --- a/pkg/app/api/server/private/binaries.go +++ b/pkg/app/api/server/private/binaries.go @@ -9,6 +9,7 @@ import ( "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/models" srvcontext "soldr/pkg/app/api/server/context" @@ -102,7 +103,7 @@ func (s *BinariesService) GetAgentBinaries(c *gin.Context) { ) if err = c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrAgentBinariesInvalidRequest, err) return } @@ -121,14 +122,14 @@ func (s *BinariesService) GetAgentBinaries(c *gin.Context) { }) if resp.Total, err = query.Query(s.db, &resp.Binaries); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent binaries") + logrus.WithError(err).Errorf("error finding agent binaries") response.Error(c, response.ErrInternal, err) return } for i := 0; i < len(resp.Binaries); i++ { if err = resp.Binaries[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent binaries data '%s'", resp.Binaries[i].Hash) + logrus.WithError(err).Errorf("error validating agent binaries data '%s'", resp.Binaries[i].Hash) response.Error(c, response.ErrAgentBinariesInvalidData, err) return } @@ -200,11 +201,11 @@ func (s *BinariesService) GetAgentBinaryFile(c *gin.Context) { err = s.db.Scopes(scope).Model(&binary).Take(&binary).Error if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - utils.FromContext(c).WithError(nil).Errorf("error getting binary info by version '%s', record not found", agentVersion) + logrus.WithError(nil).Errorf("error getting binary info by version '%s', record not found", agentVersion) response.Error(c, response.ErrAgentBinaryFileNotFound, err) return } else if err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting binary info by version '%s'", agentVersion) + logrus.WithError(err).Errorf("error getting binary info by version '%s'", agentVersion) response.Error(c, response.ErrInternal, err) return } @@ -212,25 +213,25 @@ func (s *BinariesService) GetAgentBinaryFile(c *gin.Context) { path := filepath.Join("vxagent", binary.Version, agentOS, agentArch, agentName) if chksums, ok = binary.Info.Chksums[path]; !ok { - utils.FromContext(c).WithError(nil).Errorf("error getting agent binary file check sums: '%s' not found", path) + logrus.WithError(nil).Errorf("error getting agent binary file check sums: '%s' not found", path) response.Error(c, response.ErrAgentBinaryFileNotFound, nil) return } if s3, err = storage.NewS3(nil); err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") response.Error(c, response.ErrInternal, err) return } if data, err = s3.ReadFile(path); err != nil { - utils.FromContext(c).WithError(err).Errorf("error reading agent binary file '%s'", path) + logrus.WithError(err).Errorf("error reading agent binary file '%s'", path) response.Error(c, response.ErrInternal, err) return } if err = utils.ValidateBinaryFileByChksums(data, chksums); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent binary file by check sums '%s'", path) + logrus.WithError(err).Errorf("error validating agent binary file by check sums '%s'", path) response.Error(c, response.ErrAgentBinaryFileCorrupted, err) return } diff --git a/pkg/app/api/server/private/events.go b/pkg/app/api/server/private/events.go index bf3e60ee..24040e3c 100644 --- a/pkg/app/api/server/private/events.go +++ b/pkg/app/api/server/private/events.go @@ -5,6 +5,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/client" "soldr/pkg/app/api/models" @@ -293,40 +294,40 @@ func (s *EventService) GetEvents(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrEventsInvalidRequest, err) return } serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = query.Init("events", eventsSQLMappers); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrEventsInvalidRequest, err) return } emids, epids, err = getModuleIDs(iDB, &query) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting modules list by filter") + logrus.WithError(err).Errorf("error getting modules list by filter") response.Error(c, response.ErrEventsInvalidQuery, err) return } eaids, err = getAgentIDs(iDB, &query, epids) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting agents list by filter") + logrus.WithError(err).Errorf("error getting agents list by filter") response.Error(c, response.ErrEventsInvalidQuery, err) return } @@ -341,7 +342,7 @@ func (s *EventService) GetEvents(c *gin.Context) { copyEventsSQLMappers["module_id"] = "`events`.module_id" copyEventsSQLMappers["policy_id"] = "`modules`.policy_id" if err = query.Init("events", copyEventsSQLMappers); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrEventsInvalidRequest, err) return } @@ -355,13 +356,13 @@ func (s *EventService) GetEvents(c *gin.Context) { if query.Group == "" { if err = doQuery(iDB, &query, &resp, funcs); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding events") + logrus.WithError(err).Errorf("error finding events") response.Error(c, response.ErrEventsInvalidQuery, err) return } } else { if groupedResp.Total, err = query.QueryGrouped(iDB, &groupedResp.Grouped, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding grouped events") + logrus.WithError(err).Errorf("error finding grouped events") response.Error(c, response.ErrEventsInvalidQuery, err) return } @@ -373,20 +374,20 @@ func (s *EventService) GetEvents(c *gin.Context) { aids = append(aids, resp.Events[i].AgentID) mids = append(mids, resp.Events[i].ModuleID) if resp.Events[i].Valid() != nil { - utils.FromContext(c).WithError(err).Errorf("error validating event data") + logrus.WithError(err).Errorf("error validating event data") response.Error(c, response.ErrEventsInvalidData, err) return } } aids = utils.UniqueUint64InSlice(aids) if err = iDB.Where("id IN (?)", aids).Find(&resp.Agents).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding linked agents") + logrus.WithError(err).Errorf("error finding linked agents") response.Error(c, response.ErrEventsInvalidQuery, err) return } mids = utils.UniqueUint64InSlice(mids) if err = iDB.Where("id IN (?)", mids).Find(&resp.Modules).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding linked modules") + logrus.WithError(err).Errorf("error finding linked modules") response.Error(c, response.ErrEventsInvalidQuery, err) return } @@ -396,7 +397,7 @@ func (s *EventService) GetEvents(c *gin.Context) { } gids = utils.UniqueUint64InSlice(gids) if err = iDB.Where("id IN (?)", gids).Find(&resp.Groups).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding linked gloups") + logrus.WithError(err).Errorf("error finding linked gloups") response.Error(c, response.ErrEventsInvalidQuery, err) return } @@ -406,7 +407,7 @@ func (s *EventService) GetEvents(c *gin.Context) { } pids = utils.UniqueUint64InSlice(pids) if err = iDB.Where("id IN (?)", pids).Find(&resp.Policies).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding linked policies") + logrus.WithError(err).Errorf("error finding linked policies") response.Error(c, response.ErrEventsInvalidQuery, err) return } diff --git a/pkg/app/api/server/private/groups.go b/pkg/app/api/server/private/groups.go index 52c74e75..c0af1466 100644 --- a/pkg/app/api/server/private/groups.go +++ b/pkg/app/api/server/private/groups.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/client" "soldr/pkg/app/api/models" @@ -224,26 +225,26 @@ func (s *GroupService) GetGroups(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrGroupsInvalidRequest, err) return } serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = query.Init("groups", groupsSQLMappers); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrGroupsInvalidRequest, err) return } @@ -292,13 +293,13 @@ func (s *GroupService) GetGroups(c *gin.Context) { if query.Group == "" { if resp.Total, err = query.Query(iDB, &resp.Groups, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding groups") + logrus.WithError(err).Errorf("error finding groups") response.Error(c, response.ErrGroupsInvalidQuery, err) return } } else { if groupedResp.Total, err = query.QueryGrouped(iDB, &groupedResp.Grouped, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding grouped groups") + logrus.WithError(err).Errorf("error finding grouped groups") response.Error(c, response.ErrGetAgentsInvalidQuery, err) return } @@ -309,7 +310,7 @@ func (s *GroupService) GetGroups(c *gin.Context) { for i := 0; i < len(resp.Groups); i++ { gids = append(gids, resp.Groups[i].ID) if err = resp.Groups[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group data '%s'", resp.Groups[i].Hash) + logrus.WithError(err).Errorf("error validating group data '%s'", resp.Groups[i].Hash) response.Error(c, response.ErrGroupsInvalidData, err) return } @@ -317,7 +318,7 @@ func (s *GroupService) GetGroups(c *gin.Context) { sqlQuery := sqlGroupDetails + ` WHERE g.id IN (?) AND g.deleted_at IS NULL` if err = iDB.Raw(sqlQuery, gids).Scan(&resp.Details).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading groups details") + logrus.WithError(err).Errorf("error loading groups details") response.Error(c, response.ErrGetGroupsDetailsNotFound, err) return } @@ -328,7 +329,7 @@ func (s *GroupService) GetGroups(c *gin.Context) { Joins(`LEFT JOIN groups_to_policies gtp ON gtp.policy_id = modules.policy_id`). Find(&modulesa, "gtp.group_id IN (?) AND status = 'joined'", gids).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy modules") + logrus.WithError(err).Errorf("error finding policy modules") response.Error(c, response.ErrGetGroupsModulesNotFound, err) return } else { @@ -337,7 +338,7 @@ func (s *GroupService) GetGroups(c *gin.Context) { name := modulesa[i].Info.Name policy_id := modulesa[i].PolicyID if err = modulesa[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy module data '%d' '%s'", id, name) + logrus.WithError(err).Errorf("error validating policy module data '%d' '%s'", id, name) response.Error(c, response.ErrGetGroupsInvalidModuleData, err) return } @@ -350,7 +351,7 @@ func (s *GroupService) GetGroups(c *gin.Context) { } if err = iDB.Find(&gpss, "group_id IN (?)", gids).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy to groups links") + logrus.WithError(err).Errorf("error finding policy to groups links") response.Error(c, response.ErrGroupPolicyGroupsNotFound, err) return } @@ -361,7 +362,7 @@ func (s *GroupService) GetGroups(c *gin.Context) { Joins(`LEFT JOIN groups_to_policies gtp ON gtp.policy_id = policies.id AND gtp.group_id IN (?)`, gids). Find(&policiesa).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group policies") + logrus.WithError(err).Errorf("error finding group policies") response.Error(c, response.ErrGroupPolicyPoliciesNotFound, err) return } else { @@ -369,7 +370,7 @@ func (s *GroupService) GetGroups(c *gin.Context) { id := policiesa[i].ID name := policiesa[i].Info.Name if err = policiesa[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%d' '%s'", id, name) + logrus.WithError(err).Errorf("error validating policy data '%d' '%s'", id, name) response.Error(c, response.ErrGetGroupsInvalidModuleData, err) return } @@ -432,19 +433,19 @@ func (s *GroupService) GetGroup(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = iDB.Take(&resp.Group, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group by hash") + logrus.WithError(err).Errorf("error finding group by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGroupsNotFound, err) } else { @@ -452,14 +453,14 @@ func (s *GroupService) GetGroup(c *gin.Context) { } return } else if err = resp.Group.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group data '%s'", resp.Group.Hash) + logrus.WithError(err).Errorf("error validating group data '%s'", resp.Group.Hash) response.Error(c, response.ErrGroupsInvalidData, err) return } sqlQuery := sqlGroupDetails + ` WHERE g.hash = ? AND g.deleted_at IS NULL` if err = iDB.Raw(sqlQuery, hash).Scan(&resp.Details).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading details by group hash '%s'", hash) + logrus.WithError(err).Errorf("error loading details by group hash '%s'", hash) response.Error(c, response.ErrGetGroupDetailsNotFound, err) return } @@ -469,7 +470,7 @@ func (s *GroupService) GetGroup(c *gin.Context) { Joins(`LEFT JOIN groups_to_policies gtp ON gtp.policy_id = modules.policy_id`). Find(&resp.Details.Modules, "gtp.group_id = ? AND status = 'joined'", resp.Group.ID).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group modules by group ID '%d'", resp.Group.ID) + logrus.WithError(err).Errorf("error finding group modules by group ID '%d'", resp.Group.ID) response.Error(c, response.ErrGetGroupModulesNotFound, err) return } else { @@ -477,7 +478,7 @@ func (s *GroupService) GetGroup(c *gin.Context) { if err = resp.Details.Modules[i].Valid(); err != nil { id := resp.Details.Modules[i].ID name := resp.Details.Modules[i].Info.Name - utils.FromContext(c).WithError(err).Errorf("error validating group module data '%d' '%s'", id, name) + logrus.WithError(err).Errorf("error validating group module data '%d' '%s'", id, name) response.Error(c, response.ErrGetGroupsInvalidModuleData, err) return } @@ -489,7 +490,7 @@ func (s *GroupService) GetGroup(c *gin.Context) { Group: resp.Group, } if err = iDB.Model(gps).Association("policies").Find(&gps.Policies).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group policies by group model") + logrus.WithError(err).Errorf("error finding group policies by group model") response.Error(c, response.ErrGetGroupsPoliciesNotFound, err) return } @@ -523,13 +524,13 @@ func (s *GroupService) PatchGroup(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -542,7 +543,7 @@ func (s *GroupService) PatchGroup(c *gin.Context) { if nameErr == nil { uaf.ObjectDisplayName = name } - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrGroupsValidationFail, err) return } @@ -550,13 +551,13 @@ func (s *GroupService) PatchGroup(c *gin.Context) { uaf.ObjectDisplayName = group.Info.Name.En if hash != group.Hash { - utils.FromContext(c).WithError(nil).Errorf("mismatch group hash to requested one") + logrus.WithError(nil).Errorf("mismatch group hash to requested one") response.Error(c, response.ErrGroupsValidationFail, nil) return } if err = iDB.Model(&group).Count(&count).Error; err != nil || count == 0 { - utils.FromContext(c).WithError(nil).Errorf("error updating group by hash '%s', group not found", hash) + logrus.WithError(nil).Errorf("error updating group by hash '%s', group not found", hash) response.Error(c, response.ErrGroupsNotFound, err) return } @@ -565,11 +566,11 @@ func (s *GroupService) PatchGroup(c *gin.Context) { err = iDB.Select("", public_info...).Save(&group).Error if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - utils.FromContext(c).WithError(nil).Errorf("error updating group by hash '%s', group not found", hash) + logrus.WithError(nil).Errorf("error updating group by hash '%s', group not found", hash) response.Error(c, response.ErrGroupsNotFound, err) return } else if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating group by hash '%s'", hash) + logrus.WithError(err).Errorf("error updating group by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } @@ -602,7 +603,7 @@ func (s *GroupService) PatchGroupPolicy(c *gin.Context) { defer s.userActionWriter.WriteUserAction(uaf) if err := c.ShouldBindJSON(&form); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrGroupsInvalidRequest, err) return } @@ -617,19 +618,19 @@ func (s *GroupService) PatchGroupPolicy(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = iDB.Take(&group, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group by hash") + logrus.WithError(err).Errorf("error finding group by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGroupsNotFound, err) } else { @@ -637,13 +638,13 @@ func (s *GroupService) PatchGroupPolicy(c *gin.Context) { } return } else if err = group.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group data '%s'", group.Hash) + logrus.WithError(err).Errorf("error validating group data '%s'", group.Hash) response.Error(c, response.ErrGroupsInvalidData, err) return } if err = iDB.Take(&policy, "hash = ?", form.Policy.Hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGetGroupsPoliciesNotFound, err) } else { @@ -651,14 +652,14 @@ func (s *GroupService) PatchGroupPolicy(c *gin.Context) { } return } else if err = policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policy.Hash) response.Error(c, response.ErrInternal, err) return } httpErr, err := makeGroupPolicyAction(form.Action, iDB, group, policy) if httpErr != nil { - utils.FromContext(c).WithError(err).Errorf("error patching group policy by action: %s", httpErr.Error()) + logrus.WithError(err).Errorf("error patching group policy by action: %s", httpErr.Error()) response.Error(c, httpErr, err) } @@ -686,7 +687,7 @@ func (s *GroupService) CreateGroup(c *gin.Context) { defer s.userActionWriter.WriteUserAction(uaf) if err := c.ShouldBindJSON(&info); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrEventsInvalidRequest, err) return } @@ -694,13 +695,13 @@ func (s *GroupService) CreateGroup(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -720,11 +721,11 @@ func (s *GroupService) CreateGroup(c *gin.Context) { if info.From != 0 { if err = iDB.Take(&groupFrom, "id = ?", info.From).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding source group by ID") + logrus.WithError(err).Errorf("error finding source group by ID") response.Error(c, response.ErrCreateGroupSourceNotFound, err) return } else if err = groupFrom.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group data '%s'", groupFrom.Hash) + logrus.WithError(err).Errorf("error validating group data '%s'", groupFrom.Hash) response.Error(c, response.ErrGetAgentInvalidGroupData, err) return } @@ -749,7 +750,7 @@ func (s *GroupService) CreateGroup(c *gin.Context) { } if err = iDB.Create(&group).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error creating group") + logrus.WithError(err).Errorf("error creating group") response.Error(c, response.ErrCreateGroupCreateFail, err) return } @@ -758,7 +759,7 @@ func (s *GroupService) CreateGroup(c *gin.Context) { var groupToPolicy []models.GroupToPolicy err = iDB.Where("group_id = ?", groupFrom.ID).Find(&groupToPolicy).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group policies by group ID") + logrus.WithError(err).Errorf("error finding group policies by group ID") response.Error(c, response.ErrCreateGroupGetPolicies, err) return } @@ -766,7 +767,7 @@ func (s *GroupService) CreateGroup(c *gin.Context) { gpt.ID = 0 gpt.GroupID = group.ID if err = iDB.Create(&gpt).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error creating group policies") + logrus.WithError(err).Errorf("error creating group policies") response.Error(c, response.ErrCreateGroupCreatePolicies, err) return } @@ -797,27 +798,27 @@ func (s *GroupService) DeleteGroup(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = iDB.Take(&group, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group by hash") + logrus.WithError(err).Errorf("error finding group by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGroupsNotFound, err) - } else { - response.Error(c, response.ErrInternal, err) + return } + response.Error(c, response.ErrInternal, err) return } else if err = group.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group data '%s'", group.Hash) + logrus.WithError(err).Errorf("error validating group data '%s'", group.Hash) response.Error(c, response.ErrGroupsInvalidData, err) return } @@ -825,7 +826,7 @@ func (s *GroupService) DeleteGroup(c *gin.Context) { uaf.ObjectDisplayName = group.Info.Name.En if err = iDB.Delete(&group).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting group by hash '%s'", hash) + logrus.WithError(err).Errorf("error deleting group by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } diff --git a/pkg/app/api/server/private/modules.go b/pkg/app/api/server/private/modules.go index f1b0530d..da22f443 100644 --- a/pkg/app/api/server/private/modules.go +++ b/pkg/app/api/server/private/modules.go @@ -20,6 +20,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/copier" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/client" "soldr/pkg/app/api/models" @@ -175,9 +176,9 @@ func getService(c *gin.Context) *models.Service { var sv *models.Service if val, ok := c.Get("SV"); !ok { - utils.FromContext(c).WithError(nil).Errorf("error getting vxservice instance from context") + logrus.WithError(nil).Errorf("error getting vxservice instance from context") } else if sv = val.(*models.Service); sv == nil { - utils.FromContext(c).WithError(nil).Errorf("got nil value vxservice instance from context") + logrus.WithError(nil).Errorf("got nil value vxservice instance from context") } return sv @@ -187,9 +188,9 @@ func getDBEncryptor(c *gin.Context) crypto.IDBConfigEncryptor { var encryptor crypto.IDBConfigEncryptor if cr, ok := c.Get("crp"); !ok { - utils.FromContext(c).WithError(nil).Errorf("error getting secure config encryptor from context") + logrus.WithError(nil).Errorf("error getting secure config encryptor from context") } else if encryptor = cr.(crypto.IDBConfigEncryptor); encryptor == nil { - utils.FromContext(c).WithError(nil).Errorf("got nil value secure config encryptor from context") + logrus.WithError(nil).Errorf("got nil value secure config encryptor from context") } return encryptor @@ -873,12 +874,12 @@ func removeUnusedModuleVersion(c *gin.Context, iDB *gorm.DB, name, version strin if count == 0 { s3, err := storage.NewS3(sv.Info.S3.ToS3ConnParams()) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") return err } if err = s3.RemoveDir(name + "/" + version + "/"); err != nil && err.Error() != "not found" { - utils.FromContext(c).WithError(err).Errorf("error removing module data from s3") + logrus.WithError(err).Errorf("error removing module data from s3") return err } } @@ -894,7 +895,7 @@ func updateDependenciesWhenModuleRemove(c *gin.Context, iDB *gorm.DB, name strin ) if err = iDB.Find(&modules, "dependencies LIKE ?", `%"`+name+`"%`).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding modules by dependencies") + logrus.WithError(err).Errorf("error finding modules by dependencies") return err } @@ -921,7 +922,7 @@ func updateDependenciesWhenModuleRemove(c *gin.Context, iDB *gorm.DB, name strin m.DynamicDependencies = ddeps if err = iDB.Select("", incl...).Save(&m).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating config module") + logrus.WithError(err).Errorf("error updating config module") return err } } @@ -933,14 +934,14 @@ func updatePolicyModulesByModuleS(c *gin.Context, moduleS *models.ModuleS, sv *m iDB := utils.GetDB(sv.Info.DB.User, sv.Info.DB.Pass, sv.Info.DB.Host, strconv.Itoa(int(sv.Info.DB.Port)), sv.Info.DB.Name) if iDB == nil { - utils.FromContext(c).WithError(nil).Errorf("error openning connection to instance DB") + logrus.WithError(nil).Errorf("error openning connection to instance DB") return errors.New("failed to connect to instance DB") } defer iDB.Close() encryptor := getDBEncryptor(c) if encryptor == nil { - utils.FromContext(c).WithError(nil).Errorf("encryptor not found") + logrus.WithError(nil).Errorf("encryptor not found") return errors.New("encryptor not found") } @@ -950,7 +951,7 @@ func updatePolicyModulesByModuleS(c *gin.Context, moduleS *models.ModuleS, sv *m moduleS.Info.Name, moduleS.Info.Version.String(), moduleS.LastUpdate) } if err := iDB.Scopes(scope).Find(&modules).Error; err != nil { - utils.FromContext(c).WithError(err). + logrus.WithError(err). Errorf("error finding policy modules by name and version '%s' '%s'", moduleS.Info.Name, moduleS.Info.Version.String()) return err @@ -959,7 +960,7 @@ func updatePolicyModulesByModuleS(c *gin.Context, moduleS *models.ModuleS, sv *m } if err := CopyModuleAFilesToInstanceS3(&moduleS.Info, sv); err != nil { - utils.FromContext(c).WithError(err).Errorf("error copying module files to S3") + logrus.WithError(err).Errorf("error copying module files to S3") return err } @@ -968,22 +969,22 @@ func updatePolicyModulesByModuleS(c *gin.Context, moduleS *models.ModuleS, sv *m var err error moduleA, err = MergeModuleAConfigFromModuleS(&moduleA, moduleS, encryptor) if err != nil { - utils.FromContext(c).WithError(err).Errorf("invalid module state") + logrus.WithError(err).Errorf("invalid module state") return err } if err := moduleA.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("invalid module state") + logrus.WithError(err).Errorf("invalid module state") return err } err = moduleA.EncryptSecureParameters(encryptor) if err != nil { - utils.FromContext(c).WithError(err).Errorf("failed to encrypt module secure config") + logrus.WithError(err).Errorf("failed to encrypt module secure config") return fmt.Errorf("failed to encrypt module secure config: %w", err) } if err := iDB.Omit(excl...).Save(&moduleA).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating module") + logrus.WithError(err).Errorf("error updating module") return err } } @@ -1333,20 +1334,20 @@ func (s *ModuleService) GetAgentModules(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrModulesInvalidRequest, err) return } serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -1358,7 +1359,7 @@ func (s *ModuleService) GetAgentModules(c *gin.Context) { var agentPolicies models.AgentPolicies if err = iDB.Take(&agentPolicies, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent by hash") + logrus.WithError(err).Errorf("error finding agent by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGetAgentModulesAgentNotFound, err) } else { @@ -1367,12 +1368,12 @@ func (s *ModuleService) GetAgentModules(c *gin.Context) { return } if err = iDB.Model(agentPolicies).Association("policies").Find(&agentPolicies.Policies).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent policies by agent model") + logrus.WithError(err).Errorf("error finding agent policies by agent model") response.Error(c, response.ErrGetAgentModulesAgentPoliciesNotFound, err) return } if err = agentPolicies.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent policies data '%s'", agentPolicies.Hash) + logrus.WithError(err).Errorf("error validating agent policies data '%s'", agentPolicies.Hash) response.Error(c, response.ErrGetAgentModulesInvalidAgentPoliciesData, err) return } @@ -1396,14 +1397,14 @@ func (s *ModuleService) GetAgentModules(c *gin.Context) { }, }) if resp.Total, err = query.Query(iDB, &resp.Modules); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent modules") + logrus.WithError(err).Errorf("error finding agent modules") response.Error(c, response.ErrGetAgentModulesInvalidQuery, err) return } for i := 0; i < len(resp.Modules); i++ { if err = resp.Modules[i].Valid(); err != nil { - utils.FromContext(c).WithError(err). + logrus.WithError(err). Errorf("error validating agent module data '%s'", resp.Modules[i].Info.Name) response.Error(c, response.ErrGetAgentModulesInvalidAgentData, err) return @@ -1421,14 +1422,14 @@ func (s *ModuleService) GetAgentModules(c *gin.Context) { } if err = s.db.Scopes(scope).Find(&modules).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system modules list by names") + logrus.WithError(err).Errorf("error finding system modules list by names") response.Error(c, response.ErrGetAgentsGetSystemModulesFail, err) return } var details []agentModuleDetails if err = iDB.Raw(sqlAgentModuleDetails, agentPolicies.ID).Scan(&details).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading agents modules details") + logrus.WithError(err).Errorf("error loading agents modules details") response.Error(c, response.ErrGetAgentModulesDetailsNotFound, err) return @@ -1479,20 +1480,20 @@ func (s *ModuleService) GetAgentModule(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } var agentPolicies models.AgentPolicies if err = iDB.Take(&agentPolicies, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent by hash") + logrus.WithError(err).Errorf("error finding agent by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGetAgentModuleAgentNotFound, err) } else { @@ -1501,12 +1502,12 @@ func (s *ModuleService) GetAgentModule(c *gin.Context) { return } if err = iDB.Model(agentPolicies).Association("policies").Find(&agentPolicies.Policies).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent policies by agent model") + logrus.WithError(err).Errorf("error finding agent policies by agent model") response.Error(c, response.ErrGetAgentModuleAgentPoliceNotFound, err) return } if err = agentPolicies.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent policies data '%s'", agentPolicies.Hash) + logrus.WithError(err).Errorf("error validating agent policies data '%s'", agentPolicies.Hash) response.Error(c, response.ErrGetAgentModuleInvalidAgentPoliceData, err) return } @@ -1519,11 +1520,11 @@ func (s *ModuleService) GetAgentModule(c *gin.Context) { return db.Where("policy_id IN (?) AND name = ? AND status = 'joined'", pids, moduleName) } if err = iDB.Scopes(scopeModule).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy module by module name") + logrus.WithError(err).Errorf("error finding policy module by module name") response.Error(c, response.ErrModulesNotFound, err) return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidData, err) return } @@ -1564,13 +1565,13 @@ func (s *ModuleService) GetAgentBModule(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -1581,15 +1582,15 @@ func (s *ModuleService) GetAgentBModule(c *gin.Context) { var agentPolicies models.AgentPolicies if err = iDB.Take(&agentPolicies, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent by hash") + logrus.WithError(err).Errorf("error finding agent by hash") return } if err = iDB.Model(agentPolicies).Association("policies").Find(&agentPolicies.Policies).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent policies by agent model") + logrus.WithError(err).Errorf("error finding agent policies by agent model") return } if err = agentPolicies.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent policies data '%s'", agentPolicies.Hash) + logrus.WithError(err).Errorf("error validating agent policies data '%s'", agentPolicies.Hash) response.Error(c, response.ErrGetAgentBModuleInvalidAgentPoliceData, err) return } @@ -1602,23 +1603,23 @@ func (s *ModuleService) GetAgentBModule(c *gin.Context) { return db.Where("policy_id IN (?) AND name = ? AND status = 'joined'", pids, moduleName) } if err = iDB.Scopes(scopeModule).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy module by module name") + logrus.WithError(err).Errorf("error finding policy module by module name") return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidData, err) return } s3, err = storage.NewS3(sv.Info.S3.ToS3ConnParams()) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") return } path := path.Join(moduleName, module.Info.Version.String(), "bmodule", filepath) if data, err = s3.ReadFile(path); err != nil { - utils.FromContext(c).WithError(err).Errorf("error reading module file '%s'", path) + logrus.WithError(err).Errorf("error reading module file '%s'", path) return } } @@ -1646,20 +1647,20 @@ func (s *ModuleService) GetGroupModules(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrModulesInvalidRequest, err) return } serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -1670,7 +1671,7 @@ func (s *ModuleService) GetGroupModules(c *gin.Context) { } if err = iDB.Take(&gps, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group by hash") + logrus.WithError(err).Errorf("error finding group by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGetGroupModulesGroupNotFound, err) } else { @@ -1679,12 +1680,12 @@ func (s *ModuleService) GetGroupModules(c *gin.Context) { return } if err = iDB.Model(gps).Association("policies").Find(&gps.Policies).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group policies by group model") + logrus.WithError(err).Errorf("error finding group policies by group model") response.Error(c, response.ErrGetGroupModulesGroupPoliciesNotFound, err) return } if err = gps.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group policies data '%s'", gps.Hash) + logrus.WithError(err).Errorf("error validating group policies data '%s'", gps.Hash) response.Error(c, response.ErrGetGroupModulesInvalidGroupPoliciesData, err) return } @@ -1708,14 +1709,14 @@ func (s *ModuleService) GetGroupModules(c *gin.Context) { }, }) if resp.Total, err = query.Query(iDB, &resp.Modules); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group modules") + logrus.WithError(err).Errorf("error finding group modules") response.Error(c, response.ErrGetGroupModulesInvalidGroupQuery, err) return } for i := 0; i < len(resp.Modules); i++ { if err = resp.Modules[i].Valid(); err != nil { - utils.FromContext(c).WithError(err). + logrus.WithError(err). Errorf("error validating group module data '%s'", resp.Modules[i].Info.Name) response.Error(c, response.ErrGetGroupModulesInvalidGroupData, err) @@ -1734,14 +1735,14 @@ func (s *ModuleService) GetGroupModules(c *gin.Context) { } if err = s.db.Scopes(scope).Find(&modules).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system modules list by names") + logrus.WithError(err).Errorf("error finding system modules list by names") response.Error(c, response.ErrGetGroupsGetSystemModulesFail, err) return } var details []groupModuleDetails if err = iDB.Raw(sqlGroupModuleDetails, gps.ID).Scan(&details).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading group modules details") + logrus.WithError(err).Errorf("error loading group modules details") response.Error(c, response.ErrGetGroupModulesDetailsNotFound, err) return } @@ -1792,19 +1793,19 @@ func (s *ModuleService) GetGroupModule(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = iDB.Take(&gps, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group by hash") + logrus.WithError(err).Errorf("error finding group by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGetGroupModuleGroupNotFound, err) } else { @@ -1813,12 +1814,12 @@ func (s *ModuleService) GetGroupModule(c *gin.Context) { return } if err = iDB.Model(gps).Association("policies").Find(&gps.Policies).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group policies by group model") + logrus.WithError(err).Errorf("error finding group policies by group model") response.Error(c, response.ErrGetGroupModuleGroupPoliciesNotFound, err) return } if err = gps.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group policies data '%s'", gps.Hash) + logrus.WithError(err).Errorf("error validating group policies data '%s'", gps.Hash) response.Error(c, response.ErrGetGroupModuleInvalidGroupPoliciesData, err) return } @@ -1831,12 +1832,12 @@ func (s *ModuleService) GetGroupModule(c *gin.Context) { return db.Where("policy_id IN (?) AND name = ? AND status = 'joined'", pids, moduleName) } if err = iDB.Scopes(scopeModule).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy module by module name") + logrus.WithError(err).Errorf("error finding policy module by module name") response.Error(c, response.ErrModulesNotFound, err) return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidData, err) return } @@ -1878,13 +1879,13 @@ func (s *ModuleService) GetGroupBModule(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -1894,15 +1895,15 @@ func (s *ModuleService) GetGroupBModule(c *gin.Context) { } if err = iDB.Take(&gps, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group by hash") + logrus.WithError(err).Errorf("error finding group by hash") return } if err = iDB.Model(gps).Association("policies").Find(&gps.Policies).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group policies by group model") + logrus.WithError(err).Errorf("error finding group policies by group model") return } if err = gps.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group policies data '%s'", gps.Hash) + logrus.WithError(err).Errorf("error validating group policies data '%s'", gps.Hash) response.Error(c, response.ErrGetGroupBModuleInvalidGroupPoliciesData, err) return } @@ -1915,23 +1916,23 @@ func (s *ModuleService) GetGroupBModule(c *gin.Context) { return db.Where("policy_id IN (?) AND name = ? AND status = 'joined'", pids, moduleName) } if err = iDB.Scopes(scopeModule).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy module by module name") + logrus.WithError(err).Errorf("error finding policy module by module name") return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidData, err) return } s3, err = storage.NewS3(sv.Info.S3.ToS3ConnParams()) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") return } path := path.Join(moduleName, module.Info.Version.String(), "bmodule", filepath) if data, err = s3.ReadFile(path); err != nil { - utils.FromContext(c).WithError(err).Errorf("error reading module file '%s'", path) + logrus.WithError(err).Errorf("error reading module file '%s'", path) return } } @@ -1966,13 +1967,13 @@ func (s *ModuleService) GetPolicyModules(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -1983,7 +1984,7 @@ func (s *ModuleService) GetPolicyModules(c *gin.Context) { } if err = iDB.Take(&policy, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGetPolicyModulesPolicyNotFound, err) } else { @@ -1991,7 +1992,7 @@ func (s *ModuleService) GetPolicyModules(c *gin.Context) { } return } else if err = policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policy.Hash) response.Error(c, response.ErrGetPolicyModulesInvalidPolicyData, err) return } @@ -2012,7 +2013,7 @@ func (s *ModuleService) GetPolicyModules(c *gin.Context) { }, }) if _, err = queryA.Query(iDB, &modulesA); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy modules") + logrus.WithError(err).Errorf("error finding policy modules") response.Error(c, response.ErrGetPolicyModulesInvalidPolicyQuery, err) return } @@ -2040,14 +2041,14 @@ func (s *ModuleService) GetPolicyModules(c *gin.Context) { }, } if resp.Total, err = queryS.Query(s.db, &modulesS, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system modules") + logrus.WithError(err).Errorf("error finding system modules") response.Error(c, response.ErrGetPolicyModulesInvalidModulesQuery, err) return } var details []policyModuleDetails if err = iDB.Raw(sqlPolicyModuleDetails, policy.ID).Scan(&details).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading policies modules details") + logrus.WithError(err).Errorf("error loading policies modules details") response.Error(c, response.ErrGetPolicyModulesDetailsNotFound, err) return } @@ -2086,7 +2087,7 @@ func (s *ModuleService) GetPolicyModules(c *gin.Context) { for i := 0; i < len(resp.Modules); i++ { if err = resp.Modules[i].Valid(); err != nil { - utils.FromContext(c).WithError(err). + logrus.WithError(err). Errorf("error validating policy module data '%s'", resp.Modules[i].Info.Name) response.Error(c, response.ErrGetPolicyModulesInvalidPolicyData, err) return @@ -2101,7 +2102,7 @@ func (s *ModuleService) GetPolicyModules(c *gin.Context) { } duplicateMap, err := CheckMultipleModulesDuplicate(iDB, moduleNames, policy.ID) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error checking duplicate modules") + logrus.WithError(err).Errorf("error checking duplicate modules") response.Error(c, response.ErrInternal, err) return } @@ -2134,19 +2135,19 @@ func (s *ModuleService) GetPolicyModule(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = iDB.Take(&policy, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGetPolicyModulePolicyNotFound, err) } else { @@ -2154,17 +2155,17 @@ func (s *ModuleService) GetPolicyModule(c *gin.Context) { } return } else if err = policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policy.Hash) response.Error(c, response.ErrGetPolicyModuleInvalidPolicyData, err) return } if err = iDB.Take(&module, "policy_id = ? AND name = ?", policy.ID, moduleName).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy module by name") + logrus.WithError(err).Errorf("error finding policy module by name") response.Error(c, response.ErrModulesNotFound, err) return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidData, err) return } @@ -2205,13 +2206,13 @@ func (s *ModuleService) GetPolicyBModule(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -2221,24 +2222,24 @@ func (s *ModuleService) GetPolicyBModule(c *gin.Context) { } if err = iDB.Take(&policy, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") return } if err = iDB.Take(&module, "policy_id = ? AND name = ?", policy.ID, moduleName).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy module by name") + logrus.WithError(err).Errorf("error finding policy module by name") return } s3, err = storage.NewS3(sv.Info.S3.ToS3ConnParams()) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") return } path := path.Join(moduleName, module.Info.Version.String(), "bmodule", filepath) if data, err = s3.ReadFile(path); err != nil { - utils.FromContext(c).WithError(err).Errorf("error reading module file '%s'", path) + logrus.WithError(err).Errorf("error reading module file '%s'", path) return } } @@ -2273,13 +2274,13 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -2289,7 +2290,7 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { if nameErr == nil { uaf.ObjectDisplayName = name } - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrModulesInvalidRequest, err) return } @@ -2300,7 +2301,7 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { } if err = iDB.Take(&policy, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrPatchPolicyModulePolicyNotFound, err) } else { @@ -2311,7 +2312,7 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { uaf.ObjectDisplayName = policy.Info.Name.En if err = policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policy.Hash) response.Error(c, response.ErrPatchPolicyModuleInvalidPolicyData, err) return } @@ -2333,7 +2334,7 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { } if err = s.db.Scopes(scope).Take(&moduleS).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -2341,7 +2342,7 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { } return } else if err = moduleS.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", moduleS.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", moduleS.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } @@ -2350,13 +2351,13 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { moduleA.FromModuleS(&moduleS) moduleA.PolicyID = policy.ID } else if err = moduleA.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module data '%s'", moduleA.Info.Name) + logrus.WithError(err).Errorf("error validating module data '%s'", moduleA.Info.Name) response.Error(c, response.ErrModulesInvalidData, err) return } if moduleA.ID == 0 && form.Action != "activate" { - utils.FromContext(c).WithError(nil).Errorf("error on %s module, policy module not found", form.Action) + logrus.WithError(nil).Errorf("error on %s module, policy module not found", form.Action) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } @@ -2366,26 +2367,26 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { switch form.Action { case "activate": if err = CheckModulesDuplicate(iDB, &moduleA); err != nil { - utils.FromContext(c).WithError(err).Errorf("error checking duplicate modules") + logrus.WithError(err).Errorf("error checking duplicate modules") response.Error(c, response.ErrPatchPolicyModuleDuplicatedModule, err) return } if moduleA.ID == 0 { if err = CopyModuleAFilesToInstanceS3(&moduleA.Info, sv); err != nil { - utils.FromContext(c).WithError(err).Errorf("error copying module files to S3") + logrus.WithError(err).Errorf("error copying module files to S3") response.Error(c, response.ErrInternal, err) return } if err = moduleA.ValidateEncryption(encryptor); err != nil { - utils.FromContext(c).WithError(err).Errorf("module config not encrypted") + logrus.WithError(err).Errorf("module config not encrypted") response.Error(c, response.ErrModulesDataNotEncryptedOnDBInsert, nil) return } if err = iDB.Create(&moduleA).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error creating module") + logrus.WithError(err).Errorf("error creating module") response.Error(c, response.ErrInternal, err) return } @@ -2399,7 +2400,7 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { } else { moduleA.Status = "joined" if err = iDB.Select("", incl...).Save(&moduleA).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating module") + logrus.WithError(err).Errorf("error updating module") response.Error(c, response.ErrInternal, err) return } @@ -2408,28 +2409,28 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { case "deactivate": moduleA.Status = "inactive" if err = iDB.Select("", incl...).Save(&moduleA).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating module") + logrus.WithError(err).Errorf("error updating module") response.Error(c, response.ErrInternal, err) return } case "store": if err = form.Module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module") + logrus.WithError(err).Errorf("error validating module") response.Error(c, response.ErrPatchPolicyModuleNewModuleInvalid, err) return } changes, err := compareModulesChanges(form.Module, moduleA, encryptor) if err != nil { - utils.FromContext(c).WithError(err).Errorf("failed to compare modules changes") + logrus.WithError(err).Errorf("failed to compare modules changes") response.Error(c, response.ErrModulesFailedToCompareChanges, err) return } for _, ch := range changes { if ch { - utils.FromContext(c).WithError(nil).Errorf("error accepting module changes") + logrus.WithError(nil).Errorf("error accepting module changes") response.Error(c, response.ErrPatchPolicyModuleAcceptFail, nil) return } @@ -2437,12 +2438,12 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { err = form.Module.EncryptSecureParameters(encryptor) if err != nil { - utils.FromContext(c).WithError(err).Errorf("failed to encrypt module secure config") + logrus.WithError(err).Errorf("failed to encrypt module secure config") response.Error(c, response.ErrModulesFailedToEncryptSecureConfig, err) return } if err = iDB.Omit(excl...).Save(&form.Module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error saving module") + logrus.WithError(err).Errorf("error saving module") response.Error(c, response.ErrInternal, err) return } @@ -2450,51 +2451,51 @@ func (s *ModuleService) PatchPolicyModule(c *gin.Context) { case "update": moduleVersion := moduleA.Info.Version.String() if moduleVersion == moduleS.Info.Version.String() { - utils.FromContext(c).WithError(nil).Errorf("error updating module to the same version: %s", moduleVersion) + logrus.WithError(nil).Errorf("error updating module to the same version: %s", moduleVersion) response.Error(c, response.ErrInternal, err) return } moduleA, err = MergeModuleAConfigFromModuleS(&moduleA, &moduleS, encryptor) if err != nil { - utils.FromContext(c).WithError(err).Errorf("invalid module state") + logrus.WithError(err).Errorf("invalid module state") response.Error(c, response.ErrInternal, err) return } if err = moduleA.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("invalid module state") + logrus.WithError(err).Errorf("invalid module state") response.Error(c, response.ErrInternal, err) return } err = moduleA.EncryptSecureParameters(encryptor) if err != nil { - utils.FromContext(c).WithError(err).Errorf("failed to encrypt module secure config") + logrus.WithError(err).Errorf("failed to encrypt module secure config") response.Error(c, response.ErrModulesFailedToEncryptSecureConfig, err) return } if err = CopyModuleAFilesToInstanceS3(&moduleA.Info, sv); err != nil { - utils.FromContext(c).WithError(err).Errorf("error copying module files to S3") + logrus.WithError(err).Errorf("error copying module files to S3") response.Error(c, response.ErrInternal, err) return } if err = iDB.Omit(excl...).Save(&moduleA).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating module") + logrus.WithError(err).Errorf("error updating module") response.Error(c, response.ErrInternal, err) return } if err = removeUnusedModuleVersion(c, iDB, moduleName, moduleVersion, sv); err != nil { - utils.FromContext(c).WithError(err).Errorf("error removing unused module data") + logrus.WithError(err).Errorf("error removing unused module data") response.Error(c, response.ErrInternal, err) return } default: - utils.FromContext(c).WithError(nil).Errorf("error making unknown action on module") + logrus.WithError(nil).Errorf("error making unknown action on module") response.Error(c, response.ErrPatchPolicyModuleActionNotFound, nil) return } @@ -2565,19 +2566,19 @@ func (s *ModuleService) DeletePolicyModule(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } if err = iDB.Take(&policy, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrDeletePolicyModulePolicyNotFound, err) } else { @@ -2585,7 +2586,7 @@ func (s *ModuleService) DeletePolicyModule(c *gin.Context) { } return } else if err = policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policy.Hash) response.Error(c, response.ErrDeletePolicyModuleInvalidPolicyData, err) return } @@ -2597,7 +2598,7 @@ func (s *ModuleService) DeletePolicyModule(c *gin.Context) { } if err = iDB.Take(&module, "policy_id = ? AND name = ?", policy.ID, moduleName).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy module by name") + logrus.WithError(err).Errorf("error finding policy module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesNotFound, err) } else { @@ -2605,20 +2606,20 @@ func (s *ModuleService) DeletePolicyModule(c *gin.Context) { } return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidData, err) return } if err = iDB.Delete(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting policy module by name '%s'", moduleName) + logrus.WithError(err).Errorf("error deleting policy module by name '%s'", moduleName) response.Error(c, response.ErrInternal, err) return } moduleVersion := module.Info.Version.String() if err = removeUnusedModuleVersion(c, iDB, moduleName, moduleVersion, sv); err != nil { - utils.FromContext(c).WithError(err).Errorf("error removing unused module data") + logrus.WithError(err).Errorf("error removing unused module data") response.Error(c, response.ErrInternal, err) return } @@ -2659,11 +2660,11 @@ func (s *ModuleService) SetPolicyModuleSecureConfigValue(c *gin.Context) { err := c.ShouldBindJSON(&payload) switch { case err != nil: - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrModulesInvalidRequest, err) return case len(payload) != 1: - utils.FromContext(c).WithError(err).Errorf("only one key-value pair in body allowed") + logrus.WithError(err).Errorf("only one key-value pair in body allowed") response.Error(c, response.ErrModulesInvalidRequest, err) return } @@ -2675,13 +2676,13 @@ func (s *ModuleService) SetPolicyModuleSecureConfigValue(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -2697,7 +2698,7 @@ func (s *ModuleService) SetPolicyModuleSecureConfigValue(c *gin.Context) { } if err = iDB.Take(&policy, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrPatchPolicyModulePolicyNotFound, err) } else { @@ -2705,7 +2706,7 @@ func (s *ModuleService) SetPolicyModuleSecureConfigValue(c *gin.Context) { } return } else if err = policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policy.Hash) response.Error(c, response.ErrPatchPolicyModuleInvalidPolicyData, err) return } @@ -2717,7 +2718,7 @@ func (s *ModuleService) SetPolicyModuleSecureConfigValue(c *gin.Context) { } if err = s.db.Scopes(scope).Take(&moduleS).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -2725,7 +2726,7 @@ func (s *ModuleService) SetPolicyModuleSecureConfigValue(c *gin.Context) { } return } else if err = moduleS.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", moduleS.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", moduleS.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } @@ -2735,13 +2736,13 @@ func (s *ModuleService) SetPolicyModuleSecureConfigValue(c *gin.Context) { moduleA.FromModuleS(&moduleS) moduleA.PolicyID = policy.ID } else if err = moduleA.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module data '%s'", moduleA.Info.Name) + logrus.WithError(err).Errorf("error validating module data '%s'", moduleA.Info.Name) response.Error(c, response.ErrModulesInvalidData, err) return } if err = moduleA.DecryptSecureParameters(encryptor); err != nil { - utils.FromContext(c).WithError(err). + logrus.WithError(err). Errorf("error decrypting module data '%s'", moduleA.Info.Name) response.Error(c, response.ErrModulesFailedToDecryptSecureConfig, err) return @@ -2749,7 +2750,7 @@ func (s *ModuleService) SetPolicyModuleSecureConfigValue(c *gin.Context) { param, ok := moduleA.SecureCurrentConfig[paramName] if !ok { - utils.FromContext(c).WithError(err).Errorf("module secure parameter not exists") + logrus.WithError(err).Errorf("module secure parameter not exists") response.Error(c, response.ErrModulesInvalidRequest, err) return } @@ -2759,19 +2760,19 @@ func (s *ModuleService) SetPolicyModuleSecureConfigValue(c *gin.Context) { Value: payload[paramName], } if err = moduleA.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error saving module") + logrus.WithError(err).Errorf("error saving module") response.Error(c, response.ErrModulesInvalidParameterValue, err) return } err = moduleA.EncryptSecureParameters(encryptor) if err != nil { - utils.FromContext(c).WithError(err).Errorf("failed to encrypt module secure config") + logrus.WithError(err).Errorf("failed to encrypt module secure config") response.Error(c, response.ErrModulesFailedToEncryptSecureConfig, err) return } if err = iDB.Save(&moduleA).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error saving module") + logrus.WithError(err).Errorf("error saving module") response.Error(c, response.ErrInternal, err) return } @@ -2809,13 +2810,13 @@ func (s *ModuleService) GetPolicyModuleSecureConfigValue(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -2831,7 +2832,7 @@ func (s *ModuleService) GetPolicyModuleSecureConfigValue(c *gin.Context) { } if err = iDB.Take(&policy, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrPatchPolicyModulePolicyNotFound, err) } else { @@ -2839,7 +2840,7 @@ func (s *ModuleService) GetPolicyModuleSecureConfigValue(c *gin.Context) { } return } else if err = policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policy.Hash) response.Error(c, response.ErrPatchPolicyModuleInvalidPolicyData, err) return } @@ -2851,7 +2852,7 @@ func (s *ModuleService) GetPolicyModuleSecureConfigValue(c *gin.Context) { } if err = s.db.Scopes(scope).Take(&moduleS).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -2859,7 +2860,7 @@ func (s *ModuleService) GetPolicyModuleSecureConfigValue(c *gin.Context) { } return } else if err = moduleS.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", moduleS.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", moduleS.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } @@ -2869,13 +2870,13 @@ func (s *ModuleService) GetPolicyModuleSecureConfigValue(c *gin.Context) { moduleA.FromModuleS(&moduleS) moduleA.PolicyID = policy.ID } else if err = moduleA.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module data '%s'", moduleA.Info.Name) + logrus.WithError(err).Errorf("error validating module data '%s'", moduleA.Info.Name) response.Error(c, response.ErrModulesInvalidData, err) return } if err = moduleA.DecryptSecureParameters(encryptor); err != nil { - utils.FromContext(c).WithError(err). + logrus.WithError(err). Errorf("error decrypting module data '%s'", moduleA.Info.Name) response.Error(c, response.ErrModulesFailedToDecryptSecureConfig, err) return @@ -2883,7 +2884,7 @@ func (s *ModuleService) GetPolicyModuleSecureConfigValue(c *gin.Context) { val, ok := moduleA.SecureCurrentConfig[paramName] if !ok { - utils.FromContext(c).WithError(err).Errorf("module secure parameter not exists") + logrus.WithError(err).Errorf("module secure parameter not exists") response.Error(c, response.ErrModulesInvalidRequest, err) return } @@ -2913,7 +2914,7 @@ func (s *ModuleService) GetModules(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrModulesInvalidRequest, err) return } @@ -2961,7 +2962,7 @@ func (s *ModuleService) GetModules(c *gin.Context) { } total, err := query.Query(s.db, &resp.Modules, funcs...) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system modules") + logrus.WithError(err).Errorf("error finding system modules") response.Error(c, response.ErrGetModulesInvalidModulesQuery, err) return } @@ -2969,14 +2970,14 @@ func (s *ModuleService) GetModules(c *gin.Context) { for _, module := range resp.Modules { if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err). + logrus.WithError(err). Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } if err = module.DecryptSecureParameters(encryptor); err != nil { - utils.FromContext(c).WithError(err). + logrus.WithError(err). Errorf("error decrypting module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesFailedToDecryptSecureConfig, err) return @@ -3026,7 +3027,7 @@ func (s *ModuleService) CreateModule(c *gin.Context) { if err == nil { err = info.Valid() } - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrCreateModuleInvalidInfo, err) return } @@ -3037,11 +3038,11 @@ func (s *ModuleService) CreateModule(c *gin.Context) { } if err := s.db.Scopes(scope).Model(&module).Count(&count).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding number of system module") + logrus.WithError(err).Errorf("error finding number of system module") response.Error(c, response.ErrCreateModuleGetCountFail, err) return } else if count >= 1 { - utils.FromContext(c).WithError(nil).Errorf("error creating second system module") + logrus.WithError(nil).Errorf("error creating second system module") response.Error(c, response.ErrCreateModuleSecondSystemModule, err) return } @@ -3050,7 +3051,7 @@ func (s *ModuleService) CreateModule(c *gin.Context) { var err error if template, module, err = LoadModuleSTemplate(&info); err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading module") + logrus.WithError(err).Errorf("error loading module") response.Error(c, response.ErrCreateModuleLoadFail, err) return } @@ -3060,13 +3061,13 @@ func (s *ModuleService) CreateModule(c *gin.Context) { module.TenantID = tid module.ServiceType = sv.Type if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module") + logrus.WithError(err).Errorf("error validating module") response.Error(c, response.ErrCreateModuleValidationFail, err) return } if err = StoreModuleSToGlobalS3(&info, template); err != nil { - utils.FromContext(c).WithError(err).Errorf("error storing module to S3") + logrus.WithError(err).Errorf("error storing module to S3") response.Error(c, response.ErrCreateModuleStoreS3Fail, err) return } @@ -3076,7 +3077,7 @@ func (s *ModuleService) CreateModule(c *gin.Context) { return } if err = s.db.Create(module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error creating module") + logrus.WithError(err).Errorf("error creating module") response.Error(c, response.ErrCreateModuleStoreDBFail, err) return } @@ -3118,7 +3119,7 @@ func (s *ModuleService) DeleteModule(c *gin.Context) { } if err = s.db.Scopes(scope).Find(&modules).Error; err != nil || len(modules) == 0 { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if err == nil && len(modules) == 0 { response.Error(c, response.ErrModulesNotFound, err) } else { @@ -3138,29 +3139,29 @@ func (s *ModuleService) DeleteModule(c *gin.Context) { iDB := utils.GetDB(s.Info.DB.User, s.Info.DB.Pass, s.Info.DB.Host, strconv.Itoa(int(s.Info.DB.Port)), s.Info.DB.Name) if iDB == nil { - utils.FromContext(c).WithError(nil).Errorf("error openning connection to instance DB") + logrus.WithError(nil).Errorf("error openning connection to instance DB") return errors.New("failed to connect to instance DB") } defer iDB.Close() if err = iDB.Find(&modules, "name = ?", moduleName).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding modules by name") + logrus.WithError(err).Errorf("error finding modules by name") return err } else if len(modules) == 0 { return updateDependenciesWhenModuleRemove(c, iDB, moduleName) } else if err = iDB.Where("name = ?", moduleName).Delete(&modules).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting module by name '%s'", moduleName) + logrus.WithError(err).Errorf("error deleting module by name '%s'", moduleName) return err } s3, err = storage.NewS3(sv.Info.S3.ToS3ConnParams()) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") return err } if err = s3.RemoveDir(moduleName + "/"); err != nil && err.Error() != "not found" { - utils.FromContext(c).WithError(err).Errorf("error removing modules files") + logrus.WithError(err).Errorf("error removing modules files") return err } @@ -3168,7 +3169,7 @@ func (s *ModuleService) DeleteModule(c *gin.Context) { } if err = s.db.Find(&services, "tenant_id = ? AND type = ?", tid, sv.Type).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding services") + logrus.WithError(err).Errorf("error finding services") response.Error(c, response.ErrDeleteModuleServiceNotFound, err) return } @@ -3181,19 +3182,19 @@ func (s *ModuleService) DeleteModule(c *gin.Context) { } if err = s.db.Where("name = ?", moduleName).Delete(&modules).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting system module by name '%s'", moduleName) + logrus.WithError(err).Errorf("error deleting system module by name '%s'", moduleName) response.Error(c, response.ErrInternal, err) return } if s3, err = storage.NewS3(nil); err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") response.Error(c, response.ErrInternal, err) return } if err = s3.RemoveDir(moduleName + "/"); err != nil && err.Error() != "not found" { - utils.FromContext(c).WithError(err).Errorf("error removing system modules files") + logrus.WithError(err).Errorf("error removing system modules files") response.Error(c, response.ErrDeleteModuleDeleteFilesFail, err) return } @@ -3221,7 +3222,7 @@ func (s *ModuleService) GetModuleVersions(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrModulesInvalidRequest, err) return } @@ -3246,7 +3247,7 @@ func (s *ModuleService) GetModuleVersions(c *gin.Context) { }) total, err := query.Query(s.db, &resp.Modules) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system modules") + logrus.WithError(err).Errorf("error finding system modules") response.Error(c, response.ErrGetModuleVersionsInvalidModulesQuery, err) return } @@ -3254,7 +3255,7 @@ func (s *ModuleService) GetModuleVersions(c *gin.Context) { for i := 0; i < len(resp.Modules); i++ { if err = resp.Modules[i].Valid(); err != nil { - utils.FromContext(c).WithError(err). + logrus.WithError(err). Errorf("error validating system module data '%s'", resp.Modules[i].Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return @@ -3300,7 +3301,7 @@ func (s *ModuleService) GetModuleVersion(c *gin.Context) { } if err := s.db.Scopes(FilterModulesByVersion(version), scope).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesNotFound, err) @@ -3309,13 +3310,13 @@ func (s *ModuleService) GetModuleVersion(c *gin.Context) { } return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } if err := module.DecryptSecureParameters(encryptor); err != nil { - utils.FromContext(c).WithError(err).Errorf("error decrypting module data") + logrus.WithError(err).Errorf("error decrypting module data") response.Error(c, response.ErrModulesFailedToDecryptSecureConfig, err) return } @@ -3360,7 +3361,7 @@ func (s *ModuleService) PatchModuleVersion(c *gin.Context) { if nameErr == nil { uaf.ObjectDisplayName = name } - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrModulesInvalidRequest, err) return } @@ -3388,7 +3389,7 @@ func (s *ModuleService) PatchModuleVersion(c *gin.Context) { } if err := s.db.Scopes(FilterModulesByVersion(version), scope).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -3396,13 +3397,13 @@ func (s *ModuleService) PatchModuleVersion(c *gin.Context) { } return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } if module.State == "release" { - utils.FromContext(c).WithError(nil).Errorf("error changing released system module") + logrus.WithError(nil).Errorf("error changing released system module") response.Error(c, response.ErrPatchModuleVersionAcceptReleaseChangesFail, nil) return } @@ -3418,7 +3419,7 @@ func (s *ModuleService) PatchModuleVersion(c *gin.Context) { } for _, ch := range changes { if ch { - utils.FromContext(c).WithError(nil).Errorf("error accepting system module changes") + logrus.WithError(nil).Errorf("error accepting system module changes") response.Error(c, response.ErrPatchModuleVersionAcceptSystemChangesFail, nil) return } @@ -3426,24 +3427,24 @@ func (s *ModuleService) PatchModuleVersion(c *gin.Context) { err := form.Module.EncryptSecureParameters(encryptor) if err != nil { - utils.FromContext(c).WithError(err).Errorf("failed to encrypt module secure config") + logrus.WithError(err).Errorf("failed to encrypt module secure config") response.Error(c, response.ErrModulesFailedToEncryptSecureConfig, err) return } if sqlResult := s.db.Omit("last_update").Save(&form.Module); sqlResult.Error != nil { - utils.FromContext(c).WithError(sqlResult.Error).Errorf("error saving system module") + logrus.WithError(sqlResult.Error).Errorf("error saving system module") response.Error(c, response.ErrPatchModuleVersionUpdateFail, err) return } else if sqlResult.RowsAffected != 0 { if cfiles, err = BuildModuleSConfig(&form.Module); err != nil { - utils.FromContext(c).WithError(err).Errorf("error building system module files") + logrus.WithError(err).Errorf("error building system module files") response.Error(c, response.ErrPatchModuleVersionBuildFilesFail, err) return } template["config"] = cfiles if err = StoreModuleSToGlobalS3(&form.Module.Info, template); err != nil { - utils.FromContext(c).WithError(err).Errorf("error storing system module files to S3") + logrus.WithError(err).Errorf("error storing system module files to S3") response.Error(c, response.ErrPatchModuleVersionUpdateS3Fail, err) return } @@ -3451,13 +3452,13 @@ func (s *ModuleService) PatchModuleVersion(c *gin.Context) { if module.State == "draft" && form.Module.State == "release" { if err = s.db.Find(&services, "tenant_id = ? AND type = ?", tid, sv.Type).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding services") + logrus.WithError(err).Errorf("error finding services") response.Error(c, response.ErrPatchModuleVersionServiceNotFound, err) return } if err = s.db.Model(&form.Module).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by id '%d'", form.Module.ID) + logrus.WithError(err).Errorf("error finding system module by id '%d'", form.Module.ID) response.Error(c, response.ErrInternal, err) return } @@ -3521,7 +3522,7 @@ func (s *ModuleService) CreateModuleVersion(c *gin.Context) { if nameErr == nil { uaf.ObjectDisplayName = name } - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrModulesInvalidRequest, err) return } @@ -3532,7 +3533,7 @@ func (s *ModuleService) CreateModuleVersion(c *gin.Context) { } if err := s.db.Scopes(FilterModulesByVersion("latest"), scope).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -3544,7 +3545,7 @@ func (s *ModuleService) CreateModuleVersion(c *gin.Context) { uaf.ObjectDisplayName = module.Locale.Module["en"].Title if err := module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } @@ -3554,18 +3555,18 @@ func (s *ModuleService) CreateModuleVersion(c *gin.Context) { } if err := s.db.Scopes(scope, draft).Model(&module).Count(&count).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding number of system module drafts") + logrus.WithError(err).Errorf("error finding number of system module drafts") response.Error(c, response.ErrCreateModuleVersionGetDraftNumberFail, err) return } else if count >= 1 { - utils.FromContext(c).WithError(nil).Errorf("error creating system module second draft") + logrus.WithError(nil).Errorf("error creating system module second draft") response.Error(c, response.ErrCreateModuleVersionSecondSystemModuleDraft, err) return } newModuleVersion, err := semver.NewVersion(version) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error parsing new version '%s'", version) + logrus.WithError(err).Errorf("error parsing new version '%s'", version) response.Error(c, response.ErrCreateModuleVersionInvalidModuleVersionFormat, err) return } @@ -3573,14 +3574,14 @@ func (s *ModuleService) CreateModuleVersion(c *gin.Context) { switch utils.CompareVersions(module.Info.Version.String(), version) { case utils.TargetVersionGreat: default: - utils.FromContext(c).WithError(nil).Errorf("error validating new version '%s' -> '%s'", + logrus.WithError(nil).Errorf("error validating new version '%s' -> '%s'", module.Info.Version.String(), version) response.Error(c, response.ErrCreateModuleVersionInvalidModuleVersion, nil) return } if template, err = LoadModuleSFromGlobalS3(&module.Info); err != nil { - utils.FromContext(c).WithError(err).Errorf("error building system module files") + logrus.WithError(err).Errorf("error building system module files") response.Error(c, response.ErrCreateModuleVersionBuildFilesFail, err) return } @@ -3593,32 +3594,32 @@ func (s *ModuleService) CreateModuleVersion(c *gin.Context) { module.Info.Version.Patch = newModuleVersion.Patch() module.Changelog[module.Info.Version.String()] = clver if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating module") + logrus.WithError(err).Errorf("error validating module") response.Error(c, response.ErrCreateModuleVersionValidationFail, err) return } if cfiles, err = BuildModuleSConfig(&module); err != nil { - utils.FromContext(c).WithError(err).Errorf("error building system module files") + logrus.WithError(err).Errorf("error building system module files") response.Error(c, response.ErrCreateModuleVersionBuildFilesFail, err) return } template["config"] = cfiles if err = StoreModuleSToGlobalS3(&module.Info, template); err != nil { - utils.FromContext(c).WithError(err).Errorf("error storing module to S3") + logrus.WithError(err).Errorf("error storing module to S3") response.Error(c, response.ErrCreateModuleVersionStoreS3Fail, err) return } err = module.EncryptSecureParameters(encryptor) if err != nil { - utils.FromContext(c).WithError(err).Errorf("failed to encrypt module secure config") + logrus.WithError(err).Errorf("failed to encrypt module secure config") response.Error(c, response.ErrModulesFailedToEncryptSecureConfig, err) return } if err = s.db.Create(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error creating module") + logrus.WithError(err).Errorf("error creating module") response.Error(c, response.ErrCreateModuleVersionStoreDBFail, err) return } @@ -3660,17 +3661,17 @@ func (s *ModuleService) DeleteModuleVersion(c *gin.Context) { } if err := s.db.Scopes(scope).Model(&module).Count(&count).Error; err != nil || count == 0 { - utils.FromContext(c).WithError(err).Errorf("error finding number of system module versions") + logrus.WithError(err).Errorf("error finding number of system module versions") response.Error(c, response.ErrDeleteModuleVersionGetVersionNumberFail, err) return } else if count == 1 { - utils.FromContext(c).WithError(nil).Errorf("error deleting last system module version") + logrus.WithError(nil).Errorf("error deleting last system module version") response.Error(c, response.ErrDeleteModuleVersionDeleteLastVersionFail, nil) return } if err := s.db.Scopes(FilterModulesByVersion(version), scope).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -3678,28 +3679,28 @@ func (s *ModuleService) DeleteModuleVersion(c *gin.Context) { } return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } uaf.ObjectDisplayName = module.Locale.Module["en"].Title if err := s.db.Delete(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting system module by name '%s'", moduleName) + logrus.WithError(err).Errorf("error deleting system module by name '%s'", moduleName) response.Error(c, response.ErrInternal, err) return } s3, err := storage.NewS3(nil) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") response.Error(c, response.ErrInternal, err) return } path := moduleName + "/" + module.Info.Version.String() + "/" if err = s3.RemoveDir(path); err != nil && err.Error() != "not found" { - utils.FromContext(c).WithError(err).Errorf("error removing system modules files") + logrus.WithError(err).Errorf("error removing system modules files") response.Error(c, response.ErrDeleteModuleVersionDeleteFilesFail, err) return } @@ -3731,13 +3732,13 @@ func (s *ModuleService) GetModuleVersionUpdates(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -3753,7 +3754,7 @@ func (s *ModuleService) GetModuleVersionUpdates(c *gin.Context) { } if err = s.db.Scopes(FilterModulesByVersion(version), scope).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -3761,7 +3762,7 @@ func (s *ModuleService) GetModuleVersionUpdates(c *gin.Context) { } return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } @@ -3771,7 +3772,7 @@ func (s *ModuleService) GetModuleVersionUpdates(c *gin.Context) { module.Info.Name, module.Info.Version.String(), module.LastUpdate) } if err = iDB.Scopes(scope).Find(&resp.Modules).Error; err != nil { - utils.FromContext(c).WithError(err). + logrus.WithError(err). Errorf("error finding policy modules by name and version '%s' '%s'", module.Info.Name, module.Info.Version.String()) response.Error(c, response.ErrInternal, err) @@ -3783,13 +3784,13 @@ func (s *ModuleService) GetModuleVersionUpdates(c *gin.Context) { } if err = iDB.Find(&resp.Policies, "id IN (?)", pids).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policies by IDs") + logrus.WithError(err).Errorf("error finding policies by IDs") response.Error(c, response.ErrInternal, err) return } else { for _, policy := range resp.Policies { if err = policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policy.Hash) response.Error(c, response.ErrGetModuleVersionUpdatesInvalidPolicyData, err) return } @@ -3833,7 +3834,7 @@ func (s *ModuleService) CreateModuleVersionUpdates(c *gin.Context) { } if err := s.db.Scopes(FilterModulesByVersion(version), scope).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -3841,7 +3842,7 @@ func (s *ModuleService) CreateModuleVersionUpdates(c *gin.Context) { } return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } @@ -3886,7 +3887,7 @@ func (s *ModuleService) GetModuleVersionFiles(c *gin.Context) { } if err := s.db.Scopes(FilterModulesByVersion(version), scope).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -3894,21 +3895,21 @@ func (s *ModuleService) GetModuleVersionFiles(c *gin.Context) { } return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } s3, err := storage.NewS3(nil) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") response.Error(c, response.ErrInternal, err) return } path := moduleName + "/" + module.Info.Version.String() if files, err = readDir(s3, path); err != nil { - utils.FromContext(c).WithError(err).Errorf("error listening module files from S3") + logrus.WithError(err).Errorf("error listening module files from S3") response.Error(c, response.ErrGetModuleVersionFilesListenFail, err) return } @@ -3950,7 +3951,7 @@ func (s *ModuleService) GetModuleVersionFile(c *gin.Context) { } if err := s.db.Scopes(FilterModulesByVersion(version), scope).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -3958,27 +3959,27 @@ func (s *ModuleService) GetModuleVersionFile(c *gin.Context) { } return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } s3, err := storage.NewS3(nil) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") response.Error(c, response.ErrInternal, err) return } prefix := moduleName + "/" + module.Info.Version.String() + "/" if !strings.HasPrefix(filePath, prefix) || strings.Contains(filePath, "..") { - utils.FromContext(c).WithError(nil).Errorf("error parsing path to file: mismatch base prefix") + logrus.WithError(nil).Errorf("error parsing path to file: mismatch base prefix") response.Error(c, response.ErrGetModuleVersionFileParsePathFail, nil) return } if fileData, err = s3.ReadFile(filePath); err != nil { - utils.FromContext(c).WithError(err).Errorf("error reading module file '%s'", filePath) + logrus.WithError(err).Errorf("error reading module file '%s'", filePath) response.Error(c, response.ErrGetModuleVersionFileReadFail, err) return } @@ -4026,7 +4027,7 @@ func (s *ModuleService) PatchModuleVersionFile(c *gin.Context) { } if err := s.db.Scopes(FilterModulesByVersion(version), scope).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -4034,34 +4035,34 @@ func (s *ModuleService) PatchModuleVersionFile(c *gin.Context) { } return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } uaf.ObjectDisplayName = module.Locale.Module["en"].Title if module.State == "release" { - utils.FromContext(c).WithError(nil).Errorf("error patching released module") + logrus.WithError(nil).Errorf("error patching released module") response.Error(c, response.ErrPatchModuleVersionFileUpdateFail, nil) return } s3, err := storage.NewS3(nil) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") response.Error(c, response.ErrInternal, err) return } if err = c.ShouldBindJSON(&form); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrModulesInvalidRequest, err) return } prefix := moduleName + "/" + module.Info.Version.String() + "/" if !strings.HasPrefix(form.Path, prefix) || strings.Contains(form.Path, "..") { - utils.FromContext(c).WithError(nil).Errorf("error parsing path to file: mismatch base prefix") + logrus.WithError(nil).Errorf("error parsing path to file: mismatch base prefix") response.Error(c, response.ErrPatchModuleVersionFileParsePathFail, nil) return } @@ -4069,33 +4070,33 @@ func (s *ModuleService) PatchModuleVersionFile(c *gin.Context) { switch form.Action { case "save": if data, err = base64.StdEncoding.DecodeString(form.Data); err != nil { - utils.FromContext(c).WithError(err).Errorf("error decoding file data") + logrus.WithError(err).Errorf("error decoding file data") response.Error(c, response.ErrPatchModuleVersionFileParseModuleFileFail, err) return } if err = s3.WriteFile(form.Path, data); err != nil { - utils.FromContext(c).WithError(err).Errorf("error writing file data to S3") + logrus.WithError(err).Errorf("error writing file data to S3") response.Error(c, response.ErrPatchModuleVersionFileWriteModuleFileFail, err) return } case "remove": if err = s3.Remove(form.Path); err != nil { - utils.FromContext(c).WithError(err).Errorf("error removing file from S3") + logrus.WithError(err).Errorf("error removing file from S3") response.Error(c, response.ErrPatchModuleVersionFileWriteModuleObjectFail, err) return } case "move": if !strings.HasPrefix(form.NewPath, prefix) || strings.Contains(form.NewPath, "..") { - utils.FromContext(c).WithError(nil).Errorf("error parsing path to file: mismatch base prefix") + logrus.WithError(nil).Errorf("error parsing path to file: mismatch base prefix") response.Error(c, response.ErrPatchModuleVersionFileParseNewpathFail, nil) return } if info, err = s3.GetInfo(form.Path); err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting file info from S3") + logrus.WithError(err).Errorf("error getting file info from S3") response.Error(c, response.ErrPatchModuleVersionFileObjectNotFound, err) return } else if !info.IsDir() { @@ -4104,13 +4105,13 @@ func (s *ModuleService) PatchModuleVersionFile(c *gin.Context) { } if form.Path == form.NewPath { - utils.FromContext(c).WithError(nil).Errorf("error moving file in S3: newpath is identical to path") + logrus.WithError(nil).Errorf("error moving file in S3: newpath is identical to path") response.Error(c, response.ErrPatchModuleVersionFilePathIdentical, nil) return } if err = s3.Rename(form.Path, form.NewPath); err != nil { - utils.FromContext(c).WithError(err).Errorf("error renaming file in S3") + logrus.WithError(err).Errorf("error renaming file in S3") response.Error(c, response.ErrPatchModuleVersionFileObjectMoveFail, err) return } @@ -4123,13 +4124,13 @@ func (s *ModuleService) PatchModuleVersionFile(c *gin.Context) { } if form.Path == form.NewPath { - utils.FromContext(c).WithError(nil).Errorf("error moving file in S3: newpath is identical to path") + logrus.WithError(nil).Errorf("error moving file in S3: newpath is identical to path") response.Error(c, response.ErrPatchModuleVersionFilePathIdentical, nil) return } if files, err = s3.ListDirRec(form.Path); err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting files by path from S3") + logrus.WithError(err).Errorf("error getting files by path from S3") response.Error(c, response.ErrPatchModuleVersionFileGetFilesFail, err) return } @@ -4139,7 +4140,7 @@ func (s *ModuleService) PatchModuleVersionFile(c *gin.Context) { curfile := filepath.Join(form.Path, obj) newfile := filepath.Join(form.NewPath, obj) if err = s3.Rename(curfile, newfile); err != nil { - utils.FromContext(c).WithError(err).Errorf("error moving file in S3") + logrus.WithError(err).Errorf("error moving file in S3") response.Error(c, response.ErrPatchModuleVersionFileObjectMoveFail, err) return } @@ -4148,13 +4149,13 @@ func (s *ModuleService) PatchModuleVersionFile(c *gin.Context) { } default: - utils.FromContext(c).WithError(nil).Errorf("error making unknown action on module") + logrus.WithError(nil).Errorf("error making unknown action on module") response.Error(c, response.ErrPatchModuleVersionFileActionNotFound, nil) return } if err = s.db.Model(&module).UpdateColumn("last_update", gorm.Expr("NOW()")).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating system module") + logrus.WithError(err).Errorf("error updating system module") response.Error(c, response.ErrPatchModuleVersionFileSystemModuleUpdateFail, err) return } @@ -4194,7 +4195,7 @@ func (s *ModuleService) GetModuleVersionOption(c *gin.Context) { } if err := s.db.Scopes(FilterModulesByVersion(version), scope).Take(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrModulesSystemModuleNotFound, err) } else { @@ -4202,7 +4203,7 @@ func (s *ModuleService) GetModuleVersionOption(c *gin.Context) { } return } else if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrModulesInvalidSystemModuleData, err) return } @@ -4220,15 +4221,15 @@ func (s *ModuleService) GetModuleVersionOption(c *gin.Context) { options := make(map[string]json.RawMessage) data, err := json.Marshal(module) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error building system module JSON") + logrus.WithError(err).Errorf("error building system module JSON") response.Error(c, response.ErrGetModuleVersionOptionMakeJsonFail, err) return } else if err = json.Unmarshal(data, &options); err != nil { - utils.FromContext(c).WithError(err).Errorf("error parsing system module JSON") + logrus.WithError(err).Errorf("error parsing system module JSON") response.Error(c, response.ErrGetModuleVersionOptionParseJsonFail, err) return } else if _, ok := options[optionName]; !ok { - utils.FromContext(c).WithError(err).Errorf("error finding system module option by name") + logrus.WithError(err).Errorf("error finding system module option by name") response.Error(c, response.ErrGetModuleVersionOptionNotFound, err) return } diff --git a/pkg/app/api/server/private/options.go b/pkg/app/api/server/private/options.go index 73716d5c..a3f000e4 100644 --- a/pkg/app/api/server/private/options.go +++ b/pkg/app/api/server/private/options.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/models" srvcontext "soldr/pkg/app/api/server/context" @@ -154,7 +155,7 @@ func validOptions(c *gin.Context, value interface{}) bool { } for i := 0; i < len(vlist); i++ { if err := vlist[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating response data") + logrus.WithError(err).Errorf("error validating response data") return false } } @@ -171,7 +172,7 @@ func getOption(c *gin.Context, db *gorm.DB, option string, value interface{}) (u ) if err = c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") return 0, response.ErrOptionsInvalidRequestData } @@ -208,7 +209,7 @@ func getOption(c *gin.Context, db *gorm.DB, option string, value interface{}) (u }, } if total, err = query.Query(db, value, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding global %s list", option) + logrus.WithError(err).Errorf("error finding global %s list", option) return 0, response.ErrOptionsInvalidQuery } diff --git a/pkg/app/api/server/private/policies.go b/pkg/app/api/server/private/policies.go index efcd0786..a6a8ccfb 100644 --- a/pkg/app/api/server/private/policies.go +++ b/pkg/app/api/server/private/policies.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/client" "soldr/pkg/app/api/models" @@ -206,20 +207,20 @@ func (s *PolicyService) GetPolicies(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrPoliciesInvalidRequest, err) return } serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -235,13 +236,13 @@ func (s *PolicyService) GetPolicies(c *gin.Context) { } if err = s.db.Scopes(LatestModulesQuery, scope).Find(&modules).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading system modules latest version") + logrus.WithError(err).Errorf("error loading system modules latest version") response.Error(c, response.ErrGetPoliciesSystemModulesNotFound, err) return } if err = query.Init("policies", policiesSQLMappers); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrPoliciesInvalidRequest, err) return } @@ -292,13 +293,13 @@ func (s *PolicyService) GetPolicies(c *gin.Context) { if query.Group == "" { if resp.Total, err = query.Query(iDB, &resp.Policies, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policies") + logrus.WithError(err).Errorf("error finding policies") response.Error(c, response.ErrPoliciesInvalidQuery, err) return } } else { if groupedResp.Total, err = query.QueryGrouped(iDB, &groupedResp.Grouped, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding grouped policies") + logrus.WithError(err).Errorf("error finding grouped policies") response.Error(c, response.ErrPoliciesInvalidQuery, err) return } @@ -309,7 +310,7 @@ func (s *PolicyService) GetPolicies(c *gin.Context) { for i := 0; i < len(resp.Policies); i++ { pids = append(pids, resp.Policies[i].ID) if err = resp.Policies[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", resp.Policies[i].Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", resp.Policies[i].Hash) response.Error(c, response.ErrPoliciesInvalidData, err) return } @@ -317,13 +318,13 @@ func (s *PolicyService) GetPolicies(c *gin.Context) { sqlQuery := sqlPolicyDetails + ` WHERE p.id IN (?) AND p.deleted_at IS NULL` if err = iDB.Raw(sqlQuery, pids).Scan(&resp.Details).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading policies details") + logrus.WithError(err).Errorf("error loading policies details") response.Error(c, response.ErrGetPoliciesDetailsNotFound, err) return } if err = iDB.Find(&modulesa, "policy_id IN (?) AND status = 'joined'", pids).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy modules") + logrus.WithError(err).Errorf("error finding policy modules") response.Error(c, response.ErrGetPoliciesModulesNotFound, err) return } else { @@ -331,7 +332,7 @@ func (s *PolicyService) GetPolicies(c *gin.Context) { id := modulesa[i].ID name := modulesa[i].Info.Name if err = modulesa[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy module data '%d' '%s'", id, name) + logrus.WithError(err).Errorf("error validating policy module data '%d' '%s'", id, name) response.Error(c, response.ErrGetPoliciesInvalidModuleData, err) return } @@ -339,7 +340,7 @@ func (s *PolicyService) GetPolicies(c *gin.Context) { } if err = iDB.Find(&pgss, "policy_id IN (?)", pids).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group to policies links") + logrus.WithError(err).Errorf("error finding group to policies links") response.Error(c, response.ErrGroupPolicyPoliciesNotFound, err) return } @@ -349,7 +350,7 @@ func (s *PolicyService) GetPolicies(c *gin.Context) { Joins(`LEFT JOIN groups_to_policies gtp ON gtp.group_id = groups.id`). Find(&groups, "gtp.policy_id IN (?)", pids).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy groups") + logrus.WithError(err).Errorf("error finding policy groups") response.Error(c, response.ErrGroupPolicyGroupsNotFound, err) return } else { @@ -357,7 +358,7 @@ func (s *PolicyService) GetPolicies(c *gin.Context) { id := groups[i].ID name := groups[i].Info.Name if err = groups[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent group data '%d' '%s'", id, name) + logrus.WithError(err).Errorf("error validating agent group data '%d' '%s'", id, name) response.Error(c, response.ErrGetPoliciesInvalidGroupData, err) return } @@ -426,13 +427,13 @@ func (s *PolicyService) GetPolicy(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -448,13 +449,13 @@ func (s *PolicyService) GetPolicy(c *gin.Context) { } if err = s.db.Scopes(LatestModulesQuery, scope).Find(&modules).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading system modules latest version") + logrus.WithError(err).Errorf("error loading system modules latest version") response.Error(c, response.ErrGetPolicySystemModulesNotFound, err) return } if err = iDB.Take(&resp.Policy, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrPoliciesNotFound, err) } else { @@ -462,20 +463,20 @@ func (s *PolicyService) GetPolicy(c *gin.Context) { } return } else if err = resp.Policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", resp.Policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", resp.Policy.Hash) response.Error(c, response.ErrPoliciesInvalidData, err) return } sqlQuery := sqlPolicyDetails + ` WHERE p.hash = ? AND p.deleted_at IS NULL` if err = iDB.Raw(sqlQuery, hash).Scan(&resp.Details).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading details by policy hash '%s'", hash) + logrus.WithError(err).Errorf("error loading details by policy hash '%s'", hash) response.Error(c, response.ErrGetPolicyDetailsNotFound, err) return } if err = iDB.Find(&resp.Details.Modules, "policy_id = ? AND status = 'joined'", resp.Policy.ID).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy modules by policy ID '%d'", resp.Policy.ID) + logrus.WithError(err).Errorf("error finding policy modules by policy ID '%d'", resp.Policy.ID) response.Error(c, response.ErrGetPolicyModulesNotFound, err) return } else { @@ -483,7 +484,7 @@ func (s *PolicyService) GetPolicy(c *gin.Context) { id := resp.Details.Modules[i].ID name := resp.Details.Modules[i].Info.Name if err = resp.Details.Modules[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy module data '%d' '%s'", id, name) + logrus.WithError(err).Errorf("error validating policy module data '%d' '%s'", id, name) response.Error(c, response.ErrGetPolicyInvalidModuleData, err) return } @@ -501,7 +502,7 @@ func (s *PolicyService) GetPolicy(c *gin.Context) { Policy: resp.Policy, } if err = iDB.Model(pgs).Association("groups").Find(&pgs.Groups).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy groups by policy model") + logrus.WithError(err).Errorf("error finding policy groups by policy model") response.Error(c, response.ErrGetPolicyGroupsNotFound, err) return } @@ -540,13 +541,13 @@ func (s *PolicyService) PatchPolicy(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -559,20 +560,20 @@ func (s *PolicyService) PatchPolicy(c *gin.Context) { if nameErr == nil { uaf.ObjectDisplayName = name } - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrPoliciesInvalidRequest, err) return } uaf.ObjectDisplayName = policy.Info.Name.En if hash != policy.Hash { - utils.FromContext(c).WithError(nil).Errorf("mismatch policy hash to requested one") + logrus.WithError(nil).Errorf("mismatch policy hash to requested one") response.Error(c, response.ErrPoliciesInvalidRequest, err) return } if err = iDB.Model(&policy).Count(&count).Error; err != nil || count == 0 { - utils.FromContext(c).WithError(nil).Errorf("error updating policy by hash '%s', group not found", hash) + logrus.WithError(nil).Errorf("error updating policy by hash '%s', group not found", hash) response.Error(c, response.ErrPoliciesNotFound, err) return } @@ -581,11 +582,11 @@ func (s *PolicyService) PatchPolicy(c *gin.Context) { err = iDB.Select("", public_info...).Save(&policy).Error if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - utils.FromContext(c).WithError(nil).Errorf("error updating policy by hash '%s', policy not found", hash) + logrus.WithError(nil).Errorf("error updating policy by hash '%s', policy not found", hash) response.Error(c, response.ErrPoliciesNotFound, err) return } else if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating policy by hash '%s'", hash) + logrus.WithError(err).Errorf("error updating policy by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } @@ -624,13 +625,13 @@ func (s *PolicyService) PatchPolicyGroup(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -640,7 +641,7 @@ func (s *PolicyService) PatchPolicyGroup(c *gin.Context) { if nameErr == nil { uaf.ObjectDisplayName = name } - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrPoliciesInvalidRequest, err) return } @@ -651,7 +652,7 @@ func (s *PolicyService) PatchPolicyGroup(c *gin.Context) { } if err = iDB.Take(&policy, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrPoliciesNotFound, err) } else { @@ -661,13 +662,13 @@ func (s *PolicyService) PatchPolicyGroup(c *gin.Context) { } uaf.ObjectDisplayName = policy.Info.Name.En if err = policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policy.Hash) response.Error(c, response.ErrPoliciesInvalidData, err) return } if err = iDB.Take(&group, "hash = ?", form.Group.Hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group by hash") + logrus.WithError(err).Errorf("error finding group by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrPoliciesNotFound, err) } else { @@ -675,14 +676,14 @@ func (s *PolicyService) PatchPolicyGroup(c *gin.Context) { } return } else if err = group.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group data '%s'", group.Hash) + logrus.WithError(err).Errorf("error validating group data '%s'", group.Hash) response.Error(c, response.ErrPatchPolicyGroupInvalidGroupData, err) return } httpErr, err := makeGroupPolicyAction(form.Action, iDB, group, policy) if httpErr != nil { - utils.FromContext(c).WithError(err).Errorf("error patching group policy by action: %s", httpErr.Error()) + logrus.WithError(err).Errorf("error patching group policy by action: %s", httpErr.Error()) response.Error(c, httpErr, err) } response.Success(c, http.StatusOK, struct{}{}) @@ -713,7 +714,7 @@ func (s *PolicyService) CreatePolicy(c *gin.Context) { defer s.userActionWriter.WriteUserAction(uaf) if err := c.ShouldBindJSON(&info); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrPoliciesInvalidRequest, err) return } @@ -721,13 +722,13 @@ func (s *PolicyService) CreatePolicy(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -747,11 +748,11 @@ func (s *PolicyService) CreatePolicy(c *gin.Context) { if info.From != 0 { if err = iDB.Take(&policyFrom, "id = ?", info.From).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding source policy by ID") + logrus.WithError(err).Errorf("error finding source policy by ID") response.Error(c, response.ErrCreatePolicySourceNotFound, err) return } else if err = policyFrom.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policyFrom.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policyFrom.Hash) response.Error(c, response.ErrPoliciesInvalidData, err) return } @@ -776,7 +777,7 @@ func (s *PolicyService) CreatePolicy(c *gin.Context) { } if err = iDB.Create(&policy).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error creating policy") + logrus.WithError(err).Errorf("error creating policy") response.Error(c, response.ErrCreatePolicyCreateFail, err) return } @@ -785,7 +786,7 @@ func (s *PolicyService) CreatePolicy(c *gin.Context) { var modules []models.ModuleA err = iDB.Where("policy_id = ? AND status = 'joined'", policyFrom.ID).Find(&modules).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy modules by policy ID '%d'", policyFrom.ID) + logrus.WithError(err).Errorf("error finding policy modules by policy ID '%d'", policyFrom.ID) response.Error(c, response.ErrCreatePolicyModulesNotFound, err) return } @@ -793,7 +794,7 @@ func (s *PolicyService) CreatePolicy(c *gin.Context) { module.ID = 0 module.PolicyID = policy.ID if err = iDB.Create(&module).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error creating policy module") + logrus.WithError(err).Errorf("error creating policy module") response.Error(c, response.ErrCreatePolicyCreateModulesFail, err) return } @@ -831,13 +832,13 @@ func (s *PolicyService) DeletePolicy(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -848,7 +849,7 @@ func (s *PolicyService) DeletePolicy(c *gin.Context) { } if err = iDB.Take(&policy, "hash = ?", hash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy by hash") + logrus.WithError(err).Errorf("error finding policy by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrPoliciesNotFound, err) } else { @@ -856,14 +857,14 @@ func (s *PolicyService) DeletePolicy(c *gin.Context) { } return } else if err = policy.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating policy data '%s'", policy.Hash) + logrus.WithError(err).Errorf("error validating policy data '%s'", policy.Hash) response.Error(c, response.ErrPoliciesInvalidData, err) return } uaf.ObjectDisplayName = policy.Info.Name.En if policy.Info.System { - utils.FromContext(c).WithError(nil).Errorf("error removing system policy") + logrus.WithError(nil).Errorf("error removing system policy") response.Error(c, response.ErrDeletePolicySystemPolicy, err) return } @@ -872,24 +873,24 @@ func (s *PolicyService) DeletePolicy(c *gin.Context) { Policy: policy, } if err = iDB.Model(pgs).Association("groups").Find(&pgs.Groups).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy groups by policy model") + logrus.WithError(err).Errorf("error finding policy groups by policy model") response.Error(c, response.ErrDeletePolicyGroupsNotFound, err) return } if len(pgs.Groups) != 0 { - utils.FromContext(c).WithError(nil).Errorf("error removing policy which linked to groups") + logrus.WithError(nil).Errorf("error removing policy which linked to groups") response.Error(c, response.ErrDeletePolicyPolicyLinkedToGroups, err) return } if err = iDB.Find(&modules, "policy_id = ?", policy.ID).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding policy modules by policy ID '%d'", policy.ID) + logrus.WithError(err).Errorf("error finding policy modules by policy ID '%d'", policy.ID) response.Error(c, response.ErrDeletePolicyModulesNotFound, err) return } if err = iDB.Delete(&policy).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting policy by hash '%s'", hash) + logrus.WithError(err).Errorf("error deleting policy by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } @@ -898,7 +899,7 @@ func (s *PolicyService) DeletePolicy(c *gin.Context) { moduleName := module.Info.Name moduleVersion := module.Info.Version.String() if err = removeUnusedModuleVersion(c, iDB, moduleName, moduleVersion, sv); err != nil { - utils.FromContext(c).WithError(err).Errorf("error removing unused module data") + logrus.WithError(err).Errorf("error removing unused module data") response.Error(c, response.ErrInternal, err) return } @@ -925,13 +926,13 @@ func (s *PolicyService) GetPoliciesCount(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -949,7 +950,7 @@ func (s *PolicyService) GetPoliciesCount(c *gin.Context) { Scan(&resp). Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("could not count policies") + logrus.WithError(err).Errorf("could not count policies") response.Error(c, response.ErrInternal, err) return } diff --git a/pkg/app/api/server/private/porting.go b/pkg/app/api/server/private/porting.go index 23e0a20f..8fb409a2 100644 --- a/pkg/app/api/server/private/porting.go +++ b/pkg/app/api/server/private/porting.go @@ -14,6 +14,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/models" srvcontext "soldr/pkg/app/api/server/context" @@ -144,18 +145,18 @@ func (s *PortingService) ExportModule(c *gin.Context) { } if err = s.db.Scopes(FilterModulesByVersion(version), scope).Find(&modules).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") response.Error(c, response.ErrInternal, err) return } else { if len(modules) == 0 { - utils.FromContext(c).WithError(nil).Errorf("system module by name and version not found: %s : %s", moduleName, version) + logrus.WithError(nil).Errorf("system module by name and version not found: %s : %s", moduleName, version) response.Error(c, response.ErrPortingModuleNotFound, nil) return } for _, module := range modules { if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) + logrus.WithError(err).Errorf("error validating system module data '%s'", module.Info.Name) response.Error(c, response.ErrExportInvalidModuleData, err) return } @@ -171,13 +172,13 @@ func (s *PortingService) ExportModule(c *gin.Context) { prefix := moduleName + "/" + module.Info.Version.String() template, err := LoadModuleSFromGlobalS3(&module.Info) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading system module files from S3") + logrus.WithError(err).Errorf("error loading system module files from S3") response.Error(c, response.ErrExportLoadFilesFail, err) return } config, err := BuildModuleSConfig(&module) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error building system module config") + logrus.WithError(err).Errorf("error building system module config") response.Error(c, response.ErrExportBuildConfigFail, err) return } @@ -187,13 +188,13 @@ func (s *PortingService) ExportModule(c *gin.Context) { for fileName, fileContent := range folderContent { zipFile, err := zipWriter.Create(prefix + "/" + folderName + "/" + fileName) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error adding new system module file to zip") + logrus.WithError(err).Errorf("error adding new system module file to zip") response.Error(c, response.ErrExportAddFileFail, err) return } if _, err = zipFile.Write(fileContent); err != nil { - utils.FromContext(c).WithError(err).Errorf("error writing system module file to zip") + logrus.WithError(err).Errorf("error writing system module file to zip") response.Error(c, response.ErrExportWriteFileFail, err) return } @@ -201,7 +202,7 @@ func (s *PortingService) ExportModule(c *gin.Context) { } } if err = zipWriter.Close(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error closing system module archive") + logrus.WithError(err).Errorf("error closing system module archive") response.Error(c, response.ErrExportCloseArchiveFail, err) return } @@ -260,7 +261,7 @@ func (s *PortingService) ImportModule(c *gin.Context) { } if err = s.db.Scopes(FilterModulesByVersion("all"), scope).Find(&modules).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding system module by name") + logrus.WithError(err).Errorf("error finding system module by name") response.Error(c, response.ErrInternal, err) return } @@ -275,26 +276,26 @@ func (s *PortingService) ImportModule(c *gin.Context) { zipArchive, err := c.FormFile("archive") if err != nil { - utils.FromContext(c).WithError(err).Errorf("error reading system module zip file") + logrus.WithError(err).Errorf("error reading system module zip file") response.Error(c, response.ErrImportReadArchiveFail, err) return } templates, err := getModuleTemplates(zipArchive, moduleName, version) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error parsing system module zip file") + logrus.WithError(err).Errorf("error parsing system module zip file") response.Error(c, response.ErrImportParseArchiveFail, err) return } if len(templates) == 0 { - utils.FromContext(c).WithError(nil).Errorf("system module by name and version not found: %s : %s", moduleName, version) + logrus.WithError(nil).Errorf("system module by name and version not found: %s : %s", moduleName, version) response.Error(c, response.ErrPortingModuleNotFound, nil) return } for _, template := range templates { module, err := LoadModuleSConfig(template["config"]) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error parsing system module config from zip file") + logrus.WithError(err).Errorf("error parsing system module config from zip file") response.Error(c, response.ErrImportParseConfigFail, err) return } @@ -303,19 +304,19 @@ func (s *PortingService) ImportModule(c *gin.Context) { module.TenantID = tid module.ServiceType = sv.Type if err = json.Unmarshal(template["config"]["info.json"], &module.Info); err != nil { - utils.FromContext(c).WithError(err).Errorf("error parsing system module file info") + logrus.WithError(err).Errorf("error parsing system module file info") response.Error(c, response.ErrImportParseFileFail, err) return } if err = module.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating system module data") + logrus.WithError(err).Errorf("error validating system module data") response.Error(c, response.ErrImportValidateConfigFail, err) return } svModule := getModule(module.Info.Version) if svModule != nil && !rewrite { - utils.FromContext(c).WithError(nil).Errorf("error overriding system module version: %s", module.Info.Version.String()) + logrus.WithError(nil).Errorf("error overriding system module version: %s", module.Info.Version.String()) response.Error(c, response.ErrImportOverrideNotPermitted, err) return } @@ -327,7 +328,7 @@ func (s *PortingService) ImportModule(c *gin.Context) { module := nmodules[idx] svModule := getModule(module.Info.Version) if err = StoreCleanModuleSToGlobalS3(&module.Info, template); err != nil { - utils.FromContext(c).WithError(err).Errorf("error storing system module to S3") + logrus.WithError(err).Errorf("error storing system module to S3") response.Error(c, response.ErrImportStoreS3Fail, err) return } @@ -338,7 +339,7 @@ func (s *PortingService) ImportModule(c *gin.Context) { err = s.db.Create(module).Error } if err != nil { - utils.FromContext(c).WithError(err).Errorf("error storing system module to DB") + logrus.WithError(err).Errorf("error storing system module to DB") response.Error(c, response.ErrImportStoreDBFail, err) return } diff --git a/pkg/app/api/server/private/proto.go b/pkg/app/api/server/private/proto.go index 3b9f6dbb..7e195250 100644 --- a/pkg/app/api/server/private/proto.go +++ b/pkg/app/api/server/private/proto.go @@ -7,6 +7,7 @@ import ( "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/models" srvcontext "soldr/pkg/app/api/server/context" @@ -99,23 +100,23 @@ func ValidateToken(tokenString string) (*models.ProtoAuthTokenClaims, error) { func CreateAuthToken(c *gin.Context) { var req models.ProtoAuthTokenRequest if err := c.ShouldBindJSON(&req); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrProtoInvalidRequest, err) return } else if err := req.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating JSON") + logrus.WithError(err).Errorf("error validating JSON") response.Error(c, response.ErrProtoInvalidRequest, err) return } token, err := MakeToken(c, &req) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error on making token") + logrus.WithError(err).Errorf("error on making token") response.Error(c, response.ErrProtoCreateTokenFail, err) return } if _, err := ValidateToken(token); err != nil { - utils.FromContext(c).WithError(err).Errorf("error on validating token") + logrus.WithError(err).Errorf("error on validating token") response.Error(c, response.ErrProtoInvalidToken, err) return } diff --git a/pkg/app/api/server/private/roles.go b/pkg/app/api/server/private/roles.go index 5c5b520d..a93a97a5 100644 --- a/pkg/app/api/server/private/roles.go +++ b/pkg/app/api/server/private/roles.go @@ -5,6 +5,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/models" "soldr/pkg/app/api/server/response" @@ -50,7 +51,7 @@ func (s *RoleService) GetRoles(c *gin.Context) { ) if err = c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrRolesInvalidRequest, err) return } @@ -58,14 +59,14 @@ func (s *RoleService) GetRoles(c *gin.Context) { query.Init("roles", rolesSQLMappers) if resp.Total, err = query.Query(s.db, &resp.Roles); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding roles") + logrus.WithError(err).Errorf("error finding roles") response.Error(c, response.ErrInternal, err) return } for i := 0; i < len(resp.Roles); i++ { if err = resp.Roles[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating role data '%d'", resp.Roles[i].ID) + logrus.WithError(err).Errorf("error validating role data '%d'", resp.Roles[i].ID) response.Error(c, response.ErrRolesInvalidData, err) return } diff --git a/pkg/app/api/server/private/services.go b/pkg/app/api/server/private/services.go index f8df9371..6d1d3cad 100644 --- a/pkg/app/api/server/private/services.go +++ b/pkg/app/api/server/private/services.go @@ -6,6 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/models" srvcontext "soldr/pkg/app/api/server/context" @@ -68,7 +69,7 @@ func (s *ServicesService) GetServices(c *gin.Context) { ) if err = c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrServicesInvalidRequest, err) return } @@ -87,20 +88,20 @@ func (s *ServicesService) GetServices(c *gin.Context) { }, }) default: - utils.FromContext(c).WithError(nil).Errorf("error filtering user role services: unexpected role") + logrus.WithError(nil).Errorf("error filtering user role services: unexpected role") response.Error(c, response.ErrInternal, nil) return } if resp.Total, err = query.Query(s.db, &resp.Services); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding services") + logrus.WithError(err).Errorf("error finding services") response.Error(c, response.ErrInternal, err) return } for i := 0; i < len(resp.Services); i++ { if err = resp.Services[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating service data '%s'", resp.Services[i].Hash) + logrus.WithError(err).Errorf("error validating service data '%s'", resp.Services[i].Hash) response.Error(c, response.ErrServicesInvalidData, err) return } @@ -141,7 +142,7 @@ func (s *ServicesService) GetService(c *gin.Context) { } if err = s.db.Scopes(scope).Take(&resp).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding service by hash") + logrus.WithError(err).Errorf("error finding service by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrServicesNotFound, err) } else { @@ -149,7 +150,7 @@ func (s *ServicesService) GetService(c *gin.Context) { } return } else if err = resp.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating service data '%s'", resp.Hash) + logrus.WithError(err).Errorf("error validating service data '%s'", resp.Hash) response.Error(c, response.ErrServicesInvalidData, err) return } @@ -175,7 +176,7 @@ func (s *ServicesService) CreateService(c *gin.Context) { ) if err = c.ShouldBindJSON(&service); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrServicesInvalidRequest, err) return } @@ -188,7 +189,7 @@ func (s *ServicesService) CreateService(c *gin.Context) { case models.RoleUser, models.RoleAdmin, models.RoleExternal: service.TenantID = tid default: - utils.FromContext(c).WithError(nil).Errorf("error filtering user role services: unexpected role") + logrus.WithError(nil).Errorf("error filtering user role services: unexpected role") response.Error(c, response.ErrInternal, nil) return } @@ -196,7 +197,7 @@ func (s *ServicesService) CreateService(c *gin.Context) { service.Hash = utils.MakeServiceHash(service.Name) if err = s.db.Create(&service).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error creating service") + logrus.WithError(err).Errorf("error creating service") response.Error(c, response.ErrInternal, err) return } @@ -224,15 +225,15 @@ func (s *ServicesService) PatchService(c *gin.Context) { ) if err = c.ShouldBindJSON(&service); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrServicesInvalidRequest, err) return } else if hash != service.Hash { - utils.FromContext(c).WithError(nil).Errorf("mismatch service hash to requested one") + logrus.WithError(nil).Errorf("mismatch service hash to requested one") response.Error(c, response.ErrServicesInvalidRequest, nil) return } else if err = service.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating service JSON") + logrus.WithError(err).Errorf("error validating service JSON") response.Error(c, response.ErrServicesInvalidRequest, err) return } @@ -240,7 +241,7 @@ func (s *ServicesService) PatchService(c *gin.Context) { rid, _ := srvcontext.GetUint64(c, "rid") tid, _ := srvcontext.GetUint64(c, "tid") if rid == models.RoleExternal { - utils.FromContext(c).WithError(nil).Errorf("error: no rights to patch service") + logrus.WithError(nil).Errorf("error: no rights to patch service") response.Error(c, response.ErrNotPermitted, nil) return } @@ -259,11 +260,11 @@ func (s *ServicesService) PatchService(c *gin.Context) { public_info := []interface{}{"info", "name", "status"} err = s.db.Scopes(scope).Select("", public_info...).Save(&service).Error if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - utils.FromContext(c).WithError(nil).Errorf("error updating service by hash '%s', service not found", hash) + logrus.WithError(nil).Errorf("error updating service by hash '%s', service not found", hash) response.Error(c, response.ErrServicesNotFound, err) return } else if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating service by hash '%s'", hash) + logrus.WithError(err).Errorf("error updating service by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } @@ -291,7 +292,7 @@ func (s *ServicesService) DeleteService(c *gin.Context) { rid, _ := srvcontext.GetUint64(c, "rid") tid, _ := srvcontext.GetUint64(c, "tid") if rid == models.RoleExternal { - utils.FromContext(c).WithError(nil).Errorf("error: no rights to delete service") + logrus.WithError(nil).Errorf("error: no rights to delete service") response.Error(c, response.ErrNotPermitted, nil) return } @@ -308,7 +309,7 @@ func (s *ServicesService) DeleteService(c *gin.Context) { } if err = s.db.Scopes(scope).Take(&service).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding service by hash") + logrus.WithError(err).Errorf("error finding service by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrServicesNotFound, err) } else { @@ -316,13 +317,13 @@ func (s *ServicesService) DeleteService(c *gin.Context) { } return } else if err = service.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating service data '%s'", service.Hash) + logrus.WithError(err).Errorf("error validating service data '%s'", service.Hash) response.Error(c, response.ErrServicesInvalidData, err) return } if err = s.db.Delete(&service).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting service by hash '%s'", hash) + logrus.WithError(err).Errorf("error deleting service by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } diff --git a/pkg/app/api/server/private/tags.go b/pkg/app/api/server/private/tags.go index 5b94fc92..735e3a16 100644 --- a/pkg/app/api/server/private/tags.go +++ b/pkg/app/api/server/private/tags.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/client" srvcontext "soldr/pkg/app/api/server/context" @@ -175,27 +176,27 @@ func (s *TagService) GetTags(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrTagsInvalidRequest, err) return } serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } table, sqlMappers, err = getTagMappers(&query) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting tag mappers by query") + logrus.WithError(err).Errorf("error getting tag mappers by query") response.Error(c, response.ErrTagsMappersNotFound, err) return } @@ -237,7 +238,7 @@ func (s *TagService) GetTags(c *gin.Context) { funcs := getTagFilters(&query) funcs = append(funcs, getTagJoinFuncs(table, useGroup, useModule, usePolicy)...) if resp.Total, err = query.Query(iDB, &resp.Tags, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding tags") + logrus.WithError(err).Errorf("error finding tags") response.Error(c, response.ErrTagsInvalidQuery, err) return } diff --git a/pkg/app/api/server/private/tenants.go b/pkg/app/api/server/private/tenants.go index 0f7e2662..800cdeab 100644 --- a/pkg/app/api/server/private/tenants.go +++ b/pkg/app/api/server/private/tenants.go @@ -6,6 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/models" srvcontext "soldr/pkg/app/api/server/context" @@ -54,7 +55,7 @@ func (s *TenantService) GetTenants(c *gin.Context) { ) if err = c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrTenantsInvalidRequest, err) return } @@ -75,20 +76,20 @@ func (s *TenantService) GetTenants(c *gin.Context) { }, }) default: - utils.FromContext(c).WithError(nil).Errorf("error filtering user role services: unexpected role") + logrus.WithError(nil).Errorf("error filtering user role services: unexpected role") response.Error(c, response.ErrInternal, nil) return } if resp.Total, err = query.Query(s.db, &resp.Tenants); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding tenants") + logrus.WithError(err).Errorf("error finding tenants") response.Error(c, response.ErrInternal, err) return } for i := 0; i < len(resp.Tenants); i++ { if err = resp.Tenants[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating tenant data '%s'", resp.Tenants[i].Hash) + logrus.WithError(err).Errorf("error validating tenant data '%s'", resp.Tenants[i].Hash) response.Error(c, response.ErrTenantsInvalidData, err) return } @@ -131,7 +132,7 @@ func (s *TenantService) GetTenant(c *gin.Context) { } if err = s.db.Scopes(scope).Take(&resp).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding tenant by hash") + logrus.WithError(err).Errorf("error finding tenant by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrTenantsNotFound, err) } else { @@ -139,7 +140,7 @@ func (s *TenantService) GetTenant(c *gin.Context) { } return } else if err = resp.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating tenant data '%s'", resp.Hash) + logrus.WithError(err).Errorf("error validating tenant data '%s'", resp.Hash) response.Error(c, response.ErrTenantsInvalidData, err) return } @@ -165,7 +166,7 @@ func (s *TenantService) CreateTenant(c *gin.Context) { ) if err = c.ShouldBindJSON(&tenant); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrTenantsInvalidRequest, err) return } @@ -174,7 +175,7 @@ func (s *TenantService) CreateTenant(c *gin.Context) { tenant.Hash = utils.MakeTenantHash(tenant.Description) if err = s.db.Create(&tenant).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error creating tenant") + logrus.WithError(err).Errorf("error creating tenant") response.Error(c, response.ErrInternal, err) return } @@ -202,15 +203,15 @@ func (s *TenantService) PatchTenant(c *gin.Context) { ) if err = c.ShouldBindJSON(&tenant); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrTenantsInvalidRequest, err) return } else if hash != tenant.Hash { - utils.FromContext(c).WithError(nil).Errorf("mismatch tenant hash to requested one") + logrus.WithError(nil).Errorf("mismatch tenant hash to requested one") response.Error(c, response.ErrTenantsInvalidRequest, nil) return } else if err = tenant.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating tenant JSON") + logrus.WithError(err).Errorf("error validating tenant JSON") response.Error(c, response.ErrTenantsInvalidRequest, err) return } @@ -234,11 +235,11 @@ func (s *TenantService) PatchTenant(c *gin.Context) { public_info := []interface{}{"description", "status"} err = s.db.Scopes(scope).Select("", public_info...).Save(&tenant).Error if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - utils.FromContext(c).WithError(nil).Errorf("error updating tenant by hash '%s', tenant not found", hash) + logrus.WithError(nil).Errorf("error updating tenant by hash '%s', tenant not found", hash) response.Error(c, response.ErrTenantsNotFound, err) return } else if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating tenant by hash '%s'", hash) + logrus.WithError(err).Errorf("error updating tenant by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } @@ -280,7 +281,7 @@ func (s *TenantService) DeleteTenant(c *gin.Context) { } if err = s.db.Scopes(scope).Take(&tenant).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding tenant by hash") + logrus.WithError(err).Errorf("error finding tenant by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrTenantsNotFound, err) } else { @@ -288,13 +289,13 @@ func (s *TenantService) DeleteTenant(c *gin.Context) { } return } else if err = tenant.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating tenant data '%s'", tenant.Hash) + logrus.WithError(err).Errorf("error validating tenant data '%s'", tenant.Hash) response.Error(c, response.ErrTenantsInvalidData, err) return } if err = s.db.Delete(&tenant).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting tenant by hash '%s'", hash) + logrus.WithError(err).Errorf("error deleting tenant by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } diff --git a/pkg/app/api/server/private/upgrades.go b/pkg/app/api/server/private/upgrades.go index d602488b..98d79e8e 100644 --- a/pkg/app/api/server/private/upgrades.go +++ b/pkg/app/api/server/private/upgrades.go @@ -10,6 +10,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/client" "soldr/pkg/app/api/models" @@ -88,20 +89,20 @@ func (s *UpgradeService) GetAgentsUpgrades(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrGetAgentsUpgradesInvalidRequest, err) return } serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -109,14 +110,14 @@ func (s *UpgradeService) GetAgentsUpgrades(c *gin.Context) { query.Init("upgrade_tasks", upgradesAgentsSQLMappers) if resp.Total, err = query.Query(iDB, &resp.Tasks); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agents upgrades") + logrus.WithError(err).Errorf("error finding agents upgrades") response.Error(c, response.ErrInternal, err) return } for i := 0; i < len(resp.Tasks); i++ { if err = resp.Tasks[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agents upgrades data '%d'", resp.Tasks[i].ID) + logrus.WithError(err).Errorf("error validating agents upgrades data '%d'", resp.Tasks[i].ID) response.Error(c, response.ErrGetAgentsUpgradesInvalidData, err) return } @@ -157,20 +158,20 @@ func (s *UpgradeService) CreateAgentsUpgrades(c *gin.Context) { }() if err := c.ShouldBindJSON(&upgradeReq); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrCreateAgentsUpgradesInvalidRequest, err) return } serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -183,7 +184,7 @@ func (s *UpgradeService) CreateAgentsUpgrades(c *gin.Context) { tid, _ := srvcontext.GetUint64(c, "tid") batchRaw := make([]byte, 32) if _, err := rand.Read(batchRaw); err != nil { - utils.FromContext(c).WithError(err).Errorf("failed to get random batch id") + logrus.WithError(err).Errorf("failed to get random batch id") response.Error(c, response.ErrInternal, err) return } @@ -200,11 +201,11 @@ func (s *UpgradeService) CreateAgentsUpgrades(c *gin.Context) { } err = s.db.Scopes(scope).Model(&binary).Take(&binary).Error if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - utils.FromContext(c).WithError(nil).Errorf("error getting binary info by version '%s', record not found", upgradeReq.Version) + logrus.WithError(nil).Errorf("error getting binary info by version '%s', record not found", upgradeReq.Version) response.Error(c, response.ErrCreateAgentsUpgradesAgentNotFound, err) return } else if err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting binary info by version '%s'", upgradeReq.Version) + logrus.WithError(err).Errorf("error getting binary info by version '%s'", upgradeReq.Version) response.Error(c, response.ErrInternal, err) return } @@ -220,7 +221,7 @@ func (s *UpgradeService) CreateAgentsUpgrades(c *gin.Context) { Where("status IN (?)", []string{"new", "running"}). SubQuery()).Find(&agents).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error collecting agents by filter") + logrus.WithError(err).Errorf("error collecting agents by filter") response.Error(c, response.ErrPatchAgentsInvalidQuery, err) } @@ -238,7 +239,7 @@ func (s *UpgradeService) CreateAgentsUpgrades(c *gin.Context) { QueryExpr()) if err = sqlInsertResult.Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error inserting new upgrade tasks into the table") + logrus.WithError(err).Errorf("error inserting new upgrade tasks into the table") response.Error(c, response.ErrCreateAgentsUpgradesCreateTaskFail, err) return } @@ -246,13 +247,13 @@ func (s *UpgradeService) CreateAgentsUpgrades(c *gin.Context) { if sqlInsertResult.RowsAffected != 0 { s3, err := storage.NewS3(sv.Info.S3.ToS3ConnParams()) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") response.Error(c, response.ErrInternal, err) return } if err = utils.UploadAgentBinariesToInstBucket(binary, s3); err != nil { - utils.FromContext(c).WithError(err).Errorf("error uploading agent binaries to S3 instance bucket") + logrus.WithError(err).Errorf("error uploading agent binaries to S3 instance bucket") response.Error(c, response.ErrCreateAgentsUpgradesUpdateAgentBinariesFail, err) return } @@ -281,13 +282,13 @@ func (s *UpgradeService) GetLastAgentUpgrade(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -301,21 +302,21 @@ func (s *UpgradeService) GetLastAgentUpgrade(c *gin.Context) { Take(&resp.Task, "`agents`.hash = ? AND NOT (?) AND (? OR ?)", hash, versionExpr, statusExpr, timeExpr) if err = sqlQueryResult.Error; err != nil || sqlQueryResult.RowsAffected == 0 { - utils.FromContext(c).WithError(err).Errorf("error finding last agent upgrade information") + logrus.WithError(err).Errorf("error finding last agent upgrade information") response.Error(c, response.ErrGetLastAgentUpgradeLastUpgradeNotFound, err) return } else if err = resp.Task.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating last agent upgrade data '%d'", resp.Task.ID) + logrus.WithError(err).Errorf("error validating last agent upgrade data '%d'", resp.Task.ID) response.Error(c, response.ErrGetLastAgentUpgradeInvalidLastData, err) return } if err = iDB.Take(&resp.Details.Agent, "id = ?", resp.Task.AgentID).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent") + logrus.WithError(err).Errorf("error finding agent") response.Error(c, response.ErrGetLastAgentUpgradeAgentNotFound, err) return } else if err = resp.Details.Agent.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent data '%s'", resp.Details.Agent.Hash) + logrus.WithError(err).Errorf("error validating agent data '%s'", resp.Details.Agent.Hash) response.Error(c, response.ErrGetLastAgentUpgradeInvalidAgentData, err) return } @@ -323,11 +324,11 @@ func (s *UpgradeService) GetLastAgentUpgrade(c *gin.Context) { if resp.Details.Agent.GroupID != 0 { resp.Details.Group = &models.Group{} if err = iDB.Take(resp.Details.Group, "id = ?", resp.Details.Agent.GroupID).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding group") + logrus.WithError(err).Errorf("error finding group") response.Error(c, response.ErrGetLastAgentUpgradeGroupNotFound, err) return } else if err = resp.Details.Group.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating group data '%s'", resp.Details.Group.Hash) + logrus.WithError(err).Errorf("error validating group data '%s'", resp.Details.Group.Hash) response.Error(c, response.ErrGetLastAgentUpgradeInvalidGroupData, err) return } @@ -363,13 +364,13 @@ func (s *UpgradeService) PatchLastAgentUpgrade(c *gin.Context) { serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -382,7 +383,7 @@ func (s *UpgradeService) PatchLastAgentUpgrade(c *gin.Context) { if nameErr == nil { uaf.ObjectDisplayName = name } - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrPatchLastAgentUpgradeInvalidAgentUpgradeInfo, err) return } @@ -393,15 +394,15 @@ func (s *UpgradeService) PatchLastAgentUpgrade(c *gin.Context) { } if err = iDB.Take(&agent, "id = ?", task.AgentID).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding agent") + logrus.WithError(err).Errorf("error finding agent") response.Error(c, response.ErrPatchLastAgentUpgradeAgentNotFound, err) return } else if err = agent.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating agent data '%s'", agent.Hash) + logrus.WithError(err).Errorf("error validating agent data '%s'", agent.Hash) response.Error(c, response.ErrPatchLastAgentUpgradeInvalidAgentData, err) return } else if hash != agent.Hash { - utils.FromContext(c).WithError(nil).Errorf("mismatch agent hash to requested one") + logrus.WithError(nil).Errorf("mismatch agent hash to requested one") response.Error(c, response.ErrPatchLastAgentUpgradeInvalidAgentUpgradeInfo, err) return } @@ -425,24 +426,24 @@ func (s *UpgradeService) PatchLastAgentUpgrade(c *gin.Context) { } err = s.db.Scopes(scope).Model(&binary).Take(&binary).Error if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - utils.FromContext(c).WithError(nil).Errorf("error getting binary info by version '%s', record not found", task.Version) + logrus.WithError(nil).Errorf("error getting binary info by version '%s', record not found", task.Version) response.Error(c, response.ErrPatchLastAgentUpgradeAgentBinaryNotFound, err) return } else if err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting binary info by version '%s'", task.Version) + logrus.WithError(err).Errorf("error getting binary info by version '%s'", task.Version) response.Error(c, response.ErrInternal, err) return } s3, err := storage.NewS3(sv.Info.S3.ToS3ConnParams()) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error openning connection to S3") + logrus.WithError(err).Errorf("error openning connection to S3") response.Error(c, response.ErrInternal, err) return } if err = utils.UploadAgentBinariesToInstBucket(binary, s3); err != nil { - utils.FromContext(c).WithError(err).Errorf("error uploading agent binaries to S3 instance bucket") + logrus.WithError(err).Errorf("error uploading agent binaries to S3 instance bucket") response.Error(c, response.ErrPatchLastAgentUpgradeUpdateAgentBinariesFail, err) return } @@ -458,11 +459,11 @@ func (s *UpgradeService) PatchLastAgentUpgrade(c *gin.Context) { } err = iDB.Model(&task).UpdateColumns(update_info).Error if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - utils.FromContext(c).WithError(nil).Errorf("error updating last agent upgrade information by id '%d', task not found", task.ID) + logrus.WithError(nil).Errorf("error updating last agent upgrade information by id '%d', task not found", task.ID) response.Error(c, response.ErrPatchLastAgentUpgradeLastUpgradeInfoNotFound, err) return } else if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating last agent upgrade information by id '%d'", task.ID) + logrus.WithError(err).Errorf("error updating last agent upgrade information by id '%d'", task.ID) response.Error(c, response.ErrInternal, err) return } diff --git a/pkg/app/api/server/private/users.go b/pkg/app/api/server/private/users.go index b758faed..9e4e2c3f 100644 --- a/pkg/app/api/server/private/users.go +++ b/pkg/app/api/server/private/users.go @@ -6,6 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "golang.org/x/crypto/bcrypt" "soldr/pkg/app/api/models" @@ -64,7 +65,7 @@ func (s *UserService) GetCurrentUser(c *gin.Context) { Related(&resp.Role). Related(&resp.Tenant).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding current user") + logrus.WithError(err).Errorf("error finding current user") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrUsersNotFound, err) } else { @@ -72,7 +73,7 @@ func (s *UserService) GetCurrentUser(c *gin.Context) { } return } else if err = resp.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user data '%s'", resp.Hash) + logrus.WithError(err).Errorf("error validating user data '%s'", resp.Hash) response.Error(c, response.ErrUsersInvalidData, err) return } @@ -104,7 +105,7 @@ func (s *UserService) ChangePasswordCurrentUser(c *gin.Context) { if err == nil { err = form.Valid() } - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrChangePasswordCurrentUserInvalidPassword, err) return } @@ -115,7 +116,7 @@ func (s *UserService) ChangePasswordCurrentUser(c *gin.Context) { } if err = s.db.Scopes(scope).Take(&user).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding current user") + logrus.WithError(err).Errorf("error finding current user") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrUsersNotFound, err) } else { @@ -123,19 +124,19 @@ func (s *UserService) ChangePasswordCurrentUser(c *gin.Context) { } return } else if err = user.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user data '%s'", user.Hash) + logrus.WithError(err).Errorf("error validating user data '%s'", user.Hash) response.Error(c, response.ErrUsersInvalidData, err) return } if err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(form.CurrentPassword)); err != nil { - utils.FromContext(c).WithError(err).Errorf("error checking password for current user") + logrus.WithError(err).Errorf("error checking password for current user") response.Error(c, response.ErrChangePasswordCurrentUserInvalidCurrentPassword, err) return } if encPass, err = utils.EncryptPassword(form.Password); err != nil { - utils.FromContext(c).WithError(err).Errorf("error making new password for current user") + logrus.WithError(err).Errorf("error making new password for current user") response.Error(c, response.ErrChangePasswordCurrentUserInvalidNewPassword, err) return } @@ -143,7 +144,7 @@ func (s *UserService) ChangePasswordCurrentUser(c *gin.Context) { user.PasswordChangeRequired = false if err = s.db.Scopes(scope).Select("password", "password_change_required").Save(&user).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating password for current user") + logrus.WithError(err).Errorf("error updating password for current user") response.Error(c, response.ErrInternal, err) return } @@ -173,7 +174,7 @@ func (s *UserService) GetUsers(c *gin.Context) { ) if err = c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrUsersInvalidRequest, err) return } @@ -199,13 +200,13 @@ func (s *UserService) GetUsers(c *gin.Context) { }, }) default: - utils.FromContext(c).WithError(nil).Errorf("error filtering user role services: unexpected role") + logrus.WithError(nil).Errorf("error filtering user role services: unexpected role") response.Error(c, response.ErrInternal, err) return } if resp.Total, err = query.Query(s.db, &resp.Users); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding users") + logrus.WithError(err).Errorf("error finding users") response.Error(c, response.ErrInternal, err) return } @@ -216,13 +217,13 @@ func (s *UserService) GetUsers(c *gin.Context) { } if err = s.db.Find(&roles, "id IN (?)", rids).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding linked roles") + logrus.WithError(err).Errorf("error finding linked roles") response.Error(c, response.ErrInternal, err) return } if err = s.db.Find(&tenans, "id IN (?)", tids).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding linked tenants") + logrus.WithError(err).Errorf("error finding linked tenants") response.Error(c, response.ErrInternal, err) return } @@ -247,7 +248,7 @@ func (s *UserService) GetUsers(c *gin.Context) { for i := 0; i < len(resp.Users); i++ { if err = resp.Users[i].Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user data '%s'", resp.Users[i].Hash) + logrus.WithError(err).Errorf("error validating user data '%s'", resp.Users[i].Hash) response.Error(c, response.ErrUsersInvalidData, err) return } @@ -291,7 +292,7 @@ func (s *UserService) GetUser(c *gin.Context) { } if err = s.db.Scopes(scope).Take(&resp.User).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding user by hash") + logrus.WithError(err).Errorf("error finding user by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrUsersNotFound, err) } else { @@ -300,7 +301,7 @@ func (s *UserService) GetUser(c *gin.Context) { return } if err = s.db.Model(&resp.User).Related(&resp.Role).Related(&resp.Tenant).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding related models by user hash") + logrus.WithError(err).Errorf("error finding related models by user hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrGetUserModelsNotFound, err) } else { @@ -309,7 +310,7 @@ func (s *UserService) GetUser(c *gin.Context) { return } if err = resp.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user data '%s'", resp.Hash) + logrus.WithError(err).Errorf("error validating user data '%s'", resp.Hash) response.Error(c, response.ErrUsersInvalidData, err) return } @@ -337,7 +338,7 @@ func (s *UserService) CreateUser(c *gin.Context) { ) if err = c.ShouldBindJSON(&user); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrUsersInvalidRequest, err) return } @@ -355,7 +356,7 @@ func (s *UserService) CreateUser(c *gin.Context) { } user.TenantID = tid default: - utils.FromContext(c).WithError(nil).Errorf("error filtering user role services: unexpected role") + logrus.WithError(nil).Errorf("error filtering user role services: unexpected role") response.Error(c, response.ErrInternal, nil) return } @@ -363,13 +364,13 @@ func (s *UserService) CreateUser(c *gin.Context) { user.ID = 0 user.Hash = utils.MakeUserHash(user.Name) if err = user.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user") + logrus.WithError(err).Errorf("error validating user") response.Error(c, response.ErrCreateUserInvalidUser, err) return } if encPassword, err = utils.EncryptPassword(user.Password); err != nil { - utils.FromContext(c).WithError(err).Errorf("error encoding password") + logrus.WithError(err).Errorf("error encoding password") response.Error(c, response.ErrInternal, err) return } else { @@ -377,7 +378,7 @@ func (s *UserService) CreateUser(c *gin.Context) { } if err = s.db.Create(&user).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error creating user") + logrus.WithError(err).Errorf("error creating user") response.Error(c, response.ErrInternal, err) return } @@ -386,11 +387,11 @@ func (s *UserService) CreateUser(c *gin.Context) { Related(&resp.Role). Related(&resp.Tenant).Error if err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding user by hash") + logrus.WithError(err).Errorf("error finding user by hash") response.Error(c, response.ErrInternal, err) return } else if err = resp.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user data '%s'", resp.Hash) + logrus.WithError(err).Errorf("error validating user data '%s'", resp.Hash) response.Error(c, response.ErrUsersInvalidData, err) return } @@ -419,19 +420,19 @@ func (s *UserService) PatchUser(c *gin.Context) { ) if err = c.ShouldBindJSON(&user); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding JSON") + logrus.WithError(err).Errorf("error binding JSON") response.Error(c, response.ErrUsersInvalidRequest, err) return } else if hash != user.Hash { - utils.FromContext(c).WithError(nil).Errorf("mismatch user hash to requested one") + logrus.WithError(nil).Errorf("mismatch user hash to requested one") response.Error(c, response.ErrUsersInvalidRequest, nil) return } else if err = user.User.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user JSON") + logrus.WithError(err).Errorf("error validating user JSON") response.Error(c, response.ErrUsersInvalidRequest, err) return } else if err = user.Valid(); user.Password != "" && err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user password") + logrus.WithError(err).Errorf("error validating user password") response.Error(c, response.ErrUsersInvalidRequest, err) return } @@ -456,7 +457,7 @@ func (s *UserService) PatchUser(c *gin.Context) { public_info := []interface{}{"mail", "name", "status"} if user.Password != "" { if encPassword, err := utils.EncryptPassword(user.Password); err != nil { - utils.FromContext(c).WithError(err).Errorf("error encoding password") + logrus.WithError(err).Errorf("error encoding password") response.Error(c, response.ErrInternal, err) return } else { @@ -469,17 +470,17 @@ func (s *UserService) PatchUser(c *gin.Context) { } if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - utils.FromContext(c).WithError(nil).Errorf("error updating user by hash '%s', user not found", hash) + logrus.WithError(nil).Errorf("error updating user by hash '%s', user not found", hash) response.Error(c, response.ErrUsersNotFound, err) return } else if err != nil { - utils.FromContext(c).WithError(err).Errorf("error updating user by hash '%s'", hash) + logrus.WithError(err).Errorf("error updating user by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } if err = s.db.Scopes(scope).Take(&resp.User).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding user by hash") + logrus.WithError(err).Errorf("error finding user by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrUsersNotFound, err) } else { @@ -488,7 +489,7 @@ func (s *UserService) PatchUser(c *gin.Context) { return } if err = s.db.Model(&resp.User).Related(&resp.Role).Related(&resp.Tenant).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding related models by user hash") + logrus.WithError(err).Errorf("error finding related models by user hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrPatchUserModelsNotFound, err) } else { @@ -497,7 +498,7 @@ func (s *UserService) PatchUser(c *gin.Context) { return } if err = resp.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user data '%s'", resp.Hash) + logrus.WithError(err).Errorf("error validating user data '%s'", resp.Hash) response.Error(c, response.ErrInternal, err) return } @@ -540,7 +541,7 @@ func (s *UserService) DeleteUser(c *gin.Context) { } if err = s.db.Scopes(scope).Take(&user.User).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding user by hash") + logrus.WithError(err).Errorf("error finding user by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrUsersNotFound, err) } else { @@ -549,7 +550,7 @@ func (s *UserService) DeleteUser(c *gin.Context) { return } if err = s.db.Model(&user.User).Related(&user.Role).Related(&user.Tenant).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding related models by user hash") + logrus.WithError(err).Errorf("error finding related models by user hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrDeleteUserModelsNotFound, err) } else { @@ -558,13 +559,13 @@ func (s *UserService) DeleteUser(c *gin.Context) { return } if err = user.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user data '%s'", user.Hash) + logrus.WithError(err).Errorf("error validating user data '%s'", user.Hash) response.Error(c, response.ErrUsersInvalidData, err) return } if err = s.db.Delete(&user.User).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error deleting user by hash '%s'", hash) + logrus.WithError(err).Errorf("error deleting user by hash '%s'", hash) response.Error(c, response.ErrInternal, err) return } diff --git a/pkg/app/api/server/private/versions.go b/pkg/app/api/server/private/versions.go index a4e21ccb..a5c0fe33 100644 --- a/pkg/app/api/server/private/versions.go +++ b/pkg/app/api/server/private/versions.go @@ -6,6 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/client" srvcontext "soldr/pkg/app/api/server/context" @@ -82,27 +83,27 @@ func (s *VersionService) GetVersions(c *gin.Context) { ) if err := c.ShouldBindQuery(&query); err != nil { - utils.FromContext(c).WithError(err).Errorf("error binding query") + logrus.WithError(err).Errorf("error binding query") response.Error(c, response.ErrVersionsInvalidRequest, err) return } serviceHash, ok := srvcontext.GetString(c, "svc") if !ok { - utils.FromContext(c).Errorf("could not get service hash") + logrus.Errorf("could not get service hash") response.Error(c, response.ErrInternal, nil) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } table, sqlMappers, err = getVersionMappers(&query) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting version mappers by query") + logrus.WithError(err).Errorf("error getting version mappers by query") response.Error(c, response.ErrVersionsMapperNotFound, err) return } @@ -130,7 +131,7 @@ func (s *VersionService) GetVersions(c *gin.Context) { } if resp.Total, err = query.Query(iDB, &resp.Versions, funcs...); err != nil { - utils.FromContext(c).WithError(err).Errorf("error finding versions") + logrus.WithError(err).Errorf("error finding versions") response.Error(c, response.ErrVersionsInvalidQuery, err) return } diff --git a/pkg/app/api/server/proto/proto.go b/pkg/app/api/server/proto/proto.go index 803e41be..7d3dd6a7 100644 --- a/pkg/app/api/server/proto/proto.go +++ b/pkg/app/api/server/proto/proto.go @@ -215,7 +215,7 @@ func wsConnectToVXServer(c *gin.Context, connType vxproto.AgentType, sockID, soc validate = models.GetValidator() ) - logger := utils.FromContext(c).WithFields(logrus.Fields{ + logger := logrus.WithFields(logrus.Fields{ "sock_id": sockID, "sock_type": sockType, "conn_id": getRandomID(), @@ -376,13 +376,13 @@ func (s *ProtoService) AggregateWSConnect(c *gin.Context) { serviceHash, err := getServiceHash(c) if err != nil { - utils.FromContext(c).WithError(err).Errorf("could not get service hash") + logrus.WithError(err).Errorf("could not get service hash") response.Error(c, response.ErrInternal, err) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -391,7 +391,7 @@ func (s *ProtoService) AggregateWSConnect(c *gin.Context) { if err == nil { uaf.ObjectDisplayName = name } else { - utils.FromContext(c).WithError(err).Errorf("error finding group by hash") + logrus.WithError(err).Errorf("error finding group by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrAgentsNotFound, nil) return @@ -402,7 +402,7 @@ func (s *ProtoService) AggregateWSConnect(c *gin.Context) { sockType, ok := srvcontext.GetString(c, "cpt") if !ok || sockType != "aggregate" { - utils.FromContext(c).WithError(nil).Errorf("mismatch socket type to incoming token type") + logrus.WithError(nil).Errorf("mismatch socket type to incoming token type") response.Error(c, response.ErrProtoSockMismatch, nil) return } @@ -424,13 +424,13 @@ func (s *ProtoService) BrowserWSConnect(c *gin.Context) { serviceHash, err := getServiceHash(c) if err != nil { - utils.FromContext(c).WithError(err).Errorf("could not get service hash") + logrus.WithError(err).Errorf("could not get service hash") response.Error(c, response.ErrInternal, err) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -439,7 +439,7 @@ func (s *ProtoService) BrowserWSConnect(c *gin.Context) { if err == nil { uaf.ObjectDisplayName = name } else { - utils.FromContext(c).WithError(err).Errorf("error finding agent by hash") + logrus.WithError(err).Errorf("error finding agent by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrAgentsNotFound, nil) return @@ -451,7 +451,7 @@ func (s *ProtoService) BrowserWSConnect(c *gin.Context) { sockType, ok := srvcontext.GetString(c, "cpt") if !ok || sockType != "browser" { - utils.FromContext(c).WithError(nil).Errorf("mismatch socket type to incoming token type") + logrus.WithError(nil).Errorf("mismatch socket type to incoming token type") response.Error(c, response.ErrProtoSockMismatch, nil) return } @@ -472,13 +472,13 @@ func (s *ProtoService) ExternalWSConnect(c *gin.Context) { serviceHash, err := getServiceHash(c) if err != nil { - utils.FromContext(c).WithError(err).Errorf("could not get service hash") + logrus.WithError(err).Errorf("could not get service hash") response.Error(c, response.ErrInternal, err) return } iDB, err := s.serverConnector.GetDB(c, serviceHash) if err != nil { - utils.FromContext(c).WithError(err).Error() + logrus.WithError(err).Error() response.Error(c, response.ErrInternalDBNotFound, err) return } @@ -487,7 +487,7 @@ func (s *ProtoService) ExternalWSConnect(c *gin.Context) { if err == nil { uaf.ObjectDisplayName = name } else { - utils.FromContext(c).WithError(err).Errorf("error finding agent by hash") + logrus.WithError(err).Errorf("error finding agent by hash") if errors.Is(err, gorm.ErrRecordNotFound) { response.Error(c, response.ErrAgentsNotFound, nil) return @@ -498,7 +498,7 @@ func (s *ProtoService) ExternalWSConnect(c *gin.Context) { sockType, ok := srvcontext.GetString(c, "cpt") if !ok || sockType != "external" { - utils.FromContext(c).WithError(nil).Errorf("mismatch socket type to incoming token type") + logrus.WithError(nil).Errorf("mismatch socket type to incoming token type") response.Error(c, response.ErrProtoSockMismatch, nil) return } diff --git a/pkg/app/api/server/public/auth.go b/pkg/app/api/server/public/auth.go index a8bf118d..a50d5e07 100644 --- a/pkg/app/api/server/public/auth.go +++ b/pkg/app/api/server/public/auth.go @@ -46,7 +46,7 @@ func AuthLogin(c *gin.Context) { if err == nil { err = data.Valid() } - utils.FromContext(c).WithError(err).Errorf("error validating request data") + logrus.WithError(err).Errorf("error validating request data") response.Error(c, response.ErrAuthInvalidLoginRequest, err) return } @@ -57,50 +57,50 @@ func AuthLogin(c *gin.Context) { } if err = gDB.Take(&user, "(mail = ? OR name = ?) AND password IS NOT NULL", data.Mail, data.Mail).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting user by mail '%s'", data.Mail) + logrus.WithError(err).Errorf("error getting user by mail '%s'", data.Mail) response.Error(c, response.ErrAuthInvalidCredentials, err) return } else if err = user.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user data '%s'", user.Hash) + logrus.WithError(err).Errorf("error validating user data '%s'", user.Hash) response.Error(c, response.ErrAuthInvalidUserData, err) return } else if user.RoleID == 100 { - utils.FromContext(c).WithError(err).Errorf("can't authorize external user '%s'", user.Hash) + logrus.WithError(err).Errorf("can't authorize external user '%s'", user.Hash) response.Error(c, response.ErrAuthInvalidUserData, fmt.Errorf("user is external")) return } if err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(data.Password)); err != nil { - utils.FromContext(c).Errorf("error matching user input password") + logrus.Errorf("error matching user input password") response.Error(c, response.ErrAuthInvalidCredentials, err) return } if user.Status != "active" { - utils.FromContext(c).Errorf("error checking active state for user '%s'", user.Status) + logrus.Errorf("error checking active state for user '%s'", user.Status) response.Error(c, response.ErrAuthInactiveUser, fmt.Errorf("user is inactive")) return } if service, err = getService(c, gDB, data.Service, &user.User); err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading service data by hash '%s'", data.Service) + logrus.WithError(err).Errorf("error loading service data by hash '%s'", data.Service) response.Error(c, response.ErrAuthInvalidServiceData, err) return } else if err = service.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating service data '%s'", service.Hash) + logrus.WithError(err).Errorf("error validating service data '%s'", service.Hash) response.Error(c, response.ErrAuthInvalidServiceData, err) return } if err = gDB.Table("privileges").Where("role_id = ?", user.RoleID).Pluck("name", &privs).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting user privileges list '%s'", user.Hash) + logrus.WithError(err).Errorf("error getting user privileges list '%s'", user.Hash) response.Error(c, response.ErrAuthInvalidServiceData, err) return } uuid, err := utils.MakeUuidStrFromHash(user.Hash) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user data '%s'", user.Hash) + logrus.WithError(err).Errorf("error validating user data '%s'", user.Hash) response.Error(c, response.ErrAuthInvalidUserData, err) return } @@ -125,7 +125,7 @@ func AuthLogin(c *gin.Context) { }) session.Save() - utils.FromContext(c). + logrus. WithFields(logrus.Fields{ "age": expires, "uid": user.ID, @@ -157,7 +157,7 @@ func AuthLogout(c *gin.Context) { } session := sessions.Default(c) - utils.FromContext(c). + logrus. WithFields(logrus.Fields{ "uid": session.Get("uid"), "rid": session.Get("rid"), @@ -194,7 +194,7 @@ func AuthSwitchService(c *gin.Context) { serviceHash := c.PostForm("service") if err := models.GetValidator().Var(serviceHash, "len=32,hexadecimal,lowercase,required"); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating input data") + logrus.WithError(err).Errorf("error validating input data") response.Error(c, response.ErrAuthInvalidSwitchServiceHash, err) return } @@ -209,21 +209,21 @@ func AuthSwitchService(c *gin.Context) { expires := int(time.Until(time.Unix(exp, 0)) / time.Second) if err = gDB.Take(&tenant, "id = ?", tid).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading tenant data '%d'", tid) + logrus.WithError(err).Errorf("error loading tenant data '%d'", tid) response.Error(c, response.ErrAuthInvalidTenantData, err) return } else if err = tenant.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating tenant data '%d:%s'", tid, tenant.Hash) + logrus.WithError(err).Errorf("error validating tenant data '%d:%s'", tid, tenant.Hash) response.Error(c, response.ErrAuthInvalidTenantData, err) return } if err = gDB.Take(&service, "hash LIKE ? AND status = 'active'", serviceHash).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading service data '%s'", serviceHash) + logrus.WithError(err).Errorf("error loading service data '%s'", serviceHash) response.Error(c, response.ErrAuthInvalidServiceData, err) return } else if err = service.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating service data '%s'", serviceHash) + logrus.WithError(err).Errorf("error validating service data '%s'", serviceHash) response.Error(c, response.ErrAuthInvalidServiceData, err) return } @@ -239,7 +239,7 @@ func AuthSwitchService(c *gin.Context) { }) session.Save() - utils.FromContext(c). + logrus. WithFields(logrus.Fields{ "age": expires, "uid": session.Get("uid"), diff --git a/pkg/app/api/server/public/info.go b/pkg/app/api/server/public/info.go index 0c6422e3..bb7b2c88 100644 --- a/pkg/app/api/server/public/info.go +++ b/pkg/app/api/server/public/info.go @@ -48,7 +48,7 @@ func refreshCookie(c *gin.Context, resp *info, privs []string) error { }) session.Save() - utils.FromContext(c). + logrus. WithFields(logrus.Fields{ "age": expires, "uid": resp.User.ID, @@ -120,19 +120,19 @@ func Info(c *gin.Context) { response.Error(c, response.ErrInfoUserNotFound, err) return } else if err = resp.User.Valid(); err != nil { - utils.FromContext(c).WithError(err).Errorf("error validating user data '%s'", resp.User.Hash) + logrus.WithError(err).Errorf("error validating user data '%s'", resp.User.Hash) response.Error(c, response.ErrInfoInvalidUserData, err) return } if err = gDB.Table("privileges").Where("role_id = ?", resp.User.RoleID).Pluck("name", &privs).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting user privileges list '%s'", resp.User.Hash) + logrus.WithError(err).Errorf("error getting user privileges list '%s'", resp.User.Hash) response.Error(c, response.ErrInfoInvalidUserData, err) return } if err = gDB.Find(&resp.Services, "tenant_id = ?", resp.User.TenantID).Error; err != nil { - utils.FromContext(c).WithError(err).Errorf("error getting user services list '%s'", resp.User.Hash) + logrus.WithError(err).Errorf("error getting user services list '%s'", resp.User.Hash) response.Error(c, response.ErrInfoInvalidServiceData, err) return } @@ -151,7 +151,7 @@ func Info(c *gin.Context) { var fiveMins int64 = 5 * 60 if nowt >= gtmt+fiveMins && c.Query("refresh_cookie") != "false" { if err = refreshCookie(c, &resp, privs); err != nil { - utils.FromContext(c).WithError(err).Errorf("failed to refresh token") + logrus.WithError(err).Errorf("failed to refresh token") // raise error when there is elapsing last five minutes if nowt >= gtmt+int64(utils.DefaultSessionTimeout)-fiveMins { response.Error(c, response.ErrInternal, err) @@ -164,7 +164,7 @@ func Info(c *gin.Context) { // raise error when there is elapsing last five minutes // and user hasn't permissions in the session auth cookie if resp.Type != "guest" && resp.Privs == nil { - utils.FromContext(c). + logrus. WithFields(logrus.Fields{ "uid": resp.User.ID, "rid": resp.User.RoleID, diff --git a/pkg/app/api/server/router.go b/pkg/app/api/server/router.go index 80f9b767..c3d0a1c1 100644 --- a/pkg/app/api/server/router.go +++ b/pkg/app/api/server/router.go @@ -3,7 +3,6 @@ package server import ( "crypto/tls" "encoding/gob" - "io" "io/ioutil" "net/http" "net/http/httputil" @@ -16,7 +15,6 @@ import ( "github.com/gin-contrib/sessions/cookie" "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" - "github.com/natefinch/lumberjack" "github.com/sirupsen/logrus" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" @@ -54,7 +52,6 @@ func NewRouter( userActionWriter useraction.Writer, dbConns *mem.DBConnectionStorage, s3Conns *mem.S3ConnectionStorage, - ) *gin.Engine { gin.SetMode(gin.ReleaseMode) if _, exists := os.LookupEnv("DEBUG"); exists { @@ -67,19 +64,6 @@ func NewRouter( cookieStore := cookie.NewStore(utils.MakeCookieStoreKey()) - logDir := "logs" - if dir, ok := os.LookupEnv("LOG_DIR"); ok { - logDir = dir - } - apiLogFile := &lumberjack.Logger{ - Filename: path.Join(logDir, "api.log"), - MaxSize: 100, - MaxBackups: 7, - MaxAge: 14, - Compress: true, - } - defer apiLogFile.Close() - staticPath := "./static" if uiStaticPath, ok := os.LookupEnv("API_STATIC_PATH"); ok { staticPath = uiStaticPath @@ -88,15 +72,15 @@ func NewRouter( index := func(c *gin.Context) { data, err := ioutil.ReadFile(path.Join(staticPath, "/index.html")) if err != nil { - utils.FromContext(c).WithError(err).Errorf("error loading index.html") + logrus.WithError(err).Errorf("error loading index.html") return } - c.Data(200, "text/html", []byte(data)) + c.Data(200, "text/html", data) } router := gin.New() router.Use(otelgin.Middleware("vxapi")) - router.Use(gin.LoggerWithWriter(io.MultiWriter(apiLogFile, os.Stdout))) + router.Use(WithLogger([]string{})) router.Use(gin.Recovery()) router.Use(sessions.Sessions("auth", cookieStore)) diff --git a/pkg/app/api/user_action/log.go b/pkg/app/api/user_action/log.go index e98650ef..d9c6d4e1 100644 --- a/pkg/app/api/user_action/log.go +++ b/pkg/app/api/user_action/log.go @@ -2,16 +2,12 @@ package useraction import ( "github.com/sirupsen/logrus" - - "soldr/pkg/log" ) -type LogWriter struct { - logger log.Logger -} +type LogWriter struct{} -func NewLogWriter(logger log.Logger) *LogWriter { - return &LogWriter{logger: logger} +func NewLogWriter() *LogWriter { + return &LogWriter{} } func (w *LogWriter) WriteUserAction(uaf Fields) error { @@ -27,6 +23,6 @@ func (w *LogWriter) WriteUserAction(uaf Fields) error { "success": uaf.Success, "fail_reason": uaf.FailReason, } - w.logger.WithFields(fields).Info() + logrus.WithFields(fields).Info() return nil } diff --git a/pkg/app/api/utils/utils.go b/pkg/app/api/utils/utils.go index 1f1e91ae..7e60941d 100644 --- a/pkg/app/api/utils/utils.go +++ b/pkg/app/api/utils/utils.go @@ -23,6 +23,7 @@ import ( "github.com/sirupsen/logrus" "golang.org/x/crypto/bcrypt" + "soldr/pkg/logger" "soldr/pkg/storage" "soldr/pkg/system" "soldr/pkg/version" @@ -91,11 +92,6 @@ func IsUseSSL() bool { return os.Getenv("API_USE_SSL") == "true" } -// FromContext is function to get logrus Entry with context -func FromContext(c *gin.Context) *logrus.Entry { - return logrus.WithContext(c.Request.Context()) -} - // UniqueUint64InSlice is function to remove duplicates in slice of uint64 func UniqueUint64InSlice(slice []uint64) []uint64 { keys := make(map[uint64]bool) @@ -144,7 +140,12 @@ func GetDB(user, pass, host, port, name string) *gorm.DB { Errorf("error opening gorm connection: %v", err) return nil } - conn.LogMode(true) // avoid annoying logs + + conn.SetLogger(&logger.GormLogger{}) + if _, exists := os.LookupEnv("DEBUG"); exists { + conn.LogMode(true) + } + validations.RegisterCallbacks(conn) conn.DB().SetMaxIdleConns(10) @@ -153,7 +154,6 @@ func GetDB(user, pass, host, port, name string) *gorm.DB { meter.ApplyGorm(conn) - // conn.SetLogger(logrus.StandardLogger()) return conn } diff --git a/pkg/log/context.go b/pkg/log/context.go deleted file mode 100644 index 6038934f..00000000 --- a/pkg/log/context.go +++ /dev/null @@ -1,32 +0,0 @@ -package log - -import ( - "context" - "io" - - "github.com/sirupsen/logrus" -) - -// Helper methods used by the application to get the request-scoped -// logger entry and set additional fields between handlers. -// -// This is a useful pattern to use to set state on the entry as it -// passes through the handler chain, which at any point can be logged -// with a call to .Print(), .Info(), etc. - -type loggerKey struct{} - -func FromContext(ctx context.Context) Logger { - if l, ok := ctx.Value(loggerKey{}).(Logger); ok { - return l - } - // Discard log in case if logger is not attached. - l := logrus.New() - l.Level = logrus.PanicLevel - l.Out = io.Discard - return l -} - -func AttachToContext(ctx context.Context, l Logger) context.Context { - return context.WithValue(ctx, loggerKey{}, l) -} diff --git a/pkg/log/log.go b/pkg/log/log.go deleted file mode 100644 index d17a5302..00000000 --- a/pkg/log/log.go +++ /dev/null @@ -1,73 +0,0 @@ -package log - -import ( - "errors" - "fmt" - "io" - "strings" - - "github.com/sirupsen/logrus" -) - -// Logger generalizes the Logger types -type Logger interface { - logrus.FieldLogger -} - -type Config struct { - Level logrus.Level - Format Format -} - -func New(cfg Config, output io.Writer) Logger { - logger := logrus.New() - logger.Out = output - switch cfg.Format { - case FormatJSON: - logger.Formatter = &logrus.JSONFormatter{} - case FormatText: - logger.Formatter = &logrus.TextFormatter{ForceColors: true, FullTimestamp: true} - default: - logger.Warnf("log: invalid formatter: %v, continue with default", cfg.Format) - } - logger.Level = cfg.Level - return logger -} - -const ( - FormatInvalid Format = iota - FormatText - FormatJSON -) - -const ( - formatTextStr = "text" - formatJSONStr = "json" -) - -type Format int - -func ParseFormat(s string) (Format, error) { - s = strings.ToLower(s) - switch s { - case formatTextStr: - return FormatText, nil - case formatJSONStr: - return FormatJSON, nil - } - return FormatInvalid, errors.New("invalid format string") -} - -func (f Format) String() string { - switch f { - case FormatText: - return formatTextStr - case FormatJSON: - return formatJSONStr - } - return fmt.Sprintf("invalid (%d)", f) -} - -func ParseLevel(s string) (logrus.Level, error) { - return logrus.ParseLevel(s) -} diff --git a/pkg/logger/gorm.go b/pkg/logger/gorm.go new file mode 100644 index 00000000..64c2845c --- /dev/null +++ b/pkg/logger/gorm.go @@ -0,0 +1,25 @@ +package logger + +import "github.com/sirupsen/logrus" + +type GormLogger struct{} + +func (*GormLogger) Print(v ...interface{}) { + switch v[0] { + case "sql": + logrus.WithFields( + logrus.Fields{ + "module": "gorm", + "type": "sql", + "rows_returned": v[5], + "src": v[1], + "values": v[4], + "duration": v[2], + }, + ).Info(v[3]) + case "log": + logrus.WithFields(logrus.Fields{"module": "gorm"}).Info(v[2]) + case "info": + // do not log validators + } +} diff --git a/pkg/logger/log.go b/pkg/logger/log.go new file mode 100644 index 00000000..70ccf94b --- /dev/null +++ b/pkg/logger/log.go @@ -0,0 +1,28 @@ +package logger + +import ( + "strings" + + "github.com/sirupsen/logrus" +) + +// Logger generalizes the Logger types +type Logger interface { + logrus.FieldLogger +} + +func ParseFormat(s string) logrus.Formatter { + s = strings.ToLower(s) + if s == "text" { + return &logrus.TextFormatter{ForceColors: true, FullTimestamp: true} + } + return &logrus.JSONFormatter{} +} + +func ParseLevel(s string) logrus.Level { + l, err := logrus.ParseLevel(s) + if err != nil { + return logrus.InfoLevel + } + return l +} diff --git a/pkg/storage/mysql/mysql.go b/pkg/storage/mysql/mysql.go index fa5fa22a..14bb742a 100644 --- a/pkg/storage/mysql/mysql.go +++ b/pkg/storage/mysql/mysql.go @@ -11,9 +11,10 @@ import ( "github.com/jmoiron/sqlx" "github.com/qor/validations" migrate "github.com/rubenv/sql-migrate" + "github.com/sirupsen/logrus" "soldr/pkg/app/api/utils/meter" - "soldr/pkg/log" + "soldr/pkg/logger" "soldr/pkg/secret" ) @@ -82,7 +83,7 @@ func (d *DB) RetryConnect(ctx context.Context, maxAttempts int, backoff time.Dur break } nextAttemptWait := time.Duration(attempt) * backoff - log.FromContext(ctx).Warnf( + logrus.Warnf( "attempt %v: could not establish a connection with the database, wait for %v.", attempt, nextAttemptWait, @@ -110,15 +111,13 @@ func (d *DB) Migrate(path string) error { return err } -func (d *DB) WithORM(ctx context.Context) (*gorm.DB, error) { +func (d *DB) WithORM() (*gorm.DB, error) { conn, err := gorm.Open("mysql", d.DSN().Unmask()) if err != nil { return nil, err } - conn.LogMode(true) + conn.SetLogger(&logger.GormLogger{}) validations.RegisterCallbacks(conn) meter.ApplyGorm(conn) - - conn.SetLogger(log.FromContext(ctx)) return conn, nil }