File size: 4,206 Bytes
1e92f2d |
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
package uik
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/expr-lang/expr/vm"
"github.com/google/uuid"
"github.com/target/goalert/alert"
"github.com/target/goalert/event"
"github.com/target/goalert/expflag"
"github.com/target/goalert/gadb"
"github.com/target/goalert/integrationkey"
"github.com/target/goalert/permission"
"github.com/target/goalert/util/errutil"
"github.com/target/goalert/validation"
)
type Handler struct {
intStore *integrationkey.Store
alertStore *alert.Store
db TxAble
evt *event.Bus
}
type TxAble interface {
gadb.DBTX
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}
func NewHandler(db TxAble, intStore *integrationkey.Store, aStore *alert.Store, evt *event.Bus) *Handler {
return &Handler{intStore: intStore, db: db, alertStore: aStore, evt: evt}
}
func (h *Handler) handleAction(ctx context.Context, act gadb.UIKActionV1) (inserted bool, err error) {
var didInsertSignals bool
switch act.Dest.Type {
case "builtin-webhook":
req, err := http.NewRequest("POST", act.Dest.Arg("webhook_url"), strings.NewReader(act.Param("body")))
if err != nil {
return false, err
}
req.Header.Set("Content-Type", act.Param("content-type"))
_, err = http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return false, err
}
case "builtin-alert":
status := alert.StatusTriggered
if act.Param("close") == "true" {
status = alert.StatusClosed
}
_, _, err := h.alertStore.CreateOrUpdate(ctx, &alert.Alert{
ServiceID: permission.ServiceID(ctx),
Summary: act.Param("summary"),
Details: act.Param("details"),
Source: alert.SourceUniversal,
Status: status,
})
if err != nil {
return false, err
}
default:
data, err := json.Marshal(act.Params)
if err != nil {
return false, err
}
err = gadb.New(h.db).IntKeyInsertSignalMessage(ctx, gadb.IntKeyInsertSignalMessageParams{
DestID: act.ChannelID,
ServiceID: permission.ServiceNullUUID(ctx).UUID,
Params: data,
})
if err != nil {
return false, err
}
didInsertSignals = true
}
return didInsertSignals, nil
}
// EventNewSignals is an event that is triggered when new signals are generated for a service.
type EventNewSignals struct {
ServiceID uuid.UUID
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
if !expflag.ContextHas(ctx, expflag.UnivKeys) {
errutil.HTTPError(ctx, w, validation.NewGenericError("universal keys are disabled"))
return
}
err := permission.LimitCheckAny(req.Context(), permission.Service)
if errutil.HTTPError(ctx, w, err) {
return
}
src := permission.Source(ctx)
if src.Type != permission.SourceTypeUIK {
// we don't want to allow regular API keys to be used here
errutil.HTTPError(ctx, w, permission.Unauthorized())
return
}
keyID, err := uuid.Parse(src.ID)
if errutil.HTTPError(ctx, w, err) {
return
}
data, err := io.ReadAll(req.Body)
if errutil.HTTPError(ctx, w, err) {
return
}
var body any
err = json.Unmarshal(data, &body)
if errutil.HTTPError(ctx, w, validation.WrapError(err)) {
return
}
cfg, err := h.intStore.Config(ctx, h.db, keyID)
if errutil.HTTPError(ctx, w, err) {
return
}
// TODO: cache
compiled, err := NewCompiledConfig(*cfg)
if errutil.HTTPError(ctx, w, err) {
return
}
q := req.URL.Query()
query := make(map[string]string)
for key := range q {
query[key] = q.Get(key)
}
querya := map[string][]string(q)
env := map[string]any{
"sprintf": fmt.Sprintf,
"req": map[string]any{
"body": body,
"query": query,
"querya": querya,
"ua": req.UserAgent(),
"ip": req.RemoteAddr,
},
}
var vm vm.VM
actions, err := compiled.Run(&vm, env)
if errutil.HTTPError(ctx, w, validation.WrapError(err)) {
return
}
var insertedAny bool
for _, act := range actions {
inserted, err := h.handleAction(ctx, act)
if errutil.HTTPError(ctx, w, err) {
return
}
insertedAny = insertedAny || inserted
}
if insertedAny {
event.Send(ctx, h.evt, EventNewSignals{ServiceID: permission.ServiceNullUUID(ctx).UUID})
}
w.WriteHeader(http.StatusNoContent)
}
|