Skip to content

Commit

Permalink
Extract serviceaccount name and namespace from account.spec.username (#…
Browse files Browse the repository at this point in the history
…20)

Signed-off-by: Rokibul Hasan <[email protected]>
  • Loading branch information
RokibulHasan7 authored Aug 12, 2024
1 parent 6f45fbd commit 4884b62
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pkg/manager/agent-manifests/cluster-auth/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apiVersion: v1
description: Cluster Auth Agent
name: cluster-auth-agent
version: v2024.7.10
appVersion: v0.0.2
version: v2024.8.9
appVersion: v0.0.3
home: https://github.com/kluster-manager/cluster-auth
icon: https://cdn.appscode.com/images/products/searchlight/icons/android-icon-192x192.png
sources:
Expand Down
21 changes: 16 additions & 5 deletions pkg/manager/controller/authentication/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

authenticationv1alpha1 "github.com/kluster-manager/cluster-auth/apis/authentication/v1alpha1"
"github.com/kluster-manager/cluster-auth/pkg/common"
"github.com/kluster-manager/cluster-auth/pkg/utils"

core "k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -138,12 +139,17 @@ func (r *AccountReconciler) createGatewayClusterRoleBindingForUser(ctx context.C
}

if strings.Contains(acc.Spec.Username, common.ServiceAccountPrefix) {
name, namespace, err := utils.ExtractServiceAccountNameAndNamespace(acc.Spec.Username)
if err != nil {
return err
}

sub = []rbac.Subject{
{
APIGroup: "",
Kind: "ServiceAccount",
Name: acc.Name,
Namespace: common.AddonAgentInstallNamespace,
Name: name,
Namespace: namespace,
},
}
}
Expand All @@ -164,7 +170,7 @@ func (r *AccountReconciler) createGatewayClusterRoleBindingForUser(ctx context.C
}

if strings.Contains(acc.Spec.Username, common.ServiceAccountPrefix) {
crb.Name = fmt.Sprintf("ace.%s.proxy", acc.Spec.Username)
crb.Name = fmt.Sprintf("ace.%s.proxy", acc.Name)
}

_, err := cu.CreateOrPatch(ctx, r.Client, &crb, func(obj client.Object, createOp bool) client.Object {
Expand Down Expand Up @@ -199,9 +205,14 @@ func (r *AccountReconciler) createClusterRoleAndClusterRoleBindingToImpersonate(
}

if strings.Contains(acc.Spec.Username, common.ServiceAccountPrefix) {
name, _, err := utils.ExtractServiceAccountNameAndNamespace(acc.Spec.Username)
if err != nil {
return err
}

cr = rbac.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("ace.%s.impersonate", acc.Spec.Username),
Name: fmt.Sprintf("ace.%s.impersonate", acc.Name),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(acc, authenticationv1alpha1.GroupVersion.WithKind("Account")),
},
Expand All @@ -211,7 +222,7 @@ func (r *AccountReconciler) createClusterRoleAndClusterRoleBindingToImpersonate(
APIGroups: []string{""},
Resources: []string{"serviceaccounts"},
Verbs: []string{"impersonate"},
ResourceNames: []string{acc.Name},
ResourceNames: []string{name},
},
},
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package utils

import (
"errors"
"strings"

authorizationv1alpha1 "github.com/kluster-manager/cluster-auth/apis/authorization/v1alpha1"
Expand All @@ -40,3 +41,11 @@ func ReplaceColonWithHyphen(input string) string {
parts := strings.Split(input, ":")
return strings.Join(parts, "-")
}

func ExtractServiceAccountNameAndNamespace(s string) (name, namespace string, err error) {
parts := strings.Split(s, ":")
if len(parts) == 4 {
return parts[3], parts[2], nil
}
return "", "", errors.New("account username is invalid")
}

0 comments on commit 4884b62

Please sign in to comment.