forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubeconfig.go
196 lines (161 loc) · 4.66 KB
/
kubeconfig.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package kubeconfig
import (
"encoding/base64"
"net/http"
"os"
"strings"
"github.com/pkg/errors"
"k8s.io/client-go/plugin/pkg/client/auth/exec"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/transport"
)
const (
BasicAuthScheme = "Basic"
BearerAuthScheme = "Bearer"
)
// get the default one from the filesystem
func DefaultRestConfig() (*restclient.Config, error) {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{})
return config.ClientConfig()
}
func IsBasicAuthScheme(token string) bool {
return strings.HasPrefix(token, BasicAuthScheme)
}
func IsBearerAuthScheme(token string) bool {
return strings.HasPrefix(token, BearerAuthScheme)
}
func GetRestConfig(token string) (*restclient.Config, error) {
if IsBasicAuthScheme(token) {
token = strings.TrimSpace(strings.TrimPrefix(token, BasicAuthScheme))
username, password, ok := decodeBasicAuthToken(token)
if !ok {
return nil, errors.New("Error parsing Basic Authentication")
}
return GetBasicRestConfig(username, password)
}
if IsBearerAuthScheme(token) {
token = strings.TrimSpace(strings.TrimPrefix(token, BearerAuthScheme))
return GetBearerRestConfig(token)
}
return nil, errors.New("Unsupported authentication scheme")
}
// convert a basic token (username, password) into a REST config
func GetBasicRestConfig(username, password string) (*restclient.Config, error) {
restConfig, err := DefaultRestConfig()
if err != nil {
return nil, err
}
restConfig.BearerToken = ""
restConfig.BearerTokenFile = ""
restConfig.Username = username
restConfig.Password = password
return restConfig, nil
}
// convert a bearer token into a REST config
func GetBearerRestConfig(token string) (*restclient.Config, error) {
restConfig, err := DefaultRestConfig()
if err != nil {
return nil, err
}
restConfig.BearerToken = ""
restConfig.BearerTokenFile = ""
restConfig.Username = ""
restConfig.Password = ""
if token != "" {
restConfig.BearerToken = token
}
return restConfig, nil
}
//Return the AuthString include Auth type(Basic or Bearer)
func GetAuthString(in *restclient.Config) (string, error) {
//Checking Basic Auth
if in.Username != "" {
token, err := GetBasicAuthToken(in)
return BasicAuthScheme + " " + token, err
}
token, err := GetBearerToken(in)
return BearerAuthScheme + " " + token, err
}
func GetBasicAuthToken(in *restclient.Config) (string, error) {
if in == nil {
return "", errors.Errorf("RestClient can't be nil")
}
return encodeBasicAuthToken(in.Username, in.Password), nil
}
// convert the REST config into a bearer token
func GetBearerToken(in *restclient.Config) (string, error) {
if len(in.BearerToken) > 0 {
return in.BearerToken, nil
}
if token := getEnvToken(); token != "" {
return token, nil
}
if in == nil {
return "", errors.Errorf("RestClient can't be nil")
}
if in.ExecProvider != nil {
tc, err := in.TransportConfig()
if err != nil {
return "", err
}
auth, err := exec.GetAuthenticator(in.ExecProvider)
if err != nil {
return "", err
}
//This function will return error because of TLS Cert missing,
// This code is not making actual request. We can ignore it.
_ = auth.UpdateTransportConfig(tc)
rt, err := transport.New(tc)
if err != nil {
return "", err
}
req := http.Request{Header: map[string][]string{}}
_, _ = rt.RoundTrip(&req)
token := req.Header.Get("Authorization")
return strings.TrimPrefix(token, "Bearer "), nil
}
if in.AuthProvider != nil {
if in.AuthProvider.Name == "gcp" {
tc, err := in.TransportConfig()
if err != nil {
return "", err
}
auth, err := restclient.GetAuthProvider(in.Host, in.AuthProvider, in.AuthConfigPersister)
if err != nil {
return "", err
}
rt, err := transport.New(tc)
if err != nil {
return "", err
}
rt = auth.WrapTransport(rt)
req := http.Request{Header: map[string][]string{}}
_, _ = rt.RoundTrip(&req)
token := in.AuthProvider.Config["access-token"]
return strings.TrimPrefix(token, "Bearer "), nil
}
}
return "", errors.Errorf("could not find a token")
}
// Get the Auth token from environment variable
func getEnvToken() string {
return os.Getenv("ARGO_TOKEN")
}
func encodeBasicAuthToken(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func decodeBasicAuthToken(auth string) (username, password string, ok bool) {
c, err := base64.StdEncoding.DecodeString(auth)
if err != nil {
return
}
cs := string(c)
s := strings.IndexByte(cs, ':')
if s < 0 {
return
}
return cs[:s], cs[s+1:], true
}