Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
externalize agent parameters + router (#3229)
Browse files Browse the repository at this point in the history
 fix: vc-revocation-list-2021 context

Signed-off-by: Firas Qutishat <firas.qutishat@securekey.com>
Signed-off-by: Christian Nuss <christian.nuss@gmail.com>

* externalize agent parameters + router

Signed-off-by: Christian Nuss <christian.nuss@gmail.com>

* fix linting issues

Signed-off-by: Christian Nuss <christian.nuss@gmail.com>

* 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 <Derek.Trider@securekey.com>
Signed-off-by: Christian Nuss <christian.nuss@gmail.com>

* remove unused nolint

Signed-off-by: Christian Nuss <christian.nuss@gmail.com>

* fix linting issues

Signed-off-by: Christian Nuss <christian.nuss@gmail.com>

* signoff

Signed-off-by: Christian Nuss <christian.nuss@gmail.com>

* add NewAgentParameters test

Signed-off-by: Christian Nuss <christian.nuss@gmail.com>

* use t.Setenv

Signed-off-by: Christian Nuss <christian.nuss@gmail.com>

* linting

Signed-off-by: Christian Nuss <christian.nuss@gmail.com>

Co-authored-by: Firas Qutishat <firas.qutishat@securekey.com>
Co-authored-by: Derek Trider <derek.trider@securekey.com>
  • Loading branch information
3 people authored Apr 27, 2022
1 parent 2435073 commit ae41d52
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 141 deletions.
269 changes: 145 additions & 124 deletions cmd/aries-agent-rest/startcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ var (
}
)

type agentParameters struct {
// AgentParameters represents the various options to run an Aries Agent.
type AgentParameters struct {
server server
host, defaultLabel, transportReturnRoute string
tlsCertFile, tlsKeyFile string
Expand Down Expand Up @@ -289,144 +290,153 @@ 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
}
// 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 {
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)
mediaTypeProfiles, err := getUserSetVars(cmd, agentMediaTypeProfilesFlagName, agentMediaTypeProfilesEnvKey, true)
if err != nil {
return nil, 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 parameters, nil
}

func createStartCMD(server server) *cobra.Command {
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)
},
}
Expand Down Expand Up @@ -585,7 +595,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)
Expand All @@ -605,7 +615,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)
Expand Down Expand Up @@ -775,17 +785,18 @@ func authorizationMiddleware(token string) mux.MiddlewareFunc {
return middleware
}

func startAgent(parameters *agentParameters) error {
// NewRouter returns a Router for the Aries Agent.
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)
if err != nil {
return err
return nil, err
}

// get all HTTP REST API handlers available for controller API
Expand All @@ -794,7 +805,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)
}

Expand All @@ -808,7 +819,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 err
}

// start server on given port and serve using given handlers
handler := cors.New(
cors.Options{
Expand All @@ -826,7 +847,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)
Expand Down Expand Up @@ -898,7 +919,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." +
Expand Down
Loading

0 comments on commit ae41d52

Please sign in to comment.