Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions engine/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ type Configuration struct {
API string `toml:"api" default:"http://localhost:8081" json:"api"`
UI string `toml:"ui" default:"http://localhost:8080" json:"ui"`
} `toml:"url" comment:"#####################\n CDS URLs Settings \n####################" json:"url"`
HTTP struct {
Addr string `toml:"addr" default:"" commented:"true" comment:"Listen HTTP address without port, example: 127.0.0.1" json:"addr"`
Port int `toml:"port" default:"8081" json:"port"`
} `toml:"http" json:"http"`
HTTP service.HTTPRouterConfiguration `toml:"http" json:"http"`
Secrets struct {
Key string `toml:"key" json:"-"`
} `toml:"secrets" json:"secrets"`
Expand Down Expand Up @@ -552,6 +549,7 @@ func (a *API) Serve(ctx context.Context) error {
a.Router = &Router{
Mux: mux.NewRouter(),
Background: ctx,
Config: a.Config.HTTP,
}
a.InitRouter()
if err := a.initWebsocket(event.DefaultPubSubKey); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func (api *API) InitRouter() {
r.Handle("/template/{groupName}/{templateSlug}/usage", Scope(sdk.AuthConsumerScopeTemplate), r.GET(api.getTemplateUsageHandler))

//Not Found handler
r.Mux.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
r.Mux.NotFoundHandler = http.HandlerFunc(r.NotFoundHandler)

r.computeScopeDetails()
}
28 changes: 25 additions & 3 deletions engine/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Router struct {
nbPanic int
lastPanic *time.Time
scopeDetails []sdk.AuthConsumerScopeDetail
Config service.HTTPRouterConfiguration
}

// HandlerConfigFunc is a type used in the router configuration fonction "Handle"
Expand Down Expand Up @@ -224,7 +225,6 @@ func (r *Router) computeScopeDetails() {
Methods: methods,
})
}

details[i].Scope = scope
details[i].Endpoints = endpoints
}
Expand Down Expand Up @@ -340,13 +340,21 @@ func (r *Router) handle(uri string, scope HandlerScope, handlers ...*service.Han
telemetry.Path, req.URL.Path,
telemetry.Method, req.Method)

// Retrieve the client ip address from the header (X-Forwarded-For by default)
clientIP := req.Header.Get(r.Config.HeaderXForwardedFor)
if clientIP == "" {
// If the header has not been found, fallback on the remote adress from the http request
clientIP = req.RemoteAddr
}

// Prepare logging fields
ctx = context.WithValue(ctx, cdslog.Method, req.Method)
ctx = context.WithValue(ctx, cdslog.Route, cleanURL)
ctx = context.WithValue(ctx, cdslog.RequestURI, req.RequestURI)
ctx = context.WithValue(ctx, cdslog.Deprecated, rc.IsDeprecated)
ctx = context.WithValue(ctx, cdslog.Handler, rc.Name)
ctx = context.WithValue(ctx, cdslog.Action, rc.Name)
ctx = context.WithValue(ctx, cdslog.IPAddress, clientIP)

var fields = mux.Vars(req)
for k, v := range fields {
Expand Down Expand Up @@ -534,8 +542,22 @@ func MaintenanceAware() service.HandlerConfigParam {
}

// NotFoundHandler is called by default by Mux is any matching handler has been found
func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
service.WriteError(context.Background(), w, req, sdk.NewError(sdk.ErrNotFound, fmt.Errorf("%s not found", req.URL.Path)))
func (r *Router) NotFoundHandler(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()

// Retrieve the client ip address from the header (X-Forwarded-For by default)
clientIP := req.Header.Get(r.Config.HeaderXForwardedFor)
if clientIP == "" {
// If the header has not been found, fallback on the remote adress from the http request
clientIP = req.RemoteAddr
}

// Prepare logging fields
ctx = context.WithValue(ctx, cdslog.Method, req.Method)
ctx = context.WithValue(ctx, cdslog.RequestURI, req.RequestURI)
ctx = context.WithValue(ctx, cdslog.IPAddress, clientIP)

service.WriteError(ctx, w, req, sdk.NewError(sdk.ErrNotFound, fmt.Errorf("%s not found", req.URL.Path)))
}

// StatusPanic returns router status. If nbPanic > 30 -> Alert, if nbPanic > 0 -> Warn
Expand Down
8 changes: 4 additions & 4 deletions engine/cdn/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ const (
func New() *Service {
s := new(Service)
s.GoRoutines = sdk.NewGoRoutines()
s.Router = &api.Router{
Mux: mux.NewRouter(),
}

return s
}
Expand All @@ -50,7 +47,10 @@ func (s *Service) Init(config interface{}) (cdsclient.ServiceConfig, error) {
if !ok {
return cfg, sdk.WithStack(fmt.Errorf("invalid CDN service configuration"))
}

s.Router = &api.Router{
Mux: mux.NewRouter(),
Config: sConfig.HTTP,
}
cfg.Host = sConfig.API.HTTP.URL
cfg.Token = sConfig.API.Token
cfg.InsecureSkipVerifyTLS = sConfig.API.HTTP.Insecure
Expand Down
9 changes: 3 additions & 6 deletions engine/cdn/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ type Service struct {

// Configuration is the hooks configuration structure
type Configuration struct {
Name string `toml:"name" default:"cds-cdn" comment:"Name of this CDS CDN Service\n Enter a name to enable this service" json:"name"`
TCP sdk.TCPServer `toml:"tcp" comment:"######################\n CDS CDN TCP Configuration \n######################" json:"tcp"`
HTTP struct {
Addr string `toml:"addr" default:"" commented:"true" comment:"Listen address without port, example: 127.0.0.1" json:"addr"`
Port int `toml:"port" default:"8089" json:"port"`
} `toml:"http" comment:"######################\n CDS CDN HTTP Configuration \n######################" json:"http"`
Name string `toml:"name" default:"cds-cdn" comment:"Name of this CDS CDN Service\n Enter a name to enable this service" json:"name"`
TCP sdk.TCPServer `toml:"tcp" comment:"######################\n CDS CDN TCP Configuration \n######################" json:"tcp"`
HTTP service.HTTPRouterConfiguration `toml:"http" comment:"######################\n CDS CDN HTTP Configuration \n######################" json:"http"`
URL string `default:"http://localhost:8089" json:"url" comment:"Private URL for communication with API"`
PublicTCP string `toml:"publicTCP" default:"localhost:8090" comment:"Public address to access to CDN TCP server" json:"public_tcp"`
PublicHTTP string `toml:"publicHTTP" default:"http://localhost:8089" comment:"Public address to access to CDN HTTP server" json:"public_http"`
Expand Down
14 changes: 14 additions & 0 deletions engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,39 @@ func configBootstrap(args []string) Configuration {
HealthURL: "https://ovh.github.io",
Type: "doc",
})
conf.API.HTTP.Port = 8081
case sdk.TypeUI:
conf.UI = &ui.Configuration{}
conf.UI.Name = "cds-ui-" + namesgenerator.GetRandomNameCDS(0)
defaults.SetDefaults(conf.UI)
conf.UI.HTTP.Port = 8080
case "migrate":
conf.DatabaseMigrate = &migrateservice.Configuration{}
defaults.SetDefaults(conf.DatabaseMigrate)
conf.DatabaseMigrate.Name = "cds-migrate-" + namesgenerator.GetRandomNameCDS(0)
conf.DatabaseMigrate.ServiceAPI.DB.Schema = "public"
conf.DatabaseMigrate.ServiceCDN.DB.Schema = "cdn"
conf.DatabaseMigrate.HTTP.Port = 8087
case sdk.TypeHatchery + ":local":
conf.Hatchery.Local = &local.HatcheryConfiguration{}
defaults.SetDefaults(conf.Hatchery.Local)
conf.Hatchery.Local.Name = "cds-hatchery-local-" + namesgenerator.GetRandomNameCDS(0)
conf.Hatchery.Local.HTTP.Port = 8086
case sdk.TypeHatchery + ":kubernetes":
conf.Hatchery.Kubernetes = &kubernetes.HatcheryConfiguration{}
defaults.SetDefaults(conf.Hatchery.Kubernetes)
conf.Hatchery.Kubernetes.Name = "cds-hatchery-kubernetes-" + namesgenerator.GetRandomNameCDS(0)
conf.Hatchery.Kubernetes.HTTP.Port = 8086
case sdk.TypeHatchery + ":marathon":
conf.Hatchery.Marathon = &marathon.HatcheryConfiguration{}
defaults.SetDefaults(conf.Hatchery.Marathon)
conf.Hatchery.Marathon.Name = "cds-hatchery-marathon-" + namesgenerator.GetRandomNameCDS(0)
conf.Hatchery.Marathon.HTTP.Port = 8086
case sdk.TypeHatchery + ":openstack":
conf.Hatchery.Openstack = &openstack.HatcheryConfiguration{}
defaults.SetDefaults(conf.Hatchery.Openstack)
conf.Hatchery.Openstack.Name = "cds-hatchery-openstack-" + namesgenerator.GetRandomNameCDS(0)
conf.Hatchery.Openstack.HTTP.Port = 8086
case sdk.TypeHatchery + ":swarm":
conf.Hatchery.Swarm = &swarm.HatcheryConfiguration{}
defaults.SetDefaults(conf.Hatchery.Swarm)
Expand All @@ -115,14 +122,17 @@ func configBootstrap(args []string) Configuration {
},
}
conf.Hatchery.Swarm.Name = "cds-hatchery-swarm-" + namesgenerator.GetRandomNameCDS(0)
conf.Hatchery.Swarm.HTTP.Port = 8086
case sdk.TypeHatchery + ":vsphere":
conf.Hatchery.VSphere = &vsphere.HatcheryConfiguration{}
defaults.SetDefaults(conf.Hatchery.VSphere)
conf.Hatchery.VSphere.Name = "cds-hatchery-vsphere-" + namesgenerator.GetRandomNameCDS(0)
conf.Hatchery.VSphere.HTTP.Port = 8086
case sdk.TypeHooks:
conf.Hooks = &hooks.Configuration{}
defaults.SetDefaults(conf.Hooks)
conf.Hooks.Name = "cds-hooks-" + namesgenerator.GetRandomNameCDS(0)
conf.Hooks.HTTP.Port = 8083
case sdk.TypeVCS:
conf.VCS = &vcs.Configuration{}
defaults.SetDefaults(conf.VCS)
Expand All @@ -144,13 +154,16 @@ func configBootstrap(args []string) Configuration {
"gerrit": {URL: "http://localhost:8080", Gerrit: &gerrit},
}
conf.VCS.Name = "cds-vcs-" + namesgenerator.GetRandomNameCDS(0)
conf.VCS.HTTP.Port = 8084
case sdk.TypeRepositories:
conf.Repositories = &repositories.Configuration{}
defaults.SetDefaults(conf.Repositories)
conf.Repositories.Name = "cds-repositories-" + namesgenerator.GetRandomNameCDS(0)
conf.Repositories.HTTP.Port = 8085
case sdk.TypeCDN:
conf.CDN = &cdn.Configuration{}
defaults.SetDefaults(conf.CDN)
conf.CDN.HTTP.Port = 8089
conf.CDN.Database.Schema = "cdn"
conf.CDN.Units.HashLocatorSalt = sdk.RandomString(8)
conf.CDN.Units.Buffers = map[string]storage.BufferConfiguration{
Expand Down Expand Up @@ -193,6 +206,7 @@ func configBootstrap(args []string) Configuration {
case sdk.TypeElasticsearch:
conf.ElasticSearch = &elasticsearch.Configuration{}
defaults.SetDefaults(conf.ElasticSearch)
conf.ElasticSearch.HTTP.Port = 8088
default:
sdk.Exit("Error service '%s' is unknown", a)
}
Expand Down
8 changes: 4 additions & 4 deletions engine/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ var esClient *elastic.Client
func New() *Service {
s := new(Service)
s.GoRoutines = sdk.NewGoRoutines()
s.Router = &api.Router{
Mux: mux.NewRouter(),
}
return s
}

Expand All @@ -37,7 +34,10 @@ func (s *Service) ApplyConfiguration(config interface{}) error {
if !ok {
return fmt.Errorf("ApplyConfiguration> Invalid Elasticsearch configuration")
}

s.Router = &api.Router{
Mux: mux.NewRouter(),
Config: s.Cfg.HTTP,
}
s.HTTPURL = s.Cfg.URL
s.ServiceName = s.Cfg.Name
s.ServiceType = sdk.TypeElasticsearch
Expand Down
9 changes: 3 additions & 6 deletions engine/elasticsearch/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ type Service struct {

// Configuration is the vcs configuration structure
type Configuration struct {
Name string `toml:"name" comment:"Name of this CDS elasticsearch Service\n Enter a name to enable this service" json:"name"`
HTTP struct {
Addr string `toml:"addr" default:"" commented:"true" comment:"Listen address without port, example: 127.0.0.1" json:"addr"`
Port int `toml:"port" default:"8088" json:"port"`
} `toml:"http" comment:"######################\n CDS Elasticsearch HTTP Configuration \n######################" json:"http"`
URL string `default:"http://localhost:8088" json:"url"`
Name string `toml:"name" comment:"Name of this CDS elasticsearch Service\n Enter a name to enable this service" json:"name"`
HTTP service.HTTPRouterConfiguration `toml:"http" comment:"######################\n CDS Elasticsearch HTTP Configuration \n######################" json:"http"`
URL string `default:"http://localhost:8088" json:"url"`
ElasticSearch struct {
URL string `toml:"url" json:"url"`
Username string `toml:"username" json:"username"`
Expand Down
8 changes: 5 additions & 3 deletions engine/hatchery/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ import (
func New() *HatcheryKubernetes {
s := new(HatcheryKubernetes)
s.GoRoutines = sdk.NewGoRoutines()
s.Router = &api.Router{
Mux: mux.NewRouter(),
}
return s
}

Expand All @@ -57,6 +54,11 @@ func (h *HatcheryKubernetes) Init(config interface{}) (cdsclient.ServiceConfig,
return cfg, sdk.WithStack(fmt.Errorf("invalid kubernetes hatchery configuration"))
}

h.Router = &api.Router{
Mux: mux.NewRouter(),
Config: sConfig.HTTP,
}

cfg.Host = sConfig.API.HTTP.URL
cfg.Token = sConfig.API.Token
cfg.InsecureSkipVerifyTLS = sConfig.API.HTTP.Insecure
Expand Down
8 changes: 4 additions & 4 deletions engine/hatchery/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import (
func New() *HatcheryLocal {
s := new(HatcheryLocal)
s.GoRoutines = sdk.NewGoRoutines()
s.Router = &api.Router{
Mux: mux.NewRouter(),
}
s.LocalWorkerRunner = new(localWorkerRunner)
return s
}
Expand All @@ -42,7 +39,10 @@ func (h *HatcheryLocal) Init(config interface{}) (cdsclient.ServiceConfig, error
if !ok {
return cfg, sdk.WithStack(fmt.Errorf("invalid local hatchery configuration"))
}

h.Router = &api.Router{
Mux: mux.NewRouter(),
Config: sConfig.HTTP,
}
cfg.Host = sConfig.API.HTTP.URL
cfg.Token = sConfig.API.Token
cfg.InsecureSkipVerifyTLS = sConfig.API.HTTP.Insecure
Expand Down
8 changes: 4 additions & 4 deletions engine/hatchery/marathon/marathon.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ import (
func New() *HatcheryMarathon {
s := new(HatcheryMarathon)
s.GoRoutines = sdk.NewGoRoutines()
s.Router = &api.Router{
Mux: mux.NewRouter(),
}
return s
}

Expand All @@ -50,7 +47,10 @@ func (h *HatcheryMarathon) Init(config interface{}) (cdsclient.ServiceConfig, er
if !ok {
return cfg, sdk.WithStack(fmt.Errorf("invalid marathon hatchery configuration"))
}

h.Router = &api.Router{
Mux: mux.NewRouter(),
Config: sConfig.HTTP,
}
cfg.Host = sConfig.API.HTTP.URL
cfg.Token = sConfig.API.Token
cfg.InsecureSkipVerifyTLS = sConfig.API.HTTP.Insecure
Expand Down
8 changes: 4 additions & 4 deletions engine/hatchery/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ var _ hatchery.InterfaceWithModels = new(HatcheryOpenstack)
func New() *HatcheryOpenstack {
s := new(HatcheryOpenstack)
s.GoRoutines = sdk.NewGoRoutines()
s.Router = &api.Router{
Mux: mux.NewRouter(),
}
return s
}

Expand All @@ -54,7 +51,10 @@ func (h *HatcheryOpenstack) Init(config interface{}) (cdsclient.ServiceConfig, e
if !ok {
return cfg, sdk.WithStack(fmt.Errorf("invalid openstack hatchery configuration"))
}

h.Router = &api.Router{
Mux: mux.NewRouter(),
Config: sConfig.HTTP,
}
cfg.Host = sConfig.API.HTTP.URL
cfg.Token = sConfig.API.Token
cfg.InsecureSkipVerifyTLS = sConfig.API.HTTP.Insecure
Expand Down
2 changes: 1 addition & 1 deletion engine/hatchery/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *Common) initRouter(ctx context.Context, h hatchery.Interface) {
r.Mux.HandleFunc("/debug/pprof/{action}", pprof.Index)
r.Mux.HandleFunc("/debug/pprof/", pprof.Index)

r.Mux.NotFoundHandler = http.HandlerFunc(api.NotFoundHandler)
r.Mux.NotFoundHandler = http.HandlerFunc(r.NotFoundHandler)
}

func (c *Common) GetPrivateKey() *rsa.PrivateKey {
Expand Down
5 changes: 0 additions & 5 deletions engine/hatchery/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import (
"github.com/docker/docker/api/types"
docker "github.com/docker/docker/client"
"github.com/docker/go-connections/tlsconfig"
"github.com/gorilla/mux"
"github.com/rockbears/log"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"

"github.com/ovh/cds/engine/api"
"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdn"
Expand All @@ -35,9 +33,6 @@ import (
func New() *HatcherySwarm {
s := new(HatcherySwarm)
s.GoRoutines = sdk.NewGoRoutines()
s.Router = &api.Router{
Mux: mux.NewRouter(),
}
return s
}

Expand Down
7 changes: 6 additions & 1 deletion engine/hatchery/swarm/swarm_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (

"github.com/dgrijalva/jwt-go"
types "github.com/docker/docker/api/types"
"github.com/gorilla/mux"
"github.com/rockbears/log"

"github.com/ovh/cds/engine/api"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdsclient"
)
Expand All @@ -20,7 +22,10 @@ func (h *HatcherySwarm) Init(config interface{}) (cdsclient.ServiceConfig, error
if !ok {
return cfg, sdk.WithStack(fmt.Errorf("invalid swarm hatchery configuration"))
}

h.Router = &api.Router{
Mux: mux.NewRouter(),
Config: sConfig.HTTP,
}
cfg.Host = sConfig.API.HTTP.URL
cfg.Token = sConfig.API.Token
cfg.InsecureSkipVerifyTLS = sConfig.API.HTTP.Insecure
Expand Down
Loading