Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rest api: Fix to Disable API authentication #6067

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions daemon/algod/api/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

import (
"fmt"
"golang.org/x/sync/semaphore"
"net"
"net/http"

"golang.org/x/sync/semaphore"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"

Expand Down Expand Up @@ -74,18 +75,26 @@

// NewRouter builds and returns a new router with our REST handlers registered.
func NewRouter(logger logging.Logger, node APINodeInterface, shutdown <-chan struct{}, apiToken string, adminAPIToken string, listener net.Listener, numConnectionsLimit uint64) *echo.Echo {
if err := tokens.ValidateAPIToken(apiToken); err != nil {
logger.Errorf("Invalid apiToken was passed to NewRouter ('%s'): %v", apiToken, err)
}
// check admin token and init admin middleware
if err := tokens.ValidateAPIToken(adminAPIToken); err != nil {
logger.Errorf("Invalid adminAPIToken was passed to NewRouter ('%s'): %v", adminAPIToken, err)
}
adminMiddleware := []echo.MiddlewareFunc{
middlewares.MakeAuth(TokenHeader, []string{adminAPIToken}),
}

// check public api tokens and init public middleware
publicMiddleware := []echo.MiddlewareFunc{
middleware.BodyLimit(MaxRequestBodyBytes),
middlewares.MakeAuth(TokenHeader, []string{adminAPIToken, apiToken}),
}
if apiToken == "" {
logger.Warn("Running with empty apiToken")
algorandskiy marked this conversation as resolved.
Show resolved Hide resolved
} else {
if err := tokens.ValidateAPIToken(apiToken); err != nil {
logger.Errorf("Invalid apiToken was passed to NewRouter ('%s'): %v", apiToken, err)

Check warning on line 94 in daemon/algod/api/server/router.go

View check run for this annotation

Codecov / codecov/patch

daemon/algod/api/server/router.go#L90-L94

Added lines #L90 - L94 were not covered by tests
gmalouf marked this conversation as resolved.
Show resolved Hide resolved
}
publicMiddleware = append(publicMiddleware, middlewares.MakeAuth(TokenHeader, []string{adminAPIToken, apiToken}))

Check warning on line 96 in daemon/algod/api/server/router.go

View check run for this annotation

Codecov / codecov/patch

daemon/algod/api/server/router.go#L96

Added line #L96 was not covered by tests

}

e := echo.New()
Expand Down
20 changes: 15 additions & 5 deletions test/e2e-go/restAPI/other/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func TestDisabledAPIConfig(t *testing.T) {
localFixture.Setup(t, filepath.Join("nettemplates", "DisableAPIAuth.json"))
defer localFixture.Shutdown()

testClient := localFixture.LibGoalClient
libgoalClient := localFixture.LibGoalClient

statusResponse, err := testClient.Status()
statusResponse, err := libgoalClient.Status()
a.NoError(err)
a.NotEmpty(statusResponse)
statusResponse2, err := testClient.Status()
statusResponse2, err := libgoalClient.Status()
a.NoError(err)
a.NotEmpty(statusResponse2)
a.True(statusResponse2.LastRound >= statusResponse.LastRound)
Expand All @@ -58,12 +58,22 @@ func TestDisabledAPIConfig(t *testing.T) {
assert.True(t, os.IsNotExist(err))

// check public api works without a token
testClient.WaitForRound(1)
url, err := localFixture.NC.ServerURL()
a.NoError(err)
testClient := client.MakeRestClient(url, "") // empty token

_, err = testClient.WaitForBlock(1)
assert.NoError(t, err)
_, err = testClient.Block(1)
assert.NoError(t, err)
_, err = testClient.Status()
a.NoError(err)

// check admin api works with the generated token
_, err = testClient.GetParticipationKeys()
adminClient := localFixture.LibGoalClient
algorandskiy marked this conversation as resolved.
Show resolved Hide resolved
_, err = adminClient.GetParticipationKeys()
algorandskiy marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)

// check admin api doesn't work with an invalid token
algodURL, err := nc.ServerURL()
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion test/framework/fixtures/libgoalFixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func (f *LibGoalFixture) dumpLogs(filePath string) {
fmt.Fprintf(os.Stderr, "%s/%s:\n", parts[len(parts)-2], parts[len(parts)-1]) // Primary/node.log
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Fprint(os.Stderr, scanner.Text())
fmt.Fprintln(os.Stderr, scanner.Text())
}
fmt.Fprintln(os.Stderr)
}
Expand Down
Loading