Skip to content

Commit 6c2c8ef

Browse files
feat: Extend policy validation to support update and delete actions (#522)
* fix(utils.go): allow update and delete actions in validActionPatterns regex Signed-off-by: pouriyabp <[email protected]> * Update argocd/utils.go Co-authored-by: Marco Maurer (-Kilchhofer) <[email protected]> Signed-off-by: pouriya <[email protected]> * test: Add test for fine-grained project policies Signed-off-by: Marco Maurer <[email protected]> * test: Run TestAccArgoCDProjectWithFineGrainedPolicy only on 2.12+ Signed-off-by: Marco Maurer <[email protected]> --------- Signed-off-by: pouriyabp <[email protected]> Signed-off-by: pouriya <[email protected]> Signed-off-by: Marco Maurer <[email protected]> Co-authored-by: Marco Maurer (-Kilchhofer) <[email protected]>
1 parent bd8eec9 commit 6c2c8ef

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

argocd/resource_argocd_project_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,31 @@ func TestAccArgoCDProjectWithSourceNamespaces(t *testing.T) {
254254
})
255255
}
256256

257+
func TestAccArgoCDProjectWithFineGrainedPolicy(t *testing.T) {
258+
name := acctest.RandomWithPrefix("test-acc")
259+
260+
resource.Test(t, resource.TestCase{
261+
PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureSupported(t, features.ProjectFineGrainedPolicy) },
262+
ProviderFactories: testAccProviders,
263+
Steps: []resource.TestStep{
264+
{
265+
Config: testAccArgoCDProjectWithFineGrainedPolicy(name),
266+
Check: resource.ComposeTestCheckFunc(
267+
resource.TestCheckResourceAttrSet(
268+
"argocd_project.fine_grained_policy",
269+
"metadata.0.uid",
270+
),
271+
resource.TestCheckResourceAttr(
272+
"argocd_project.fine_grained_policy",
273+
"spec.0.role.0.policies.#",
274+
"2",
275+
),
276+
),
277+
},
278+
},
279+
})
280+
}
281+
257282
func testAccArgoCDProjectSimple(name string) string {
258283
return fmt.Sprintf(`
259284
resource "argocd_project" "simple" {
@@ -834,3 +859,35 @@ resource "argocd_project" "failure" {
834859
}
835860
`, name, name, name)
836861
}
862+
863+
func testAccArgoCDProjectWithFineGrainedPolicy(name string) string {
864+
return fmt.Sprintf(`
865+
resource "argocd_project" "fine_grained_policy" {
866+
metadata {
867+
name = "%[1]s"
868+
namespace = "argocd"
869+
labels = {
870+
acceptance = "true"
871+
}
872+
}
873+
874+
spec {
875+
description = "simple project with fine-grained policies"
876+
source_repos = ["*"]
877+
878+
destination {
879+
server = "https://kubernetes.default.svc"
880+
namespace = "default"
881+
}
882+
883+
role {
884+
name = "fine-grained"
885+
policies = [
886+
"p, proj:%[1]s:fine-grained, applications, update/*, %[1]s/*, allow",
887+
"p, proj:%[1]s:fine-grained, applications, delete/*/Pod/*/*, %[1]s/*, allow",
888+
]
889+
}
890+
}
891+
}
892+
`, name)
893+
}

argocd/utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ func isValidPolicyAction(action string) bool {
9898
}
9999
validActionPatterns := []*regexp.Regexp{
100100
regexp.MustCompile("action/.*"),
101+
regexp.MustCompile("update/.*"),
102+
regexp.MustCompile("delete/.*"),
101103
}
102104

103105
if validActions[action] {

internal/features/features.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
ApplicationSetIgnoreApplicationDifferences
1818
ApplicationSetTemplatePatch
1919
ApplicationKustomizePatches
20+
ProjectFineGrainedPolicy
2021
)
2122

2223
type FeatureConstraint struct {
@@ -35,4 +36,5 @@ var ConstraintsMap = map[Feature]FeatureConstraint{
3536
ApplicationSetIgnoreApplicationDifferences: {"application set ignore application differences", semver.MustParse("2.9.0")},
3637
ApplicationSetTemplatePatch: {"application set template patch", semver.MustParse("2.10.0")},
3738
ApplicationKustomizePatches: {"application kustomize patches", semver.MustParse("2.9.0")},
39+
ProjectFineGrainedPolicy: {"fine-grained policy in project", semver.MustParse("2.12.0")},
3840
}

0 commit comments

Comments
 (0)