File size: 3,643 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
package alert

import (
	"context"
	"database/sql"
	"encoding/json"
	"errors"

	"github.com/sqlc-dev/pqtype"
	"github.com/target/goalert/gadb"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/validation"
	"github.com/target/goalert/validation/validate"
)

const metaV1 = "alert_meta_v1"

type metadataDBFormat struct {
	Type        string
	AlertMetaV1 map[string]string
}

// Metadata returns the metadata for a single alert. If err == nil, meta is guaranteed to be non-nil. If the alert has no metadata, an empty map is returned.
func (s *Store) Metadata(ctx context.Context, db gadb.DBTX, alertID int) (meta map[string]string, err error) {
	err = permission.LimitCheckAny(ctx, permission.System, permission.User)
	if err != nil {
		return nil, err
	}

	md, err := gadb.New(db).Alert_GetAlertMetadata(ctx, int64(alertID))
	if errors.Is(err, sql.ErrNoRows) || !md.Valid {
		return map[string]string{}, nil
	}
	if err != nil {
		return nil, err
	}

	var doc metadataDBFormat
	err = json.Unmarshal(md.RawMessage, &doc)
	if err != nil {
		return nil, err
	}

	if doc.Type != metaV1 || doc.AlertMetaV1 == nil {
		return nil, errors.New("unsupported metadata type")
	}

	return doc.AlertMetaV1, nil
}

type MetadataAlertID struct {
	// AlertID is the ID of the alert.
	ID   int64
	Meta map[string]string
}

func (s Store) FindManyMetadata(ctx context.Context, db gadb.DBTX, alertIDs []int) ([]MetadataAlertID, error) {
	err := permission.LimitCheckAny(ctx, permission.System, permission.User)
	if err != nil {
		return nil, err
	}

	err = validate.Range("AlertIDs", len(alertIDs), 1, maxBatch)
	if err != nil {
		return nil, err
	}
	ids := make([]int64, len(alertIDs))
	for i, id := range alertIDs {
		ids[i] = int64(id)
	}

	rows, err := gadb.New(db).Alert_GetAlertManyMetadata(ctx, ids)
	if errors.Is(err, sql.ErrNoRows) {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}

	res := make([]MetadataAlertID, len(rows))
	for i, r := range rows {
		var doc metadataDBFormat
		err = json.Unmarshal(r.Metadata.RawMessage, &doc)
		if err != nil {
			return nil, err
		}

		if doc.Type != metaV1 || doc.AlertMetaV1 == nil {
			return nil, errors.New("unsupported metadata type")
		}

		res[i] = MetadataAlertID{
			ID:   r.AlertID,
			Meta: doc.AlertMetaV1,
		}
	}

	return res, nil
}

func (s Store) SetMetadataTx(ctx context.Context, db gadb.DBTX, alertID int, meta map[string]string) error {
	err := permission.LimitCheckAny(ctx, permission.User, permission.Service)
	if err != nil {
		return err
	}

	err = ValidateMetadata(meta)
	if err != nil {
		return err
	}

	var doc metadataDBFormat
	doc.Type = metaV1
	doc.AlertMetaV1 = meta

	md, err := json.Marshal(&doc)
	if err != nil {
		return err
	}

	rowCount, err := gadb.New(db).Alert_SetAlertMetadata(ctx, gadb.Alert_SetAlertMetadataParams{
		ID:        int64(alertID),
		ServiceID: permission.ServiceNullUUID(ctx), // only provide service_id restriction if request is from a service
		Metadata:  pqtype.NullRawMessage{Valid: true, RawMessage: json.RawMessage(md)},
	})
	if err != nil {
		return err
	}

	if rowCount == 0 {
		// shouldn't happen, but just in case
		return permission.NewAccessDenied("alert closed, invalid, or wrong service")
	}

	return nil
}

func ValidateMetadata(m map[string]string) error {
	if m == nil {
		return validation.NewFieldError("Meta", "cannot be nil")
	}

	var totalSize int
	for k, v := range m {
		err := validate.ASCII("Meta[<key>]", k, 1, 255)
		if err != nil {
			return err
		}

		totalSize += len(k) + len(v)
	}

	if totalSize > 32768 {
		return validation.NewFieldError("Meta", "cannot exceed 32KiB in size")
	}

	return nil
}