proxy access log

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
Jörn Friedrich Dreyer
2021-07-14 13:14:27 +00:00
parent 31918dd44a
commit 1dc7aac6fd
8 changed files with 67 additions and 14 deletions
+9
View File
@@ -15,6 +15,7 @@ import (
acc "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/owncloud/ocis/ocis-pkg/conversions"
"github.com/owncloud/ocis/ocis-pkg/log"
pkgmiddleware "github.com/owncloud/ocis/ocis-pkg/middleware"
"github.com/owncloud/ocis/ocis-pkg/service/grpc"
"github.com/owncloud/ocis/ocis-pkg/sync"
"github.com/owncloud/ocis/proxy/pkg/config"
@@ -176,7 +177,13 @@ func loadMiddlewares(ctx context.Context, l log.Logger, cfg *config.Config) alic
}
return alice.New(
// first make sure we log all requests and redirect to https if necessary
pkgmiddleware.RealIP,
pkgmiddleware.RequestID,
middleware.AccessLog(l),
middleware.HTTPSRedirect,
// now that we established the basics, on with authentication middleware
middleware.Authentication(
// OIDC Options
middleware.OIDCProviderFunc(func() (middleware.OIDCProvider, error) {
@@ -211,6 +218,8 @@ func loadMiddlewares(ctx context.Context, l log.Logger, cfg *config.Config) alic
middleware.TokenManagerConfig(cfg.TokenManager),
middleware.AutoprovisionAccounts(cfg.AutoprovisionAccounts),
),
// finally, trigger home creation when a user logs in
middleware.CreateHome(
middleware.Logger(l),
middleware.TokenManagerConfig(cfg.TokenManager),
+33
View File
@@ -0,0 +1,33 @@
package middleware
import (
"net/http"
"time"
"github.com/go-chi/chi/middleware"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/proxy/pkg/proxy/policy"
)
// AccessLog is a middleware to log http requests at info level logging.
func AccessLog(logger log.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
wrap := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(wrap, r)
logger.Info().
Str("proto", r.Proto).
Str("request", r.Header.Get("X-Request-ID")).
Str("remote-addr", r.RemoteAddr).
Str("method", r.Method).
Str("routing-policy", policy.ContextGetPolicy(r.Context())).
Int("status", wrap.Status()).
Str("path", r.URL.Path).
Dur("duration", time.Since(start)).
Int("bytes", wrap.BytesWritten()).
Msg("access-log")
})
}
}
+14
View File
@@ -20,6 +20,20 @@ var (
ErrUnexpectedConfigError = fmt.Errorf("could not initialize policy-selector for given config")
)
// policyKey serves as key for the routing policy in the context
var policyKey struct{}
// ContextGetPolicy returns the policy if set in the given context.
func ContextGetPolicy(ctx context.Context) string {
u, _ := ctx.Value(policyKey).(string)
return u
}
// ContextSetPolicy stores the selected policy in the context.
func ContextSetPolicy(ctx context.Context, p string) context.Context {
return context.WithValue(ctx, policyKey, p)
}
// Selector is a function which selects a proxy-policy based on the request.
//
// A policy is a random name which identifies a set of proxy-routes:
+1 -6
View File
@@ -146,12 +146,7 @@ func (p *MultiHostReverseProxy) directorSelectionDirector(r *http.Request) {
Str("routeType", string(rt)).
Msg("director found")
p.logger.Info().
Str("method", r.Method).
Str("path", r.Method).
Str("from", r.RemoteAddr).
Msg("access-log")
r = r.WithContext(policy.ContextSetPolicy(r.Context(), pol))
p.Directors[pol][rt][endpoint](r)
return
}