1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-29 02:45:06 +00:00
kyverno/pkg/cel/libs/user/impl.go
Charles-Edouard Brétéché af550f54d5
feat: add cel user lib (#12414)
* feat: add cel user lib

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* unit test

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2025-03-14 13:51:25 +01:00

33 lines
673 B
Go

package user
import (
"strings"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/kyverno/kyverno/pkg/cel/utils"
)
const (
saPrefix = "system:serviceaccount:"
)
type impl struct {
types.Adapter
}
func (c *impl) parse_service_account_string(user ref.Val) ref.Val {
if user, err := utils.ConvertToNative[string](user); err != nil {
return types.WrapErr(err)
} else {
var sa ServiceAccount
if strings.HasPrefix(user, saPrefix) {
user = user[len(saPrefix):]
if sep := strings.Index(user, ":"); sep != -1 {
sa.Namesapce = user[:sep]
sa.Name = user[sep+1:]
}
}
return c.NativeToValue(sa)
}
}