-
Notifications
You must be signed in to change notification settings - Fork 523
api: Add support for Private Network Access header preflight requests #6089
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
88d1853
Allow Private Network Access requests on preflight
nullun e22248b
Allow PNA preflight requests on KMD
nullun 7f7301c
Add config option and initial tests
nullun d26fc33
Add config option for KMD
nullun 609290a
gofmt and add comment to exported function
nullun 0aba646
Use a PNA specific config for Cors tests
nullun b0321e8
Shortened KMD's PNA config name
nullun da187c2
Tests added for CORS/PNA
nullun 9be9309
added license headed
nullun 53d2216
fix partitiontest linter warnings
cce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // Copyright (C) 2019-2024 Algorand, Inc. | ||
| // This file is part of go-algorand | ||
| // | ||
| // go-algorand is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // go-algorand is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with go-algorand. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| package middlewares | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/algorand/go-algorand/test/partitiontest" | ||
| "github.com/labstack/echo/v4" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestMakeCORS(t *testing.T) { | ||
| partitiontest.PartitionTest(t) | ||
| e := echo.New() | ||
| tokenHeader := "X-Algo-API-Token" | ||
| corsMiddleware := MakeCORS(tokenHeader) | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| method string | ||
| headers map[string]string | ||
| expectedStatus int | ||
| expectedHeaders map[string]string | ||
| }{ | ||
| { | ||
| name: "OPTIONS request", | ||
| method: http.MethodOptions, | ||
| headers: map[string]string{ | ||
| "Origin": "http://algorand.com", | ||
| "Access-Control-Request-Headers": "Content-Type," + tokenHeader, | ||
| }, | ||
| expectedStatus: http.StatusNoContent, | ||
| expectedHeaders: map[string]string{ | ||
| "Access-Control-Allow-Origin": "*", | ||
| "Access-Control-Allow-Methods": "GET,POST,PUT,DELETE,OPTIONS", | ||
| "Access-Control-Allow-Headers": tokenHeader + ",Content-Type", | ||
| }, | ||
| }, | ||
| { | ||
| name: "GET request", | ||
| method: http.MethodGet, | ||
| headers: map[string]string{ | ||
| "Origin": "http://algorand.com", | ||
| }, | ||
| expectedStatus: http.StatusOK, | ||
| expectedHeaders: map[string]string{ | ||
| "Access-Control-Allow-Origin": "*", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| req := httptest.NewRequest(tc.method, "/health", nil) | ||
| for key, value := range tc.headers { | ||
| req.Header.Set(key, value) | ||
| } | ||
| rec := httptest.NewRecorder() | ||
| c := e.NewContext(req, rec) | ||
|
|
||
| handler := corsMiddleware(func(c echo.Context) error { | ||
| return c.NoContent(http.StatusOK) | ||
| }) | ||
|
|
||
| err := handler(c) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, tc.expectedStatus, rec.Code) | ||
| for key, value := range tc.expectedHeaders { | ||
| assert.Equal(t, value, rec.Header().Get(key)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestMakePNA(t *testing.T) { | ||
| partitiontest.PartitionTest(t) | ||
| e := echo.New() | ||
| pnaMiddleware := MakePNA() | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| method string | ||
| headers map[string]string | ||
| expectedStatusCode int | ||
| expectedHeader string | ||
| }{ | ||
| { | ||
| name: "OPTIONS request with PNA header", | ||
| method: http.MethodOptions, | ||
| headers: map[string]string{"Access-Control-Request-Private-Network": "true"}, | ||
| expectedStatusCode: http.StatusOK, | ||
| expectedHeader: "true", | ||
| }, | ||
| { | ||
| name: "OPTIONS request without PNA header", | ||
| method: http.MethodOptions, | ||
| headers: map[string]string{}, | ||
| expectedStatusCode: http.StatusOK, | ||
| expectedHeader: "", | ||
| }, | ||
| { | ||
| name: "GET request", | ||
| method: http.MethodGet, | ||
| headers: map[string]string{}, | ||
| expectedStatusCode: http.StatusOK, | ||
| expectedHeader: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| req := httptest.NewRequest(tc.method, "/", nil) | ||
| for key, value := range tc.headers { | ||
| req.Header.Set(key, value) | ||
| } | ||
| rec := httptest.NewRecorder() | ||
| c := e.NewContext(req, rec) | ||
|
|
||
| handler := pnaMiddleware(func(c echo.Context) error { | ||
| return c.NoContent(http.StatusOK) | ||
| }) | ||
|
|
||
| err := handler(c) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, tc.expectedStatusCode, rec.Code) | ||
| assert.Equal(t, tc.expectedHeader, rec.Header().Get("Access-Control-Allow-Private-Network")) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| { | ||
| "Genesis": { | ||
| "NetworkName": "tbd", | ||
| "LastPartKeyRound": 3000, | ||
| "Wallets": [ | ||
| { | ||
| "Name": "Wallet1", | ||
| "Stake": 50, | ||
| "Online": true | ||
| }, | ||
| { | ||
| "Name": "Wallet2", | ||
| "Stake": 50, | ||
| "Online": true | ||
| } | ||
| ] | ||
| }, | ||
| "Nodes": [ | ||
| { | ||
| "Name": "Primary", | ||
| "IsRelay": true, | ||
| "ConfigJSONOverride": "{\"EnablePrivateNetworkAccessHeader\":true}", | ||
| "Wallets": [ | ||
| { "Name": "Wallet1", | ||
| "ParticipationOnly": false } | ||
| ] | ||
| }, | ||
| { | ||
| "Name": "Node", | ||
| "ConfigJSONOverride": "{\"EnablePrivateNetworkAccessHeader\":true}", | ||
| "Wallets": [ | ||
| { "Name": "Wallet2", | ||
| "ParticipationOnly": false } | ||
| ] | ||
| } | ||
| ] | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.