From d178051832efa7b617b117c0d4c4dbe543d79c06 Mon Sep 17 00:00:00 2001 From: "Bo-Yi.Wu" Date: Fri, 28 Oct 2022 23:23:19 +0800 Subject: [PATCH] chore(runner): add new token in header Signed-off-by: Bo-Yi.Wu --- client/options.go | 18 ++++++++++++++++++ cmd/daemon.go | 4 ++++ config/config.go | 3 +++ core/runner.go | 5 ++++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/client/options.go b/client/options.go index 9a31300..92773b1 100644 --- a/client/options.go +++ b/client/options.go @@ -77,3 +77,21 @@ func WithUUIDHeader(uuid string) Option { ) }) } + +// WithTokenHeader add runner token in header +func WithTokenHeader(token string) Option { + return OptionFunc(func(cfg *config) { + if token == "" { + 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.TokenHeader, token) + return next(ctx, req) + } + })), + ) + }) +} diff --git a/cmd/daemon.go b/cmd/daemon.go index fe59232..50807c2 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -82,6 +82,9 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg if data.UUID != "" { cfg.Runner.UUID = data.UUID } + if data.Token != "" { + cfg.Runner.Token = data.Token + } } // try to connect to docker daemon @@ -98,6 +101,7 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg client.WithGRPC(cfg.Client.GRPC), client.WithGRPCWeb(cfg.Client.GRPCWeb), client.WithUUIDHeader(cfg.Runner.UUID), + client.WithTokenHeader(cfg.Runner.Token), ) runner := &runtime.Runner{ diff --git a/config/config.go b/config/config.go index db560c6..b5ce40b 100644 --- a/config/config.go +++ b/config/config.go @@ -69,6 +69,9 @@ func FromEnviron() (Config, error) { if runner.UUID != "" { cfg.Runner.UUID = runner.UUID } + if runner.Token != "" { + cfg.Runner.Token = runner.Token + } cfg.ForgeInstance = runner.ForgeInstance } diff --git a/core/runner.go b/core/runner.go index 94093a8..e41040e 100644 --- a/core/runner.go +++ b/core/runner.go @@ -1,6 +1,9 @@ package core -const UUIDHeader = "x-runner-uuid" +const ( + UUIDHeader = "x-runner-uuid" + TokenHeader = "x-runner-token" +) // Runner struct type Runner struct {