From c4f2077f61bcc97a46050afd657785c458eaa367 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Fri, 22 Sep 2023 16:43:40 +0200 Subject: [PATCH 1/2] Some cleanups about host config --- cmd/server/server.go | 17 +++++++++-------- server/grpc/rpc.go | 1 - server/grpc/server.go | 3 +-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/cmd/server/server.go b/cmd/server/server.go index 0753deee917..ca17f66e1df 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -136,7 +136,6 @@ func run(c *cli.Context) error { server.Config.Services.Logs, server.Config.Services.Pubsub, _store, - server.Config.Server.Host, ) proto.RegisterWoodpeckerServer(grpcServer, woodpeckerServer) @@ -206,11 +205,9 @@ func run(c *cli.Context) error { // http to https redirect redirect := func(w http.ResponseWriter, req *http.Request) { - serverHost := server.Config.Server.Host - serverHost = strings.TrimPrefix(serverHost, "http://") - serverHost = strings.TrimPrefix(serverHost, "https://") + serverURL, _ := url.Parse(server.Config.Server.Host) req.URL.Scheme = "https" - req.URL.Host = serverHost + req.URL.Host = serverURL.Host w.Header().Set("Strict-Transport-Security", "max-age=31536000") @@ -327,16 +324,20 @@ func setupEvilGlobals(c *cli.Context, v store.Store, f forge.Forge) { server.Config.Server.Cert = c.String("server-cert") server.Config.Server.Key = c.String("server-key") server.Config.Server.AgentToken = c.String("agent-secret") - server.Config.Server.Host = c.String("server-host") + serverHost := c.String("server-host") + if _, err := url.Parse(serverHost); err != nil { + log.Fatal().Err(err).Msg("could not parse WOODPECKER_HOST") + } + server.Config.Server.Host = serverHost if c.IsSet("server-webhook-host") { server.Config.Server.WebhookHost = c.String("server-webhook-host") } else { - server.Config.Server.WebhookHost = c.String("server-host") + server.Config.Server.WebhookHost = serverHost } if c.IsSet("server-dev-oauth-host") { server.Config.Server.OAuthHost = c.String("server-dev-oauth-host") } else { - server.Config.Server.OAuthHost = c.String("server-host") + server.Config.Server.OAuthHost = serverHost } server.Config.Server.Port = c.String("server-addr") server.Config.Server.PortTLS = c.String("server-addr-tls") diff --git a/server/grpc/rpc.go b/server/grpc/rpc.go index 41ac02cf352..26a39cf1d12 100644 --- a/server/grpc/rpc.go +++ b/server/grpc/rpc.go @@ -46,7 +46,6 @@ type RPC struct { pubsub pubsub.Publisher logger logging.Log store store.Store - host string pipelineTime *prometheus.GaugeVec pipelineCount *prometheus.CounterVec } diff --git a/server/grpc/server.go b/server/grpc/server.go index 1cf1a1b50f6..353c6c86a4e 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -37,7 +37,7 @@ type WoodpeckerServer struct { peer RPC } -func NewWoodpeckerServer(forge forge.Forge, queue queue.Queue, logger logging.Log, pubsub pubsub.Publisher, store store.Store, host string) proto.WoodpeckerServer { +func NewWoodpeckerServer(forge forge.Forge, queue queue.Queue, logger logging.Log, pubsub pubsub.Publisher, store store.Store) proto.WoodpeckerServer { pipelineTime := promauto.NewGaugeVec(prometheus.GaugeOpts{ Namespace: "woodpecker", Name: "pipeline_time", @@ -54,7 +54,6 @@ func NewWoodpeckerServer(forge forge.Forge, queue queue.Queue, logger logging.Lo queue: queue, pubsub: pubsub, logger: logger, - host: host, pipelineTime: pipelineTime, pipelineCount: pipelineCount, } From 5ae47de248f9964b93da99d06dc73263c9b07a07 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Fri, 22 Sep 2023 16:49:25 +0200 Subject: [PATCH 2/2] improve parse check --- cmd/common/logger.go | 2 +- cmd/server/server.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/common/logger.go b/cmd/common/logger.go index 52ec424ca35..242f9343e6d 100644 --- a/cmd/common/logger.go +++ b/cmd/common/logger.go @@ -96,5 +96,5 @@ func SetupGlobalLogger(c *cli.Context) { log.Logger = log.With().Caller().Logger() } - log.Log().Msgf("LogLevel = %s", zerolog.GlobalLevel().String()) + log.Info().Msgf("LogLevel = %s", zerolog.GlobalLevel().String()) } diff --git a/cmd/server/server.go b/cmd/server/server.go index 1f0753e2b67..e30fe530278 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -73,6 +73,10 @@ func run(c *cli.Context) error { ) } + if _, err := url.Parse(c.String("server-host")); err != nil { + log.Fatal().Err(err).Msg("could not parse WOODPECKER_HOST") + } + if strings.Contains(c.String("server-host"), "://localhost") { log.Warn().Msg( "WOODPECKER_HOST should probably be publicly accessible (not localhost)", @@ -325,9 +329,6 @@ func setupEvilGlobals(c *cli.Context, v store.Store, f forge.Forge) { server.Config.Server.Key = c.String("server-key") server.Config.Server.AgentToken = c.String("agent-secret") serverHost := c.String("server-host") - if _, err := url.Parse(serverHost); err != nil { - log.Fatal().Err(err).Msg("could not parse WOODPECKER_HOST") - } server.Config.Server.Host = serverHost if c.IsSet("server-webhook-host") { server.Config.Server.WebhookHost = c.String("server-webhook-host")