-
Notifications
You must be signed in to change notification settings - Fork 3
/
validator.go
132 lines (107 loc) · 3.87 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package validator
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
)
const (
minRefreshTokenDuration = 15
)
var (
ErrNoValidFile = errors.New("no valid file")
validFile = regexp.MustCompile(`^([a-zA-Z_\:\\\-\s0-9\.\/]+)+$`)
ErrNoValidLogFile = errors.New("no valid log file")
validLogFile = regexp.MustCompile(`^([a-zA-Z_\:\\\-\s0-9\.\/]+)+(\.log)$`)
ErrNoValidPath = errors.New("no valid path")
validPath = regexp.MustCompile(`^([a-z_\-\s0-9\.\/]+)+$`)
ErrNoValidEndpoint = errors.New("no valid s3 endpoint")
ErrNoValidWebURL = errors.New("no valid web URL")
validWebURL = regexp.MustCompile(`^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)?(?:localhost)?[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$`)
ErrNoValidS3RemotePath = errors.New("no valid s3 remote path")
validRemotePath = regexp.MustCompile(`^/[\w\-_/]+$`)
ErrNoValidInstanceName = errors.New("no valid instance name")
validInstanceName = regexp.MustCompile(`^[\w\-_]+$`)
ErrNoValidRefreshTokenRenewTime = errors.New("no valid refresh token time duration: min 15min")
)
// RefreshTokenRenew checks if the number of minutes are valid: minimum is 15min.
func RefreshTokenRenew(minutes int) (bool, error) {
if minutes < minRefreshTokenDuration {
return false, ErrNoValidRefreshTokenRenewTime
}
return true, nil
}
// LogFile checks if the path indicated for the log file is valid.
func LogFile(logFilePath string) (bool, error) {
if !validLogFile.MatchString(logFilePath) {
return false, ErrNoValidLogFile
}
return true, nil
}
// InstanceName checks if the user instance name is valid.
func InstanceName(url string) (bool, error) {
if !validInstanceName.MatchString(url) {
return false, ErrNoValidInstanceName
}
return true, nil
}
// WebURL checks if the given URL is a valid Web URL: http://example.com:port
func WebURL(url string) (bool, error) {
if !validWebURL.MatchString(url) {
return false, ErrNoValidWebURL
}
return true, nil
}
// S3Endpoint checks if the given endpoint is a valid s3 endpoint.
func S3Endpoint(endpoint string) (bool, error) {
if valid, err := WebURL(endpoint); err != nil {
return valid, ErrNoValidEndpoint
}
// The ending slash is not required
// reference: https://github.com/minio/minio/blob/master/docs/sts/web-identity.md#sample-post-request
// if endpoint[len(endpoint)-1] != '/' {
// return false, ErrNoValidEndpoint
// }
return true, nil
}
// RemotePath checks if the remote path is valid (a valid bucket path: /something...).
func RemotePath(remotePath string) (bool, error) {
if !validRemotePath.MatchString(remotePath) {
return false, ErrNoValidS3RemotePath
}
return true, nil
}
// LocalPath verify if the path indicated could be a proper mountpoint.
func LocalPath(localMountPoint string) (bool, error) {
if localMountPoint == "/" {
return false, ErrNoValidPath
}
if !validPath.MatchString(localMountPoint) {
return false, ErrNoValidPath
}
absPath, errAbs := filepath.Abs(filepath.Clean(localMountPoint))
if errAbs != nil {
return false, fmt.Errorf("no valid path: %w", errAbs)
}
_, errStat := os.Stat(absPath)
if errStat != nil && !os.IsNotExist(errStat) {
return false, fmt.Errorf("no valid path: %w", errStat)
}
// TODO: better check over folder permissions
// errMkdir := os.MkdirAll(absPath, os.ModePerm)
// if errMkdir != nil {
// return false, fmt.Errorf("no valid path: %w", errStat)
// }
// os.RemoveAll(absPath)
parentFolder, errStat := os.Stat(filepath.Dir(absPath))
if errStat != nil {
return false, fmt.Errorf("no valid path: %w", errStat)
}
if !parentFolder.IsDir() || os.IsNotExist(errStat) {
return false, fmt.Errorf("%w: %s is not valid because it has not a parent folder",
ErrNoValidPath, localMountPoint,
)
}
return true, nil
}