From ba2ea9952077d6d47e66d028ff7ae78fc7e99676 Mon Sep 17 00:00:00 2001 From: Firas Qutishat Date: Fri, 22 Apr 2022 07:50:13 -0400 Subject: [PATCH 01/10] fix: vc-revocation-list-2021 context Signed-off-by: Firas Qutishat Signed-off-by: Christian Nuss --- pkg/doc/ldcontext/embed/embed_contexts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/doc/ldcontext/embed/embed_contexts.go b/pkg/doc/ldcontext/embed/embed_contexts.go index 0ee313785..95dbd89de 100644 --- a/pkg/doc/ldcontext/embed/embed_contexts.go +++ b/pkg/doc/ldcontext/embed/embed_contexts.go @@ -115,7 +115,7 @@ var Contexts = []ldcontext.Document{ //nolint:gochecknoglobals Content: revocationList2020, }, { - URL: "https://w3id.org/vc-revocation-list-2021/v1", + URL: "https://w3id.org/vc/status-list/2021/v1", DocumentURL: "https://w3c-ccg.github.io/vc-status-list-2021/contexts/v1.jsonld", Content: revocationList2021, }, From 9dab122ddad2f31c4fbfd023d337f04daecb0da7 Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Mon, 25 Apr 2022 10:43:18 -0700 Subject: [PATCH 02/10] externalize agent parameters + router Signed-off-by: Christian Nuss --- cmd/aries-agent-rest/startcmd/start.go | 268 +++++++++++--------- cmd/aries-agent-rest/startcmd/start_test.go | 34 +-- 2 files changed, 160 insertions(+), 142 deletions(-) diff --git a/cmd/aries-agent-rest/startcmd/start.go b/cmd/aries-agent-rest/startcmd/start.go index 6775c034c..7f26f2420 100644 --- a/cmd/aries-agent-rest/startcmd/start.go +++ b/cmd/aries-agent-rest/startcmd/start.go @@ -233,7 +233,7 @@ var ( } ) -type agentParameters struct { +type AgentParameters struct { server server host, defaultLabel, transportReturnRoute string tlsCertFile, tlsKeyFile string @@ -289,144 +289,152 @@ func Cmd(server server) (*cobra.Command, error) { 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 - } +func NewAgentParameters(server server, cmd *cobra.Command) (*AgentParameters, error) { + // log level + logLevel, err := getUserSetVar(cmd, agentLogLevelFlagName, agentLogLevelEnvKey, true) + if err != nil { + return nil, err + } - err = setLogLevel(logLevel) - if err != nil { - return err - } + err = setLogLevel(logLevel) + if err != nil { + return nil, err + } - host, err := getUserSetVar(cmd, agentHostFlagName, agentHostEnvKey, false) - if err != nil { - return err - } + host, err := getUserSetVar(cmd, agentHostFlagName, agentHostEnvKey, false) + if err != nil { + return nil, err + } - token, err := getUserSetVar(cmd, agentTokenFlagName, agentTokenEnvKey, true) - if err != nil { - return err - } + token, err := getUserSetVar(cmd, agentTokenFlagName, agentTokenEnvKey, true) + if err != nil { + return nil, err + } - inboundHosts, err := getUserSetVars(cmd, agentInboundHostFlagName, agentInboundHostEnvKey, true) - if err != nil { - return err - } + inboundHosts, err := getUserSetVars(cmd, agentInboundHostFlagName, agentInboundHostEnvKey, true) + if err != nil { + return nil, err + } - inboundHostExternals, err := getUserSetVars(cmd, agentInboundHostExternalFlagName, - agentInboundHostExternalEnvKey, true) - if err != nil { - return err - } + inboundHostExternals, err := getUserSetVars(cmd, agentInboundHostExternalFlagName, + agentInboundHostExternalEnvKey, true) + if err != nil { + return nil, err + } - websocketReadLimit, err := getWebSocketReadLimit(cmd) - if err != nil { - return err - } + websocketReadLimit, err := getWebSocketReadLimit(cmd) + if err != nil { + return nil, err + } - dbParam, err := getDBParam(cmd) - if err != nil { - return err - } + dbParam, err := getDBParam(cmd) + if err != nil { + return nil, err + } - defaultLabel, err := getUserSetVar(cmd, agentDefaultLabelFlagName, agentDefaultLabelEnvKey, true) - if err != nil { - return err - } + defaultLabel, err := getUserSetVar(cmd, agentDefaultLabelFlagName, agentDefaultLabelEnvKey, true) + if err != nil { + return nil, err + } - autoAccept, err := getAutoAcceptValue(cmd) - if err != nil { - return err - } + autoAccept, err := getAutoAcceptValue(cmd) + if err != nil { + return nil, err + } - webhookURLs, err := getUserSetVars(cmd, agentWebhookFlagName, agentWebhookEnvKey, autoAccept) - if err != nil { - return err - } + webhookURLs, err := getUserSetVars(cmd, agentWebhookFlagName, agentWebhookEnvKey, autoAccept) + if err != nil { + return nil, err + } - httpResolvers, err := getUserSetVars(cmd, agentHTTPResolverFlagName, agentHTTPResolverEnvKey, true) - if err != nil { - return err - } + httpResolvers, err := getUserSetVars(cmd, agentHTTPResolverFlagName, agentHTTPResolverEnvKey, true) + if err != nil { + return nil, err + } - outboundTransports, err := getUserSetVars(cmd, agentOutboundTransportFlagName, - agentOutboundTransportEnvKey, true) - if err != nil { - return err - } + outboundTransports, err := getUserSetVars(cmd, agentOutboundTransportFlagName, + agentOutboundTransportEnvKey, true) + if err != nil { + return nil, err + } - transportReturnRoute, err := getUserSetVar(cmd, agentTransportReturnRouteFlagName, - agentTransportReturnRouteEnvKey, true) - if err != nil { - return err - } + transportReturnRoute, err := getUserSetVar(cmd, agentTransportReturnRouteFlagName, + agentTransportReturnRouteEnvKey, true) + if err != nil { + return nil, err + } - contextProviderURLs, err := getUserSetVars(cmd, agentContextProviderFlagName, agentContextProviderEnvKey, true) - if err != nil { - return err - } + contextProviderURLs, err := getUserSetVars(cmd, agentContextProviderFlagName, agentContextProviderEnvKey, true) + if err != nil { + return nil, err + } - autoExecuteRFC0593, err := getAutoExecuteRFC0593(cmd) - if err != nil { - return err - } + autoExecuteRFC0593, err := getAutoExecuteRFC0593(cmd) + if err != nil { + return nil, err + } - tlsCertFile, err := getUserSetVar(cmd, agentTLSCertFileFlagName, agentTLSCertFileEnvKey, true) - if err != nil { - return err - } + tlsCertFile, err := getUserSetVar(cmd, agentTLSCertFileFlagName, agentTLSCertFileEnvKey, true) + if err != nil { + return nil, err + } - tlsKeyFile, err := getUserSetVar(cmd, agentTLSKeyFileFlagName, agentTLSKeyFileEnvKey, true) - if err != nil { - return err - } + tlsKeyFile, err := getUserSetVar(cmd, agentTLSKeyFileFlagName, agentTLSKeyFileEnvKey, true) + if err != nil { + return nil, err + } - keyType, err := getUserSetVar(cmd, agentKeyTypeFlagName, agentKeyTypeEnvKey, true) - if err != nil { - return err - } + keyType, err := getUserSetVar(cmd, agentKeyTypeFlagName, agentKeyTypeEnvKey, true) + if err != nil { + return nil, err + } - keyAgreementType, err := getUserSetVar(cmd, agentKeyAgreementTypeFlagName, agentKeyAgreementTypeEnvKey, true) - if err != nil { - return err - } + keyAgreementType, err := getUserSetVar(cmd, agentKeyAgreementTypeFlagName, agentKeyAgreementTypeEnvKey, true) + if err != nil { + return nil, err + } + + mediaTypeProfiles, err := getUserSetVars(cmd, agentMediaTypeProfilesFlagName, agentMediaTypeProfilesEnvKey, true) + if err != nil { + return nil, err + } - mediaTypeProfiles, err := getUserSetVars(cmd, agentMediaTypeProfilesFlagName, agentMediaTypeProfilesEnvKey, true) + parameters := &AgentParameters{ + server: server, + host: host, + token: token, + inboundHostInternals: inboundHosts, + inboundHostExternals: inboundHostExternals, + websocketReadLimit: websocketReadLimit, + 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 parameters, 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 { + parameters, err := NewAgentParameters(server, cmd) if err != nil { return err } - - parameters := &agentParameters{ - server: server, - host: host, - token: token, - inboundHostInternals: inboundHosts, - inboundHostExternals: inboundHostExternals, - websocketReadLimit: websocketReadLimit, - 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) }, } @@ -585,7 +593,7 @@ func createFlags(startCmd *cobra.Command) { } func getUserSetVar(cmd *cobra.Command, flagName, envKey string, isOptional bool) (string, error) { - if cmd.Flags().Changed(flagName) { + if cmd != nil && cmd.Flags().Changed(flagName) { value, err := cmd.Flags().GetString(flagName) if err != nil { return "", fmt.Errorf(flagName+" flag not found: %s", err) @@ -605,7 +613,7 @@ func getUserSetVar(cmd *cobra.Command, flagName, envKey string, isOptional bool) } func getUserSetVars(cmd *cobra.Command, flagName, envKey string, isOptional bool) ([]string, error) { - if cmd.Flags().Changed(flagName) { + if cmd != nil && cmd.Flags().Changed(flagName) { value, err := cmd.Flags().GetStringSlice(flagName) if err != nil { return nil, fmt.Errorf(flagName+" flag not found: %s", err) @@ -775,17 +783,17 @@ func authorizationMiddleware(token string) mux.MiddlewareFunc { return middleware } -func startAgent(parameters *agentParameters) error { +func (parameters AgentParameters) NewRouter() (*mux.Router, error) { if parameters.host == "" { - return errMissingHost + return nil, errMissingHost } // set message handler parameters.msgHandler = msghandler.NewRegistrar() - ctx, err := createAriesAgent(parameters) + ctx, err := createAriesAgent(¶meters) if err != nil { - return err + return nil, err } // get all HTTP REST API handlers available for controller API @@ -794,7 +802,7 @@ func startAgent(parameters *agentParameters) error { 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", + return nil, fmt.Errorf("failed to start aries agent rest on port [%s], failed to get rest service api : %w", parameters.host, err) } @@ -808,7 +816,17 @@ func startAgent(parameters *agentParameters) error { router.HandleFunc(handler.Path(), handler.Handle()).Methods(handler.Method()) } + return router, nil +} + +func startAgent(parameters *AgentParameters) error { logger.Infof("Starting aries agent rest on host [%s]", parameters.host) + + router, err := parameters.NewRouter() + if err != nil { + return fmt.Errorf("failed to start aries agent rest on port [%s], unable to create router: %w", parameters.host, err) + } + // start server on given port and serve using given handlers handler := cors.New( cors.Options{ @@ -826,7 +844,7 @@ func startAgent(parameters *agentParameters) error { } //nolint:funlen,gocyclo -func createAriesAgent(parameters *agentParameters) (*context.Provider, error) { +func createAriesAgent(parameters *AgentParameters) (*context.Provider, error) { var opts []aries.Option storePro, err := createStoreProviders(parameters) @@ -898,7 +916,7 @@ func createAriesAgent(parameters *agentParameters) (*context.Provider, error) { return ctx, nil } -func createStoreProviders(parameters *agentParameters) (storage.Provider, error) { +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." + diff --git a/cmd/aries-agent-rest/startcmd/start_test.go b/cmd/aries-agent-rest/startcmd/start_test.go index b742076e6..876a43187 100644 --- a/cmd/aries-agent-rest/startcmd/start_test.go +++ b/cmd/aries-agent-rest/startcmd/start_test.go @@ -104,7 +104,7 @@ func TestStartAriesDRequests(t *testing.T) { testInboundHostURL := randomURL() go func() { - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: testHostURL, inboundHostInternals: []string{httpProtocol + "@" + testInboundHostURL}, @@ -295,7 +295,7 @@ func TestStartCmdWithMissingHostArg(t *testing.T) { } func TestStartAgentWithBlankHost(t *testing.T) { - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &mockServer{}, inboundHostInternals: []string{randomURL()}, } @@ -535,7 +535,7 @@ func TestStartMultipleAgentsWithSameHost(t *testing.T) { inboundHost2 := "localhost:8097" go func() { - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: host, inboundHostInternals: []string{httpProtocol + "@" + inboundHost}, @@ -548,7 +548,7 @@ func TestStartMultipleAgentsWithSameHost(t *testing.T) { waitForServerToStart(t, host, inboundHost) - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: host, inboundHostInternals: []string{httpProtocol + "@" + inboundHost2}, @@ -568,7 +568,7 @@ func TestStartAriesErrorWithResolvers(t *testing.T) { testHostURL := randomURL() testInboundHostURL := randomURL() - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: testHostURL, inboundHostInternals: []string{httpProtocol + "@" + testInboundHostURL}, @@ -586,7 +586,7 @@ func TestStartAriesErrorWithResolvers(t *testing.T) { testHostURL := randomURL() testInboundHostURL := randomURL() - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: testHostURL, inboundHostInternals: []string{httpProtocol + "@" + testInboundHostURL}, @@ -606,7 +606,7 @@ func TestStartAriesWithOutboundTransports(t *testing.T) { testInboundHostURL := randomURL() go func() { - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: testHostURL, inboundHostInternals: []string{httpProtocol + "@" + testInboundHostURL}, @@ -627,7 +627,7 @@ func TestStartAriesWithOutboundTransports(t *testing.T) { testHostURL := randomURL() testInboundHostURL := randomURL() - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: testHostURL, inboundHostInternals: []string{httpProtocol + "@" + testInboundHostURL}, @@ -647,7 +647,7 @@ func TestStartAriesWithInboundTransport(t *testing.T) { testInboundHostURL := randomURL() go func() { - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: testHostURL, inboundHostInternals: []string{websocketProtocol + "@" + testInboundHostURL}, @@ -668,7 +668,7 @@ func TestStartAriesWithInboundTransport(t *testing.T) { testHostURL := randomURL() testInboundHostURL := randomURL() - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: testHostURL, inboundHostInternals: []string{"wss" + "@" + testInboundHostURL}, @@ -687,7 +687,7 @@ func TestStartAriesWithAutoAccept(t *testing.T) { testInboundHostURL := randomURL() go func() { - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: testHostURL, inboundHostInternals: []string{httpProtocol + "@" + testInboundHostURL}, @@ -706,7 +706,7 @@ func TestStartAriesWithAutoAccept(t *testing.T) { } func TestStartAriesTLS(t *testing.T) { - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: ":0", dbParam: &dbParam{dbType: databaseTypeMemOption}, @@ -756,7 +756,7 @@ func TestCreateAriesWithKeyType(t *testing.T) { for _, tt := range tests { tc := tt t.Run(tc.name, func(t *testing.T) { - parameters := &agentParameters{ + parameters := &AgentParameters{ dbParam: &dbParam{dbType: databaseTypeMemOption}, keyType: tc.kt, } @@ -794,7 +794,7 @@ func TestCreateAriesWithKeyAgreementType(t *testing.T) { for _, tt := range tests { tc := tt t.Run(tc.name, func(t *testing.T) { - parameters := &agentParameters{ + parameters := &AgentParameters{ dbParam: &dbParam{dbType: databaseTypeMemOption}, keyAgreementType: tc.kt, } @@ -832,7 +832,7 @@ func TestCreateAriesWithMediaTypeProfiles(t *testing.T) { for _, tt := range tests { tc := tt t.Run(tc.name, func(t *testing.T) { - parameters := &agentParameters{ + parameters := &AgentParameters{ dbParam: &dbParam{dbType: databaseTypeMemOption}, mediaTypeProfiles: tc.mtp, } @@ -854,7 +854,7 @@ func TestStartAriesWithAuthorization(t *testing.T) { testInboundHostURL := randomURL() go func() { - parameters := &agentParameters{ + parameters := &AgentParameters{ server: &HTTPServer{}, host: testHostURL, token: goodToken, @@ -893,7 +893,7 @@ func TestStartAriesWithAuthorization(t *testing.T) { func TestStoreProvider(t *testing.T) { t.Run("test invalid database type", func(t *testing.T) { - _, err := createAriesAgent(&agentParameters{dbParam: &dbParam{dbType: "data1"}}) + _, err := createAriesAgent(&AgentParameters{dbParam: &dbParam{dbType: "data1"}}) require.Error(t, err) require.Contains(t, err.Error(), "database type not set to a valid type") }) From 730e41a82fcd8315a0c2b6555e78af99d76f6f32 Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Mon, 25 Apr 2022 16:05:35 -0700 Subject: [PATCH 03/10] fix linting issues Signed-off-by: Christian Nuss --- cmd/aries-agent-rest/startcmd/start.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/aries-agent-rest/startcmd/start.go b/cmd/aries-agent-rest/startcmd/start.go index 7f26f2420..f168405c1 100644 --- a/cmd/aries-agent-rest/startcmd/start.go +++ b/cmd/aries-agent-rest/startcmd/start.go @@ -233,6 +233,7 @@ var ( } ) +// AgentParameters represents the various options to run an Aries Agent. type AgentParameters struct { server server host, defaultLabel, transportReturnRoute string @@ -289,7 +290,8 @@ func Cmd(server server) (*cobra.Command, error) { return startCmd, nil } -func NewAgentParameters(server server, cmd *cobra.Command) (*AgentParameters, error) { +// NewAgentParameters constructs AgentParameters with the given cobra command. +func NewAgentParameters(server server, cmd *cobra.Command) (*AgentParameters, error) { //nolint: funlen,gocyclo // log level logLevel, err := getUserSetVar(cmd, agentLogLevelFlagName, agentLogLevelEnvKey, true) if err != nil { @@ -425,7 +427,7 @@ func NewAgentParameters(server server, cmd *cobra.Command) (*AgentParameters, er return parameters, nil } -func createStartCMD(server server) *cobra.Command { //nolint: funlen,gocyclo,gocognit +func createStartCMD(server server) *cobra.Command { //nolint: gocyclo,gocognit return &cobra.Command{ Use: "start", Short: "Start an agent", @@ -783,7 +785,8 @@ func authorizationMiddleware(token string) mux.MiddlewareFunc { return middleware } -func (parameters AgentParameters) NewRouter() (*mux.Router, error) { +// NewRouter returns a Router for the Aries Agent. +func (parameters *AgentParameters) NewRouter() (*mux.Router, error) { if parameters.host == "" { return nil, errMissingHost } @@ -791,7 +794,7 @@ func (parameters AgentParameters) NewRouter() (*mux.Router, error) { // set message handler parameters.msgHandler = msghandler.NewRegistrar() - ctx, err := createAriesAgent(¶meters) + ctx, err := createAriesAgent(parameters) if err != nil { return nil, err } From ce87dc03fbb68183cbda4c0721940a1f818f26d5 Mon Sep 17 00:00:00 2001 From: Derek Trider Date: Mon, 25 Apr 2022 13:50:23 -0400 Subject: [PATCH 04/10] feat: In-memory store "ping" method The method always returns nil. It's there just to allow it to implement a "Pinger" sort of interface which may be defined somewhere. Signed-off-by: Derek Trider Signed-off-by: Christian Nuss --- component/storageutil/mem/mem.go | 6 ++++++ component/storageutil/mem/mem_test.go | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/component/storageutil/mem/mem.go b/component/storageutil/mem/mem.go index 612248965..c2ffebe69 100644 --- a/component/storageutil/mem/mem.go +++ b/component/storageutil/mem/mem.go @@ -132,6 +132,12 @@ func (p *Provider) Close() error { return nil } +// Ping always returns nil. It's here just to allow it to implement a "Pinger" sort of interface which may be defined +// somewhere and implemented by other storage implementations that use a remote database. +func (p *Provider) Ping() error { + return nil +} + func (p *Provider) removeStore(name string) { p.lock.Lock() defer p.lock.Unlock() diff --git a/component/storageutil/mem/mem_test.go b/component/storageutil/mem/mem_test.go index 1ccf9506c..ac1646306 100644 --- a/component/storageutil/mem/mem_test.go +++ b/component/storageutil/mem/mem_test.go @@ -60,3 +60,10 @@ func TestMemIterator(t *testing.T) { require.EqualError(t, err, "iterator is exhausted") require.Nil(t, tags) } + +func TestProvider_Ping(t *testing.T) { + provider := mem.NewProvider() + + err := provider.Ping() + require.NoError(t, err) +} From c0a1cc4ee2146feff07f62e2c757fffeae36d868 Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Tue, 26 Apr 2022 08:02:16 -0700 Subject: [PATCH 05/10] remove unused nolint Signed-off-by: Christian Nuss --- cmd/aries-agent-rest/startcmd/start.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/aries-agent-rest/startcmd/start.go b/cmd/aries-agent-rest/startcmd/start.go index f168405c1..6fc515433 100644 --- a/cmd/aries-agent-rest/startcmd/start.go +++ b/cmd/aries-agent-rest/startcmd/start.go @@ -427,7 +427,7 @@ func NewAgentParameters(server server, cmd *cobra.Command) (*AgentParameters, er return parameters, nil } -func createStartCMD(server server) *cobra.Command { //nolint: gocyclo,gocognit +func createStartCMD(server server) *cobra.Command { //nolint: gocognit return &cobra.Command{ Use: "start", Short: "Start an agent", From 322d4e643668a8a7e465ed1d821495b47ebd2cd4 Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Tue, 26 Apr 2022 08:08:08 -0700 Subject: [PATCH 06/10] fix linting issues Signed-off-by: Christian Nuss --- cmd/aries-agent-rest/startcmd/start.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/aries-agent-rest/startcmd/start.go b/cmd/aries-agent-rest/startcmd/start.go index 6fc515433..5f3be1b83 100644 --- a/cmd/aries-agent-rest/startcmd/start.go +++ b/cmd/aries-agent-rest/startcmd/start.go @@ -427,7 +427,7 @@ func NewAgentParameters(server server, cmd *cobra.Command) (*AgentParameters, er return parameters, nil } -func createStartCMD(server server) *cobra.Command { //nolint: gocognit +func createStartCMD(server server) *cobra.Command { return &cobra.Command{ Use: "start", Short: "Start an agent", From e03e60ce743f13ce3a518524b1ecdefa2cdee6f3 Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Tue, 26 Apr 2022 08:39:59 -0700 Subject: [PATCH 07/10] signoff Signed-off-by: Christian Nuss --- cmd/aries-agent-rest/startcmd/start.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/aries-agent-rest/startcmd/start.go b/cmd/aries-agent-rest/startcmd/start.go index 5f3be1b83..3c5f39e9f 100644 --- a/cmd/aries-agent-rest/startcmd/start.go +++ b/cmd/aries-agent-rest/startcmd/start.go @@ -827,7 +827,7 @@ func startAgent(parameters *AgentParameters) error { router, err := parameters.NewRouter() if err != nil { - return fmt.Errorf("failed to start aries agent rest on port [%s], unable to create router: %w", parameters.host, err) + return err } // start server on given port and serve using given handlers From 1aa05750a93cc10865de8d122ff94b6adbe4e319 Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Wed, 27 Apr 2022 08:17:49 -0700 Subject: [PATCH 08/10] add NewAgentParameters test Signed-off-by: Christian Nuss --- cmd/aries-agent-rest/startcmd/start_test.go | 72 +++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/cmd/aries-agent-rest/startcmd/start_test.go b/cmd/aries-agent-rest/startcmd/start_test.go index 876a43187..471e2e39e 100644 --- a/cmd/aries-agent-rest/startcmd/start_test.go +++ b/cmd/aries-agent-rest/startcmd/start_test.go @@ -926,6 +926,78 @@ func TestStartCmdInvalidAutoExecuteRFC0593Value(t *testing.T) { require.Contains(t, err.Error(), "invalid syntax") } +func TestNewAgentParametersUsingEnv(t *testing.T) { + os.Setenv(agentHostEnvKey, "agentHost") + defer os.Unsetenv(agentHostEnvKey) + os.Setenv(agentTokenEnvKey, "agentToken") + defer os.Unsetenv(agentTokenEnvKey) + os.Setenv(databaseTypeEnvKey, "databaseType") + defer os.Unsetenv(databaseTypeEnvKey) + os.Setenv(databasePrefixEnvKey, "databasePrefix") + defer os.Unsetenv(databasePrefixEnvKey) + os.Setenv(databaseTimeoutEnvKey, "1") + defer os.Unsetenv(databaseTimeoutEnvKey) + os.Setenv(agentWebhookEnvKey, "agentWebhook") + defer os.Unsetenv(agentWebhookEnvKey) + os.Setenv(agentDefaultLabelEnvKey, "agentDefaultLabel") + defer os.Unsetenv(agentDefaultLabelEnvKey) + os.Setenv(agentLogLevelEnvKey, "DEBUG") + defer os.Unsetenv(agentLogLevelEnvKey) + os.Setenv(agentHTTPResolverEnvKey, "agentHTTPResolver") + defer os.Unsetenv(agentHTTPResolverEnvKey) + os.Setenv(agentOutboundTransportEnvKey, "agentOutboundTransport") + defer os.Unsetenv(agentOutboundTransportEnvKey) + os.Setenv(agentTLSCertFileEnvKey, "agentTLSCertFile") + defer os.Unsetenv(agentTLSCertFileEnvKey) + os.Setenv(agentTLSKeyFileEnvKey, "agentTLSKeyFile") + defer os.Unsetenv(agentTLSKeyFileEnvKey) + os.Setenv(agentInboundHostEnvKey, "agentInboundHost") + defer os.Unsetenv(agentInboundHostEnvKey) + os.Setenv(agentInboundHostExternalEnvKey, "agentInboundHostExternal") + defer os.Unsetenv(agentInboundHostExternalEnvKey) + os.Setenv(agentWebSocketReadLimitEnvKey, "0") + defer os.Unsetenv(agentWebSocketReadLimitEnvKey) + os.Setenv(agentAutoAcceptEnvKey, "true") + defer os.Unsetenv(agentAutoAcceptEnvKey) + os.Setenv(agentTransportReturnRouteEnvKey, "agentTransportReturnRoute") + defer os.Unsetenv(agentTransportReturnRouteEnvKey) + os.Setenv(agentAutoExecuteRFC0593EnvKey, "true") + defer os.Unsetenv(agentAutoExecuteRFC0593EnvKey) + os.Setenv(agentContextProviderEnvKey, "agentContextProvider") + defer os.Unsetenv(agentContextProviderEnvKey) + os.Setenv(agentKeyTypeEnvKey, "agentKeyType") + defer os.Unsetenv(agentKeyTypeEnvKey) + os.Setenv(agentKeyAgreementTypeEnvKey, "agentKeyAgreementType") + defer os.Unsetenv(agentKeyAgreementTypeEnvKey) + os.Setenv(agentMediaTypeProfilesEnvKey, "agentMediaTypeProfiles") + defer os.Unsetenv(agentMediaTypeProfilesEnvKey) + + parameters, err := NewAgentParameters(&mockServer{}, nil) + + require.Nil(t, err) + require.Equal(t, spi.DEBUG, log.GetLevel("")) + require.Equal(t, "agentHost", parameters.host) + require.Equal(t, "agentToken", parameters.token) + require.Equal(t, "agentInboundHost", parameters.inboundHostInternals[0]) + require.Equal(t, "agentInboundHostExternal", parameters.inboundHostExternals[0]) + require.Equal(t, int64(0), parameters.websocketReadLimit) + require.Equal(t, "databaseType", parameters.dbParam.dbType) + require.Equal(t, "databasePrefix", parameters.dbParam.prefix) + require.Equal(t, uint64(1), parameters.dbParam.timeout) + require.Equal(t, "agentDefaultLabel", parameters.defaultLabel) + require.Equal(t, true, parameters.autoAccept) + require.Equal(t, "agentWebhook", parameters.webhookURLs[0]) + require.Equal(t, "agentOutboundTransport", parameters.outboundTransports[0]) + require.Equal(t, "agentTransportReturnRoute", parameters.transportReturnRoute) + require.Equal(t, "agentContextProvider", parameters.contextProviderURLs[0]) + require.Equal(t, true, parameters.autoExecuteRFC0593) + require.Equal(t, "agentTLSCertFile", parameters.tlsCertFile) + require.Equal(t, "agentTLSKeyFile", parameters.tlsKeyFile) + require.Equal(t, "agentKeyType", parameters.keyType) + require.Equal(t, "agentKeyAgreementType", parameters.keyAgreementType) + require.Equal(t, "agentMediaTypeProfiles", parameters.mediaTypeProfiles[0]) +} + func waitForServerToStart(t *testing.T, host, inboundHost string) { if err := listenFor(host); err != nil { t.Fatal(err) From 7387bc6d6fb036edf18e0d5983cf4aa4ff3f4fbc Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Wed, 27 Apr 2022 08:43:00 -0700 Subject: [PATCH 09/10] use t.Setenv Signed-off-by: Christian Nuss --- cmd/aries-agent-rest/startcmd/start_test.go | 66 +++++++-------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/cmd/aries-agent-rest/startcmd/start_test.go b/cmd/aries-agent-rest/startcmd/start_test.go index 471e2e39e..be295447a 100644 --- a/cmd/aries-agent-rest/startcmd/start_test.go +++ b/cmd/aries-agent-rest/startcmd/start_test.go @@ -927,50 +927,28 @@ func TestStartCmdInvalidAutoExecuteRFC0593Value(t *testing.T) { } func TestNewAgentParametersUsingEnv(t *testing.T) { - os.Setenv(agentHostEnvKey, "agentHost") - defer os.Unsetenv(agentHostEnvKey) - os.Setenv(agentTokenEnvKey, "agentToken") - defer os.Unsetenv(agentTokenEnvKey) - os.Setenv(databaseTypeEnvKey, "databaseType") - defer os.Unsetenv(databaseTypeEnvKey) - os.Setenv(databasePrefixEnvKey, "databasePrefix") - defer os.Unsetenv(databasePrefixEnvKey) - os.Setenv(databaseTimeoutEnvKey, "1") - defer os.Unsetenv(databaseTimeoutEnvKey) - os.Setenv(agentWebhookEnvKey, "agentWebhook") - defer os.Unsetenv(agentWebhookEnvKey) - os.Setenv(agentDefaultLabelEnvKey, "agentDefaultLabel") - defer os.Unsetenv(agentDefaultLabelEnvKey) - os.Setenv(agentLogLevelEnvKey, "DEBUG") - defer os.Unsetenv(agentLogLevelEnvKey) - os.Setenv(agentHTTPResolverEnvKey, "agentHTTPResolver") - defer os.Unsetenv(agentHTTPResolverEnvKey) - os.Setenv(agentOutboundTransportEnvKey, "agentOutboundTransport") - defer os.Unsetenv(agentOutboundTransportEnvKey) - os.Setenv(agentTLSCertFileEnvKey, "agentTLSCertFile") - defer os.Unsetenv(agentTLSCertFileEnvKey) - os.Setenv(agentTLSKeyFileEnvKey, "agentTLSKeyFile") - defer os.Unsetenv(agentTLSKeyFileEnvKey) - os.Setenv(agentInboundHostEnvKey, "agentInboundHost") - defer os.Unsetenv(agentInboundHostEnvKey) - os.Setenv(agentInboundHostExternalEnvKey, "agentInboundHostExternal") - defer os.Unsetenv(agentInboundHostExternalEnvKey) - os.Setenv(agentWebSocketReadLimitEnvKey, "0") - defer os.Unsetenv(agentWebSocketReadLimitEnvKey) - os.Setenv(agentAutoAcceptEnvKey, "true") - defer os.Unsetenv(agentAutoAcceptEnvKey) - os.Setenv(agentTransportReturnRouteEnvKey, "agentTransportReturnRoute") - defer os.Unsetenv(agentTransportReturnRouteEnvKey) - os.Setenv(agentAutoExecuteRFC0593EnvKey, "true") - defer os.Unsetenv(agentAutoExecuteRFC0593EnvKey) - os.Setenv(agentContextProviderEnvKey, "agentContextProvider") - defer os.Unsetenv(agentContextProviderEnvKey) - os.Setenv(agentKeyTypeEnvKey, "agentKeyType") - defer os.Unsetenv(agentKeyTypeEnvKey) - os.Setenv(agentKeyAgreementTypeEnvKey, "agentKeyAgreementType") - defer os.Unsetenv(agentKeyAgreementTypeEnvKey) - os.Setenv(agentMediaTypeProfilesEnvKey, "agentMediaTypeProfiles") - defer os.Unsetenv(agentMediaTypeProfilesEnvKey) + t.Setenv(agentHostEnvKey, "agentHost") + t.Setenv(agentTokenEnvKey, "agentToken") + t.Setenv(databaseTypeEnvKey, "databaseType") + t.Setenv(databasePrefixEnvKey, "databasePrefix") + t.Setenv(databaseTimeoutEnvKey, "1") + t.Setenv(agentWebhookEnvKey, "agentWebhook") + t.Setenv(agentDefaultLabelEnvKey, "agentDefaultLabel") + t.Setenv(agentLogLevelEnvKey, "DEBUG") + t.Setenv(agentHTTPResolverEnvKey, "agentHTTPResolver") + t.Setenv(agentOutboundTransportEnvKey, "agentOutboundTransport") + t.Setenv(agentTLSCertFileEnvKey, "agentTLSCertFile") + t.Setenv(agentTLSKeyFileEnvKey, "agentTLSKeyFile") + t.Setenv(agentInboundHostEnvKey, "agentInboundHost") + t.Setenv(agentInboundHostExternalEnvKey, "agentInboundHostExternal") + t.Setenv(agentWebSocketReadLimitEnvKey, "0") + t.Setenv(agentAutoAcceptEnvKey, "true") + t.Setenv(agentTransportReturnRouteEnvKey, "agentTransportReturnRoute") + t.Setenv(agentAutoExecuteRFC0593EnvKey, "true") + t.Setenv(agentContextProviderEnvKey, "agentContextProvider") + t.Setenv(agentKeyTypeEnvKey, "agentKeyType") + t.Setenv(agentKeyAgreementTypeEnvKey, "agentKeyAgreementType") + t.Setenv(agentMediaTypeProfilesEnvKey, "agentMediaTypeProfiles") parameters, err := NewAgentParameters(&mockServer{}, nil) From f8399f32880a76a22a01f05e3d74036d44136d1d Mon Sep 17 00:00:00 2001 From: Christian Nuss Date: Wed, 27 Apr 2022 09:04:46 -0700 Subject: [PATCH 10/10] linting Signed-off-by: Christian Nuss --- cmd/aries-agent-rest/startcmd/start_test.go | 88 +++++++++++++++------ 1 file changed, 66 insertions(+), 22 deletions(-) diff --git a/cmd/aries-agent-rest/startcmd/start_test.go b/cmd/aries-agent-rest/startcmd/start_test.go index be295447a..61a382ad5 100644 --- a/cmd/aries-agent-rest/startcmd/start_test.go +++ b/cmd/aries-agent-rest/startcmd/start_test.go @@ -926,29 +926,73 @@ func TestStartCmdInvalidAutoExecuteRFC0593Value(t *testing.T) { require.Contains(t, err.Error(), "invalid syntax") } +// nolint: errcheck,gosec func TestNewAgentParametersUsingEnv(t *testing.T) { - t.Setenv(agentHostEnvKey, "agentHost") - t.Setenv(agentTokenEnvKey, "agentToken") - t.Setenv(databaseTypeEnvKey, "databaseType") - t.Setenv(databasePrefixEnvKey, "databasePrefix") - t.Setenv(databaseTimeoutEnvKey, "1") - t.Setenv(agentWebhookEnvKey, "agentWebhook") - t.Setenv(agentDefaultLabelEnvKey, "agentDefaultLabel") - t.Setenv(agentLogLevelEnvKey, "DEBUG") - t.Setenv(agentHTTPResolverEnvKey, "agentHTTPResolver") - t.Setenv(agentOutboundTransportEnvKey, "agentOutboundTransport") - t.Setenv(agentTLSCertFileEnvKey, "agentTLSCertFile") - t.Setenv(agentTLSKeyFileEnvKey, "agentTLSKeyFile") - t.Setenv(agentInboundHostEnvKey, "agentInboundHost") - t.Setenv(agentInboundHostExternalEnvKey, "agentInboundHostExternal") - t.Setenv(agentWebSocketReadLimitEnvKey, "0") - t.Setenv(agentAutoAcceptEnvKey, "true") - t.Setenv(agentTransportReturnRouteEnvKey, "agentTransportReturnRoute") - t.Setenv(agentAutoExecuteRFC0593EnvKey, "true") - t.Setenv(agentContextProviderEnvKey, "agentContextProvider") - t.Setenv(agentKeyTypeEnvKey, "agentKeyType") - t.Setenv(agentKeyAgreementTypeEnvKey, "agentKeyAgreementType") - t.Setenv(agentMediaTypeProfilesEnvKey, "agentMediaTypeProfiles") + os.Setenv(agentHostEnvKey, "agentHost") + defer os.Unsetenv(agentHostEnvKey) + + os.Setenv(agentTokenEnvKey, "agentToken") + defer os.Unsetenv(agentTokenEnvKey) + + os.Setenv(databaseTypeEnvKey, "databaseType") + defer os.Unsetenv(databaseTypeEnvKey) + + os.Setenv(databasePrefixEnvKey, "databasePrefix") + defer os.Unsetenv(databasePrefixEnvKey) + + os.Setenv(databaseTimeoutEnvKey, "1") + defer os.Unsetenv(databaseTimeoutEnvKey) + + os.Setenv(agentWebhookEnvKey, "agentWebhook") + defer os.Unsetenv(agentWebhookEnvKey) + + os.Setenv(agentDefaultLabelEnvKey, "agentDefaultLabel") + defer os.Unsetenv(agentDefaultLabelEnvKey) + + os.Setenv(agentLogLevelEnvKey, "DEBUG") + defer os.Unsetenv(agentLogLevelEnvKey) + + os.Setenv(agentHTTPResolverEnvKey, "agentHTTPResolver") + defer os.Unsetenv(agentHTTPResolverEnvKey) + + os.Setenv(agentOutboundTransportEnvKey, "agentOutboundTransport") + defer os.Unsetenv(agentOutboundTransportEnvKey) + + os.Setenv(agentTLSCertFileEnvKey, "agentTLSCertFile") + defer os.Unsetenv(agentTLSCertFileEnvKey) + + os.Setenv(agentTLSKeyFileEnvKey, "agentTLSKeyFile") + defer os.Unsetenv(agentTLSKeyFileEnvKey) + + os.Setenv(agentInboundHostEnvKey, "agentInboundHost") + defer os.Unsetenv(agentInboundHostEnvKey) + + os.Setenv(agentInboundHostExternalEnvKey, "agentInboundHostExternal") + defer os.Unsetenv(agentInboundHostExternalEnvKey) + + os.Setenv(agentWebSocketReadLimitEnvKey, "0") + defer os.Unsetenv(agentWebSocketReadLimitEnvKey) + + os.Setenv(agentAutoAcceptEnvKey, "true") + defer os.Unsetenv(agentAutoAcceptEnvKey) + + os.Setenv(agentTransportReturnRouteEnvKey, "agentTransportReturnRoute") + defer os.Unsetenv(agentTransportReturnRouteEnvKey) + + os.Setenv(agentAutoExecuteRFC0593EnvKey, "true") + defer os.Unsetenv(agentAutoExecuteRFC0593EnvKey) + + os.Setenv(agentContextProviderEnvKey, "agentContextProvider") + defer os.Unsetenv(agentContextProviderEnvKey) + + os.Setenv(agentKeyTypeEnvKey, "agentKeyType") + defer os.Unsetenv(agentKeyTypeEnvKey) + + os.Setenv(agentKeyAgreementTypeEnvKey, "agentKeyAgreementType") + defer os.Unsetenv(agentKeyAgreementTypeEnvKey) + + os.Setenv(agentMediaTypeProfilesEnvKey, "agentMediaTypeProfiles") + defer os.Unsetenv(agentMediaTypeProfilesEnvKey) parameters, err := NewAgentParameters(&mockServer{}, nil)