-
Notifications
You must be signed in to change notification settings - Fork 6
/
auth.go
71 lines (58 loc) · 1.55 KB
/
auth.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
package kubeconfig
import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
type AuthType int
const (
AuthTypeUnknown AuthType = iota
AuthTypeServiceAccount
AuthTypeAuthProvider
AuthTypeClientCertificate
)
func GetAuthType(config *clientcmdapi.Config, contextName string) AuthType {
if contextName == "" {
return AuthTypeUnknown
}
currentContext, exists := config.Contexts[contextName]
if !exists {
return AuthTypeUnknown
}
authInfo, exists := config.AuthInfos[currentContext.AuthInfo]
if !exists {
return AuthTypeUnknown
}
switch {
case len(authInfo.Token) > 0:
return AuthTypeServiceAccount
case authInfo.AuthProvider != nil:
return AuthTypeAuthProvider
case len(authInfo.ClientCertificate) > 0:
return AuthTypeClientCertificate
case len(authInfo.ClientCertificateData) > 0:
return AuthTypeClientCertificate
case len(authInfo.ClientKey) > 0:
return AuthTypeClientCertificate
case len(authInfo.ClientKeyData) > 0:
return AuthTypeClientCertificate
}
return AuthTypeUnknown
}
// GetAuthProvider fetches the authentication provider from kubeconfig,
// for a desired context name.
func GetAuthProvider(config *clientcmdapi.Config, contextName string) (*clientcmdapi.AuthProviderConfig, bool) {
if contextName == "" {
return nil, false
}
currentContext, exists := config.Contexts[contextName]
if !exists {
return nil, false
}
authInfo, exists := config.AuthInfos[currentContext.AuthInfo]
if !exists {
return nil, false
}
if authInfo.AuthProvider == nil {
return nil, false
}
return authInfo.AuthProvider, true
}