client.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2023 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  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. package validation
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "slices"
  20. "github.com/samber/lo"
  21. v1 "github.com/fatedier/frp/pkg/config/v1"
  22. "github.com/fatedier/frp/pkg/policy/featuregate"
  23. "github.com/fatedier/frp/pkg/policy/security"
  24. )
  25. func (v *ConfigValidator) ValidateClientCommonConfig(c *v1.ClientCommonConfig) (Warning, error) {
  26. var (
  27. warnings Warning
  28. errs error
  29. )
  30. validators := []func() (Warning, error){
  31. func() (Warning, error) { return validateFeatureGates(c) },
  32. func() (Warning, error) { return v.validateAuthConfig(&c.Auth) },
  33. func() (Warning, error) { return nil, validateLogConfig(&c.Log) },
  34. func() (Warning, error) { return nil, validateWebServerConfig(&c.WebServer) },
  35. func() (Warning, error) { return validateTransportConfig(&c.Transport) },
  36. func() (Warning, error) { return validateIncludeFiles(c.IncludeConfigFiles) },
  37. }
  38. for _, validator := range validators {
  39. w, err := validator()
  40. warnings = AppendError(warnings, w)
  41. errs = AppendError(errs, err)
  42. }
  43. return warnings, errs
  44. }
  45. func validateFeatureGates(c *v1.ClientCommonConfig) (Warning, error) {
  46. if c.VirtualNet.Address != "" {
  47. if !featuregate.Enabled(featuregate.VirtualNet) {
  48. return nil, fmt.Errorf("VirtualNet feature is not enabled; enable it by setting the appropriate feature gate flag")
  49. }
  50. }
  51. return nil, nil
  52. }
  53. func (v *ConfigValidator) validateAuthConfig(c *v1.AuthClientConfig) (Warning, error) {
  54. var errs error
  55. if !slices.Contains(SupportedAuthMethods, c.Method) {
  56. errs = AppendError(errs, fmt.Errorf("invalid auth method, optional values are %v", SupportedAuthMethods))
  57. }
  58. if !lo.Every(SupportedAuthAdditionalScopes, c.AdditionalScopes) {
  59. errs = AppendError(errs, fmt.Errorf("invalid auth additional scopes, optional values are %v", SupportedAuthAdditionalScopes))
  60. }
  61. // Validate token/tokenSource mutual exclusivity
  62. if c.Token != "" && c.TokenSource != nil {
  63. errs = AppendError(errs, fmt.Errorf("cannot specify both auth.token and auth.tokenSource"))
  64. }
  65. // Validate tokenSource if specified
  66. if c.TokenSource != nil {
  67. if c.TokenSource.Type == "exec" {
  68. if err := v.ValidateUnsafeFeature(security.TokenSourceExec); err != nil {
  69. errs = AppendError(errs, err)
  70. }
  71. }
  72. if err := c.TokenSource.Validate(); err != nil {
  73. errs = AppendError(errs, fmt.Errorf("invalid auth.tokenSource: %v", err))
  74. }
  75. }
  76. if err := v.validateOIDCConfig(&c.OIDC); err != nil {
  77. errs = AppendError(errs, err)
  78. }
  79. return nil, errs
  80. }
  81. func (v *ConfigValidator) validateOIDCConfig(c *v1.AuthOIDCClientConfig) error {
  82. if c.TokenSource == nil {
  83. return nil
  84. }
  85. var errs error
  86. // Validate oidc.tokenSource mutual exclusivity with other fields of oidc
  87. if c.ClientID != "" || c.ClientSecret != "" || c.Audience != "" ||
  88. c.Scope != "" || c.TokenEndpointURL != "" || len(c.AdditionalEndpointParams) > 0 ||
  89. c.TrustedCaFile != "" || c.InsecureSkipVerify || c.ProxyURL != "" {
  90. errs = AppendError(errs, fmt.Errorf("cannot specify both auth.oidc.tokenSource and any other field of auth.oidc"))
  91. }
  92. if c.TokenSource.Type == "exec" {
  93. if err := v.ValidateUnsafeFeature(security.TokenSourceExec); err != nil {
  94. errs = AppendError(errs, err)
  95. }
  96. }
  97. if err := c.TokenSource.Validate(); err != nil {
  98. errs = AppendError(errs, fmt.Errorf("invalid auth.oidc.tokenSource: %v", err))
  99. }
  100. return errs
  101. }
  102. func validateTransportConfig(c *v1.ClientTransportConfig) (Warning, error) {
  103. var (
  104. warnings Warning
  105. errs error
  106. )
  107. if c.HeartbeatTimeout > 0 && c.HeartbeatInterval > 0 {
  108. if c.HeartbeatTimeout < c.HeartbeatInterval {
  109. errs = AppendError(errs, fmt.Errorf("invalid transport.heartbeatTimeout, heartbeat timeout should not less than heartbeat interval"))
  110. }
  111. }
  112. if !lo.FromPtr(c.TLS.Enable) {
  113. checkTLSConfig := func(name string, value string) Warning {
  114. if value != "" {
  115. return fmt.Errorf("%s is invalid when transport.tls.enable is false", name)
  116. }
  117. return nil
  118. }
  119. warnings = AppendError(warnings, checkTLSConfig("transport.tls.certFile", c.TLS.CertFile))
  120. warnings = AppendError(warnings, checkTLSConfig("transport.tls.keyFile", c.TLS.KeyFile))
  121. warnings = AppendError(warnings, checkTLSConfig("transport.tls.trustedCaFile", c.TLS.TrustedCaFile))
  122. }
  123. if !slices.Contains(SupportedTransportProtocols, c.Protocol) {
  124. errs = AppendError(errs, fmt.Errorf("invalid transport.protocol, optional values are %v", SupportedTransportProtocols))
  125. }
  126. return warnings, errs
  127. }
  128. func validateIncludeFiles(files []string) (Warning, error) {
  129. var errs error
  130. for _, f := range files {
  131. absDir, err := filepath.Abs(filepath.Dir(f))
  132. if err != nil {
  133. errs = AppendError(errs, fmt.Errorf("include: parse directory of %s failed: %v", f, err))
  134. continue
  135. }
  136. if _, err := os.Stat(absDir); os.IsNotExist(err) {
  137. errs = AppendError(errs, fmt.Errorf("include: directory of %s not exist", f))
  138. }
  139. }
  140. return nil, errs
  141. }
  142. func ValidateAllClientConfig(
  143. c *v1.ClientCommonConfig,
  144. proxyCfgs []v1.ProxyConfigurer,
  145. visitorCfgs []v1.VisitorConfigurer,
  146. unsafeFeatures *security.UnsafeFeatures,
  147. ) (Warning, error) {
  148. validator := NewConfigValidator(unsafeFeatures)
  149. var warnings Warning
  150. if c != nil {
  151. warning, err := validator.ValidateClientCommonConfig(c)
  152. warnings = AppendError(warnings, warning)
  153. if err != nil {
  154. return warnings, err
  155. }
  156. }
  157. for _, c := range proxyCfgs {
  158. if err := ValidateProxyConfigurerForClient(c); err != nil {
  159. return warnings, fmt.Errorf("proxy %s: %v", c.GetBaseConfig().Name, err)
  160. }
  161. }
  162. for _, c := range visitorCfgs {
  163. if err := ValidateVisitorConfigurer(c); err != nil {
  164. return warnings, fmt.Errorf("visitor %s: %v", c.GetBaseConfig().Name, err)
  165. }
  166. }
  167. return warnings, nil
  168. }