| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package validation |
| 16 |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "os" |
| 20 | "path/filepath" |
| 21 | "slices" |
| 22 |
| 23 | "github.com/samber/lo" |
| 24 |
| 25 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 26 | "github.com/fatedier/frp/pkg/policy/featuregate" |
| 27 | "github.com/fatedier/frp/pkg/policy/security" |
| 28 | ) |
| 29 |
| 30 | func (v *ConfigValidator) ValidateClientCommonConfig(c *v1.ClientCommonConfig) (Warning, error) { |
| 31 | var ( |
| 32 | warnings Warning |
| 33 | errs error |
| 34 | ) |
| 35 |
| 36 | validators := []func() (Warning, error){ |
| 37 | func() (Warning, error) { return validateFeatureGates(c) }, |
| 38 | func() (Warning, error) { return v.validateAuthConfig(&c.Auth) }, |
| 39 | func() (Warning, error) { return nil, validateLogConfig(&c.Log) }, |
| 40 | func() (Warning, error) { return nil, validateWebServerConfig(&c.WebServer) }, |
| 41 | func() (Warning, error) { return validateTransportConfig(&c.Transport) }, |
| 42 | func() (Warning, error) { return validateIncludeFiles(c.IncludeConfigFiles) }, |
| 43 | } |
| 44 |
| 45 | for _, validator := range validators { |
| 46 | w, err := validator() |
| 47 | warnings = AppendError(warnings, w) |
| 48 | errs = AppendError(errs, err) |
| 49 | } |
| 50 | return warnings, errs |
| 51 | } |
| 52 |
| 53 | func validateFeatureGates(c *v1.ClientCommonConfig) (Warning, error) { |
| 54 | if c.VirtualNet.Address != "" { |
| 55 | if !featuregate.Enabled(featuregate.VirtualNet) { |
| 56 | return nil, fmt.Errorf("VirtualNet feature is not enabled; enable it by setting the appropriate feature gate flag") |
| 57 | } |
| 58 | } |
| 59 | return nil, nil |
| 60 | } |
| 61 |
| 62 | func (v *ConfigValidator) validateAuthConfig(c *v1.AuthClientConfig) (Warning, error) { |
| 63 | var errs error |
| 64 | if !slices.Contains(SupportedAuthMethods, c.Method) { |
| 65 | errs = AppendError(errs, fmt.Errorf("invalid auth method, optional values are %v", SupportedAuthMethods)) |
| 66 | } |
| 67 | if !lo.Every(SupportedAuthAdditionalScopes, c.AdditionalScopes) { |
| 68 | errs = AppendError(errs, fmt.Errorf("invalid auth additional scopes, optional values are %v", SupportedAuthAdditionalScopes)) |
| 69 | } |
| 70 |
| 71 | errs = AppendError(errs, v.validateAuthTokenSource(c.Token, c.TokenSource)) |
| 72 |
| 73 | if err := v.validateOIDCConfig(&c.OIDC); err != nil { |
| 74 | errs = AppendError(errs, err) |
| 75 | } |
| 76 | if c.Method == v1.AuthMethodOIDC && c.OIDC.TokenSource == nil { |
| 77 | if err := ValidateOIDCClientCredentialsConfig(&c.OIDC); err != nil { |
| 78 | errs = AppendError(errs, err) |
| 79 | } |
| 80 | } |
| 81 | return nil, errs |
| 82 | } |
| 83 |
| 84 | func (v *ConfigValidator) validateOIDCConfig(c *v1.AuthOIDCClientConfig) error { |
| 85 | if c.TokenSource == nil { |
| 86 | return nil |
| 87 | } |
| 88 | var errs error |
| 89 | // Validate oidc.tokenSource mutual exclusivity with other fields of oidc |
| 90 | if c.ClientID != "" || c.ClientSecret != "" || c.Audience != "" || |
| 91 | c.Scope != "" || c.TokenEndpointURL != "" || len(c.AdditionalEndpointParams) > 0 || |
| 92 | c.TrustedCaFile != "" || c.InsecureSkipVerify || c.ProxyURL != "" { |
| 93 | errs = AppendError(errs, fmt.Errorf("cannot specify both auth.oidc.tokenSource and any other field of auth.oidc")) |
| 94 | } |
| 95 | if c.TokenSource.Type == "exec" { |
| 96 | if err := v.ValidateUnsafeFeature(security.TokenSourceExec); err != nil { |
| 97 | errs = AppendError(errs, err) |
| 98 | } |
| 99 | } |
| 100 | if err := c.TokenSource.Validate(); err != nil { |
| 101 | errs = AppendError(errs, fmt.Errorf("invalid auth.oidc.tokenSource: %v", err)) |
| 102 | } |
| 103 | return errs |
| 104 | } |
| 105 |
| 106 | func validateTransportConfig(c *v1.ClientTransportConfig) (Warning, error) { |
| 107 | var ( |
| 108 | warnings Warning |
| 109 | errs error |
| 110 | ) |
| 111 |
| 112 | if c.HeartbeatTimeout > 0 && c.HeartbeatInterval > 0 { |
| 113 | if c.HeartbeatTimeout < c.HeartbeatInterval { |
| 114 | errs = AppendError(errs, fmt.Errorf("invalid transport.heartbeatTimeout, heartbeat timeout should not less than heartbeat interval")) |
| 115 | } |
| 116 | } |
| 117 |
| 118 | if !lo.FromPtr(c.TLS.Enable) { |
| 119 | checkTLSConfig := func(name string, value string) Warning { |
| 120 | if value != "" { |
| 121 | return fmt.Errorf("%s is invalid when transport.tls.enable is false", name) |
| 122 | } |
| 123 | return nil |
| 124 | } |
| 125 |
| 126 | warnings = AppendError(warnings, checkTLSConfig("transport.tls.certFile", c.TLS.CertFile)) |
| 127 | warnings = AppendError(warnings, checkTLSConfig("transport.tls.keyFile", c.TLS.KeyFile)) |
| 128 | warnings = AppendError(warnings, checkTLSConfig("transport.tls.trustedCaFile", c.TLS.TrustedCaFile)) |
| 129 | } |
| 130 |
| 131 | if !slices.Contains(SupportedTransportProtocols, c.Protocol) { |
| 132 | errs = AppendError(errs, fmt.Errorf("invalid transport.protocol, optional values are %v", SupportedTransportProtocols)) |
| 133 | } |
| 134 | if !slices.Contains(SupportedWireProtocols, c.WireProtocol) { |
| 135 | errs = AppendError(errs, fmt.Errorf("invalid transport.wireProtocol, optional values are %v", SupportedWireProtocols)) |
| 136 | } |
| 137 | return warnings, errs |
| 138 | } |
| 139 |
| 140 | func validateIncludeFiles(files []string) (Warning, error) { |
| 141 | var errs error |
| 142 | for _, f := range files { |
| 143 | absDir, err := filepath.Abs(filepath.Dir(f)) |
| 144 | if err != nil { |
| 145 | errs = AppendError(errs, fmt.Errorf("include: parse directory of %s failed: %v", f, err)) |
| 146 | continue |
| 147 | } |
| 148 | if _, err := os.Stat(absDir); os.IsNotExist(err) { |
| 149 | errs = AppendError(errs, fmt.Errorf("include: directory of %s not exist", f)) |
| 150 | } |
| 151 | } |
| 152 | return nil, errs |
| 153 | } |
| 154 |
| 155 | func ValidateAllClientConfig( |
| 156 | c *v1.ClientCommonConfig, |
| 157 | proxyCfgs []v1.ProxyConfigurer, |
| 158 | visitorCfgs []v1.VisitorConfigurer, |
| 159 | unsafeFeatures *security.UnsafeFeatures, |
| 160 | ) (Warning, error) { |
| 161 | validator := NewConfigValidator(unsafeFeatures) |
| 162 | var warnings Warning |
| 163 | if c != nil { |
| 164 | warning, err := validator.ValidateClientCommonConfig(c) |
| 165 | warnings = AppendError(warnings, warning) |
| 166 | if err != nil { |
| 167 | return warnings, err |
| 168 | } |
| 169 | } |
| 170 |
| 171 | for _, c := range proxyCfgs { |
| 172 | if err := ValidateProxyConfigurerForClient(c); err != nil { |
| 173 | return warnings, fmt.Errorf("proxy %s: %v", c.GetBaseConfig().Name, err) |
| 174 | } |
| 175 | } |
| 176 |
| 177 | for _, c := range visitorCfgs { |
| 178 | if err := ValidateVisitorConfigurer(c); err != nil { |
| 179 | return warnings, fmt.Errorf("visitor %s: %v", c.GetBaseConfig().Name, err) |
| 180 | } |
| 181 | } |
| 182 | return warnings, nil |
| 183 | } |