...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/rules/server/rules.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/rules/server

     1  package server
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/gin-gonic/gin"
     8  
     9  	"edge-infra.dev/pkg/lib/fog"
    10  	rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules"
    11  )
    12  
    13  //nolint:dupl
    14  func (res RulesEngineService) postDefaultRules(c *gin.Context) {
    15  	log := fog.FromContext(c).WithName("rulesengineservice")
    16  
    17  	var rules rulesengine.WriteRules
    18  	if err := c.ShouldBindJSON(&rules); err != nil {
    19  		log.Error(err, "bind error")
    20  		c.Status(http.StatusBadRequest)
    21  		return
    22  	}
    23  	if err := rules.Validate(); err != nil {
    24  		log.Error(err, "validation error")
    25  		c.Status(http.StatusBadRequest)
    26  		return
    27  	}
    28  
    29  	result, err := res.RulesEngine.AddDefaultRules(c.Request.Context(), rules)
    30  	if err != nil {
    31  		log.Error(err, "Default rule addition error")
    32  		c.Status(http.StatusInternalServerError)
    33  		return
    34  	}
    35  
    36  	if len(result.Errors) > 0 {
    37  		log.Error(fmt.Errorf("error adding rule"), "Requested data not found. No rule inserted")
    38  		c.JSON(http.StatusNotFound, result)
    39  		return
    40  	}
    41  
    42  	c.Status(http.StatusOK)
    43  }
    44  
    45  func (res RulesEngineService) readAllDefaultRules(c *gin.Context) {
    46  	log := fog.FromContext(c).WithName("rulesengineservice")
    47  	vals, err := res.RulesEngine.ReadAllDefaultRules(c.Request.Context())
    48  	if err != nil {
    49  		log.Error(err, "read error")
    50  		c.Status(http.StatusInternalServerError)
    51  		return
    52  	}
    53  	if len(vals) == 0 {
    54  		log.Info("No default rules returned")
    55  		c.JSON(http.StatusOK, nil)
    56  		return
    57  	}
    58  	c.JSON(http.StatusOK, vals)
    59  }
    60  
    61  func (res RulesEngineService) readDefaultRule(c *gin.Context) {
    62  	log := fog.FromContext(c).WithName("rulesengineservice")
    63  	name := c.Param("commandName")
    64  	// TODO change name of this var
    65  	privs, err := res.RulesEngine.ReadDefaultRulesForCommand(c.Request.Context(), name)
    66  	if err != nil {
    67  		log.Error(err, "read error")
    68  		c.Status(http.StatusInternalServerError)
    69  		return
    70  	}
    71  	if len(privs) == 0 {
    72  		log.Info("No rule returned")
    73  		c.JSON(http.StatusOK, nil)
    74  		return
    75  	}
    76  	c.JSON(http.StatusOK, privs[0])
    77  }
    78  
    79  func (res RulesEngineService) deleteDefaultRule(c *gin.Context) {
    80  	log := fog.FromContext(c).WithName("rulesengineservice")
    81  	command, privilege := c.Param("commandName"), c.Param("privilegeName")
    82  
    83  	log = log.WithValues("commandName", command, "privilegeName", privilege)
    84  	c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log))
    85  
    86  	if command == "" {
    87  		log.Error(fmt.Errorf("invalid parameter"), "request invalid")
    88  		c.Status(http.StatusBadRequest)
    89  		return
    90  	}
    91  
    92  	result, err := res.RulesEngine.DeleteDefaultRule(c.Request.Context(), command, privilege)
    93  	if err != nil {
    94  		log.Error(err, "deletion error")
    95  		c.Status(http.StatusInternalServerError)
    96  		return
    97  	}
    98  	if len(result.Errors) > 0 {
    99  		for _, err := range result.Errors {
   100  			if err.Type == rulesengine.Conflict {
   101  				log.Error(fmt.Errorf("database query conflict"), "Conflict with database")
   102  				c.JSON(http.StatusConflict, result)
   103  				return
   104  			}
   105  		}
   106  
   107  		log.Error(fmt.Errorf("error deleting rule"), "Requested data not found. No deletion performed")
   108  		c.JSON(http.StatusNotFound, result)
   109  		return
   110  	}
   111  	c.Status(http.StatusOK)
   112  }
   113  
   114  func (res RulesEngineService) readAllRulesForCommand(c *gin.Context) {
   115  	log := fog.FromContext(c).WithName("rulesengineservice")
   116  	name := c.Param("commandName")
   117  	log = log.WithValues("commandName", name)
   118  	c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log))
   119  	rule, err := res.RulesEngine.ReadAllRulesForCommand(c, name)
   120  	if err != nil {
   121  		log.Error(err, "read error")
   122  		c.Status(http.StatusInternalServerError)
   123  		return
   124  	}
   125  	if rule.Command.ID == "" || rule.Command.Name == "" {
   126  		log.Info("command not listed in database")
   127  		c.JSON(http.StatusOK, nil)
   128  		return
   129  	}
   130  	c.JSON(http.StatusOK, rule)
   131  }
   132  

View as plain text