Move HTTPService and ConfigService into services package (#9422)

* Move HTTPService and ConfigService into utils package

* Re-add StaticConfigService

* Move config and http services into their own packages
Этот коммит содержится в:
Harrison Healey
2018-09-26 12:42:51 -04:00
коммит произвёл GitHub
родитель 15d64fb201
Коммит 4e59a27293
11 изменённых файлов: 147 добавлений и 110 удалений

Просмотреть файл

@@ -25,6 +25,7 @@ import (
"github.com/mattermost/mattermost-server/mlog" "github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin" "github.com/mattermost/mattermost-server/plugin"
"github.com/mattermost/mattermost-server/services/httpservice"
"github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/store/sqlstore" "github.com/mattermost/mattermost-server/store/sqlstore"
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
@@ -101,7 +102,7 @@ type App struct {
phase2PermissionsMigrationComplete bool phase2PermissionsMigrationComplete bool
HTTPService HTTPService HTTPService httpservice.HTTPService
} }
var appCount = 0 var appCount = 0
@@ -128,7 +129,7 @@ func New(options ...Option) (outApp *App, outErr error) {
licenseListeners: map[string]func(){}, licenseListeners: map[string]func(){},
} }
app.HTTPService = MakeHTTPService(app) app.HTTPService = httpservice.MakeHTTPService(app)
app.CreatePushNotificationsHub() app.CreatePushNotificationsHub()
app.StartPushNotificationsHubWorkers() app.StartPushNotificationsHubWorkers()

Просмотреть файл

@@ -7,7 +7,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@@ -21,6 +20,7 @@ import (
"github.com/mattermost/mattermost-server/store/sqlstore" "github.com/mattermost/mattermost-server/store/sqlstore"
"github.com/mattermost/mattermost-server/store/storetest" "github.com/mattermost/mattermost-server/store/storetest"
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/testutils"
) )
type TestHelper struct { type TestHelper struct {
@@ -36,7 +36,7 @@ type TestHelper struct {
tempConfigPath string tempConfigPath string
tempWorkspace string tempWorkspace string
MockedHTTPService *MockedHTTPService MockedHTTPService *testutils.MockedHTTPService
} }
type persistentTestStore struct { type persistentTestStore struct {
@@ -168,7 +168,7 @@ func (me *TestHelper) InitSystemAdmin() *TestHelper {
} }
func (me *TestHelper) MockHTTPService(handler http.Handler) *TestHelper { func (me *TestHelper) MockHTTPService(handler http.Handler) *TestHelper {
me.MockedHTTPService = MakeMockedHTTPService(handler) me.MockedHTTPService = testutils.MakeMockedHTTPService(handler)
me.App.HTTPService = me.MockedHTTPService me.App.HTTPService = me.MockedHTTPService
return me return me
@@ -514,22 +514,3 @@ func (me *FakeClusterInterface) sendClearRoleCacheMessage() {
Event: model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_ROLES, Event: model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_ROLES,
}) })
} }
type MockedHTTPService struct {
Server *httptest.Server
}
func MakeMockedHTTPService(handler http.Handler) *MockedHTTPService {
return &MockedHTTPService{
Server: httptest.NewServer(handler),
}
}
func (h *MockedHTTPService) MakeClient(trustURLs bool) *http.Client {
return h.Server.Client()
}
func (h *MockedHTTPService) Close() {
h.Server.CloseClientConnections()
h.Server.Close()
}

Просмотреть файл

@@ -1,67 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"net"
"net/http"
"strings"
"github.com/mattermost/mattermost-server/utils"
)
// Wraps the functionality for creating a new http.Client to encapsulate that and allow it to be mocked when testing
type HTTPService interface {
MakeClient(trustURLs bool) *http.Client
Close()
}
type HTTPServiceImpl struct {
app *App
}
func MakeHTTPService(app *App) HTTPService {
return &HTTPServiceImpl{app}
}
func (h *HTTPServiceImpl) MakeClient(trustURLs bool) *http.Client {
insecure := h.app.Config().ServiceSettings.EnableInsecureOutgoingConnections != nil && *h.app.Config().ServiceSettings.EnableInsecureOutgoingConnections
if trustURLs {
return utils.NewHTTPClient(insecure, nil, nil)
}
allowHost := func(host string) bool {
if h.app.Config().ServiceSettings.AllowedUntrustedInternalConnections == nil {
return false
}
for _, allowed := range strings.Fields(*h.app.Config().ServiceSettings.AllowedUntrustedInternalConnections) {
if host == allowed {
return true
}
}
return false
}
allowIP := func(ip net.IP) bool {
if !utils.IsReservedIP(ip) {
return true
}
if h.app.Config().ServiceSettings.AllowedUntrustedInternalConnections == nil {
return false
}
for _, allowed := range strings.Fields(*h.app.Config().ServiceSettings.AllowedUntrustedInternalConnections) {
if _, ipRange, err := net.ParseCIDR(allowed); err == nil && ipRange.Contains(ip) {
return true
}
}
return false
}
return utils.NewHTTPClient(insecure, allowHost, allowIP)
}
func (h *HTTPServiceImpl) Close() {
// Does nothing, but allows this to be overridden when mocking the service
}

Просмотреть файл

@@ -7,25 +7,12 @@ import (
ejobs "github.com/mattermost/mattermost-server/einterfaces/jobs" ejobs "github.com/mattermost/mattermost-server/einterfaces/jobs"
tjobs "github.com/mattermost/mattermost-server/jobs/interfaces" tjobs "github.com/mattermost/mattermost-server/jobs/interfaces"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/services/configservice"
"github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/store"
) )
type ConfigService interface {
Config() *model.Config
AddConfigListener(func(old, current *model.Config)) string
RemoveConfigListener(string)
}
type StaticConfigService struct {
Cfg *model.Config
}
func (s StaticConfigService) Config() *model.Config { return s.Cfg }
func (StaticConfigService) AddConfigListener(func(old, current *model.Config)) string { return "" }
func (StaticConfigService) RemoveConfigListener(string) {}
type JobServer struct { type JobServer struct {
ConfigService ConfigService ConfigService configservice.ConfigService
Store store.Store Store store.Store
Workers *Workers Workers *Workers
Schedulers *Schedulers Schedulers *Schedulers
@@ -38,7 +25,7 @@ type JobServer struct {
Migrations tjobs.MigrationsJobInterface Migrations tjobs.MigrationsJobInterface
} }
func NewJobServer(configService ConfigService, store store.Store) *JobServer { func NewJobServer(configService configservice.ConfigService, store store.Store) *JobServer {
return &JobServer{ return &JobServer{
ConfigService: configService, ConfigService: configService,
Store: store, Store: store,

Просмотреть файл

@@ -8,11 +8,12 @@ import (
"github.com/mattermost/mattermost-server/mlog" "github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/services/configservice"
) )
type Workers struct { type Workers struct {
startOnce sync.Once startOnce sync.Once
ConfigService ConfigService ConfigService configservice.ConfigService
Watcher *Watcher Watcher *Watcher
DataRetention model.Worker DataRetention model.Worker

15
services/configservice/configservice.go Обычный файл
Просмотреть файл

@@ -0,0 +1,15 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package configservice
import (
"github.com/mattermost/mattermost-server/model"
)
// An interface representing something that contains a Config, such as the app.App struct
type ConfigService interface {
Config() *model.Config
AddConfigListener(func(old, current *model.Config)) string
RemoveConfigListener(string)
}

Просмотреть файл

@@ -1,7 +1,7 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information. // See License.txt for license information.
package utils package httpservice
import ( import (
"context" "context"

Просмотреть файл

@@ -1,7 +1,7 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information. // See License.txt for license information.
package utils package httpservice
import ( import (
"context" "context"

67
services/httpservice/httpservice.go Обычный файл
Просмотреть файл

@@ -0,0 +1,67 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package httpservice
import (
"net"
"net/http"
"strings"
"github.com/mattermost/mattermost-server/services/configservice"
)
// Wraps the functionality for creating a new http.Client to encapsulate that and allow it to be mocked when testing
type HTTPService interface {
MakeClient(trustURLs bool) *http.Client
Close()
}
type HTTPServiceImpl struct {
configService configservice.ConfigService
}
func MakeHTTPService(configService configservice.ConfigService) HTTPService {
return &HTTPServiceImpl{configService}
}
func (h *HTTPServiceImpl) MakeClient(trustURLs bool) *http.Client {
insecure := h.configService.Config().ServiceSettings.EnableInsecureOutgoingConnections != nil && *h.configService.Config().ServiceSettings.EnableInsecureOutgoingConnections
if trustURLs {
return NewHTTPClient(insecure, nil, nil)
}
allowHost := func(host string) bool {
if h.configService.Config().ServiceSettings.AllowedUntrustedInternalConnections == nil {
return false
}
for _, allowed := range strings.Fields(*h.configService.Config().ServiceSettings.AllowedUntrustedInternalConnections) {
if host == allowed {
return true
}
}
return false
}
allowIP := func(ip net.IP) bool {
if !IsReservedIP(ip) {
return true
}
if h.configService.Config().ServiceSettings.AllowedUntrustedInternalConnections == nil {
return false
}
for _, allowed := range strings.Fields(*h.configService.Config().ServiceSettings.AllowedUntrustedInternalConnections) {
if _, ipRange, err := net.ParseCIDR(allowed); err == nil && ipRange.Contains(ip) {
return true
}
}
return false
}
return NewHTTPClient(insecure, allowHost, allowIP)
}
func (h *HTTPServiceImpl) Close() {
// Does nothing, but allows this to be overridden when mocking the service
}

28
utils/testutils/mocked_http_service.go Обычный файл
Просмотреть файл

@@ -0,0 +1,28 @@
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package testutils
import (
"net/http"
"net/http/httptest"
)
type MockedHTTPService struct {
Server *httptest.Server
}
func MakeMockedHTTPService(handler http.Handler) *MockedHTTPService {
return &MockedHTTPService{
Server: httptest.NewServer(handler),
}
}
func (h *MockedHTTPService) MakeClient(trustURLs bool) *http.Client {
return h.Server.Client()
}
func (h *MockedHTTPService) Close() {
h.Server.CloseClientConnections()
h.Server.Close()
}

24
utils/testutils/static_config_service.go Обычный файл
Просмотреть файл

@@ -0,0 +1,24 @@
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package testutils
import (
"github.com/mattermost/mattermost-server/model"
)
type StaticConfigService struct {
Cfg *model.Config
}
func (s StaticConfigService) Config() *model.Config {
return s.Cfg
}
func (StaticConfigService) AddConfigListener(func(old, current *model.Config)) string {
return ""
}
func (StaticConfigService) RemoveConfigListener(string) {
}