Skip to content

Commit 3b2da7e

Browse files
authored
Redefine the meaning of column is_active to make Actions Registration Token generation easier (#27143)
Partially Fix #25041 This PR redefined the meaning of column `is_active` in table `action_runner_token`. Before this PR, `is_active` means whether it has been used by any runner. If it's true, other runner cannot use it to register again. In this PR, `is_active` means whether it's validated to be used to register runner. And if it's true, then it can be used to register runners until it become false. When creating a new `is_active` register token, any previous tokens will be set `is_active` to false.
1 parent ee27b94 commit 3b2da7e

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

models/actions/runner_token.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type ActionRunnerToken struct {
2222
Owner *user_model.User `xorm:"-"`
2323
RepoID int64 `xorm:"index"` // repo level runner, if orgid also is zero, then it's a global
2424
Repo *repo_model.Repository `xorm:"-"`
25-
IsActive bool
25+
IsActive bool // true means it can be used
2626

2727
Created timeutil.TimeStamp `xorm:"created"`
2828
Updated timeutil.TimeStamp `xorm:"updated"`
@@ -57,7 +57,7 @@ func UpdateRunnerToken(ctx context.Context, r *ActionRunnerToken, cols ...string
5757
return err
5858
}
5959

60-
// NewRunnerToken creates a new runner token
60+
// NewRunnerToken creates a new active runner token and invalidate all old tokens
6161
func NewRunnerToken(ctx context.Context, ownerID, repoID int64) (*ActionRunnerToken, error) {
6262
token, err := util.CryptoRandomString(40)
6363
if err != nil {
@@ -66,17 +66,27 @@ func NewRunnerToken(ctx context.Context, ownerID, repoID int64) (*ActionRunnerTo
6666
runnerToken := &ActionRunnerToken{
6767
OwnerID: ownerID,
6868
RepoID: repoID,
69-
IsActive: false,
69+
IsActive: true,
7070
Token: token,
7171
}
72-
_, err = db.GetEngine(ctx).Insert(runnerToken)
73-
return runnerToken, err
72+
73+
return runnerToken, db.WithTx(ctx, func(ctx context.Context) error {
74+
if _, err := db.GetEngine(ctx).Where("owner_id =? AND repo_id = ?", ownerID, repoID).Cols("is_active").Update(&ActionRunnerToken{
75+
IsActive: false,
76+
}); err != nil {
77+
return err
78+
}
79+
80+
_, err = db.GetEngine(ctx).Insert(runnerToken)
81+
return err
82+
})
7483
}
7584

76-
// GetUnactivatedRunnerToken returns a unactivated runner token
77-
func GetUnactivatedRunnerToken(ctx context.Context, ownerID, repoID int64) (*ActionRunnerToken, error) {
85+
// GetLastestRunnerToken returns the latest runner token
86+
func GetLastestRunnerToken(ctx context.Context, ownerID, repoID int64) (*ActionRunnerToken, error) {
7887
var runnerToken ActionRunnerToken
79-
has, err := db.GetEngine(ctx).Where("owner_id=? AND repo_id=? AND is_active=?", ownerID, repoID, false).OrderBy("id DESC").Get(&runnerToken)
88+
has, err := db.GetEngine(ctx).Where("owner_id=? AND repo_id=?", ownerID, repoID).
89+
OrderBy("id DESC").Get(&runnerToken)
8090
if err != nil {
8191
return nil, err
8292
} else if !has {

routers/api/actions/runner/runner.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ func (s *Service) Register(
4747

4848
runnerToken, err := actions_model.GetRunnerToken(ctx, req.Msg.Token)
4949
if err != nil {
50-
return nil, errors.New("runner token not found")
50+
return nil, errors.New("runner registration token not found")
5151
}
5252

53-
if runnerToken.IsActive {
54-
return nil, errors.New("runner token has already been activated")
53+
if !runnerToken.IsActive {
54+
return nil, errors.New("runner registration token has been invalidated, please use the latest one")
5555
}
5656

5757
labels := req.Msg.Labels

routers/private/actions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func GenerateActionsRunnerToken(ctx *context.PrivateContext) {
4141
})
4242
}
4343

44-
token, err := actions_model.GetUnactivatedRunnerToken(ctx, owner, repo)
45-
if errors.Is(err, util.ErrNotExist) {
44+
token, err := actions_model.GetLastestRunnerToken(ctx, owner, repo)
45+
if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) {
4646
token, err = actions_model.NewRunnerToken(ctx, owner, repo)
4747
if err != nil {
4848
err := fmt.Sprintf("error while creating runner token: %v", err)

routers/web/shared/actions/runners.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ func RunnersList(ctx *context.Context, opts actions_model.FindRunnerOptions) {
3535

3636
// ownid=0,repo_id=0,means this token is used for global
3737
var token *actions_model.ActionRunnerToken
38-
token, err = actions_model.GetUnactivatedRunnerToken(ctx, opts.OwnerID, opts.RepoID)
39-
if errors.Is(err, util.ErrNotExist) {
38+
token, err = actions_model.GetLastestRunnerToken(ctx, opts.OwnerID, opts.RepoID)
39+
if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) {
4040
token, err = actions_model.NewRunnerToken(ctx, opts.OwnerID, opts.RepoID)
4141
if err != nil {
4242
ctx.ServerError("CreateRunnerToken", err)
4343
return
4444
}
4545
} else if err != nil {
46-
ctx.ServerError("GetUnactivatedRunnerToken", err)
46+
ctx.ServerError("GetLastestRunnerToken", err)
4747
return
4848
}
4949

0 commit comments

Comments
 (0)