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 func (res RulesEngineService) readAllRulesForBanners(c *gin.Context) {
14 log := fog.FromContext(c).WithName("rulesengineservice")
15
16 _, ok := c.GetQuery("bannerName")
17 if ok {
18 log.Info("Querying for rules in one banner")
19 res.readAllRulesInBanner(c)
20 } else {
21 log.Info("Querying for rules in all banners")
22 res.readAllRulesInAllBanners(c)
23 }
24 }
25
26 func (res RulesEngineService) readAllRulesInBanner(c *gin.Context) {
27 log := fog.FromContext(c).WithName("rulesengineservice")
28
29 bannerName := c.Query("bannerName")
30
31 log = log.WithValues("bannerName", bannerName)
32 c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log))
33
34 if bannerName == "" {
35 log.Error(fmt.Errorf("invalid parameter"), "request invalid")
36 c.Status(http.StatusBadRequest)
37 return
38 }
39
40 rules, err := res.RulesEngine.ReadRulesForBanner(c.Request.Context(), bannerName)
41 if err != nil {
42 log.Error(err, "Read error")
43 c.Status(http.StatusInternalServerError)
44 return
45 }
46
47 if len(rules) == 0 {
48 c.JSON(http.StatusOK, nil)
49 return
50 }
51
52 c.JSON(http.StatusOK, rules)
53 }
54
55 func (res RulesEngineService) readAllRulesInAllBanners(c *gin.Context) {
56 log := fog.FromContext(c).WithName("rulesengineservice")
57
58 rules, err := res.RulesEngine.ReadRulesForAllBanners(c.Request.Context())
59 if err != nil {
60 log.Error(err, "Error reading rules for all banners")
61 c.Status(http.StatusInternalServerError)
62 return
63 }
64
65 c.JSON(http.StatusOK, rules)
66 }
67
68 func (res RulesEngineService) postRulesInBanner(c *gin.Context) {
69 log := fog.FromContext(c).WithName("rulesengineservice")
70
71 bannerName := c.Query("bannerName")
72
73 log = log.WithValues("bannerName", bannerName)
74 c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log))
75
76 if bannerName == "" {
77 log.Error(fmt.Errorf("invalid parameter"), "request invalid")
78 c.Status(http.StatusBadRequest)
79 return
80 }
81
82 var rules rulesengine.WriteRules
83 if err := c.ShouldBindJSON(&rules); err != nil {
84 log.Error(err, "bind error")
85 c.Status(http.StatusBadRequest)
86 return
87 }
88
89 if err := rules.Validate(); err != nil {
90 log.Error(err, "validation error")
91 c.Status(http.StatusBadRequest)
92 return
93 }
94
95 result, err := res.RulesEngine.AddBannerRules(c.Request.Context(), bannerName, rules)
96 if err != nil {
97 log.Error(err, "Error adding banner rules")
98 c.Status(http.StatusInternalServerError)
99 return
100 }
101
102 if len(result.Errors) > 0 {
103 log.Error(fmt.Errorf("error adding rule"), "Requested data not found. No banner rule inserted")
104 c.JSON(http.StatusNotFound, result)
105 return
106 }
107
108 c.Status(http.StatusOK)
109 }
110
111
112
113
114 func (res RulesEngineService) readBannerRulesForCommand(c *gin.Context) {
115 log := fog.FromContext(c).WithName("rulesengineservice")
116
117 _, ok := c.GetQuery("bannerName")
118 if ok {
119 log.Info("Querying for rules in one banner")
120 res.readAllRulesInBannerForCommand(c)
121 } else {
122 log.Info("Querying for rules in all banners")
123 res.readAllRulesInAllBannersForCommand(c)
124 }
125 }
126
127 func (res RulesEngineService) readAllRulesInBannerForCommand(c *gin.Context) {
128 log := fog.FromContext(c).WithName("rulesengineservice")
129
130 bannerName := c.Query("bannerName")
131 commandName := c.Param("commandName")
132
133 log = log.WithValues("bannerName", bannerName, "commandName", commandName)
134 c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log))
135
136 if bannerName == "" {
137 log.Error(fmt.Errorf("invalid parameter"), "request invalid")
138 c.Status(http.StatusBadRequest)
139 return
140 }
141
142 rules, err := res.RulesEngine.ReadBannerRulesForCommandAndBanner(c.Request.Context(), bannerName, commandName)
143 if err != nil {
144 log.Error(err, "Read error")
145 c.Status(http.StatusInternalServerError)
146 return
147 }
148
149 if (rules.Command == rulesengine.Command{} && len(rules.Privileges) == 0) {
150 c.JSON(http.StatusOK, nil)
151 return
152 }
153
154 c.JSON(http.StatusOK, rules)
155 }
156
157 func (res RulesEngineService) readAllRulesInAllBannersForCommand(c *gin.Context) {
158 log := fog.FromContext(c).WithName("rulesengineservice")
159
160 commandName := c.Param("commandName")
161
162 log = log.WithValues("commandName", commandName)
163 c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log))
164
165 rules, err := res.RulesEngine.ReadBannerRulesForCommand(c.Request.Context(), commandName)
166 if err != nil {
167 log.Error(err, "Read error")
168 c.Status(http.StatusInternalServerError)
169 return
170 }
171
172 if (rules.Command == rulesengine.Command{} && len(rules.Banners) == 0) {
173 c.JSON(http.StatusOK, nil)
174 return
175 }
176
177 c.JSON(http.StatusOK, rules)
178 }
179
180 func (res RulesEngineService) deletePrivilegeFromBannerRule(c *gin.Context) {
181 log := fog.FromContext(c).WithName("rulesengineservice")
182
183 bannerName := c.Query("bannerName")
184 commandName := c.Param("commandName")
185 privilegeName := c.Param("privilegeName")
186
187 log = log.WithValues("bannerName", bannerName, "commandName", commandName, "privilegeName", privilegeName)
188 c.Request = c.Request.Clone(fog.IntoContext(c.Request.Context(), log))
189
190 if bannerName == "" || commandName == "" {
191 log.Error(fmt.Errorf("invalid parameter"), "request invalid")
192 c.Status(http.StatusBadRequest)
193 return
194 }
195
196 result, err := res.RulesEngine.DeletePrivilegeFromBannerRule(c.Request.Context(), bannerName, commandName, privilegeName)
197 if err != nil {
198 log.Error(err, "Delete banner privilege error")
199 c.Status(http.StatusInternalServerError)
200 return
201 }
202
203 status := http.StatusOK
204 if len(result.Errors) != 0 || result.RowsAffected == 0 {
205 log.Error(fmt.Errorf("error deleting rule"), "Requested data not found. No deletion performed")
206 c.JSON(http.StatusNotFound, result)
207 }
208
209 c.Status(status)
210 }
211
View as plain text