From 7fba9ba5223d57c73e3b42642d18bf1c6a300dc5 Mon Sep 17 00:00:00 2001 From: Karl Persson Date: Wed, 16 Oct 2024 10:27:06 +0200 Subject: [PATCH] SA: Fix name validation so we can prevent creating service account with protected prefix (#94762) Fix name validation so we can prevent creating service account with protected prefix --- pkg/services/serviceaccounts/proxy/service.go | 2 +- pkg/services/serviceaccounts/proxy/service_test.go | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/services/serviceaccounts/proxy/service.go b/pkg/services/serviceaccounts/proxy/service.go index 8da7af34a41..de4153597b3 100644 --- a/pkg/services/serviceaccounts/proxy/service.go +++ b/pkg/services/serviceaccounts/proxy/service.go @@ -183,5 +183,5 @@ func (s *ServiceAccountsProxy) SearchOrgServiceAccounts(ctx context.Context, que } func isNameValid(name string) bool { - return !strings.HasPrefix(name, serviceaccounts.ExtSvcPrefix) + return !strings.HasPrefix(name, strings.TrimSuffix(serviceaccounts.ExtSvcPrefix, "-")) } diff --git a/pkg/services/serviceaccounts/proxy/service_test.go b/pkg/services/serviceaccounts/proxy/service_test.go index 5e3569c586a..53e79746b4f 100644 --- a/pkg/services/serviceaccounts/proxy/service_test.go +++ b/pkg/services/serviceaccounts/proxy/service_test.go @@ -43,12 +43,19 @@ func TestProvideServiceAccount_crudServiceAccount(t *testing.T) { expectedError: nil, }, { - description: "should not allow to create a service account with extsvc prefix", + description: "should not allow to create a service account with extsvc- prefix", form: sa.CreateServiceAccountForm{ Name: "extsvc-my-service-account", }, expectedError: extsvcaccounts.ErrInvalidName, }, + { + description: "should not allow to create a service account with extsvc prefix", + form: sa.CreateServiceAccountForm{ + Name: "extsvc my-service-account", + }, + expectedError: extsvcaccounts.ErrInvalidName, + }, } for _, tc := range testCases {