mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-24 01:27:49 +01:00
Merge pull request #5705 from steven-zou/fix_redis_parsing_issue
Fix the issue of parsing redis connection address in chart API
This commit is contained in:
commit
d895ebef13
@ -68,22 +68,32 @@ func parseRedisConfig(redisConfigV string) (string, error) {
|
|||||||
redisConfig := make(map[string]string)
|
redisConfig := make(map[string]string)
|
||||||
redisConfig["key"] = cacheCollectionName
|
redisConfig["key"] = cacheCollectionName
|
||||||
|
|
||||||
//The full pattern
|
//Try best to parse the configuration segments.
|
||||||
if strings.Index(redisConfigV, ",") != -1 {
|
//If the related parts are missing, assign default value.
|
||||||
//Read only the previous 4 segments
|
//The default database index for UI process is 0.
|
||||||
configSegments := strings.SplitN(redisConfigV, ",", 4)
|
configSegments := strings.Split(redisConfigV, ",")
|
||||||
if len(configSegments) != 4 {
|
for i, segment := range configSegments {
|
||||||
return "", errors.New("invalid redis config, it should be address:port[,weight,password,db_index]")
|
if i > 3 {
|
||||||
|
//ignore useless segments
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
redisConfig["conn"] = configSegments[0]
|
switch i {
|
||||||
redisConfig["password"] = configSegments[2]
|
//address:port
|
||||||
redisConfig["dbNum"] = configSegments[3]
|
case 0:
|
||||||
} else {
|
redisConfig["conn"] = segment
|
||||||
//The short pattern
|
//password, may not exist
|
||||||
redisConfig["conn"] = redisConfigV
|
case 2:
|
||||||
|
redisConfig["password"] = segment
|
||||||
|
//database index, may not exist
|
||||||
|
case 3:
|
||||||
|
redisConfig["dbNum"] = segment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Assign default value
|
||||||
|
if len(redisConfig["dbNum"]) == 0 {
|
||||||
redisConfig["dbNum"] = "0"
|
redisConfig["dbNum"] = "0"
|
||||||
redisConfig["password"] = ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Try to validate the connection address
|
//Try to validate the connection address
|
||||||
|
42
src/chartserver/utils_test.go
Normal file
42
src/chartserver/utils_test.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package chartserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Test the utility function parseRedisConfig
|
||||||
|
func TestParseRedisConfig(t *testing.T) {
|
||||||
|
//Case 1: empty addr
|
||||||
|
redisAddr := ""
|
||||||
|
if _, err := parseRedisConfig(redisAddr); err == nil {
|
||||||
|
t.Fatal("expect non nil error but got nil one if addr is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
//Case 2: short pattern, addr:port
|
||||||
|
redisAddr = "redis:6379"
|
||||||
|
if parsedConnStr, err := parseRedisConfig(redisAddr); err != nil {
|
||||||
|
t.Fatalf("expect nil error but got non nil one if addr is short pattern: %s\n", parsedConnStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
//Case 3: long pattern but miss some parts
|
||||||
|
redisAddr = "redis:6379,100"
|
||||||
|
if parsedConnStr, err := parseRedisConfig(redisAddr); err != nil {
|
||||||
|
t.Fatalf("expect nil error but got non nil one if addr is long pattern with some parts missing: %s\n", parsedConnStr)
|
||||||
|
} else {
|
||||||
|
if strings.Index(parsedConnStr, `"dbNum":"0"`) == -1 {
|
||||||
|
t.Fatalf("expect 'dbNum:0' in the parsed conn str but got nothing: %s\n", parsedConnStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Case 4: long pattern
|
||||||
|
redisAddr = "redis:6379,100,Passw0rd,1"
|
||||||
|
if parsedConnStr, err := parseRedisConfig(redisAddr); err != nil {
|
||||||
|
t.Fatal("expect nil error but got non nil one if addr is long pattern")
|
||||||
|
} else {
|
||||||
|
if strings.Index(parsedConnStr, `"dbNum":"1"`) == -1 ||
|
||||||
|
strings.Index(parsedConnStr, `"password":"Passw0rd"`) == -1 {
|
||||||
|
t.Fatalf("expect 'dbNum:0' and 'password:Passw0rd' in the parsed conn str but got nothing: %s", parsedConnStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user