forked from influxdata/chronograf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsources.go
1027 lines (876 loc) · 27.2 KB
/
sources.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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package server
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/influxdata/chronograf/enterprise"
"github.com/influxdata/chronograf/flux"
"github.com/bouk/httprouter"
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/influx"
)
type sourceLinks struct {
Self string `json:"self"` // Self link mapping to this resource
Kapacitors string `json:"kapacitors"` // URL for kapacitors endpoint
Services string `json:"services"` // URL for services endpoint
Proxy string `json:"proxy"` // URL for proxy endpoint
Queries string `json:"queries"` // URL for the queries analysis endpoint
Write string `json:"write"` // URL for the write line-protocol endpoint
Permissions string `json:"permissions"` // URL for all allowed permissions for this source
Users string `json:"users"` // URL for all users associated with this source
Roles string `json:"roles,omitempty"` // URL for all users associated with this source
Databases string `json:"databases"` // URL for the databases contained within this source
Annotations string `json:"annotations"` // URL for the annotations of this source
Health string `json:"health"` // URL for source health
Flux string `json:"flux,omitempty"` // URL for flux if it exists
}
type sourceResponse struct {
chronograf.Source
AuthenticationMethod string `json:"authentication"`
Links sourceLinks `json:"links"`
}
type authenticationResponse struct {
ID int `json:"id,string"`
AuthenticationMethod string
}
func sourceAuthenticationMethod(ctx context.Context, src chronograf.Source) authenticationResponse {
ldapEnabled := false
if src.MetaURL != "" {
authorizer := influx.DefaultAuthorization(&src)
metaURL, err := url.Parse(src.MetaURL)
if err == nil {
client := enterprise.NewMetaClient(metaURL, src.InsecureSkipVerify, authorizer)
config, err := client.GetLDAPConfig(ctx)
if err == nil {
ldapEnabled = config.Structured.Enabled
}
}
}
if ldapEnabled {
return authenticationResponse{ID: src.ID, AuthenticationMethod: "ldap"}
} else if src.Username != "" && src.Password != "" {
return authenticationResponse{ID: src.ID, AuthenticationMethod: "basic"}
} else if src.SharedSecret != "" {
return authenticationResponse{ID: src.ID, AuthenticationMethod: "shared"}
} else {
return authenticationResponse{ID: src.ID, AuthenticationMethod: "unknown"}
}
}
func hasFlux(ctx context.Context, src chronograf.Source) (bool, error) {
// flux is always available in v2 version, but it requires v2 Token authentication (distinguished by Type)
// and a non-empty Organization (stored in Username)
if src.Version == "" /* v2 OSS reports no version */ || strings.HasPrefix(src.Version, "2.") {
return src.Type == chronograf.InfluxDBv2 && src.Username != "", nil
}
url, err := url.ParseRequestURI(src.URL)
if err != nil {
return false, err
}
cli := &flux.Client{
URL: url,
InsecureSkipVerify: src.InsecureSkipVerify,
Timeout: 500 * time.Millisecond,
}
return cli.FluxEnabled()
}
func newSourceResponse(ctx context.Context, src chronograf.Source) sourceResponse {
// If telegraf is not set, we'll set it to the default value.
if src.Telegraf == "" {
src.Telegraf = "telegraf"
}
authMethod := sourceAuthenticationMethod(ctx, src)
// Omit the password and shared secret on response
src.Password = ""
src.SharedSecret = ""
httpAPISrcs := "/chronograf/v1/sources"
res := sourceResponse{
Source: src,
AuthenticationMethod: authMethod.AuthenticationMethod,
Links: sourceLinks{
Self: fmt.Sprintf("%s/%d", httpAPISrcs, src.ID),
Kapacitors: fmt.Sprintf("%s/%d/kapacitors", httpAPISrcs, src.ID),
Services: fmt.Sprintf("%s/%d/services", httpAPISrcs, src.ID),
Proxy: fmt.Sprintf("%s/%d/proxy", httpAPISrcs, src.ID),
Queries: fmt.Sprintf("%s/%d/queries", httpAPISrcs, src.ID),
Write: fmt.Sprintf("%s/%d/write", httpAPISrcs, src.ID),
Permissions: fmt.Sprintf("%s/%d/permissions", httpAPISrcs, src.ID),
Users: fmt.Sprintf("%s/%d/users", httpAPISrcs, src.ID),
Databases: fmt.Sprintf("%s/%d/dbs", httpAPISrcs, src.ID),
Annotations: fmt.Sprintf("%s/%d/annotations", httpAPISrcs, src.ID),
Health: fmt.Sprintf("%s/%d/health", httpAPISrcs, src.ID),
},
}
// we are ignoring the error because the error state means that we'll
// turn off the flux querying in the frontend anyway. Is this English?
// good 'nuf
isFluxEnabled, _ := hasFlux(ctx, src)
if isFluxEnabled {
res.Links.Flux = fmt.Sprintf("%s/%d/proxy/flux", httpAPISrcs, src.ID)
}
// MetaURL is currently a string, but eventually, we'd like to change it
// to a slice. Checking len(src.MetaURL) is functionally equivalent to
// checking if it is equal to the empty string.
if src.Type == chronograf.InfluxEnterprise && len(src.MetaURL) != 0 {
res.Links.Roles = fmt.Sprintf("%s/%d/roles", httpAPISrcs, src.ID)
}
return res
}
// NewSource adds a new valid source to the store
func (s *Service) NewSource(w http.ResponseWriter, r *http.Request) {
var src chronograf.Source
if err := json.NewDecoder(r.Body).Decode(&src); err != nil {
invalidJSON(w, s.Logger)
return
}
ctx := r.Context()
defaultOrg, err := s.Store.Organizations(ctx).DefaultOrganization(ctx)
if err != nil {
unknownErrorWithMessage(w, err, s.Logger)
return
}
if err := ValidSourceRequest(&src, defaultOrg.ID); err != nil {
invalidData(w, err, s.Logger)
return
}
// By default the telegraf database will be telegraf
if src.Telegraf == "" {
src.Telegraf = "telegraf"
}
src.Version = s.sourceVersion(ctx, &src)
dbType, err := s.tsdbType(ctx, &src)
if err != nil {
Error(w, http.StatusBadRequest, "Error contacting source", s.Logger)
return
}
if err := s.validateCredentials(ctx, &src); err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
src.Type = dbType
// persist unless it is a dry-run
if _, dryRun := r.URL.Query()["dryRun"]; !dryRun {
if src, err = s.Store.Sources(ctx).Add(ctx, src); err != nil {
msg := fmt.Errorf("Error storing source %v: %v", src, err)
unknownErrorWithMessage(w, msg, s.Logger)
return
}
}
res := newSourceResponse(ctx, src)
location(w, res.Links.Self)
encodeJSON(w, http.StatusCreated, res, s.Logger)
}
func (s *Service) sourceVersion(ctx context.Context, src *chronograf.Source) string {
retVal, err := s.tsdbVersion(ctx, src)
if err == nil {
return retVal
}
s.Logger.WithField("error", err.Error()).WithField("url", src.URL).Info("Failed to retrieve database version")
if strings.HasPrefix(src.Version, "1.") || strings.HasPrefix(src.Version, "2.") {
// keep the client version unchanged
return src.Version
}
return "Unknown"
}
func (s *Service) tsdbVersion(ctx context.Context, src *chronograf.Source) (string, error) {
cli := &influx.Client{
Logger: s.Logger,
}
if err := cli.Connect(ctx, src); err != nil {
return "", err
}
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
return cli.Version(ctx)
}
func (s *Service) tsdbType(ctx context.Context, src *chronograf.Source) (string, error) {
if src.Type == chronograf.InfluxDBv2 {
return chronograf.InfluxDBv2, nil // v2 selected by the user
}
cli := &influx.Client{
Logger: s.Logger,
}
if err := cli.Connect(ctx, src); err != nil {
return "", err
}
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
return cli.Type(ctx)
}
func (s *Service) validateCredentials(ctx context.Context, src *chronograf.Source) error {
cli := &influx.Client{
Logger: s.Logger,
}
if err := cli.Connect(ctx, src); err != nil {
return err
}
return cli.ValidateAuth(ctx, src)
}
type getSourcesResponse struct {
Sources []sourceResponse `json:"sources"`
}
// Sources returns all sources from the store.
func (s *Service) Sources(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
res := getSourcesResponse{
Sources: make([]sourceResponse, 0),
}
srcs, err := s.Store.Sources(ctx).All(ctx)
if err != nil {
Error(w, http.StatusInternalServerError, "Error loading sources", s.Logger)
return
}
sourceCh := make(chan sourceResponse, len(srcs))
for _, src := range srcs {
go func(src chronograf.Source) {
src.Version = s.sourceVersion(ctx, &src)
sourceCh <- newSourceResponse(ctx, src)
}(src)
}
for i := 0; i < len(srcs); i++ {
res.Sources = append(res.Sources, <-sourceCh)
}
encodeJSON(w, http.StatusOK, res, s.Logger)
}
// SourcesID retrieves a source from the store
func (s *Service) SourcesID(w http.ResponseWriter, r *http.Request) {
id, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
return
}
ctx := r.Context()
src, err := s.Store.Sources(ctx).Get(ctx, id)
if err != nil {
notFound(w, id, s.Logger)
return
}
src.Version = s.sourceVersion(ctx, &src)
res := newSourceResponse(ctx, src)
encodeJSON(w, http.StatusOK, res, s.Logger)
}
// RemoveSource deletes the source from the store
func (s *Service) RemoveSource(w http.ResponseWriter, r *http.Request) {
id, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
return
}
src := chronograf.Source{ID: id}
ctx := r.Context()
if err = s.Store.Sources(ctx).Delete(ctx, src); err != nil {
if err == chronograf.ErrSourceNotFound {
notFound(w, id, s.Logger)
} else {
unknownErrorWithMessage(w, err, s.Logger)
}
return
}
// Remove all the associated kapacitors for this source
if err = s.removeSrcsKapa(ctx, id); err != nil {
unknownErrorWithMessage(w, err, s.Logger)
return
}
w.WriteHeader(http.StatusNoContent)
}
// SourceHealth determines if the tsdb is running
func (s *Service) SourceHealth(w http.ResponseWriter, r *http.Request) {
id, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
return
}
ctx := r.Context()
src, err := s.Store.Sources(ctx).Get(ctx, id)
if err != nil {
notFound(w, id, s.Logger)
return
}
cli := &influx.Client{
Logger: s.Logger,
}
if err := cli.Connect(ctx, &src); err != nil {
Error(w, http.StatusBadRequest, "Error contacting source", s.Logger)
return
}
if err := cli.Ping(ctx); err != nil {
Error(w, http.StatusBadRequest, "Error contacting source", s.Logger)
return
}
w.WriteHeader(http.StatusNoContent)
}
// removeSrcsKapa will remove all kapacitors and kapacitor rules from the stores.
// However, it will not remove the kapacitor tickscript from kapacitor itself.
func (s *Service) removeSrcsKapa(ctx context.Context, srcID int) error {
kapas, err := s.Store.Servers(ctx).All(ctx)
if err != nil {
return err
}
// Filter the kapacitors to delete by matching the source id
deleteKapa := []int{}
for _, kapa := range kapas {
if kapa.SrcID == srcID {
deleteKapa = append(deleteKapa, kapa.ID)
}
}
for _, kapaID := range deleteKapa {
kapa := chronograf.Server{
ID: kapaID,
}
s.Logger.Debug("Deleting kapacitor resource id ", kapa.ID)
if err := s.Store.Servers(ctx).Delete(ctx, kapa); err != nil {
return err
}
}
return nil
}
// UpdateSource handles incremental updates of a data source
func (s *Service) UpdateSource(w http.ResponseWriter, r *http.Request) {
id, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
return
}
ctx := r.Context()
src, err := s.Store.Sources(ctx).Get(ctx, id)
if err != nil {
notFound(w, id, s.Logger)
return
}
var req chronograf.Source
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
invalidJSON(w, s.Logger)
return
}
src.Default = req.Default
src.InsecureSkipVerify = req.InsecureSkipVerify
if req.Name != "" {
src.Name = req.Name
}
if req.Password != "" {
src.Password = req.Password
}
if req.Username != "" {
src.Username = req.Username
}
if req.URL != "" {
src.URL = req.URL
}
// If the supplied MetaURL is different from the
// one supplied on the request, update the value
if req.MetaURL != src.MetaURL {
src.MetaURL = req.MetaURL
}
if req.Type != "" {
src.Type = req.Type
}
if req.Telegraf != "" {
src.Telegraf = req.Telegraf
}
src.DefaultRP = req.DefaultRP
defaultOrg, err := s.Store.Organizations(ctx).DefaultOrganization(ctx)
if err != nil {
unknownErrorWithMessage(w, err, s.Logger)
return
}
if err := ValidSourceRequest(&src, defaultOrg.ID); err != nil {
invalidData(w, err, s.Logger)
return
}
src.Version = s.sourceVersion(ctx, &src)
dbType, err := s.tsdbType(ctx, &src)
if err != nil {
Error(w, http.StatusBadRequest, "Error contacting source", s.Logger)
return
}
src.Type = dbType
if err := s.validateCredentials(ctx, &src); err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
// persist unless it is a dry-run
if _, dryRun := r.URL.Query()["dryRun"]; !dryRun {
if err := s.Store.Sources(ctx).Update(ctx, src); err != nil {
msg := fmt.Sprintf("Error updating source ID %d", id)
Error(w, http.StatusInternalServerError, msg, s.Logger)
return
}
}
encodeJSON(w, http.StatusOK, newSourceResponse(context.Background(), src), s.Logger)
}
// ValidSourceRequest checks if name, url, type, and role are valid
func ValidSourceRequest(s *chronograf.Source, defaultOrgID string) error {
if s == nil {
return fmt.Errorf("source must be non-nil")
}
// Name and URL areq required
if s.URL == "" {
return fmt.Errorf("url required")
}
// Validate Type
if s.Type != "" {
if s.Type != chronograf.InfluxDB &&
s.Type != chronograf.InfluxDBv2 &&
s.Type != chronograf.InfluxEnterprise &&
s.Type != chronograf.InfluxRelay {
return fmt.Errorf("invalid source type %s", s.Type)
}
}
if s.Organization == "" {
s.Organization = defaultOrgID
}
url, err := url.ParseRequestURI(s.URL)
if err != nil {
return fmt.Errorf("invalid source URI: %v", err)
}
if len(url.Scheme) == 0 {
return fmt.Errorf("invalid URL; no URL scheme defined")
}
return nil
}
// NewSourceUser adds user to source
func (s *Service) NewSourceUser(w http.ResponseWriter, r *http.Request) {
var req sourceUserRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
invalidJSON(w, s.Logger)
return
}
if err := req.ValidCreate(); err != nil {
invalidData(w, err, s.Logger)
return
}
ctx := r.Context()
srcID, ts, err := s.sourcesSeries(ctx, w, r)
if err != nil {
return
}
store := ts.Users(ctx)
user := &chronograf.User{
Name: req.Username,
Passwd: req.Password,
Permissions: req.Permissions,
Roles: req.Roles,
}
res, err := store.Add(ctx, user)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
su := newSourceUserResponse(srcID, res.Name).WithPermissions(res.Permissions)
if _, hasRoles := s.hasRoles(ctx, ts); hasRoles {
su.WithRoles(srcID, res.Roles)
}
location(w, su.Links.Self)
encodeJSON(w, http.StatusCreated, su, s.Logger)
}
// SourceUsers retrieves all users from source.
func (s *Service) SourceUsers(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, ts, err := s.sourcesSeries(ctx, w, r)
if err != nil {
return
}
store := ts.Users(ctx)
users, err := store.All(ctx)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
_, hasRoles := s.hasRoles(ctx, ts)
ur := make([]sourceUserResponse, len(users))
for i, u := range users {
usr := newSourceUserResponse(srcID, u.Name).WithPermissions(u.Permissions)
if hasRoles {
usr.WithRoles(srcID, u.Roles)
}
ur[i] = *usr
}
res := sourceUsersResponse{
Users: ur,
}
encodeJSON(w, http.StatusOK, res, s.Logger)
}
// SourceUserID retrieves a user with ID from store.
// In InfluxDB, a User's Name is their UID, hence the semantic below.
func (s *Service) SourceUserID(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
uid := httprouter.GetParamFromContext(ctx, "uid")
srcID, ts, err := s.sourcesSeries(ctx, w, r)
if err != nil {
return
}
store := ts.Users(ctx)
u, err := store.Get(ctx, chronograf.UserQuery{Name: &uid})
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
res := newSourceUserResponse(srcID, u.Name).WithPermissions(u.Permissions)
if _, hasRoles := s.hasRoles(ctx, ts); hasRoles {
res.WithRoles(srcID, u.Roles)
}
encodeJSON(w, http.StatusOK, res, s.Logger)
}
// RemoveSourceUser removes the user from the InfluxDB source
func (s *Service) RemoveSourceUser(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
uid := httprouter.GetParamFromContext(ctx, "uid")
_, store, err := s.sourceUsersStore(ctx, w, r)
if err != nil {
return
}
if err := store.Delete(ctx, &chronograf.User{Name: uid}); err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
w.WriteHeader(http.StatusNoContent)
}
// UpdateSourceUser changes the password or permissions of a source user
func (s *Service) UpdateSourceUser(w http.ResponseWriter, r *http.Request) {
var req sourceUserRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
invalidJSON(w, s.Logger)
return
}
if err := req.ValidUpdate(); err != nil {
invalidData(w, err, s.Logger)
return
}
ctx := r.Context()
uid := httprouter.GetParamFromContext(ctx, "uid")
srcID, ts, err := s.sourcesSeries(ctx, w, r)
if err != nil {
return
}
user := &chronograf.User{
Name: uid,
Passwd: req.Password,
Permissions: req.Permissions,
Roles: req.Roles,
}
store := ts.Users(ctx)
if err := store.Update(ctx, user); err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
u, err := store.Get(ctx, chronograf.UserQuery{Name: &uid})
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
res := newSourceUserResponse(srcID, u.Name).WithPermissions(u.Permissions)
if _, hasRoles := s.hasRoles(ctx, ts); hasRoles {
res.WithRoles(srcID, u.Roles)
}
location(w, res.Links.Self)
encodeJSON(w, http.StatusOK, res, s.Logger)
}
func (s *Service) sourcesSeries(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, chronograf.TimeSeries, error) {
srcID, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
return 0, nil, err
}
src, err := s.Store.Sources(ctx).Get(ctx, srcID)
if err != nil {
notFound(w, srcID, s.Logger)
return 0, nil, err
}
ts, err := s.TimeSeries(src)
if err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, s.Logger)
return 0, nil, err
}
if err = ts.Connect(ctx, &src); err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, s.Logger)
return 0, nil, err
}
return srcID, ts, nil
}
func (s *Service) sourceUsersStore(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, chronograf.UsersStore, error) {
srcID, ts, err := s.sourcesSeries(ctx, w, r)
if err != nil {
return 0, nil, err
}
store := ts.Users(ctx)
return srcID, store, nil
}
// hasRoles checks if the influx source has roles or not
func (s *Service) hasRoles(ctx context.Context, ts chronograf.TimeSeries) (chronograf.RolesStore, bool) {
store, err := ts.Roles(ctx)
if err != nil {
return nil, false
}
return store, true
}
type sourceUserRequest struct {
Username string `json:"name,omitempty"` // Username for new account
Password string `json:"password,omitempty"` // Password for new account
Permissions chronograf.Permissions `json:"permissions,omitempty"` // Optional permissions
Roles []chronograf.Role `json:"roles,omitempty"` // Optional roles
}
func (r *sourceUserRequest) ValidCreate() error {
if r.Username == "" {
return fmt.Errorf("username required")
}
if r.Password == "" {
return fmt.Errorf("password required")
}
return validPermissions(&r.Permissions)
}
type sourceUsersResponse struct {
Users []sourceUserResponse `json:"users"`
}
func (r *sourceUserRequest) ValidUpdate() error {
if r.Password == "" && r.Permissions == nil && r.Roles == nil {
return fmt.Errorf("no fields to update")
}
return validPermissions(&r.Permissions)
}
type sourceUserResponse struct {
Name string // Username for new account
Permissions chronograf.Permissions // Account's permissions
Roles []sourceRoleResponse // Roles if source uses them
Links selfLinks // Links are URI locations related to user
hasPermissions bool
hasRoles bool
}
func (u *sourceUserResponse) MarshalJSON() ([]byte, error) {
res := map[string]interface{}{
"name": u.Name,
"links": u.Links,
}
if u.hasRoles {
res["roles"] = u.Roles
}
if u.hasPermissions {
res["permissions"] = u.Permissions
}
return json.Marshal(res)
}
// newSourceUserResponse creates an HTTP JSON response for a user w/o roles
func newSourceUserResponse(srcID int, name string) *sourceUserResponse {
self := newSelfLinks(srcID, "users", name)
return &sourceUserResponse{
Name: name,
Links: self,
}
}
func (u *sourceUserResponse) WithPermissions(perms chronograf.Permissions) *sourceUserResponse {
u.hasPermissions = true
if perms == nil {
perms = make(chronograf.Permissions, 0)
}
u.Permissions = perms
return u
}
// WithRoles adds roles to the HTTP JSON response for a user
func (u *sourceUserResponse) WithRoles(srcID int, roles []chronograf.Role) *sourceUserResponse {
u.hasRoles = true
rr := make([]sourceRoleResponse, len(roles))
for i, role := range roles {
rr[i] = newSourceRoleResponse(srcID, &role)
}
u.Roles = rr
return u
}
type selfLinks struct {
Self string `json:"self"` // Self link mapping to this resource
}
func newSelfLinks(id int, parent, resource string) selfLinks {
httpAPISrcs := "/chronograf/v1/sources"
u := &url.URL{Path: resource}
encodedResource := u.String()
return selfLinks{
Self: fmt.Sprintf("%s/%d/%s/%s", httpAPISrcs, id, parent, encodedResource),
}
}
// NewSourceRole adds role to source
func (s *Service) NewSourceRole(w http.ResponseWriter, r *http.Request) {
var req sourceRoleRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
invalidJSON(w, s.Logger)
return
}
if err := req.ValidCreate(); err != nil {
invalidData(w, err, s.Logger)
return
}
ctx := r.Context()
srcID, ts, err := s.sourcesSeries(ctx, w, r)
if err != nil {
return
}
roles, ok := s.hasRoles(ctx, ts)
if !ok {
Error(w, http.StatusNotFound, fmt.Sprintf("Source %d does not have role capability", srcID), s.Logger)
return
}
if _, err := roles.Get(ctx, req.Name); err == nil {
Error(w, http.StatusBadRequest, fmt.Sprintf("Source %d already has role %s", srcID, req.Name), s.Logger)
return
}
res, err := roles.Add(ctx, &req.Role)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
rr := newSourceRoleResponse(srcID, res)
location(w, rr.Links.Self)
encodeJSON(w, http.StatusCreated, rr, s.Logger)
}
// UpdateSourceRole changes the permissions or users of a role
func (s *Service) UpdateSourceRole(w http.ResponseWriter, r *http.Request) {
var req sourceRoleRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
invalidJSON(w, s.Logger)
return
}
if err := req.ValidUpdate(); err != nil {
invalidData(w, err, s.Logger)
return
}
ctx := r.Context()
srcID, ts, err := s.sourcesSeries(ctx, w, r)
if err != nil {
return
}
roles, ok := s.hasRoles(ctx, ts)
if !ok {
Error(w, http.StatusNotFound, fmt.Sprintf("Source %d does not have role capability", srcID), s.Logger)
return
}
rid := httprouter.GetParamFromContext(ctx, "rid")
req.Name = rid
if err := roles.Update(ctx, &req.Role); err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
role, err := roles.Get(ctx, req.Name)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
rr := newSourceRoleResponse(srcID, role)
location(w, rr.Links.Self)
encodeJSON(w, http.StatusOK, rr, s.Logger)
}
// SourceRoleID retrieves a role with ID from store.
func (s *Service) SourceRoleID(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, ts, err := s.sourcesSeries(ctx, w, r)
if err != nil {
return
}
roles, ok := s.hasRoles(ctx, ts)
if !ok {
Error(w, http.StatusNotFound, fmt.Sprintf("Source %d does not have role capability", srcID), s.Logger)
return
}
rid := httprouter.GetParamFromContext(ctx, "rid")
role, err := roles.Get(ctx, rid)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
rr := newSourceRoleResponse(srcID, role)
encodeJSON(w, http.StatusOK, rr, s.Logger)
}
// SourceRoles retrieves all roles from the store
func (s *Service) SourceRoles(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, ts, err := s.sourcesSeries(ctx, w, r)
if err != nil {
return
}
store, ok := s.hasRoles(ctx, ts)
if !ok {
Error(w, http.StatusNotFound, fmt.Sprintf("Source %d does not have role capability", srcID), s.Logger)
return
}
roles, err := store.All(ctx)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
rr := make([]sourceRoleResponse, len(roles))
for i, role := range roles {
rr[i] = newSourceRoleResponse(srcID, &role)
}
res := struct {
Roles []sourceRoleResponse `json:"roles"`
}{rr}
encodeJSON(w, http.StatusOK, res, s.Logger)
}
// RemoveSourceRole removes role from data source.
func (s *Service) RemoveSourceRole(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, ts, err := s.sourcesSeries(ctx, w, r)
if err != nil {
return
}
roles, ok := s.hasRoles(ctx, ts)
if !ok {
Error(w, http.StatusNotFound, fmt.Sprintf("Source %d does not have role capability", srcID), s.Logger)
return
}
rid := httprouter.GetParamFromContext(ctx, "rid")
if err := roles.Delete(ctx, &chronograf.Role{Name: rid}); err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return
}
w.WriteHeader(http.StatusNoContent)
}
// sourceRoleRequest is the format used for both creating and updating roles
type sourceRoleRequest struct {
chronograf.Role
}
func (r *sourceRoleRequest) ValidCreate() error {
if r.Name == "" || len(r.Name) > 254 {
return fmt.Errorf("Name is required for a role")
}
for _, user := range r.Users {
if user.Name == "" {
return fmt.Errorf("username required")
}
}
return validPermissions(&r.Permissions)
}
func (r *sourceRoleRequest) ValidUpdate() error {
if len(r.Name) > 254 {
return fmt.Errorf("username too long; must be less than 254 characters")
}
for _, user := range r.Users {
if user.Name == "" {
return fmt.Errorf("username required")
}
}