value_source.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2025 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 v1
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "os"
  20. "os/exec"
  21. "strings"
  22. )
  23. // ValueSource provides a way to dynamically resolve configuration values
  24. // from various sources like files, environment variables, or external services.
  25. type ValueSource struct {
  26. Type string `json:"type"`
  27. File *FileSource `json:"file,omitempty"`
  28. Exec *ExecSource `json:"exec,omitempty"`
  29. }
  30. // FileSource specifies how to load a value from a file.
  31. type FileSource struct {
  32. Path string `json:"path"`
  33. }
  34. // ExecSource specifies how to get a value from another program launched as subprocess.
  35. type ExecSource struct {
  36. Command string `json:"command"`
  37. Args []string `json:"args,omitempty"`
  38. Env []ExecEnvVar `json:"env,omitempty"`
  39. }
  40. type ExecEnvVar struct {
  41. Name string `json:"name"`
  42. Value string `json:"value"`
  43. }
  44. // Validate validates the ValueSource configuration.
  45. func (v *ValueSource) Validate() error {
  46. if v == nil {
  47. return errors.New("valueSource cannot be nil")
  48. }
  49. switch v.Type {
  50. case "file":
  51. if v.File == nil {
  52. return errors.New("file configuration is required when type is 'file'")
  53. }
  54. return v.File.Validate()
  55. case "exec":
  56. if v.Exec == nil {
  57. return errors.New("exec configuration is required when type is 'exec'")
  58. }
  59. return v.Exec.Validate()
  60. default:
  61. return fmt.Errorf("unsupported value source type: %s (only 'file' and 'exec' are supported)", v.Type)
  62. }
  63. }
  64. // Resolve resolves the value from the configured source.
  65. func (v *ValueSource) Resolve(ctx context.Context) (string, error) {
  66. if err := v.Validate(); err != nil {
  67. return "", err
  68. }
  69. switch v.Type {
  70. case "file":
  71. return v.File.Resolve(ctx)
  72. case "exec":
  73. return v.Exec.Resolve(ctx)
  74. default:
  75. return "", fmt.Errorf("unsupported value source type: %s", v.Type)
  76. }
  77. }
  78. // Validate validates the FileSource configuration.
  79. func (f *FileSource) Validate() error {
  80. if f == nil {
  81. return errors.New("fileSource cannot be nil")
  82. }
  83. if f.Path == "" {
  84. return errors.New("file path cannot be empty")
  85. }
  86. return nil
  87. }
  88. // Resolve reads and returns the content from the specified file.
  89. func (f *FileSource) Resolve(_ context.Context) (string, error) {
  90. if err := f.Validate(); err != nil {
  91. return "", err
  92. }
  93. content, err := os.ReadFile(f.Path)
  94. if err != nil {
  95. return "", fmt.Errorf("failed to read file %s: %v", f.Path, err)
  96. }
  97. // Trim whitespace, which is important for file-based tokens
  98. return strings.TrimSpace(string(content)), nil
  99. }
  100. // Validate validates the ExecSource configuration.
  101. func (e *ExecSource) Validate() error {
  102. if e == nil {
  103. return errors.New("execSource cannot be nil")
  104. }
  105. if e.Command == "" {
  106. return errors.New("exec command cannot be empty")
  107. }
  108. for _, env := range e.Env {
  109. if env.Name == "" {
  110. return errors.New("exec env name cannot be empty")
  111. }
  112. if strings.Contains(env.Name, "=") {
  113. return errors.New("exec env name cannot contain '='")
  114. }
  115. }
  116. return nil
  117. }
  118. // Resolve reads and returns the content captured from stdout of launched subprocess.
  119. func (e *ExecSource) Resolve(ctx context.Context) (string, error) {
  120. if err := e.Validate(); err != nil {
  121. return "", err
  122. }
  123. cmd := exec.CommandContext(ctx, e.Command, e.Args...)
  124. if len(e.Env) != 0 {
  125. cmd.Env = os.Environ()
  126. for _, env := range e.Env {
  127. cmd.Env = append(cmd.Env, env.Name+"="+env.Value)
  128. }
  129. }
  130. content, err := cmd.Output()
  131. if err != nil {
  132. return "", fmt.Errorf("failed to execute command %v: %v", e.Command, err)
  133. }
  134. // Trim whitespace, which is important for exec-based tokens
  135. return strings.TrimSpace(string(content)), nil
  136. }