/
whoami.go
51 lines (42 loc) · 1.23 KB
/
whoami.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cmd
import (
"fmt"
"time"
"github.com/metal-stack/metal-lib/jwt/sec"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newWhoamiCmd(c *config) *cobra.Command {
whoamiCmd := &cobra.Command{
Use: "whoami",
Short: "shows current user",
Long: "shows the current user, that will be used to authenticate commands.",
RunE: func(cmd *cobra.Command, args []string) error {
authContext, err := getAuthContext(viper.GetString("kubeconfig"))
if err != nil {
return err
}
if !authContext.AuthProviderOidc {
return fmt.Errorf("active user %s has no oidc authProvider, check config", authContext.User)
}
user, parsedClaims, err := sec.ParseTokenUnvalidatedUnfiltered(authContext.IDToken)
if err != nil {
return err
}
fmt.Fprintf(c.out, "UserId: %s\n", user.Name)
if user.Tenant != "" {
fmt.Fprintf(c.out, "Tenant: %s\n", user.Tenant)
}
if user.Issuer != "" {
fmt.Fprintf(c.out, "Issuer: %s\n", user.Issuer)
}
fmt.Fprintf(c.out, "Groups:\n")
for _, g := range user.Groups {
fmt.Fprintf(c.out, " %s\n", g)
}
fmt.Fprintf(c.out, "Expires at %s\n", time.Unix(parsedClaims.ExpiresAt, 0).Format("Mon Jan 2 15:04:05 MST 2006"))
return nil
},
}
return whoamiCmd
}