-
Notifications
You must be signed in to change notification settings - Fork 11
/
actions.go
128 lines (102 loc) · 3.36 KB
/
actions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package api
import (
"net/http"
"time"
"github.com/common-fate/iamzero/api/io"
"github.com/common-fate/iamzero/pkg/recommendations"
"github.com/go-chi/chi"
)
type ActionResponse struct {
ID string `json:"id"`
PolicyID string `json:"policyId"`
Event recommendations.AWSEvent `json:"event"`
Status string `json:"status"`
Time time.Time `json:"time"`
Resources []recommendations.Resource `json:"resources"`
Recommendations []recommendations.RecommendationDetails `json:"recommendations"`
HasRecommendations bool `json:"hasRecommendations"`
}
// buildActionResponse loops through the advisories associated with an action
// to build a response
func buildActionResponse(action recommendations.AWSAction) ActionResponse {
var detailsArr []recommendations.RecommendationDetails
for _, rec := range action.Recommendations {
details := rec.Details()
detailsArr = append(detailsArr, details)
}
return ActionResponse{
ID: action.ID,
PolicyID: action.PolicyID,
Event: action.Event,
Status: action.Status,
Resources: action.Resources,
Time: action.Time,
Recommendations: detailsArr,
HasRecommendations: action.HasRecommendations,
}
}
func (h *Handlers) ListActions(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
actionsResponse := []ActionResponse{}
actions := h.ActionStorage.List()
for _, action := range actions {
res := buildActionResponse(action)
actionsResponse = append(actionsResponse, res)
}
io.RespondJSON(ctx, h.Log, w, actionsResponse, http.StatusOK)
}
func (h *Handlers) GetAction(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
actionID := chi.URLParam(r, "actionID")
action := h.ActionStorage.Get(actionID)
if action == nil {
http.Error(w, "action not found", http.StatusNotFound)
return
}
res := buildActionResponse(*action)
io.RespondJSON(ctx, h.Log, w, res, http.StatusOK)
}
type editActionBody struct {
Enabled *bool `json:"enabled"`
SelectedAdvisoryID *string `json:"selectedAdvisoryId"`
}
func (h *Handlers) EditAction(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
actionID := chi.URLParam(r, "actionID")
var b editActionBody
if err := io.DecodeJSONBody(w, r, &b); err != nil {
io.RespondError(ctx, h.Log, w, err)
return
}
action := h.ActionStorage.Get(actionID)
if action == nil {
io.RespondText(ctx, h.Log, w, "action not found", http.StatusNotFound)
return
}
policy := h.PolicyStorage.Get(action.PolicyID)
if policy == nil {
io.RespondText(ctx, h.Log, w, "policy not found", http.StatusNotFound)
return
}
if b.Enabled != nil {
action.Enabled = *b.Enabled
}
if b.SelectedAdvisoryID != nil {
if err := action.SelectAdvisory(*b.SelectedAdvisoryID); err != nil {
io.RespondError(ctx, h.Log, w, err)
return
}
}
if err := h.ActionStorage.Update(*action); err != nil {
io.RespondError(ctx, h.Log, w, err)
return
}
// return the updated Policy corresponding to this alert
actions := h.ActionStorage.ListForPolicy(policy.ID)
policy.RecalculateDocument(actions)
if err := h.PolicyStorage.CreateOrUpdate(*policy); err != nil {
io.RespondError(ctx, h.Log, w, err)
return
}
io.RespondJSON(ctx, h.Log, w, policy, http.StatusOK)
}