K8s: Improve identity mapping setup (#89450)

This commit is contained in:
Ryan McKinley
2024-06-20 17:53:07 +03:00
committed by GitHub
parent fc4a9904c9
commit 27e800768e
30 changed files with 205 additions and 142 deletions
@@ -8,38 +8,38 @@ import (
k8suser "k8s.io/apiserver/pkg/authentication/user"
"k8s.io/klog/v2"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/apimachinery/identity"
)
var _ authenticator.RequestFunc = signedInUserAuthenticator
func signedInUserAuthenticator(req *http.Request) (*authenticator.Response, bool, error) {
ctx := req.Context()
signedInUser, err := appcontext.User(ctx)
signedInUser, err := identity.GetRequester(ctx)
if err != nil {
klog.V(5).Info("failed to get signed in user", "err", err)
return nil, false, nil
}
userInfo := &k8suser.DefaultInfo{
Name: signedInUser.Login,
UID: signedInUser.UserUID,
Name: signedInUser.GetLogin(),
UID: signedInUser.GetUID().ID(),
Groups: []string{},
// In order to faithfully round-trip through an impersonation flow, Extra keys MUST be lowercase.
// see: https://pkg.go.dev/k8s.io/apiserver@v0.27.1/pkg/authentication/user#Info
Extra: map[string][]string{},
}
for _, v := range signedInUser.Teams {
for _, v := range signedInUser.GetTeams() {
userInfo.Groups = append(userInfo.Groups, strconv.FormatInt(v, 10))
}
//
if signedInUser.IDToken != "" {
userInfo.Extra["id-token"] = []string{signedInUser.IDToken}
if signedInUser.GetIDToken() != "" {
userInfo.Extra["id-token"] = []string{signedInUser.GetIDToken()}
}
if signedInUser.OrgRole.IsValid() {
userInfo.Extra["user-instance-role"] = []string{string(signedInUser.OrgRole)}
if signedInUser.GetOrgRole().IsValid() {
userInfo.Extra["user-instance-role"] = []string{string(signedInUser.GetOrgRole())}
}
return &authenticator.Response{