k8c.io/api/v3@v3.0.0-20230904060738-b0a93889c0b6/pkg/apis/ee.kubermatic/v1/helper/user.go (about)

     1  /*
     2  Copyright 2023 The Kubermatic Kubernetes Platform contributors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package helper
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  )
    23  
    24  const (
    25  	UserServiceAccountPrefix = "serviceaccount-"
    26  )
    27  
    28  // IsProjectServiceAccount determines whether the given email address
    29  // or user object name belongs to a project service account. For a service
    30  // account, they must have the UserServiceAccountPrefix.
    31  func IsProjectServiceAccount(nameOrEmail string) bool {
    32  	return strings.HasPrefix(nameOrEmail, UserServiceAccountPrefix)
    33  }
    34  
    35  // RemoveProjectServiceAccountPrefix removes "serviceaccount-" from a SA's ID,
    36  // for example given "serviceaccount-7d4b5695vb" it returns "7d4b5695vb".
    37  func RemoveProjectServiceAccountPrefix(str string) string {
    38  	return strings.TrimPrefix(str, UserServiceAccountPrefix)
    39  }
    40  
    41  // EnsureProjectServiceAccountPrefix adds "serviceaccount-" prefix to a SA's ID,
    42  // for example given "7d4b5695vb" it returns "serviceaccount-7d4b5695vb".
    43  func EnsureProjectServiceAccountPrefix(str string) string {
    44  	if !IsProjectServiceAccount(str) {
    45  		return fmt.Sprintf("%s%s", UserServiceAccountPrefix, str)
    46  	}
    47  
    48  	return str
    49  }