Files
gitea/routers/web/shared/actions/general.go
T
Max P. a4908d4af4 enhance(actions): add OIDC workload identity support (#39052)
Adds OpenID Connect workload identity to Gitea Actions: jobs declaring
`id-token: write` can request short-lived tokens to authenticate to
cloud providers and other services without stored secrets. Tokens follow
GitHub's format, so clients like `@actions/core` and existing trust
policies carry over.

- Issuer at `/api/actions/oidc` with discovery and JWKS, signed with a
dedicated RS256 key
- GitHub's claims, including `workflow_ref`, `job_workflow_ref` and the
immutable `repo:owner@id/repo@id:ref:...` subject
- `id-token: write` or `write-all` grant it, default permissions and
fork pull requests never get it, owner and repository maximum
permissions can disable it
- Reusable workflows record the exact ref they were loaded from, and
dispatch rejects refs that only resolve through a same-named tag
- Generated signing keys are written atomically, and generated ES384 and
ES512 OAuth2 keys use their own curves instead of P-256
- `github.ref_type` is `branch` for pull request runs, like on GitHub

Fixes https://github.com/go-gitea/gitea/issues/26383
Fixes https://github.com/go-gitea/gitea/issues/33681
Docs: https://gitea.com/gitea/docs/pulls/526
Assisted-by: ChatGPT:gpt-5.6-sol, Claude Code:claude-opus-5-5
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
2026-10-18 07:08:55 +00:00

162 lines
4.9 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package actions
import (
"net/http"
"slices"
"strconv"
actions_model "gitea.dev/models/actions"
"gitea.dev/models/perm"
repo_model "gitea.dev/models/repo"
"gitea.dev/models/unit"
"gitea.dev/modules/templates"
"gitea.dev/modules/util"
"gitea.dev/services/context"
)
const (
tplOrgSettingsActionsGeneral templates.TplName = "org/settings/actions_general"
tplUserSettingsActionsGeneral templates.TplName = "user/settings/actions_general"
)
// ParseMaxTokenPermissions parses the maximum token permissions from form values
func ParseMaxTokenPermissions(ctx *context.Context) *repo_model.ActionsTokenPermissions {
parseMaxPerm := func(unitType unit.Type) perm.AccessMode {
value := ctx.FormString("max_unit_access_mode_" + strconv.Itoa(int(unitType)))
switch value {
case "write":
return perm.AccessModeWrite
case "read":
return perm.AccessModeRead
default:
return perm.AccessModeNone
}
}
ret := new(repo_model.MakeActionsTokenPermissions(perm.AccessModeNone))
for _, ut := range repo_model.ActionsTokenUnitTypes {
ret.UnitAccessModes[ut] = parseMaxPerm(ut)
}
ret.IDToken = ctx.FormString("max_id_token") == "write"
return ret
}
// GeneralSettings renders the actions general settings page
func GeneralSettings(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("actions.actions")
rCtx, err := getRunnersCtx(ctx)
if err != nil {
ctx.ServerError("getRunnersCtx", err)
return
}
if rCtx.IsOrg {
ctx.Data["PageIsOrgSettings"] = true
ctx.Data["PageIsOrgSettingsActionsGeneral"] = true
} else if rCtx.IsUser {
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsUserSettingsActionsGeneral"] = true
} else {
ctx.NotFound(nil)
return
}
// Load User/Org Actions Config
actionsCfg, err := actions_model.GetOwnerActionsConfig(ctx, rCtx.OwnerID)
if err != nil {
ctx.ServerError("GetOwnerActionsConfig", err)
return
}
ctx.Data["TokenPermissionMode"] = actionsCfg.TokenPermissionMode
ctx.Data["TokenPermissionModePermissive"] = repo_model.ActionsTokenPermissionModePermissive
ctx.Data["TokenPermissionModeRestricted"] = repo_model.ActionsTokenPermissionModeRestricted
ctx.Data["MaxTokenPermissions"] = actionsCfg.MaxTokenPermissions
if actionsCfg.MaxTokenPermissions == nil {
ctx.Data["MaxTokenPermissions"] = (&repo_model.ActionsConfig{}).GetMaxTokenPermissions()
}
ctx.Data["EnableMaxTokenPermissions"] = actionsCfg.MaxTokenPermissions != nil
// Load Allowed Repositories
allowedRepos, err := repo_model.GetOwnerRepositoriesByIDs(ctx, rCtx.OwnerID, actionsCfg.AllowedCrossRepoIDs)
if err != nil {
ctx.ServerError("GetOwnerRepositoriesByIDs", err)
return
}
ctx.Data["AllowedRepos"] = allowedRepos
ctx.Data["OwnerID"] = rCtx.OwnerID
if rCtx.IsOrg {
ctx.HTML(http.StatusOK, tplOrgSettingsActionsGeneral)
} else {
ctx.HTML(http.StatusOK, tplUserSettingsActionsGeneral)
}
}
// UpdateGeneralSettings responses for actions general settings page
func UpdateGeneralSettings(ctx *context.Context) {
rCtx, err := getRunnersCtx(ctx)
if err != nil {
ctx.ServerError("getRunnersCtx", err)
return
}
if !rCtx.IsOrg && !rCtx.IsUser {
ctx.NotFound(nil)
return
}
actionsCfg, err := actions_model.GetOwnerActionsConfig(ctx, rCtx.OwnerID)
if err != nil {
ctx.ServerError("GetOwnerActionsConfig", err)
return
}
if ctx.FormBool("cross_repo_add_target") {
targetRepoName := ctx.FormString("cross_repo_add_target_name")
if targetRepoName != "" {
targetRepo, err := repo_model.GetRepositoryByName(ctx, rCtx.OwnerID, targetRepoName)
if err != nil {
if repo_model.IsErrRepoNotExist(err) {
ctx.JSONError("Repository doesn't exist")
return
}
ctx.ServerError("GetRepositoryByName", err)
return
}
if !slices.Contains(actionsCfg.AllowedCrossRepoIDs, targetRepo.ID) {
actionsCfg.AllowedCrossRepoIDs = append(actionsCfg.AllowedCrossRepoIDs, targetRepo.ID)
}
}
}
if crossRepoRemoveTargetID := ctx.FormInt64("cross_repo_remove_target_id"); crossRepoRemoveTargetID != 0 {
actionsCfg.AllowedCrossRepoIDs = util.SliceRemoveAll(actionsCfg.AllowedCrossRepoIDs, crossRepoRemoveTargetID)
}
// Update Token Permission Mode
tokenPermissionMode, tokenPermissionModeValid := util.EnumValue(repo_model.ActionsTokenPermissionMode(ctx.FormString("token_permission_mode")))
if tokenPermissionModeValid {
actionsCfg.TokenPermissionMode = tokenPermissionMode
enableMaxPermissions := ctx.FormBool("enable_max_permissions")
// Update Maximum Permissions (radio buttons: none/read/write)
if enableMaxPermissions {
actionsCfg.MaxTokenPermissions = ParseMaxTokenPermissions(ctx)
} else {
actionsCfg.MaxTokenPermissions = nil
}
}
if err := actions_model.SetOwnerActionsConfig(ctx, rCtx.OwnerID, actionsCfg); err != nil {
ctx.ServerError("SetOwnerActionsConfig", err)
return
}
ctx.Flash.Success(ctx.Tr("settings.saved_successfully"))
ctx.JSONRedirect("") // use JSONRedirect because frontend uses form-fetch-action
}