From 9a1e5174366a711acd3676984f5f961ebe36e52b Mon Sep 17 00:00:00 2001 From: Will Winder Date: Wed, 9 Mar 2022 17:18:53 -0500 Subject: [PATCH 1/3] Feature flag for query param configuration. --- cmd/algorand-indexer/daemon.go | 48 ++++++++++++++++++++-------------- cmd/algorand-indexer/main.go | 4 ++- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/cmd/algorand-indexer/daemon.go b/cmd/algorand-indexer/daemon.go index 611e2bc3a..c0d63efbd 100644 --- a/cmd/algorand-indexer/daemon.go +++ b/cmd/algorand-indexer/daemon.go @@ -39,6 +39,8 @@ var ( enableAllParameters bool ) +const paramConfigEnableFlag = false + var daemonCmd = &cobra.Command{ Use: "daemon", Short: "run indexer daemon", @@ -140,22 +142,6 @@ var daemonCmd = &cobra.Command{ options := makeOptions() - swag, err := generated.GetSwagger() - if err != nil { - fmt.Fprintf(os.Stderr, "failed to get swagger: %v", err) - os.Exit(1) - } - - if suppliedAPIConfigFile != "" { - logger.Infof("supplied api configuration file located at: %s", suppliedAPIConfigFile) - potentialDisabledMapConfig, err := api.MakeDisabledMapConfigFromFile(swag, suppliedAPIConfigFile) - if err != nil { - fmt.Fprintf(os.Stderr, "failed to created disabled map config from file: %v", err) - os.Exit(1) - } - options.DisabledMapConfig = potentialDisabledMapConfig - } - api.Serve(ctx, daemonServerAddr, db, bot, logger, options) wg.Wait() }, @@ -177,6 +163,10 @@ func init() { daemonCmd.Flags().Uint32VarP(&maxConn, "max-conn", "", 0, "set the maximum connections allowed in the connection pool, if the maximum is reached subsequent connections will wait until a connection becomes available, or timeout according to the read-timeout setting") daemonCmd.Flags().StringVar(&suppliedAPIConfigFile, "api-config-file", "", "supply an API config file to enable/disable parameters") daemonCmd.Flags().BoolVar(&enableAllParameters, "enable-all-parameters", false, "override default configuration and enable all parameters. Can't be used with --api-config-file") + if !paramConfigEnableFlag { + daemonCmd.Flags().MarkHidden("api-config-file") + daemonCmd.Flags().MarkHidden("enable-all-parameters") + } viper.RegisterAlias("algod", "algod-data-dir") viper.RegisterAlias("algod-net", "algod-address") @@ -205,10 +195,28 @@ func makeOptions() (options api.ExtraOptions) { options.WriteTimeout = writeTimeout options.ReadTimeout = readTimeout - if enableAllParameters { - options.DisabledMapConfig = api.MakeDisabledMapConfig() - } else { - options.DisabledMapConfig = api.GetDefaultDisabledMapConfigForPostgres() + if paramConfigEnableFlag { + if enableAllParameters { + options.DisabledMapConfig = api.MakeDisabledMapConfig() + } else { + options.DisabledMapConfig = api.GetDefaultDisabledMapConfigForPostgres() + } + + if suppliedAPIConfigFile != "" { + swag, err := generated.GetSwagger() + if err != nil { + fmt.Fprintf(os.Stderr, "failed to get swagger: %v", err) + os.Exit(1) + } + + logger.Infof("supplied api configuration file located at: %s", suppliedAPIConfigFile) + potentialDisabledMapConfig, err := api.MakeDisabledMapConfigFromFile(swag, suppliedAPIConfigFile) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to created disabled map config from file: %v", err) + os.Exit(1) + } + options.DisabledMapConfig = potentialDisabledMapConfig + } } return diff --git a/cmd/algorand-indexer/main.go b/cmd/algorand-indexer/main.go index 7ca86e459..3dda5591e 100644 --- a/cmd/algorand-indexer/main.go +++ b/cmd/algorand-indexer/main.go @@ -137,7 +137,9 @@ func init() { rootCmd.AddCommand(importCmd) importCmd.Hidden = true rootCmd.AddCommand(daemonCmd) - rootCmd.AddCommand(apiConfigCmd) + if paramConfigEnableFlag { + rootCmd.AddCommand(apiConfigCmd) + } // Not applied globally to avoid adding to utility commands. addFlags := func(cmd *cobra.Command) { From c30d7fea14b8b329eb54bd129fc79ab3a87b2ad6 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Wed, 9 Mar 2022 17:36:21 -0500 Subject: [PATCH 2/3] Handle missing disable map --- api/disabled_parameters.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/disabled_parameters.go b/api/disabled_parameters.go index 3769e9708..42fb31426 100644 --- a/api/disabled_parameters.go +++ b/api/disabled_parameters.go @@ -411,6 +411,9 @@ func (dmc *DisabledMapConfig) validate(swag *openapi3.Swagger) error { // MakeDisabledMapFromOA3 Creates a new disabled map from an openapi3 definition func MakeDisabledMapFromOA3(swag *openapi3.Swagger, config *DisabledMapConfig) (*DisabledMap, error) { + if config == nil { + return nil, nil + } err := config.validate(swag) From badc5b2d51fcf74a542f075a7b913e4a00631216 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Wed, 9 Mar 2022 18:27:38 -0500 Subject: [PATCH 3/3] fix test --- api/disabled_parameters_test.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/api/disabled_parameters_test.go b/api/disabled_parameters_test.go index 4444b1b24..984ca52d1 100644 --- a/api/disabled_parameters_test.go +++ b/api/disabled_parameters_test.go @@ -77,7 +77,7 @@ func TestSchemaCheck(t *testing.T) { type testingStruct struct { name string ddm *DisplayDisabledMap - expectError string + expectError []string } tests := []testingStruct{ {"test param types - good", @@ -87,7 +87,7 @@ func TestSchemaCheck(t *testing.T) { "optional": {{"p3": "enabled"}}, }}, }, - "", + nil, }, {"test param types - bad required", @@ -97,7 +97,7 @@ func TestSchemaCheck(t *testing.T) { "optional": {{"p3": "enabled"}}, }}, }, - "required-FAKE", + []string{"required-FAKE"}, }, {"test param types - bad optional", @@ -107,7 +107,7 @@ func TestSchemaCheck(t *testing.T) { "optional-FAKE": {{"p3": "enabled"}}, }}, }, - "optional-FAKE", + []string{"optional-FAKE"}, }, {"test param types - bad both", @@ -117,7 +117,7 @@ func TestSchemaCheck(t *testing.T) { "optional-FAKE": {{"p3": "enabled"}}, }}, }, - "required-FAKE optional-FAKE", + []string{"required-FAKE", "optional-FAKE"}, }, {"test param status - good", @@ -127,7 +127,7 @@ func TestSchemaCheck(t *testing.T) { "optional": {{"p3": "enabled"}}, }}, }, - "", + nil, }, {"test param status - bad required", @@ -137,7 +137,7 @@ func TestSchemaCheck(t *testing.T) { "optional": {{"p3": "enabled"}}, }}, }, - "p2", + []string{"p2"}, }, {"test param status - bad optional", @@ -147,7 +147,7 @@ func TestSchemaCheck(t *testing.T) { "optional": {{"p3": "enabled-FAKE"}}, }}, }, - "p3", + []string{"p3"}, }, {"test param status - bad both", @@ -157,7 +157,7 @@ func TestSchemaCheck(t *testing.T) { "optional": {{"p3": "enabled-FAKE"}}, }}, }, - "p1", + []string{"p1"}, }, } @@ -165,9 +165,11 @@ func TestSchemaCheck(t *testing.T) { t.Run(test.name, func(t *testing.T) { err := test.ddm.validateSchema() - if test.expectError != "" { + if len(test.expectError) != 0 { require.Error(t, err) - require.Contains(t, err.Error(), test.expectError) + for _, str := range test.expectError { + require.Contains(t, err.Error(), str) + } } else { require.NoError(t, err) }