mirror of
https://git.vectorsigma.ru/public/atlantis.git
synced 2026-08-05 05:58:44 +00:00
fix: allowed regexp prefixes for exact matches (#1962)
* test: adds failing test case * fix: return project if there's an exact match, even if the prefix is not on the allowed list
This commit is contained in:
@@ -4,6 +4,7 @@ package valid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user