|
| 1 | +// Copyright The Forgejo Authors. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package forgejo |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/hex" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + |
| 14 | + actions_model "code.gitea.io/gitea/models/actions" |
| 15 | + "code.gitea.io/gitea/modules/private" |
| 16 | + "code.gitea.io/gitea/modules/setting" |
| 17 | + private_routers "code.gitea.io/gitea/routers/private" |
| 18 | + |
| 19 | + "github.com/urfave/cli/v2" |
| 20 | +) |
| 21 | + |
| 22 | +func CmdActions(ctx context.Context) *cli.Command { |
| 23 | + return &cli.Command{ |
| 24 | + Name: "actions", |
| 25 | + Usage: "Commands for managing Forgejo Actions", |
| 26 | + Subcommands: []*cli.Command{ |
| 27 | + SubcmdActionsGenerateRunnerToken(ctx), |
| 28 | + SubcmdActionsGenerateRunnerSecret(ctx), |
| 29 | + SubcmdActionsRegister(ctx), |
| 30 | + }, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func SubcmdActionsGenerateRunnerToken(ctx context.Context) *cli.Command { |
| 35 | + return &cli.Command{ |
| 36 | + Name: "generate-runner-token", |
| 37 | + Usage: "Generate a new token for a runner to use to register with the server", |
| 38 | + Action: prepareWorkPathAndCustomConf(ctx, func(cliCtx *cli.Context) error { return RunGenerateActionsRunnerToken(ctx, cliCtx) }), |
| 39 | + Flags: []cli.Flag{ |
| 40 | + &cli.StringFlag{ |
| 41 | + Name: "scope", |
| 42 | + Aliases: []string{"s"}, |
| 43 | + Value: "", |
| 44 | + Usage: "{owner}[/{repo}] - leave empty for a global runner", |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func SubcmdActionsGenerateRunnerSecret(ctx context.Context) *cli.Command { |
| 51 | + return &cli.Command{ |
| 52 | + Name: "generate-secret", |
| 53 | + Usage: "Generate a secret suitable for input to the register subcommand", |
| 54 | + Action: func(cliCtx *cli.Context) error { return RunGenerateSecret(ctx, cliCtx) }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func SubcmdActionsRegister(ctx context.Context) *cli.Command { |
| 59 | + return &cli.Command{ |
| 60 | + Name: "register", |
| 61 | + Usage: "Idempotent registration of a runner using a shared secret", |
| 62 | + Action: prepareWorkPathAndCustomConf(ctx, func(cliCtx *cli.Context) error { return RunRegister(ctx, cliCtx) }), |
| 63 | + Flags: []cli.Flag{ |
| 64 | + &cli.StringFlag{ |
| 65 | + Name: "secret", |
| 66 | + Usage: "the secret the runner will use to connect as a 40 character hexadecimal string", |
| 67 | + }, |
| 68 | + &cli.StringFlag{ |
| 69 | + Name: "secret-stdin", |
| 70 | + Usage: "the secret the runner will use to connect as a 40 character hexadecimal string, read from stdin", |
| 71 | + }, |
| 72 | + &cli.StringFlag{ |
| 73 | + Name: "secret-file", |
| 74 | + Usage: "path to the file containing the secret the runner will use to connect as a 40 character hexadecimal string", |
| 75 | + }, |
| 76 | + &cli.StringFlag{ |
| 77 | + Name: "scope", |
| 78 | + Aliases: []string{"s"}, |
| 79 | + Value: "", |
| 80 | + Usage: "{owner}[/{repo}] - leave empty for a global runner", |
| 81 | + }, |
| 82 | + &cli.StringFlag{ |
| 83 | + Name: "labels", |
| 84 | + Value: "", |
| 85 | + Usage: "comma separated list of labels supported by the runner (e.g. docker,ubuntu-latest,self-hosted) (not required since v1.21)", |
| 86 | + }, |
| 87 | + &cli.StringFlag{ |
| 88 | + Name: "name", |
| 89 | + Value: "runner", |
| 90 | + Usage: "name of the runner (default runner)", |
| 91 | + }, |
| 92 | + &cli.StringFlag{ |
| 93 | + Name: "version", |
| 94 | + Value: "", |
| 95 | + Usage: "version of the runner (not required since v1.21)", |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func readSecret(ctx context.Context, cliCtx *cli.Context) (string, error) { |
| 102 | + if cliCtx.IsSet("secret") { |
| 103 | + return cliCtx.String("secret"), nil |
| 104 | + } |
| 105 | + if cliCtx.IsSet("secret-stdin") { |
| 106 | + buf, err := io.ReadAll(ContextGetStdin(ctx)) |
| 107 | + if err != nil { |
| 108 | + return "", err |
| 109 | + } |
| 110 | + return string(buf), nil |
| 111 | + } |
| 112 | + if cliCtx.IsSet("secret-file") { |
| 113 | + path := cliCtx.String("secret-file") |
| 114 | + buf, err := os.ReadFile(path) |
| 115 | + if err != nil { |
| 116 | + return "", err |
| 117 | + } |
| 118 | + return string(buf), nil |
| 119 | + } |
| 120 | + return "", fmt.Errorf("at least one of the --secret, --secret-stdin, --secret-file options is required") |
| 121 | +} |
| 122 | + |
| 123 | +func validateSecret(secret string) error { |
| 124 | + secretLen := len(secret) |
| 125 | + if secretLen != 40 { |
| 126 | + return fmt.Errorf("the secret must be exactly 40 characters long, not %d: generate-secret can provide a secret matching the requirements", secretLen) |
| 127 | + } |
| 128 | + if _, err := hex.DecodeString(secret); err != nil { |
| 129 | + return fmt.Errorf("the secret must be an hexadecimal string: %w", err) |
| 130 | + } |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +func RunRegister(ctx context.Context, cliCtx *cli.Context) error { |
| 135 | + if !ContextGetNoInit(ctx) { |
| 136 | + var cancel context.CancelFunc |
| 137 | + ctx, cancel = installSignals(ctx) |
| 138 | + defer cancel() |
| 139 | + |
| 140 | + if err := initDB(ctx); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + } |
| 144 | + setting.MustInstalled() |
| 145 | + |
| 146 | + secret, err := readSecret(ctx, cliCtx) |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + if err := validateSecret(secret); err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + scope := cliCtx.String("scope") |
| 154 | + labels := cliCtx.String("labels") |
| 155 | + name := cliCtx.String("name") |
| 156 | + version := cliCtx.String("version") |
| 157 | + |
| 158 | + // |
| 159 | + // There are two kinds of tokens |
| 160 | + // |
| 161 | + // - "registration token" only used when a runner interacts to |
| 162 | + // register |
| 163 | + // |
| 164 | + // - "token" obtained after a successful registration and stored by |
| 165 | + // the runner to authenticate |
| 166 | + // |
| 167 | + // The register subcommand does not need a "registration token", it |
| 168 | + // needs a "token". Using the same name is confusing and secret is |
| 169 | + // preferred for this reason in the cli. |
| 170 | + // |
| 171 | + // The ActionsRunnerRegister argument is token to be consistent with |
| 172 | + // the internal naming. It is still confusing to the developer but |
| 173 | + // not to the user. |
| 174 | + // |
| 175 | + owner, repo, err := private_routers.ParseScope(ctx, scope) |
| 176 | + if err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + |
| 180 | + runner, err := actions_model.RegisterRunner(ctx, owner, repo, secret, strings.Split(labels, ","), name, version) |
| 181 | + if err != nil { |
| 182 | + return fmt.Errorf("error while registering runner: %v", err) |
| 183 | + } |
| 184 | + |
| 185 | + if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", runner.UUID); err != nil { |
| 186 | + panic(err) |
| 187 | + } |
| 188 | + return nil |
| 189 | +} |
| 190 | + |
| 191 | +func RunGenerateSecret(ctx context.Context, cliCtx *cli.Context) error { |
| 192 | + runner := actions_model.ActionRunner{} |
| 193 | + if err := runner.GenerateToken(); err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", runner.Token); err != nil { |
| 197 | + panic(err) |
| 198 | + } |
| 199 | + return nil |
| 200 | +} |
| 201 | + |
| 202 | +func RunGenerateActionsRunnerToken(ctx context.Context, cliCtx *cli.Context) error { |
| 203 | + if !ContextGetNoInit(ctx) { |
| 204 | + var cancel context.CancelFunc |
| 205 | + ctx, cancel = installSignals(ctx) |
| 206 | + defer cancel() |
| 207 | + } |
| 208 | + |
| 209 | + setting.MustInstalled() |
| 210 | + |
| 211 | + scope := cliCtx.String("scope") |
| 212 | + |
| 213 | + respText, extra := private.GenerateActionsRunnerToken(ctx, scope) |
| 214 | + if extra.HasError() { |
| 215 | + return handleCliResponseExtra(ctx, extra) |
| 216 | + } |
| 217 | + if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", respText); err != nil { |
| 218 | + panic(err) |
| 219 | + } |
| 220 | + return nil |
| 221 | +} |
| 222 | + |
| 223 | +func prepareWorkPathAndCustomConf(ctx context.Context, action cli.ActionFunc) func(cliCtx *cli.Context) error { |
| 224 | + return func(cliCtx *cli.Context) error { |
| 225 | + if !ContextGetNoInit(ctx) { |
| 226 | + var args setting.ArgWorkPathAndCustomConf |
| 227 | + // from children to parent, check the global flags |
| 228 | + for _, curCtx := range cliCtx.Lineage() { |
| 229 | + if curCtx.IsSet("work-path") && args.WorkPath == "" { |
| 230 | + args.WorkPath = curCtx.String("work-path") |
| 231 | + } |
| 232 | + if curCtx.IsSet("custom-path") && args.CustomPath == "" { |
| 233 | + args.CustomPath = curCtx.String("custom-path") |
| 234 | + } |
| 235 | + if curCtx.IsSet("config") && args.CustomConf == "" { |
| 236 | + args.CustomConf = curCtx.String("config") |
| 237 | + } |
| 238 | + } |
| 239 | + setting.InitWorkPathAndCommonConfig(os.Getenv, args) |
| 240 | + } |
| 241 | + return action(cliCtx) |
| 242 | + } |
| 243 | +} |
0 commit comments