-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.go
93 lines (78 loc) · 2.38 KB
/
accounts.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package models
import (
"fmt"
"net/http"
jwt "github.com/dgrijalva/jwt-go"
radixmodels "github.com/equinor/radix-common/models"
radixclient "github.com/equinor/radix-operator/pkg/client/clientset/versioned"
"k8s.io/client-go/kubernetes"
)
// NewAccounts creates a new Accounts struct
func NewAccounts(
inClusterClient kubernetes.Interface,
inClusterRadixClient radixclient.Interface,
outClusterClient kubernetes.Interface,
outClusterRadixClient radixclient.Interface,
token string,
impersonation radixmodels.Impersonation) Accounts {
return Accounts{
UserAccount: Account{
Client: outClusterClient,
RadixClient: outClusterRadixClient,
},
ServiceAccount: Account{
Client: inClusterClient,
RadixClient: inClusterRadixClient,
},
token: token,
impersonation: impersonation,
}
}
func NewServiceAccount(inClusterClient kubernetes.Interface, inClusterRadixClient radixclient.Interface) Account {
return Account{
Client: inClusterClient,
RadixClient: inClusterRadixClient,
}
}
// Accounts contains accounts for accessing k8s API.
type Accounts struct {
UserAccount Account
ServiceAccount Account
token string
impersonation radixmodels.Impersonation
}
// RadixHandlerFunc Pattern for handler functions
type RadixHandlerFunc func(Accounts, http.ResponseWriter, *http.Request)
// Controller Pattern of an rest/stream controller
type Controller interface {
GetRoutes() Routes
}
// DefaultController Default implementation
type DefaultController struct {
}
// Routes Holder of all routes
type Routes []Route
// Route Describe route
type Route struct {
Path string
Method string
HandlerFunc RadixHandlerFunc
AllowUnauthenticatedUsers bool
}
// GetUserAccountUserPrincipleName get the user principle name represented in UserAccount
func (accounts Accounts) GetUserAccountUserPrincipleName() (string, error) {
if accounts.impersonation.PerformImpersonation() {
return accounts.impersonation.User, nil
}
return getUserPrincipleNameFromToken(accounts.token)
}
func getUserPrincipleNameFromToken(token string) (string, error) {
claims := jwt.MapClaims{}
parser := jwt.Parser{}
_, _, err := parser.ParseUnverified(token, claims)
if err != nil {
return "", fmt.Errorf("could not parse token (%v)", err)
}
userPrincipleName := fmt.Sprintf("%v", claims["upn"])
return userPrincipleName, nil
}