From 97143e585d801d48df824da948d99dbd0ab9d4cf Mon Sep 17 00:00:00 2001 From: Tsachi Herman Date: Mon, 22 Jun 2020 15:44:19 -0400 Subject: [PATCH] Add EnableDeveloperAPI configuration flag (#1191) Add a local config to disable teal/compile in algod by default. --- config/config.go | 4 ++++ config/local_defaults.go | 1 + daemon/algod/api/server/v2/handlers.go | 5 +++++ daemon/algod/api/server/v2/test/handlers_test.go | 10 ++++++---- daemon/algod/api/server/v2/test/helpers.go | 5 +++-- installer/config.json.example | 1 + 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/config/config.go b/config/config.go index eb83f13b33..8a9084c31e 100644 --- a/config/config.go +++ b/config/config.go @@ -317,6 +317,10 @@ type Local struct { // CatchupLedgerDownloadRetryAttempts controls the number of attempt the block fetching would be attempted before giving up catching up to the provided catchpoint. CatchupBlockDownloadRetryAttempts int `version[9]:"1000"` + + // EnableDeveloperAPI enables teal/compile, teal/dryrun API endpoints. + // This functionlity is disabled by default. + EnableDeveloperAPI bool `version[9]:"false"` } // Filenames of config files within the configdir (e.g. ~/.algorand) diff --git a/config/local_defaults.go b/config/local_defaults.go index 11624bd49f..9dc909a8f3 100644 --- a/config/local_defaults.go +++ b/config/local_defaults.go @@ -44,6 +44,7 @@ var defaultLocal = Local{ EnableAgreementTimeMetrics: false, EnableAssembleStats: false, EnableBlockService: false, + EnableDeveloperAPI: false, EnableGossipBlockService: true, EnableIncomingMessageFilter: false, EnableLedgerService: false, diff --git a/daemon/algod/api/server/v2/handlers.go b/daemon/algod/api/server/v2/handlers.go index ab06adc081..a0f251dd8a 100644 --- a/daemon/algod/api/server/v2/handlers.go +++ b/daemon/algod/api/server/v2/handlers.go @@ -64,6 +64,7 @@ type NodeInterface interface { SuggestedFee() basics.MicroAlgos StartCatchup(catchpoint string) error AbortCatchup(catchpoint string) error + Config() config.Local } // RegisterParticipationKeys registers participation keys. @@ -589,6 +590,10 @@ func (v2 *Handlers) AbortCatchup(ctx echo.Context, catchpoint string) error { // TealCompile compiles TEAL code to binary, return both binary and hash // (POST /v2/teal/compile) func (v2 *Handlers) TealCompile(ctx echo.Context) error { + // return early if teal compile is not allowed in node config + if !v2.Node.Config().EnableDeveloperAPI { + return ctx.String(http.StatusNotFound, "/teal/compile was not enabled in the configuration file by setting the EnableDeveloperAPI to true") + } buf := new(bytes.Buffer) ctx.Request().Body = http.MaxBytesReader(nil, ctx.Request().Body, maxTealSourceBytes) buf.ReadFrom(ctx.Request().Body) diff --git a/daemon/algod/api/server/v2/test/handlers_test.go b/daemon/algod/api/server/v2/test/handlers_test.go index 51f6f716a3..6f023fdf0f 100644 --- a/daemon/algod/api/server/v2/test/handlers_test.go +++ b/daemon/algod/api/server/v2/test/handlers_test.go @@ -293,7 +293,7 @@ func TestAbortCatchup(t *testing.T) { abortCatchupTest(t, badCatchPoint, 400) } -func tealCompileTest(t *testing.T, bytesToUse []byte, expectedCode int) { +func tealCompileTest(t *testing.T, bytesToUse []byte, expectedCode int, enableDeveloperAPI bool) { numAccounts := 1 numTransactions := 1 offlineAccounts := true @@ -301,6 +301,7 @@ func tealCompileTest(t *testing.T, bytesToUse []byte, expectedCode int) { defer releasefunc() dummyShutdownChan := make(chan struct{}) mockNode := makeMockNode(mockLedger, t.Name()) + mockNode.config.EnableDeveloperAPI = enableDeveloperAPI handler := v2.Handlers{ Node: &mockNode, Log: logging.Base(), @@ -316,11 +317,12 @@ func tealCompileTest(t *testing.T, bytesToUse []byte, expectedCode int) { } func TestTealCompile(t *testing.T) { - tealCompileTest(t, nil, 200) // nil program should work + tealCompileTest(t, nil, 200, true) // nil program should work goodProgram := `int 1` goodProgramBytes := []byte(goodProgram) - tealCompileTest(t, goodProgramBytes, 200) + tealCompileTest(t, goodProgramBytes, 200, true) + tealCompileTest(t, goodProgramBytes, 404, false) badProgram := "bad program" badProgramBytes := []byte(badProgram) - tealCompileTest(t, badProgramBytes, 400) + tealCompileTest(t, badProgramBytes, 400, true) } diff --git a/daemon/algod/api/server/v2/test/helpers.go b/daemon/algod/api/server/v2/test/helpers.go index 3d017acaff..4617da6d8c 100644 --- a/daemon/algod/api/server/v2/test/helpers.go +++ b/daemon/algod/api/server/v2/test/helpers.go @@ -74,10 +74,11 @@ var poolAddrResponseGolden = generatedV2.AccountResponse{ type mockNode struct { ledger *data.Ledger genesisID string + config config.Local } func makeMockNode(ledger *data.Ledger, genesisID string) mockNode { - return mockNode{ledger: ledger, genesisID: genesisID} + return mockNode{ledger: ledger, genesisID: genesisID, config: config.GetDefaultLocal()} } func (m mockNode) Ledger() *data.Ledger { @@ -116,7 +117,7 @@ func (m mockNode) SuggestedFee() basics.MicroAlgos { // unused by handlers: func (m mockNode) Config() config.Local { - return config.GetDefaultLocal() + return m.config } func (m mockNode) Start() {} diff --git a/installer/config.json.example b/installer/config.json.example index 0c156580fe..4eb867ed76 100644 --- a/installer/config.json.example +++ b/installer/config.json.example @@ -23,6 +23,7 @@ "EnableAgreementTimeMetrics": false, "EnableAssembleStats": false, "EnableBlockService": false, + "EnableDeveloperAPI": false, "EnableGossipBlockService": true, "EnableIncomingMessageFilter": false, "EnableLedgerService": false,