mirror of
https://github.com/go-gitea/gitea.git
synced 2026-12-04 15:54:57 +00:00
fix(actions): normalize legacy repository runner registration tokens (#40556)
Repository runner tokens created by `gitea actions generate-runner-token --scope owner/repo` before 1.23, or by the repository API before 1.22.2, kept the owner ID. Migration 314 skipped this table, so these tokens stay valid but are hidden in the settings and survive a reset. A migration now normalizes the active ones. Registering a runner also wrote the token back as active, which could undo a concurrent reset. That write and the unused `UpdateRunnerToken` are removed. Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
1 parent
63b2af6f37
commit
372d4dc97a
6 files changed
+61
-28
No files matched your search
@@ -451,6 +451,7 @@ func prepareMigrationTasks() []*migration {
|
||||
newMigration(372, "Add workflow_ref to action_run", v29.AddWorkflowRefToActionRun),
|
||||
newMigration(373, "Repair Debian and RPM signing keys rejected by Sequoia", v29.RepairPackageSigningKeys),
|
||||
newMigration(374, "Add pull_merge_queue_entry table", v29.AddPullMergeQueueEntryTable),
|
||||
newMigration(375, "Update OwnerID as zero for repository level runner tokens", v29.UpdateOwnerIDOfRepoLevelRunnerTokens),
|
||||
}
|
||||
return preparedMigrations
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package v29
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.dev/modelmigration/base"
|
||||
)
|
||||
|
||||
func UpdateOwnerIDOfRepoLevelRunnerTokens(_ctx context.Context, x base.EngineMigration) error {
|
||||
// inactive rows can't register, a newer one moved into scope would become the latest token and trigger an auto-reset
|
||||
_, err := x.Exec("UPDATE `action_runner_token` SET `owner_id` = 0 WHERE `repo_id` > 0 AND `owner_id` > 0 AND `is_active` = ?", true)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package v29
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modelmigration/migrationtest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUpdateOwnerIDOfRepoLevelRunnerTokens(t *testing.T) {
|
||||
type ActionRunnerToken struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
OwnerID int64
|
||||
RepoID int64
|
||||
IsActive bool
|
||||
}
|
||||
|
||||
x, deferrable := migrationtest.PrepareTestEnv(t, 0, new(ActionRunnerToken))
|
||||
defer deferrable()
|
||||
if x == nil || t.Failed() {
|
||||
return
|
||||
}
|
||||
|
||||
_, err := x.Insert(
|
||||
&ActionRunnerToken{OwnerID: 2, RepoID: 1, IsActive: true},
|
||||
&ActionRunnerToken{OwnerID: 2, RepoID: 1},
|
||||
&ActionRunnerToken{OwnerID: 2, IsActive: true},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, UpdateOwnerIDOfRepoLevelRunnerTokens(t.Context(), x))
|
||||
|
||||
var tokens []ActionRunnerToken
|
||||
require.NoError(t, x.Asc("id").Find(&tokens))
|
||||
assert.Equal(t, []ActionRunnerToken{
|
||||
{ID: 1, OwnerID: 0, RepoID: 1, IsActive: true},
|
||||
{ID: 2, OwnerID: 2, RepoID: 1},
|
||||
{ID: 3, OwnerID: 2, IsActive: true},
|
||||
}, tokens)
|
||||
}
|
||||
@@ -57,18 +57,6 @@ func GetRunnerToken(ctx context.Context, token string) (*ActionRunnerToken, erro
|
||||
return &runnerToken, nil
|
||||
}
|
||||
|
||||
// UpdateRunnerToken updates runner token information.
|
||||
func UpdateRunnerToken(ctx context.Context, r *ActionRunnerToken, cols ...string) (err error) {
|
||||
e := db.GetEngine(ctx)
|
||||
|
||||
if len(cols) == 0 {
|
||||
_, err = e.ID(r.ID).AllCols().Update(r)
|
||||
} else {
|
||||
_, err = e.ID(r.ID).Cols(cols...).Update(r)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// NewRunnerTokenWithValue creates a new active runner token and invalidate all old tokens
|
||||
// ownerID will be ignored and treated as 0 if repoID is non-zero.
|
||||
func NewRunnerTokenWithValue(ctx context.Context, ownerID, repoID int64, token string) (*ActionRunnerToken, error) {
|
||||
|
||||
@@ -27,13 +27,3 @@ func TestNewRunnerToken(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedToken, token)
|
||||
}
|
||||
|
||||
func TestUpdateRunnerToken(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
token := unittest.AssertExistsAndLoadBean(t, &ActionRunnerToken{ID: 3})
|
||||
token.IsActive = true
|
||||
assert.NoError(t, UpdateRunnerToken(t.Context(), token))
|
||||
expectedToken, err := GetLatestRunnerToken(t.Context(), 1, 0)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedToken, token)
|
||||
}
|
||||
@@ -93,12 +93,6 @@ func (s *Service) Register(
|
||||
return nil, errors.New("can't create new runner")
|
||||
}
|
||||
|
||||
// update token status
|
||||
runnerToken.IsActive = true
|
||||
if err := actions_model.UpdateRunnerToken(ctx, runnerToken, "is_active"); err != nil {
|
||||
return nil, errors.New("can't update runner token status")
|
||||
}
|
||||
|
||||
res := &runnerv1.RegisterResponse{
|
||||
Runner: &runnerv1.Runner{
|
||||
Id: runner.ID,
|
||||
|
||||
Reference in new issue
Block a user