mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-16 08:01:15 +00:00
Backport https://github.com/go-gitea/gitea/pull/40322 Several API and background paths change repositories without writing the audit event the web UI writes for the same change. This records the existing actions on those paths, and webhook, secret and OAuth2 application events now also keep the impersonator. Conflicts resolved for release/v28: - The web project close/reopen handler uses `NotFoundOrServerError`/`ServerError`, as `ServerErrorAuto` does not exist on this branch. - The repository webhook delete API keeps its documented 404 response. - The notifier test keeps the release branch layout with the new cases appended. Signed-off-by: bircni <bircni@icloud.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
19 files changed
+232
-36
No files matched your search
@@ -14,6 +14,7 @@ import (
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/modules/web"
|
||||
"gitea.dev/routers/api/v1/utils"
|
||||
"gitea.dev/services/audit"
|
||||
"gitea.dev/services/context"
|
||||
webhook_service "gitea.dev/services/webhook"
|
||||
)
|
||||
@@ -191,8 +192,12 @@ func DeleteHook(ctx *context.APIContext) {
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
|
||||
hookID := ctx.PathParamInt64("id")
|
||||
if err := webhook.DeleteDefaultSystemWebhook(ctx, hookID); err != nil {
|
||||
hook, err := webhook.GetSystemOrDefaultWebhook(ctx, ctx.PathParamInt64("id"))
|
||||
if err != nil {
|
||||
ctx.APIErrorAuto(err)
|
||||
return
|
||||
}
|
||||
if err := webhook.DeleteDefaultSystemWebhook(ctx, hook.ID); err != nil {
|
||||
if errors.Is(err, util.ErrNotExist) {
|
||||
ctx.APIErrorNotFound()
|
||||
} else {
|
||||
@@ -200,5 +205,6 @@ func DeleteHook(ctx *context.APIContext) {
|
||||
}
|
||||
return
|
||||
}
|
||||
audit.RecordScoped(ctx, nil, nil, audit.WebhookRemove, "webhook", hook.URL)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
audit_model "gitea.dev/models/audit"
|
||||
"gitea.dev/models/db"
|
||||
git_model "gitea.dev/models/git"
|
||||
"gitea.dev/models/organization"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/modules/web"
|
||||
"gitea.dev/routers/api/v1/utils"
|
||||
"gitea.dev/services/audit"
|
||||
"gitea.dev/services/context"
|
||||
"gitea.dev/services/convert"
|
||||
pull_service "gitea.dev/services/pull"
|
||||
@@ -822,6 +824,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryBranchProtectionAdd, ctx.Repo.Repository, "rule", protectBranch.RuleName)
|
||||
|
||||
// Reload from db to get all whitelists
|
||||
bp, err := git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, ruleName)
|
||||
@@ -1154,6 +1157,7 @@ func EditBranchProtection(ctx *context.APIContext) {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryBranchProtectionUpdate, ctx.Repo.Repository, "rule", protectBranch.RuleName)
|
||||
|
||||
isPlainRule := !git_model.IsRuleNameSpecial(bpName)
|
||||
var isBranchExist bool
|
||||
@@ -1255,6 +1259,7 @@ func DeleteBranchProtection(ctx *context.APIContext) {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryBranchProtectionRemove, ctx.Repo.Repository, "rule", bp.RuleName)
|
||||
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"gitea.dev/modules/web"
|
||||
webhook_module "gitea.dev/modules/webhook"
|
||||
"gitea.dev/routers/api/v1/utils"
|
||||
"gitea.dev/services/audit"
|
||||
"gitea.dev/services/context"
|
||||
"gitea.dev/services/convert"
|
||||
webhook_service "gitea.dev/services/webhook"
|
||||
@@ -296,13 +297,14 @@ func DeleteHook(ctx *context.APIContext) {
|
||||
// "$ref": "#/responses/empty"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
if err := webhook.DeleteWebhookByRepoID(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id")); err != nil {
|
||||
if webhook.IsErrWebhookNotExist(err) {
|
||||
ctx.APIErrorNotFound()
|
||||
} else {
|
||||
ctx.APIErrorInternal(err)
|
||||
}
|
||||
hook, err := utils.GetRepoHook(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err := webhook.DeleteWebhookByRepoID(ctx, ctx.Repo.Repository.ID, hook.ID); err != nil {
|
||||
ctx.APIErrorAuto(err)
|
||||
return
|
||||
}
|
||||
audit.RecordScoped(ctx, nil, ctx.Repo.Repository, audit.WebhookRemove, "webhook", hook.URL)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
asymkey_model "gitea.dev/models/asymkey"
|
||||
audit_model "gitea.dev/models/audit"
|
||||
"gitea.dev/models/db"
|
||||
deploykey_model "gitea.dev/models/deploykey"
|
||||
"gitea.dev/models/perm"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
"gitea.dev/modules/web"
|
||||
"gitea.dev/routers/api/v1/utils"
|
||||
asymkey_service "gitea.dev/services/asymkey"
|
||||
"gitea.dev/services/audit"
|
||||
"gitea.dev/services/context"
|
||||
"gitea.dev/services/convert"
|
||||
)
|
||||
@@ -219,6 +221,7 @@ func CreateDeployKey(ctx *context.APIContext) {
|
||||
HandleAddKeyError(ctx, err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryDeployKeyAdd, ctx.Repo.Repository, "deploy_key", key.Name)
|
||||
ctx.JSON(http.StatusCreated, convert.ToDeployKey(ctx, ctx.Repo.Repository, key))
|
||||
}
|
||||
|
||||
@@ -261,6 +264,7 @@ func CreateDeployToken(ctx *context.APIContext) {
|
||||
HandleAddKeyError(ctx, err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryDeployKeyAdd, ctx.Repo.Repository, "deploy_key", key.Name)
|
||||
|
||||
ctx.JSON(http.StatusCreated, convert.ToDeployKey(ctx, ctx.Repo.Repository, key))
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
audit_model "gitea.dev/models/audit"
|
||||
"gitea.dev/models/db"
|
||||
repo_model "gitea.dev/models/repo"
|
||||
"gitea.dev/models/unit"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/modules/web"
|
||||
"gitea.dev/routers/api/v1/utils"
|
||||
"gitea.dev/services/audit"
|
||||
"gitea.dev/services/context"
|
||||
"gitea.dev/services/convert"
|
||||
"gitea.dev/services/migrations"
|
||||
@@ -399,6 +401,7 @@ func CreatePushMirror(ctx *context.APIContext, mirrorOption *api.CreatePushMirro
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryMirrorPushAdd, repo, "mirror_id", pushMirror.ID, "remote_address", pushMirror.RemoteAddress)
|
||||
m, err := convert.ToPushMirror(ctx, pushMirror)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
|
||||
@@ -726,6 +726,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
|
||||
|
||||
if oldDefaultBranch != "" {
|
||||
actions_service.SwitchSchedulesToDefaultBranch(ctx, repo, oldDefaultBranch)
|
||||
audit.Record(ctx, audit_model.RepositoryBranchDefault, repo, "default_branch", repo.DefaultBranch)
|
||||
}
|
||||
|
||||
if visibilityChanged {
|
||||
@@ -984,6 +985,9 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
|
||||
ctx.APIError(http.StatusUnprocessableEntity, err.Error())
|
||||
return err
|
||||
}
|
||||
if *opts.Archived == repo.IsArchived {
|
||||
return nil
|
||||
}
|
||||
if *opts.Archived {
|
||||
if err := repo_model.SetArchiveRepoState(ctx, repo, *opts.Archived); err != nil {
|
||||
log.Error("Tried to archive a repo: %s", err)
|
||||
@@ -993,6 +997,7 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
|
||||
if err := actions_service.CleanRepoScheduleTasks(ctx, repo); err != nil {
|
||||
log.Error("CleanRepoScheduleTasks for archived repo %s/%s: %v", ctx.Repo.Owner.Name, repo.Name, err)
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryArchive, repo)
|
||||
log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
|
||||
} else {
|
||||
if err := repo_model.SetArchiveRepoState(ctx, repo, *opts.Archived); err != nil {
|
||||
@@ -1005,6 +1010,7 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
|
||||
log.Error("DetectAndHandleSchedules for un-archived repo %s/%s: %v", ctx.Repo.Owner.Name, repo.Name, err)
|
||||
}
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryUnarchive, repo)
|
||||
log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
audit_model "gitea.dev/models/audit"
|
||||
git_model "gitea.dev/models/git"
|
||||
"gitea.dev/models/organization"
|
||||
repo_model "gitea.dev/models/repo"
|
||||
@@ -16,6 +17,7 @@ import (
|
||||
api "gitea.dev/modules/structs"
|
||||
"gitea.dev/modules/web"
|
||||
"gitea.dev/routers/api/v1/utils"
|
||||
"gitea.dev/services/audit"
|
||||
"gitea.dev/services/context"
|
||||
"gitea.dev/services/convert"
|
||||
release_service "gitea.dev/services/release"
|
||||
@@ -467,6 +469,7 @@ func CreateTagProtection(ctx *context.APIContext) {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryTagProtectionAdd, repo, "pattern", protectTag.NamePattern)
|
||||
|
||||
pt, err = git_model.GetProtectedTagByID(ctx, protectTag.ID)
|
||||
if err != nil {
|
||||
@@ -574,6 +577,7 @@ func EditTagProtection(ctx *context.APIContext) {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryTagProtectionUpdate, repo, "pattern", pt.NamePattern)
|
||||
|
||||
pt, err = git_model.GetProtectedTagByID(ctx, id)
|
||||
if err != nil {
|
||||
@@ -636,6 +640,7 @@ func DeleteTagProtection(ctx *context.APIContext) {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryTagProtectionRemove, repo, "pattern", pt.NamePattern)
|
||||
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"math"
|
||||
"net/http"
|
||||
|
||||
audit_model "gitea.dev/models/audit"
|
||||
"gitea.dev/models/db"
|
||||
issues_model "gitea.dev/models/issues"
|
||||
access_model "gitea.dev/models/perm/access"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
"gitea.dev/modules/web"
|
||||
"gitea.dev/routers/api/v1/utils"
|
||||
"gitea.dev/routers/common"
|
||||
"gitea.dev/services/audit"
|
||||
"gitea.dev/services/context"
|
||||
"gitea.dev/services/convert"
|
||||
project_service "gitea.dev/services/projects"
|
||||
@@ -57,6 +59,13 @@ func (s projectScope) ownerID() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s projectScope) auditScope() any {
|
||||
if s.Repo != nil {
|
||||
return s.Repo
|
||||
}
|
||||
return s.Owner
|
||||
}
|
||||
|
||||
// attach preloads the relation the converter needs for HTMLURL, sparing a lookup per project.
|
||||
func (s projectScope) attach(projects ...*project_model.Project) {
|
||||
for _, project := range projects {
|
||||
@@ -505,6 +514,7 @@ func CreateProject(ctx *context.APIContext) {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.ProjectCreate, scope.auditScope(), "project", project.Title, "project_id", project.ID)
|
||||
|
||||
scope.attach(project)
|
||||
ctx.JSON(http.StatusCreated, convert.ToProject(ctx, project, ctx.Doer))
|
||||
@@ -606,7 +616,8 @@ func EditProject(ctx *context.APIContext) {
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
project := projectScopeFromContext(ctx).findProject(ctx)
|
||||
scope := projectScopeFromContext(ctx)
|
||||
project := scope.findProject(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
@@ -639,6 +650,7 @@ func EditProject(ctx *context.APIContext) {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.ProjectUpdate, scope.auditScope(), "project", project.Title, "project_id", project.ID)
|
||||
|
||||
if err := project_service.LoadIssueNumbersForProject(ctx, project, ctx.Doer); err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
@@ -713,7 +725,8 @@ func DeleteProject(ctx *context.APIContext) {
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
project := projectScopeFromContext(ctx).findProject(ctx)
|
||||
scope := projectScopeFromContext(ctx)
|
||||
project := scope.findProject(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
@@ -721,6 +734,7 @@ func DeleteProject(ctx *context.APIContext) {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.ProjectDelete, scope.auditScope(), "project", project.Title, "project_id", project.ID)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/modules/validation"
|
||||
webhook_module "gitea.dev/modules/webhook"
|
||||
"gitea.dev/services/audit"
|
||||
"gitea.dev/services/context"
|
||||
webhook_service "gitea.dev/services/webhook"
|
||||
)
|
||||
@@ -105,6 +106,7 @@ func checkCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption)
|
||||
func AddSystemHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
||||
hook, ok := addHook(ctx, form, 0, 0)
|
||||
if ok {
|
||||
audit.RecordScoped(ctx, nil, nil, audit.WebhookAdd, "webhook", hook.URL)
|
||||
h, err := webhook_service.ToHook(setting.AppSubURL+"/-/admin", hook)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
@@ -120,6 +122,7 @@ func AddOwnerHook(ctx *context.APIContext, owner *user_model.User, form *api.Cre
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
audit.RecordScoped(ctx, owner, nil, audit.WebhookAdd, "webhook", hook.URL)
|
||||
apiHook, ok := toAPIHook(ctx, owner.HomeLink(), hook)
|
||||
if !ok {
|
||||
return
|
||||
@@ -134,6 +137,7 @@ func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
audit.RecordScoped(ctx, nil, repo.Repository, audit.WebhookAdd, "webhook", hook.URL)
|
||||
apiHook, ok := toAPIHook(ctx, repo.RepoLink, hook)
|
||||
if !ok {
|
||||
return
|
||||
@@ -278,9 +282,9 @@ func EditSystemHook(ctx *context.APIContext, form *api.EditHookOption, hookID in
|
||||
return
|
||||
}
|
||||
if !editHook(ctx, form, hook) {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
audit.RecordScoped(ctx, nil, nil, audit.WebhookUpdate, "webhook", hook.URL)
|
||||
updated, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
@@ -303,6 +307,7 @@ func EditOwnerHook(ctx *context.APIContext, owner *user_model.User, form *api.Ed
|
||||
if !editHook(ctx, form, hook) {
|
||||
return
|
||||
}
|
||||
audit.RecordScoped(ctx, owner, nil, audit.WebhookUpdate, "webhook", hook.URL)
|
||||
updated, err := GetOwnerHook(ctx, owner.ID, hookID)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -324,6 +329,7 @@ func EditRepoHook(ctx *context.APIContext, form *api.EditHookOption, hookID int6
|
||||
if !editHook(ctx, form, hook) {
|
||||
return
|
||||
}
|
||||
audit.RecordScoped(ctx, nil, repo.Repository, audit.WebhookUpdate, "webhook", hook.URL)
|
||||
updated, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -406,6 +412,10 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
|
||||
|
||||
// DeleteOwnerHook deletes the hook owned by the owner.
|
||||
func DeleteOwnerHook(ctx *context.APIContext, owner *user_model.User, hookID int64) {
|
||||
hook, err := GetOwnerHook(ctx, owner.ID, hookID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err := webhook.DeleteWebhookByOwnerID(ctx, owner.ID, hookID); err != nil {
|
||||
if webhook.IsErrWebhookNotExist(err) {
|
||||
ctx.APIErrorNotFound()
|
||||
@@ -414,5 +424,6 @@ func DeleteOwnerHook(ctx *context.APIContext, owner *user_model.User, hookID int
|
||||
}
|
||||
return
|
||||
}
|
||||
audit.RecordScoped(ctx, owner, nil, audit.WebhookRemove, "webhook", hook.URL)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
@@ -203,6 +203,7 @@ func ChangeProjectStatus(ctx *context.Context) {
|
||||
ctx.NotFoundOrServerError("ChangeProjectStatusByRepoIDAndID", project_model.IsErrProjectNotExist, err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.ProjectUpdate, ctx.ContextUser, "project", project.Title, "project_id", project.ID)
|
||||
ctx.JSONRedirect(project_model.ProjectLinkForOrg(ctx.ContextUser, project.ID))
|
||||
}
|
||||
|
||||
|
||||
@@ -169,10 +169,16 @@ func ChangeProjectStatus(ctx *context.Context) {
|
||||
}
|
||||
id := ctx.PathParamInt64("id")
|
||||
|
||||
if err := project_model.ChangeProjectStatusByRepoIDAndID(ctx, ctx.Repo.Repository.ID, id, toClose); err != nil {
|
||||
ctx.NotFoundOrServerError("ChangeProjectStatusByRepoIDAndID", project_model.IsErrProjectNotExist, err)
|
||||
project, err := project_model.GetProjectForRepoByID(ctx, ctx.Repo.Repository.ID, id)
|
||||
if err != nil {
|
||||
ctx.NotFoundOrServerError("GetProjectForRepoByID", project_model.IsErrProjectNotExist, err)
|
||||
return
|
||||
}
|
||||
if err := project_model.ChangeProjectStatus(ctx, project, toClose); err != nil {
|
||||
ctx.ServerError("ChangeProjectStatus", err)
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.ProjectUpdate, ctx.Repo.Repository, "project", project.Title, "project_id", project.ID)
|
||||
ctx.JSONRedirect(project_model.ProjectLinkForRepo(ctx.Repo.Repository, id))
|
||||
}
|
||||
|
||||
|
||||
@@ -103,6 +103,7 @@ func DeployKeyGenerateToken(ctx *context.Context) {
|
||||
}
|
||||
return
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryDeployKeyAdd, ctx.Repo.Repository, "deploy_key", key.Name)
|
||||
|
||||
ctx.Flash.Success(ctx.Tr("repo.settings.generate_deploy_token_success", htmlutil.HTMLFormat("<code>%s</code>", key.Token)))
|
||||
ctx.JSONRedirect(ctx.Repo.RepoLink + "/settings/keys")
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
audit_model "gitea.dev/models/audit"
|
||||
repository_model "gitea.dev/models/repo"
|
||||
"gitea.dev/models/unittest"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/reqctx"
|
||||
"gitea.dev/modules/setting"
|
||||
@@ -224,6 +225,11 @@ func TestImpersonatorRef(t *testing.T) {
|
||||
// an admin acting as themselves is not an impersonation
|
||||
assert.Nil(t, impersonatorRef(admin, admin))
|
||||
assert.Nil(t, impersonatorRef(nil, impersonated))
|
||||
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
defer test.MockVariableValue(&setting.Audit.RecordOutput, setting.AuditRecordOutputDatabase)()
|
||||
RecordScoped(rc, impersonated, nil, SecretAdd, "secret", "name")
|
||||
unittest.AssertExistsAndLoadBean(t, &audit_model.Event{Action: audit_model.UserSecretAdd, ImpersonatorID: admin.ID})
|
||||
}
|
||||
|
||||
// An unresolvable actor must still produce an event, so a missing entry point
|
||||
|
||||
+36
-14
@@ -29,6 +29,10 @@ func (n *auditNotifier) CreateRepository(ctx context.Context, doer, _ *user_mode
|
||||
RecordAs(ctx, doer, audit_model.RepositoryCreate, repo)
|
||||
}
|
||||
|
||||
func (n *auditNotifier) MigrateRepository(ctx context.Context, doer, _ *user_model.User, repo *repo_model.Repository) {
|
||||
RecordAs(ctx, doer, audit_model.RepositoryCreate, repo)
|
||||
}
|
||||
|
||||
func (n *auditNotifier) ForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
|
||||
RecordAs(ctx, doer, audit_model.RepositoryCreateFork, repo, "base_repo", oldRepo.FullName())
|
||||
}
|
||||
@@ -63,6 +67,14 @@ func loadIssueRepo(ctx context.Context, issue *issues_model.Issue) *repo_model.R
|
||||
return issue.Repo
|
||||
}
|
||||
|
||||
func loadPullRequestRepo(ctx context.Context, pr *issues_model.PullRequest) *repo_model.Repository {
|
||||
if err := pr.LoadIssue(ctx); err != nil {
|
||||
log.Error("audit: LoadIssue for pull request %d: %v", pr.ID, err)
|
||||
return nil
|
||||
}
|
||||
return loadIssueRepo(ctx, pr.Issue)
|
||||
}
|
||||
|
||||
func issueOrPR(issue *issues_model.Issue, issueAction, prAction audit_model.Action) (audit_model.Action, string) {
|
||||
if issue.IsPull {
|
||||
return prAction, "pull_request"
|
||||
@@ -94,13 +106,7 @@ func (n *auditNotifier) DeleteIssue(ctx context.Context, doer *user_model.User,
|
||||
}
|
||||
|
||||
func (n *auditNotifier) NewPullRequest(ctx context.Context, pr *issues_model.PullRequest, _ []*user_model.User) {
|
||||
if pr.Issue == nil {
|
||||
if err := pr.LoadIssue(ctx); err != nil {
|
||||
log.Error("audit: LoadIssue for pull request %d: %v", pr.ID, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
repo := loadIssueRepo(ctx, pr.Issue)
|
||||
repo := loadPullRequestRepo(ctx, pr)
|
||||
if repo == nil {
|
||||
return
|
||||
}
|
||||
@@ -108,19 +114,35 @@ func (n *auditNotifier) NewPullRequest(ctx context.Context, pr *issues_model.Pul
|
||||
}
|
||||
|
||||
func (n *auditNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
||||
if pr.Issue == nil {
|
||||
if err := pr.LoadIssue(ctx); err != nil {
|
||||
log.Error("audit: LoadIssue for pull request %d: %v", pr.ID, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
repo := loadIssueRepo(ctx, pr.Issue)
|
||||
repo := loadPullRequestRepo(ctx, pr)
|
||||
if repo == nil {
|
||||
return
|
||||
}
|
||||
RecordAs(ctx, doer, audit_model.PullRequestMerge, repo, issueMeta(pr.Issue, "pull_request")...)
|
||||
}
|
||||
|
||||
func (n *auditNotifier) AutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
||||
n.MergePullRequest(ctx, doer, pr)
|
||||
}
|
||||
|
||||
var reviewTypeNames = map[issues_model.ReviewType]string{
|
||||
issues_model.ReviewTypeApprove: "approve",
|
||||
issues_model.ReviewTypeComment: "comment",
|
||||
issues_model.ReviewTypeReject: "reject",
|
||||
}
|
||||
|
||||
func (n *auditNotifier) PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, _ []*user_model.User) {
|
||||
if repo := loadPullRequestRepo(ctx, pr); repo != nil {
|
||||
RecordAs(ctx, review.Reviewer, audit_model.PullRequestCommentCreate, repo, "pull_request", issueLabel(pr.Issue), "comment_id", comment.ID, "review_id", review.ID, "review_type", reviewTypeNames[review.Type])
|
||||
}
|
||||
}
|
||||
|
||||
func (n *auditNotifier) PullRequestCodeComment(ctx context.Context, pr *issues_model.PullRequest, comment *issues_model.Comment, _ []*user_model.User) {
|
||||
if repo := loadPullRequestRepo(ctx, pr); repo != nil {
|
||||
RecordAs(ctx, comment.Poster, audit_model.PullRequestCommentCreate, repo, "pull_request", issueLabel(pr.Issue), "comment_id", comment.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func (n *auditNotifier) CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, comment *issues_model.Comment, _ []*user_model.User) {
|
||||
action, key := issueOrPR(issue, audit_model.IssueCommentCreate, audit_model.PullRequestCommentCreate)
|
||||
RecordAs(ctx, doer, action, repo, key, issueLabel(issue), "comment_id", comment.ID)
|
||||
|
||||
@@ -27,6 +27,7 @@ func TestAuditNotifier(t *testing.T) {
|
||||
ctx := reqctx.NewRequestContextForTest(t)
|
||||
SetRequestInfo(ctx, audit_model.OriginUI, "127.0.0.1")
|
||||
notifier := new(auditNotifier)
|
||||
migrator := &user_model.User{ID: 3, Name: "migrator"}
|
||||
|
||||
issue := &issues_model.Issue{ID: 10, Index: 5, Title: "Issue title", Poster: doer, Repo: repo}
|
||||
notifier.NewIssue(ctx, issue, nil)
|
||||
@@ -78,4 +79,36 @@ func TestAuditNotifier(t *testing.T) {
|
||||
ScopeType: audit_model.ScopeRepository,
|
||||
ScopeID: repo.ID,
|
||||
})
|
||||
|
||||
notifier.MigrateRepository(ctx, migrator, migrator, repo)
|
||||
unittest.AssertExistsAndLoadBean(t, &audit_model.Event{
|
||||
Action: audit_model.RepositoryCreate,
|
||||
ActorID: migrator.ID,
|
||||
ScopeType: audit_model.ScopeRepository,
|
||||
ScopeID: repo.ID,
|
||||
Origin: audit_model.OriginUI,
|
||||
})
|
||||
|
||||
autoMergePR := &issues_model.PullRequest{Issue: &issues_model.Issue{Index: 7, Repo: repo}}
|
||||
notifier.AutoMergePullRequest(ctx, doer, autoMergePR)
|
||||
unittest.AssertExistsAndLoadBean(t, &audit_model.Event{
|
||||
Action: audit_model.PullRequestMerge,
|
||||
ActorID: doer.ID,
|
||||
ScopeType: audit_model.ScopeRepository,
|
||||
ScopeID: repo.ID,
|
||||
})
|
||||
|
||||
notifier.PullRequestCodeComment(ctx, autoMergePR, &issues_model.Comment{ID: 32, Poster: doer}, nil)
|
||||
unittest.AssertExistsAndLoadBean(t, &audit_model.Event{
|
||||
Action: audit_model.PullRequestCommentCreate,
|
||||
ActorID: doer.ID,
|
||||
ScopeType: audit_model.ScopeRepository,
|
||||
ScopeID: repo.ID,
|
||||
Message: "Added comment 32 to pull request #7 in repository owner/repo.",
|
||||
})
|
||||
|
||||
notifier.PullRequestReview(ctx, autoMergePR, &issues_model.Review{ID: 21, Type: issues_model.ReviewTypeApprove, Reviewer: doer}, &issues_model.Comment{ID: 31}, nil)
|
||||
assert.JSONEq(t, `{"pull_request":"#7","comment_id":31,"review_id":21,"review_type":"approve"}`, unittest.AssertExistsAndLoadBean(t, &audit_model.Event{
|
||||
Action: audit_model.PullRequestCommentCreate, ActorID: doer.ID, Message: "Added comment 31 to pull request #7 in repository owner/repo.",
|
||||
}).Metadata)
|
||||
}
|
||||
@@ -111,12 +111,5 @@ func RecordScoped(ctx context.Context, owner *user_model.User, repo *repository_
|
||||
log.Error("audit: no action configured for scope type %s", scope.Type)
|
||||
return
|
||||
}
|
||||
doer := doerFromContext(ctx)
|
||||
writeEvent(ctx, RecordParams{
|
||||
Action: action,
|
||||
Actor: actorRef(doer),
|
||||
ActorCredential: actorCredential(ctx, doer),
|
||||
Scope: scope,
|
||||
Metadata: metaPairs(metadata...),
|
||||
})
|
||||
Record(ctx, action, scope, metadata...)
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
audit_model "gitea.dev/models/audit"
|
||||
"gitea.dev/models/db"
|
||||
repo_model "gitea.dev/models/repo"
|
||||
system_model "gitea.dev/models/system"
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
"gitea.dev/modules/git/gitcmd"
|
||||
"gitea.dev/modules/log"
|
||||
repo_module "gitea.dev/modules/repository"
|
||||
"gitea.dev/services/audit"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
@@ -164,8 +166,10 @@ func DeleteMissingRepositories(ctx context.Context, doer *user_model.User) error
|
||||
if err := DeleteRepositoryDirectly(ctx, repo.ID); err != nil {
|
||||
log.Error("Failed to DeleteRepository %-v: Error: %v", repo, err)
|
||||
if err2 := system_model.CreateRepositoryNotice("Failed to DeleteRepository (%s) [%d]: Error: %v", repo.FullName(), repo.ID, err); err2 != nil {
|
||||
log.Error("CreateRepositoryNotice: %v", err)
|
||||
log.Error("CreateRepositoryNotice: %v", err2)
|
||||
}
|
||||
} else {
|
||||
audit.RecordAs(ctx, doer, audit_model.RepositoryDelete, repo)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
actions_model "gitea.dev/models/actions"
|
||||
activities_model "gitea.dev/models/activities"
|
||||
admin_model "gitea.dev/models/admin"
|
||||
audit_model "gitea.dev/models/audit"
|
||||
"gitea.dev/models/db"
|
||||
git_model "gitea.dev/models/git"
|
||||
issues_model "gitea.dev/models/issues"
|
||||
@@ -31,6 +32,7 @@ import (
|
||||
"gitea.dev/modules/storage"
|
||||
actions_service "gitea.dev/services/actions"
|
||||
asymkey_service "gitea.dev/services/asymkey"
|
||||
"gitea.dev/services/audit"
|
||||
issue_service "gitea.dev/services/issue"
|
||||
|
||||
"xorm.io/builder"
|
||||
@@ -410,6 +412,7 @@ func DeleteOwnerRepositoriesDirectly(ctx context.Context, owner *user_model.User
|
||||
if err := DeleteRepositoryDirectly(ctx, repo.ID); err != nil {
|
||||
return fmt.Errorf("unable to delete repository %s for %s[%d]. Error: %w", repo.Name, owner.Name, owner.ID, err)
|
||||
}
|
||||
audit.Record(ctx, audit_model.RepositoryDelete, repo)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -5,6 +5,7 @@ package integration
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
|
||||
audit_model "gitea.dev/models/audit"
|
||||
auth_model "gitea.dev/models/auth"
|
||||
"gitea.dev/models/unittest"
|
||||
"gitea.dev/modules/json"
|
||||
"gitea.dev/modules/setting"
|
||||
api "gitea.dev/modules/structs"
|
||||
@@ -170,3 +172,72 @@ func TestAdminAuditLogExport(t *testing.T) {
|
||||
assert.True(t, found)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAuditLogRepositoryAPIEvents(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
defer test.MockVariableValue(&setting.Audit.RecordOutput, setting.AuditRecordOutputDatabase)()
|
||||
|
||||
token := getUserToken(t, "user1", auth_model.AccessTokenScopeAll)
|
||||
const repoAPI = "/api/v1/repos/user2/repo1"
|
||||
|
||||
assertEvents := func(t *testing.T, scopeType audit_model.ScopeType, scopeID int64, actions ...audit_model.Action) {
|
||||
t.Helper()
|
||||
for _, action := range actions {
|
||||
assert.Equal(t, 1, unittest.GetCount(t, &audit_model.Event{Action: action, ScopeType: scopeType, ScopeID: scopeID}), "audit events for %s", action)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("Webhooks", func(t *testing.T) {
|
||||
hookOption := &api.CreateHookOption{Type: "gitea", Config: api.CreateHookOptionConfig{"url": "http://example.com/hook", "content_type": "json"}}
|
||||
|
||||
resp := MakeRequest(t, NewRequestWithJSON(t, "POST", repoAPI+"/hooks", hookOption).AddTokenAuth(token), http.StatusCreated)
|
||||
hookURL := fmt.Sprintf("%s/hooks/%d", repoAPI, DecodeJSON(t, resp, &api.Hook{}).ID)
|
||||
MakeRequest(t, NewRequestWithJSON(t, "PATCH", hookURL, &api.EditHookOption{Active: new(false)}).AddTokenAuth(token), http.StatusOK)
|
||||
MakeRequest(t, NewRequest(t, "DELETE", hookURL).AddTokenAuth(token), http.StatusNoContent)
|
||||
assertEvents(t, audit_model.ScopeRepository, 1, audit_model.RepositoryWebhookAdd, audit_model.RepositoryWebhookUpdate, audit_model.RepositoryWebhookRemove)
|
||||
|
||||
resp = MakeRequest(t, NewRequestWithJSON(t, "POST", "/api/v1/orgs/org3/hooks", hookOption).AddTokenAuth(token), http.StatusCreated)
|
||||
MakeRequest(t, NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/orgs/org3/hooks/%d", DecodeJSON(t, resp, &api.Hook{}).ID)).AddTokenAuth(token), http.StatusNoContent)
|
||||
assertEvents(t, audit_model.ScopeOrganization, 3, audit_model.OrganizationWebhookAdd, audit_model.OrganizationWebhookRemove)
|
||||
})
|
||||
|
||||
t.Run("BranchProtection", func(t *testing.T) {
|
||||
MakeRequest(t, NewRequestWithJSON(t, "POST", repoAPI+"/branch_protections", &api.CreateBranchProtectionOption{RuleName: "branch2"}).AddTokenAuth(token), http.StatusCreated)
|
||||
MakeRequest(t, NewRequestWithJSON(t, "PATCH", repoAPI+"/branch_protections/branch2", &api.EditBranchProtectionOption{EnablePush: new(true)}).AddTokenAuth(token), http.StatusOK)
|
||||
MakeRequest(t, NewRequest(t, "DELETE", repoAPI+"/branch_protections/branch2").AddTokenAuth(token), http.StatusNoContent)
|
||||
assertEvents(t, audit_model.ScopeRepository, 1, audit_model.RepositoryBranchProtectionAdd, audit_model.RepositoryBranchProtectionUpdate, audit_model.RepositoryBranchProtectionRemove)
|
||||
})
|
||||
|
||||
t.Run("TagProtection", func(t *testing.T) {
|
||||
resp := MakeRequest(t, NewRequestWithJSON(t, "POST", repoAPI+"/tag_protections", &api.CreateTagProtectionOption{NamePattern: "v*", WhitelistUsernames: []string{"user2"}}).AddTokenAuth(token), http.StatusCreated)
|
||||
tagURL := fmt.Sprintf("%s/tag_protections/%d", repoAPI, DecodeJSON(t, resp, &api.TagProtection{}).ID)
|
||||
MakeRequest(t, NewRequestWithJSON(t, "PATCH", tagURL, &api.EditTagProtectionOption{NamePattern: new("release-*")}).AddTokenAuth(token), http.StatusOK)
|
||||
MakeRequest(t, NewRequest(t, "DELETE", tagURL).AddTokenAuth(token), http.StatusNoContent)
|
||||
assertEvents(t, audit_model.ScopeRepository, 1, audit_model.RepositoryTagProtectionAdd, audit_model.RepositoryTagProtectionUpdate, audit_model.RepositoryTagProtectionRemove)
|
||||
})
|
||||
|
||||
t.Run("DeployKeyAndToken", func(t *testing.T) {
|
||||
MakeRequest(t, NewRequestWithJSON(t, "POST", repoAPI+"/keys", &api.CreateKeyOption{
|
||||
Title: "audit-deploy-key",
|
||||
Key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEYv1rUQ6Sx6XQNM5o8URz8xvuXaV9bGXfj+OMkh8T0X",
|
||||
}).AddTokenAuth(token), http.StatusCreated)
|
||||
MakeRequest(t, NewRequestWithJSON(t, "POST", repoAPI+"/keys/tokens", &api.CreateDeployKeyTokenOption{Title: "audit-deploy-token"}).AddTokenAuth(token), http.StatusCreated)
|
||||
|
||||
unittest.AssertCount(t, &audit_model.Event{Action: audit_model.RepositoryDeployKeyAdd, ScopeType: audit_model.ScopeRepository, ScopeID: 1}, 2)
|
||||
})
|
||||
|
||||
t.Run("RepoSettings", func(t *testing.T) {
|
||||
MakeRequest(t, NewRequestWithJSON(t, "PATCH", repoAPI, &api.EditRepoOption{DefaultBranch: new("branch2"), Archived: new(false)}).AddTokenAuth(token), http.StatusOK)
|
||||
MakeRequest(t, NewRequestWithJSON(t, "PATCH", repoAPI, &api.EditRepoOption{Archived: new(true)}).AddTokenAuth(token), http.StatusOK)
|
||||
MakeRequest(t, NewRequestWithJSON(t, "PATCH", repoAPI, &api.EditRepoOption{Archived: new(false)}).AddTokenAuth(token), http.StatusOK)
|
||||
assertEvents(t, audit_model.ScopeRepository, 1, audit_model.RepositoryBranchDefault, audit_model.RepositoryArchive, audit_model.RepositoryUnarchive)
|
||||
})
|
||||
|
||||
t.Run("Projects", func(t *testing.T) {
|
||||
resp := MakeRequest(t, NewRequestWithJSON(t, "POST", repoAPI+"/projects", &api.CreateProjectOption{Title: "audit-project"}).AddTokenAuth(token), http.StatusCreated)
|
||||
projectURL := fmt.Sprintf("%s/projects/%d", repoAPI, DecodeJSON(t, resp, &api.Project{}).ID)
|
||||
MakeRequest(t, NewRequestWithJSON(t, "PATCH", projectURL, &api.EditProjectOption{Title: new("audit-project-renamed")}).AddTokenAuth(token), http.StatusOK)
|
||||
MakeRequest(t, NewRequest(t, "DELETE", projectURL).AddTokenAuth(token), http.StatusNoContent)
|
||||
assertEvents(t, audit_model.ScopeRepository, 1, audit_model.ProjectCreate, audit_model.ProjectUpdate, audit_model.ProjectDelete)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user