client.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/featuregate"
  23. )
  24. func ValidateClientCommonConfig(c *v1.ClientCommonConfig, unsafeFeatures v1.UnsafeFeatures) (Warning, error) {
  25. var (
  26. warnings Warning
  27. errs error
  28. )
  29. // validate feature gates
  30. if c.VirtualNet.Address != "" {
  31. if !featuregate.Enabled(featuregate.VirtualNet) {
  32. return warnings, fmt.Errorf("VirtualNet feature is not enabled; enable it by setting the appropriate feature gate flag")
  33. }
  34. }
  35. if !slices.Contains(SupportedAuthMethods, c.Auth.Method) {
  36. errs = AppendError(errs, fmt.Errorf("invalid auth method, optional values are %v", SupportedAuthMethods))
  37. }
  38. if !lo.Every(SupportedAuthAdditionalScopes, c.Auth.AdditionalScopes) {
  39. errs = AppendError(errs, fmt.Errorf("invalid auth additional scopes, optional values are %v", SupportedAuthAdditionalScopes))
  40. }
  41. // Validate token/tokenSource mutual exclusivity
  42. if c.Auth.Token != "" && c.Auth.TokenSource != nil {
  43. errs = AppendError(errs, fmt.Errorf("cannot specify both auth.token and auth.tokenSource"))
  44. }
  45. // Validate tokenSource if specified
  46. if c.Auth.TokenSource != nil {
  47. if c.Auth.TokenSource.Type == "exec" && !unsafeFeatures.IsEnabled(v1.UnsafeFeatureTokenSourceExec) {
  48. errs = AppendError(errs, fmt.Errorf("unsafe 'exec' not allowed for auth.tokenSource.type"))
  49. }
  50. if err := c.Auth.TokenSource.Validate(); err != nil {
  51. errs = AppendError(errs, fmt.Errorf("invalid auth.tokenSource: %v", err))
  52. }
  53. }
  54. if c.Auth.OIDC.TokenSource != nil {
  55. // Validate oidc.tokenSource mutual exclusivity with other fields of oidc
  56. if c.Auth.OIDC.ClientID != "" || c.Auth.OIDC.ClientSecret != "" || c.Auth.OIDC.Audience != "" ||
  57. c.Auth.OIDC.Scope != "" || c.Auth.OIDC.TokenEndpointURL != "" || len(c.Auth.OIDC.AdditionalEndpointParams) > 0 ||
  58. c.Auth.OIDC.TrustedCaFile != "" || c.Auth.OIDC.InsecureSkipVerify || c.Auth.OIDC.ProxyURL != "" {
  59. errs = AppendError(errs, fmt.Errorf("cannot specify both auth.oidc.tokenSource and any other field of auth.oidc"))
  60. }
  61. if c.Auth.OIDC.TokenSource.Type == "exec" && !unsafeFeatures.IsEnabled(v1.UnsafeFeatureTokenSourceExec) {
  62. errs = AppendError(errs, fmt.Errorf("unsafe 'exec' not allowed for auth.oidc.tokenSource.type"))
  63. }
  64. }
  65. if err := validateLogConfig(&c.Log); err != nil {
  66. errs = AppendError(errs, err)
  67. }
  68. if err := validateWebServerConfig(&c.WebServer); err != nil {
  69. errs = AppendError(errs, err)
  70. }
  71. if c.Transport.HeartbeatTimeout > 0 && c.Transport.HeartbeatInterval > 0 {
  72. if c.Transport.HeartbeatTimeout < c.Transport.HeartbeatInterval {
  73. errs = AppendError(errs, fmt.Errorf("invalid transport.heartbeatTimeout, heartbeat timeout should not less than heartbeat interval"))
  74. }
  75. }
  76. if !lo.FromPtr(c.Transport.TLS.Enable) {
  77. checkTLSConfig := func(name string, value string) Warning {
  78. if value != "" {
  79. return fmt.Errorf("%s is invalid when transport.tls.enable is false", name)
  80. }
  81. return nil
  82. }
  83. warnings = AppendError(warnings, checkTLSConfig("transport.tls.certFile", c.Transport.TLS.CertFile))
  84. warnings = AppendError(warnings, checkTLSConfig("transport.tls.keyFile", c.Transport.TLS.KeyFile))
  85. warnings = AppendError(warnings, checkTLSConfig("transport.tls.trustedCaFile", c.Transport.TLS.TrustedCaFile))
  86. }
  87. if !slices.Contains(SupportedTransportProtocols, c.Transport.Protocol) {
  88. errs = AppendError(errs, fmt.Errorf("invalid transport.protocol, optional values are %v", SupportedTransportProtocols))
  89. }
  90. for _, f := range c.IncludeConfigFiles {
  91. absDir, err := filepath.Abs(filepath.Dir(f))
  92. if err != nil {
  93. errs = AppendError(errs, fmt.Errorf("include: parse directory of %s failed: %v", f, err))
  94. continue
  95. }
  96. if _, err := os.Stat(absDir); os.IsNotExist(err) {
  97. errs = AppendError(errs, fmt.Errorf("include: directory of %s not exist", f))
  98. }
  99. }
  100. return warnings, errs
  101. }
  102. func ValidateAllClientConfig(
  103. c *v1.ClientCommonConfig,
  104. proxyCfgs []v1.ProxyConfigurer,
  105. visitorCfgs []v1.VisitorConfigurer,
  106. unsafeFeatures v1.UnsafeFeatures,
  107. ) (Warning, error) {
  108. var warnings Warning
  109. if c != nil {
  110. warning, err := ValidateClientCommonConfig(c, unsafeFeatures)
  111. warnings = AppendError(warnings, warning)
  112. if err != nil {
  113. return warnings, err
  114. }
  115. }
  116. for _, c := range proxyCfgs {
  117. if err := ValidateProxyConfigurerForClient(c); err != nil {
  118. return warnings, fmt.Errorf("proxy %s: %v", c.GetBaseConfig().Name, err)
  119. }
  120. }
  121. for _, c := range visitorCfgs {
  122. if err := ValidateVisitorConfigurer(c); err != nil {
  123. return warnings, fmt.Errorf("visitor %s: %v", c.GetBaseConfig().Name, err)
  124. }
  125. }
  126. return warnings, nil
  127. }