/
client.go
498 lines (410 loc) · 13.8 KB
/
client.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright 2023 The Okteto Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package okteto
import (
"context"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"github.com/okteto/okteto/pkg/constants"
oktetoErrors "github.com/okteto/okteto/pkg/errors"
oktetoHttp "github.com/okteto/okteto/pkg/http"
oktetoLog "github.com/okteto/okteto/pkg/log"
"github.com/okteto/okteto/pkg/types"
"github.com/shurcooL/graphql"
"golang.org/x/oauth2"
)
// Client implementation to connect to Okteto API
type Client struct {
client graphqlClientInterface
namespace types.NamespaceInterface
user types.UserInterface
preview types.PreviewInterface
pipeline types.PipelineInterface
stream types.StreamInterface
kubetoken types.KubetokenInterface
endpoint types.EndpointClientInterface
}
type ClientProvider struct{}
var insecureSkipTLSVerify bool
var onceInsecureWarning *sync.Once = &sync.Once{}
var serverName string
var strictTLSOnce sync.Once
var errURLNotSet = errors.New("the okteto URL is not set")
// graphqlClientInterface contains the functions that a graphqlClient must have
type graphqlClientInterface interface {
Query(ctx context.Context, q interface{}, variables map[string]interface{}) error
Mutate(ctx context.Context, m interface{}, variables map[string]interface{}) error
}
func NewOktetoClientProvider() *ClientProvider {
return &ClientProvider{}
}
func (*ClientProvider) Provide(opts ...Option) (types.OktetoInterface, error) {
c, err := NewOktetoClient(opts...)
if err != nil {
return nil, err
}
return c, err
}
type ClientCfg struct {
CtxName string
Token string
Cert string
}
func WithCtxName(ctxName string) Option {
return func(cfg *ClientCfg) {
cfg.CtxName = ctxName
}
}
func WithToken(token string) Option {
return func(cfg *ClientCfg) {
cfg.Token = token
}
}
func defaultOktetoClientCfg() *ClientCfg {
if CurrentStore == nil && !ContextExists() {
return &ClientCfg{}
}
if CurrentStore != nil && CurrentStore.CurrentContext == "" {
return &ClientCfg{}
}
return &ClientCfg{
CtxName: GetContext().Name,
Token: GetContext().Token,
}
}
type Option func(*ClientCfg)
// NewOktetoClient creates a new client to connect with Okteto API
func NewOktetoClient(opts ...Option) (*Client, error) {
cfg := defaultOktetoClientCfg()
for _, opt := range opts {
opt(cfg)
}
if cfg.Token == "" {
okCtx, exists := GetContextStore().Contexts[cfg.CtxName]
if !exists {
return nil, fmt.Errorf("%s context doesn't exists", cfg.CtxName)
}
cfg.Token = okCtx.Token
}
httpClient, u, err := newOktetoHttpClient(cfg.CtxName, cfg.Token, "graphql")
if err != nil {
return nil, err
}
return newOktetoClientFromGraphqlClient(u, httpClient)
}
// NewOktetoClientStateless creates a new client to connect with Okteto API
func NewOktetoClientStateless(ocfg *ClientCfg, opts ...Option) (*Client, error) {
for _, opt := range opts {
opt(ocfg)
}
httpClient, u, err := newOktetoHttpClientStateless(ocfg.CtxName, ocfg.Token, ocfg.Cert, "graphql")
if err != nil {
return nil, err
}
return newOktetoClientFromGraphqlClient(u, httpClient)
}
func newOktetoHttpClient(contextName, token, oktetoUrlPath string) (*http.Client, string, error) {
if token == "" {
return nil, "", fmt.Errorf(oktetoErrors.ErrNotLogged, contextName)
}
u, err := parseOktetoURLWithPath(contextName, oktetoUrlPath)
if err != nil {
return nil, "", err
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token,
TokenType: "Bearer"},
)
sslTransportOption := &oktetoHttp.SSLTransportOption{}
if serverName != "" {
sslTransportOption.ServerName = serverName
sslTransportOption.URLsToIntercept = []string{u}
}
ctxHttpClient := oktetoHttp.StrictSSLHTTPClient(sslTransportOption)
if insecureSkipTLSVerify {
ctxHttpClient = oktetoHttp.InsecureHTTPClient()
} else if cert, err := GetContextCertificate(); err == nil {
sslTransportOption.Certs = []*x509.Certificate{cert}
ctxHttpClient = oktetoHttp.StrictSSLHTTPClient(sslTransportOption)
}
ctx := contextWithOauth2HttpClient(context.Background(), ctxHttpClient)
httpClient := oauth2.NewClient(ctx, src)
return httpClient, u, err
}
func newOktetoHttpClientStateless(contextName, token, cert, oktetoUrlPath string) (*http.Client, string, error) {
if token == "" {
return nil, "", fmt.Errorf(oktetoErrors.ErrNotLogged, contextName)
}
u, err := parseOktetoURLWithPath(contextName, oktetoUrlPath)
if err != nil {
return nil, "", err
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token,
TokenType: "Bearer"},
)
sslTransportOption := &oktetoHttp.SSLTransportOption{}
if serverName != "" {
sslTransportOption.ServerName = serverName
sslTransportOption.URLsToIntercept = []string{u}
}
ctxHttpClient := oktetoHttp.StrictSSLHTTPClient(sslTransportOption)
if insecureSkipTLSVerify {
ctxHttpClient = oktetoHttp.InsecureHTTPClient()
} else if cert, err := GetContextCertificateStateless(cert); err == nil {
sslTransportOption.Certs = []*x509.Certificate{cert}
ctxHttpClient = oktetoHttp.StrictSSLHTTPClient(sslTransportOption)
}
ctx := contextWithOauth2HttpClient(context.Background(), ctxHttpClient)
httpClient := oauth2.NewClient(ctx, src)
return httpClient, u, err
}
// NewOktetoClientFromUrlAndToken creates a new client to connect with Okteto API provided url and token
func NewOktetoClientFromUrlAndToken(url, token string) (*Client, error) {
u, err := parseOktetoURL(url)
if err != nil {
return nil, fmt.Errorf("could not create okteto client: %w", err)
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token,
TokenType: "Bearer"},
)
sslTransportOption := &oktetoHttp.SSLTransportOption{}
if serverName != "" {
sslTransportOption.ServerName = serverName
sslTransportOption.URLsToIntercept = []string{u}
}
ctxHttpClient := oktetoHttp.StrictSSLHTTPClient(sslTransportOption)
if insecureSkipTLSVerify {
ctxHttpClient = oktetoHttp.InsecureHTTPClient()
} else if cert, err := GetContextCertificate(); err == nil {
sslTransportOption.Certs = []*x509.Certificate{cert}
ctxHttpClient = oktetoHttp.StrictSSLHTTPClient(sslTransportOption)
}
ctx := contextWithOauth2HttpClient(context.Background(), ctxHttpClient)
httpClient := oauth2.NewClient(ctx, src)
return newOktetoClientFromGraphqlClient(u, httpClient)
}
// NewOktetoClientFromUrl creates a new client to connect with Okteto API provided an url
func NewOktetoClientFromUrl(url string) (*Client, error) {
u, err := parseOktetoURL(url)
if err != nil {
return nil, err
}
sslTransportOption := &oktetoHttp.SSLTransportOption{}
if serverName != "" {
sslTransportOption.ServerName = serverName
sslTransportOption.URLsToIntercept = []string{u}
}
ctxHttpClient := oktetoHttp.StrictSSLHTTPClient(sslTransportOption)
if insecureSkipTLSVerify {
ctxHttpClient = oktetoHttp.InsecureHTTPClient()
} else if cert, err := GetContextCertificate(); err == nil {
sslTransportOption.Certs = []*x509.Certificate{cert}
ctxHttpClient = oktetoHttp.StrictSSLHTTPClient(sslTransportOption)
}
ctx := contextWithOauth2HttpClient(context.Background(), ctxHttpClient)
httpClient := oauth2.NewClient(ctx, nil)
return newOktetoClientFromGraphqlClient(u, httpClient)
}
// contextWithOauth2HttpClient returns a context.Context with a value of type oauth2.HTTPClient so oauth2.NewClient() can be bootstrapped with a custom http.Client
func contextWithOauth2HttpClient(ctx context.Context, httpClient *http.Client) context.Context {
return context.WithValue(
ctx,
oauth2.HTTPClient,
httpClient,
)
}
func newOktetoClientFromGraphqlClient(url string, httpClient *http.Client) (*Client, error) {
c := &Client{
client: graphql.NewClient(url, httpClient),
}
c.namespace = newNamespaceClient(c.client)
c.preview = newPreviewClient(c.client)
c.user = newUserClient(c.client)
c.pipeline = newPipelineClient(c.client, url)
c.stream = newStreamClient(httpClient)
c.kubetoken = newKubeTokenClient(httpClient)
c.endpoint = newEndpointClient(c.client)
return c, nil
}
func parseOktetoURL(u string) (string, error) {
return parseOktetoURLWithPath(u, "graphql")
}
func parseOktetoURLWithPath(u, path string) (string, error) {
if u == "" {
return "", errURLNotSet
}
parsed, err := url.Parse(u)
if err != nil {
return "", err
}
if parsed.Scheme == "" {
parsed.Scheme = "https"
parsed.Host = parsed.Path
}
parsed.Path = path
return parsed.String(), nil
}
func translateAPIErr(err error) error {
e := strings.TrimPrefix(err.Error(), "graphql: ")
switch e {
case "not-authorized":
return fmt.Errorf(oktetoErrors.ErrNotLogged, GetContext().Name)
case "namespace-quota-exceeded":
return fmt.Errorf("you have exceeded your namespace quota. Contact us at hello@okteto.com to learn more")
case "namespace-quota-exceeded-onpremises":
return fmt.Errorf("you have exceeded your namespace quota, please contact your administrator to increase it")
case "users-limit-exceeded":
return fmt.Errorf("license limit exceeded. Contact your administrator to update your license and try again")
case "internal-server-error":
return fmt.Errorf("server temporarily unavailable, please try again")
case "non-200 OK status code: 401 Unauthorized body: \"\"":
return fmt.Errorf("unauthorized. Please run 'okteto context url' and try again")
case "non-200 OK status code: 401 Unauthorized body: \"not-authorized: token is expired\\n\"":
return oktetoErrors.ErrTokenExpired
case "not-found":
return oktetoErrors.ErrNotFound
default:
switch {
case oktetoErrors.IsX509(err):
return oktetoErrors.UserError{
E: err,
Hint: oktetoErrors.ErrX509Hint,
}
case isAPILicenseError(err):
return oktetoErrors.UserError{
E: oktetoErrors.ErrInvalidLicense,
Hint: "The Okteto instance for your current context has an expired or missing license. Please contact your administrator for more information",
}
}
oktetoLog.Infof("Unrecognized API error: %s", err)
return err
}
}
func isAPILicenseError(err error) bool {
return strings.HasPrefix(err.Error(), "non-200 OK status code: 423")
}
func isAPITransientErr(err error) bool {
if err == nil {
return false
}
switch {
case
strings.Contains(err.Error(), "can't assign requested address"),
strings.Contains(err.Error(), "command exited without exit status or exit signal"),
strings.Contains(err.Error(), "connection refused"),
strings.Contains(err.Error(), "connection reset by peer"),
strings.Contains(err.Error(), "client connection lost"),
strings.Contains(err.Error(), "nodename nor servname provided, or not known"),
strings.Contains(err.Error(), "unexpected EOF"),
strings.Contains(err.Error(), "TLS handshake timeout"),
strings.Contains(err.Error(), "broken pipe"),
strings.Contains(err.Error(), "No connection could be made"),
strings.Contains(err.Error(), "dial tcp: operation was canceled"),
strings.Contains(err.Error(), "network is unreachable"),
strings.Contains(err.Error(), "development container has been removed"):
return true
default:
return false
}
}
// InDevContainer returns true if running in an okteto dev container
func InDevContainer() bool {
if v, ok := os.LookupEnv(constants.OktetoNameEnvVar); ok && v != "" {
return true
}
return false
}
func query(ctx context.Context, query interface{}, variables map[string]interface{}, client graphqlClientInterface) error {
err := client.Query(ctx, query, variables)
if err != nil {
if isAPITransientErr(err) {
err = client.Query(ctx, query, variables)
}
if err != nil {
return translateAPIErr(err)
}
}
return nil
}
func mutate(ctx context.Context, mutation interface{}, variables map[string]interface{}, client graphqlClientInterface) error {
err := client.Mutate(ctx, mutation, variables)
if err != nil {
return translateAPIErr(err)
}
return nil
}
// Namespaces retrieves the NamespaceClient
func (c *Client) Namespaces() types.NamespaceInterface {
return c.namespace
}
// Previews retrieves the Previews client
func (c *Client) Previews() types.PreviewInterface {
return c.preview
}
// Pipeline retrieves the Pipeline client
func (c *Client) Pipeline() types.PipelineInterface {
return c.pipeline
}
// User retrieves the UserClient
func (c *Client) User() types.UserInterface {
return c.user
}
// Stream retrieves the Stream client
func (c *Client) Stream() types.StreamInterface {
return c.stream
}
// Kubetoken retrieves the Kubetoken client
func (c *Client) Kubetoken() types.KubetokenInterface {
return c.kubetoken
}
// Endpoint retrieves the Endpoint client
func (c *Client) Endpoint() types.EndpointClientInterface {
return c.endpoint
}
func SetInsecureSkipTLSVerifyPolicy(isInsecure bool) {
onceInsecureWarning.Do(func() {
oktetoLog.Debugf("insecure mode: %t", isInsecure)
if isInsecure {
oktetoLog.Warning("Insecure mode enabled")
}
insecureSkipTLSVerify = isInsecure
})
}
func IsInsecureSkipTLSVerifyPolicy() bool {
return insecureSkipTLSVerify
}
func SetServerNameOverride(serverNameOverride string) {
oktetoLog.Debugf("server name override: %q", serverNameOverride)
if serverNameOverride == "" {
return
}
host, port, err := net.SplitHostPort(serverNameOverride)
if err != nil {
oktetoLog.Fatalf("invalid server name %q: %s", serverNameOverride, err)
return
}
oktetoLog.Warning("Server name overridden to host=%s port=%s", host, port)
serverName = net.JoinHostPort(host, port)
}
func GetServerNameOverride() string {
return serverName
}