From e08495af090634d69704f2d9667bdddce7c47684 Mon Sep 17 00:00:00 2001 From: "Bo-Yi.Wu" Date: Sat, 15 Oct 2022 20:03:33 +0800 Subject: [PATCH] chore(runner): remove client secret and add UUID in runner Signed-off-by: Bo-Yi.Wu --- .gitignore | 1 + client/http.go | 14 +------ client/options.go | 21 +++++++++++ cmd/daemon.go | 48 ++++++++++++++++++------ cmd/register.go | 88 -------------------------------------------- cmd/root.go | 11 +----- config/config.go | 4 +- poller/poller.go | 43 +--------------------- register/register.go | 59 +++++++++++++++++++++++++++++ 9 files changed, 122 insertions(+), 167 deletions(-) delete mode 100644 cmd/register.go create mode 100644 register/register.go diff --git a/.gitignore b/.gitignore index 0a9cdb8..4bd1d0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ act_runner .env +.runner diff --git a/client/http.go b/client/http.go index 88a7126..e937989 100644 --- a/client/http.go +++ b/client/http.go @@ -1,22 +1,19 @@ package client import ( - "context" "crypto/tls" "net" "net/http" "time" - "gitea.com/gitea/act_runner/core" "gitea.com/gitea/proto-go/ping/v1/pingv1connect" "gitea.com/gitea/proto-go/runner/v1/runnerv1connect" - "github.com/bufbuild/connect-go" "golang.org/x/net/http2" ) // New returns a new runner client. -func New(endpoint, secret string, opts ...Option) *HTTPClient { +func New(endpoint string, opts ...Option) *HTTPClient { cfg := &config{} // Loop through each option @@ -25,15 +22,6 @@ func New(endpoint, secret string, opts ...Option) *HTTPClient { opt.apply(cfg) } - interceptor := connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc { - return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) { - req.Header().Set(core.UUIDHeader, secret) - return next(ctx, req) - } - }) - - cfg.opts = append(cfg.opts, connect.WithInterceptors(interceptor)) - if cfg.httpClient == nil { cfg.httpClient = &http.Client{ Timeout: 1 * time.Minute, diff --git a/client/options.go b/client/options.go index bd529ac..9a31300 100644 --- a/client/options.go +++ b/client/options.go @@ -1,8 +1,11 @@ package client import ( + "context" "net/http" + "gitea.com/gitea/act_runner/core" + "github.com/bufbuild/connect-go" ) @@ -56,3 +59,21 @@ func WithGRPCWeb(c bool) Option { cfg.opts = append(cfg.opts, connect.WithGRPCWeb()) }) } + +// WithUUIDHeader add runner uuid in header +func WithUUIDHeader(uuid string) Option { + return OptionFunc(func(cfg *config) { + if uuid == "" { + return + } + cfg.opts = append( + cfg.opts, + connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc { + return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) { + req.Header().Set(core.UUIDHeader, uuid) + return next(ctx, req) + } + })), + ) + }) +} diff --git a/cmd/daemon.go b/cmd/daemon.go index 4c58eb3..db876b4 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -8,6 +8,7 @@ import ( "gitea.com/gitea/act_runner/config" "gitea.com/gitea/act_runner/engine" "gitea.com/gitea/act_runner/poller" + "gitea.com/gitea/act_runner/register" "gitea.com/gitea/act_runner/runtime" pingv1 "gitea.com/gitea/proto-go/ping/v1" runnerv1 "gitea.com/gitea/proto-go/runner/v1" @@ -32,15 +33,9 @@ func runDaemon(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command, initLogging(cfg) - // try to connect to docker daemon - // if failed, exit with error - if err := engine.Start(ctx); err != nil { - log.WithError(err).Fatalln("failed to connect docker daemon engine") - } - + // initial http client cli := client.New( cfg.Client.Address, - cfg.Client.Secret, client.WithSkipVerify(cfg.Client.SkipVerify), client.WithGRPC(cfg.Client.GRPC), client.WithGRPCWeb(cfg.Client.GRPCWeb), @@ -69,8 +64,42 @@ func runDaemon(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command, } } + // register new runner + if cfg.Runner.UUID == "" { + register := register.New( + cli, + &client.Filter{ + OS: cfg.Platform.OS, + Arch: cfg.Platform.Arch, + Labels: cfg.Runner.Labels, + }, + ) + + data, err := register.Register(ctx, cfg.Runner) + if err != nil { + return err + } + if data.UUID != "" { + cfg.Runner.UUID = data.UUID + } + } + + // try to connect to docker daemon + // if failed, exit with error + if err := engine.Start(ctx); err != nil { + log.WithError(err).Fatalln("failed to connect docker daemon engine") + } + var g errgroup.Group + cli = client.New( + cfg.Client.Address, + client.WithSkipVerify(cfg.Client.SkipVerify), + client.WithGRPC(cfg.Client.GRPC), + client.WithGRPCWeb(cfg.Client.GRPCWeb), + client.WithUUIDHeader(cfg.Runner.UUID), + ) + runner := &runtime.Runner{ Client: cli, Machine: cfg.Runner.Name, @@ -80,11 +109,6 @@ func runDaemon(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command, poller := poller.New( cli, runner.Run, - &client.Filter{ - OS: cfg.Platform.OS, - Arch: cfg.Platform.Arch, - Labels: cfg.Runner.Labels, - }, ) g.Go(func() error { diff --git a/cmd/register.go b/cmd/register.go deleted file mode 100644 index 72aa280..0000000 --- a/cmd/register.go +++ /dev/null @@ -1,88 +0,0 @@ -package cmd - -import ( - "context" - "time" - - "gitea.com/gitea/act_runner/client" - "gitea.com/gitea/act_runner/config" - "gitea.com/gitea/act_runner/poller" - "gitea.com/gitea/act_runner/runtime" - pingv1 "gitea.com/gitea/proto-go/ping/v1" - - "github.com/bufbuild/connect-go" - "github.com/joho/godotenv" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" -) - -func runRegister(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command, args []string) error { - return func(cmd *cobra.Command, args []string) error { - log.Infoln("Starting runner daemon") - - _ = godotenv.Load(task.Input.EnvFile) - cfg, err := config.FromEnviron() - if err != nil { - log.WithError(err). - Fatalln("invalid configuration") - } - - initLogging(cfg) - - cli := client.New( - cfg.Client.Address, - cfg.Client.Secret, - client.WithSkipVerify(cfg.Client.SkipVerify), - client.WithGRPC(cfg.Client.GRPC), - client.WithGRPCWeb(cfg.Client.GRPCWeb), - ) - - for { - _, err := cli.Ping(ctx, connect.NewRequest(&pingv1.PingRequest{ - Data: cfg.Runner.Name, - })) - select { - case <-ctx.Done(): - return nil - default: - } - if ctx.Err() != nil { - break - } - if err != nil { - log.WithError(err). - Errorln("cannot ping the remote server") - // TODO: if ping failed, retry or exit - time.Sleep(time.Second) - } else { - log.Infoln("successfully connected the remote server") - break - } - } - - runner := &runtime.Runner{ - Client: cli, - Machine: cfg.Runner.Name, - Environ: cfg.Runner.Environ, - } - - poller := poller.New( - cli, - runner.Run, - &client.Filter{ - OS: cfg.Platform.OS, - Arch: cfg.Platform.Arch, - Labels: cfg.Runner.Labels, - }, - ) - - // register new runner - if err := poller.Register(ctx, cfg.Runner); err != nil { - return err - } - - log.Infoln("successfully registered new runner") - - return nil - } -} diff --git a/cmd/root.go b/cmd/root.go index 9fc3dda..09bab0d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -56,17 +56,8 @@ func Execute(ctx context.Context) { Args: cobra.MaximumNArgs(1), RunE: runDaemon(ctx, task), } - - // ./act_runner daemon - registerCmd := &cobra.Command{ - Aliases: []string{"register"}, - Use: "register new runner", - Args: cobra.MaximumNArgs(1), - RunE: runRegister(ctx, task), - } - // add all command - rootCmd.AddCommand(daemonCmd, registerCmd) + rootCmd.AddCommand(daemonCmd) if err := rootCmd.Execute(); err != nil { os.Exit(1) diff --git a/config/config.go b/config/config.go index 4a200fa..edcf8ec 100644 --- a/config/config.go +++ b/config/config.go @@ -27,13 +27,13 @@ type ( Address string `ignored:"true"` Proto string `envconfig:"GITEA_RPC_PROTO" default:"http"` Host string `envconfig:"GITEA_RPC_HOST"` - Secret string `envconfig:"GITEA_RPC_SECRET"` SkipVerify bool `envconfig:"GITEA_RPC_SKIP_VERIFY"` GRPC bool `envconfig:"GITEA_RPC_GRPC" default:"true"` GRPCWeb bool `envconfig:"GITEA_RPC_GRPC_WEB"` } Runner struct { + UUID string `ignored:"true"` Name string `envconfig:"GITEA_RUNNER_NAME"` Token string `envconfig:"GITEA_RUNNER_TOKEN" required:"true"` Capacity int `envconfig:"GITEA_RUNNER_CAPACITY" default:"1"` @@ -66,7 +66,7 @@ func FromEnviron() (Config, error) { return cfg, err } if runner.UUID != "" { - cfg.Client.Secret = runner.UUID + cfg.Runner.UUID = runner.UUID } } diff --git a/poller/poller.go b/poller/poller.go index 95f914a..74e7be5 100644 --- a/poller/poller.go +++ b/poller/poller.go @@ -2,17 +2,12 @@ package poller import ( "context" - "encoding/json" "errors" - "os" "time" "gitea.com/gitea/act_runner/client" - "gitea.com/gitea/act_runner/config" - "gitea.com/gitea/act_runner/core" runnerv1 "gitea.com/gitea/proto-go/runner/v1" - "github.com/appleboy/com/file" "github.com/bufbuild/connect-go" log "github.com/sirupsen/logrus" ) @@ -27,10 +22,9 @@ var ( defaultLabels = []string{"self-hosted"} ) -func New(cli client.Client, dispatch func(context.Context, *runnerv1.Task) error, filter *client.Filter) *Poller { +func New(cli client.Client, dispatch func(context.Context, *runnerv1.Task) error) *Poller { return &Poller{ Client: cli, - Filter: filter, Dispatch: dispatch, routineGroup: newRoutineGroup(), } @@ -45,41 +39,6 @@ type Poller struct { errorRetryCounter int } -func (p *Poller) Register(ctx context.Context, cfg config.Runner) error { - // check .runner config exist - if file.IsFile(cfg.File) { - return nil - } - - // register new runner. - resp, err := p.Client.Register(ctx, connect.NewRequest(&runnerv1.RegisterRequest{ - Name: cfg.Name, - Token: cfg.Token, - AgentLabels: append(defaultLabels, []string{p.Filter.OS, p.Filter.Arch}...), - CustomLabels: p.Filter.Labels, - })) - if err != nil { - log.WithError(err).Error("poller: cannot register new runner") - return err - } - - data := &core.Runner{ - ID: resp.Msg.Runner.Id, - UUID: resp.Msg.Runner.Uuid, - Name: resp.Msg.Runner.Name, - Token: resp.Msg.Runner.Token, - } - - file, err := json.MarshalIndent(data, "", " ") - if err != nil { - log.WithError(err).Error("poller: cannot marshal the json input") - return err - } - - // store runner config in .runner file - return os.WriteFile(cfg.File, file, 0o644) -} - func (p *Poller) Poll(ctx context.Context, n int) error { for i := 0; i < n; i++ { func(i int) { diff --git a/register/register.go b/register/register.go new file mode 100644 index 0000000..fb8e285 --- /dev/null +++ b/register/register.go @@ -0,0 +1,59 @@ +package register + +import ( + "context" + "encoding/json" + "os" + + "gitea.com/gitea/act_runner/client" + "gitea.com/gitea/act_runner/config" + "gitea.com/gitea/act_runner/core" + runnerv1 "gitea.com/gitea/proto-go/runner/v1" + + "github.com/bufbuild/connect-go" + log "github.com/sirupsen/logrus" +) + +var defaultLabels = []string{"self-hosted"} + +func New(cli client.Client, filter *client.Filter) *Register { + return &Register{ + Client: cli, + Filter: filter, + } +} + +type Register struct { + Client client.Client + Filter *client.Filter +} + +func (p *Register) Register(ctx context.Context, cfg config.Runner) (*core.Runner, error) { + // register new runner. + resp, err := p.Client.Register(ctx, connect.NewRequest(&runnerv1.RegisterRequest{ + Name: cfg.Name, + Token: cfg.Token, + AgentLabels: append(defaultLabels, []string{p.Filter.OS, p.Filter.Arch}...), + CustomLabels: p.Filter.Labels, + })) + if err != nil { + log.WithError(err).Error("poller: cannot register new runner") + return nil, err + } + + data := &core.Runner{ + ID: resp.Msg.Runner.Id, + UUID: resp.Msg.Runner.Uuid, + Name: resp.Msg.Runner.Name, + Token: resp.Msg.Runner.Token, + } + + file, err := json.MarshalIndent(data, "", " ") + if err != nil { + log.WithError(err).Error("poller: cannot marshal the json input") + return data, err + } + + // store runner config in .runner file + return data, os.WriteFile(cfg.File, file, 0o644) +}