Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(service): add ip address in router logs #5789

Merged
merged 6 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
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
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
7 changes: 2 additions & 5 deletions engine/hooks/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ type Service struct {

// Configuration is the hooks configuration structure
type Configuration struct {
Name string `toml:"name" comment:"Name of this CDS Hooks 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:"8083" json:"port"`
} `toml:"http" comment:"######################\n CDS Hooks HTTP Configuration \n######################" json:"http"`
Name string `toml:"name" comment:"Name of this CDS Hooks Service\n Enter a name to enable this service" json:"name"`
HTTP service.HTTPRouterConfiguration `toml:"http" comment:"######################\n CDS Hooks HTTP Configuration \n######################" json:"http"`
URL string `toml:"url" default:"http://localhost:8083" json:"url"`
URLPublic string `toml:"urlPublic" default:"http://localhost:8080/cdshooks" comment:"Public url for external call (webhook)" json:"urlPublic"`
RetryDelay int64 `toml:"retryDelay" default:"120" comment:"Execution retry delay in seconds" json:"retryDelay"`
Expand Down
12 changes: 4 additions & 8 deletions engine/migrateservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ var _ service.BeforeStart = new(dbmigservice)

// Configuration is the exposed type for database API configuration
type Configuration struct {
Name string `toml:"name" comment:"Name of this CDS Database Migrate service\n Enter a name to enable this service" json:"name"`
URL string `default:"http://localhost:8087" json:"url"`
Directory string `toml:"directory" comment:"SQL Migration files directory" default:"sql" json:"directory"`
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:"8087" json:"port"`
Insecure bool `toml:"insecure" default:"false" commented:"true" comment:"sslInsecureSkipVerify, set to true if you use a self-signed SSL on CDS API" json:"insecure"`
} `toml:"http" comment:"#####################################\n CDS DB Migrate HTTP configuration \n####################################" json:"http"`
Name string `toml:"name" comment:"Name of this CDS Database Migrate service\n Enter a name to enable this service" json:"name"`
URL string `default:"http://localhost:8087" json:"url"`
Directory string `toml:"directory" comment:"SQL Migration files directory" default:"sql" json:"directory"`
HTTP service.HTTPRouterConfiguration `toml:"http" comment:"#####################################\n CDS DB Migrate HTTP configuration \n####################################" json:"http"`
API service.APIServiceConfiguration `toml:"api" comment:"####################\n CDS API Settings \n###################" json:"api"`
ServiceAPI struct {
Enable bool `toml:"enable" default:"true" comment:"set to false to disable migration for API database" json:"enable"`
Expand Down
19 changes: 8 additions & 11 deletions engine/repositories/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ type Service struct {

// Configuration is the vcs configuration structure
type Configuration struct {
Name string `toml:"name" comment:"Name of this CDS Repositories Service\n Enter a name to enable this service" json:"name"`
Basedir string `toml:"basedir" comment:"Root directory where the service will store all checked-out repositories" json:"basedir"`
OperationRetention int `toml:"operationRetention" comment:"Operation retention in redis store (in days)" default:"5" json:"operationRetention"`
RepositoriesRetention int `toml:"repositoriesRetention" comment:"Re retention on the filesystem (in days)" default:"10" json:"repositoriesRetention"`
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:"8085" json:"port"`
} `toml:"http" comment:"######################\n CDS Repositories HTTP Configuration \n######################" json:"http"`
URL string `default:"http://localhost:8085" json:"url"`
API service.APIServiceConfiguration `toml:"api" comment:"######################\n CDS API Settings \n######################" json:"api"`
Cache struct {
Name string `toml:"name" comment:"Name of this CDS Repositories Service\n Enter a name to enable this service" json:"name"`
Basedir string `toml:"basedir" comment:"Root directory where the service will store all checked-out repositories" json:"basedir"`
OperationRetention int `toml:"operationRetention" comment:"Operation retention in redis store (in days)" default:"5" json:"operationRetention"`
RepositoriesRetention int `toml:"repositoriesRetention" comment:"Re retention on the filesystem (in days)" default:"10" json:"repositoriesRetention"`
HTTP service.HTTPRouterConfiguration `toml:"http" comment:"######################\n CDS Repositories HTTP Configuration \n######################" json:"http"`
URL string `default:"http://localhost:8085" json:"url"`
API service.APIServiceConfiguration `toml:"api" comment:"######################\n CDS API Settings \n######################" json:"api"`
Cache struct {
TTL int `toml:"ttl" default:"60" json:"ttl"`
Redis struct {
Host string `toml:"host" default:"localhost:6379" comment:"If your want to use a redis-sentinel based cluster, follow this syntax! <clustername>@sentinel1:26379,sentinel2:26379,sentinel3:26379" json:"host"`
Expand Down
19 changes: 11 additions & 8 deletions engine/service/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ type APIServiceConfiguration struct {
MaxHeartbeatFailures int `toml:"maxHeartbeatFailures" default:"10" json:"maxHeartbeatFailures"`
}

type HTTPRouterConfiguration 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"`
HeaderXForwardedFor string `toml:"headerXForwardedFor" default:"X-Forwarded-For" json:"header_w_forwarded_for"`
}

// HatcheryCommonConfiguration is the base configuration for all hatcheries
type HatcheryCommonConfiguration struct {
Name string `toml:"name" default:"" comment:"Name of Hatchery" json:"name"`
RSAPrivateKey string `toml:"rsaPrivateKey" default:"" comment:"The RSA Private Key used by the hatchery.\nThis is mandatory." json:"-"`
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:"8086" json:"port"`
} `toml:"http" comment:"######################\n CDS Hatchery HTTP Configuration \n######################" json:"http"`
URL string `toml:"url" default:"http://localhost:8086" comment:"URL of this Hatchery" json:"url"`
API struct {
Name string `toml:"name" default:"" comment:"Name of Hatchery" json:"name"`
RSAPrivateKey string `toml:"rsaPrivateKey" default:"" comment:"The RSA Private Key used by the hatchery.\nThis is mandatory." json:"-"`
HTTP HTTPRouterConfiguration `toml:"http" comment:"######################\n CDS Hatchery HTTP Configuration \n######################" json:"http"`
URL string `toml:"url" default:"http://localhost:8086" comment:"URL of this Hatchery" json:"url"`
API struct {
HTTP struct {
URL string `toml:"url" default:"http://localhost:8081" comment:"CDS API URL" json:"url"`
Insecure bool `toml:"insecure" default:"false" commented:"true" comment:"sslInsecureSkipVerify, set to true if you use a self-signed SSL on CDS API" json:"insecure"`
Expand Down
23 changes: 10 additions & 13 deletions engine/ui/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ type Service struct {

// Configuration is the ui configuration structure
type Configuration struct {
Name string `toml:"name" comment:"Name of this CDS UI Service\n Enter a name to enable this service" json:"name"`
Staticdir string `toml:"staticdir" default:"./ui_static_files" comment:"This directory must contain the dist directory." json:"staticdir"`
BaseURL string `toml:"baseURL" commented:"true" comment:"If you expose CDS UI with https://your-domain.com/ui, enter the value '/ui/'. Optional" json:"baseURL"`
DeployURL string `toml:"deployURL" commented:"true" comment:"You can start CDS UI proxy on a sub path like https://your-domain.com/ui with value '/ui' (the value should not be given when the sub path is added by a proxy in front of CDS). Optional" json:"deployURL"`
SentryURL string `toml:"sentryURL" commented:"true" comment:"Sentry URL. Optional" json:"-"`
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:"8080" json:"port"`
} `toml:"http" comment:"######################\n CDS UI HTTP Configuration \n######################" json:"http"`
URL string `toml:"url" comment:"Public URL of this UI service." default:"http://localhost:8080" json:"url"`
API service.APIServiceConfiguration `toml:"api" comment:"######################\n CDS API Settings \n######################" json:"api"`
HooksURL string `toml:"hooksURL" comment:"Hooks µService URL" default:"http://localhost:8083" json:"hooksURL"`
CDNURL string `toml:"cdnURL" comment:"CDN µService URL" default:"http://localhost:8089" json:"cdnURL"`
Name string `toml:"name" comment:"Name of this CDS UI Service\n Enter a name to enable this service" json:"name"`
Staticdir string `toml:"staticdir" default:"./ui_static_files" comment:"This directory must contain the dist directory." json:"staticdir"`
BaseURL string `toml:"baseURL" commented:"true" comment:"If you expose CDS UI with https://your-domain.com/ui, enter the value '/ui/'. Optional" json:"baseURL"`
DeployURL string `toml:"deployURL" commented:"true" comment:"You can start CDS UI proxy on a sub path like https://your-domain.com/ui with value '/ui' (the value should not be given when the sub path is added by a proxy in front of CDS). Optional" json:"deployURL"`
SentryURL string `toml:"sentryURL" commented:"true" comment:"Sentry URL. Optional" json:"-"`
HTTP service.HTTPRouterConfiguration `toml:"http" comment:"######################\n CDS UI HTTP Configuration \n######################" json:"http"`
URL string `toml:"url" comment:"Public URL of this UI service." default:"http://localhost:8080" json:"url"`
API service.APIServiceConfiguration `toml:"api" comment:"######################\n CDS API Settings \n######################" json:"api"`
HooksURL string `toml:"hooksURL" comment:"Hooks µService URL" default:"http://localhost:8083" json:"hooksURL"`
CDNURL string `toml:"cdnURL" comment:"CDN µService URL" default:"http://localhost:8089" json:"cdnURL"`
}
11 changes: 4 additions & 7 deletions engine/vcs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ type Service struct {

// Configuration is the vcs configuration structure
type Configuration struct {
Name string `toml:"name" comment:"Name of this CDS VCS 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:"8084" json:"port"`
} `toml:"http" comment:"######################\n CDS VCS HTTP Configuration \n######################" json:"http"`
URL string `default:"http://localhost:8084" json:"url"`
UI struct {
Name string `toml:"name" comment:"Name of this CDS VCS Service\n Enter a name to enable this service" json:"name"`
HTTP service.HTTPRouterConfiguration `toml:"http" comment:"######################\n CDS VCS HTTP Configuration \n######################" json:"http"`
URL string `default:"http://localhost:8084" json:"url"`
UI struct {
HTTP struct {
URL string `toml:"url" default:"http://localhost:8080" json:"url"`
} `toml:"http" json:"http"`
Expand Down
2 changes: 2 additions & 0 deletions sdk/log/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
StatusNum = log.Field("status_num")
Goroutine = log.Field("goroutine")
RequestID = log.Field("request_id")
IPAddress = log.Field("ip_address")
Service = log.Field("service")
Stacktrace = log.Field("stack_trace")
Duration = log.Field("duration_milliseconds_num")
Expand All @@ -46,6 +47,7 @@ func init() {
AuthSessionID,
AuthSessionIAT,
AuthSessionTokenID,
IPAddress,
Method,
Route,
RequestURI,
Expand Down