feat(jobs): expand environment variables in paths
This commit is contained in:
+52
-16
@@ -60,8 +60,9 @@ inputs, output and options:
|
||||
logfile: .relspec/log/build-schema.log
|
||||
|
||||
Rules and guarantees:
|
||||
- command is a closed allow-list (convert, merge, scripts-list, templ). Arbitrary
|
||||
shell strings are never executed.
|
||||
- command is a closed allow-list (convert, merge, split, scripts-list,
|
||||
scripts-exec, templ, inspect, diff). Arbitrary shell strings are never
|
||||
executed.
|
||||
- Every path is relative to the directory holding the job file and may not
|
||||
escape it. Absolute and home-relative paths are rejected.
|
||||
- Remote database credentials are referenced by environment-variable name
|
||||
@@ -246,22 +247,37 @@ type resolvedInput struct {
|
||||
func preflightJob(j *jobs.Job, resolvedByName map[string]*resolvedJob) (*resolvedJob, error) {
|
||||
root := j.Dir()
|
||||
rj := &resolvedJob{job: j, root: root, logPolicy: j.ResolvedLogPolicy()}
|
||||
expandPath := func(label, value string) (string, error) {
|
||||
expanded, err := jobs.ExpandEnv(value)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: %w", label, err)
|
||||
}
|
||||
return expanded, nil
|
||||
}
|
||||
|
||||
if j.Logfile != "" {
|
||||
p, err := jobs.SafeJoin(root, j.Logfile)
|
||||
logfile, err := expandPath("logfile", j.Logfile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := jobs.SafeJoin(root, logfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("logfile: %w", err)
|
||||
}
|
||||
rj.logPath = p
|
||||
}
|
||||
if j.Template != "" {
|
||||
p, err := jobs.SafeJoin(root, j.Template)
|
||||
templatePath, err := expandPath("template", j.Template)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := jobs.SafeJoin(root, templatePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("template: %w", err)
|
||||
}
|
||||
info, err := os.Stat(p)
|
||||
if err != nil || info.IsDir() {
|
||||
return nil, fmt.Errorf("template %q: not found or is a directory", j.Template)
|
||||
return nil, fmt.Errorf("template %q: not found or is a directory", templatePath)
|
||||
}
|
||||
rj.templatePath = p
|
||||
}
|
||||
@@ -291,16 +307,20 @@ func preflightJob(j *jobs.Job, resolvedByName map[string]*resolvedJob) (*resolve
|
||||
ri.connEnv = in.ConnEnv
|
||||
rj.secrets = append(rj.secrets, v)
|
||||
} else {
|
||||
p, err := jobs.SafeJoin(root, in.Path)
|
||||
inputPath, err := expandPath(fmt.Sprintf("input[%d]", i), in.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := jobs.SafeJoin(root, inputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("input[%d]: %w", i, err)
|
||||
}
|
||||
info, err := os.Stat(p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("input[%d]: %s: file not found", i, in.Path)
|
||||
return nil, fmt.Errorf("input[%d]: %s: file not found", i, inputPath)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil, fmt.Errorf("input[%d]: %s: is a directory, not a file", i, in.Path)
|
||||
return nil, fmt.Errorf("input[%d]: %s: is a directory, not a file", i, inputPath)
|
||||
}
|
||||
ri.path = p
|
||||
}
|
||||
@@ -308,13 +328,17 @@ func preflightJob(j *jobs.Job, resolvedByName map[string]*resolvedJob) (*resolve
|
||||
}
|
||||
|
||||
for _, d := range j.ScriptDirs {
|
||||
p, err := jobs.SafeJoin(root, d)
|
||||
scriptDir, err := expandPath("script_dir", d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := jobs.SafeJoin(root, scriptDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("script_dir %q: %w", d, err)
|
||||
}
|
||||
info, err := os.Stat(p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("script_dir %q: not found", d)
|
||||
return nil, fmt.Errorf("script_dir %q: not found", scriptDir)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return nil, fmt.Errorf("script_dir %q: not a directory", d)
|
||||
@@ -332,25 +356,33 @@ func preflightJob(j *jobs.Job, resolvedByName map[string]*resolvedJob) (*resolve
|
||||
rj.outputConnEnv = j.Output.ConnEnv
|
||||
rj.secrets = append(rj.secrets, v)
|
||||
} else {
|
||||
p, err := jobs.SafeJoin(root, j.Output.Path)
|
||||
outputPath, err := expandPath("output", j.Output.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := jobs.SafeJoin(root, outputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("output: %w", err)
|
||||
}
|
||||
if _, err := os.Stat(p); err == nil && !j.Output.Overwrite {
|
||||
return nil, fmt.Errorf("output %s already exists (set output.overwrite: true to replace it)", j.Output.Path)
|
||||
return nil, fmt.Errorf("output %s already exists (set output.overwrite: true to replace it)", outputPath)
|
||||
}
|
||||
rj.outputPath = p
|
||||
}
|
||||
}
|
||||
|
||||
if j.Rules != "" {
|
||||
p, err := jobs.SafeJoin(root, j.Rules)
|
||||
rulesPath, err := expandPath("rules", j.Rules)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := jobs.SafeJoin(root, rulesPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("rules: %w", err)
|
||||
}
|
||||
info, err := os.Stat(p)
|
||||
if err != nil || info.IsDir() {
|
||||
return nil, fmt.Errorf("rules %q: not found or is a directory", j.Rules)
|
||||
return nil, fmt.Errorf("rules %q: not found or is a directory", rulesPath)
|
||||
}
|
||||
rj.rulesPath = p
|
||||
}
|
||||
@@ -358,12 +390,16 @@ func preflightJob(j *jobs.Job, resolvedByName map[string]*resolvedJob) (*resolve
|
||||
if j.Report != nil {
|
||||
rj.reportFormat = strings.ToLower(j.Report.Format)
|
||||
if j.Report.Path != "" {
|
||||
p, err := jobs.SafeJoin(root, j.Report.Path)
|
||||
reportPath, err := expandPath("report", j.Report.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := jobs.SafeJoin(root, reportPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("report: %w", err)
|
||||
}
|
||||
if _, err := os.Stat(p); err == nil && !j.Report.Overwrite {
|
||||
return nil, fmt.Errorf("report %s already exists (set report.overwrite: true to replace it)", j.Report.Path)
|
||||
return nil, fmt.Errorf("report %s already exists (set report.overwrite: true to replace it)", reportPath)
|
||||
}
|
||||
rj.reportPath = p
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user