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