From a779453857b1d956ce5a78fc7d66f63011d092f2 Mon Sep 17 00:00:00 2001 From: bircni Date: Fri, 4 Dec 2026 08:06:17 +0000 Subject: [PATCH] enhance(audit): record Actions runner registration and enable/disable (#40634) Registering a runner was not recorded in the audit log, so a new machine that can pick up jobs and their secrets left no trace. Enabling and disabling a runner were both logged as a generic "update", and editing a runner's description was not recorded at all. This adds a `runner:register` event for every scope. It is attributed to the Gitea Actions system user and includes the runner's IP address. Enabling and disabling become separate `runner:enable` and `runner:disable` events, and `runner:update` now covers description edits. Runners removed automatically by cleanup are not recorded, because ephemeral runners would flood the log. --- models/actions/runner.go | 6 +++--- models/audit/action.go | 20 ++++++++++++++++---- routers/api/actions/actions.go | 3 +++ routers/api/actions/runner/runner.go | 4 ++++ routers/api/v1/shared/runners.go | 9 ++++++--- routers/web/shared/actions/runners.go | 26 +++++++++++++++++++------- services/audit/scoped.go | 26 ++++++++++++++++++++++++++ tests/integration/admin_audit_test.go | 27 ++++++++++++++++++++++++--- 8 files changed, 101 insertions(+), 20 deletions(-) diff --git a/models/actions/runner.go b/models/actions/runner.go index 60f280df21f..68a68345f47 100644 --- a/models/actions/runner.go +++ b/models/actions/runner.go @@ -344,12 +344,12 @@ func UpdateRunner(ctx context.Context, r *ActionRunner, cols ...string) error { return err } -func SetRunnerDisabled(ctx context.Context, runner *ActionRunner, isDisabled bool) error { +func SetRunnerDisabled(ctx context.Context, runner *ActionRunner, isDisabled bool) (changed bool, err error) { if runner.IsDisabled == isDisabled { - return nil + return false, nil } - return db.WithTx(ctx, func(ctx context.Context) error { + return true, db.WithTx(ctx, func(ctx context.Context) error { runner.IsDisabled = isDisabled if err := UpdateRunner(ctx, runner, "is_disabled"); err != nil { return err diff --git a/models/audit/action.go b/models/audit/action.go index 61e7c21fb84..6d139ac8657 100644 --- a/models/audit/action.go +++ b/models/audit/action.go @@ -111,7 +111,10 @@ var ( UserWebhookUpdate = define("user:webhook:update", "Updated webhook {webhook} of user {scope}.") UserWebhookRemove = define("user:webhook:remove", "Removed webhook {webhook} of user {scope}.") UserRunnerToken = define("user:runner:token", "Issued Actions runner registration token for user {scope}.") - UserRunnerUpdate = define("user:runner:update", "Updated Actions runner {runner} of user {scope}.") + UserRunnerRegister = define("user:runner:register", "Registered Actions runner {runner} for user {scope}.") + UserRunnerUpdate = define("user:runner:update", "Updated description of Actions runner {runner} of user {scope}.") + UserRunnerEnable = define("user:runner:enable", "Enabled Actions runner {runner} of user {scope}.") + UserRunnerDisable = define("user:runner:disable", "Disabled Actions runner {runner} of user {scope}.") UserRunnerRemove = define("user:runner:remove", "Removed Actions runner {runner} from user {scope}.") UserVariableAdd = define("user:variable:add", "Added variable {variable} to user {scope}.") UserVariableUpdate = define("user:variable:update", "Updated variable {variable} of user {scope}.") @@ -141,7 +144,10 @@ var ( OrganizationWebhookRemove = define("organization:webhook:remove", "Removed webhook {webhook} of organization {scope}.") OrganizationTeamAccess = define("organization:teamaccess:update", "Changed whether repository admins of organization {scope} can change team access to {repo_admin_change_team_access}.") OrganizationRunnerToken = define("organization:runner:token", "Issued Actions runner registration token for organization {scope}.") - OrganizationRunnerUpdate = define("organization:runner:update", "Updated Actions runner {runner} of organization {scope}.") + OrganizationRunnerRegister = define("organization:runner:register", "Registered Actions runner {runner} for organization {scope}.") + OrganizationRunnerUpdate = define("organization:runner:update", "Updated description of Actions runner {runner} of organization {scope}.") + OrganizationRunnerEnable = define("organization:runner:enable", "Enabled Actions runner {runner} of organization {scope}.") + OrganizationRunnerDisable = define("organization:runner:disable", "Disabled Actions runner {runner} of organization {scope}.") OrganizationRunnerRemove = define("organization:runner:remove", "Removed Actions runner {runner} from organization {scope}.") OrganizationVariableAdd = define("organization:variable:add", "Added variable {variable} to organization {scope}.") OrganizationVariableUpdate = define("organization:variable:update", "Updated variable {variable} of organization {scope}.") @@ -187,7 +193,10 @@ var ( RepositorySecretUpdate = define("repository:secret:update", "Updated secret {secret} of repository {scope}.") RepositorySecretRemove = define("repository:secret:remove", "Removed secret {secret} from repository {scope}.") RepositoryRunnerToken = define("repository:runner:token", "Issued Actions runner registration token for repository {scope}.") - RepositoryRunnerUpdate = define("repository:runner:update", "Updated Actions runner {runner} of repository {scope}.") + RepositoryRunnerRegister = define("repository:runner:register", "Registered Actions runner {runner} for repository {scope}.") + RepositoryRunnerUpdate = define("repository:runner:update", "Updated description of Actions runner {runner} of repository {scope}.") + RepositoryRunnerEnable = define("repository:runner:enable", "Enabled Actions runner {runner} of repository {scope}.") + RepositoryRunnerDisable = define("repository:runner:disable", "Disabled Actions runner {runner} of repository {scope}.") RepositoryRunnerRemove = define("repository:runner:remove", "Removed Actions runner {runner} from repository {scope}.") RepositoryVariableAdd = define("repository:variable:add", "Added variable {variable} to repository {scope}.") RepositoryVariableUpdate = define("repository:variable:update", "Updated variable {variable} of repository {scope}.") @@ -240,7 +249,10 @@ var ( SystemOAuth2ApplicationRemove = define("system:oauth2application:remove", "Removed instance-wide OAuth2 application {oauth2_application}.") SystemConfigUpdate = define("system:config:update", "Changed system setting {key}.") SystemRunnerToken = define("system:runner:token", "Issued instance-wide Actions runner registration token.") - SystemRunnerUpdate = define("system:runner:update", "Updated instance-wide Actions runner {runner}.") + SystemRunnerRegister = define("system:runner:register", "Registered instance-wide Actions runner {runner}.") + SystemRunnerUpdate = define("system:runner:update", "Updated description of instance-wide Actions runner {runner}.") + SystemRunnerEnable = define("system:runner:enable", "Enabled instance-wide Actions runner {runner}.") + SystemRunnerDisable = define("system:runner:disable", "Disabled instance-wide Actions runner {runner}.") SystemRunnerRemove = define("system:runner:remove", "Removed instance-wide Actions runner {runner}.") SystemVariableAdd = define("system:variable:add", "Added instance-wide variable {variable}.") SystemVariableUpdate = define("system:variable:update", "Updated instance-wide variable {variable}.") diff --git a/routers/api/actions/actions.go b/routers/api/actions/actions.go index 9082e71a7b9..708e9edf879 100644 --- a/routers/api/actions/actions.go +++ b/routers/api/actions/actions.go @@ -6,13 +6,16 @@ package actions import ( "net/http" + audit_model "gitea.dev/models/audit" "gitea.dev/modules/web" "gitea.dev/routers/api/actions/ping" "gitea.dev/routers/api/actions/runner" + "gitea.dev/routers/common" ) func Routes(prefix string) *web.Router { m := web.NewRouter() + m.AfterRouting(common.AuditOrigin(audit_model.OriginAPI)) m.Group("/oidc", func() { m.Get("/.well-known/openid-configuration", oidcConfiguration) m.Get("/jwks", oidcKeys) diff --git a/routers/api/actions/runner/runner.go b/routers/api/actions/runner/runner.go index fb3c4d16739..8c6b83f4c51 100644 --- a/routers/api/actions/runner/runner.go +++ b/routers/api/actions/runner/runner.go @@ -20,6 +20,7 @@ import ( "gitea.dev/modules/log" "gitea.dev/modules/util" actions_service "gitea.dev/services/actions" + "gitea.dev/services/audit" "connectrpc.com/connect/v2" "connectrpc.com/connect/v2/connecthttp" @@ -92,6 +93,9 @@ func (s *Service) Register( if err := actions_model.CreateRunner(ctx, runner); err != nil { return nil, errors.New("can't create new runner") } + // the registration token authenticates the runner, no user is involved + audit.RecordScopedByID(audit.WithDoer(ctx, user_model.NewActionsUser()), runner.OwnerID, runner.RepoID, audit.RunnerRegister, + "runner", runner.Name, "runner_id", runner.ID, "version", runner.Version, "ephemeral", runner.Ephemeral) res := &runnerv1.RegisterResponse{ Runner: &runnerv1.Runner{ diff --git a/routers/api/v1/shared/runners.go b/routers/api/v1/shared/runners.go index cd49b868ea8..eeee0f1083f 100644 --- a/routers/api/v1/shared/runners.go +++ b/routers/api/v1/shared/runners.go @@ -118,7 +118,7 @@ func DeleteRunner(ctx *context.APIContext, ownerID, repoID, runnerID int64) { ctx.APIErrorInternal(err) return } - audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerRemove, "runner", runner.Name) + audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerRemove, "runner", runner.Name, "runner_id", runner.ID) ctx.Status(http.StatusNoContent) } @@ -134,11 +134,14 @@ func UpdateRunner(ctx *context.APIContext, ownerID, repoID, runnerID int64) { return } - if err := actions_model.SetRunnerDisabled(ctx, runner, *form.Disabled); err != nil { + changed, err := actions_model.SetRunnerDisabled(ctx, runner, *form.Disabled) + if err != nil { ctx.APIErrorInternal(err) return } - audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerUpdate, "runner", runner.Name, "disabled", *form.Disabled) + if changed { + audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerToggle(*form.Disabled), "runner", runner.Name, "runner_id", runner.ID) + } GetRunner(ctx, ownerID, repoID, runnerID) } diff --git a/routers/web/shared/actions/runners.go b/routers/web/shared/actions/runners.go index 407274b84ec..dd02395933d 100644 --- a/routers/web/shared/actions/runners.go +++ b/routers/web/shared/actions/runners.go @@ -179,6 +179,7 @@ func RunnersEditPost(ctx *context.Context) { } form := web.GetForm[*forms.EditRunnerForm](ctx) + descriptionChanged := runner.Description != form.Description runner.Description = form.Description err = actions_model.UpdateRunner(ctx, runner, "description") @@ -188,6 +189,9 @@ func RunnersEditPost(ctx *context.Context) { ctx.Redirect(redirectTo) return } + if descriptionChanged { + audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerUpdate, "runner", runner.Name, "runner_id", runner.ID) + } log.Debug("RunnerDetailsEditPost success: %s", ctx.Req.URL) @@ -236,7 +240,7 @@ func RunnerDeletePost(ctx *context.Context) { return } - audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerRemove, "runner", runner.Name) + audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerRemove, "runner", runner.Name, "runner_id", runner.ID) log.Info("DeleteRunnerPost success: %s", ctx.Req.URL) ctx.Flash.Success(ctx.Tr("actions.runners.delete_runner_success")) @@ -269,13 +273,16 @@ func RunnerUpdatePost(ctx *context.Context) { failedKey = "actions.runners.disable_runner_failed" } - if err := actions_model.SetRunnerDisabled(ctx, runner, isDisabled.Value()); err != nil { + changed, err := actions_model.SetRunnerDisabled(ctx, runner, isDisabled.Value()) + if err != nil { log.Warn("RunnerUpdatePost.SetRunnerDisabled failed: %v, url: %s", err, ctx.Req.URL) ctx.Flash.Error(ctx.Tr(failedKey)) ctx.JSONRedirect("") return } - audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerUpdate, "runner", runner.Name, "disabled", isDisabled.Value()) + if changed { + audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerToggle(isDisabled.Value()), "runner", runner.Name, "runner_id", runner.ID) + } ctx.Flash.Success(ctx.Tr(successKey)) ctx.JSONRedirect("") @@ -324,17 +331,22 @@ func RunnerBulkActionPost(ctx *context.Context) { } deleting, disabled := action == "delete", action == "disable" + var changedRunners []*actions_model.ActionRunner if err := db.WithTx(ctx, func(txCtx stdctx.Context) error { for _, runner := range runners { + changed := true var err error if deleting { err = actions_model.DeleteRunner(txCtx, runner.ID) } else { - err = actions_model.SetRunnerDisabled(txCtx, runner, disabled) + changed, err = actions_model.SetRunnerDisabled(txCtx, runner, disabled) } if err != nil { return err } + if changed { + changedRunners = append(changedRunners, runner) + } } return nil }); err != nil { @@ -344,11 +356,11 @@ func RunnerBulkActionPost(ctx *context.Context) { return } - for _, runner := range runners { + for _, runner := range changedRunners { if deleting { - audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerRemove, "runner", runner.Name) + audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerRemove, "runner", runner.Name, "runner_id", runner.ID) } else { - audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerUpdate, "runner", runner.Name, "disabled", disabled) + audit.RecordScopedByID(ctx, runner.OwnerID, runner.RepoID, audit.RunnerToggle(disabled), "runner", runner.Name, "runner_id", runner.ID) } } diff --git a/services/audit/scoped.go b/services/audit/scoped.go index 2e97e6be22e..b93194b3348 100644 --- a/services/audit/scoped.go +++ b/services/audit/scoped.go @@ -95,6 +95,24 @@ var ( User: audit_model.UserRunnerUpdate, System: audit_model.SystemRunnerUpdate, } + RunnerRegister = ScopedActions{ + Repo: audit_model.RepositoryRunnerRegister, + Org: audit_model.OrganizationRunnerRegister, + User: audit_model.UserRunnerRegister, + System: audit_model.SystemRunnerRegister, + } + RunnerEnable = ScopedActions{ + Repo: audit_model.RepositoryRunnerEnable, + Org: audit_model.OrganizationRunnerEnable, + User: audit_model.UserRunnerEnable, + System: audit_model.SystemRunnerEnable, + } + RunnerDisable = ScopedActions{ + Repo: audit_model.RepositoryRunnerDisable, + Org: audit_model.OrganizationRunnerDisable, + User: audit_model.UserRunnerDisable, + System: audit_model.SystemRunnerDisable, + } RunnerRemove = ScopedActions{ Repo: audit_model.RepositoryRunnerRemove, Org: audit_model.OrganizationRunnerRemove, @@ -127,6 +145,14 @@ var ( } ) +// RunnerToggle returns the actions for enabling or disabling a runner. +func RunnerToggle(disabled bool) ScopedActions { + if disabled { + return RunnerDisable + } + return RunnerEnable +} + // resolveScope maps an (owner, repo) pair to the scoped action and audit scope. // The rules cover every multi-scope resource (secrets, OAuth2 apps, webhooks): // a repo wins when set, a nil owner means the instance, otherwise the owner's diff --git a/tests/integration/admin_audit_test.go b/tests/integration/admin_audit_test.go index 86bb18954e3..ff226a97be9 100644 --- a/tests/integration/admin_audit_test.go +++ b/tests/integration/admin_audit_test.go @@ -191,10 +191,31 @@ func TestAuditLogAdministrationEvents(t *testing.T) { } t.Run("ActionsRunners", func(t *testing.T) { - MakeRequest(t, NewRequest(t, "POST", repoAPI+"/actions/runners/registration-token").AddTokenAuth(token), http.StatusOK) - MakeRequest(t, NewRequestWithJSON(t, "PATCH", repoAPI+"/actions/runners/34348", &api.EditActionRunnerOption{Disabled: new(true)}).AddTokenAuth(token), http.StatusOK) + resp := MakeRequest(t, NewRequest(t, "POST", repoAPI+"/actions/runners/registration-token").AddTokenAuth(token), http.StatusOK) + regToken := DecodeJSON(t, resp, &struct{ Token string }{}).Token + MakeRequest(t, NewRequestWithJSON(t, "POST", "/api/actions/runner.v1.RunnerService/Register", map[string]string{"token": regToken, "name": "audit-runner"}), http.StatusOK) + registered := unittest.AssertExistsAndLoadBean(t, &audit_model.Event{Action: audit_model.RepositoryRunnerRegister, ScopeID: 1}) + assert.Equal(t, user_model.ActionsUserID, registered.ActorID) + assert.Equal(t, audit_model.OriginAPI, registered.Origin) + assert.NotEmpty(t, registered.IPAddress) + assert.Equal(t, "audit-runner", audit_model.DecodeMetadata(registered.Metadata)["runner"]) + + session := loginUser(t, "user2") + for range 2 { // unchanged state must not be recorded again + MakeRequest(t, NewRequestWithJSON(t, "PATCH", repoAPI+"/actions/runners/34348", &api.EditActionRunnerOption{Disabled: new(true)}).AddTokenAuth(token), http.StatusOK) + session.MakeRequest(t, NewRequestWithValues(t, "POST", "/user2/repo1/settings/actions/runners/34348", map[string]string{"description": "audited"}), http.StatusSeeOther) + } + MakeRequest(t, NewRequestWithJSON(t, "PATCH", repoAPI+"/actions/runners/34348", &api.EditActionRunnerOption{Disabled: new(false)}).AddTokenAuth(token), http.StatusOK) MakeRequest(t, NewRequest(t, "DELETE", repoAPI+"/actions/runners/34348").AddTokenAuth(token), http.StatusNoContent) - for _, action := range []audit_model.Action{audit_model.RepositoryRunnerToken, audit_model.RepositoryRunnerUpdate, audit_model.RepositoryRunnerRemove} { + removed := unittest.AssertExistsAndLoadBean(t, &audit_model.Event{Action: audit_model.RepositoryRunnerRemove, ScopeID: 1}) + assert.EqualValues(t, 34348, audit_model.DecodeMetadata(removed.Metadata)["runner_id"]) + for _, action := range []audit_model.Action{ + audit_model.RepositoryRunnerToken, + audit_model.RepositoryRunnerDisable, + audit_model.RepositoryRunnerEnable, + audit_model.RepositoryRunnerUpdate, + audit_model.RepositoryRunnerRemove, + } { assertEvent(t, action, audit_model.ScopeRepository, 1) } })