Files
gitea/services/audit/actor.go
T
bircniandsilverwind 3f1b738f09 enhance(audit): record remaining security-relevant events (#40323)
Adds audit actions for security-relevant changes that were not logged at
all, and records a few remaining events on paths that skipped them:

- Account login prohibition (`user:status:prohibitlogin`) and the
organization setting that lets repository admins change team access
- Actions runner registration tokens issued, runners removed or
enabled/disabled, and Actions variables, for every scope
- Instance config changes, including maintenance mode
(`system:config:update`)
- Deploy token regeneration and pull mirror address changes
- Review deletion and dismissal, package removal, and release and tag
removal
- Repository restores via `gitea admin restore-repo`, OAuth2 secret
rotation on API update, and SSH keys added or removed by authentication
source sync

The authentication source SSH key sync moved from the asymkey model to
the asymkey service so it can record events.

Follow-up to https://github.com/go-gitea/gitea/pull/40317 and
https://github.com/go-gitea/gitea/pull/40322.

---------

Co-authored-by: silverwind <me@silverwind.io>
2026-11-16 00:10:54 +00:00

72 lines
2.5 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package audit
import (
"context"
audit_model "gitea.dev/models/audit"
user_model "gitea.dev/models/user"
"gitea.dev/modules/web/middleware"
)
type doerContextKeyType struct{}
var doerContextKey doerContextKeyType
type impersonatorContextKeyType struct{}
var impersonatorContextKey impersonatorContextKeyType
// WithImpersonator returns a context that records audit events as performed by
// the doer on behalf of the given admin.
func WithImpersonator(ctx context.Context, impersonator *user_model.User) context.Context {
return context.WithValue(ctx, impersonatorContextKey, impersonator)
}
// WithDoer returns a context that records audit events as the given user.
//
// Web and API requests need this only in unusual cases: the signed-in user is
// already published to the request data store by routers/common.AuthShared and
// is picked up automatically. Use it at entry points that have no signed-in
// user - the CLI, cron tasks, authentication source syncs and git hooks - before
// calling into services that record audit events themselves.
func WithDoer(ctx context.Context, doer *user_model.User) context.Context {
return context.WithValue(ctx, doerContextKey, doer)
}
func WithCLIDoer(ctx context.Context) context.Context {
return WithDoer(WithOrigin(ctx, audit_model.OriginCLI), user_model.NewCliUser())
}
// doerFromContext resolves the actor of an audit event: an explicit WithDoer
// value wins over the signed-in user of the surrounding request. Returns nil
// when neither is available, which Record turns into an unknown actor.
func doerFromContext(ctx context.Context) *user_model.User {
if doer, ok := ctx.Value(doerContextKey).(*user_model.User); ok && doer != nil {
return doer
}
if data := middleware.GetContextData(ctx); data != nil {
if doer, ok := data[middleware.ContextDataKeySignedUser].(*user_model.User); ok {
return doer
}
}
return nil
}
// ImpersonatorFromContext resolves the admin acting as the doer, so an event
// recorded during an impersonated session cannot be pinned on the impersonated
// user alone. Returns nil for ordinary sessions.
func ImpersonatorFromContext(ctx context.Context) *user_model.User {
if impersonator, ok := ctx.Value(impersonatorContextKey).(*user_model.User); ok && impersonator != nil {
return impersonator
}
if data := middleware.GetContextData(ctx); data != nil {
if impersonator, ok := data[middleware.ContextDataKeyImpersonator].(*user_model.User); ok {
return impersonator
}
}
return nil
}