forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiserver.go
781 lines (672 loc) · 23.9 KB
/
apiserver.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
/*
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 (
"fmt"
"net/http"
"strconv"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/backend"
"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"
"github.com/gravitational/teleport/lib/utils"
log "github.com/Sirupsen/logrus"
"github.com/codahale/lunk"
"github.com/gravitational/trace"
"github.com/julienschmidt/httprouter"
)
// Config is APIServer config
type Config struct {
Backend backend.Backend
Addr string
}
// APIServer implements http API server for AuthServer interface
type APIServer struct {
httprouter.Router
a *AuthWithRoles
s *AuthServer
elog events.Log
se session.Service
rec recorder.Recorder
}
// NewAPIServer returns a new instance of APIServer HTTP handler
func NewAPIServer(a *AuthWithRoles) *APIServer {
srv := &APIServer{
a: a,
}
srv.Router = *httprouter.New()
// Operations on certificate authorities
srv.GET("/v1/domain", httplib.MakeHandler(srv.getLocalDomain))
srv.POST("/v1/authorities/:type", httplib.MakeHandler(srv.upsertCertAuthority))
srv.DELETE("/v1/authorities/:type/:domain", httplib.MakeHandler(srv.deleteCertAuthority))
srv.GET("/v1/authorities/:type", httplib.MakeHandler(srv.getCertAuthorities))
// Generating certificates for user and host authorities
srv.POST("/v1/ca/host/certs", httplib.MakeHandler(srv.generateHostCert))
srv.POST("/v1/ca/user/certs", httplib.MakeHandler(srv.generateUserCert))
// Operations on users
srv.GET("/v1/users", httplib.MakeHandler(srv.getUsers))
srv.DELETE("/v1/users/:user", httplib.MakeHandler(srv.deleteUser))
// Generating keypairs
srv.POST("/v1/keypair", httplib.MakeHandler(srv.generateKeyPair))
// Passwords and sessions
srv.POST("/v1/users/:user/web/password", httplib.MakeHandler(srv.upsertPassword))
srv.POST("/v1/users/:user/web/password/check", httplib.MakeHandler(srv.checkPassword))
srv.POST("/v1/users/:user/web/signin", httplib.MakeHandler(srv.signIn))
srv.POST("/v1/users/:user/web/sessions", httplib.MakeHandler(srv.createWebSession))
srv.GET("/v1/users/:user/web/sessions/:sid", httplib.MakeHandler(srv.getWebSession))
srv.GET("/v1/users/:user/web/sessions", httplib.MakeHandler(srv.getWebSessions))
srv.DELETE("/v1/users/:user/web/sessions/:sid", httplib.MakeHandler(srv.deleteWebSession))
srv.GET("/v1/signuptokens/:token", httplib.MakeHandler(srv.getSignupTokenData))
srv.POST("/v1/signuptokens/users", httplib.MakeHandler(srv.createUserWithToken))
srv.POST("/v1/signuptokens", httplib.MakeHandler(srv.createSignupToken))
// Servers and presence heartbeat
srv.POST("/v1/nodes", httplib.MakeHandler(srv.upsertNode))
srv.GET("/v1/nodes", httplib.MakeHandler(srv.getNodes))
srv.POST("/v1/authservers", httplib.MakeHandler(srv.upsertAuthServer))
srv.GET("/v1/authservers", httplib.MakeHandler(srv.getAuthServers))
srv.POST("/v1/proxies", httplib.MakeHandler(srv.upsertProxy))
srv.GET("/v1/proxies", httplib.MakeHandler(srv.getProxies))
// Reverse tunnels
srv.POST("/v1/reversetunnels", httplib.MakeHandler(srv.upsertReverseTunnel))
srv.GET("/v1/reversetunnels", httplib.MakeHandler(srv.getReverseTunnels))
srv.DELETE("/v1/reversetunnels/:domain", httplib.MakeHandler(srv.deleteReverseTunnel))
// Tokens
srv.POST("/v1/tokens", httplib.MakeHandler(srv.generateToken))
srv.POST("/v1/tokens/register", httplib.MakeHandler(srv.registerUsingToken))
srv.POST("/v1/tokens/register/auth", httplib.MakeHandler(srv.registerNewAuthServer))
// Events
srv.POST("/v1/events", httplib.MakeHandler(srv.submitEvents))
srv.GET("/v1/events", httplib.MakeHandler(srv.getEvents))
srv.POST("/v1/events/sessions", httplib.MakeHandler(srv.logSessionEvents))
srv.GET("/v1/events/sessions", httplib.MakeHandler(srv.getSessionEvents))
// Recorded sessions
srv.POST("/v1/records/:sid/chunks", httplib.MakeHandler(srv.submitChunks))
srv.GET("/v1/records/:sid/chunks", httplib.MakeHandler(srv.getChunks))
srv.GET("/v1/records/:sid/chunkscount", httplib.MakeHandler(srv.getChunksCount))
// Sesssions
srv.POST("/v1/sessions/:id/parties", httplib.MakeHandler(srv.upsertSessionParty))
srv.POST("/v1/sessions", httplib.MakeHandler(srv.createSession))
srv.PUT("/v1/sessions/:id", httplib.MakeHandler(srv.updateSession))
srv.GET("/v1/sessions", httplib.MakeHandler(srv.getSessions))
srv.GET("/v1/sessions/:id", httplib.MakeHandler(srv.getSession))
return srv
}
type upsertServerReq struct {
Server services.Server `json:"server"`
TTL time.Duration `json:"ttl"`
}
// upsertNode is called by remote SSH nodes when they ping back into the auth service
func (s *APIServer) upsertServer(role teleport.Role, w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req upsertServerReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
log.Debugf("[AUTH API] ping from %v %v (%v) at %v", role, req.Server.ID, req.Server.Hostname, r.RemoteAddr)
// if server sent "local" IP address to us, replace the ip/host part with the remote address we see
// on the socket, but keep the original port:
req.Server.Addr = utils.ReplaceLocalhost(req.Server.Addr, r.RemoteAddr)
switch role {
case teleport.RoleNode:
if err := s.a.UpsertNode(req.Server, req.TTL); err != nil {
return nil, trace.Wrap(err)
}
case teleport.RoleAuth:
if err := s.a.UpsertAuthServer(req.Server, req.TTL); err != nil {
return nil, trace.Wrap(err)
}
case teleport.RoleProxy:
if err := s.a.UpsertProxy(req.Server, req.TTL); err != nil {
return nil, trace.Wrap(err)
}
}
return message("ok"), nil
}
// upsertNode is called by remote SSH nodes when they ping back into the auth service
func (s *APIServer) upsertNode(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
return s.upsertServer(teleport.RoleNode, w, r, p)
}
// getNodes returns registered SSH nodes
func (s *APIServer) getNodes(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
servers, err := s.a.GetNodes()
if err != nil {
return nil, trace.Wrap(err)
}
return servers, nil
}
// upsertProxy is called by remote SSH nodes when they ping back into the auth service
func (s *APIServer) upsertProxy(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
return s.upsertServer(teleport.RoleProxy, w, r, p)
}
// getProxies returns registered proxies
func (s *APIServer) getProxies(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
servers, err := s.a.GetProxies()
if err != nil {
return nil, trace.Wrap(err)
}
return servers, nil
}
// upsertAuthServer is called by remote Auth servers when they ping back into the auth service
func (s *APIServer) upsertAuthServer(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
return s.upsertServer(teleport.RoleAuth, w, r, p)
}
// getAuthServers returns registered auth servers
func (s *APIServer) getAuthServers(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
servers, err := s.a.GetAuthServers()
if err != nil {
return nil, trace.Wrap(err)
}
return servers, nil
}
type upsertReverseTunnelReq struct {
ReverseTunnel services.ReverseTunnel `json:"reverse_tunnel"`
TTL time.Duration `json:"ttl"`
}
// upsertReverseTunnel is called by admin to create a reverse tunnel to remote proxy
func (s *APIServer) upsertReverseTunnel(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req upsertReverseTunnelReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
if err := s.a.UpsertReverseTunnel(req.ReverseTunnel, req.TTL); err != nil {
return nil, trace.Wrap(err)
}
return message("ok"), nil
}
// getReverseTunnels returns a list of reverse tunnels
func (s *APIServer) getReverseTunnels(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
reverseTunnels, err := s.a.GetReverseTunnels()
if err != nil {
return nil, trace.Wrap(err)
}
return reverseTunnels, nil
}
// deleteReverseTunnel deletes reverse tunnel
func (s *APIServer) deleteReverseTunnel(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
domainName := p[0].Value
err := s.a.DeleteReverseTunnel(domainName)
if err != nil {
return nil, trace.Wrap(err)
}
return message(fmt.Sprintf("reverse tunnel %v deleted", domainName)), nil
}
func (s *APIServer) deleteWebSession(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
user, sid := p[0].Value, p[1].Value
err := s.a.DeleteWebSession(user, sid)
if err != nil {
return nil, trace.Wrap(err)
}
return message(fmt.Sprintf("session '%v' for user '%v' deleted", sid, user)), nil
}
func (s *APIServer) getWebSession(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
user, sid := p[0].Value, p[1].Value
sess, err := s.a.GetWebSessionInfo(user, sid)
if err != nil {
return nil, trace.Wrap(err)
}
return sess, nil
}
func (s *APIServer) getWebSessions(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
user := p[0].Value
keys, err := s.a.GetWebSessionsKeys(user)
if err != nil {
return nil, trace.Wrap(err)
}
return keys, nil
}
type signInReq struct {
Password string `json:"password"`
}
func (s *APIServer) signIn(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req *signInReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
user := p[0].Value
sess, err := s.a.SignIn(user, []byte(req.Password))
if err != nil {
return nil, trace.Wrap(err)
}
return sess, nil
}
type createWebSessionReq struct {
PrevSessionID string `json:"prev_session_id"`
}
func (s *APIServer) createWebSession(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req *createWebSessionReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
user := p[0].Value
sess, err := s.a.CreateWebSession(user, req.PrevSessionID)
if err != nil {
return nil, trace.Wrap(err)
}
return sess, nil
}
type upsertPasswordReq struct {
Password string `json:"password"`
}
type upsertPasswordResponse struct {
HotpURL string `json:"hotp_url"`
HotpQR []byte `json:"hotp_qr"`
}
func (s *APIServer) upsertPassword(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req *upsertPasswordReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
user := p[0].Value
hotpURL, hotpQR, err := s.a.UpsertPassword(user, []byte(req.Password))
if err != nil {
return nil, trace.Wrap(err)
}
return &upsertPasswordResponse{HotpURL: hotpURL, HotpQR: hotpQR}, nil
}
type checkPasswordReq struct {
Password string `json:"password"`
HOTPToken string `json:"hotp_token"`
}
func (s *APIServer) checkPassword(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req checkPasswordReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
user := p[0].Value
if err := s.a.CheckPassword(user, []byte(req.Password), req.HOTPToken); err != nil {
return nil, trace.Wrap(err)
}
return message(fmt.Sprintf("'%v' user password matches", user)), nil
}
func (s *APIServer) getUsers(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
users, err := s.a.GetUsers()
if err != nil {
return nil, trace.Wrap(err)
}
return users, nil
}
func (s *APIServer) deleteUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
user := p[0].Value
if err := s.a.DeleteUser(user); err != nil {
return nil, trace.Wrap(err)
}
return message(fmt.Sprintf("user '%v' deleted", user)), nil
}
type generateKeyPairReq struct {
Password string `json:"password"`
}
type generateKeyPairResponse struct {
PrivKey []byte `json:"privkey"`
PubKey string `json:"pubkey"`
}
func (s *APIServer) generateKeyPair(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
var req *generateKeyPairReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
priv, pub, err := s.a.GenerateKeyPair(req.Password)
if err != nil {
return nil, trace.Wrap(err)
}
return &generateKeyPairResponse{PrivKey: priv, PubKey: string(pub)}, nil
}
type generateHostCertReq struct {
Key []byte `json:"key"`
Hostname string `json:"hostname"`
AuthDomain string `json:"auth_domain"`
Role teleport.Role `json:"role"`
TTL time.Duration `json:"ttl"`
}
func (s *APIServer) generateHostCert(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
var req *generateHostCertReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
cert, err := s.a.GenerateHostCert(req.Key, req.Hostname, req.AuthDomain, req.Role, req.TTL)
if err != nil {
return nil, trace.Wrap(err)
}
return string(cert), nil
}
type generateUserCertReq struct {
Key []byte `json:"key"`
User string `json:"user"`
TTL time.Duration `json:"ttl"`
}
func (s *APIServer) generateUserCert(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
var req *generateUserCertReq
if err := httplib.ReadJSON(r, &req); err != nil {
log.Errorf("failed parsing JSON request. %v", err)
return nil, trace.Wrap(err)
}
cert, err := s.a.GenerateUserCert(req.Key, req.User, req.TTL)
if err != nil {
log.Error(err)
return nil, trace.Wrap(err)
}
return string(cert), nil
}
type generateTokenReq struct {
Role teleport.Role `json:"role"`
TTL time.Duration `json:"ttl"`
}
func (s *APIServer) generateToken(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
var req *generateTokenReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
token, err := s.a.GenerateToken(req.Role, req.TTL)
if err != nil {
return nil, trace.Wrap(err)
}
return string(token), nil
}
type registerUsingTokenReq struct {
HostID string `json:"hostID"`
Role teleport.Role `json:"role"`
Token string `json:"token"`
}
func (s *APIServer) registerUsingToken(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
var req *registerUsingTokenReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
keys, err := s.a.RegisterUsingToken(req.Token, req.HostID, req.Role)
if err != nil {
return nil, trace.Wrap(err)
}
return keys, nil
}
type registerNewAuthServerReq struct {
Token string `json:"token"`
}
func (s *APIServer) registerNewAuthServer(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
var req *registerNewAuthServerReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
err := s.a.RegisterNewAuthServer(req.Token)
if err != nil {
return nil, trace.Wrap(err)
}
return message("ok"), nil
}
type submitEventsReq struct {
Events []lunk.Entry `json:"events"`
}
func (s *APIServer) submitEvents(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
var req *submitEventsReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
for _, e := range req.Events {
if err := s.a.LogEntry(e); err != nil {
return nil, trace.Wrap(err)
}
}
return message("events submitted"), nil
}
type logSessionsReq struct {
Sessions []session.Session `json:"sessions"`
}
func (s *APIServer) logSessionEvents(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
var req *logSessionsReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
for i := range req.Sessions {
if err := s.a.LogSession(req.Sessions[i]); err != nil {
return nil, trace.Wrap(err)
}
}
return message("session events submitted"), nil
}
func (s *APIServer) getEvents(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
f, err := events.FilterFromURL(r.URL.Query())
if err != nil {
return nil, trace.Wrap(err)
}
events, err := s.a.GetEvents(*f)
if err != nil {
return nil, trace.Wrap(err)
}
return events, nil
}
func (s *APIServer) getSessionEvents(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
f, err := events.FilterFromURL(r.URL.Query())
if err != nil {
return nil, trace.Wrap(err)
}
sessionEvents, err := s.a.GetSessionEvents(*f)
if err != nil {
return nil, trace.Wrap(err)
}
return sessionEvents, nil
}
type writeChunksReq struct {
Chunks []recorder.Chunk `json:"chunk"`
}
func (s *APIServer) submitChunks(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
sid := p[0].Value
var req *writeChunksReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
wr, err := s.a.GetChunkWriter(sid)
if err != nil {
return nil, trace.Wrap(err)
}
defer wr.Close()
if err := wr.WriteChunks(req.Chunks); err != nil {
return nil, trace.Wrap(err)
}
return message("chunks submitted"), nil
}
func (s *APIServer) getChunks(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
sid := p[0].Value
st, en := r.URL.Query().Get("start"), r.URL.Query().Get("end")
start, err := strconv.Atoi(st)
if err != nil {
return nil, trace.Wrap(teleport.BadParameter("start", "need integer"))
}
end, err := strconv.Atoi(en)
if err != nil {
return nil, trace.Wrap(teleport.BadParameter("end", "need integer"))
}
re, err := s.a.GetChunkReader(sid)
if err != nil {
return nil, trace.Wrap(err)
}
defer re.Close()
chunks, err := re.ReadChunks(start, end)
if err != nil {
return nil, trace.Wrap(err)
}
return chunks, nil
}
func (s *APIServer) getChunksCount(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
sid := p[0].Value
re, err := s.a.GetChunkReader(sid)
if err != nil {
return nil, trace.Wrap(err)
}
defer re.Close()
count, err := re.GetChunksCount()
if err != nil {
return nil, trace.Wrap(err)
}
return count, nil
}
type upsertCertAuthorityReq struct {
CA services.CertAuthority `json:"ca"`
TTL time.Duration `json:"ttl"`
}
func (s *APIServer) upsertCertAuthority(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req *upsertCertAuthorityReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
if err := s.a.UpsertCertAuthority(req.CA, req.TTL); err != nil {
return nil, trace.Wrap(err)
}
return message("ok"), nil
}
func (s *APIServer) getCertAuthorities(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var loadKeys bool
if loadKeysS := r.URL.Query().Get("load_keys"); loadKeysS != "" {
var err error
loadKeys, err = strconv.ParseBool(loadKeysS)
if err != nil {
return nil, trace.Wrap(teleport.BadParameter("load_keys", fmt.Sprintf("should be 'true' or 'false': %v", loadKeysS)))
}
}
certs, err := s.a.GetCertAuthorities(services.CertAuthType(p[0].Value), loadKeys)
if err != nil {
return nil, trace.Wrap(err)
}
return certs, nil
}
func (s *APIServer) getLocalDomain(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
domain, err := s.a.GetLocalDomain()
if err != nil {
return nil, trace.Wrap(err)
}
return domain, nil
}
func (s *APIServer) deleteCertAuthority(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
id := services.CertAuthID{
DomainName: p[1].Value,
Type: services.CertAuthType(p[0].Value),
}
if err := s.a.DeleteCertAuthority(id); err != nil {
return nil, trace.Wrap(err)
}
return message(fmt.Sprintf("cert '%v' deleted", id)), nil
}
type createSessionReq struct {
Session session.Session `json:"session"`
}
func (s *APIServer) createSession(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req *createSessionReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
if err := s.a.CreateSession(req.Session); err != nil {
return nil, trace.Wrap(err)
}
return message("ok"), nil
}
type updateSessionReq struct {
Update session.UpdateRequest `json:"update"`
}
func (s *APIServer) updateSession(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req *updateSessionReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
if err := s.a.UpdateSession(req.Update); err != nil {
return nil, trace.Wrap(err)
}
return message("ok"), nil
}
type upsertPartyReq struct {
Party session.Party `json:"party"`
TTL time.Duration `json:"ttl"`
}
func (s *APIServer) upsertSessionParty(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req *upsertPartyReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
sid, err := session.ParseID(p[0].Value)
if err != nil {
return nil, trace.Wrap(err)
}
if err := s.a.UpsertParty(*sid, req.Party, req.TTL); err != nil {
return nil, trace.Wrap(err)
}
return req.Party, nil
}
func (s *APIServer) getSessions(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
sessions, err := s.a.GetSessions()
if err != nil {
return nil, trace.Wrap(err)
}
return sessions, nil
}
func (s *APIServer) getSession(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
sid, err := session.ParseID(p[0].Value)
if err != nil {
return nil, trace.Wrap(err)
}
se, err := s.a.GetSession(*sid)
if err != nil {
return nil, trace.Wrap(err)
}
return se, nil
}
type getSignupTokenDataResponse struct {
User string `json:"user"`
QRImg []byte `json:"qrimg"`
HotpFirstValues []string `json:"hotp_first_values"`
}
// getSignupTokenData auth API method creates a new sign-up token for adding a new user
func (s *APIServer) getSignupTokenData(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
token := p[0].Value
user, QRImg, hotpFirstValues, err := s.a.GetSignupTokenData(token)
if err != nil {
return nil, trace.Wrap(err)
}
return &getSignupTokenDataResponse{
User: user,
QRImg: QRImg,
HotpFirstValues: hotpFirstValues,
}, nil
}
type createSignupTokenReq struct {
User string `json:"user"`
AllowedLogins []string `json:"allowed_logins"`
}
func (s *APIServer) createSignupToken(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req *createSignupTokenReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
token, err := s.a.CreateSignupToken(req.User, req.AllowedLogins)
if err != nil {
return nil, trace.Wrap(err)
}
return token, nil
}
type createUserWithTokenReq struct {
Token string `json:"token"`
Password string `json:"password"`
HOTPToken string `json:"hotp_token"`
}
func (s *APIServer) createUserWithToken(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
var req *createUserWithTokenReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
sess, err := s.a.CreateUserWithToken(req.Token, req.Password, req.HOTPToken)
if err != nil {
return nil, trace.Wrap(err)
}
return sess, nil
}
func message(msg string) map[string]interface{} {
return map[string]interface{}{"message": msg}
}