[MM-14400] Programmatically generate default.json (#10551)

* create/update config.json using go generate

* added default config generator
added config-reset to Jenkins and make package, updated defaults to consider 'isNew' flag

* corrections after code review

* fixed Config.isValid to handle empty encryption keys

* fixed Config.isValid to handle empty encryption keys

* fixed Config.isValid to handle empty encryption keys

* isUpdate now only checks for nil

* Addressed review comments, added unit testing for default config generator

* err shadowing

* license

* provide output file for config generator via ENV variable, since go generate doesn't support arguments and we need two output paths (config-reset and package)

* cleanup

* proper defaults for PushNotificationServer and SendPushNotifications

* corrected generating defaults for TrustedProxyIPHeader to be consistent with default.json in master

* Check for empty SiteURL as well as nil

* corrected SiteURL settings and checking

* crazy typos fixed

* corrected tests to newly expected values

* relaxed the checks

* fixed formatting
Этот коммит содержится в:
Eli Yukelzon
2019-06-06 20:57:01 +03:00
коммит произвёл Jesse Hallam
родитель 978ee13262
Коммит 6a42ad2af5
16 изменённых файлов: 197 добавлений и 497 удалений

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

@@ -46,6 +46,7 @@ const (
GENERIC_NO_CHANNEL_NOTIFICATION = "generic_no_channel"
GENERIC_NOTIFICATION = "generic"
GENERIC_NOTIFICATION_SERVER = "https://push-test.mattermost.com"
FULL_NOTIFICATION = "full"
DIRECT_MESSAGE_ANY = "any"
@@ -184,6 +185,16 @@ const (
IMAGE_PROXY_TYPE_LOCAL = "local"
IMAGE_PROXY_TYPE_ATMOS_CAMO = "atmos/camo"
GOOGLE_SETTINGS_DEFAULT_SCOPE = "profile email"
GOOGLE_SETTINGS_DEFAULT_AUTH_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth"
GOOGLE_SETTINGS_DEFAULT_TOKEN_ENDPOINT = "https://www.googleapis.com/oauth2/v4/token"
GOOGLE_SETTINGS_DEFAULT_USER_API_ENDPOINT = "https://people.googleapis.com/v1/people/me?personFields=names,emailAddresses,nicknames,metadata"
OFFICE365_SETTINGS_DEFAULT_SCOPE = "User.Read"
OFFICE365_SETTINGS_DEFAULT_AUTH_ENDPOINT = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
OFFICE365_SETTINGS_DEFAULT_TOKEN_ENDPOINT = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
OFFICE365_SETTINGS_DEFAULT_USER_API_ENDPOINT = "https://graph.microsoft.com/v1.0/me"
)
var ServerTLSSupportedCiphers = map[string]uint16{
@@ -297,7 +308,7 @@ type ServiceSettings struct {
EnableBotAccountCreation *bool
}
func (s *ServiceSettings) SetDefaults() {
func (s *ServiceSettings) SetDefaults(isUpdate bool) {
if s.EnableEmailInvitations == nil {
// If the site URL is also not present then assume this is a clean install
if s.SiteURL == nil {
@@ -308,7 +319,11 @@ func (s *ServiceSettings) SetDefaults() {
}
if s.SiteURL == nil {
s.SiteURL = NewString(SERVICE_SETTINGS_DEFAULT_SITE_URL)
if s.EnableDeveloper != nil && *s.EnableDeveloper {
s.SiteURL = NewString(SERVICE_SETTINGS_DEFAULT_SITE_URL)
} else {
s.SiteURL = NewString("")
}
}
if s.WebsocketURL == nil {
@@ -435,8 +450,14 @@ func (s *ServiceSettings) SetDefaults() {
s.Forward80To443 = NewBool(false)
}
if s.TrustedProxyIPHeader == nil {
s.TrustedProxyIPHeader = []string{HEADER_FORWARDED, HEADER_REAL_IP}
if isUpdate {
// When updating an existing configuration, ensure that defaults are set.
if s.TrustedProxyIPHeader == nil {
s.TrustedProxyIPHeader = []string{HEADER_FORWARDED, HEADER_REAL_IP}
}
} else {
// When generating a blank configuration, leave the list empty.
s.TrustedProxyIPHeader = []string{}
}
if s.TimeBetweenUserTypingUpdatesMilliseconds == nil {
@@ -627,7 +648,7 @@ func (s *ServiceSettings) SetDefaults() {
}
if s.DisableLegacyMFA == nil {
s.DisableLegacyMFA = NewBool(false)
s.DisableLegacyMFA = NewBool(!isUpdate)
}
if s.ExperimentalLdapGroupSync == nil {
@@ -782,7 +803,7 @@ type SSOSettings struct {
UserApiEndpoint *string
}
func (s *SSOSettings) setDefaults() {
func (s *SSOSettings) setDefaults(scope, authEndpoint, tokenEndpoint, userApiEndpoint string) {
if s.Enable == nil {
s.Enable = NewBool(false)
}
@@ -796,19 +817,19 @@ func (s *SSOSettings) setDefaults() {
}
if s.Scope == nil {
s.Scope = NewString("")
s.Scope = NewString(scope)
}
if s.AuthEndpoint == nil {
s.AuthEndpoint = NewString("")
s.AuthEndpoint = NewString(authEndpoint)
}
if s.TokenEndpoint == nil {
s.TokenEndpoint = NewString("")
s.TokenEndpoint = NewString(tokenEndpoint)
}
if s.UserApiEndpoint == nil {
s.UserApiEndpoint = NewString("")
s.UserApiEndpoint = NewString(userApiEndpoint)
}
}
@@ -825,7 +846,7 @@ type SqlSettings struct {
QueryTimeout *int `restricted:"true"`
}
func (s *SqlSettings) SetDefaults() {
func (s *SqlSettings) SetDefaults(isUpdate bool) {
if s.DriverName == nil {
s.DriverName = NewString(DATABASE_DRIVER_MYSQL)
}
@@ -842,8 +863,14 @@ func (s *SqlSettings) SetDefaults() {
s.DataSourceSearchReplicas = []string{}
}
if s.AtRestEncryptKey == nil || len(*s.AtRestEncryptKey) == 0 {
s.AtRestEncryptKey = NewString(NewRandomString(32))
if isUpdate {
// When updating an existing configuration, ensure an encryption key has been specified.
if s.AtRestEncryptKey == nil || len(*s.AtRestEncryptKey) == 0 {
s.AtRestEncryptKey = NewString(NewRandomString(32))
}
} else {
// When generating a blank configuration, leave this key empty to be generated on server start.
s.AtRestEncryptKey = NewString("")
}
if s.MaxIdleConns == nil {
@@ -1008,7 +1035,7 @@ type FileSettings struct {
AmazonS3Trace *bool `restricted:"true"`
}
func (s *FileSettings) SetDefaults() {
func (s *FileSettings) SetDefaults(isUpdate bool) {
if s.EnableFileAttachments == nil {
s.EnableFileAttachments = NewBool(true)
}
@@ -1037,8 +1064,14 @@ func (s *FileSettings) SetDefaults() {
s.EnablePublicLink = NewBool(false)
}
if s.PublicLinkSalt == nil || len(*s.PublicLinkSalt) == 0 {
s.PublicLinkSalt = NewString(NewRandomString(32))
if isUpdate {
// When updating an existing configuration, ensure link salt has been specified.
if s.PublicLinkSalt == nil || len(*s.PublicLinkSalt) == 0 {
s.PublicLinkSalt = NewString(NewRandomString(32))
}
} else {
// When generating a blank configuration, leave link salt empty to be generated on server start.
s.PublicLinkSalt = NewString("")
}
if s.InitialFont == nil {
@@ -1116,7 +1149,7 @@ type EmailSettings struct {
LoginButtonTextColor *string
}
func (s *EmailSettings) SetDefaults() {
func (s *EmailSettings) SetDefaults(isUpdate bool) {
if s.EnableSignUpWithEmail == nil {
s.EnableSignUpWithEmail = NewBool(true)
}
@@ -1186,11 +1219,15 @@ func (s *EmailSettings) SetDefaults() {
}
if s.SendPushNotifications == nil {
s.SendPushNotifications = NewBool(false)
s.SendPushNotifications = NewBool(!isUpdate)
}
if s.PushNotificationServer == nil {
s.PushNotificationServer = NewString("")
if isUpdate {
s.PushNotificationServer = NewString("")
} else {
s.PushNotificationServer = NewString(GENERIC_NOTIFICATION_SERVER)
}
}
if s.PushNotificationContents == nil {
@@ -2364,7 +2401,14 @@ func ConfigFromJson(data io.Reader) *Config {
return o
}
// isUpdate detects a pre-existing config based on whether SiteURL has been changed
func (o *Config) isUpdate() bool {
return o.ServiceSettings.SiteURL != nil
}
func (o *Config) SetDefaults() {
isUpdate := o.isUpdate()
o.LdapSettings.SetDefaults()
o.SamlSettings.SetDefaults()
@@ -2376,14 +2420,14 @@ func (o *Config) SetDefaults() {
}
}
o.SqlSettings.SetDefaults()
o.FileSettings.SetDefaults()
o.EmailSettings.SetDefaults()
o.SqlSettings.SetDefaults(isUpdate)
o.FileSettings.SetDefaults(isUpdate)
o.EmailSettings.SetDefaults(isUpdate)
o.PrivacySettings.setDefaults()
o.Office365Settings.setDefaults()
o.GitLabSettings.setDefaults()
o.GoogleSettings.setDefaults()
o.ServiceSettings.SetDefaults()
o.Office365Settings.setDefaults(OFFICE365_SETTINGS_DEFAULT_SCOPE, OFFICE365_SETTINGS_DEFAULT_AUTH_ENDPOINT, OFFICE365_SETTINGS_DEFAULT_TOKEN_ENDPOINT, OFFICE365_SETTINGS_DEFAULT_USER_API_ENDPOINT)
o.GitLabSettings.setDefaults("", "", "", "")
o.GoogleSettings.setDefaults(GOOGLE_SETTINGS_DEFAULT_SCOPE, GOOGLE_SETTINGS_DEFAULT_AUTH_ENDPOINT, GOOGLE_SETTINGS_DEFAULT_TOKEN_ENDPOINT, GOOGLE_SETTINGS_DEFAULT_USER_API_ENDPOINT)
o.ServiceSettings.SetDefaults(isUpdate)
o.PasswordSettings.SetDefaults()
o.TeamSettings.SetDefaults()
o.MetricsSettings.SetDefaults()
@@ -2517,7 +2561,7 @@ func (ts *TeamSettings) isValid() *AppError {
}
func (ss *SqlSettings) isValid() *AppError {
if len(*ss.AtRestEncryptKey) < 32 {
if *ss.AtRestEncryptKey != "" && len(*ss.AtRestEncryptKey) < 32 {
return NewAppError("Config.IsValid", "model.config.is_valid.encrypt_sql.app_error", nil, "", http.StatusBadRequest)
}
@@ -2557,7 +2601,7 @@ func (fs *FileSettings) isValid() *AppError {
return NewAppError("Config.IsValid", "model.config.is_valid.file_driver.app_error", nil, "", http.StatusBadRequest)
}
if len(*fs.PublicLinkSalt) < 32 {
if *fs.PublicLinkSalt != "" && len(*fs.PublicLinkSalt) < 32 {
return NewAppError("Config.IsValid", "model.config.is_valid.file_salt.app_error", nil, "", http.StatusBadRequest)
}

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

@@ -593,7 +593,7 @@ func TestListenAddressIsValidated(t *testing.T) {
ss := &ServiceSettings{
ListenAddress: NewString(key),
}
ss.SetDefaults()
ss.SetDefaults(true)
if expected {
require.Nil(t, ss.isValid(), fmt.Sprintf("Got an error from '%v'.", key))
} else {