value_source.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "strings"
  21. )
  22. // ValueSource provides a way to dynamically resolve configuration values
  23. // from various sources like files, environment variables, or external services.
  24. type ValueSource struct {
  25. Type string `json:"type"`
  26. File *FileSource `json:"file,omitempty"`
  27. }
  28. // FileSource specifies how to load a value from a file.
  29. type FileSource struct {
  30. Path string `json:"path"`
  31. }
  32. // Validate validates the ValueSource configuration.
  33. func (v *ValueSource) Validate() error {
  34. if v == nil {
  35. return errors.New("valueSource cannot be nil")
  36. }
  37. switch v.Type {
  38. case "file":
  39. if v.File == nil {
  40. return errors.New("file configuration is required when type is 'file'")
  41. }
  42. return v.File.Validate()
  43. default:
  44. return fmt.Errorf("unsupported value source type: %s (only 'file' is supported)", v.Type)
  45. }
  46. }
  47. // Resolve resolves the value from the configured source.
  48. func (v *ValueSource) Resolve(ctx context.Context) (string, error) {
  49. if err := v.Validate(); err != nil {
  50. return "", err
  51. }
  52. switch v.Type {
  53. case "file":
  54. return v.File.Resolve(ctx)
  55. default:
  56. return "", fmt.Errorf("unsupported value source type: %s", v.Type)
  57. }
  58. }
  59. // Validate validates the FileSource configuration.
  60. func (f *FileSource) Validate() error {
  61. if f == nil {
  62. return errors.New("fileSource cannot be nil")
  63. }
  64. if f.Path == "" {
  65. return errors.New("file path cannot be empty")
  66. }
  67. return nil
  68. }
  69. // Resolve reads and returns the content from the specified file.
  70. func (f *FileSource) Resolve(_ context.Context) (string, error) {
  71. if err := f.Validate(); err != nil {
  72. return "", err
  73. }
  74. content, err := os.ReadFile(f.Path)
  75. if err != nil {
  76. return "", fmt.Errorf("failed to read file %s: %v", f.Path, err)
  77. }
  78. // Trim whitespace, which is important for file-based tokens
  79. return strings.TrimSpace(string(content)), nil
  80. }