generated from mattermost/mattermost-plugin-starter-template
/
service.go
321 lines (265 loc) · 8.51 KB
/
service.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
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
312
313
314
315
316
317
318
319
320
321
package config
import (
"net"
"net/url"
"os"
"strings"
"sync"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
pluginapi "github.com/mattermost/mattermost-plugin-api"
"github.com/mattermost/mattermost-plugin-api/i18n"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/services/configservice"
"github.com/mattermost/mattermost-plugin-apps/server/telemetry"
"github.com/mattermost/mattermost-plugin-apps/upstream/upaws"
"github.com/mattermost/mattermost-plugin-apps/utils"
)
type Configurable interface {
Configure(Config, utils.Logger) error
}
type Service interface {
Get() Config
MattermostAPI() *pluginapi.Client
MattermostConfig() configservice.ConfigService
I18N() *i18n.Bundle
Telemetry() *telemetry.Telemetry
NewBaseLogger() utils.Logger
SystemDefaultFlags() (devMode, allowHTTPApps bool)
Reconfigure(_ StoredConfig, verbose bool, _ ...Configurable) error
StoreConfig(StoredConfig, utils.Logger) error
}
var _ Service = (*service)(nil)
type service struct {
pluginManifest model.Manifest
botUserID string
mm *pluginapi.Client
i18n *i18n.Bundle
telemetry *telemetry.Telemetry
lock *sync.RWMutex
conf *Config
mattermostConfig *model.Config
}
func MakeService(mm *pluginapi.Client, pliginManifest model.Manifest, botUserID string, telemetry *telemetry.Telemetry, i18nBundle *i18n.Bundle, log utils.Logger) (Service, error) {
s := &service{
pluginManifest: pliginManifest,
botUserID: botUserID,
mm: mm,
lock: &sync.RWMutex{},
i18n: i18nBundle,
telemetry: telemetry,
}
sc := StoredConfig{}
err := s.mm.Configuration.LoadPluginConfiguration(&sc)
if err != nil {
return nil, err
}
clone, err := s.newInitializedConfig(sc, log)
if err != nil {
return nil, err
}
s.lock.Lock()
s.conf = clone
s.lock.Unlock()
return s, nil
}
func (s *service) newConfig() Config {
return Config{
PluginManifest: s.pluginManifest,
BuildDate: BuildDate,
BuildHash: BuildHash,
BuildHashShort: BuildHashShort,
BotUserID: s.botUserID,
}
}
func (s *service) newInitializedConfig(newStoredConfig StoredConfig, log utils.Logger) (*Config, error) {
conf := s.newConfig()
conf.StoredConfig = newStoredConfig
newMattermostConfig := s.reloadMattermostConfig()
if conf.DeveloperModeOverride != nil {
conf.DeveloperMode = *conf.DeveloperModeOverride
} else {
conf.DeveloperMode = pluginapi.IsConfiguredForDevelopment(newMattermostConfig)
}
mattermostSiteURL := newMattermostConfig.ServiceSettings.SiteURL
if mattermostSiteURL == nil {
return nil, errors.New("plugin requires Mattermost Site URL to be set")
}
u, err := url.Parse(*mattermostSiteURL)
if err != nil {
return nil, errors.Wrap(err, "invalid SiteURL in config")
}
var localURL string
if newMattermostConfig.ServiceSettings.ConnectionSecurity != nil && *newMattermostConfig.ServiceSettings.ConnectionSecurity == model.ConnSecurityTLS {
// If there is no reverse proxy use the server URL
localURL = u.String()
} else {
// Avoid the reverse proxy by using the local port
listenAddress := newMattermostConfig.ServiceSettings.ListenAddress
if listenAddress == nil {
return nil, errors.New("plugin requires Mattermost Listen Address to be set")
}
host, port, err := net.SplitHostPort(*listenAddress)
if err != nil {
return nil, err
}
if host == "" {
host = "127.0.0.1"
}
localURL = "http://" + host + ":" + port + u.Path
}
conf.MattermostSiteURL = u.String()
conf.MattermostLocalURL = localURL
conf.PluginURLPath = "/plugins/" + conf.PluginManifest.Id
conf.PluginURL = strings.TrimRight(u.String(), "/") + conf.PluginURLPath
conf.MaxWebhookSize = 75 * 1024 * 1024 // 75Mb
if newMattermostConfig.FileSettings.MaxFileSize != nil {
conf.MaxWebhookSize = int(*newMattermostConfig.FileSettings.MaxFileSize)
}
conf.AWSAccessKey = os.Getenv(upaws.AccessEnvVar)
conf.AWSSecretKey = os.Getenv(upaws.SecretEnvVar)
conf.AWSRegion = upaws.Region()
conf.AWSS3Bucket = upaws.S3BucketName()
license := s.getMattermostLicense(log)
conf.MattermostCloudMode = license != nil &&
license.Features != nil &&
license.Features.Cloud != nil &&
*license.Features.Cloud
if conf.MattermostCloudMode {
log.Debugf("Detected Mattermost Cloud mode based on the license")
}
// On community.mattermost.com license is not suitable for checking, resort
// to the presence of legacy environment variable to trigger it.
legacyAccessKey := os.Getenv(upaws.DeprecatedCloudAccessEnvVar)
if legacyAccessKey != "" {
conf.MattermostCloudMode = true
log.Debugf("Detected Mattermost Cloud mode based on the %s variable", upaws.DeprecatedCloudAccessEnvVar)
conf.AWSAccessKey = legacyAccessKey
}
if conf.MattermostCloudMode {
legacySecretKey := os.Getenv(upaws.DeprecatedCloudSecretEnvVar)
if legacySecretKey != "" {
conf.AWSSecretKey = legacySecretKey
}
if conf.AWSAccessKey == "" || conf.AWSSecretKey == "" {
return nil, errors.New("access credentials for AWS must be set in Mattermost Cloud mode")
}
}
if conf.AllowHTTPAppsOverride != nil {
conf.AllowHTTPApps = *conf.AllowHTTPAppsOverride
} else {
conf.AllowHTTPApps = !conf.MattermostCloudMode || conf.DeveloperMode
}
return &conf, nil
}
func (s *service) Get() Config {
s.lock.RLock()
conf := s.conf
s.lock.RUnlock()
if conf == nil {
return s.newConfig()
}
return *conf
}
func (s *service) MattermostAPI() *pluginapi.Client {
return s.mm
}
func (s *service) I18N() *i18n.Bundle {
return s.i18n
}
func (s *service) Telemetry() *telemetry.Telemetry {
return s.telemetry
}
func (s *service) MattermostConfig() configservice.ConfigService {
s.lock.RLock()
mmconf := s.mattermostConfig
s.lock.RUnlock()
if mmconf == nil {
mmconf = s.reloadMattermostConfig()
}
return &mattermostConfigService{
mmconf: mmconf,
}
}
func (s *service) reloadMattermostConfig() *model.Config {
mmconf := s.mm.Configuration.GetConfig()
s.lock.Lock()
s.mattermostConfig = mmconf
s.lock.Unlock()
return mmconf
}
func (s *service) getMattermostLicense(log utils.Logger) *model.License {
// GetLicense silently drops an RPC error
// (https://github.com/mattermost/mattermost-server/blob/fc75b72bbabf7fabfad24b9e1e4c321ca9b9b7f1/plugin/client_rpc_generated.go#L864).
// When running in Mattermost cloud we must not fall back to the on-prem mode, so in case we get a nil retry once.
license := s.mm.System.GetLicense()
if license == nil {
license = s.mm.System.GetLicense()
if license == nil {
log.Debugf("failed to fetch license twice. May incorrectly default to on-prem mode")
}
}
return license
}
func (s *service) Reconfigure(newStoredConfig StoredConfig, verbose bool, services ...Configurable) error {
var log utils.Logger
if verbose {
// Use the old settings to log the processing of the new config.
log = s.NewBaseLogger()
} else {
log = utils.NilLogger{}
}
clone, err := s.newInitializedConfig(newStoredConfig, log)
if err != nil {
return err
}
s.lock.Lock()
s.conf = clone
s.lock.Unlock()
for _, service := range services {
if err := service.Configure(*clone, log); err != nil {
return errors.Wrapf(err, "failed to configure service %T", service)
}
}
return nil
}
func (s *service) StoreConfig(sc StoredConfig, log utils.Logger) error {
log.Debugf("Storing configuration: %d installed, %d listed apps, developer mode %t, allow http apps %t",
len(sc.InstalledApps),
len(sc.LocalManifests),
sc.DeveloperModeOverride != nil && *sc.DeveloperModeOverride,
sc.AllowHTTPAppsOverride != nil && *sc.AllowHTTPAppsOverride,
)
// Refresh computed values immediately, do not wait for OnConfigurationChanged
err := s.Reconfigure(sc, false)
if err != nil {
return err
}
out := map[string]interface{}{}
utils.Remarshal(&out, sc)
// TODO test that SaveConfig will always cause OnConfigurationChange->c.Refresh
return s.mm.Configuration.SavePluginConfig(out)
}
func (s *service) NewBaseLogger() utils.Logger {
if s.Get().DeveloperMode {
return utils.NewPluginLogger(s.mm, s)
}
return utils.NewPluginLogger(s.mm, nil)
}
func (s *service) GetLogConfig() utils.LogConfig {
conf := s.Get()
return utils.LogConfig{
ChannelID: conf.LogChannelID,
Level: zapcore.Level(conf.LogChannelLevel),
BotUserID: s.botUserID,
IncludeJSON: conf.LogChannelJSON,
}
}
func (s *service) SystemDefaultFlags() (bool, bool) {
s.lock.RLock()
mmconf := s.mattermostConfig
s.lock.RUnlock()
devMode := pluginapi.IsConfiguredForDevelopment(mmconf)
allowHTTP := devMode || !s.Get().MattermostCloudMode
return devMode, allowHTTP
}