-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Feat/actions token permissions #36113
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
SBALAVIGNESH123
wants to merge
25
commits into
go-gitea:main
Choose a base branch
from
SBALAVIGNESH123:feat/actions-token-permissions
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.
+2,251
−63
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
4b8cf42
docs: add notes on actions permissions proposal
SBALAVIGNESH123 9b476f8
feat(db): initial migration for actions permissions
SBALAVIGNESH123 659cb87
feat(models): add permission configuration models
SBALAVIGNESH123 713ddeb
feat(models): add cross-repo access and package linking
SBALAVIGNESH123 b29204c
wip: working on permission checking logic
SBALAVIGNESH123 c2465f9
test: add unit tests for permission checker
SBALAVIGNESH123 bddccc2
feat(api): add repository permissions endpoints
SBALAVIGNESH123 2420536
feat(api): add organization permissions endpoints
SBALAVIGNESH123 03b3af4
feat(ui): add repository permissions settings page
SBALAVIGNESH123 4c794c6
test: add integration tests for permissions API
SBALAVIGNESH123 4cf5510
fix: register migration and correct imports
SBALAVIGNESH123 5ef7c05
fix: use correct API context methods for org ownership checks
SBALAVIGNESH123 e491ceb
fix: replace all ctx.Org.IsOwner with proper IsOwnedBy method
SBALAVIGNESH123 e4a1061
docs: add swagger annotations to API structs
SBALAVIGNESH123 b0d693f
fix: markdown linting and complete all remaining fixes
SBALAVIGNESH123 3aa0c6f
fix: use APIErrorInternal for internal server errors
SBALAVIGNESH123 442f74c
refactor: remove duplicate permission checks and use middleware
SBALAVIGNESH123 a498e10
fix: add OrgAssignment middleware to populate org context
SBALAVIGNESH123 349a1a7
fix: apply gofumpt formatting and clean up comments
SBALAVIGNESH123 a7b8046
style: apply gofumpt formatting
SBALAVIGNESH123 1ef6e06
docs: regenerate swagger spec and fix comment syntax
SBALAVIGNESH123 0b63809
Merge branch 'main' into feat/actions-token-permissions
SBALAVIGNESH123 c920124
Merge branch 'main' into feat/actions-token-permissions
SBALAVIGNESH123 1691e75
refactor: move actions permissions JS to separate file for CSP compli…
SBALAVIGNESH123 aa8e4ff
Merge branch 'main' into feat/actions-token-permissions
SBALAVIGNESH123 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Actions Permissions Implementation Notes | ||
|
|
||
| Reading through #24635 and related PRs. | ||
| Need to understand why #23729 and #24554 were rejected. | ||
|
|
||
| Key points: | ||
| - Security first | ||
| - Org/repo boundaries | ||
| - No blanket permissions |
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,226 @@ | ||
| // Copyright 2024 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package actions | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "code.gitea.io/gitea/models/db" | ||
| "code.gitea.io/gitea/modules/timeutil" | ||
| ) | ||
|
|
||
| // PermissionMode represents the permission configuration mode | ||
| type PermissionMode int | ||
|
|
||
| const ( | ||
| // PermissionModeRestricted - minimal permissions (default, secure) | ||
| PermissionModeRestricted PermissionMode = 0 | ||
|
|
||
| // PermissionModePermissive - broad permissions (convenience) | ||
| PermissionModePermissive PermissionMode = 1 | ||
|
|
||
| // PermissionModeCustom - user-defined permissions | ||
| PermissionModeCustom PermissionMode = 2 | ||
| ) | ||
|
|
||
| // ActionTokenPermission represents repository-level Actions token permissions | ||
| type ActionTokenPermission struct { | ||
| ID int64 `xorm:"pk autoincr"` | ||
| RepoID int64 `xorm:"UNIQUE NOT NULL"` | ||
|
|
||
| PermissionMode PermissionMode `xorm:"NOT NULL DEFAULT 0"` | ||
|
|
||
| // Granular permissions (only used in Custom mode) | ||
| ActionsRead bool `xorm:"NOT NULL DEFAULT false"` | ||
| ActionsWrite bool `xorm:"NOT NULL DEFAULT false"` | ||
| ContentsRead bool `xorm:"NOT NULL DEFAULT true"` | ||
| ContentsWrite bool `xorm:"NOT NULL DEFAULT false"` | ||
| IssuesRead bool `xorm:"NOT NULL DEFAULT false"` | ||
| IssuesWrite bool `xorm:"NOT NULL DEFAULT false"` | ||
| PackagesRead bool `xorm:"NOT NULL DEFAULT false"` | ||
| PackagesWrite bool `xorm:"NOT NULL DEFAULT false"` | ||
| PullRequestsRead bool `xorm:"NOT NULL DEFAULT false"` | ||
| PullRequestsWrite bool `xorm:"NOT NULL DEFAULT false"` | ||
| MetadataRead bool `xorm:"NOT NULL DEFAULT true"` | ||
|
|
||
| CreatedUnix timeutil.TimeStamp `xorm:"created"` | ||
| UpdatedUnix timeutil.TimeStamp `xorm:"updated"` | ||
| } | ||
|
|
||
| // ActionOrgPermission represents organization-level Actions token permissions | ||
| type ActionOrgPermission struct { | ||
| ID int64 `xorm:"pk autoincr"` | ||
| OrgID int64 `xorm:"UNIQUE NOT NULL"` | ||
|
|
||
| PermissionMode PermissionMode `xorm:"NOT NULL DEFAULT 0"` | ||
| AllowRepoOverride bool `xorm:"NOT NULL DEFAULT true"` | ||
|
|
||
| // Granular permissions (only used in Custom mode) | ||
| ActionsRead bool `xorm:"NOT NULL DEFAULT false"` | ||
| ActionsWrite bool `xorm:"NOT NULL DEFAULT false"` | ||
| ContentsRead bool `xorm:"NOT NULL DEFAULT true"` | ||
| ContentsWrite bool `xorm:"NOT NULL DEFAULT false"` | ||
| IssuesRead bool `xorm:"NOT NULL DEFAULT false"` | ||
| IssuesWrite bool `xorm:"NOT NULL DEFAULT false"` | ||
| PackagesRead bool `xorm:"NOT NULL DEFAULT false"` | ||
| PackagesWrite bool `xorm:"NOT NULL DEFAULT false"` | ||
| PullRequestsRead bool `xorm:"NOT NULL DEFAULT false"` | ||
| PullRequestsWrite bool `xorm:"NOT NULL DEFAULT false"` | ||
| MetadataRead bool `xorm:"NOT NULL DEFAULT true"` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand the purpose of |
||
|
|
||
| CreatedUnix timeutil.TimeStamp `xorm:"created"` | ||
| UpdatedUnix timeutil.TimeStamp `xorm:"updated"` | ||
| } | ||
|
|
||
| func init() { | ||
| db.RegisterModel(new(ActionTokenPermission)) | ||
| db.RegisterModel(new(ActionOrgPermission)) | ||
| } | ||
|
|
||
| // GetRepoActionPermissions retrieves the Actions permissions for a repository | ||
| // If no configuration exists, returns nil (will use defaults) | ||
| func GetRepoActionPermissions(ctx context.Context, repoID int64) (*ActionTokenPermission, error) { | ||
| perm := &ActionTokenPermission{RepoID: repoID} | ||
| has, err := db.GetEngine(ctx).Get(perm) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if !has { | ||
| return nil, nil // No custom config, will use defaults | ||
| } | ||
| return perm, nil | ||
| } | ||
|
|
||
| // GetOrgActionPermissions retrieves the Actions permissions for an organization | ||
| func GetOrgActionPermissions(ctx context.Context, orgID int64) (*ActionOrgPermission, error) { | ||
| perm := &ActionOrgPermission{OrgID: orgID} | ||
| has, err := db.GetEngine(ctx).Get(perm) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if !has { | ||
| return nil, nil // No custom config, will use defaults | ||
| } | ||
| return perm, nil | ||
| } | ||
|
|
||
| // CreateOrUpdateRepoPermissions creates or updates repository-level permissions | ||
| func CreateOrUpdateRepoPermissions(ctx context.Context, perm *ActionTokenPermission) error { | ||
| existing := &ActionTokenPermission{RepoID: perm.RepoID} | ||
| has, err := db.GetEngine(ctx).Get(existing) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if has { | ||
| // Update existing | ||
| perm.ID = existing.ID | ||
| perm.CreatedUnix = existing.CreatedUnix | ||
| _, err = db.GetEngine(ctx).ID(perm.ID).Update(perm) | ||
| return err | ||
| } | ||
|
|
||
| // Create new | ||
| _, err = db.GetEngine(ctx).Insert(perm) | ||
| return err | ||
| } | ||
|
|
||
| // CreateOrUpdateOrgPermissions creates or updates organization-level permissions | ||
| func CreateOrUpdateOrgPermissions(ctx context.Context, perm *ActionOrgPermission) error { | ||
| existing := &ActionOrgPermission{OrgID: perm.OrgID} | ||
| has, err := db.GetEngine(ctx).Get(existing) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if has { | ||
| // Update existing | ||
| perm.ID = existing.ID | ||
| perm.CreatedUnix = existing.CreatedUnix | ||
| _, err = db.GetEngine(ctx).ID(perm.ID).Update(perm) | ||
| return err | ||
| } | ||
|
|
||
| // Create new | ||
| _, err = db.GetEngine(ctx).Insert(perm) | ||
| return err | ||
| } | ||
|
|
||
| // ToPermissionMap converts permission struct to a map for easy access | ||
| func (p *ActionTokenPermission) ToPermissionMap() map[string]map[string]bool { | ||
| // Apply permission mode defaults | ||
| var perms map[string]map[string]bool | ||
|
|
||
| switch p.PermissionMode { | ||
| case PermissionModeRestricted: | ||
| // Minimal permissions - only read metadata and contents | ||
| perms = map[string]map[string]bool{ | ||
| "actions": {"read": false, "write": false}, | ||
| "contents": {"read": true, "write": false}, | ||
| "issues": {"read": false, "write": false}, | ||
| "packages": {"read": false, "write": false}, | ||
| "pull_requests": {"read": false, "write": false}, | ||
| "metadata": {"read": true, "write": false}, | ||
| } | ||
| case PermissionModePermissive: | ||
| // Broad permissions - read/write for most things | ||
| perms = map[string]map[string]bool{ | ||
| "actions": {"read": true, "write": true}, | ||
| "contents": {"read": true, "write": true}, | ||
| "issues": {"read": true, "write": true}, | ||
| "packages": {"read": true, "write": true}, | ||
| "pull_requests": {"read": true, "write": true}, | ||
| "metadata": {"read": true, "write": false}, | ||
| } | ||
| case PermissionModeCustom: | ||
| // Use explicitly set permissions | ||
| perms = map[string]map[string]bool{ | ||
| "actions": {"read": p.ActionsRead, "write": p.ActionsWrite}, | ||
| "contents": {"read": p.ContentsRead, "write": p.ContentsWrite}, | ||
| "issues": {"read": p.IssuesRead, "write": p.IssuesWrite}, | ||
| "packages": {"read": p.PackagesRead, "write": p.PackagesWrite}, | ||
| "pull_requests": {"read": p.PullRequestsRead, "write": p.PullRequestsWrite}, | ||
| "metadata": {"read": p.MetadataRead, "write": false}, | ||
| } | ||
| } | ||
|
|
||
| return perms | ||
| } | ||
|
|
||
| // ToPermissionMap converts org permission struct to a map | ||
| func (p *ActionOrgPermission) ToPermissionMap() map[string]map[string]bool { | ||
| var perms map[string]map[string]bool | ||
|
|
||
| switch p.PermissionMode { | ||
| case PermissionModeRestricted: | ||
| perms = map[string]map[string]bool{ | ||
| "actions": {"read": false, "write": false}, | ||
| "contents": {"read": true, "write": false}, | ||
| "issues": {"read": false, "write": false}, | ||
| "packages": {"read": false, "write": false}, | ||
| "pull_requests": {"read": false, "write": false}, | ||
| "metadata": {"read": true, "write": false}, | ||
| } | ||
| case PermissionModePermissive: | ||
| perms = map[string]map[string]bool{ | ||
| "actions": {"read": true, "write": true}, | ||
| "contents": {"read": true, "write": true}, | ||
| "issues": {"read": true, "write": true}, | ||
| "packages": {"read": true, "write": true}, | ||
| "pull_requests": {"read": true, "write": true}, | ||
| "metadata": {"read": true, "write": false}, | ||
| } | ||
| case PermissionModeCustom: | ||
| perms = map[string]map[string]bool{ | ||
| "actions": {"read": p.ActionsRead, "write": p.ActionsWrite}, | ||
| "contents": {"read": p.ContentsRead, "write": p.ContentsWrite}, | ||
| "issues": {"read": p.IssuesRead, "write": p.IssuesWrite}, | ||
| "packages": {"read": p.PackagesRead, "write": p.PackagesWrite}, | ||
| "pull_requests": {"read": p.PullRequestsRead, "write": p.PullRequestsWrite}, | ||
| "metadata": {"read": p.MetadataRead, "write": false}, | ||
| } | ||
| } | ||
|
|
||
| return perms | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these fields be replaced by
unit.Typeandperm.AccessMode? Just likeTeamUnitgitea/models/organization/team_unit.go
Lines 14 to 21 in ebf9b4d