Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allowed regexp prefixes for exact matches #1962

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions server/events/yaml/valid/repo_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package valid

import (
"fmt"
"log"
"regexp"
"strings"

Expand Down Expand Up @@ -58,16 +59,19 @@ func (r RepoCfg) FindProjectByName(name string) *Project {
// FindProjectsByName returns all projects that match with name.
func (r RepoCfg) FindProjectsByName(name string) []Project {
var ps []Project
if isRegexAllowed(name, r.AllowedRegexpPrefixes) {
sanitizedName := "^" + name + "$"
for _, p := range r.Projects {
if p.Name != nil {
if match, _ := regexp.MatchString(sanitizedName, *p.Name); match {
ps = append(ps, p)
}
sanitizedName := "^" + name + "$"
for _, p := range r.Projects {
if p.Name != nil {
if match, _ := regexp.MatchString(sanitizedName, *p.Name); match {
ps = append(ps, p)
}
}
}
// If we found more than one project then we need to make sure that the regex is allowed.
if len(ps) > 1 && !isRegexAllowed(name, r.AllowedRegexpPrefixes) {
log.Printf("Found more than one project for regex %q. This regex is not on the allow list.", name)
return nil
}
return ps
}

Expand Down
42 changes: 42 additions & 0 deletions server/events/yaml/valid/repo_cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,48 @@ func TestConfig_FindProjectsByDir(t *testing.T) {
},
},
},
{
description: "Always find exact matches even if the prefix is not allowed",
nameRegex: ".*",
input: valid.RepoCfg{
Version: 3,
Projects: []valid.Project{
{
Dir: ".",
Name: String("prod_terragrunt_myproject"),
Workspace: "myworkspace",
TerraformVersion: tfVersion,
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*", "**/terragrunt.hcl"},
Enabled: false,
},
ApplyRequirements: []string{"approved"},
},
},
Workflows: map[string]valid.Workflow{
"myworkflow": {
Name: "myworkflow",
Apply: valid.DefaultApplyStage,
Plan: valid.DefaultPlanStage,
PolicyCheck: valid.DefaultPolicyCheckStage,
},
},
AllowedRegexpPrefixes: []string{"dev", "staging"},
},
expProjects: []valid.Project{
{
Dir: ".",
Name: String("prod_terragrunt_myproject"),
Workspace: "myworkspace",
TerraformVersion: tfVersion,
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*", "**/terragrunt.hcl"},
Enabled: false,
},
ApplyRequirements: []string{"approved"},
},
},
},
}
validation.ErrorTag = "yaml"
for _, c := range cases {
Expand Down