2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-06-30 00:51:22 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-07-25 01:15:25 +02:00
|
|
|
"crypto/tls"
|
2017-06-30 01:35:38 +02:00
|
|
|
"errors"
|
2017-06-30 00:51:22 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-07-25 01:15:25 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-30 00:51:22 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2018-01-08 22:46:44 +02:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2017-06-30 01:35:38 +02:00
|
|
|
"google.golang.org/grpc/metadata"
|
2017-06-30 00:51:22 +02:00
|
|
|
|
|
|
|
"golang.org/x/crypto/acme/autocert"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2021-09-24 13:18:34 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc/proto"
|
2021-09-22 20:48:01 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
|
|
|
woodpeckerGrpcServer "github.com/woodpecker-ci/woodpecker/server/grpc"
|
2021-09-23 22:29:09 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/logging"
|
2021-09-23 16:12:46 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/plugins/sender"
|
2021-09-23 22:29:09 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/pubsub"
|
2021-09-23 18:25:51 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
2021-09-22 22:41:32 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/router"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/router/middleware"
|
2021-09-23 13:33:59 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2017-06-30 00:51:22 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/contrib/ginrus"
|
2020-05-18 16:46:13 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-06-30 00:51:22 +02:00
|
|
|
"github.com/urfave/cli"
|
2017-06-30 01:35:38 +02:00
|
|
|
oldcontext "golang.org/x/net/context"
|
2017-06-30 00:51:22 +02:00
|
|
|
)
|
|
|
|
|
2021-09-22 20:48:01 +02:00
|
|
|
func loop(c *cli.Context) error {
|
2017-06-30 00:51:22 +02:00
|
|
|
|
|
|
|
// debug level if requested by user
|
|
|
|
if c.Bool("debug") {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
} else {
|
|
|
|
logrus.SetLevel(logrus.WarnLevel)
|
|
|
|
}
|
|
|
|
|
2017-07-12 20:48:56 +02:00
|
|
|
// must configure the drone_host variable
|
|
|
|
if c.String("server-host") == "" {
|
2021-05-31 13:28:24 +02:00
|
|
|
logrus.Fatalln("DRONE_HOST/DRONE_SERVER_HOST/WOODPECKER_HOST/WOODPECKER_SERVER_HOST is not properly configured")
|
2017-07-12 20:48:56 +02:00
|
|
|
}
|
|
|
|
|
2017-12-20 04:08:55 +02:00
|
|
|
if !strings.Contains(c.String("server-host"), "://") {
|
|
|
|
logrus.Fatalln(
|
2021-05-31 13:28:24 +02:00
|
|
|
"DRONE_HOST/DRONE_SERVER_HOST/WOODPECKER_HOST/WOODPECKER_SERVER_HOST must be <scheme>://<hostname> format",
|
2017-12-20 04:08:55 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-20 16:32:52 +02:00
|
|
|
if strings.Contains(c.String("server-host"), "://localhost") {
|
|
|
|
logrus.Warningln(
|
|
|
|
"DRONE_HOST/DRONE_SERVER_HOST/WOODPECKER_HOST/WOODPECKER_SERVER_HOST should probably be publicly accessible (not localhost)",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-12-20 14:49:02 +02:00
|
|
|
if strings.HasSuffix(c.String("server-host"), "/") {
|
2017-12-20 04:08:55 +02:00
|
|
|
logrus.Fatalln(
|
2021-05-31 13:28:24 +02:00
|
|
|
"DRONE_HOST/DRONE_SERVER_HOST/WOODPECKER_HOST/WOODPECKER_SERVER_HOST must not have trailing slash",
|
2017-12-20 04:08:55 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-06-30 00:51:22 +02:00
|
|
|
remote_, err := SetupRemote(c)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
store_ := setupStore(c)
|
|
|
|
setupEvilGlobals(c, store_, remote_)
|
|
|
|
|
2017-07-31 21:15:05 +02:00
|
|
|
// we are switching from gin to httpservermux|treemux,
|
|
|
|
// so if this code looks strange, that is why.
|
|
|
|
tree := setupTree(c)
|
|
|
|
|
2017-06-30 00:51:22 +02:00
|
|
|
// setup the server and start the listener
|
|
|
|
handler := router.Load(
|
2017-07-31 21:15:05 +02:00
|
|
|
tree,
|
2017-06-30 00:51:22 +02:00
|
|
|
ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true),
|
|
|
|
middleware.Version,
|
|
|
|
middleware.Config(c),
|
|
|
|
middleware.Store(c, store_),
|
|
|
|
middleware.Remote(remote_),
|
|
|
|
)
|
|
|
|
|
|
|
|
var g errgroup.Group
|
|
|
|
|
|
|
|
// start the grpc server
|
|
|
|
g.Go(func() error {
|
|
|
|
|
2021-09-09 18:34:29 +02:00
|
|
|
lis, err := net.Listen("tcp", c.String("grpc-addr"))
|
2017-06-30 00:51:22 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
2017-06-30 01:35:38 +02:00
|
|
|
auther := &authorizer{
|
|
|
|
password: c.String("agent-secret"),
|
|
|
|
}
|
2019-06-28 14:23:52 +02:00
|
|
|
grpcServer := grpc.NewServer(
|
2017-06-30 01:35:38 +02:00
|
|
|
grpc.StreamInterceptor(auther.streamInterceptor),
|
|
|
|
grpc.UnaryInterceptor(auther.unaryIntercaptor),
|
2018-01-08 22:46:44 +02:00
|
|
|
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
|
|
|
|
MinTime: c.Duration("keepalive-min-time"),
|
|
|
|
}),
|
2017-06-30 01:35:38 +02:00
|
|
|
)
|
2021-09-22 20:48:01 +02:00
|
|
|
droneServer := woodpeckerGrpcServer.NewDroneServer(
|
|
|
|
remote_,
|
|
|
|
server.Config.Services.Queue,
|
|
|
|
server.Config.Services.Logs,
|
|
|
|
server.Config.Services.Pubsub,
|
|
|
|
store_,
|
|
|
|
server.Config.Server.Host,
|
|
|
|
)
|
2019-06-28 14:23:52 +02:00
|
|
|
proto.RegisterDroneServer(grpcServer, droneServer)
|
2017-06-30 00:51:22 +02:00
|
|
|
|
2019-06-28 14:23:52 +02:00
|
|
|
err = grpcServer.Serve(lis)
|
2017-06-30 00:51:22 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2019-05-30 12:15:29 +02:00
|
|
|
setupMetrics(&g, store_)
|
|
|
|
|
2017-06-30 00:51:22 +02:00
|
|
|
// start the server with tls enabled
|
|
|
|
if c.String("server-cert") != "" {
|
2017-07-26 16:44:38 +02:00
|
|
|
g.Go(func() error {
|
2017-11-15 00:01:07 +02:00
|
|
|
return http.ListenAndServe(":http", http.HandlerFunc(redirect))
|
2017-07-26 16:44:38 +02:00
|
|
|
})
|
|
|
|
g.Go(func() error {
|
2017-09-20 00:30:31 +02:00
|
|
|
serve := &http.Server{
|
|
|
|
Addr: ":https",
|
|
|
|
Handler: handler,
|
|
|
|
TLSConfig: &tls.Config{
|
|
|
|
NextProtos: []string{"http/1.1"}, // disable h2 because Safari :(
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return serve.ListenAndServeTLS(
|
2017-07-26 16:44:38 +02:00
|
|
|
c.String("server-cert"),
|
|
|
|
c.String("server-key"),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return g.Wait()
|
2017-06-30 00:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// start the server without tls enabled
|
|
|
|
if !c.Bool("lets-encrypt") {
|
|
|
|
return http.ListenAndServe(
|
|
|
|
c.String("server-addr"),
|
|
|
|
handler,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the server with lets encrypt enabled
|
|
|
|
// listen on ports 443 and 80
|
2018-01-13 06:54:49 +02:00
|
|
|
address, err := url.Parse(c.String("server-host"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := cacheDir()
|
|
|
|
os.MkdirAll(dir, 0700)
|
|
|
|
|
2018-01-18 18:33:41 +02:00
|
|
|
manager := &autocert.Manager{
|
2018-01-13 06:54:49 +02:00
|
|
|
Prompt: autocert.AcceptTOS,
|
|
|
|
HostPolicy: autocert.HostWhitelist(address.Host),
|
|
|
|
Cache: autocert.DirCache(dir),
|
|
|
|
}
|
2017-06-30 00:51:22 +02:00
|
|
|
g.Go(func() error {
|
2018-01-13 06:54:49 +02:00
|
|
|
return http.ListenAndServe(":http", manager.HTTPHandler(http.HandlerFunc(redirect)))
|
2017-06-30 00:51:22 +02:00
|
|
|
})
|
|
|
|
g.Go(func() error {
|
2017-09-20 00:30:31 +02:00
|
|
|
serve := &http.Server{
|
|
|
|
Addr: ":https",
|
|
|
|
Handler: handler,
|
|
|
|
TLSConfig: &tls.Config{
|
|
|
|
GetCertificate: manager.GetCertificate,
|
|
|
|
NextProtos: []string{"http/1.1"}, // disable h2 because Safari :(
|
|
|
|
},
|
2017-07-25 01:15:25 +02:00
|
|
|
}
|
2017-09-20 00:30:31 +02:00
|
|
|
return serve.ListenAndServeTLS("", "")
|
2017-06-30 00:51:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupEvilGlobals(c *cli.Context, v store.Store, r remote.Remote) {
|
|
|
|
|
|
|
|
// storage
|
2021-09-22 20:48:01 +02:00
|
|
|
server.Config.Storage.Files = v
|
|
|
|
server.Config.Storage.Config = v
|
2017-06-30 00:51:22 +02:00
|
|
|
|
|
|
|
// services
|
2021-09-22 20:48:01 +02:00
|
|
|
server.Config.Services.Queue = setupQueue(c, v)
|
|
|
|
server.Config.Services.Logs = logging.New()
|
|
|
|
server.Config.Services.Pubsub = pubsub.New()
|
|
|
|
server.Config.Services.Pubsub.Create(context.Background(), "topic/events")
|
|
|
|
server.Config.Services.Registries = setupRegistryService(c, v)
|
|
|
|
server.Config.Services.Secrets = setupSecretService(c, v)
|
|
|
|
server.Config.Services.Senders = sender.New(v, v)
|
|
|
|
server.Config.Services.Environ = setupEnvironService(c, v)
|
2017-06-30 00:51:22 +02:00
|
|
|
|
|
|
|
if endpoint := c.String("gating-service"); endpoint != "" {
|
2021-09-22 20:48:01 +02:00
|
|
|
server.Config.Services.Senders = sender.NewRemote(endpoint)
|
2017-06-30 00:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// limits
|
2021-09-22 20:48:01 +02:00
|
|
|
server.Config.Pipeline.Limits.MemSwapLimit = c.Int64("limit-mem-swap")
|
|
|
|
server.Config.Pipeline.Limits.MemLimit = c.Int64("limit-mem")
|
|
|
|
server.Config.Pipeline.Limits.ShmSize = c.Int64("limit-shm-size")
|
|
|
|
server.Config.Pipeline.Limits.CPUQuota = c.Int64("limit-cpu-quota")
|
|
|
|
server.Config.Pipeline.Limits.CPUShares = c.Int64("limit-cpu-shares")
|
|
|
|
server.Config.Pipeline.Limits.CPUSet = c.String("limit-cpu-set")
|
2017-06-30 00:51:22 +02:00
|
|
|
|
|
|
|
// server configuration
|
2021-09-22 20:48:01 +02:00
|
|
|
server.Config.Server.Cert = c.String("server-cert")
|
|
|
|
server.Config.Server.Key = c.String("server-key")
|
|
|
|
server.Config.Server.Pass = c.String("agent-secret")
|
|
|
|
server.Config.Server.Host = c.String("server-host")
|
|
|
|
server.Config.Server.Port = c.String("server-addr")
|
|
|
|
server.Config.Server.RepoConfig = c.String("repo-config")
|
|
|
|
server.Config.Server.SessionExpires = c.Duration("session-expires")
|
|
|
|
server.Config.Pipeline.Networks = c.StringSlice("network")
|
|
|
|
server.Config.Pipeline.Volumes = c.StringSlice("volume")
|
|
|
|
server.Config.Pipeline.Privileged = c.StringSlice("escalate")
|
2018-03-21 14:51:54 +02:00
|
|
|
|
|
|
|
// prometheus
|
2021-09-22 20:48:01 +02:00
|
|
|
server.Config.Prometheus.AuthToken = c.String("prometheus-auth-token")
|
2017-06-30 00:51:22 +02:00
|
|
|
}
|
2017-06-30 01:35:38 +02:00
|
|
|
|
|
|
|
type authorizer struct {
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *authorizer) streamInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
|
|
|
if err := a.authorize(stream.Context()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return handler(srv, stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *authorizer) unaryIntercaptor(ctx oldcontext.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
|
|
if err := a.authorize(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *authorizer) authorize(ctx context.Context) error {
|
2020-05-18 16:46:13 +02:00
|
|
|
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
2017-06-30 01:35:38 +02:00
|
|
|
if len(md["password"]) > 0 && md["password"][0] == a.password {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("invalid agent token")
|
|
|
|
}
|
|
|
|
return errors.New("missing agent token")
|
|
|
|
}
|
2017-07-25 01:15:25 +02:00
|
|
|
|
2017-11-15 00:01:07 +02:00
|
|
|
func redirect(w http.ResponseWriter, req *http.Request) {
|
2021-09-22 20:48:01 +02:00
|
|
|
var serverHost string = server.Config.Server.Host
|
2017-11-15 00:01:07 +02:00
|
|
|
serverHost = strings.TrimPrefix(serverHost, "http://")
|
|
|
|
serverHost = strings.TrimPrefix(serverHost, "https://")
|
|
|
|
req.URL.Scheme = "https"
|
|
|
|
req.URL.Host = serverHost
|
2017-11-16 06:27:57 +02:00
|
|
|
|
|
|
|
w.Header().Set("Strict-Transport-Security", "max-age=31536000")
|
|
|
|
|
2017-11-15 00:01:07 +02:00
|
|
|
http.Redirect(w, req, req.URL.String(), http.StatusMovedPermanently)
|
|
|
|
}
|
|
|
|
|
2017-07-25 01:15:25 +02:00
|
|
|
func cacheDir() string {
|
|
|
|
const base = "golang-autocert"
|
|
|
|
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
|
|
|
|
return filepath.Join(xdg, base)
|
|
|
|
}
|
|
|
|
return filepath.Join(os.Getenv("HOME"), ".cache", base)
|
|
|
|
}
|