-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
path_app.go
1029 lines (919 loc) · 34.3 KB
/
path_app.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 appgroup
import (
"fmt"
"strings"
"time"
"github.com/fatih/structs"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/helper/policyutil"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
// appStorageEntry stores all the options that are set on an App
type appStorageEntry struct {
// UUID that uniquely represents this App
SelectorID string `json:"selector_id" structs:"selector_id" mapstructure:"selector_id"`
// UUID that serves as the HMAC key for the hashing the 'secret_id's of the App
HMACKey string `json:"hmac_key" structs:"hmac_key" mapstructure:"hmac_key"`
// Policies that are to be required by the token to access the App
Policies []string `json:"policies" structs:"policies" mapstructure:"policies"`
// Number of times the SecretID generated against the App can be used to perform login
SecretIDNumUses int `json:"secret_id_num_uses" structs:"secret_id_num_uses" mapstructure:"secret_id_num_uses"`
// Duration (less than the backend mount's max TTL) after which a SecretID generated against the App will expire
SecretIDTTL time.Duration `json:"secret_id_ttl" structs:"secret_id_ttl" mapstructure:"secret_id_ttl"`
// Duration before which an issued token must be renewed
TokenTTL time.Duration `json:"token_ttl" structs:"token_ttl" mapstructure:"token_ttl"`
// Duration after which an issued token should not be allowed to be renewed
TokenMaxTTL time.Duration `json:"token_max_ttl" structs:"token_max_ttl" mapstructure:"token_max_ttl"`
// A constraint to require 'secret_id' credential during login
BindSecretID bool `json:"bind_secret_id" structs:"bind_secret_id" mapstructure:"bind_secret_id"`
}
// appPaths creates all the paths that are used to register and manage an App.
//
// Paths returned:
// app/
// app/<app_name>
// app/<app_name>/policies
// app/<app_name>/num-uses
// app/<app_name>/secret-id-ttl
// app/<app_name>/token-ttl
// app/<app_name>/token-max-ttl
// app/<app_name>/bind-secret-id
// app/<app_name>/selector-id
// app/<app_name>/secret-id
// app/<app_name>/custom-secret-id
func appPaths(b *backend) []*framework.Path {
return []*framework.Path{
&framework.Path{
Pattern: "app/?",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.pathAppList,
},
HelpSynopsis: strings.TrimSpace(appHelp["app-list"][0]),
HelpDescription: strings.TrimSpace(appHelp["app-list"][1]),
},
&framework.Path{
Pattern: "app/" + framework.GenericNameRegex("app_name"),
Fields: map[string]*framework.FieldSchema{
"app_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the App.",
},
"bind_secret_id": &framework.FieldSchema{
Type: framework.TypeBool,
Default: true,
Description: "Impose secret_id to be presented during login using this App. Defaults to 'true'.",
},
"policies": &framework.FieldSchema{
Type: framework.TypeString,
Default: "default",
Description: "Comma separated list of policies on the App.",
},
"secret_id_num_uses": &framework.FieldSchema{
Type: framework.TypeInt,
Description: "Number of times the a SecretID can access the App, after which it will expire.",
},
"secret_id_ttl": &framework.FieldSchema{
Type: framework.TypeDurationSecond,
Description: "Duration in seconds after which the issued SecretID should expire.",
},
"token_ttl": &framework.FieldSchema{
Type: framework.TypeDurationSecond,
Description: "Duration in seconds after which the issued token should expire.",
},
"token_max_ttl": &framework.FieldSchema{
Type: framework.TypeDurationSecond,
Description: "Duration in seconds after which the issued token should not be allowed to be renewed.",
},
},
ExistenceCheck: b.pathAppExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.CreateOperation: b.pathAppCreateUpdate,
logical.UpdateOperation: b.pathAppCreateUpdate,
logical.ReadOperation: b.pathAppRead,
logical.DeleteOperation: b.pathAppDelete,
},
HelpSynopsis: strings.TrimSpace(appHelp["app"][0]),
HelpDescription: strings.TrimSpace(appHelp["app"][1]),
},
&framework.Path{
Pattern: "app/" + framework.GenericNameRegex("app_name") + "/policies$",
Fields: map[string]*framework.FieldSchema{
"app_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the App.",
},
"policies": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Comma separated list of policies on the App.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathAppPoliciesUpdate,
logical.ReadOperation: b.pathAppPoliciesRead,
logical.DeleteOperation: b.pathAppPoliciesDelete,
},
HelpSynopsis: strings.TrimSpace(appHelp["app-policies"][0]),
HelpDescription: strings.TrimSpace(appHelp["app-policies"][1]),
},
&framework.Path{
Pattern: "app/" + framework.GenericNameRegex("app_name") + "/bind-secret-id$",
Fields: map[string]*framework.FieldSchema{
"app_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the App.",
},
"bind_secret_id": &framework.FieldSchema{
Type: framework.TypeBool,
Description: "Impose secret_id to be presented during login using this App.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathAppBindSecretIDUpdate,
logical.ReadOperation: b.pathAppBindSecretIDRead,
logical.DeleteOperation: b.pathAppBindSecretIDDelete,
},
HelpSynopsis: strings.TrimSpace(appHelp["app-bind-secret-id"][0]),
HelpDescription: strings.TrimSpace(appHelp["app-bind-secret-id"][1]),
},
&framework.Path{
Pattern: "app/" + framework.GenericNameRegex("app_name") + "/num-uses$",
Fields: map[string]*framework.FieldSchema{
"app_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the App.",
},
"secret_id_num_uses": &framework.FieldSchema{
Type: framework.TypeInt,
Description: "Number of times the a SecretID can access the App, after which it will expire.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathAppSecretIDNumUsesUpdate,
logical.ReadOperation: b.pathAppSecretIDNumUsesRead,
logical.DeleteOperation: b.pathAppSecretIDNumUsesDelete,
},
HelpSynopsis: strings.TrimSpace(appHelp["app-num-uses"][0]),
HelpDescription: strings.TrimSpace(appHelp["app-num-uses"][1]),
},
&framework.Path{
Pattern: "app/" + framework.GenericNameRegex("app_name") + "/secret-id-ttl$",
Fields: map[string]*framework.FieldSchema{
"app_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the App.",
},
"secret_id_ttl": &framework.FieldSchema{
Type: framework.TypeDurationSecond,
Description: "Duration in seconds after which the issued SecretID should expire.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathAppSecretIDTTLUpdate,
logical.ReadOperation: b.pathAppSecretIDTTLRead,
logical.DeleteOperation: b.pathAppSecretIDTTLDelete,
},
HelpSynopsis: strings.TrimSpace(appHelp["app-secret-id-ttl"][0]),
HelpDescription: strings.TrimSpace(appHelp["app-secret-id-ttl"][1]),
},
&framework.Path{
Pattern: "app/" + framework.GenericNameRegex("app_name") + "/token-ttl$",
Fields: map[string]*framework.FieldSchema{
"app_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the App.",
},
"token_ttl": &framework.FieldSchema{
Type: framework.TypeDurationSecond,
Description: "Duration in seconds after which the issued token should expire.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathAppTokenTTLUpdate,
logical.ReadOperation: b.pathAppTokenTTLRead,
logical.DeleteOperation: b.pathAppTokenTTLDelete,
},
HelpSynopsis: strings.TrimSpace(appHelp["app-token-ttl"][0]),
HelpDescription: strings.TrimSpace(appHelp["app-token-ttl"][1]),
},
&framework.Path{
Pattern: "app/" + framework.GenericNameRegex("app_name") + "/token-max-ttl$",
Fields: map[string]*framework.FieldSchema{
"app_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the App.",
},
"token_max_ttl": &framework.FieldSchema{
Type: framework.TypeDurationSecond,
Description: "Duration in seconds after which the issued token should not be allowed to be renewed.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathAppTokenMaxTTLUpdate,
logical.ReadOperation: b.pathAppTokenMaxTTLRead,
logical.DeleteOperation: b.pathAppTokenMaxTTLDelete,
},
HelpSynopsis: strings.TrimSpace(appHelp["app-token-max-ttl"][0]),
HelpDescription: strings.TrimSpace(appHelp["app-token-max-ttl"][1]),
},
&framework.Path{
Pattern: "app/" + framework.GenericNameRegex("app_name") + "/selector-id$",
Fields: map[string]*framework.FieldSchema{
"app_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the App.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathAppSelectorIDRead,
},
HelpSynopsis: strings.TrimSpace(appHelp["app-selector-id"][0]),
HelpDescription: strings.TrimSpace(appHelp["app-selector-id"][1]),
},
&framework.Path{
Pattern: "app/" + framework.GenericNameRegex("app_name") + "/secret-id$",
Fields: map[string]*framework.FieldSchema{
"app_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the App.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathAppSecretIDRead,
logical.ListOperation: b.pathAppSecretIDList,
},
HelpSynopsis: strings.TrimSpace(appHelp["app-secret-id"][0]),
HelpDescription: strings.TrimSpace(appHelp["app-secret-id"][1]),
},
&framework.Path{
Pattern: "app/" + framework.GenericNameRegex("app_name") + "/custom-secret-id$",
Fields: map[string]*framework.FieldSchema{
"app_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the App.",
},
"secret_id": &framework.FieldSchema{
Type: framework.TypeString,
Default: "",
Description: "SecretID to be attached to the App.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathAppCustomSecretIDUpdate,
},
HelpSynopsis: strings.TrimSpace(appHelp["app-custom-secret-id"][0]),
HelpDescription: strings.TrimSpace(appHelp["app-custom-secret-id"][1]),
},
}
}
// pathAppExistenceCheck returns if the app with the given name exists or not.
func (b *backend) pathAppExistenceCheck(req *logical.Request, data *framework.FieldData) (bool, error) {
app, err := b.appEntry(req.Storage, data.Get("app_name").(string))
if err != nil {
return false, err
}
return app != nil, nil
}
// pathAppList is used to list all the Apps registered with the backend.
func (b *backend) pathAppList(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.appLock.RLock()
defer b.appLock.RUnlock()
apps, err := req.Storage.List("app/")
if err != nil {
return nil, err
}
return logical.ListResponse(apps), nil
}
// pathAppSecretIDList is used to list all the Apps registered with the backend.
func (b *backend) pathAppSecretIDList(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
// Get the "custom" lock
lock := b.getSecretIDLock("")
lock.RLock()
defer lock.RUnlock()
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return logical.ErrorResponse(fmt.Sprintf("app %s does not exist", appName)), nil
}
secrets, err := req.Storage.List(fmt.Sprintf("secret_id/%s", b.salt.SaltID(app.SelectorID)))
if err != nil {
return nil, err
}
return logical.ListResponse(secrets), nil
}
// setAppEntry grabs a write lock and stores the options on an App into the storage
func (b *backend) setAppEntry(s logical.Storage, appName string, app *appStorageEntry) error {
b.appLock.Lock()
defer b.appLock.Unlock()
entry, err := logical.StorageEntryJSON("app/"+strings.ToLower(appName), app)
if err != nil {
return err
}
if err = s.Put(entry); err != nil {
return err
}
return b.setSelectorIDEntry(s, app.SelectorID, &selectorIDStorageEntry{
Type: selectorTypeApp,
Name: appName,
})
}
// appEntry grabs the read lock and fetches the options of an App from the storage
func (b *backend) appEntry(s logical.Storage, appName string) (*appStorageEntry, error) {
if appName == "" {
return nil, fmt.Errorf("missing app_name")
}
var result appStorageEntry
b.appLock.RLock()
defer b.appLock.RUnlock()
if entry, err := s.Get("app/" + strings.ToLower(appName)); err != nil {
return nil, err
} else if entry == nil {
return nil, nil
} else if err := entry.DecodeJSON(&result); err != nil {
return nil, err
}
return &result, nil
}
// pathAppCreateUpdate registers a new App with the backend or updates the options
// of an existing App
func (b *backend) pathAppCreateUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
// Fetch or create an entry for the app
app, err := b.appEntry(req.Storage, appName)
if err != nil {
return nil, err
}
// Create a new entry object if this is a CreateOperation
if app == nil {
selectorID, err := uuid.GenerateUUID()
if err != nil {
return nil, fmt.Errorf("failed to create selector_id: %s\n", err)
}
hmacKey, err := uuid.GenerateUUID()
if err != nil {
return nil, fmt.Errorf("failed to create selector_id: %s\n", err)
}
app = &appStorageEntry{
SelectorID: selectorID,
HMACKey: hmacKey,
}
}
if bindSecretIDRaw, ok := data.GetOk("bind_secret_id"); ok {
app.BindSecretID = bindSecretIDRaw.(bool)
} else if req.Operation == logical.CreateOperation {
app.BindSecretID = data.Get("bind_secret_id").(bool)
}
if policiesRaw, ok := data.GetOk("policies"); ok {
app.Policies = policyutil.ParsePolicies(policiesRaw.(string))
} else if req.Operation == logical.CreateOperation {
app.Policies = policyutil.ParsePolicies(data.Get("policies").(string))
}
if numUsesRaw, ok := data.GetOk("secret_id_num_uses"); ok {
app.SecretIDNumUses = numUsesRaw.(int)
} else if req.Operation == logical.CreateOperation {
app.SecretIDNumUses = data.Get("secret_id_num_uses").(int)
}
if app.SecretIDNumUses < 0 {
return logical.ErrorResponse("secret_id_num_uses cannot be negative"), nil
}
if secretIDTTLRaw, ok := data.GetOk("secret_id_ttl"); ok {
app.SecretIDTTL = time.Second * time.Duration(secretIDTTLRaw.(int))
} else if req.Operation == logical.CreateOperation {
app.SecretIDTTL = time.Second * time.Duration(data.Get("secret_id_ttl").(int))
}
if tokenTTLRaw, ok := data.GetOk("token_ttl"); ok {
app.TokenTTL = time.Second * time.Duration(tokenTTLRaw.(int))
} else if req.Operation == logical.CreateOperation {
app.TokenTTL = time.Second * time.Duration(data.Get("token_ttl").(int))
}
if tokenMaxTTLRaw, ok := data.GetOk("token_max_ttl"); ok {
app.TokenMaxTTL = time.Second * time.Duration(tokenMaxTTLRaw.(int))
} else if req.Operation == logical.CreateOperation {
app.TokenMaxTTL = time.Second * time.Duration(data.Get("token_max_ttl").(int))
}
resp := &logical.Response{}
// Check that the TokenMaxTTL value provided is less than the TokenMaxTTL.
// Sanitizing the TTL and MaxTTL is not required now and can be performed
// at credential issue time.
if app.TokenMaxTTL > time.Duration(0) && app.TokenTTL > app.TokenMaxTTL {
return logical.ErrorResponse("token_ttl should not be greater than token_max_ttl"), nil
}
if app.TokenMaxTTL > b.System().MaxLeaseTTL() {
resp.AddWarning("token_max_ttl is greater than the backend mount's maximum TTL value; issued tokens' max TTL value will be truncated")
}
// Store the entry.
return resp, b.setAppEntry(req.Storage, appName, app)
}
// pathAppRead grabs a read lock and reads the options set on the App from the storage
func (b *backend) pathAppRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
if app, err := b.appEntry(req.Storage, strings.ToLower(appName)); err != nil {
return nil, err
} else if app == nil {
return nil, nil
} else {
// Convert the 'time.Duration' values to second.
app.SecretIDTTL = app.SecretIDTTL / time.Second
app.TokenTTL = app.TokenTTL / time.Second
app.TokenMaxTTL = app.TokenMaxTTL / time.Second
// Create a map of data to be returned and remove sensitive information from it
data := structs.New(app).Map()
delete(data, "selector_id")
delete(data, "hmac_key")
return &logical.Response{
Data: data,
}, nil
}
}
// pathAppDelete removes the App from the storage
func (b *backend) pathAppDelete(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
b.appLock.Lock()
defer b.appLock.Unlock()
if err := req.Storage.Delete("app/" + strings.ToLower(appName)); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathAppBindSecretIDUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
if bindSecretIDRaw, ok := data.GetOk("bind_secret_id"); ok {
app.BindSecretID = bindSecretIDRaw.(bool)
return nil, b.setAppEntry(req.Storage, appName, app)
} else {
return logical.ErrorResponse("missing bind_secret_id"), nil
}
}
func (b *backend) pathAppBindSecretIDRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
if app, err := b.appEntry(req.Storage, strings.ToLower(appName)); err != nil {
return nil, err
} else if app == nil {
return nil, nil
} else {
return &logical.Response{
Data: map[string]interface{}{
"bind_secret_id": app.BindSecretID,
},
}, nil
}
}
func (b *backend) pathAppBindSecretIDDelete(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
// Deleting a field means resetting the value in the entry.
app.BindSecretID = (&appStorageEntry{}).BindSecretID
return nil, b.setAppEntry(req.Storage, appName, app)
}
func (b *backend) pathAppPoliciesUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
if policiesRaw, ok := data.GetOk("policies"); ok {
app.Policies = policyutil.ParsePolicies(policiesRaw.(string))
return nil, b.setAppEntry(req.Storage, appName, app)
} else {
return logical.ErrorResponse("missing policies"), nil
}
}
func (b *backend) pathAppPoliciesRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
if app, err := b.appEntry(req.Storage, strings.ToLower(appName)); err != nil {
return nil, err
} else if app == nil {
return nil, nil
} else {
return &logical.Response{
Data: map[string]interface{}{
"policies": app.Policies,
},
}, nil
}
}
func (b *backend) pathAppPoliciesDelete(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
// Deleting a field means resetting the value in the entry.
app.Policies = (&appStorageEntry{}).Policies
return nil, b.setAppEntry(req.Storage, appName, app)
}
func (b *backend) pathAppSecretIDNumUsesUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
if numUsesRaw, ok := data.GetOk("secret_id_num_uses"); ok {
app.SecretIDNumUses = numUsesRaw.(int)
if app.SecretIDNumUses < 0 {
return logical.ErrorResponse("secret_id_num_uses cannot be negative"), nil
}
return nil, b.setAppEntry(req.Storage, appName, app)
} else {
return logical.ErrorResponse("missing secret_id_num_uses"), nil
}
}
func (b *backend) pathAppSelectorIDRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
if app, err := b.appEntry(req.Storage, strings.ToLower(appName)); err != nil {
return nil, err
} else if app == nil {
return nil, nil
} else {
return &logical.Response{
Data: map[string]interface{}{
"selector_id": app.SelectorID,
},
}, nil
}
}
func (b *backend) pathAppSecretIDNumUsesRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
if app, err := b.appEntry(req.Storage, strings.ToLower(appName)); err != nil {
return nil, err
} else if app == nil {
return nil, nil
} else {
return &logical.Response{
Data: map[string]interface{}{
"secret_id_num_uses": app.SecretIDNumUses,
},
}, nil
}
}
func (b *backend) pathAppSecretIDNumUsesDelete(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
// Deleting a field means resetting the value in the entry.
app.SecretIDNumUses = (&appStorageEntry{}).SecretIDNumUses
return nil, b.setAppEntry(req.Storage, appName, app)
}
func (b *backend) pathAppSecretIDTTLUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
if secretIDTTLRaw, ok := data.GetOk("secret_id_ttl"); ok {
app.SecretIDTTL = time.Second * time.Duration(secretIDTTLRaw.(int))
return nil, b.setAppEntry(req.Storage, appName, app)
} else {
return logical.ErrorResponse("missing secret_id_ttl"), nil
}
}
func (b *backend) pathAppSecretIDTTLRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
if app, err := b.appEntry(req.Storage, strings.ToLower(appName)); err != nil {
return nil, err
} else if app == nil {
return nil, nil
} else {
app.SecretIDTTL = app.SecretIDTTL / time.Second
return &logical.Response{
Data: map[string]interface{}{
"secret_id_ttl": app.SecretIDTTL,
},
}, nil
}
}
func (b *backend) pathAppSecretIDTTLDelete(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
// Deleting a field means resetting the value in the entry.
app.SecretIDTTL = (&appStorageEntry{}).SecretIDTTL
return nil, b.setAppEntry(req.Storage, appName, app)
}
func (b *backend) pathAppTokenTTLUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
if tokenTTLRaw, ok := data.GetOk("token_ttl"); ok {
app.TokenTTL = time.Second * time.Duration(tokenTTLRaw.(int))
if app.TokenMaxTTL > time.Duration(0) && app.TokenTTL > app.TokenMaxTTL {
return logical.ErrorResponse("token_ttl should not be greater than token_max_ttl"), nil
}
return nil, b.setAppEntry(req.Storage, appName, app)
} else {
return logical.ErrorResponse("missing token_ttl"), nil
}
}
func (b *backend) pathAppTokenTTLRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
if app, err := b.appEntry(req.Storage, strings.ToLower(appName)); err != nil {
return nil, err
} else if app == nil {
return nil, nil
} else {
app.TokenTTL = app.TokenTTL / time.Second
return &logical.Response{
Data: map[string]interface{}{
"token_ttl": app.TokenTTL,
},
}, nil
}
}
func (b *backend) pathAppTokenTTLDelete(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
// Deleting a field means resetting the value in the entry.
app.TokenTTL = (&appStorageEntry{}).TokenTTL
return nil, b.setAppEntry(req.Storage, appName, app)
}
func (b *backend) pathAppTokenMaxTTLUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
if tokenMaxTTLRaw, ok := data.GetOk("token_max_ttl"); ok {
app.TokenMaxTTL = time.Second * time.Duration(tokenMaxTTLRaw.(int))
if app.TokenMaxTTL > time.Duration(0) && app.TokenTTL > app.TokenMaxTTL {
return logical.ErrorResponse("token_max_ttl should be greater than or equal to token_ttl"), nil
}
return nil, b.setAppEntry(req.Storage, appName, app)
} else {
return logical.ErrorResponse("missing token_max_ttl"), nil
}
}
func (b *backend) pathAppTokenMaxTTLRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
if app, err := b.appEntry(req.Storage, strings.ToLower(appName)); err != nil {
return nil, err
} else if app == nil {
return nil, nil
} else {
app.TokenMaxTTL = app.TokenMaxTTL / time.Second
return &logical.Response{
Data: map[string]interface{}{
"token_max_ttl": app.TokenMaxTTL,
},
}, nil
}
}
func (b *backend) pathAppTokenMaxTTLDelete(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return nil, nil
}
// Deleting a field means resetting the value in the entry.
app.TokenMaxTTL = (&appStorageEntry{}).TokenMaxTTL
return nil, b.setAppEntry(req.Storage, appName, app)
}
func (b *backend) pathAppSecretIDRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
secretID, err := uuid.GenerateUUID()
if err != nil {
return nil, fmt.Errorf("failed to generate SecretID:%s", err)
}
return b.handleAppSecretIDCommon(req, data, secretID)
}
func (b *backend) pathAppCustomSecretIDUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
return b.handleAppSecretIDCommon(req, data, data.Get("secret_id").(string))
}
func (b *backend) handleAppSecretIDCommon(req *logical.Request, data *framework.FieldData, secretID string) (*logical.Response, error) {
appName := data.Get("app_name").(string)
if appName == "" {
return logical.ErrorResponse("missing app_name"), nil
}
if secretID == "" {
return logical.ErrorResponse("missing secret_id"), nil
}
app, err := b.appEntry(req.Storage, strings.ToLower(appName))
if err != nil {
return nil, err
}
if app == nil {
return logical.ErrorResponse(fmt.Sprintf("app %s does not exist", appName)), nil
}
if !app.BindSecretID {
return logical.ErrorResponse("bind_secret_id is not set on the app"), nil
}
if err = b.registerSecretIDEntry(req.Storage, app.SelectorID, secretID, app.HMACKey, &secretIDStorageEntry{
SecretIDNumUses: app.SecretIDNumUses,
SecretIDTTL: app.SecretIDTTL,
}); err != nil {
return nil, fmt.Errorf("failed to store secret ID: %s", err)
}
return &logical.Response{
Data: map[string]interface{}{
"secret_id": secretID,
},
}, nil
}
var appHelp = map[string][2]string{
"app-list": {
"Lists all the Apps registered with the backend.",
"The list will contain the names of the Apps.",
},
"app": {
"Register an App with the backend.",
`An App can represent a service, a machine or anything that can be IDed.
The set of policies on the App defines access to the App, meaning, any
Vault token with a policy set that is a superset of the policies on the
App registered here will have access to the App. If a SecretID is desired
to be generated against only this specific App, it can be done via
'app/<app_name>/secret-id' and 'app/<app_name>/custom-secret-id' endpoints.
The properties of the SecretID created against the App and the properties
of the token issued with the SecretID generated againt the App, can be
configured using the parameters of this endpoint.`,
},
"app-bind-secret-id": {
"Impose secret_id to be presented during login using this App.",
`By setting this to 'true', during login the parameter 'secret_id' becomes a mandatory argument.
The value of 'secret_id' can be retrieved using 'app/<app_name>/secret-id' endpoint.`,
},
"app-policies": {
"Policies of the App.",
`A comma-delimited set of Vault policies that defines access to the App.
All the Vault tokens with policies that encompass the policy set
defined on the App, can access the App.`,
},
"app-num-uses": {
"Use limit of the SecretID generated against the App.",
`If the SecretIDs are generated/assigned against the App using the
'app/<app_name>/secret-id' or 'app/<app_name>/custom-secret-id' endpoints,
then the number of times that SecretID can access the App is defined by
this option.`,
},
"app-secret-id-ttl": {
`Duration in seconds, representing the lifetime of the SecretIDs
that are generated against the App using 'app/<app_name>/secret-id' or
'app/<app_name>/custom-secret-id' endpoints.`,
``,
},
"app-token-ttl": {
`Duration in seconds, the lifetime of the token issued by using the SecretID that
is generated against this App, before which the token needs to be renewed.`,
`If SecretIDs are generated against the App, using 'app/<app_name>/secret-id' or the
'app/<app_name>/custom-secret-id' endpoints, and if those SecretIDs are used
to perform the login operation, then the value of 'token-ttl' defines the
lifetime of the token issued, before which the token needs to be renewed.`,
},
"app-token-max-ttl": {
`Duration in seconds, the maximum lifetime of the tokens issued by using
the SecretIDs that were generated against this App, after which the
tokens are not allowed to be renewed.`,
`If SecretIDs are generated against the App using 'app/<app_name>/secret-id'
or the 'app/<app_name>/custom-secret-id' endpoints, and if those SecretIDs