support impersonate in local client manager
@@ -3,9 +3,11 @@ package client
import (
"fmt"
+ "github.com/kalmhq/kalm/api/log"
"github.com/kalmhq/kalm/api/rbac"
"github.com/kalmhq/kalm/controller/api/v1alpha1"
"github.com/labstack/echo/v4"
+ "go.uber.org/zap"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/rest"
)
@@ -25,18 +27,32 @@ func (m *LocalClientManager) GetClientInfoFromToken(_ string) (*ClientInfo, erro
return nil, errors.NewUnauthorized("auth via token is not allowed in local client manager")
}
-func (m *LocalClientManager) SetImpersonation(_ *ClientInfo, _ string) {
+func (m *LocalClientManager) SetImpersonation(clientInfo *ClientInfo, rawImpersonation string) {
+ if rawImpersonation != "" && m.CanManageCluster(clientInfo) {
+ impersonation, impersonationType, err := parseImpersonationString(rawImpersonation)
+
+ if err == nil {
+ clientInfo.Impersonation = impersonation
+ clientInfo.ImpersonationType = impersonationType
+ } else {
+ log.Error("parse impersonation raw string failed", zap.Error(err))
+ }
+ }
}
-func (m *LocalClientManager) GetClientInfoFromContext(_ echo.Context) (*ClientInfo, error) {
- return &ClientInfo{
+func (m *LocalClientManager) GetClientInfoFromContext(c echo.Context) (*ClientInfo, error) {
+ clientInfo := &ClientInfo{
Cfg: m.ClusterConfig,
Name: localhostAdminUser,
Email: localhostAdminUser,
EmailVerified: false,
Tenant: "global",
Groups: []string{},
- }, nil
+ }
+
+ m.SetImpersonation(clientInfo, c.Request().Header.Get("Kalm-Impersonation"))
+
+ return clientInfo, nil
}
func NewLocalClientManager(cfg *rest.Config) *LocalClientManager {
