Skip to content

Commit

Permalink
fix(pipeline): set max template depth and adjust CompileLite call (#467)
Browse files Browse the repository at this point in the history
* fix(pipeline): set max template depth and adjust CompileLite call

* update validate test

* protect against SCM spamming with template depth config

* fix comment
  • Loading branch information
ecrupper authored Jul 27, 2023
1 parent 00b511e commit f6f95ed
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion action/pipeline/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (c *Config) ValidateLocal(client compiler.Engine) error {
logrus.Tracef("compiling pipeline %s", path)

// compile the object into a pipeline
p, _, err := client.CompileLite(path, c.Template, false, c.TemplateFiles)
p, _, err := client.CompileLite(path, c.Template, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion action/pipeline/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func TestPipeline_Config_ValidateLocal(t *testing.T) {
for _, test := range tests {
isLocal := len(test.config.TemplateFiles) > 0

err := test.config.ValidateLocal(client.WithLocal(isLocal))
err := test.config.ValidateLocal(client.WithLocal(isLocal).WithLocalTemplates(test.config.TemplateFiles))

if test.failure {
if err == nil {
Expand Down
10 changes: 10 additions & 0 deletions command/pipeline/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/go-vela/cli/action/pipeline"
"github.com/go-vela/cli/internal"
"github.com/go-vela/server/compiler/native"
"github.com/go-vela/server/util"
"github.com/go-vela/types/constants"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -94,6 +95,12 @@ var CommandExec = &cli.Command{
Aliases: []string{"v"},
Usage: "provide list of local volumes to mount",
},
&cli.IntFlag{
EnvVars: []string{"VELA_MAX_TEMPLATE_DEPTH", "MAX_TEMPLATE_DEPTH"},
Name: "max-template-depth",
Usage: "set the maximum depth for nested templates",
Value: 3,
},

// Repo Flags

Expand Down Expand Up @@ -184,6 +191,9 @@ func exec(c *cli.Context) error {
return err
}

// set the max template depth using provided configuration (max of 5)
client.TemplateDepth = util.MinInt(c.Int("max-template-depth"), 5)

// execute the exec call for the pipeline configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/pipeline?tab=doc#Config.Exec
Expand Down
17 changes: 17 additions & 0 deletions command/pipeline/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/go-vela/cli/internal"
"github.com/go-vela/cli/internal/client"
"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"

"github.com/go-vela/server/compiler/native"
"github.com/go-vela/server/util"

"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -80,6 +82,12 @@ var CommandValidate = &cli.Command{
Name: "template-file",
Usage: "enables using a local template file for expansion",
},
&cli.IntFlag{
EnvVars: []string{"VELA_MAX_TEMPLATE_DEPTH", "MAX_TEMPLATE_DEPTH"},
Name: "max-template-depth",
Usage: "set the maximum depth for nested templates",
Value: 3,
},
&cli.BoolFlag{
EnvVars: []string{"VELA_REMOTE", "PIPELINE_REMOTE"},
Name: "remote",
Expand Down Expand Up @@ -120,6 +128,8 @@ EXAMPLES:
$ {{.HelpName}} --template
8. Validate a local template pipeline with expanding steps
$ {{.HelpName}} --template --template-file name:/path/to/file
9. Validate a local, nested template pipeline with custom template depth.
$ {{.HelpName}} --template --template-file name:/path/to/file name:/path/to/file --max-template-depth 2
DOCUMENTATION:
https://go-vela.github.io/docs/reference/cli/pipeline/validate/
Expand Down Expand Up @@ -186,6 +196,13 @@ func validate(c *cli.Context) error {
// set when user is sourcing templates from local machine
if len(p.TemplateFiles) != 0 {
client.WithLocal(true)
client.WithLocalTemplates(p.TemplateFiles)
client.TemplateDepth = c.Int("max-template-depth")
} else {
// set max template depth to minimum of 5 and provided value if local templates are not provided.
// This prevents users from spamming SCM
client.TemplateDepth = util.MinInt(c.Int("max-template-depth"), 5)
logrus.Debugf("no local template files provided, setting max template depth to %d", client.TemplateDepth)
}

// execute the validate local call for the pipeline configuration
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/go-git/go-git/v5 v5.7.0
github.com/go-vela/sdk-go v0.20.0
github.com/go-vela/server v0.20.0
github.com/go-vela/server v0.20.1-0.20230725195029-acbb8f116c4e
github.com/go-vela/types v0.20.0
github.com/go-vela/worker v0.20.0
github.com/golang-jwt/jwt/v5 v5.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QX
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-vela/sdk-go v0.20.0 h1:+kM2sF/DKibzuHK/4XO0QeRkLgjorpuDkr//P95FhqQ=
github.com/go-vela/sdk-go v0.20.0/go.mod h1:0cgibC9qs9/JheSsU1qHuAecY1njJDwPSE4XeD4gP7c=
github.com/go-vela/server v0.20.0 h1:niU7Hg9/uJRaRuA2VR+Obq2sAbG8iw32esPgTBawZ/g=
github.com/go-vela/server v0.20.0/go.mod h1:l6ToQGKLn9RP9Kcqwyq3JtgkFRhq1nlIKHq/mynkA+E=
github.com/go-vela/server v0.20.1-0.20230725195029-acbb8f116c4e h1:KldHo5HQICJv5G9nTcNeuuiW9DDUzAIjptRH2X+9cFU=
github.com/go-vela/server v0.20.1-0.20230725195029-acbb8f116c4e/go.mod h1:l6ToQGKLn9RP9Kcqwyq3JtgkFRhq1nlIKHq/mynkA+E=
github.com/go-vela/types v0.20.0 h1:u/wHwc6ElVbIEI+q9TaVl9Iai1EoEr4Lwis6mikOte8=
github.com/go-vela/types v0.20.0/go.mod h1:1ZSmKWX9MamKogwaIb53mzzRpZMV34mJFKiGfVFadFk=
github.com/go-vela/worker v0.20.0 h1:hZuHTf6CGyKEJh5z9vEY2qFd9yRUxvLzMT2x0xUXG8o=
Expand Down

0 comments on commit f6f95ed

Please sign in to comment.