-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bumping knative.dev/pkg 15e6cdf...339c22b: > 339c22b Add AuthenticatableType duck type (# 3056) bumping knative.dev/networking 85e269d...3b8764c: > 3b8764c upgrade to latest dependencies (# 989) bumping knative.dev/eventing ea8f0fd...e298f32: > e298f32 Add authz library (# 8002) > 1a21fee Add all JobSink symlinks in config/ (# 8007) > 2157639 Add validation for EventPolicy sub suffix matching (# 8008) > 0eee301 Propagate read error correctly in event-dispatcher (# 8005) > 43cf75a [main] Upgrade to latest dependencies (# 8004) bumping knative.dev/serving 1f7cc48...f464e2d: > f464e2d upgrade to latest dependencies (# 15329) > 0b61640 Update net-kourier nightly (# 15314) > 8d768f5 Cert rotation test does not use specific ingress namespace (# 15331) Signed-off-by: Knative Automation <automation@knative.team>
- Loading branch information
1 parent
5f25bff
commit a2e2210
Showing
11 changed files
with
658 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
Copyright 2024 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"knative.dev/eventing/pkg/apis/eventing/v1alpha1" | ||
listerseventingv1alpha1 "knative.dev/eventing/pkg/client/listers/eventing/v1alpha1" | ||
"knative.dev/pkg/resolver" | ||
) | ||
|
||
// GetEventPoliciesForResource returns the applying EventPolicies for a given resource | ||
func GetEventPoliciesForResource(lister listerseventingv1alpha1.EventPolicyLister, resourceGVK schema.GroupVersionKind, resourceObjectMeta metav1.ObjectMeta) ([]*v1alpha1.EventPolicy, error) { | ||
policies, err := lister.EventPolicies(resourceObjectMeta.GetNamespace()).List(labels.Everything()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list eventpolicies: %w", err) | ||
} | ||
|
||
relevantPolicies := []*v1alpha1.EventPolicy{} | ||
|
||
for _, policy := range policies { | ||
if len(policy.Spec.To) == 0 { | ||
// policy applies to all resources in namespace | ||
relevantPolicies = append(relevantPolicies, policy) | ||
} | ||
|
||
for _, to := range policy.Spec.To { | ||
if to.Ref != nil { | ||
refGV, err := schema.ParseGroupVersion(to.Ref.APIVersion) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot split apiVersion into group and version: %s", to.Ref.APIVersion) | ||
} | ||
|
||
if strings.EqualFold(to.Ref.Name, resourceObjectMeta.GetName()) && | ||
strings.EqualFold(refGV.Group, resourceGVK.Group) && | ||
strings.EqualFold(to.Ref.Kind, resourceGVK.Kind) { | ||
|
||
relevantPolicies = append(relevantPolicies, policy) | ||
break // no need to check the other .spec.to's from this policy | ||
} | ||
} | ||
|
||
if to.Selector != nil { | ||
selectorGV, err := schema.ParseGroupVersion(to.Selector.APIVersion) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot split apiVersion into group and version: %s", to.Selector.APIVersion) | ||
} | ||
|
||
if strings.EqualFold(selectorGV.Group, resourceGVK.Group) && | ||
strings.EqualFold(to.Selector.Kind, resourceGVK.Kind) { | ||
|
||
selector, err := metav1.LabelSelectorAsSelector(to.Selector.LabelSelector) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse selector: %w", err) | ||
} | ||
|
||
if selector.Matches(labels.Set(resourceObjectMeta.Labels)) { | ||
relevantPolicies = append(relevantPolicies, policy) | ||
break // no need to check the other .spec.to's from this policy | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return relevantPolicies, nil | ||
} | ||
|
||
// ResolveSubjects returns the OIDC service accounts names for the objects referenced in the EventPolicySpecFrom. | ||
func ResolveSubjects(resolver *resolver.AuthenticatableResolver, eventPolicy *v1alpha1.EventPolicy) ([]string, error) { | ||
allSAs := []string{} | ||
for _, from := range eventPolicy.Spec.From { | ||
if from.Ref != nil { | ||
sas, err := resolveSubjectsFromReference(resolver, *from.Ref, eventPolicy) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not resolve subjects from reference: %w", err) | ||
} | ||
allSAs = append(allSAs, sas...) | ||
} else if from.Sub != nil { | ||
allSAs = append(allSAs, *from.Sub) | ||
} | ||
} | ||
|
||
return allSAs, nil | ||
} | ||
|
||
func resolveSubjectsFromReference(resolver *resolver.AuthenticatableResolver, reference v1alpha1.EventPolicyFromReference, trackingEventPolicy *v1alpha1.EventPolicy) ([]string, error) { | ||
authStatus, err := resolver.AuthStatusFromObjectReference(&corev1.ObjectReference{ | ||
APIVersion: reference.APIVersion, | ||
Kind: reference.Kind, | ||
Namespace: reference.Namespace, | ||
Name: reference.Name, | ||
}, trackingEventPolicy) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("could not resolve auth status: %w", err) | ||
} | ||
|
||
objSAs := authStatus.ServiceAccountNames | ||
if authStatus.ServiceAccountName != nil { | ||
objSAs = append(objSAs, *authStatus.ServiceAccountName) | ||
} | ||
|
||
objFullSANames := make([]string, 0, len(objSAs)) | ||
for _, sa := range objSAs { | ||
objFullSANames = append(objFullSANames, fmt.Sprintf("system:serviceaccount:%s:%s", reference.Namespace, sa)) | ||
} | ||
|
||
return objFullSANames, nil | ||
} | ||
|
||
// SubjectContained checks if the given sub is contained in the list of allowedSubs | ||
// or if it matches a prefix pattern in subs (e.g. system:serviceaccounts:my-ns:*) | ||
func SubjectContained(sub string, allowedSubs []string) bool { | ||
for _, s := range allowedSubs { | ||
if strings.EqualFold(s, sub) { | ||
return true | ||
} | ||
|
||
if strings.HasSuffix(s, "*") && | ||
strings.HasPrefix(sub, strings.TrimSuffix(s, "*")) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
99 changes: 99 additions & 0 deletions
99
vendor/knative.dev/eventing/pkg/client/listers/eventing/v1alpha1/eventpolicy.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
vendor/knative.dev/eventing/pkg/client/listers/eventing/v1alpha1/expansion_generated.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.