File size: 7,519 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
package graphqlapp
import (
context "context"
"database/sql"
"net/url"
"github.com/google/uuid"
"github.com/target/goalert/config"
"github.com/target/goalert/gadb"
"github.com/target/goalert/graphql2"
"github.com/target/goalert/integrationkey"
"github.com/target/goalert/search"
"github.com/target/goalert/validation"
"github.com/target/goalert/validation/validate"
)
type IntegrationKey App
type KeyConfig App
func (a *App) IntegrationKey() graphql2.IntegrationKeyResolver { return (*IntegrationKey)(a) }
func (a *App) KeyConfig() graphql2.KeyConfigResolver { return (*KeyConfig)(a) }
func (k *KeyConfig) OneRule(ctx context.Context, key *gadb.UIKConfigV1, ruleID string) (*gadb.UIKRuleV1, error) {
for _, r := range key.Rules {
if r.ID.String() == ruleID {
return &r, nil
}
}
return nil, validation.NewFieldError("RuleID", "not found")
}
func (q *Query) IntegrationKey(ctx context.Context, id string) (*integrationkey.IntegrationKey, error) {
return q.IntKeyStore.FindOne(ctx, id)
}
func (q *Query) ActionInputValidate(ctx context.Context, input gadb.UIKActionV1) (bool, error) {
err := (*App)(q).ValidateDestination(ctx, "input.dest", &input.Dest)
if err != nil {
return false, err
}
return true, nil
}
func (m *Mutation) GenerateKeyToken(ctx context.Context, keyID string) (string, error) {
id, err := validate.ParseUUID("ID", keyID)
if err != nil {
return "", err
}
return m.IntKeyStore.GenerateToken(ctx, m.DB, id)
}
func (m *Mutation) DeleteSecondaryToken(ctx context.Context, keyID string) (bool, error) {
id, err := validate.ParseUUID("ID", keyID)
if err != nil {
return false, err
}
err = m.IntKeyStore.DeleteSecondaryToken(ctx, m.DB, id)
if err != nil {
return false, err
}
return true, nil
}
func (m *Mutation) PromoteSecondaryToken(ctx context.Context, keyID string) (bool, error) {
id, err := validate.ParseUUID("ID", keyID)
if err != nil {
return false, err
}
err = m.IntKeyStore.PromoteSecondaryToken(ctx, m.DB, id)
if err != nil {
return false, err
}
return true, nil
}
func (key *IntegrationKey) TokenInfo(ctx context.Context, raw *integrationkey.IntegrationKey) (*graphql2.TokenInfo, error) {
id, err := validate.ParseUUID("ID", raw.ID)
if err != nil {
return nil, err
}
prim, sec, err := key.IntKeyStore.TokenHints(ctx, key.DB, id)
if err != nil {
return nil, err
}
return &graphql2.TokenInfo{
PrimaryHint: prim,
SecondaryHint: sec,
}, nil
}
func (m *Mutation) UpdateKeyConfig(ctx context.Context, input graphql2.UpdateKeyConfigInput) (bool, error) {
err := withContextTx(ctx, m.DB, func(ctx context.Context, tx *sql.Tx) error {
id, err := validate.ParseUUID("IntegrationKey.ID", input.KeyID)
if err != nil {
return err
}
cfg, err := m.IntKeyStore.Config(ctx, tx, id)
if err != nil {
return err
}
if input.Rules != nil {
cfg.Rules = input.Rules
}
if input.SetRule != nil {
if input.SetRule.ID == uuid.Nil {
// Since we don't have a rule ID, we're need to create a new rule.
cfg.Rules = append(cfg.Rules, *input.SetRule)
} else {
var found bool
for i, r := range cfg.Rules {
if r.ID == input.SetRule.ID {
cfg.Rules[i] = *input.SetRule
found = true
break
}
}
if !found {
return validation.NewFieldError("SetRule.ID", "not found")
}
}
}
if input.SetRuleActions != nil {
var found bool
for i, r := range cfg.Rules {
if r.ID != input.SetRuleActions.ID {
continue
}
cfg.Rules[i].Actions = input.SetRuleActions.Actions
found = true
break
}
if !found {
return validation.NewFieldError("SetRuleActions.RuleID", "not found")
}
}
if input.DeleteRule != nil {
for i, r := range cfg.Rules {
if r.ID.String() == *input.DeleteRule {
cfg.Rules = append(cfg.Rules[:i], cfg.Rules[i+1:]...)
break
}
}
}
if input.SetRuleOrder != nil {
rules := make(map[string]gadb.UIKRuleV1, len(cfg.Rules))
for _, r := range cfg.Rules {
rules[r.ID.String()] = r
}
cfg.Rules = cfg.Rules[:0]
for _, id := range input.SetRuleOrder {
if r, ok := rules[id]; ok {
cfg.Rules = append(cfg.Rules, r)
}
}
}
if input.DefaultActions != nil {
cfg.DefaultActions = input.DefaultActions
}
err = m.IntKeyStore.SetConfig(ctx, tx, id, cfg)
return err
})
if err != nil {
return false, err
}
return true, nil
}
func (m *Mutation) CreateIntegrationKey(ctx context.Context, input graphql2.CreateIntegrationKeyInput) (key *integrationkey.IntegrationKey, err error) {
var serviceID string
if input.ServiceID != nil {
serviceID = *input.ServiceID
}
err = withContextTx(ctx, m.DB, func(ctx context.Context, tx *sql.Tx) error {
key = &integrationkey.IntegrationKey{
ServiceID: serviceID,
Name: input.Name,
Type: integrationkey.Type(input.Type),
}
if input.ExternalSystemName != nil {
key.ExternalSystemName = *input.ExternalSystemName
}
key, err = m.IntKeyStore.Create(ctx, tx, key)
return err
})
return key, err
}
func (key *IntegrationKey) Config(ctx context.Context, raw *integrationkey.IntegrationKey) (*gadb.UIKConfigV1, error) {
id, err := validate.ParseUUID("IntegrationKey.ID", raw.ID)
if err != nil {
return nil, err
}
return key.IntKeyStore.Config(ctx, key.DB, id)
}
func (key *IntegrationKey) Type(ctx context.Context, raw *integrationkey.IntegrationKey) (graphql2.IntegrationKeyType, error) {
return graphql2.IntegrationKeyType(raw.Type), nil
}
func (key *IntegrationKey) Href(ctx context.Context, raw *integrationkey.IntegrationKey) (string, error) {
cfg := config.FromContext(ctx)
q := make(url.Values)
q.Set("token", raw.ID)
switch raw.Type {
case integrationkey.TypeGeneric:
return cfg.CallbackURL("/api/v2/generic/incoming", q), nil
case integrationkey.TypeGrafana:
return cfg.CallbackURL("/api/v2/grafana/incoming", q), nil
case integrationkey.TypeSite24x7:
return cfg.CallbackURL("/api/v2/site24x7/incoming", q), nil
case integrationkey.TypePrometheusAlertmanager:
return cfg.CallbackURL("/api/v2/prometheusalertmanager/incoming", q), nil
case integrationkey.TypeEmail:
if !cfg.EmailIngressEnabled() {
return "", nil
}
return "mailto:" + raw.ID + "@" + cfg.EmailIngressDomain(), nil
case integrationkey.TypeUniversal:
return cfg.CallbackURL("/api/v2/uik"), nil
}
return "", nil
}
func (q *Query) IntegrationKeys(ctx context.Context, input *graphql2.IntegrationKeySearchOptions) (conn *graphql2.IntegrationKeyConnection, err error) {
if input == nil {
input = &graphql2.IntegrationKeySearchOptions{}
}
var opts integrationkey.InKeySearchOptions
if input.Search != nil {
opts.Search = *input.Search
}
opts.Omit = input.Omit
if input.After != nil && *input.After != "" {
err = search.ParseCursor(*input.After, &opts)
if err != nil {
return conn, err
}
}
if input.First != nil {
opts.Limit = *input.First
}
if opts.Limit == 0 {
opts.Limit = 15
}
opts.Limit++
intKeys, err := q.IntKeyStore.Search(ctx, &opts)
if err != nil {
return nil, err
}
conn = new(graphql2.IntegrationKeyConnection)
conn.PageInfo = &graphql2.PageInfo{}
if len(intKeys) == opts.Limit {
intKeys = intKeys[:len(intKeys)-1]
conn.PageInfo.HasNextPage = true
}
if len(intKeys) > 0 {
lastKey := intKeys[len(intKeys)-1]
opts.After = lastKey.Name
cur, err := search.Cursor(opts)
if err != nil {
return nil, err
}
conn.PageInfo.EndCursor = &cur
}
conn.Nodes = intKeys
return conn, err
}
|