| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package exec |
| 5 |
|
| 6 |
import ( |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"os/exec" |
| 10 |
"path/filepath" |
| 11 |
|
| 12 |
"sigs.k8s.io/kustomize/kyaml/errors" |
| 13 |
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil" |
| 14 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 15 |
) |
| 16 |
|
| 17 |
type Filter struct { |
| 18 |
|
| 19 |
Path string `yaml:"path,omitempty"` |
| 20 |
|
| 21 |
|
| 22 |
Args []string `yaml:"args,omitempty"` |
| 23 |
|
| 24 |
|
| 25 |
Env []string `yaml:"env,omitempty"` |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
WorkingDir string |
| 30 |
|
| 31 |
runtimeutil.FunctionFilter |
| 32 |
} |
| 33 |
|
| 34 |
func (c *Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) { |
| 35 |
c.FunctionFilter.Run = c.Run |
| 36 |
return c.FunctionFilter.Filter(nodes) |
| 37 |
} |
| 38 |
|
| 39 |
func (c *Filter) Run(reader io.Reader, writer io.Writer) error { |
| 40 |
cmd := exec.Command(c.Path, c.Args...) |
| 41 |
cmd.Env = append(os.Environ(), c.Env...) |
| 42 |
cmd.Stdin = reader |
| 43 |
cmd.Stdout = writer |
| 44 |
cmd.Stderr = os.Stderr |
| 45 |
if c.WorkingDir == "" { |
| 46 |
return errors.Errorf("no working directory set for exec function") |
| 47 |
} |
| 48 |
if !filepath.IsAbs(c.WorkingDir) { |
| 49 |
return errors.Errorf( |
| 50 |
"relative working directory %s not allowed", c.WorkingDir) |
| 51 |
} |
| 52 |
if c.WorkingDir == "/" { |
| 53 |
return errors.Errorf( |
| 54 |
"root working directory '/' not allowed") |
| 55 |
} |
| 56 |
cmd.Dir = c.WorkingDir |
| 57 |
return cmd.Run() |
| 58 |
} |