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: refine deny destination checks #20045

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
9 changes: 5 additions & 4 deletions pkg/apis/application/v1alpha1/app_project_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ func (proj AppProject) IsDestinationPermitted(dst ApplicationDestination, projec

func (proj AppProject) isDestinationMatched(dst ApplicationDestination) bool {
anyDestinationMatched := false
noDenyDestinationsMatched := true

for _, item := range proj.Spec.Destinations {
dstNameMatched := dst.Name != "" && globMatch(item.Name, dst.Name, true)
Expand All @@ -471,12 +470,14 @@ func (proj AppProject) isDestinationMatched(dst ApplicationDestination) bool {
matched := (dstServerMatched || dstNameMatched) && dstNamespaceMatched
if matched {
anyDestinationMatched = true
} else if ((!dstNameMatched && isDenyPattern(item.Name)) || (!dstServerMatched && isDenyPattern(item.Server))) || (!dstNamespaceMatched && isDenyPattern(item.Namespace)) {
noDenyDestinationsMatched = false
} else if (!dstNameMatched && isDenyPattern(item.Name)) || (!dstServerMatched && isDenyPattern(item.Server)) && dstNamespaceMatched {
return false
} else if !dstNamespaceMatched && isDenyPattern(item.Namespace) && dstServerMatched {
return false
}
}

return anyDestinationMatched && noDenyDestinationsMatched
return anyDestinationMatched
}

func isDenyPattern(pattern string) bool {
Expand Down
20 changes: 18 additions & 2 deletions pkg/apis/application/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func TestAppProject_IsNegatedDestinationPermitted(t *testing.T) {
Server: "*", Namespace: "!kube-system",
}},
appDest: ApplicationDestination{Server: "https://kubernetes.default.svc", Namespace: "default"},
isPermitted: false,
isPermitted: true,
}, {
projDest: []ApplicationDestination{{
Server: "*", Namespace: "*",
Expand All @@ -339,6 +339,22 @@ func TestAppProject_IsNegatedDestinationPermitted(t *testing.T) {
}},
appDest: ApplicationDestination{Name: "test", Namespace: "test"},
isPermitted: false,
}, {
projDest: []ApplicationDestination{{
Server: "*", Namespace: "test",
}, {
Server: "!https://test-server", Namespace: "other",
}},
appDest: ApplicationDestination{Server: "https://test-server", Namespace: "test"},
isPermitted: true,
}, {
projDest: []ApplicationDestination{{
Server: "*", Namespace: "*",
}, {
Server: "https://test-server", Namespace: "!other",
}},
appDest: ApplicationDestination{Server: "https://other-test-server", Namespace: "other"},
isPermitted: true,
}}

for _, data := range testData {
Expand All @@ -350,7 +366,7 @@ func TestAppProject_IsNegatedDestinationPermitted(t *testing.T) {
permitted, _ := proj.IsDestinationPermitted(data.appDest, func(project string) ([]*Cluster, error) {
return []*Cluster{}, nil
})
assert.Equal(t, data.isPermitted, permitted)
assert.Equalf(t, data.isPermitted, permitted, "appDest mismatch for %+v with project destinations %+v", data.appDest, data.projDest)
}
}

Expand Down