package server import ( "fmt" "net/http" "github.com/gin-gonic/gin" "edge-infra.dev/pkg/lib/fog" rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules" ) //nolint:dupl func (res RulesEngineService) postDefaultRules(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") var rules rulesengine.WriteRules if err := c.ShouldBindJSON(&rules); err != nil { log.Error(err, "bind error") c.Status(http.StatusBadRequest) return } if err := rules.Validate(); err != nil { log.Error(err, "validation error") c.Status(http.StatusBadRequest) return } result, err := res.RulesEngine.AddDefaultRules(c.Request.Context(), rules) if err != nil { log.Error(err, "Default rule addition error") c.Status(http.StatusInternalServerError) return } if len(result.Errors) > 0 { log.Error(fmt.Errorf("error adding rule"), "Requested data not found. No rule inserted") c.JSON(http.StatusNotFound, result) return } c.Status(http.StatusOK) } func (res RulesEngineService) readAllDefaultRules(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") vals, err := res.RulesEngine.ReadAllDefaultRules(c.Request.Context()) if err != nil { log.Error(err, "read error") c.Status(http.StatusInternalServerError) return } if len(vals) == 0 { log.Info("No default rules returned") c.JSON(http.StatusOK, nil) return } c.JSON(http.StatusOK, vals) } func (res RulesEngineService) readDefaultRule(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") name := c.Param("commandName") // TODO change name of this var privs, err := res.RulesEngine.ReadDefaultRulesForCommand(c.Request.Context(), name) if err != nil { log.Error(err, "read error") c.Status(http.StatusInternalServerError) return } if len(privs) == 0 { log.Info("No rule returned") c.JSON(http.StatusOK, nil) return } c.JSON(http.StatusOK, privs[0]) } func (res RulesEngineService) deleteDefaultRule(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") command, privilege := c.Param("commandName"), c.Param("privilegeName") log = log.WithValues("commandName", command, "privilegeName", privilege) c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log)) if command == "" { log.Error(fmt.Errorf("invalid parameter"), "request invalid") c.Status(http.StatusBadRequest) return } result, err := res.RulesEngine.DeleteDefaultRule(c.Request.Context(), command, privilege) if err != nil { log.Error(err, "deletion error") c.Status(http.StatusInternalServerError) return } if len(result.Errors) > 0 { for _, err := range result.Errors { if err.Type == rulesengine.Conflict { log.Error(fmt.Errorf("database query conflict"), "Conflict with database") c.JSON(http.StatusConflict, result) return } } log.Error(fmt.Errorf("error deleting rule"), "Requested data not found. No deletion performed") c.JSON(http.StatusNotFound, result) return } c.Status(http.StatusOK) } func (res RulesEngineService) readAllRulesForCommand(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") name := c.Param("commandName") log = log.WithValues("commandName", name) c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log)) rule, err := res.RulesEngine.ReadAllRulesForCommand(c, name) if err != nil { log.Error(err, "read error") c.Status(http.StatusInternalServerError) return } if rule.Command.ID == "" || rule.Command.Name == "" { log.Info("command not listed in database") c.JSON(http.StatusOK, nil) return } c.JSON(http.StatusOK, rule) }