diff --git a/gluster/driver/tools.go b/gluster/driver/tools.go index e6d4d08..1f26ee2 100644 --- a/gluster/driver/tools.go +++ b/gluster/driver/tools.go @@ -8,29 +8,35 @@ import ( "github.com/docker/go-plugins-helpers/volume" "github.com/sapk/docker-volume-helpers/basic" + "golang.org/x/net/idna" ) const ( - validVolUriRegex = `([^:]+?):\/?([^\/]+)(/.+)?` + validHostnameRegex = `(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9\-])\.)*([A-Za-z0-9\-]|[A-Za-z0-9\-][A-Za-z0-9\-]*[A-Za-z0-9\-])` + validVolURIRegex = `((` + validHostnameRegex + `)(,` + validHostnameRegex + `)*):\/?([^\/]+)(/.+)?` ) func isValidURI(volURI string) bool { - re := regexp.MustCompile(validVolUriRegex) + volURI, err := idna.ToASCII(volURI) + if err != nil { + return false + } + re := regexp.MustCompile(validVolURIRegex) return re.MatchString(volURI) } func parseVolURI(volURI string) string { - re := regexp.MustCompile(validVolUriRegex) + volURI, _ = idna.ToASCII(volURI) + re := regexp.MustCompile(validVolURIRegex) res := re.FindAllStringSubmatch(volURI, -1) volServers := strings.Split(res[0][1], ",") - volumeId := res[0][2] - subDir := res[0][3] - - if (subDir == "") { - return fmt.Sprintf("--volfile-id='%s' -s '%s'", volumeId, strings.Join(volServers, "' -s '")) - } else { - return fmt.Sprintf("--volfile-id='%s' --subdir-mount='%s' -s '%s'", volumeId, subDir, strings.Join(volServers, "' -s '")) + volumeID := res[0][10] + subDir := res[0][11] + + if subDir == "" { + return fmt.Sprintf("--volfile-id='%s' -s '%s'", volumeID, strings.Join(volServers, "' -s '")) } + return fmt.Sprintf("--volfile-id='%s' --subdir-mount='%s' -s '%s'", volumeID, subDir, strings.Join(volServers, "' -s '")) } //GetMountName get moint point base on request and driver config (mountUniqName) diff --git a/gluster/driver/tools_test.go b/gluster/driver/tools_test.go index 0090e50..d57efd5 100644 --- a/gluster/driver/tools_test.go +++ b/gluster/driver/tools_test.go @@ -18,19 +18,28 @@ func TestIsValidURI(t *testing.T) { {"test,volume", false}, {"test,test2:volume", true}, {"192.168.1.1:volume", true}, - {"192.168.1.:volume", true}, + {"192.168.1.:volume", false}, {"192.168.1.1,10.8.0.1:volume", true}, {"192.168.1.1,test2:volume", true}, {"192.168.1.1,test2:volume/subdir", true}, {"192.168.1.1,test2:/volume/subdir", true}, {"192.168.1.1,test2://volume/subdir", false}, {"192.168.1.1,test2:/volume", true}, + {"مثال.إختبار:volume", true}, + {"例子.测试:volume", true}, + {"例子.測試:volume", true}, + {"παράδειγμα.δοκιμή:volume", true}, + {"उदाहरण.परीक्षा:volume", true}, + {"例え.テスト:volume", true}, + {"실례.테스트:volume", true}, + {"مثال.آزمایشی:volume", true}, + {"пример.испытание:volume", true}, } for _, test := range tt { r := isValidURI(test.value) if test.result != r { - t.Errorf("Expected to be '%v' , got '%v'", test.result, r) + t.Errorf("Expected URI '%s' to be '%v' , got '%v'", test.value, test.result, r) } } } @@ -49,10 +58,10 @@ func TestParseVolURI(t *testing.T) { {"192.168.1.1,test2:volume", "--volfile-id='volume' -s '192.168.1.1' -s 'test2'"}, } - for _, test := range tt { + for i, test := range tt { r := parseVolURI(test.value) if test.result != r { - t.Errorf("Expected to be '%v' , got '%v'", test.result, r) + t.Errorf("Expected test %d to be '%v' , got '%v'", i, test.result, r) } } }