Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use kmeta.ChildName() to generate OIDC service account name #7521

Merged
merged 1 commit into from
Dec 20, 2023
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
6 changes: 4 additions & 2 deletions pkg/auth/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"knative.dev/eventing/pkg/apis/feature"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/kmeta"
pkgreconciler "knative.dev/pkg/reconciler"

"go.uber.org/zap"
Expand All @@ -39,8 +40,9 @@ import (
// GetOIDCServiceAccountNameForResource returns the service account name to use
// for OIDC authentication for the given resource.
func GetOIDCServiceAccountNameForResource(gvk schema.GroupVersionKind, objectMeta metav1.ObjectMeta) string {
sa := fmt.Sprintf("oidc-%s-%s-%s", gvk.GroupKind().Group, gvk.GroupKind().Kind, objectMeta.GetName())

suffix := fmt.Sprintf("-oidc-%s-%s", gvk.Group, gvk.Kind)
parent := objectMeta.GetName()
sa := kmeta.ChildName(parent, suffix)
return strings.ToLower(sa)
}

Expand Down
13 changes: 11 additions & 2 deletions pkg/auth/serviceaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestGetOIDCServiceAccountNameForResource(t *testing.T) {
Name: "name",
Namespace: "namespace",
},
want: "oidc-group-kind-name",
want: "name-oidc-group-kind",
},
{
name: "should return SA name in lower case",
Expand All @@ -62,7 +62,16 @@ func TestGetOIDCServiceAccountNameForResource(t *testing.T) {
Name: "my-Broker",
Namespace: "my-Namespace",
},
want: "oidc-eventing.knative.dev-broker-my-broker",
want: "my-broker-oidc-eventing.knative.dev-broker",
},
{
name: "long Broker name",
gvk: eventingv1.SchemeGroupVersion.WithKind("Broker"),
objectMeta: metav1.ObjectMeta{
Name: "my-loooooooooooooooooooooooooooooooooooooog-Broker",
Namespace: "my-Namespace",
},
want: "my-looooooooooooooooooooooooooo2dfc2a3825b8d82077b0f25518b36884",
},
}
for _, tt := range tests {
Expand Down