github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/config/utils_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package config 5 6 import ( 7 "testing" 8 9 "github.com/mattermost/mattermost-server/v5/model" 10 "github.com/mattermost/mattermost-server/v5/utils" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestDesanitize(t *testing.T) { 15 actual := &model.Config{} 16 actual.SetDefaults() 17 18 // These setting should be ignored 19 actual.LdapSettings.Enable = bToP(false) 20 actual.FileSettings.DriverName = sToP("s3") 21 22 // These settings should be desanitized into target. 23 actual.LdapSettings.BindPassword = sToP("bind_password") 24 actual.FileSettings.PublicLinkSalt = sToP("public_link_salt") 25 actual.FileSettings.AmazonS3SecretAccessKey = sToP("amazon_s3_secret_access_key") 26 actual.EmailSettings.SMTPPassword = sToP("smtp_password") 27 actual.GitLabSettings.Secret = sToP("secret") 28 actual.SqlSettings.DataSource = sToP("data_source") 29 actual.SqlSettings.AtRestEncryptKey = sToP("at_rest_encrypt_key") 30 actual.ElasticsearchSettings.Password = sToP("password") 31 actual.SqlSettings.DataSourceReplicas = append(actual.SqlSettings.DataSourceReplicas, "replica0") 32 actual.SqlSettings.DataSourceReplicas = append(actual.SqlSettings.DataSourceReplicas, "replica1") 33 actual.SqlSettings.DataSourceSearchReplicas = append(actual.SqlSettings.DataSourceSearchReplicas, "search_replica0") 34 actual.SqlSettings.DataSourceSearchReplicas = append(actual.SqlSettings.DataSourceSearchReplicas, "search_replica1") 35 36 target := &model.Config{} 37 target.SetDefaults() 38 39 // These setting should be ignored 40 target.LdapSettings.Enable = bToP(true) 41 target.FileSettings.DriverName = sToP("file") 42 43 // These settings should be updated from actual 44 target.LdapSettings.BindPassword = sToP(model.FAKE_SETTING) 45 target.FileSettings.PublicLinkSalt = sToP(model.FAKE_SETTING) 46 target.FileSettings.AmazonS3SecretAccessKey = sToP(model.FAKE_SETTING) 47 target.EmailSettings.SMTPPassword = sToP(model.FAKE_SETTING) 48 target.GitLabSettings.Secret = sToP(model.FAKE_SETTING) 49 target.SqlSettings.DataSource = sToP(model.FAKE_SETTING) 50 target.SqlSettings.AtRestEncryptKey = sToP(model.FAKE_SETTING) 51 target.ElasticsearchSettings.Password = sToP(model.FAKE_SETTING) 52 target.SqlSettings.DataSourceReplicas = append(target.SqlSettings.DataSourceReplicas, "old_replica0") 53 target.SqlSettings.DataSourceSearchReplicas = append(target.SqlSettings.DataSourceReplicas, "old_search_replica0") 54 55 actualClone := actual.Clone() 56 desanitize(actual, target) 57 assert.Equal(t, actualClone, actual, "actual should not have been changed") 58 59 // Verify the settings that should have been left untouched in target 60 assert.True(t, *target.LdapSettings.Enable, "LdapSettings.Enable should not have changed") 61 assert.Equal(t, "file", *target.FileSettings.DriverName, "FileSettings.DriverName should not have been changed") 62 63 // Verify the settings that should have been desanitized into target 64 assert.Equal(t, *actual.LdapSettings.BindPassword, *target.LdapSettings.BindPassword) 65 assert.Equal(t, *actual.FileSettings.PublicLinkSalt, *target.FileSettings.PublicLinkSalt) 66 assert.Equal(t, *actual.FileSettings.AmazonS3SecretAccessKey, *target.FileSettings.AmazonS3SecretAccessKey) 67 assert.Equal(t, *actual.EmailSettings.SMTPPassword, *target.EmailSettings.SMTPPassword) 68 assert.Equal(t, *actual.GitLabSettings.Secret, *target.GitLabSettings.Secret) 69 assert.Equal(t, *actual.SqlSettings.DataSource, *target.SqlSettings.DataSource) 70 assert.Equal(t, *actual.SqlSettings.AtRestEncryptKey, *target.SqlSettings.AtRestEncryptKey) 71 assert.Equal(t, *actual.ElasticsearchSettings.Password, *target.ElasticsearchSettings.Password) 72 assert.Equal(t, actual.SqlSettings.DataSourceReplicas, target.SqlSettings.DataSourceReplicas) 73 assert.Equal(t, actual.SqlSettings.DataSourceSearchReplicas, target.SqlSettings.DataSourceSearchReplicas) 74 } 75 76 func TestFixInvalidLocales(t *testing.T) { 77 utils.TranslationsPreInit() 78 79 cfg := &model.Config{} 80 cfg.SetDefaults() 81 82 *cfg.LocalizationSettings.DefaultServerLocale = "en" 83 *cfg.LocalizationSettings.DefaultClientLocale = "en" 84 *cfg.LocalizationSettings.AvailableLocales = "" 85 86 changed := FixInvalidLocales(cfg) 87 assert.False(t, changed) 88 89 *cfg.LocalizationSettings.DefaultServerLocale = "junk" 90 changed = FixInvalidLocales(cfg) 91 assert.True(t, changed) 92 assert.Equal(t, "en", *cfg.LocalizationSettings.DefaultServerLocale) 93 94 *cfg.LocalizationSettings.DefaultServerLocale = "" 95 changed = FixInvalidLocales(cfg) 96 assert.True(t, changed) 97 assert.Equal(t, "en", *cfg.LocalizationSettings.DefaultServerLocale) 98 99 *cfg.LocalizationSettings.AvailableLocales = "en" 100 *cfg.LocalizationSettings.DefaultServerLocale = "de" 101 changed = FixInvalidLocales(cfg) 102 assert.False(t, changed) 103 assert.NotContains(t, *cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultServerLocale, "DefaultServerLocale should not be added to AvailableLocales") 104 105 *cfg.LocalizationSettings.AvailableLocales = "" 106 *cfg.LocalizationSettings.DefaultClientLocale = "junk" 107 changed = FixInvalidLocales(cfg) 108 assert.True(t, changed) 109 assert.Equal(t, "en", *cfg.LocalizationSettings.DefaultClientLocale) 110 111 *cfg.LocalizationSettings.DefaultClientLocale = "" 112 changed = FixInvalidLocales(cfg) 113 assert.True(t, changed) 114 assert.Equal(t, "en", *cfg.LocalizationSettings.DefaultClientLocale) 115 116 *cfg.LocalizationSettings.AvailableLocales = "en" 117 *cfg.LocalizationSettings.DefaultClientLocale = "de" 118 changed = FixInvalidLocales(cfg) 119 assert.True(t, changed) 120 assert.Contains(t, *cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultServerLocale, "DefaultClientLocale should have been added to AvailableLocales") 121 122 // validate AvailableLocales 123 *cfg.LocalizationSettings.DefaultServerLocale = "en" 124 *cfg.LocalizationSettings.DefaultClientLocale = "en" 125 *cfg.LocalizationSettings.AvailableLocales = "junk" 126 changed = FixInvalidLocales(cfg) 127 assert.True(t, changed) 128 assert.Equal(t, "", *cfg.LocalizationSettings.AvailableLocales) 129 130 *cfg.LocalizationSettings.AvailableLocales = "en,de,junk" 131 changed = FixInvalidLocales(cfg) 132 assert.True(t, changed) 133 assert.Equal(t, "", *cfg.LocalizationSettings.AvailableLocales) 134 135 *cfg.LocalizationSettings.DefaultServerLocale = "fr" 136 *cfg.LocalizationSettings.DefaultClientLocale = "de" 137 *cfg.LocalizationSettings.AvailableLocales = "en" 138 changed = FixInvalidLocales(cfg) 139 assert.True(t, changed) 140 assert.NotContains(t, *cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultServerLocale, "DefaultServerLocale should not be added to AvailableLocales") 141 assert.Contains(t, *cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultClientLocale, "DefaultClientLocale should have been added to AvailableLocales") 142 } 143 144 func TestStripPassword(t *testing.T) { 145 for name, test := range map[string]struct { 146 DSN string 147 Schema string 148 ExpectedOut string 149 }{ 150 "mysql": { 151 DSN: "mysql://mmuser:password@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s", 152 Schema: "mysql", 153 ExpectedOut: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s", 154 }, 155 "mysql idempotent": { 156 DSN: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s", 157 Schema: "mysql", 158 ExpectedOut: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s", 159 }, 160 "mysql: password with : and @": { 161 DSN: "mysql://mmuser:p:assw@ord@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s", 162 Schema: "mysql", 163 ExpectedOut: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s", 164 }, 165 "mysql: password with @ and :": { 166 DSN: "mysql://mmuser:pa@sswo:rd@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s", 167 Schema: "mysql", 168 ExpectedOut: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s", 169 }, 170 "postgres": { 171 DSN: "postgres://mmuser:password@localhost:5432/mattermost?sslmode=disable&connect_timeout=10", 172 Schema: "postgres", 173 ExpectedOut: "postgres://mmuser:@localhost:5432/mattermost?sslmode=disable&connect_timeout=10", 174 }, 175 "pipe": { 176 DSN: "mysql://user@unix(/path/to/socket)/dbname", 177 Schema: "mysql", 178 ExpectedOut: "mysql://user@unix(/path/to/socket)/dbname", 179 }, 180 "malformed without :": { 181 DSN: "postgres://mmuserpassword@localhost:5432/mattermost?sslmode=disable&connect_timeout=10", 182 Schema: "postgres", 183 ExpectedOut: "postgres://mmuserpassword@localhost:5432/mattermost?sslmode=disable&connect_timeout=10", 184 }, 185 "malformed without @": { 186 DSN: "postgres://mmuser:passwordlocalhost:5432/mattermost?sslmode=disable&connect_timeout=10", 187 Schema: "postgres", 188 ExpectedOut: "(omitted due to error parsing the DSN)", 189 }, 190 } { 191 t.Run(name, func(t *testing.T) { 192 out := stripPassword(test.DSN, test.Schema) 193 194 assert.Equal(t, test.ExpectedOut, out) 195 }) 196 } 197 } 198 199 func sToP(s string) *string { 200 return &s 201 } 202 203 func bToP(b bool) *bool { 204 return &b 205 } 206 207 func TestIsJsonMap(t *testing.T) { 208 tests := []struct { 209 name string 210 data string 211 want bool 212 }{ 213 {name: "good json", data: `{"local_tcp": { 214 "Type": "tcp","Format": "json","Levels": [ 215 {"ID": 5,"Name": "debug","Stacktrace": false} 216 ], 217 "Options": {"ip": "localhost","port": 18065}, 218 "MaxQueueSize": 1000}} 219 `, want: true, 220 }, 221 {name: "empty json", data: "{}", want: true}, 222 {name: "string json", data: `"test"`, want: false}, 223 {name: "array json", data: `["test1", "test2"]`, want: false}, 224 {name: "bad json", data: `{huh?}`, want: false}, 225 {name: "filename", data: "/tmp/logger.conf", want: false}, 226 {name: "mysql dsn", data: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s", want: false}, 227 {name: "postgres dsn", data: "postgres://mmuser:passwordlocalhost:5432/mattermost?sslmode=disable&connect_timeout=10", want: false}, 228 } 229 for _, tt := range tests { 230 t.Run(tt.name, func(t *testing.T) { 231 if got := IsJsonMap(tt.data); got != tt.want { 232 t.Errorf("IsJsonMap() = %v, want %v", got, tt.want) 233 } 234 }) 235 } 236 }