-
Notifications
You must be signed in to change notification settings - Fork 0
/
clt.go
849 lines (771 loc) · 26 KB
/
clt.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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
Copyright 2015 Gravitational, Inc.
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 auth
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/httplib"
"github.com/gravitational/teleport/lib/recorder"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/session"
log "github.com/Sirupsen/logrus"
"github.com/codahale/lunk"
"github.com/gravitational/configure/cstrings"
"github.com/gravitational/roundtrip"
"github.com/gravitational/trace"
)
// CurrentVersion is a current API version
const CurrentVersion = "v1"
// Client is HTTP API client that connects to the remote server
type Client struct {
roundtrip.Client
}
// NewClient returns a new instance of the client
func NewClient(addr string, params ...roundtrip.ClientParam) (*Client, error) {
log.Infof("auth.NewClient(%v)", addr)
c, err := roundtrip.NewClient(addr, CurrentVersion, params...)
if err != nil {
return nil, err
}
return &Client{*c}, nil
}
// PostJSON is a generic method that issues http POST request to the server
func (c *Client) PostJSON(
endpoint string, val interface{}) (*roundtrip.Response, error) {
return httplib.ConvertResponse(c.Client.PostJSON(endpoint, val))
}
// PutJSON is a generic method that issues http PUT request to the server
func (c *Client) PutJSON(
endpoint string, val interface{}) (*roundtrip.Response, error) {
return httplib.ConvertResponse(c.Client.PutJSON(endpoint, val))
}
// PostForm is a generic method that issues http POST request to the server
func (c *Client) PostForm(
endpoint string,
vals url.Values,
files ...roundtrip.File) (*roundtrip.Response, error) {
return httplib.ConvertResponse(c.Client.PostForm(endpoint, vals, files...))
}
// Get issues http GET request to the server
func (c *Client) Get(u string, params url.Values) (*roundtrip.Response, error) {
return httplib.ConvertResponse(c.Client.Get(u, params))
}
// Delete issues http Delete Request to the server
func (c *Client) Delete(u string) (*roundtrip.Response, error) {
return httplib.ConvertResponse(c.Client.Delete(u))
}
// GetSessions returns a list of active sessions in the cluster
// as reported by auth server
func (c *Client) GetSessions() ([]session.Session, error) {
out, err := c.Get(c.Endpoint("sessions"), url.Values{})
if err != nil {
return nil, trace.Wrap(err)
}
var sessions []session.Session
if err := json.Unmarshal(out.Bytes(), &sessions); err != nil {
return nil, err
}
return sessions, nil
}
// GetSession returns a session by ID
func (c *Client) GetSession(id session.ID) (*session.Session, error) {
// saving extra round-trip
if err := id.Check(); err != nil {
return nil, trace.Wrap(err)
}
out, err := c.Get(c.Endpoint("sessions", string(id)), url.Values{})
if err != nil {
return nil, trace.Wrap(err)
}
var sess *session.Session
if err := json.Unmarshal(out.Bytes(), &sess); err != nil {
return nil, trace.Wrap(err)
}
return sess, nil
}
// DeleteSession deletes a session by ID
func (c *Client) DeleteSession(id string) error {
_, err := c.Delete(c.Endpoint("sessions", id))
return trace.Wrap(err)
}
// CreateSession creates new session
func (c *Client) CreateSession(sess session.Session) error {
_, err := c.PostJSON(c.Endpoint("sessions"), createSessionReq{Session: sess})
return trace.Wrap(err)
}
// UpdateSession updates existing session
func (c *Client) UpdateSession(req session.UpdateRequest) error {
if err := req.Check(); err != nil {
return trace.Wrap(err)
}
_, err := c.PutJSON(c.Endpoint("sessions", string(req.ID)), updateSessionReq{Update: req})
return trace.Wrap(err)
}
// UpsertParty updates existing session party or inserts new party
func (c *Client) UpsertParty(id session.ID, p session.Party, ttl time.Duration) error {
// saving extra round-trip
if err := id.Check(); err != nil {
return trace.Wrap(err)
}
_, err := c.PostJSON(c.Endpoint("sessions", string(id), "parties"), upsertPartyReq{Party: p, TTL: ttl})
return trace.Wrap(err)
}
// GetLocalDomain returns local auth domain of the current auth server
func (c *Client) GetLocalDomain() (string, error) {
out, err := c.Get(c.Endpoint("domain"), url.Values{})
if err != nil {
return "", trace.Wrap(err)
}
var domain string
if err := json.Unmarshal(out.Bytes(), &domain); err != nil {
return "", trace.Wrap(err)
}
return domain, nil
}
func (c *Client) Close() error {
return nil
}
// UpsertCertAuthority updates or inserts new cert authority
func (c *Client) UpsertCertAuthority(ca services.CertAuthority, ttl time.Duration) error {
if err := ca.Check(); err != nil {
return trace.Wrap(err)
}
_, err := c.PostJSON(c.Endpoint("authorities", string(ca.Type)),
upsertCertAuthorityReq{CA: ca, TTL: ttl})
return trace.Wrap(err)
}
func (c *Client) GetCertAuthorities(caType services.CertAuthType, loadKeys bool) ([]*services.CertAuthority, error) {
if err := caType.Check(); err != nil {
return nil, trace.Wrap(err)
}
out, err := c.Get(c.Endpoint("authorities", string(caType)), url.Values{
"load_keys": []string{fmt.Sprintf("%t", loadKeys)},
})
if err != nil {
return nil, trace.Wrap(err)
}
var re []*services.CertAuthority
if err := json.Unmarshal(out.Bytes(), &re); err != nil {
return nil, err
}
return re, nil
}
func (c *Client) DeleteCertAuthority(id services.CertAuthID) error {
if err := id.Check(); err != nil {
return trace.Wrap(err)
}
_, err := c.Delete(c.Endpoint("authorities", string(id.Type), id.DomainName))
return trace.Wrap(err)
}
// GenerateToken creates a special provisioning token for a new SSH server
// that is valid for ttl period seconds.
//
// This token is used by SSH server to authenticate with Auth server
// and get signed certificate and private key from the auth server.
//
// The token can be used only once.
func (c *Client) GenerateToken(role teleport.Role, ttl time.Duration) (string, error) {
out, err := c.PostJSON(c.Endpoint("tokens"), generateTokenReq{
Role: role,
})
if err != nil {
return "", trace.Wrap(err)
}
var token string
if err := json.Unmarshal(out.Bytes(), &token); err != nil {
return "", trace.Wrap(err)
}
return token, nil
}
// RegisterUserToken calls the auth service API to register a new node via registration token
// which has been previously issued via GenerateToken
func (c *Client) RegisterUsingToken(token, hostID string, role teleport.Role) (*PackedKeys, error) {
out, err := c.PostJSON(c.Endpoint("tokens", "register"),
registerUsingTokenReq{
HostID: hostID,
Token: token,
Role: role,
})
if err != nil {
return nil, trace.Wrap(err)
}
var keys PackedKeys
if err := json.Unmarshal(out.Bytes(), &keys); err != nil {
return nil, trace.Wrap(err)
}
return &keys, nil
}
func (c *Client) RegisterNewAuthServer(token string) error {
_, err := c.PostJSON(c.Endpoint("tokens", "register", "auth"), registerNewAuthServerReq{
Token: token,
})
return trace.Wrap(err)
}
func (c *Client) Log(id lunk.EventID, e lunk.Event) {
en := lunk.NewEntry(id, e)
en.Time = time.Now()
c.LogEntry(en)
}
func (c *Client) LogEntry(en lunk.Entry) error {
_, err := c.PostJSON(c.Endpoint("events"), submitEventsReq{Events: []lunk.Entry{en}})
return trace.Wrap(err)
}
func (c *Client) LogSession(sess session.Session) error {
_, err := c.PostJSON(c.Endpoint("events", "sessions"), logSessionsReq{Sessions: []session.Session{sess}})
return trace.Wrap(err)
}
// GetEvents returns a list of filtered events
func (c *Client) GetEvents(filter events.Filter) ([]lunk.Entry, error) {
vals, err := events.FilterToURL(filter)
if err != nil {
return nil, trace.Wrap(err)
}
out, err := c.Get(c.Endpoint("events"), vals)
if err != nil {
return nil, trace.Wrap(err)
}
var events []lunk.Entry
if err := json.Unmarshal(out.Bytes(), &events); err != nil {
return nil, trace.Wrap(err)
}
return events, nil
}
// GetSessionEvents returns a list of filtered session events
func (c *Client) GetSessionEvents(filter events.Filter) ([]session.Session, error) {
vals, err := events.FilterToURL(filter)
if err != nil {
return nil, trace.Wrap(err)
}
out, err := c.Get(c.Endpoint("events", "sessions"), vals)
if err != nil {
return nil, trace.Wrap(err)
}
var events []session.Session
if err := json.Unmarshal(out.Bytes(), &events); err != nil {
return nil, trace.Wrap(err)
}
return events, nil
}
// GetChunkWriter returns a writer for chunks (parts of the recorded session)
func (c *Client) GetChunkWriter(id string) (recorder.ChunkWriteCloser, error) {
return &chunkRW{c: c, id: id}, nil
}
// GetChunkReader returns a reader of recorded session
func (c *Client) GetChunkReader(id string) (recorder.ChunkReadCloser, error) {
return &chunkRW{c: c, id: id}, nil
}
// UpsertNode is used by SSH servers to reprt their presense
// to the auth servers in form of hearbeat expiring after ttl period.
func (c *Client) UpsertNode(s services.Server, ttl time.Duration) error {
args := upsertServerReq{
Server: s,
TTL: ttl,
}
_, err := c.PostJSON(c.Endpoint("nodes"), args)
return trace.Wrap(err)
}
// GetNodes returns the list of servers registered in the cluster.
func (c *Client) GetNodes() ([]services.Server, error) {
out, err := c.Get(c.Endpoint("nodes"), url.Values{})
if err != nil {
return nil, trace.Wrap(err)
}
var re []services.Server
if err := json.Unmarshal(out.Bytes(), &re); err != nil {
return nil, trace.Wrap(err)
}
return re, nil
}
// UpsertReverseTunnel is used by admins to create a new reverse tunnel
// to the remote proxy to bypass firewall restrictions
func (c *Client) UpsertReverseTunnel(tunnel services.ReverseTunnel, ttl time.Duration) error {
args := upsertReverseTunnelReq{
ReverseTunnel: tunnel,
TTL: ttl,
}
_, err := c.PostJSON(c.Endpoint("reversetunnels"), args)
return trace.Wrap(err)
}
// GetReverseTunnels returns the list of created reverse tunnels
func (c *Client) GetReverseTunnels() ([]services.ReverseTunnel, error) {
out, err := c.Get(c.Endpoint("reversetunnels"), url.Values{})
if err != nil {
return nil, trace.Wrap(err)
}
var tunnels []services.ReverseTunnel
if err := json.Unmarshal(out.Bytes(), &tunnels); err != nil {
return nil, trace.Wrap(err)
}
return tunnels, nil
}
// DeleteReverseTunnel deletes reverse tunnel by domain name
func (c *Client) DeleteReverseTunnel(domainName string) error {
// this is to avoid confusing error in case if domain emtpy for example
// HTTP route will fail producing generic not found error
// instead we catch the error here
if !cstrings.IsValidDomainName(domainName) {
return trace.BadParameter("'%v' is a bad domain name", domainName)
}
_, err := c.Delete(c.Endpoint("reversetunnels", domainName))
return trace.Wrap(err)
}
// UpsertAuthServer is used by auth servers to report their presense
// to other auth servers in form of hearbeat expiring after ttl period.
func (c *Client) UpsertAuthServer(s services.Server, ttl time.Duration) error {
args := upsertServerReq{
Server: s,
TTL: ttl,
}
_, err := c.PostJSON(c.Endpoint("authservers"), args)
return trace.Wrap(err)
}
// GetAuthServers returns the list of auth servers registered in the cluster.
func (c *Client) GetAuthServers() ([]services.Server, error) {
out, err := c.Get(c.Endpoint("authservers"), url.Values{})
if err != nil {
return nil, trace.Wrap(err)
}
var re []services.Server
if err := json.Unmarshal(out.Bytes(), &re); err != nil {
return nil, trace.Wrap(err)
}
return re, nil
}
// UpsertProxy is used by proxies to report their presense
// to other auth servers in form of hearbeat expiring after ttl period.
func (c *Client) UpsertProxy(s services.Server, ttl time.Duration) error {
args := upsertServerReq{
Server: s,
TTL: ttl,
}
_, err := c.PostJSON(c.Endpoint("proxies"), args)
return trace.Wrap(err)
}
// GetProxies returns the list of auth servers registered in the cluster.
func (c *Client) GetProxies() ([]services.Server, error) {
out, err := c.Get(c.Endpoint("proxies"), url.Values{})
if err != nil {
return nil, trace.Wrap(err)
}
var re []services.Server
if err := json.Unmarshal(out.Bytes(), &re); err != nil {
return nil, trace.Wrap(err)
}
return re, nil
}
// UpsertPassword updates web access password for the user
func (c *Client) UpsertPassword(user string,
password []byte) (hotpURL string, hotpQR []byte, err error) {
out, err := c.PostJSON(
c.Endpoint("users", user, "web", "password"),
upsertPasswordReq{
Password: string(password),
})
if err != nil {
return "", nil, trace.Wrap(err)
}
var re *upsertPasswordResponse
if err := json.Unmarshal(out.Bytes(), &re); err != nil {
return "", nil, err
}
return re.HotpURL, re.HotpQR, err
}
// UpsertUser user updates or inserts user entry
func (c *Client) UpsertUser(user services.User) error {
_, err := c.PostJSON(c.Endpoint("users"), upsertUserReq{User: user})
return trace.Wrap(err)
}
// CheckPassword checks if the suplied web access password is valid.
func (c *Client) CheckPassword(user string,
password []byte, hotpToken string) error {
_, err := c.PostJSON(
c.Endpoint("users", user, "web", "password", "check"),
checkPasswordReq{
Password: string(password),
HOTPToken: hotpToken,
})
return trace.Wrap(err)
}
// SignIn checks if the web access password is valid, and if it is valid
// returns a secure web session id.
func (c *Client) SignIn(user string, password []byte) (*Session, error) {
out, err := c.PostJSON(
c.Endpoint("users", user, "web", "signin"),
signInReq{
Password: string(password),
},
)
if err != nil {
return nil, trace.Wrap(err)
}
var sess *Session
if err := json.Unmarshal(out.Bytes(), &sess); err != nil {
return nil, err
}
return sess, nil
}
// ExtendWebSession creates a new web session for a user based on another
// valid web session
func (c *Client) ExtendWebSession(user string, prevSessionID string) (*Session, error) {
out, err := c.PostJSON(
c.Endpoint("users", user, "web", "sessions"),
createWebSessionReq{
PrevSessionID: prevSessionID,
},
)
if err != nil {
return nil, trace.Wrap(err)
}
var sess *Session
if err := json.Unmarshal(out.Bytes(), &sess); err != nil {
return nil, err
}
return sess, nil
}
// CreateWebSession creates a new web session for a user
func (c *Client) CreateWebSession(user string) (*Session, error) {
out, err := c.PostJSON(
c.Endpoint("users", user, "web", "sessions"),
createWebSessionReq{},
)
if err != nil {
return nil, trace.Wrap(err)
}
var sess *Session
if err := json.Unmarshal(out.Bytes(), &sess); err != nil {
return nil, err
}
return sess, nil
}
// GetWebSessionInfo check if a web sesion is valid, returns session id in case if
// it is valid, or error otherwise.
func (c *Client) GetWebSessionInfo(user string, sid string) (*Session, error) {
out, err := c.Get(
c.Endpoint("users", user, "web", "sessions", sid), url.Values{})
if err != nil {
return nil, trace.Wrap(err)
}
var sess *Session
if err := json.Unmarshal(out.Bytes(), &sess); err != nil {
return nil, trace.Wrap(err)
}
return sess, nil
}
// DeleteWebSession deletes a web session for this user by id
func (c *Client) DeleteWebSession(user string, sid string) error {
_, err := c.Delete(c.Endpoint("users", user, "web", "sessions", sid))
return trace.Wrap(err)
}
// GetUser returns a list of usernames registered in the system
func (c *Client) GetUser(name string) (services.User, error) {
if name == "" {
return nil, trace.BadParameter("missing username")
}
out, err := c.Get(c.Endpoint("users", name), url.Values{})
if err != nil {
return nil, trace.Wrap(err)
}
user, err := services.GetUserUnmarshaler()(out.Bytes())
if err != nil {
return nil, trace.Wrap(err)
}
return user, nil
}
// GetUsers returns a list of usernames registered in the system
func (c *Client) GetUsers() ([]services.User, error) {
out, err := c.Get(c.Endpoint("users"), url.Values{})
if err != nil {
return nil, trace.Wrap(err)
}
var items []json.RawMessage
if err := json.Unmarshal(out.Bytes(), &items); err != nil {
return nil, trace.Wrap(err)
}
users := make([]services.User, len(items))
for i, userBytes := range items {
user, err := services.GetUserUnmarshaler()(userBytes)
if err != nil {
return nil, trace.Wrap(err)
}
users[i] = user
}
return users, nil
}
// DeleteUser deletes a user by username
func (c *Client) DeleteUser(user string) error {
_, err := c.Delete(c.Endpoint("users", user))
return trace.Wrap(err)
}
// GenerateKeyPair generates SSH private/public key pair optionally protected
// by password. If the pass parameter is an empty string, the key pair
// is not password-protected.
func (c *Client) GenerateKeyPair(pass string) ([]byte, []byte, error) {
out, err := c.PostJSON(c.Endpoint("keypair"), generateKeyPairReq{Password: pass})
if err != nil {
return nil, nil, trace.Wrap(err)
}
var kp *generateKeyPairResponse
if err := json.Unmarshal(out.Bytes(), &kp); err != nil {
return nil, nil, err
}
return kp.PrivKey, []byte(kp.PubKey), err
}
// GenerateHostCert takes the public key in the Open SSH ``authorized_keys``
// plain text format, signs it using Host Certificate Authority private key and returns the
// resulting certificate.
func (c *Client) GenerateHostCert(
key []byte, hostname, authDomain string, role teleport.Role, ttl time.Duration) ([]byte, error) {
out, err := c.PostJSON(c.Endpoint("ca", "host", "certs"),
generateHostCertReq{
Key: key,
Hostname: hostname,
AuthDomain: authDomain,
Role: role,
TTL: ttl,
})
if err != nil {
return nil, trace.Wrap(err)
}
var cert string
if err := json.Unmarshal(out.Bytes(), &cert); err != nil {
return nil, err
}
return []byte(cert), nil
}
// GenerateUserCert takes the public key in the Open SSH ``authorized_keys``
// plain text format, signs it using User Certificate Authority signing key and returns the
// resulting certificate.
func (c *Client) GenerateUserCert(
key []byte, user string, ttl time.Duration) ([]byte, error) {
out, err := c.PostJSON(c.Endpoint("ca", "user", "certs"),
generateUserCertReq{
Key: key,
User: user,
TTL: ttl,
})
if err != nil {
return nil, trace.Wrap(err)
}
var cert string
if err := json.Unmarshal(out.Bytes(), &cert); err != nil {
return nil, trace.Wrap(err)
}
return []byte(cert), nil
}
// CreateSignupToken creates one time token for creating account for the user
// For each token it creates username and hotp generator
func (c *Client) CreateSignupToken(user services.User) (string, error) {
if err := user.Check(); err != nil {
return "", trace.Wrap(err)
}
out, err := c.PostJSON(c.Endpoint("signuptokens"), createSignupTokenReq{
User: user,
})
if err != nil {
return "", trace.Wrap(err)
}
var token string
if err := json.Unmarshal(out.Bytes(), &token); err != nil {
return "", trace.Wrap(err)
}
return token, nil
}
// GetSignupTokenData returns token data for a valid token
func (c *Client) GetSignupTokenData(token string) (user string,
QRImg []byte, hotpFirstValues []string, e error) {
out, err := c.Get(c.Endpoint("signuptokens", token), url.Values{})
if err != nil {
return "", nil, nil, err
}
var tokenData getSignupTokenDataResponse
if err := json.Unmarshal(out.Bytes(), &tokenData); err != nil {
return "", nil, nil, err
}
return tokenData.User, tokenData.QRImg, tokenData.HotpFirstValues, nil
}
// CreateUserWithToken creates account with provided token and password.
// Account username and hotp generator are taken from token data.
// Deletes token after account creation.
func (c *Client) CreateUserWithToken(token, password, hotpToken string) (*Session, error) {
out, err := c.PostJSON(c.Endpoint("signuptokens", "users"), createUserWithTokenReq{
Token: token,
Password: password,
HOTPToken: hotpToken,
})
if err != nil {
return nil, trace.Wrap(err)
}
var sess *Session
if err := json.Unmarshal(out.Bytes(), &sess); err != nil {
return nil, trace.Wrap(err)
}
return sess, nil
}
func (c *Client) UpsertOIDCConnector(connector services.OIDCConnector, ttl time.Duration) error {
_, err := c.PostJSON(c.Endpoint("oidc", "connectors"), upsertOIDCConnectorReq{
Connector: connector,
TTL: ttl,
})
if err != nil {
return trace.Wrap(err)
}
return nil
}
func (c *Client) GetOIDCConnector(id string, withSecrets bool) (*services.OIDCConnector, error) {
if id == "" {
return nil, trace.BadParameter("missing connector id")
}
out, err := c.Get(c.Endpoint("oidc", "connectors", id),
url.Values{"with_secrets": []string{fmt.Sprintf("%t", withSecrets)}})
if err != nil {
return nil, err
}
var conn *services.OIDCConnector
if err := json.Unmarshal(out.Bytes(), &conn); err != nil {
return nil, trace.Wrap(err)
}
return conn, nil
}
func (c *Client) GetOIDCConnectors(withSecrets bool) ([]services.OIDCConnector, error) {
out, err := c.Get(c.Endpoint("oidc", "connectors"),
url.Values{"with_secrets": []string{fmt.Sprintf("%t", withSecrets)}})
if err != nil {
return nil, err
}
var connectors []services.OIDCConnector
if err := json.Unmarshal(out.Bytes(), &connectors); err != nil {
return nil, trace.Wrap(err)
}
return connectors, nil
}
func (c *Client) DeleteOIDCConnector(connectorID string) error {
if connectorID == "" {
return trace.BadParameter("missing connector id")
}
_, err := c.Delete(c.Endpoint("oidc", "connectors", connectorID))
return trace.Wrap(err)
}
func (c *Client) CreateOIDCAuthRequest(req services.OIDCAuthRequest) (*services.OIDCAuthRequest, error) {
out, err := c.PostJSON(c.Endpoint("oidc", "requests", "create"), createOIDCAuthRequestReq{
Req: req,
})
if err != nil {
return nil, trace.Wrap(err)
}
var response *services.OIDCAuthRequest
if err := json.Unmarshal(out.Bytes(), &response); err != nil {
return nil, trace.Wrap(err)
}
return response, nil
}
func (c *Client) ValidateOIDCAuthCallback(q url.Values) (*OIDCAuthResponse, error) {
out, err := c.PostJSON(c.Endpoint("oidc", "requests", "validate"), validateOIDCAuthCallbackReq{
Query: q,
})
if err != nil {
return nil, trace.Wrap(err)
}
var response *OIDCAuthResponse
if err := json.Unmarshal(out.Bytes(), &response); err != nil {
return nil, trace.Wrap(err)
}
return response, nil
}
type chunkRW struct {
c *Client
id string
}
func (c *chunkRW) ReadChunks(start int, end int) ([]recorder.Chunk, error) {
out, err := c.c.Get(c.c.Endpoint("records", c.id, "chunks"), url.Values{
"start": []string{strconv.Itoa(start)},
"end": []string{strconv.Itoa(end)},
})
if err != nil {
return nil, trace.Wrap(err)
}
var chunks []recorder.Chunk
if err := json.Unmarshal(out.Bytes(), &chunks); err != nil {
return nil, trace.Wrap(err)
}
return chunks, nil
}
func (c *chunkRW) GetChunksCount() (uint64, error) {
out, err := c.c.Get(c.c.Endpoint("records", c.id, "chunkscount"), url.Values{})
if err != nil {
return 0, trace.Wrap(err)
}
var count uint64
if err := json.Unmarshal(out.Bytes(), &count); err != nil {
return 0, trace.Wrap(err)
}
return count, nil
}
func (c *chunkRW) WriteChunks(chunks []recorder.Chunk) error {
_, err := c.c.PostJSON(
c.c.Endpoint("records", c.id, "chunks"), writeChunksReq{Chunks: chunks})
return trace.Wrap(err)
}
func (c *chunkRW) Close() error {
return nil
}
// TOODO(klizhentas) this should be just including appropriate service implementations
type ClientI interface {
GetUser(name string) (services.User, error)
UpsertUser(user services.User) error
GetSessions() ([]session.Session, error)
GetSession(id session.ID) (*session.Session, error)
CreateSession(s session.Session) error
UpdateSession(req session.UpdateRequest) error
UpsertParty(id session.ID, p session.Party, ttl time.Duration) error
UpsertCertAuthority(cert services.CertAuthority, ttl time.Duration) error
GetCertAuthorities(caType services.CertAuthType, loadKeys bool) ([]*services.CertAuthority, error)
DeleteCertAuthority(caType services.CertAuthID) error
GenerateToken(role teleport.Role, ttl time.Duration) (string, error)
RegisterUsingToken(token, hostID string, role teleport.Role) (*PackedKeys, error)
RegisterNewAuthServer(token string) error
Log(id lunk.EventID, e lunk.Event)
LogEntry(en lunk.Entry) error
LogSession(sess session.Session) error
GetEvents(filter events.Filter) ([]lunk.Entry, error)
GetSessionEvents(filter events.Filter) ([]session.Session, error)
GetChunkWriter(id string) (recorder.ChunkWriteCloser, error)
GetChunkReader(id string) (recorder.ChunkReadCloser, error)
UpsertNode(s services.Server, ttl time.Duration) error
GetNodes() ([]services.Server, error)
GetAuthServers() ([]services.Server, error)
UpsertPassword(user string, password []byte) (hotpURL string, hotpQR []byte, err error)
CheckPassword(user string, password []byte, hotpToken string) error
SignIn(user string, password []byte) (*Session, error)
CreateWebSession(user string) (*Session, error)
ExtendWebSession(user string, prevSessionID string) (*Session, error)
GetWebSessionInfo(user string, sid string) (*Session, error)
DeleteWebSession(user string, sid string) error
GetUsers() ([]services.User, error)
DeleteUser(user string) error
GenerateKeyPair(pass string) ([]byte, []byte, error)
GenerateHostCert(key []byte, hostname, authServer string, role teleport.Role, ttl time.Duration) ([]byte, error)
GenerateUserCert(key []byte, user string, ttl time.Duration) ([]byte, error)
GetSignupTokenData(token string) (user string, QRImg []byte, hotpFirstValues []string, e error)
CreateUserWithToken(token, password, hotpToken string) (*Session, error)
UpsertOIDCConnector(connector services.OIDCConnector, ttl time.Duration) error
GetOIDCConnector(id string, withSecrets bool) (*services.OIDCConnector, error)
GetOIDCConnectors(withSecrets bool) ([]services.OIDCConnector, error)
DeleteOIDCConnector(connectorID string) error
CreateOIDCAuthRequest(req services.OIDCAuthRequest) (*services.OIDCAuthRequest, error)
ValidateOIDCAuthCallback(q url.Values) (*OIDCAuthResponse, error)
}