-
Notifications
You must be signed in to change notification settings - Fork 2
/
validators.go
63 lines (50 loc) · 1.57 KB
/
validators.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
package forward
import (
"errors"
)
var (
ErrJumpHostsNotSupportd = errors.New("only 1 jump host is supported atm") //nolint: revive
ErrLocalAndRemoteAddressUnset = errors.New("localAddress and RemoteAddress have to be set") //nolint: revive
ErrSSHConfigUnset = errors.New("SSHConfig cannot be nil")
ErrUserEmpty = errors.New("user cannot be empty")
ErrAddressEmpty = errors.New("address cannot be empty")
ErrPrivateKeyFileAndPasswordUnset = errors.New("either PrivateKeyFile or Password has to be set")
ErrConfigIsNil = errors.New("Config cannot be nil")
)
// checkConfig checks the config if it is feasible.
func checkConfig(config *Config) error {
if config == nil {
return ErrConfigIsNil
}
if len(config.JumpHostConfigs) > 1 {
return ErrJumpHostsNotSupportd
}
for _, jumpConfig := range config.JumpHostConfigs {
if err := checkSSHConfig(jumpConfig); err != nil {
return err
}
}
if err := checkSSHConfig(config.EndHostConfig); err != nil {
return err
}
if config.LocalAddress == "" || config.RemoteAddress == "" {
return ErrLocalAndRemoteAddressUnset
}
return nil
}
// checkSSHConfig checks the ssh config for feasibility.
func checkSSHConfig(sshConfig *SSHConfig) error {
if sshConfig == nil {
return ErrSSHConfigUnset
}
if sshConfig.User == "" {
return ErrUserEmpty
}
if sshConfig.Address == "" {
return ErrAddressEmpty
}
if sshConfig.PrivateKeyFile == "" && sshConfig.Password == "" {
return ErrPrivateKeyFileAndPasswordUnset
}
return nil
}