Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Support reusable Registration Token of runners #25041

Closed
sillyguodong opened this issue Jun 1, 2023 · 9 comments · Fixed by #27143
Closed

[Proposal] Support reusable Registration Token of runners #25041

sillyguodong opened this issue Jun 1, 2023 · 9 comments · Fixed by #27143
Labels
topic/gitea-actions related to the actions of Gitea type/enhancement An improvement of existing functionality type/proposal The new feature has not been accepted yet but needs to be discussed first.

Comments

@sillyguodong
Copy link
Contributor

sillyguodong commented Jun 1, 2023

Background

Currently, the Registration Token obtained in the UI becomes invalid immediately after registering a runner, making it impossible to reuse. However, it is annoying that not having reusbale annoying in some auto-scaling environments.

Here are some related issues::

In fact, we already have related PRs that enable Gitea to provide the ability to generate Tokens via the command line or API:

But I believe it would be more convenient to have a reusable token. Of course, it would also be necessary to provide the ability to invalidate Tokens in real-time to prevent misuse.

Solution

  1. Add the following fields to the table:

       type ActionRunnerToken struct {
           ID       int64
           Token    string `xorm:"UNIQUE"`
           IsActive bool
           // Omitting other fields
           // The following are the added fields:
           Type          ActionRunnerTokenType
           ParentTokenID int64
           Desc          string
       }
    
       type ActionRunnerTokenType int
    
       const (
           NormalRunnerToken ActionRunnerTokenType = iota
    
           ReusableRunnerToken
       )
  2. In the UI, users can create reusable tokens and view them in a list. In the list, they can enable, disable, or delete tokens at any time.
    image

  3. When Gitea receives a runner registration request, it first checks if the token is a reusable token. If it is, a normal token is generated for the runner and returned. This normal token will be written to the .runner file. At the same time, the normal token inherits the owner_id and repo_id from the reusable token and is stored in the database.

  4. As mentioned in this comment,we should also provide the ability to deregister runners:

    It would also be great if there was a way to unregister act_runner as well, especially if you want them to be created and deleted dynamically, to avoid having unnecessary act_runner corpses in the database.

    To achieve this, we need to add the following to the protocol buffer:

       service RunnerService {
         // Deregister deregister new runner in server.
         rpc Deregister(DeregisterRequest) returns (DeregisterResponse) {}
       }
    
       message DeregisterRequest {
         int64 id = 1;
         string uuid = 2;
         string token = 3; // must be normal token in .runner file
       }
    
       message DeregisterResponse {
         Runner runner = 1;
       }

Benefits

  1. Creating a reusable token needs to be done in Site Administration, Repo Settings, or Org Settings, defining the scope of runners that use this reusable token. This is consistent with the previous token logic.
  2. The implementation is simple and does not affect the existing logic.
@sillyguodong sillyguodong added type/proposal The new feature has not been accepted yet but needs to be discussed first. type/feature Completely new functionality. Can only be merged if feature freeze is not active. topic/gitea-actions related to the actions of Gitea labels Jun 1, 2023
@silverwind
Copy link
Member

silverwind commented Jun 1, 2023

ReusableRunnerToken

Would not introduce a new struct, but extend current struct with a boolean isPermanent that defaults to false. When set to true, it will not be deleted on use. On the UI, add a checkbox that controls this value on token creation.

@silverwind
Copy link
Member

In the UI, users can create reusable tokens and view them in a list. In the list, they can enable, disable, or delete tokens at any time.

Maybe not make it a modal, but a separate page that can be linked to, etc. Someone recently linked this in discord 😆.

@wolfogre
Copy link
Member

wolfogre commented Jun 6, 2023

I have different ideas. I think we can just keep it simple.

  1. No Normal token or Reusable token, just Registration token
  2. By default, a registration token will be inactive mark used (actually, IsActive = true means used) when it has been used, just like what has been done now.
  3. Users can "pin" a registration token to make it reusable, that means the token will keep usable when it has been used.
  4. Users can still reset the registration token or "unpin" it.
image

BTW, I think you may misunderstand "registration token"(ActionRunnerToken.Token) and "runner token"(ActionRunner.Token), they are different. Runners will have different token for authentication when connecting to Gitea even they have used the same registration token.

See

if err := runner.GenerateToken(); err != nil {
return nil, errors.New("can't generate token")
}
// create new runner
if err := actions_model.CreateRunner(ctx, runner); err != nil {
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")
}


Agree with Deregister, but it could be another PR/issue.

@silverwind
Copy link
Member

silverwind commented Jun 6, 2023

Yes, I had been suggesting a checkbox too. Would name it "Permanent".

To manage tokens, add Actions -> Tokens to manage them with a table view. Non-permanent tokens should show up in the table, but be automatically deleted upon use.

To avoid generating trash tokens every time this menu is opened, I suggest a explicit "Generate" button, which then could link to a single-token page, or popup with instructions how to use.

@jon-ressio
Copy link

This would be great. I've got a starting point for a helm chart for gitea-runner but can't scale it out beyond 1 runner, or delete and recreate the pod

@nwmcsween
Copy link

This needs to be done for any sort of autoscaling.

@garymoon
Copy link
Contributor

garymoon commented Aug 4, 2023

This needs to be done for any sort of autoscaling.

While I'm a strong proponent of this proposal, autoscaling would also be made possible (though involving greater effort than if this proposal were accepted) if this PR (and subsequently the org-level and global variants) were merged.

@a9eesh
Copy link

a9eesh commented Aug 9, 2023

please add this feature anyways. its perfect for docker stacks!

@lunny lunny added type/enhancement An improvement of existing functionality and removed type/feature Completely new functionality. Can only be merged if feature freeze is not active. labels Sep 20, 2023
@lunny
Copy link
Member

lunny commented Sep 20, 2023

I sent #27143 to try to fix this one. I just have a simple design to reuse is_active column and only the latest token could be used. Then the UI kept as before.

lunny added a commit that referenced this issue Sep 27, 2023
… 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.
GiteaBot pushed a commit to GiteaBot/gitea that referenced this issue Sep 27, 2023
… Token generation easier (go-gitea#27143)

Partially Fix go-gitea#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.
@lunny lunny reopened this Sep 27, 2023
lunny added a commit that referenced this issue Sep 28, 2023
… Token generation easier (#27143) (#27304)

Backport #27143 by @lunny

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.

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
@lunny lunny closed this as completed Dec 19, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
topic/gitea-actions related to the actions of Gitea type/enhancement An improvement of existing functionality type/proposal The new feature has not been accepted yet but needs to be discussed first.
Projects
None yet
8 participants