client.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 ValidateClientCommonConfig(c *v1.ClientCommonConfig, unsafeFeatures *security.UnsafeFeatures) (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 validateAuthConfig(&c.Auth, unsafeFeatures) },
  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 _, v := range validators {
  39. w, err := v()
  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 validateAuthConfig(c *v1.AuthClientConfig, unsafeFeatures *security.UnsafeFeatures) (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 !unsafeFeatures.IsEnabled(security.TokenSourceExec) {
  69. errs = AppendError(errs, fmt.Errorf("unsafe feature %q is not enabled. "+
  70. "To enable it, start frpc with '--allow-unsafe %s'", security.TokenSourceExec, security.TokenSourceExec))
  71. }
  72. }
  73. if err := c.TokenSource.Validate(); err != nil {
  74. errs = AppendError(errs, fmt.Errorf("invalid auth.tokenSource: %v", err))
  75. }
  76. }
  77. if err := validateOIDCConfig(&c.OIDC, unsafeFeatures); err != nil {
  78. errs = AppendError(errs, err)
  79. }
  80. return nil, errs
  81. }
  82. func validateOIDCConfig(c *v1.AuthOIDCClientConfig, unsafeFeatures *security.UnsafeFeatures) error {
  83. if c.TokenSource == nil {
  84. return nil
  85. }
  86. var errs error
  87. // Validate oidc.tokenSource mutual exclusivity with other fields of oidc
  88. if c.ClientID != "" || c.ClientSecret != "" || c.Audience != "" ||
  89. c.Scope != "" || c.TokenEndpointURL != "" || len(c.AdditionalEndpointParams) > 0 ||
  90. c.TrustedCaFile != "" || c.InsecureSkipVerify || c.ProxyURL != "" {
  91. errs = AppendError(errs, fmt.Errorf("cannot specify both auth.oidc.tokenSource and any other field of auth.oidc"))
  92. }
  93. if c.TokenSource.Type == "exec" {
  94. if !unsafeFeatures.IsEnabled(security.TokenSourceExec) {
  95. errs = AppendError(errs, fmt.Errorf("unsafe feature %q is not enabled. "+
  96. "To enable it, start frpc with '--allow-unsafe %s'", security.TokenSourceExec, security.TokenSourceExec))
  97. }
  98. }
  99. if err := c.TokenSource.Validate(); err != nil {
  100. errs = AppendError(errs, fmt.Errorf("invalid auth.oidc.tokenSource: %v", err))
  101. }
  102. return errs
  103. }
  104. func validateTransportConfig(c *v1.ClientTransportConfig) (Warning, error) {
  105. var (
  106. warnings Warning
  107. errs error
  108. )
  109. if c.HeartbeatTimeout > 0 && c.HeartbeatInterval > 0 {
  110. if c.HeartbeatTimeout < c.HeartbeatInterval {
  111. errs = AppendError(errs, fmt.Errorf("invalid transport.heartbeatTimeout, heartbeat timeout should not less than heartbeat interval"))
  112. }
  113. }
  114. if !lo.FromPtr(c.TLS.Enable) {
  115. checkTLSConfig := func(name string, value string) Warning {
  116. if value != "" {
  117. return fmt.Errorf("%s is invalid when transport.tls.enable is false", name)
  118. }
  119. return nil
  120. }
  121. warnings = AppendError(warnings, checkTLSConfig("transport.tls.certFile", c.TLS.CertFile))
  122. warnings = AppendError(warnings, checkTLSConfig("transport.tls.keyFile", c.TLS.KeyFile))
  123. warnings = AppendError(warnings, checkTLSConfig("transport.tls.trustedCaFile", c.TLS.TrustedCaFile))
  124. }
  125. if !slices.Contains(SupportedTransportProtocols, c.Protocol) {
  126. errs = AppendError(errs, fmt.Errorf("invalid transport.protocol, optional values are %v", SupportedTransportProtocols))
  127. }
  128. return warnings, errs
  129. }
  130. func validateIncludeFiles(files []string) (Warning, error) {
  131. var errs error
  132. for _, f := range files {
  133. absDir, err := filepath.Abs(filepath.Dir(f))
  134. if err != nil {
  135. errs = AppendError(errs, fmt.Errorf("include: parse directory of %s failed: %v", f, err))
  136. continue
  137. }
  138. if _, err := os.Stat(absDir); os.IsNotExist(err) {
  139. errs = AppendError(errs, fmt.Errorf("include: directory of %s not exist", f))
  140. }
  141. }
  142. return nil, errs
  143. }
  144. func ValidateAllClientConfig(
  145. c *v1.ClientCommonConfig,
  146. proxyCfgs []v1.ProxyConfigurer,
  147. visitorCfgs []v1.VisitorConfigurer,
  148. unsafeFeatures *security.UnsafeFeatures,
  149. ) (Warning, error) {
  150. var warnings Warning
  151. if c != nil {
  152. warning, err := ValidateClientCommonConfig(c, unsafeFeatures)
  153. warnings = AppendError(warnings, warning)
  154. if err != nil {
  155. return warnings, err
  156. }
  157. }
  158. for _, c := range proxyCfgs {
  159. if err := ValidateProxyConfigurerForClient(c); err != nil {
  160. return warnings, fmt.Errorf("proxy %s: %v", c.GetBaseConfig().Name, err)
  161. }
  162. }
  163. for _, c := range visitorCfgs {
  164. if err := ValidateVisitorConfigurer(c); err != nil {
  165. return warnings, fmt.Errorf("visitor %s: %v", c.GetBaseConfig().Name, err)
  166. }
  167. }
  168. return warnings, nil
  169. }