aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/yandex/tvm/tvmtool/tool.go
blob: 2e8dee98e1e50750e07252ef87f51304233da828 (plain) (blame)
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package tvmtool

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"sync/atomic"
	"time"

	"github.com/ydb-platform/ydb/library/go/core/log"
	"github.com/ydb-platform/ydb/library/go/core/log/nop"
	"github.com/ydb-platform/ydb/library/go/core/xerrors"
	"github.com/ydb-platform/ydb/library/go/yandex/tvm"
	"github.com/ydb-platform/ydb/library/go/yandex/tvm/tvmtool/internal/cache"
)

const (
	dialTimeout    = 100 * time.Millisecond
	requestTimeout = 500 * time.Millisecond
	keepAlive      = 60 * time.Second
	cacheTTL       = 10 * time.Minute
	cacheMaxTTL    = 11 * time.Hour
)

var _ tvm.Client = (*Client)(nil)

type (
	Client struct {
		lastSync        int64
		apiURI          string
		baseURI         string
		src             string
		authToken       string
		bbEnv           string
		refreshFreq     int64
		bgCtx           context.Context
		bgCancel        context.CancelFunc
		inFlightRefresh uint32
		cache           *cache.Cache
		pingRequest     *http.Request
		ownHTTPClient   bool
		httpClient      *http.Client
		l               log.Structured
		cachedRoles     *atomic.Pointer[tvm.Roles]
	}

	ticketsResponse map[string]struct {
		Error  string       `json:"error"`
		Ticket string       `json:"ticket"`
		TvmID  tvm.ClientID `json:"tvm_id"`
	}

	checkSrvResponse struct {
		SrcID   tvm.ClientID `json:"src"`
		DstID   tvm.ClientID `json:"dst"`
		Error   string       `json:"error"`
		DbgInfo string       `json:"debug_string"`
		LogInfo string       `json:"logging_string"`
	}

	checkUserResponse struct {
		DefaultUID tvm.UID   `json:"default_uid"`
		UIDs       []tvm.UID `json:"uids"`
		Scopes     []string  `json:"scopes"`
		Error      string    `json:"error"`
		DbgInfo    string    `json:"debug_string"`
		LogInfo    string    `json:"logging_string"`
	}
)

// NewClient method creates a new tvmtool client.
// You must reuse it to prevent connection/goroutines leakage.
func NewClient(apiURI string, opts ...Option) (*Client, error) {
	baseURI := strings.TrimRight(apiURI, "/") + "/tvm"
	pingRequest, err := http.NewRequest("GET", baseURI+"/ping", nil)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: failed to configure client: %w", err)
	}

	transport := http.DefaultTransport.(*http.Transport).Clone()
	transport.DialContext = (&net.Dialer{
		Timeout:   dialTimeout,
		KeepAlive: keepAlive,
	}).DialContext

	tool := &Client{
		apiURI:        apiURI,
		baseURI:       baseURI,
		refreshFreq:   8 * 60,
		cache:         cache.New(cacheTTL, cacheMaxTTL),
		pingRequest:   pingRequest,
		l:             &nop.Logger{},
		ownHTTPClient: true,
		httpClient: &http.Client{
			Transport: transport,
			Timeout:   requestTimeout,
			CheckRedirect: func(req *http.Request, via []*http.Request) error {
				return http.ErrUseLastResponse
			},
		},
		cachedRoles: &atomic.Pointer[tvm.Roles]{},
	}

	for _, opt := range opts {
		if err := opt(tool); err != nil {
			return nil, xerrors.Errorf("tvmtool: failed to configure client: %w", err)
		}
	}

	if tool.bgCtx != nil {
		go tool.serviceTicketsRefreshLoop()
	}

	return tool, nil
}

// GetServiceTicketForAlias returns TVM service ticket for alias
//
// WARNING: alias must be configured in tvmtool
//
// TVMTool documentation: https://wiki.yandex-team.ru/passport/tvm2/tvm-daemon/#/tvm/tickets
func (c *Client) GetServiceTicketForAlias(ctx context.Context, alias string) (string, error) {
	var (
		cachedTicket *string
		cacheStatus  = cache.Miss
	)

	if c.cache != nil {
		c.refreshServiceTickets()

		if cachedTicket, cacheStatus = c.cache.LoadByAlias(alias); cacheStatus == cache.Hit {
			return *cachedTicket, nil
		}
	}

	tickets, err := c.getServiceTickets(ctx, alias)
	if err != nil {
		if cachedTicket != nil && cacheStatus == cache.GonnaMissy {
			return *cachedTicket, nil
		}
		return "", err
	}

	entry, ok := tickets[alias]
	if !ok {
		return "", xerrors.Errorf("tvmtool: alias %q was not found in response", alias)
	}

	if entry.Error != "" {
		return "", &Error{Code: ErrorOther, Msg: entry.Error}
	}

	ticket := entry.Ticket
	if c.cache != nil {
		c.cache.Store(entry.TvmID, alias, &ticket)
	}
	return ticket, nil
}

// GetServiceTicketForID returns TVM service ticket for destination application id
//
// WARNING: id must be configured in tvmtool
//
// TVMTool documentation: https://wiki.yandex-team.ru/passport/tvm2/tvm-daemon/#/tvm/tickets
func (c *Client) GetServiceTicketForID(ctx context.Context, dstID tvm.ClientID) (string, error) {
	var (
		cachedTicket *string
		cacheStatus  = cache.Miss
	)

	if c.cache != nil {
		c.refreshServiceTickets()

		if cachedTicket, cacheStatus = c.cache.Load(dstID); cacheStatus == cache.Hit {
			return *cachedTicket, nil
		}
	}

	alias := strconv.FormatUint(uint64(dstID), 10)
	tickets, err := c.getServiceTickets(ctx, alias)
	if err != nil {
		if cachedTicket != nil && cacheStatus == cache.GonnaMissy {
			return *cachedTicket, nil
		}
		return "", err
	}

	entry, ok := tickets[alias]
	if !ok {
		// ok, let's find him
		for candidateAlias, candidate := range tickets {
			if candidate.TvmID == dstID {
				entry = candidate
				alias = candidateAlias
				ok = true
				break
			}
		}

		if !ok {
			return "", xerrors.Errorf("tvmtool: dst %q was not found in response", alias)
		}
	}

	if entry.Error != "" {
		return "", &Error{Code: ErrorOther, Msg: entry.Error}
	}

	ticket := entry.Ticket
	if c.cache != nil {
		c.cache.Store(dstID, alias, &ticket)
	}
	return ticket, nil
}

// Close stops background ticket updates (if configured) and closes idle connections.
func (c *Client) Close() {
	if c.bgCancel != nil {
		c.bgCancel()
	}

	if c.ownHTTPClient {
		c.httpClient.CloseIdleConnections()
	}
}

func (c *Client) refreshServiceTickets() {
	if c.bgCtx != nil {
		// service tickets will be updated at background in the separated goroutine
		return
	}

	now := time.Now().Unix()
	if now-atomic.LoadInt64(&c.lastSync) > c.refreshFreq {
		atomic.StoreInt64(&c.lastSync, now)
		if atomic.CompareAndSwapUint32(&c.inFlightRefresh, 0, 1) {
			go c.doServiceTicketsRefresh(context.Background())
		}
	}
}

func (c *Client) serviceTicketsRefreshLoop() {
	var ticker = time.NewTicker(time.Duration(c.refreshFreq) * time.Second)
	defer ticker.Stop()
	for {
		select {
		case <-c.bgCtx.Done():
			return
		case <-ticker.C:
			c.doServiceTicketsRefresh(c.bgCtx)
		}
	}
}

func (c *Client) doServiceTicketsRefresh(ctx context.Context) {
	defer atomic.CompareAndSwapUint32(&c.inFlightRefresh, 1, 0)

	c.cache.Gc()
	aliases := c.cache.Aliases()
	if len(aliases) == 0 {
		return
	}

	c.l.Debug("tvmtool: service ticket update started")
	defer c.l.Debug("tvmtool: service ticket update finished")

	// fast path: batch update, must work most of time
	err := c.refreshServiceTicket(ctx, aliases...)
	if err == nil {
		return
	}

	if tvmErr, ok := err.(*Error); ok && tvmErr.Code != ErrorBadRequest {
		c.l.Error(
			"tvmtool: failed to refresh all service tickets at background",
			log.Strings("dsts", aliases),
			log.Error(err),
		)

		// if we have non "bad request" error - something really terrible happens, nothing to do with it :(
		// TODO(buglloc): implement adaptive refreshFreq based on errors?
		return
	}

	// slow path: trying to update service tickets one by one
	c.l.Error(
		"tvmtool: failed to refresh all service tickets at background, switched to slow path",
		log.Strings("dsts", aliases),
		log.Error(err),
	)

	for _, dst := range aliases {
		if err := c.refreshServiceTicket(ctx, dst); err != nil {
			c.l.Error(
				"tvmtool: failed to refresh service ticket at background",
				log.String("dst", dst),
				log.Error(err),
			)
		}
	}
}

func (c *Client) refreshServiceTicket(ctx context.Context, dsts ...string) error {
	tickets, err := c.getServiceTickets(ctx, strings.Join(dsts, ","))
	if err != nil {
		return err
	}

	for _, dst := range dsts {
		entry, ok := tickets[dst]
		if !ok {
			c.l.Error(
				"tvmtool: destination was not found in tvmtool response",
				log.String("dst", dst),
			)
			continue
		}

		if entry.Error != "" {
			c.l.Error(
				"tvmtool: failed to get service ticket for destination",
				log.String("dst", dst),
				log.String("err", entry.Error),
			)
			continue
		}

		c.cache.Store(entry.TvmID, dst, &entry.Ticket)
	}
	return nil
}

func (c *Client) getServiceTickets(ctx context.Context, dst string) (ticketsResponse, error) {
	params := url.Values{
		"dsts": {dst},
	}
	if c.src != "" {
		params.Set("src", c.src)
	}

	req, err := http.NewRequest("GET", c.baseURI+"/tickets?"+params.Encode(), nil)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: failed to call tvmtool: %w", err)
	}
	req.Header.Set("Authorization", c.authToken)

	req = req.WithContext(ctx)
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: failed to call tvmtool: %w", err)
	}

	var result ticketsResponse
	err = readResponse(resp, &result)
	return result, err
}

// Check TVM service ticket
//
// TVMTool documentation: https://wiki.yandex-team.ru/passport/tvm2/tvm-daemon/#/tvm/checksrv
func (c *Client) CheckServiceTicket(ctx context.Context, ticket string) (*tvm.CheckedServiceTicket, error) {
	req, err := http.NewRequest("GET", c.baseURI+"/checksrv", nil)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: failed to call tvmtool: %w", err)
	}

	if c.src != "" {
		req.URL.RawQuery += "dst=" + url.QueryEscape(c.src)
	}
	req.Header.Set("Authorization", c.authToken)
	req.Header.Set("X-Ya-Service-Ticket", ticket)

	req = req.WithContext(ctx)
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: failed to call tvmtool: %w", err)
	}

	var result checkSrvResponse
	if err = readResponse(resp, &result); err != nil {
		return nil, err
	}

	ticketInfo := &tvm.CheckedServiceTicket{
		SrcID:   result.SrcID,
		DstID:   result.DstID,
		DbgInfo: result.DbgInfo,
		LogInfo: result.LogInfo,
	}

	if resp.StatusCode == http.StatusForbidden {
		err = &TicketError{Status: TicketErrorOther, Msg: result.Error}
	}

	return ticketInfo, err
}

// Check TVM user ticket
//
// TVMTool documentation: https://wiki.yandex-team.ru/passport/tvm2/tvm-daemon/#/tvm/checkusr
func (c *Client) CheckUserTicket(ctx context.Context, ticket string, opts ...tvm.CheckUserTicketOption) (*tvm.CheckedUserTicket, error) {
	for range opts {
		panic("implement me")
	}

	req, err := http.NewRequest("GET", c.baseURI+"/checkusr", nil)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: failed to call tvmtool: %w", err)
	}
	if c.bbEnv != "" {
		req.URL.RawQuery += "override_env=" + url.QueryEscape(c.bbEnv)
	}
	req.Header.Set("Authorization", c.authToken)
	req.Header.Set("X-Ya-User-Ticket", ticket)

	req = req.WithContext(ctx)
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: failed to call tvmtool: %w", err)
	}

	var result checkUserResponse
	if err = readResponse(resp, &result); err != nil {
		return nil, err
	}

	ticketInfo := &tvm.CheckedUserTicket{
		DefaultUID: result.DefaultUID,
		UIDs:       result.UIDs,
		Scopes:     result.Scopes,
		DbgInfo:    result.DbgInfo,
		LogInfo:    result.LogInfo,
	}

	if resp.StatusCode == http.StatusForbidden {
		err = &TicketError{Status: TicketErrorOther, Msg: result.Error}
	}

	return ticketInfo, err
}

// Checks TVMTool liveness
//
// TVMTool documentation: https://wiki.yandex-team.ru/passport/tvm2/tvm-daemon/#/tvm/ping
func (c *Client) GetStatus(ctx context.Context) (tvm.ClientStatusInfo, error) {
	req := c.pingRequest.WithContext(ctx)
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return tvm.ClientStatusInfo{Status: tvm.ClientError},
			&PingError{Code: PingCodeDie, Err: err}
	}
	defer func() { _ = resp.Body.Close() }()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return tvm.ClientStatusInfo{Status: tvm.ClientError},
			&PingError{Code: PingCodeDie, Err: err}
	}

	var status tvm.ClientStatusInfo
	switch resp.StatusCode {
	case http.StatusOK:
		// OK!
		status = tvm.ClientStatusInfo{Status: tvm.ClientOK}
		err = nil
	case http.StatusPartialContent:
		status = tvm.ClientStatusInfo{Status: tvm.ClientWarning}
		err = &PingError{Code: PingCodeWarning, Err: xerrors.New(string(body))}
	case http.StatusInternalServerError:
		status = tvm.ClientStatusInfo{Status: tvm.ClientError}
		err = &PingError{Code: PingCodeError, Err: xerrors.New(string(body))}
	default:
		status = tvm.ClientStatusInfo{Status: tvm.ClientError}
		err = &PingError{Code: PingCodeOther, Err: xerrors.Errorf("tvmtool: unexpected status: %d", resp.StatusCode)}
	}
	return status, err
}

// Returns TVMTool version
func (c *Client) Version(ctx context.Context) (string, error) {
	req := c.pingRequest.WithContext(ctx)
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return "", xerrors.Errorf("tvmtool: failed to call tmvtool: %w", err)
	}
	_, _ = io.ReadAll(resp.Body)
	_ = resp.Body.Close()

	return resp.Header.Get("Server"), nil
}

func (c *Client) GetRoles(ctx context.Context) (*tvm.Roles, error) {
	var cachedRevision string
	cachedRolesValue := c.cachedRoles.Load()
	if cachedRolesValue != nil {
		cachedRevision = cachedRolesValue.GetMeta().Revision
	}

	params := url.Values{
		"self": []string{c.src},
	}
	req, err := http.NewRequest("GET", c.apiURI+"/v2/roles?"+params.Encode(), nil)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: failed to make request to roles: %w", err)
	}
	req.Header.Set("Authorization", c.authToken)
	if cachedRevision != "" {
		req.Header.Set("If-None-Match", "\""+cachedRevision+"\"")
	}
	req = req.WithContext(ctx)

	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: failed to call tvmtool: %w", err)
	}
	defer func() {
		_, _ = io.Copy(io.Discard, resp.Body)
		_ = resp.Body.Close()
	}()

	if resp.StatusCode == http.StatusNotModified {
		if cachedRolesValue == nil {
			return nil, xerrors.Errorf("tvmtool: logic error got 304 on empty cached roles data")
		}
		return cachedRolesValue, nil
	}

	if resp.StatusCode != http.StatusOK {
		return nil, xerrors.Errorf("tvmtool: getroles: [%d] %s", resp.StatusCode, resp.Status)
	}

	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: getroles: [%d] %s: %w", resp.StatusCode, resp.Status, err)
	}

	roles, err := tvm.NewRoles(b)
	if err != nil {
		return nil, xerrors.Errorf("tvmtool: unable to parse roles: %w", err)
	}

	c.cachedRoles.Store(roles)

	return roles, nil
}

func readResponse(resp *http.Response, dst interface{}) error {
	body, err := io.ReadAll(resp.Body)
	_ = resp.Body.Close()
	if err != nil {
		return xerrors.Errorf("tvmtool: failed to read response: %w", err)
	}

	switch resp.StatusCode {
	case http.StatusOK, http.StatusForbidden:
		// ok
		return json.Unmarshal(body, dst)
	case http.StatusUnauthorized:
		return &Error{
			Code: ErrorAuthFail,
			Msg:  string(body),
		}
	case http.StatusBadRequest:
		return &Error{
			Code: ErrorBadRequest,
			Msg:  string(body),
		}
	case http.StatusInternalServerError:
		return &Error{
			Code:      ErrorOther,
			Msg:       string(body),
			Retriable: true,
		}
	default:
		return &Error{
			Code: ErrorOther,
			Msg:  fmt.Sprintf("tvmtool: unexpected status: %d, msg: %s", resp.StatusCode, string(body)),
		}
	}
}