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" ) func (res RulesEngineService) readAllRulesForBanners(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") _, ok := c.GetQuery("bannerName") if ok { log.Info("Querying for rules in one banner") res.readAllRulesInBanner(c) } else { log.Info("Querying for rules in all banners") res.readAllRulesInAllBanners(c) } } func (res RulesEngineService) readAllRulesInBanner(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") bannerName := c.Query("bannerName") log = log.WithValues("bannerName", bannerName) c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log)) if bannerName == "" { log.Error(fmt.Errorf("invalid parameter"), "request invalid") c.Status(http.StatusBadRequest) return } rules, err := res.RulesEngine.ReadRulesForBanner(c.Request.Context(), bannerName) if err != nil { log.Error(err, "Read error") c.Status(http.StatusInternalServerError) return } if len(rules) == 0 { c.JSON(http.StatusOK, nil) return } c.JSON(http.StatusOK, rules) } func (res RulesEngineService) readAllRulesInAllBanners(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") rules, err := res.RulesEngine.ReadRulesForAllBanners(c.Request.Context()) if err != nil { log.Error(err, "Error reading rules for all banners") c.Status(http.StatusInternalServerError) return } c.JSON(http.StatusOK, rules) } func (res RulesEngineService) postRulesInBanner(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") bannerName := c.Query("bannerName") log = log.WithValues("bannerName", bannerName) c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log)) if bannerName == "" { log.Error(fmt.Errorf("invalid parameter"), "request invalid") c.Status(http.StatusBadRequest) return } 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.AddBannerRules(c.Request.Context(), bannerName, rules) if err != nil { log.Error(err, "Error adding banner rules") c.Status(http.StatusInternalServerError) return } if len(result.Errors) > 0 { log.Error(fmt.Errorf("error adding rule"), "Requested data not found. No banner rule inserted") c.JSON(http.StatusNotFound, result) return } c.Status(http.StatusOK) } // Top level function for finding all banner rules limited to a single command. // Selects the appropriate query from the presence or absence of the bannerName // query param func (res RulesEngineService) readBannerRulesForCommand(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") _, ok := c.GetQuery("bannerName") if ok { log.Info("Querying for rules in one banner") res.readAllRulesInBannerForCommand(c) } else { log.Info("Querying for rules in all banners") res.readAllRulesInAllBannersForCommand(c) } } func (res RulesEngineService) readAllRulesInBannerForCommand(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") bannerName := c.Query("bannerName") commandName := c.Param("commandName") log = log.WithValues("bannerName", bannerName, "commandName", commandName) c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log)) if bannerName == "" { log.Error(fmt.Errorf("invalid parameter"), "request invalid") c.Status(http.StatusBadRequest) return } rules, err := res.RulesEngine.ReadBannerRulesForCommandAndBanner(c.Request.Context(), bannerName, commandName) if err != nil { log.Error(err, "Read error") c.Status(http.StatusInternalServerError) return } if (rules.Command == rulesengine.Command{} && len(rules.Privileges) == 0) { c.JSON(http.StatusOK, nil) return } c.JSON(http.StatusOK, rules) } func (res RulesEngineService) readAllRulesInAllBannersForCommand(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") commandName := c.Param("commandName") log = log.WithValues("commandName", commandName) c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log)) rules, err := res.RulesEngine.ReadBannerRulesForCommand(c.Request.Context(), commandName) if err != nil { log.Error(err, "Read error") c.Status(http.StatusInternalServerError) return } if (rules.Command == rulesengine.Command{} && len(rules.Banners) == 0) { c.JSON(http.StatusOK, nil) return } c.JSON(http.StatusOK, rules) } func (res RulesEngineService) deletePrivilegeFromBannerRule(c *gin.Context) { log := fog.FromContext(c).WithName("rulesengineservice") bannerName := c.Query("bannerName") commandName := c.Param("commandName") privilegeName := c.Param("privilegeName") log = log.WithValues("bannerName", bannerName, "commandName", commandName, "privilegeName", privilegeName) c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log)) if bannerName == "" || commandName == "" { log.Error(fmt.Errorf("invalid parameter"), "request invalid") c.Status(http.StatusBadRequest) return } result, err := res.RulesEngine.DeletePrivilegeFromBannerRule(c.Request.Context(), bannerName, commandName, privilegeName) if err != nil { log.Error(err, "Delete banner privilege error") c.Status(http.StatusInternalServerError) return } status := http.StatusOK if len(result.Errors) != 0 || result.RowsAffected == 0 { log.Error(fmt.Errorf("error deleting rule"), "Requested data not found. No deletion performed") c.JSON(http.StatusNotFound, result) } c.Status(status) }