-
-
Notifications
You must be signed in to change notification settings - Fork 485
feat(api/v2): gate admin routes by feature + instance admin #2817
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
Open
tink-bot
wants to merge
5
commits into
main
Choose a base branch
from
feat-v2-admin-gate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+205
−17
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef53a34
feat(api/v2): gate admin routes by feature + instance admin
kolaente 53bb5f8
fix(api/v2): apply rate limit before the admin gate
kolaente 7333a41
test(api/v2): defer session close in admin webtest
kolaente c2eb4b6
test(api/v2): defer license reset in admin webtest
kolaente 78e38d9
test(api/v2): assert admin project id via structured json
kolaente 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Vikunja is a to-do list application to facilitate your life. | ||
| // Copyright 2018-present Vikunja and contributors. All rights reserved. | ||
| // | ||
| // This program 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. | ||
| // | ||
| // This program 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 this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| package apiv2 | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "code.vikunja.io/api/pkg/models" | ||
| "code.vikunja.io/api/pkg/web/handler" | ||
|
|
||
| "github.com/danielgtaylor/huma/v2" | ||
| ) | ||
|
|
||
| type adminProjectListBody struct { | ||
| Body Paginated[*models.Project] | ||
| } | ||
|
|
||
| // Permissions are enforced by the gateV2AdminRoutes path middleware, not per-handler. | ||
| func RegisterAdminProjectRoutes(api huma.API) { | ||
| tags := []string{"admin"} | ||
|
|
||
| Register(api, huma.Operation{ | ||
| OperationID: "admin-projects-list", | ||
| Summary: "List all projects (admin)", | ||
| Description: "Returns every project on the instance, including archived ones and projects the caller does not own. Restricted to instance admins on a licensed instance; unlicensed or non-admin callers get a 404, making the endpoint indistinguishable from one that is not registered.", | ||
| Method: http.MethodGet, | ||
| Path: "/admin/projects", | ||
| Tags: tags, | ||
| }, adminProjectsList) | ||
| } | ||
|
|
||
| func adminProjectsList(ctx context.Context, in *ListParams) (*adminProjectListBody, error) { | ||
| a, err := authFromCtx(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| result, _, total, err := handler.DoReadAll(ctx, &models.AdminProjectList{}, a, in.Q, in.Page, in.PerPage) | ||
| if err != nil { | ||
| return nil, translateDomainError(err) | ||
| } | ||
| items, ok := result.([]*models.Project) | ||
| if !ok { | ||
| return nil, fmt.Errorf("AdminProjectList.ReadAll returned unexpected type %T (expected []*models.Project)", result) | ||
| } | ||
| return &adminProjectListBody{Body: NewPaginated(items, total, in.Page, in.PerPage)}, nil | ||
| } |
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,103 @@ | ||
| // Vikunja is a to-do list application to facilitate your life. | ||
| // Copyright 2018-present Vikunja and contributors. All rights reserved. | ||
| // | ||
| // This program 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. | ||
| // | ||
| // This program 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 this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| package webtests | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "code.vikunja.io/api/pkg/db" | ||
| "code.vikunja.io/api/pkg/license" | ||
| "code.vikunja.io/api/pkg/user" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // The error body shape is covered by TestHuma_ErrorShapeIsRFC9457; this test | ||
| // only asserts gate status codes (404 on failure, matching v1). | ||
| func TestHumaAdminProjects(t *testing.T) { | ||
| t.Run("non-admin user gets 404", func(t *testing.T) { | ||
| e, err := setupTestEnv() | ||
| require.NoError(t, err) | ||
| license.SetForTests([]license.Feature{license.FeatureAdminPanel}) | ||
| defer license.ResetForTests() | ||
|
|
||
| s := db.NewSession() | ||
| defer s.Close() | ||
| u, err := user.GetUserByID(s, 1) | ||
| require.NoError(t, err) | ||
| require.False(t, u.IsAdmin, "fixture precondition: user1 is not an admin") | ||
|
|
||
| res := adminReq(t, e, http.MethodGet, "/api/v2/admin/projects", u, "") | ||
| assert.Equal(t, http.StatusNotFound, res.Code) | ||
| }) | ||
|
|
||
| t.Run("admin without the feature gets 404", func(t *testing.T) { | ||
| e, err := setupTestEnv() | ||
| require.NoError(t, err) | ||
| // Empty feature set = licensed instance without the admin feature. | ||
| license.SetForTests([]license.Feature{}) | ||
| defer license.ResetForTests() | ||
|
|
||
| admin := promoteToAdmin(t, 1) | ||
|
|
||
| res := adminReq(t, e, http.MethodGet, "/api/v2/admin/projects", admin, "") | ||
| assert.Equal(t, http.StatusNotFound, res.Code) | ||
| }) | ||
|
|
||
| t.Run("admin with the feature sees every project", func(t *testing.T) { | ||
| e, err := setupTestEnv() | ||
| require.NoError(t, err) | ||
| license.SetForTests([]license.Feature{license.FeatureAdminPanel}) | ||
| defer license.ResetForTests() | ||
|
|
||
| admin := promoteToAdmin(t, 1) | ||
|
|
||
| res := adminReq(t, e, http.MethodGet, "/api/v2/admin/projects", admin, "") | ||
| require.Equal(t, http.StatusOK, res.Code, res.Body.String()) | ||
|
|
||
| var envelope struct { | ||
| Items []struct { | ||
| ID int64 `json:"id"` | ||
| } `json:"items"` | ||
| Total int64 `json:"total"` | ||
| } | ||
| require.NoError(t, json.Unmarshal(res.Body.Bytes(), &envelope)) | ||
|
|
||
| ids := make(map[int64]bool, len(envelope.Items)) | ||
| for _, item := range envelope.Items { | ||
| ids[item.ID] = true | ||
| } | ||
| // Project 6 (owned by user6, not shared with user1) proves the list ignores ownership. | ||
| assert.True(t, ids[6], "expected project 6 in the admin list, got items %v", ids) | ||
| // Project 22 is archived, proving the list includes archived projects. | ||
| assert.True(t, ids[22], "expected archived project 22 in the admin list, got items %v", ids) | ||
| }) | ||
|
|
||
| t.Run("unauthenticated caller gets 401", func(t *testing.T) { | ||
| e, err := setupTestEnv() | ||
| require.NoError(t, err) | ||
| license.SetForTests([]license.Feature{license.FeatureAdminPanel}) | ||
| defer license.ResetForTests() | ||
|
|
||
| // The token middleware rejects with 401 before the gate runs, matching v1. | ||
| res := adminReq(t, e, http.MethodGet, "/api/v2/admin/projects", nil, "") | ||
| assert.Equal(t, http.StatusUnauthorized, res.Code) | ||
| }) | ||
| } | ||
Oops, something went wrong.
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.