-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.go
885 lines (712 loc) · 29.7 KB
/
start.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package startcmd
import (
"crypto/subtle"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/spf13/cobra"
"github.com/hyperledger/aries-framework-go/component/storage/leveldb"
"github.com/hyperledger/aries-framework-go/component/storageutil/mem"
"github.com/hyperledger/aries-framework-go/pkg/common/log"
"github.com/hyperledger/aries-framework-go/pkg/controller"
"github.com/hyperledger/aries-framework-go/pkg/controller/command"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/messaging/msghandler"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport"
arieshttp "github.com/hyperledger/aries-framework-go/pkg/didcomm/transport/http"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport/ws"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries/defaults"
"github.com/hyperledger/aries-framework-go/pkg/framework/context"
"github.com/hyperledger/aries-framework-go/pkg/kms"
"github.com/hyperledger/aries-framework-go/pkg/vdr/httpbinding"
"github.com/hyperledger/aries-framework-go/spi/storage"
)
const (
// api host flag.
agentHostFlagName = "api-host"
agentHostEnvKey = "ARIESD_API_HOST"
agentHostFlagShorthand = "a"
agentHostFlagUsage = "Host Name:Port." +
" Alternatively, this can be set with the following environment variable: " + agentHostEnvKey
// api token flag.
agentTokenFlagName = "api-token"
agentTokenEnvKey = "ARIESD_API_TOKEN" // nolint:gosec
agentTokenFlagShorthand = "t"
agentTokenFlagUsage = "Check for bearer token in the authorization header (optional)." +
" Alternatively, this can be set with the following environment variable: " + agentTokenEnvKey
databaseTypeFlagName = "database-type"
databaseTypeEnvKey = "ARIESD_DATABASE_TYPE"
databaseTypeFlagShorthand = "q"
databaseTypeFlagUsage = "The type of database to use for everything except key storage. " +
"Supported options: mem, leveldb. " +
" Alternatively, this can be set with the following environment variable: " + databaseTypeEnvKey
databasePrefixFlagName = "database-prefix"
databasePrefixEnvKey = "ARIESD_DATABASE_PREFIX"
databasePrefixFlagShorthand = "u"
databasePrefixFlagUsage = "An optional prefix to be used when creating and retrieving underlying databases. " +
" Alternatively, this can be set with the following environment variable: " + databasePrefixEnvKey
databaseTimeoutFlagName = "database-timeout"
databaseTimeoutFlagUsage = "Total time in seconds to wait until the db is available before giving up." +
" Default: " + databaseTimeoutDefault + " seconds." +
" Alternatively, this can be set with the following environment variable: " + databaseTimeoutEnvKey
databaseTimeoutEnvKey = "ARIESD_DATABASE_TIMEOUT"
databaseTimeoutDefault = "30"
// webhook url flag.
agentWebhookFlagName = "webhook-url"
agentWebhookEnvKey = "ARIESD_WEBHOOK_URL"
agentWebhookFlagShorthand = "w"
agentWebhookFlagUsage = "URL to send notifications to." +
" This flag can be repeated, allowing for multiple listeners." +
" Alternatively, this can be set with the following environment variable (in CSV format): " + agentWebhookEnvKey
// default label flag.
agentDefaultLabelFlagName = "agent-default-label"
agentDefaultLabelEnvKey = "ARIESD_DEFAULT_LABEL"
agentDefaultLabelFlagShorthand = "l"
agentDefaultLabelFlagUsage = "Default Label for this agent. Defaults to blank if not set." +
" Alternatively, this can be set with the following environment variable: " + agentDefaultLabelEnvKey
// log level.
agentLogLevelFlagName = "log-level"
agentLogLevelEnvKey = "ARIESD_LOG_LEVEL"
agentLogLevelFlagUsage = "Log level." +
" Possible values [INFO] [DEBUG] [ERROR] [WARNING] [CRITICAL] . Defaults to INFO if not set." +
" Alternatively, this can be set with the following environment variable: " + agentLogLevelEnvKey
// http resolver url flag.
agentHTTPResolverFlagName = "http-resolver-url"
agentHTTPResolverEnvKey = "ARIESD_HTTP_RESOLVER"
agentHTTPResolverFlagShorthand = "r"
agentHTTPResolverFlagUsage = "HTTP binding DID resolver method and url. Values should be in `method@url` format." +
" This flag can be repeated, allowing multiple http resolvers. Defaults to peer DID resolver if not set." +
" Alternatively, this can be set with the following environment variable (in CSV format): " +
agentHTTPResolverEnvKey
// outbound transport flag.
agentOutboundTransportFlagName = "outbound-transport"
agentOutboundTransportEnvKey = "ARIESD_OUTBOUND_TRANSPORT"
agentOutboundTransportFlagShorthand = "o"
agentOutboundTransportFlagUsage = "Outbound transport type." +
" This flag can be repeated, allowing for multiple transports." +
" Possible values [http] [ws]. Defaults to http if not set." +
" Alternatively, this can be set with the following environment variable: " + agentOutboundTransportEnvKey
agentTLSCertFileFlagName = "tls-cert-file"
agentTLSCertFileEnvKey = "TLS_CERT_FILE"
agentTLSCertFileFlagShorthand = "c"
agentTLSCertFileFlagUsage = "tls certificate file." +
" Alternatively, this can be set with the following environment variable: " + agentTLSCertFileEnvKey
agentTLSKeyFileFlagName = "tls-key-file"
agentTLSKeyFileEnvKey = "TLS_KEY_FILE"
agentTLSKeyFileFlagShorthand = "k"
agentTLSKeyFileFlagUsage = "tls key file." +
" Alternatively, this can be set with the following environment variable: " + agentTLSKeyFileEnvKey
// inbound host url flag.
agentInboundHostFlagName = "inbound-host"
agentInboundHostEnvKey = "ARIESD_INBOUND_HOST"
agentInboundHostFlagShorthand = "i"
agentInboundHostFlagUsage = "Inbound Host Name:Port. This is used internally to start the inbound server." +
" Values should be in `scheme@url` format." +
" This flag can be repeated, allowing to configure multiple inbound transports." +
" Alternatively, this can be set with the following environment variable: " + agentInboundHostEnvKey
// inbound host external url flag.
agentInboundHostExternalFlagName = "inbound-host-external"
agentInboundHostExternalEnvKey = "ARIESD_INBOUND_HOST_EXTERNAL"
agentInboundHostExternalFlagShorthand = "e"
agentInboundHostExternalFlagUsage = "Inbound Host External Name:Port and values should be in `scheme@url` format" +
" This is the URL for the inbound server as seen externally." +
" If not provided, then the internal inbound host will be used here." +
" This flag can be repeated, allowing to configure multiple inbound transports." +
" Alternatively, this can be set with the following environment variable: " + agentInboundHostExternalEnvKey
// auto accept flag.
agentAutoAcceptFlagName = "auto-accept"
agentAutoAcceptEnvKey = "ARIESD_AUTO_ACCEPT"
agentAutoAcceptFlagUsage = "Auto accept requests." +
" Possible values [true] [false]. Defaults to false if not set." +
" Alternatively, this can be set with the following environment variable: " + agentAutoAcceptEnvKey
// transport return route option flag.
agentTransportReturnRouteFlagName = "transport-return-route"
agentTransportReturnRouteEnvKey = "ARIESD_TRANSPORT_RETURN_ROUTE"
agentTransportReturnRouteFlagUsage = "Transport Return Route option." +
" Refer https://github.com/hyperledger/aries-framework-go/blob/8449c727c7c44f47ed7c9f10f35f0cd051dcb4e9/pkg/framework/aries/framework.go#L165-L168." + // nolint: lll
" Alternatively, this can be set with the following environment variable: " + agentTransportReturnRouteEnvKey
agentAutoExecuteRFC0593FlagName = "rfc0593-auto-execute"
agentAutoExecuteRFC0593EnvKey = "ARIESD_RFC0593_AUTO_EXECUTE"
agentAutoExecuteRFC0593FlagUsage = "Enables automatic execution of the issue-credential protocol with" +
"RFC0593-compliant attachment formats. Default is false." +
" Alternatively, this can be set with the following environment variable: " + agentAutoExecuteRFC0593EnvKey
// remote JSON-LD context provider url flag.
agentContextProviderFlagName = "context-provider-url"
agentContextProviderEnvKey = "ARIESD_CONTEXT_PROVIDER_URL"
agentContextProviderFlagUsage = "Remote context provider URL to get JSON-LD contexts from." +
" This flag can be repeated, allowing setting up multiple context providers." +
" Alternatively, this can be set with the following environment variable (in CSV format): " +
agentContextProviderEnvKey
// default verification key type flag.
agentKeyTypeFlagName = "key-type"
agentKeyTypeEnvKey = "ARIESD_KEY_TYPE"
agentKeyTypeUsage = "Default key type supported by this agent." +
" This flag sets the verification (and for DIDComm V1 encryption as well) key type used for key creation in the agent." + //nolint:lll
" Alternatively, this can be set with the following environment variable: " +
agentKeyTypeEnvKey
// default key agreement type flag.
agentKeyAgreementTypeFlagName = "key-agreement-type"
agentKeyAgreementTypeEnvKey = "ARIESD_KEY_AGREEMENT_TYPE"
agentKeyAgreementTypeUsage = "Default key agreement type supported by this agent." +
" Default encryption (used in DIDComm V2) key type used for key agreement creation in the agent." +
" Alternatively, this can be set with the following environment variable: " +
agentKeyAgreementTypeEnvKey
// media type profiles flag.
agentMediaTypeProfilesFlagName = "media-type-profiles"
agentMediaTypeProfilesEnvKey = "ARIESD_MEDIA_TYPE_PROFILES"
agentMediaTypeProfilesUsage = "Media Type Profiles supported by this agent." +
" This flag can be repeated, allowing setting up multiple profiles." +
" Alternatively, this can be set with the following environment variable (in CSV format): " +
agentMediaTypeProfilesEnvKey
httpProtocol = "http"
websocketProtocol = "ws"
databaseTypeMemOption = "mem"
databaseTypeLevelDBOption = "leveldb"
)
var (
errMissingHost = errors.New("host not provided")
logger = log.New("aries-framework/agent-rest")
//nolint:gochecknoglobals
keyTypes = map[string]kms.KeyType{
"ed25519": kms.ED25519Type,
"ecdsap256ieee1363": kms.ECDSAP256TypeIEEEP1363,
"ecdsap256der": kms.ECDSAP256TypeDER,
"ecdsap384ieee1363": kms.ECDSAP384TypeIEEEP1363,
"ecdsap384der": kms.ECDSAP384TypeDER,
"ecdsap521ieee1363": kms.ECDSAP521TypeIEEEP1363,
"ecdsap521der": kms.ECDSAP521TypeDER,
}
//nolint:gochecknoglobals
keyAgreementTypes = map[string]kms.KeyType{
"x25519kw": kms.X25519ECDHKWType,
"p256kw": kms.NISTP256ECDHKWType,
"p384kw": kms.NISTP384ECDHKWType,
"p521kw": kms.NISTP521ECDHKWType,
}
)
type agentParameters struct {
server server
host, defaultLabel, transportReturnRoute string
tlsCertFile, tlsKeyFile string
token, keyType, keyAgreementType string
webhookURLs, httpResolvers, outboundTransports []string
inboundHostInternals, inboundHostExternals []string
contextProviderURLs, mediaTypeProfiles []string
autoAccept bool
msgHandler command.MessageHandler
dbParam *dbParam
autoExecuteRFC0593 bool
}
type dbParam struct {
dbType string
prefix string
timeout uint64
}
// nolint:gochecknoglobals
var supportedStorageProviders = map[string]func(prefix string) (storage.Provider, error){
databaseTypeMemOption: func(_ string) (storage.Provider, error) { // nolint:unparam
return mem.NewProvider(), nil
},
databaseTypeLevelDBOption: func(path string) (storage.Provider, error) { // nolint:unparam
return leveldb.NewProvider(path), nil
},
}
type server interface {
ListenAndServe(host string, router http.Handler, certFile, keyFile string) error
}
// HTTPServer represents an actual server implementation.
type HTTPServer struct{}
// ListenAndServe starts the server using the standard Go HTTP server implementation.
func (s *HTTPServer) ListenAndServe(host string, router http.Handler, certFile, keyFile string) error {
if certFile != "" && keyFile != "" {
return http.ListenAndServeTLS(host, certFile, keyFile, router)
}
return http.ListenAndServe(host, router)
}
// Cmd returns the Cobra start command.
func Cmd(server server) (*cobra.Command, error) {
startCmd := createStartCMD(server)
createFlags(startCmd)
return startCmd, nil
}
func createStartCMD(server server) *cobra.Command { //nolint: funlen,gocyclo,gocognit
return &cobra.Command{
Use: "start",
Short: "Start an agent",
Long: `Start an Aries agent controller`,
RunE: func(cmd *cobra.Command, args []string) error {
// log level
logLevel, err := getUserSetVar(cmd, agentLogLevelFlagName, agentLogLevelEnvKey, true)
if err != nil {
return err
}
err = setLogLevel(logLevel)
if err != nil {
return err
}
host, err := getUserSetVar(cmd, agentHostFlagName, agentHostEnvKey, false)
if err != nil {
return err
}
token, err := getUserSetVar(cmd, agentTokenFlagName, agentTokenEnvKey, true)
if err != nil {
return err
}
inboundHosts, err := getUserSetVars(cmd, agentInboundHostFlagName, agentInboundHostEnvKey, true)
if err != nil {
return err
}
inboundHostExternals, err := getUserSetVars(cmd, agentInboundHostExternalFlagName,
agentInboundHostExternalEnvKey, true)
if err != nil {
return err
}
dbParam, err := getDBParam(cmd)
if err != nil {
return err
}
defaultLabel, err := getUserSetVar(cmd, agentDefaultLabelFlagName, agentDefaultLabelEnvKey, true)
if err != nil {
return err
}
autoAccept, err := getAutoAcceptValue(cmd)
if err != nil {
return err
}
webhookURLs, err := getUserSetVars(cmd, agentWebhookFlagName, agentWebhookEnvKey, autoAccept)
if err != nil {
return err
}
httpResolvers, err := getUserSetVars(cmd, agentHTTPResolverFlagName, agentHTTPResolverEnvKey, true)
if err != nil {
return err
}
outboundTransports, err := getUserSetVars(cmd, agentOutboundTransportFlagName,
agentOutboundTransportEnvKey, true)
if err != nil {
return err
}
transportReturnRoute, err := getUserSetVar(cmd, agentTransportReturnRouteFlagName,
agentTransportReturnRouteEnvKey, true)
if err != nil {
return err
}
contextProviderURLs, err := getUserSetVars(cmd, agentContextProviderFlagName, agentContextProviderEnvKey, true)
if err != nil {
return err
}
autoExecuteRFC0593, err := getAutoExecuteRFC0593(cmd)
if err != nil {
return err
}
tlsCertFile, err := getUserSetVar(cmd, agentTLSCertFileFlagName, agentTLSCertFileEnvKey, true)
if err != nil {
return err
}
tlsKeyFile, err := getUserSetVar(cmd, agentTLSKeyFileFlagName, agentTLSKeyFileEnvKey, true)
if err != nil {
return err
}
keyType, err := getUserSetVar(cmd, agentKeyTypeFlagName, agentKeyTypeEnvKey, true)
if err != nil {
return err
}
keyAgreementType, err := getUserSetVar(cmd, agentKeyAgreementTypeFlagName, agentKeyAgreementTypeEnvKey, true)
if err != nil {
return err
}
mediaTypeProfiles, err := getUserSetVars(cmd, agentMediaTypeProfilesFlagName, agentMediaTypeProfilesEnvKey, true)
if err != nil {
return err
}
parameters := &agentParameters{
server: server,
host: host,
token: token,
inboundHostInternals: inboundHosts,
inboundHostExternals: inboundHostExternals,
dbParam: dbParam,
defaultLabel: defaultLabel,
webhookURLs: webhookURLs,
httpResolvers: httpResolvers,
outboundTransports: outboundTransports,
autoAccept: autoAccept,
transportReturnRoute: transportReturnRoute,
contextProviderURLs: contextProviderURLs,
tlsCertFile: tlsCertFile,
tlsKeyFile: tlsKeyFile,
autoExecuteRFC0593: autoExecuteRFC0593,
keyType: keyType,
keyAgreementType: keyAgreementType,
mediaTypeProfiles: mediaTypeProfiles,
}
return startAgent(parameters)
},
}
}
func getDBParam(cmd *cobra.Command) (*dbParam, error) {
dbParam := &dbParam{}
var err error
dbParam.dbType, err = getUserSetVar(cmd, databaseTypeFlagName, databaseTypeEnvKey, false)
if err != nil {
return nil, err
}
dbParam.prefix, err = getUserSetVar(cmd, databasePrefixFlagName, databasePrefixEnvKey, true)
if err != nil {
return nil, err
}
dbTimeout, err := getUserSetVar(cmd, databaseTimeoutFlagName, databaseTimeoutEnvKey, true)
if err != nil {
return nil, err
}
if dbTimeout == "" || dbTimeout == "0" {
dbTimeout = databaseTimeoutDefault
}
t, err := strconv.Atoi(dbTimeout)
if err != nil {
return nil, fmt.Errorf("failed to parse db timeout %s: %w", dbTimeout, err)
}
dbParam.timeout = uint64(t)
return dbParam, nil
}
func getAutoAcceptValue(cmd *cobra.Command) (bool, error) {
v, err := getUserSetVar(cmd, agentAutoAcceptFlagName, agentAutoAcceptEnvKey, true)
if err != nil {
return false, err
}
if v == "" {
return false, nil
}
return strconv.ParseBool(v)
}
func getAutoExecuteRFC0593(cmd *cobra.Command) (bool, error) {
autoExecuteRFC0593Str, err := getUserSetVar(cmd, agentAutoExecuteRFC0593FlagName,
agentAutoExecuteRFC0593EnvKey, true)
if err != nil {
return false, err
}
if autoExecuteRFC0593Str == "" {
return false, nil
}
return strconv.ParseBool(autoExecuteRFC0593Str)
}
//nolint:funlen
func createFlags(startCmd *cobra.Command) {
// agent host flag
startCmd.Flags().StringP(agentHostFlagName, agentHostFlagShorthand, "", agentHostFlagUsage)
// agent token flag
startCmd.Flags().StringP(agentTokenFlagName, agentTokenFlagShorthand, "", agentTokenFlagUsage)
// inbound host flag
startCmd.Flags().StringSliceP(agentInboundHostFlagName, agentInboundHostFlagShorthand, []string{},
agentInboundHostFlagUsage)
// inbound external host flag
startCmd.Flags().StringSliceP(agentInboundHostExternalFlagName, agentInboundHostExternalFlagShorthand,
[]string{}, agentInboundHostExternalFlagUsage)
// db type
startCmd.Flags().StringP(databaseTypeFlagName, databaseTypeFlagShorthand, "", databaseTypeFlagUsage)
// db prefix
startCmd.Flags().StringP(databasePrefixFlagName, databasePrefixFlagShorthand, "", databasePrefixFlagUsage)
// webhook url flag
startCmd.Flags().StringSliceP(agentWebhookFlagName, agentWebhookFlagShorthand, []string{}, agentWebhookFlagUsage)
// log level
startCmd.Flags().StringP(agentLogLevelFlagName, "", "", agentLogLevelFlagUsage)
// http resolver url flag
startCmd.Flags().StringSliceP(agentHTTPResolverFlagName, agentHTTPResolverFlagShorthand, []string{},
agentHTTPResolverFlagUsage)
// agent default label flag
startCmd.Flags().StringP(agentDefaultLabelFlagName, agentDefaultLabelFlagShorthand, "",
agentDefaultLabelFlagUsage)
// agent outbound transport flag
startCmd.Flags().StringSliceP(agentOutboundTransportFlagName, agentOutboundTransportFlagShorthand, []string{},
agentOutboundTransportFlagUsage)
// auto accept flag
startCmd.Flags().StringP(agentAutoAcceptFlagName, "", "", agentAutoAcceptFlagUsage)
// transport return route option flag
startCmd.Flags().StringP(agentTransportReturnRouteFlagName, "", "", agentTransportReturnRouteFlagUsage)
// remote JSON-LD context provider url flag
startCmd.Flags().StringSliceP(agentContextProviderFlagName, "", []string{}, agentContextProviderFlagUsage)
startCmd.Flags().StringP(agentAutoExecuteRFC0593FlagName, "", "", agentAutoExecuteRFC0593FlagUsage)
// tls cert file
startCmd.Flags().StringP(agentTLSCertFileFlagName,
agentTLSCertFileFlagShorthand, "", agentTLSCertFileFlagUsage)
// tls key file
startCmd.Flags().StringP(agentTLSKeyFileFlagName,
agentTLSKeyFileFlagShorthand, "", agentTLSKeyFileFlagUsage)
// db timeout
startCmd.Flags().StringP(databaseTimeoutFlagName, "", "", databaseTimeoutFlagUsage)
startCmd.Flags().StringP(agentKeyTypeFlagName, "", "", agentKeyTypeUsage)
startCmd.Flags().StringP(agentKeyAgreementTypeFlagName, "", "", agentKeyAgreementTypeUsage)
startCmd.Flags().StringSliceP(agentMediaTypeProfilesFlagName, "", []string{}, agentMediaTypeProfilesUsage)
}
func getUserSetVar(cmd *cobra.Command, flagName, envKey string, isOptional bool) (string, error) {
if cmd.Flags().Changed(flagName) {
value, err := cmd.Flags().GetString(flagName)
if err != nil {
return "", fmt.Errorf(flagName+" flag not found: %s", err)
}
return value, nil
}
value, isSet := os.LookupEnv(envKey)
if isOptional || isSet {
return value, nil
}
return "", errors.New("Neither " + flagName + " (command line flag) nor " + envKey +
" (environment variable) have been set.")
}
func getUserSetVars(cmd *cobra.Command, flagName, envKey string, isOptional bool) ([]string, error) {
if cmd.Flags().Changed(flagName) {
value, err := cmd.Flags().GetStringSlice(flagName)
if err != nil {
return nil, fmt.Errorf(flagName+" flag not found: %s", err)
}
return value, nil
}
value, isSet := os.LookupEnv(envKey)
var values []string
if isSet {
values = strings.Split(value, ",")
}
if isOptional || isSet {
return values, nil
}
return nil, fmt.Errorf(" %s not set. "+
"It must be set via either command line or environment variable", flagName)
}
func getResolverOpts(httpResolvers []string) ([]aries.Option, error) {
var opts []aries.Option
const numPartsResolverOption = 2
if len(httpResolvers) > 0 {
for _, httpResolver := range httpResolvers {
r := strings.Split(httpResolver, "@")
if len(r) != numPartsResolverOption {
return nil, fmt.Errorf("invalid http resolver options found")
}
httpVDR, err := httpbinding.New(r[1],
httpbinding.WithAccept(func(method string) bool { return method == r[0] }))
if err != nil {
return nil, fmt.Errorf("failed to setup http resolver : %w", err)
}
opts = append(opts, aries.WithVDR(httpVDR))
}
}
return opts, nil
}
func getOutboundTransportOpts(outboundTransports []string) ([]aries.Option, error) {
var opts []aries.Option
var transports []transport.OutboundTransport
for _, outboundTransport := range outboundTransports {
switch outboundTransport {
case httpProtocol:
outbound, err := arieshttp.NewOutbound(arieshttp.WithOutboundHTTPClient(&http.Client{}))
if err != nil {
return nil, fmt.Errorf("http outbound transport initialization failed: %w", err)
}
transports = append(transports, outbound)
case websocketProtocol:
transports = append(transports, ws.NewOutbound())
default:
return nil, fmt.Errorf("outbound transport [%s] not supported", outboundTransport)
}
}
if len(transports) > 0 {
opts = append(opts, aries.WithOutboundTransports(transports...))
}
return opts, nil
}
func getInboundTransportOpts(inboundHostInternals, inboundHostExternals []string, certFile,
keyFile string) ([]aries.Option, error) {
internalHost, err := getInboundSchemeToURLMap(inboundHostInternals)
if err != nil {
return nil, fmt.Errorf("inbound internal host : %w", err)
}
externalHost, err := getInboundSchemeToURLMap(inboundHostExternals)
if err != nil {
return nil, fmt.Errorf("inbound external host : %w", err)
}
var opts []aries.Option
for scheme, host := range internalHost {
switch scheme {
case httpProtocol:
opts = append(opts, defaults.WithInboundHTTPAddr(host, externalHost[scheme], certFile, keyFile))
case websocketProtocol:
opts = append(opts, defaults.WithInboundWSAddr(host, externalHost[scheme], certFile, keyFile))
default:
return nil, fmt.Errorf("inbound transport [%s] not supported", scheme)
}
}
return opts, nil
}
func getInboundSchemeToURLMap(schemeHostStr []string) (map[string]string, error) {
const validSliceLen = 2
schemeHostMap := make(map[string]string)
for _, schemeHost := range schemeHostStr {
schemeHostSlice := strings.Split(schemeHost, "@")
if len(schemeHostSlice) != validSliceLen {
return nil, fmt.Errorf("invalid inbound host option: Use scheme@url to pass the option")
}
schemeHostMap[schemeHostSlice[0]] = schemeHostSlice[1]
}
return schemeHostMap, nil
}
func setLogLevel(logLevel string) error {
if logLevel != "" {
level, err := log.ParseLevel(logLevel)
if err != nil {
return fmt.Errorf("failed to parse log level '%s' : %w", logLevel, err)
}
log.SetLevel("", level)
logger.Infof("logger level set to %s", logLevel)
}
return nil
}
func validateAuthorizationBearerToken(w http.ResponseWriter, r *http.Request, token string) bool {
actHdr := r.Header.Get("Authorization")
expHdr := "Bearer " + token
if subtle.ConstantTimeCompare([]byte(actHdr), []byte(expHdr)) != 1 {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorised.\n")) // nolint:gosec,errcheck
return false
}
return true
}
func authorizationMiddleware(token string) mux.MiddlewareFunc {
middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if validateAuthorizationBearerToken(w, r, token) {
next.ServeHTTP(w, r)
}
})
}
return middleware
}
func startAgent(parameters *agentParameters) error {
if parameters.host == "" {
return errMissingHost
}
// set message handler
parameters.msgHandler = msghandler.NewRegistrar()
ctx, err := createAriesAgent(parameters)
if err != nil {
return err
}
// get all HTTP REST API handlers available for controller API
handlers, err := controller.GetRESTHandlers(ctx, controller.WithWebhookURLs(parameters.webhookURLs...),
controller.WithDefaultLabel(parameters.defaultLabel), controller.WithAutoAccept(parameters.autoAccept),
controller.WithMessageHandler(parameters.msgHandler),
controller.WithAutoExecuteRFC0593(parameters.autoExecuteRFC0593))
if err != nil {
return fmt.Errorf("failed to start aries agent rest on port [%s], failed to get rest service api : %w",
parameters.host, err)
}
router := mux.NewRouter()
if parameters.token != "" {
router.Use(authorizationMiddleware(parameters.token))
}
for _, handler := range handlers {
router.HandleFunc(handler.Path(), handler.Handle()).Methods(handler.Method())
}
logger.Infof("Starting aries agent rest on host [%s]", parameters.host)
// start server on given port and serve using given handlers
handler := cors.New(
cors.Options{
AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodDelete, http.MethodHead},
AllowedHeaders: []string{"Origin", "Accept", "Content-Type", "X-Requested-With", "Authorization"},
},
).Handler(router)
err = parameters.server.ListenAndServe(parameters.host, handler, parameters.tlsCertFile, parameters.tlsKeyFile)
if err != nil {
return fmt.Errorf("failed to start aries agent rest on port [%s], cause: %w", parameters.host, err)
}
return nil
}
//nolint:funlen,gocyclo
func createAriesAgent(parameters *agentParameters) (*context.Provider, error) {
var opts []aries.Option
storePro, err := createStoreProviders(parameters)
if err != nil {
return nil, err
}
opts = append(opts, aries.WithStoreProvider(storePro))
if parameters.transportReturnRoute != "" {
opts = append(opts, aries.WithTransportReturnRoute(parameters.transportReturnRoute))
}
inboundTransportOpt, err := getInboundTransportOpts(parameters.inboundHostInternals,
parameters.inboundHostExternals, parameters.tlsCertFile, parameters.tlsKeyFile)
if err != nil {
return nil, fmt.Errorf("failed to start aries agent rest on port [%s], failed to inbound tranpsort opt : %w",
parameters.host, err)
}
opts = append(opts, inboundTransportOpt...)
resolverOpts, err := getResolverOpts(parameters.httpResolvers)
if err != nil {
return nil, fmt.Errorf("failed to start aries agent rest on port [%s], failed to resolver opts : %w",
parameters.host, err)
}
opts = append(opts, resolverOpts...)
outboundTransportOpts, err := getOutboundTransportOpts(parameters.outboundTransports)
if err != nil {
return nil, fmt.Errorf("failed to start aries agent rest on port [%s], failed to outbound transport opts : %w",
parameters.host, err)
}
opts = append(opts, outboundTransportOpts...)
opts = append(opts, aries.WithMessageServiceProvider(parameters.msgHandler))
if len(parameters.contextProviderURLs) > 0 {
opts = append(opts, aries.WithJSONLDContextProviderURL(parameters.contextProviderURLs...))
}
if kt, ok := keyTypes[parameters.keyType]; ok {
opts = append(opts, aries.WithKeyType(kt))
}
if kat, ok := keyAgreementTypes[parameters.keyAgreementType]; ok {
opts = append(opts, aries.WithKeyAgreementType(kat))
}
if len(parameters.mediaTypeProfiles) > 0 {
opts = append(opts, aries.WithMediaTypeProfiles(parameters.mediaTypeProfiles))
}
framework, err := aries.New(opts...)
if err != nil {
return nil, fmt.Errorf("failed to start aries agent rest on port [%s], failed to initialize framework : %w",
parameters.host, err)
}
ctx, err := framework.Context()
if err != nil {
return nil, fmt.Errorf("failed to start aries agent rest on port [%s], failed to get aries context : %w",
parameters.host, err)
}
return ctx, nil
}
func createStoreProviders(parameters *agentParameters) (storage.Provider, error) {
provider, supported := supportedStorageProviders[parameters.dbParam.dbType]
if !supported {
return nil, fmt.Errorf("key database type not set to a valid type." +
" run start --help to see the available options")
}
var store storage.Provider
err := backoff.RetryNotify(
func() error {
var openErr error
store, openErr = provider(parameters.dbParam.prefix)
return openErr
},
backoff.WithMaxRetries(backoff.NewConstantBackOff(time.Second), parameters.dbParam.timeout),
func(retryErr error, t time.Duration) {
logger.Warnf(
"failed to connect to storage, will sleep for %s before trying again : %s\n",
t, retryErr)
},
)
if err != nil {
return nil, fmt.Errorf("failed to connect to storage at %s : %w", parameters.dbParam.prefix, err)
}
return store, nil
}