MM-51768: Scrub username/password from SQL datasource (#22731)

https://mattermost.atlassian.net/browse/MM-51768
```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2023-04-03 23:41:51 +05:30
коммит произвёл GitHub
родитель 1762dfce22
Коммит dacac2e3ed
7 изменённых файлов: 80 добавлений и 81 удалений

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

@@ -407,7 +407,10 @@ func (ds *DatabaseStore) RemoveFile(name string) error {
// String returns the path to the database backing the config, masking the password.
func (ds *DatabaseStore) String() string {
return stripPassword(ds.originalDsn, ds.driverName)
// This is called during the running of MM, so we expect the parsing of DSN
// to be successful.
sanitized, _ := sqlstore.SanitizeDataSource(ds.driverName, ds.originalDsn)
return sanitized
}
// Close cleans up resources associated with the store.

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

@@ -1107,12 +1107,12 @@ func TestDatabaseStoreString(t *testing.T) {
if *mainHelper.GetSQLSettings().DriverName == "postgres" {
maskedDSN := ds.String()
assert.True(t, strings.HasPrefix(maskedDSN, "postgres://"))
assert.True(t, strings.Contains(maskedDSN, "mmuser"))
assert.False(t, strings.Contains(maskedDSN, "mmuser"))
assert.False(t, strings.Contains(maskedDSN, "mostest"))
} else {
maskedDSN := ds.String()
assert.True(t, strings.HasPrefix(maskedDSN, "mysql://"))
assert.True(t, strings.Contains(maskedDSN, "mmuser"))
assert.False(t, strings.HasPrefix(maskedDSN, "mysql://"))
assert.False(t, strings.Contains(maskedDSN, "mmuser"))
assert.False(t, strings.Contains(maskedDSN, "mostest"))
}
}

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

@@ -179,27 +179,6 @@ func IsDatabaseDSN(dsn string) bool {
strings.HasPrefix(dsn, "postgresql://")
}
// stripPassword remove the password from a given DSN
func stripPassword(dsn, schema string) string {
prefix := schema + "://"
dsn = strings.TrimPrefix(dsn, prefix)
i := strings.Index(dsn, ":")
j := strings.LastIndex(dsn, "@")
// Return error if no @ sign is found
if j < 0 {
return "(omitted due to error parsing the DSN)"
}
// Return back the input if no password is found
if i < 0 || i > j {
return prefix + dsn
}
return prefix + dsn[:i+1] + dsn[j:]
}
func isJSONMap(data string) bool {
var m map[string]any
return json.Unmarshal([]byte(data), &m) == nil

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

@@ -197,61 +197,6 @@ func TestIsDatabaseDSN(t *testing.T) {
}
}
func TestStripPassword(t *testing.T) {
for name, test := range map[string]struct {
DSN string
Schema string
ExpectedOut string
}{
"mysql": {
DSN: "mysql://mmuser:password@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s",
Schema: "mysql",
ExpectedOut: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s",
},
"mysql idempotent": {
DSN: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s",
Schema: "mysql",
ExpectedOut: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s",
},
"mysql: password with : and @": {
DSN: "mysql://mmuser:p:assw@ord@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s",
Schema: "mysql",
ExpectedOut: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s",
},
"mysql: password with @ and :": {
DSN: "mysql://mmuser:pa@sswo:rd@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s",
Schema: "mysql",
ExpectedOut: "mysql://mmuser:@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s",
},
"postgres": {
DSN: "postgres://mmuser:password@localhost:5432/mattermost?sslmode=disable&connect_timeout=10",
Schema: "postgres",
ExpectedOut: "postgres://mmuser:@localhost:5432/mattermost?sslmode=disable&connect_timeout=10",
},
"pipe": {
DSN: "mysql://user@unix(/path/to/socket)/dbname",
Schema: "mysql",
ExpectedOut: "mysql://user@unix(/path/to/socket)/dbname",
},
"malformed without :": {
DSN: "postgres://mmuserpassword@localhost:5432/mattermost?sslmode=disable&connect_timeout=10",
Schema: "postgres",
ExpectedOut: "postgres://mmuserpassword@localhost:5432/mattermost?sslmode=disable&connect_timeout=10",
},
"malformed without @": {
DSN: "postgres://mmuser:passwordlocalhost:5432/mattermost?sslmode=disable&connect_timeout=10",
Schema: "postgres",
ExpectedOut: "(omitted due to error parsing the DSN)",
},
} {
t.Run(name, func(t *testing.T) {
out := stripPassword(test.DSN, test.Schema)
assert.Equal(t, test.ExpectedOut, out)
})
}
}
func TestIsJSONMap(t *testing.T) {
tests := []struct {
name string