Skip to content

Commit 7c9925b

Browse files
committed
refactor: Update ReferenceGrant validation to use ObjectReference for cross-namespace checks
1 parent de81779 commit 7c9925b

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

internal/controller/httproute_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,11 @@ func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.Transla
470470
Kind: KindHTTPRoute,
471471
Namespace: v1beta1.Namespace(hrNN.Namespace),
472472
},
473-
v1beta1.ReferenceGrantTo{
474-
Group: corev1.GroupName,
475-
Kind: KindService,
476-
Name: (*gatewayv1.ObjectName)(&targetNN.Name),
473+
gatewayv1.ObjectReference{
474+
Group: corev1.GroupName,
475+
Kind: KindService,
476+
Name: gatewayv1.ObjectName(targetNN.Name),
477+
Namespace: (*gatewayv1.Namespace)(&targetNN.Namespace),
477478
},
478479
referenceGrantList.Items,
479480
); !permitted {

internal/controller/utils.go

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -739,28 +739,26 @@ func getListenerStatus(
739739
conditionProgrammed.Reason = string(gatewayv1.ListenerReasonInvalid)
740740
break
741741
}
742-
// if cross namespaces, check if the Gateway has the permission to access the Secret
743-
if ref.Namespace != nil && string(*ref.Namespace) != gateway.Namespace {
744-
if permitted := checkReferenceGrant(
745-
v1beta1.ReferenceGrantFrom{
746-
Group: gatewayv1.GroupName,
747-
Kind: KindGateway,
748-
Namespace: v1beta1.Namespace(gateway.Namespace),
749-
},
750-
v1beta1.ReferenceGrantTo{
751-
Group: corev1.GroupName,
752-
Kind: KindSecret,
753-
Name: &ref.Name,
754-
},
755-
grants,
756-
); !permitted {
757-
conditionResolvedRefs.Status = metav1.ConditionFalse
758-
conditionResolvedRefs.Reason = string(gatewayv1.ListenerReasonRefNotPermitted)
759-
conditionResolvedRefs.Message = "certificateRefs cross namespaces is not permitted"
760-
conditionProgrammed.Status = metav1.ConditionFalse
761-
conditionProgrammed.Reason = string(gatewayv1.ListenerReasonInvalid)
762-
break
763-
}
742+
if permitted := checkReferenceGrant(
743+
v1beta1.ReferenceGrantFrom{
744+
Group: gatewayv1.GroupName,
745+
Kind: KindGateway,
746+
Namespace: v1beta1.Namespace(gateway.Namespace),
747+
},
748+
gatewayv1.ObjectReference{
749+
Group: corev1.GroupName,
750+
Kind: KindSecret,
751+
Name: ref.Name,
752+
Namespace: ref.Namespace,
753+
},
754+
grants,
755+
); !permitted {
756+
conditionResolvedRefs.Status = metav1.ConditionFalse
757+
conditionResolvedRefs.Reason = string(gatewayv1.ListenerReasonRefNotPermitted)
758+
conditionResolvedRefs.Message = "certificateRefs cross namespaces is not permitted"
759+
conditionProgrammed.Status = metav1.ConditionFalse
760+
conditionProgrammed.Reason = string(gatewayv1.ListenerReasonInvalid)
761+
break
764762
}
765763

766764
secretNN := types.NamespacedName{
@@ -1109,16 +1107,21 @@ func referenceGrantPredicates(kind gatewayv1.Kind) predicate.Funcs {
11091107
return predicates
11101108
}
11111109

1112-
func checkReferenceGrant(from v1beta1.ReferenceGrantFrom, to v1beta1.ReferenceGrantTo, grants []v1beta1.ReferenceGrant) bool {
1110+
func checkReferenceGrant(obj v1beta1.ReferenceGrantFrom, ref gatewayv1.ObjectReference, grants []v1beta1.ReferenceGrant) bool {
1111+
if ref.Namespace == nil || *ref.Namespace == obj.Namespace {
1112+
return true
1113+
}
11131114
for _, grant := range grants {
1114-
grantFrom := slices.ContainsFunc(grant.Spec.From, func(item v1beta1.ReferenceGrantFrom) bool {
1115-
return item == from
1116-
})
1117-
grantTo := slices.ContainsFunc(grant.Spec.To, func(item v1beta1.ReferenceGrantTo) bool {
1118-
return item.Group == to.Group && item.Kind == to.Kind && to.Name != nil && (item.Name == nil || *item.Name == *to.Name)
1119-
})
1120-
if grantFrom && grantTo {
1121-
return true
1115+
if grant.Namespace == string(*ref.Namespace) {
1116+
for _, from := range grant.Spec.From {
1117+
if obj == from {
1118+
for _, to := range grant.Spec.To {
1119+
if to.Group == ref.Group && to.Kind == ref.Kind && (to.Name == nil || *to.Name == ref.Name) {
1120+
return true
1121+
}
1122+
}
1123+
}
1124+
}
11221125
}
11231126
}
11241127
return false

0 commit comments

Comments
 (0)