File size: 8,405 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package app

import (
	"context"
	"net/url"

	"github.com/target/goalert/alert"
	"github.com/target/goalert/alert/alertlog"
	"github.com/target/goalert/alert/alertmetrics"
	"github.com/target/goalert/apikey"
	"github.com/target/goalert/auth/authlink"
	"github.com/target/goalert/auth/basic"
	"github.com/target/goalert/auth/nonce"
	"github.com/target/goalert/calsub"
	"github.com/target/goalert/config"
	"github.com/target/goalert/escalation"
	"github.com/target/goalert/heartbeat"
	"github.com/target/goalert/integrationkey"
	"github.com/target/goalert/integrationkey/uik"
	"github.com/target/goalert/keyring"
	"github.com/target/goalert/label"
	"github.com/target/goalert/limit"
	"github.com/target/goalert/notice"
	"github.com/target/goalert/notification"
	"github.com/target/goalert/notification/nfydest"
	"github.com/target/goalert/notification/slack"
	"github.com/target/goalert/notificationchannel"
	"github.com/target/goalert/oncall"
	"github.com/target/goalert/override"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/schedule"
	"github.com/target/goalert/schedule/rotation"
	"github.com/target/goalert/schedule/rule"
	"github.com/target/goalert/service"
	"github.com/target/goalert/timezone"
	"github.com/target/goalert/user"
	"github.com/target/goalert/user/contactmethod"
	"github.com/target/goalert/user/favorite"
	"github.com/target/goalert/user/notificationrule"

	"github.com/pkg/errors"
)

func (app *App) initStores(ctx context.Context) error {
	var err error

	app.DestRegistry = nfydest.NewRegistry()

	if app.ConfigStore == nil {
		var fallback url.URL
		fallback.Scheme = "http"
		fallback.Host = app.l.Addr().String()
		fallback.Path = app.cfg.HTTPPrefix
		storeCfg := config.StoreConfig{
			DB:                 app.db,
			Keys:               app.cfg.EncryptionKeys,
			FallbackURL:        fallback.String(),
			ExplicitURL:        app.cfg.PublicURL,
			IngressEmailDomain: app.cfg.EmailIntegrationDomain,
		}
		app.ConfigStore, err = config.NewStore(ctx, storeCfg)
	}
	if err != nil {
		return errors.Wrap(err, "init config store")
	}
	if app.cfg.InitialConfig != nil {
		permission.SudoContext(ctx, func(ctx context.Context) {
			err = app.ConfigStore.SetConfig(ctx, *app.cfg.InitialConfig)
		})
		if err != nil {
			return errors.Wrap(err, "set initial config")
		}
	}

	if app.NonceStore == nil {
		app.NonceStore, err = nonce.NewStore(ctx, app.cfg.LegacyLogger, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init nonce store")
	}

	if app.OAuthKeyring == nil {
		app.OAuthKeyring, err = keyring.NewDB(ctx, app.cfg.LegacyLogger, app.db, &keyring.Config{
			Name:         "oauth-state",
			RotationDays: 1,
			MaxOldKeys:   1,
			Keys:         app.cfg.EncryptionKeys,
		})
	}
	if err != nil {
		return errors.Wrap(err, "init oauth state keyring")
	}

	if app.AuthLinkKeyring == nil {
		app.AuthLinkKeyring, err = keyring.NewDB(ctx, app.cfg.LegacyLogger, app.db, &keyring.Config{
			Name:         "auth-link",
			RotationDays: 1,
			MaxOldKeys:   1,
			Keys:         app.cfg.EncryptionKeys,
		})
	}
	if err != nil {
		return errors.Wrap(err, "init oauth state keyring")
	}

	if app.SessionKeyring == nil {
		app.SessionKeyring, err = keyring.NewDB(ctx, app.cfg.LegacyLogger, app.db, &keyring.Config{
			Name:         "browser-sessions",
			RotationDays: 1,
			MaxOldKeys:   30,
			Keys:         app.cfg.EncryptionKeys,
		})
	}
	if err != nil {
		return errors.Wrap(err, "init session keyring")
	}

	if app.APIKeyring == nil {
		app.APIKeyring, err = keyring.NewDB(ctx, app.cfg.LegacyLogger, app.db, &keyring.Config{
			Name:       "api-keys",
			MaxOldKeys: 100,
			Keys:       app.cfg.EncryptionKeys,
		})
	}
	if err != nil {
		return errors.Wrap(err, "init API keyring")
	}

	if app.AuthLinkStore == nil {
		app.AuthLinkStore, err = authlink.NewStore(ctx, app.db, app.AuthLinkKeyring)
	}
	if err != nil {
		return errors.Wrap(err, "init auth link store")
	}

	if app.AlertMetricsStore == nil {
		app.AlertMetricsStore, err = alertmetrics.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init alert metrics store")
	}

	if app.AlertLogStore == nil {
		app.AlertLogStore, err = alertlog.NewStore(ctx, app.db, app.DestRegistry)
	}
	if err != nil {
		return errors.Wrap(err, "init alertlog store")
	}

	if app.AlertStore == nil {
		app.AlertStore, err = alert.NewStore(ctx, app.db, app.AlertLogStore, app.EventBus)
	}
	if err != nil {
		return errors.Wrap(err, "init alert store")
	}

	if app.ContactMethodStore == nil {
		app.ContactMethodStore = contactmethod.NewStore(app.DestRegistry)
	}

	if app.NotificationRuleStore == nil {
		app.NotificationRuleStore, err = notificationrule.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init notification rule store")
	}

	if app.ServiceStore == nil {
		app.ServiceStore, err = service.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init service store")
	}

	if app.AuthBasicStore == nil {
		app.AuthBasicStore, err = basic.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init basic auth store")
	}

	if app.UserStore == nil {
		app.UserStore, err = user.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init user store")
	}

	if app.ScheduleStore == nil {
		app.ScheduleStore, err = schedule.NewStore(ctx, app.db, app.UserStore)
	}
	if err != nil {
		return errors.Wrap(err, "init schedule store")
	}

	if app.RotationStore == nil {
		app.RotationStore, err = rotation.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init rotation store")
	}

	if app.NCStore == nil {
		app.NCStore, err = notificationchannel.NewStore(ctx, app.db, app.DestRegistry)
	}
	if err != nil {
		return errors.Wrap(err, "init notification channel store")
	}

	if app.EscalationStore == nil {
		app.EscalationStore, err = escalation.NewStore(ctx, app.db, escalation.Config{
			LogStore: app.AlertLogStore,
			NCStore:  app.NCStore,
			Registry: app.DestRegistry,
			SlackLookupFunc: func(ctx context.Context, channelID string) (*slack.Channel, error) {
				return app.slackChan.Channel(ctx, channelID)
			},
		})
	}
	if err != nil {
		return errors.Wrap(err, "init escalation policy store")
	}

	if app.IntegrationKeyStore == nil {
		app.IntegrationKeyStore = integrationkey.NewStore(ctx, app.db, app.APIKeyring, app.DestRegistry, app.NCStore)
	}

	if app.ScheduleRuleStore == nil {
		app.ScheduleRuleStore, err = rule.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init schedule rule store")
	}

	if app.NotificationStore == nil {
		app.NotificationStore, err = notification.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init notification store")
	}

	if app.FavoriteStore == nil {
		app.FavoriteStore, err = favorite.NewStore(ctx)
	}
	if err != nil {
		return errors.Wrap(err, "init favorite store")
	}

	if app.OverrideStore == nil {
		app.OverrideStore, err = override.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init override store")
	}

	if app.LimitStore == nil {
		app.LimitStore, err = limit.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init limit config store")
	}
	if app.HeartbeatStore == nil {
		app.HeartbeatStore, err = heartbeat.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init heartbeat store")
	}
	if app.LabelStore == nil {
		app.LabelStore, err = label.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init label store")
	}

	if app.OnCallStore == nil {
		app.OnCallStore, err = oncall.NewStore(ctx, app.db, app.ScheduleRuleStore, app.ScheduleStore)
	}
	if err != nil {
		return errors.Wrap(err, "init on-call store")
	}

	if app.TimeZoneStore == nil {
		app.TimeZoneStore = timezone.NewStore(ctx, app.db)
	}

	if app.CalSubStore == nil {
		app.CalSubStore, err = calsub.NewStore(ctx, app.db, app.APIKeyring, app.OnCallStore)
	}
	if err != nil {
		return errors.Wrap(err, "init calendar subscription store")
	}

	if app.NoticeStore == nil {
		app.NoticeStore, err = notice.NewStore(ctx, app.db)
	}
	if err != nil {
		return errors.Wrap(err, "init notice store")
	}

	if app.APIKeyStore == nil {
		app.APIKeyStore, err = apikey.NewStore(ctx, app.db, app.APIKeyring)
	}
	if err != nil {
		return errors.Wrap(err, "init API key store")
	}

	app.UIKHandler = uik.NewHandler(app.db, app.IntegrationKeyStore, app.AlertStore, app.EventBus)

	return nil
}