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

Add a new command to set a predefined runner register token and also add a set-token parameter for registration-token API #32878

Closed

Conversation

lunny
Copy link
Member

@lunny lunny commented Dec 17, 2024

Resolve #23703

This PR adds a new gitea actions command to set a runner register token.
It also adds a new parameter set-token for the /api/v1/admin/runners/registration-token API to allow setting a predefined register token.

After the PR is merged, you can now use the command line to set a token like below.

./gitea actions set-runner-token --token v24pMvsJmiaoQ60CUzdAq6JN05nfqv20WbaAX6nH

or invoking the API like

curl -H "Authorization: token <access_token>" http://localhost:3000/api/v1/admin/runners/registration-token?put-token=v24pMvsJmiaoQ60CUzdAq6JN05nfqv20WbaAX6nH

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Dec 17, 2024
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Dec 17, 2024
@github-actions github-actions bot added modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code modifies/cli PR changes something on the CLI, i.e. gitea doctor or gitea admin labels Dec 17, 2024
@lunny lunny added this to the 1.24.0 milestone Dec 17, 2024
@lunny lunny added the backport/v1.23 This PR should be backported to Gitea 1.23 label Dec 17, 2024
@lunny lunny changed the title Support put a redefined token for Gitea actions command or API Support putting a predefined token for Gitea actions register token generation command or API Dec 17, 2024
@wxiaoguang
Copy link
Contributor

Improper names ....... why put-token, why generate token could put.

And it needs tests

@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 18, 2024
@lunny lunny changed the title Support putting a predefined token for Gitea actions register token generation command or API Add a new command to set a predefined runner register token and also add a set-token parameter for registration-token API Dec 18, 2024

subcmdActionsSetRunnerToken = &cli.Command{
Name: "set-runner-token",
Usage: "Set a new token for a runner to as register token",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't "Set a new token" simply "adding a token"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because once the new token is added, all old tokens will be invalided. Only one token will be valid. So that I use set rather than add.

cmd/actions.go Outdated
Name: "set-runner-token",
Usage: "Set a new token for a runner to as register token",
Action: runSetActionsRunnerToken,
Aliases: []string{"grt"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is it? Copied and pasted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// in: body
// description: set a runner register token instead of generating one.
// type: string
// required: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really strange that a GET request accepts "set_token" parameter. And it is anti-pattern to make the GET request updates the target.

https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#create-a-registration-token-for-an-organization

Does GitHub do so?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous API is GET but looks like GitHub uses POST and Github doesn't support a similar feature.

if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) {
token, err = actions_model.NewRunnerToken(ctx, owner, repo)
var token *actions_model.ActionRunnerToken
if genRequest.PutToken == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you sometimes call it "set token" but sometimes call it "put token"

Name: "token",
Aliases: []string{"t"},
Value: "",
Usage: "[{token}] - leave empty will generate a new token, otherwise will update the token to database. The token MUST be a 40 digital string containing only [0-9a-zA-Z]",
Copy link
Contributor

@wxiaoguang wxiaoguang Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without a tool, it is difficult for end users to generate it.

The whole design should be like this:

./gitea actions generate-runner-token --global # generate and update
./gitea actions generate-runner-token --scope owner/repo  # generate and update
./gitea actions generate-runner-token --display-only # display a token only
./gitea actions generate-runner-token --global --set-token the-token-value # set the token to the pre-generated one
./gitea actions generate-runner-token --scope owner/repo --set-token the-token-value # set the token to the pre-generated one

Do not introduce more technical debts

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my previous solution. I created a new subcommand now because you said generate-runner-token is conflicted with set-token(previous put-token).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my previous solution. I created a new subcommand now because you said generate-runner-token is conflicted with set-token(previous put-token).

That's not your previous solution. At least I can see huge difference.

Copy link
Contributor

@wxiaoguang wxiaoguang Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The differences:

  1. we should forbid the ./gitea actions generate-runner-token without arguments (maybe it is a breaking change, but I believe we should do it, and it is easy for end users to follow)
  2. we should add --display-only to help end users to generate the token
  3. --set-token only works with --global or --scope (put itself is an improper name, I do not remember where else we ever used it)

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Dec 18, 2024

And one more thing, to fix #23703, there could be a better solution, without making users to manually set the tokens:

Add an expiry time to the token, and make the token could be re-used before expiration.

There could also be a "global permanent token" which is valid forever

@wxiaoguang
Copy link
Contributor

Use env GITEA_RUNNER_REGISTRATION_TOKEN as global runner token #32946

@wxiaoguang wxiaoguang closed this Dec 22, 2024
@GiteaBot GiteaBot removed this from the 1.24.0 milestone Dec 22, 2024
@lunny lunny deleted the lunny/support_predefined_runner_register_token branch December 22, 2024 18:10
@lunny lunny removed the backport/v1.23 This PR should be backported to Gitea 1.23 label Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/api This PR adds API routes or modifies them modifies/cli PR changes something on the CLI, i.e. gitea doctor or gitea admin modifies/go Pull requests that update Go code size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Config Management/Stateless Runner Deploy Workflows
3 participants