Files
grafana/pkg/services/serviceaccounts/utils.go
Misi badea8bc37 IAM: Create Service Account API and legacy store impl (#110411)
* wip

* IAM: Create Service Account

* Add dual writer

* Update openapi_test.go

* Add integration tests

* Add sql tests

* Add Role to SA spec, add validation, add DBTime, add tests

* Format, update test

* Fixes

* Add check for External

* Address feedback

* Update tests

* Address feedback

* make gen-go

* Simplify a bit

* Fixes

* make update-workspace

* Update pkg/registry/apis/iam/serviceaccount/store.go

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>

* Address feedback, add test for generateName

---------

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2025-09-08 14:31:32 +02:00

33 lines
1.1 KiB
Go

package serviceaccounts
import (
"fmt"
"strings"
)
// generateLogin makes a generated string to have a ID for the service account across orgs and it's name
// this causes you to create a service account with the same name in different orgs
// not the same name in the same org
// -- WARNING:
// -- if you change this function you need to change the ExtSvcLoginPrefix as well
// -- to make sure they are not considered as regular service accounts
func GenerateLogin(prefix string, orgId int64, name string) string {
generatedLogin := fmt.Sprintf("%v-%v-%v", prefix, orgId, strings.ToLower(name))
// in case the name has multiple spaces or dashes in the prefix or otherwise, replace them with a single dash
generatedLogin = strings.Replace(generatedLogin, "--", "-", 1)
return strings.ReplaceAll(generatedLogin, " ", "-")
}
func ExtSvcLoginPrefix(orgID int64) string {
return fmt.Sprintf("%s%d-%s", ServiceAccountPrefix, orgID, ExtSvcPrefix)
}
func IsExternalServiceAccount(login string) bool {
parts := strings.SplitAfter(login, "-")
if len(parts) < 4 {
return false
}
return parts[0] == ServiceAccountPrefix && parts[2] == ExtSvcPrefix
}