Files
mattermost/app/security_update_check.go
Jesús Espino 9cc5089af8 Moving mlog to corelibs (#16915)
* Moving mlog to corelibs

* Regenerating app layers

* Fix golangci-lint problem

* Fixing golangci-lint errors

* Renaming from corelibs to shared

* Renaming from corelibs to shared

* Fixing import

* Fixing merge problems

* Fixing build
2021-03-05 09:18:37 +01:00

129 lines
4.0 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"io/ioutil"
"net/http"
"net/url"
"runtime"
"strconv"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/services/mailservice"
"github.com/mattermost/mattermost-server/v5/shared/i18n"
"github.com/mattermost/mattermost-server/v5/shared/mlog"
)
const (
PropSecurityURL = "https://securityupdatecheck.mattermost.com"
SecurityUpdatePeriod = 86400000 // 24 hours in milliseconds.
PropSecurityID = "id"
PropSecurityBuild = "b"
PropSecurityEnterpriseReady = "be"
PropSecurityDatabase = "db"
PropSecurityOS = "os"
PropSecurityUserCount = "uc"
PropSecurityTeamCount = "tc"
PropSecurityActiveUserCount = "auc"
PropSecurityUnitTests = "ut"
)
func (s *Server) DoSecurityUpdateCheck() {
if !*s.Config().ServiceSettings.EnableSecurityFixAlert {
return
}
props, err := s.Store.System().Get()
if err != nil {
return
}
lastSecurityTime, _ := strconv.ParseInt(props[model.SYSTEM_LAST_SECURITY_TIME], 10, 0)
currentTime := model.GetMillis()
if (currentTime - lastSecurityTime) > SecurityUpdatePeriod {
mlog.Debug("Checking for security update from Mattermost")
v := url.Values{}
v.Set(PropSecurityID, s.TelemetryId())
v.Set(PropSecurityBuild, model.CurrentVersion+"."+model.BuildNumber)
v.Set(PropSecurityEnterpriseReady, model.BuildEnterpriseReady)
v.Set(PropSecurityDatabase, *s.Config().SqlSettings.DriverName)
v.Set(PropSecurityOS, runtime.GOOS)
if props[model.SYSTEM_RAN_UNIT_TESTS] != "" {
v.Set(PropSecurityUnitTests, "1")
} else {
v.Set(PropSecurityUnitTests, "0")
}
systemSecurityLastTime := &model.System{Name: model.SYSTEM_LAST_SECURITY_TIME, Value: strconv.FormatInt(currentTime, 10)}
if lastSecurityTime == 0 {
s.Store.System().Save(systemSecurityLastTime)
} else {
s.Store.System().Update(systemSecurityLastTime)
}
if count, err := s.Store.User().Count(model.UserCountOptions{IncludeDeleted: true}); err == nil {
v.Set(PropSecurityUserCount, strconv.FormatInt(count, 10))
}
if ucr, err := s.Store.Status().GetTotalActiveUsersCount(); err == nil {
v.Set(PropSecurityActiveUserCount, strconv.FormatInt(ucr, 10))
}
if teamCount, err := s.Store.Team().AnalyticsTeamCount(false); err == nil {
v.Set(PropSecurityTeamCount, strconv.FormatInt(teamCount, 10))
}
res, err := http.Get(PropSecurityURL + "/security?" + v.Encode())
if err != nil {
mlog.Error("Failed to get security update information from Mattermost.")
return
}
defer res.Body.Close()
bulletins := model.SecurityBulletinsFromJson(res.Body)
for _, bulletin := range bulletins {
if bulletin.AppliesToVersion == model.CurrentVersion {
if props["SecurityBulletin_"+bulletin.Id] == "" {
users, userErr := s.Store.User().GetSystemAdminProfiles()
if userErr != nil {
mlog.Error("Failed to get system admins for security update information from Mattermost.")
return
}
resBody, err := http.Get(PropSecurityURL + "/bulletins/" + bulletin.Id)
if err != nil {
mlog.Error("Failed to get security bulletin details")
return
}
body, err := ioutil.ReadAll(resBody.Body)
resBody.Body.Close()
if err != nil || resBody.StatusCode != 200 {
mlog.Error("Failed to read security bulletin details")
return
}
for _, user := range users {
mlog.Info("Sending security bulletin", mlog.String("bulletin_id", bulletin.Id), mlog.String("user_email", user.Email))
license := s.License()
mailConfig := s.MailServiceConfig()
mailservice.SendMailUsingConfig(user.Email, i18n.T("mattermost.bulletin.subject"), string(body), mailConfig, license != nil && *license.Features.Compliance, "")
}
bulletinSeen := &model.System{Name: "SecurityBulletin_" + bulletin.Id, Value: bulletin.Id}
s.Store.System().Save(bulletinSeen)
}
}
}
}
}