fleet/server/contexts/logging/logging.go
Lucas Manuel Rodriguez 3757aace08
Add UUID to Fleet errors and clean up error msgs (#10411)
#8129 

Apart from fixing the issue in #8129, this change also introduces UUIDs
to Fleet errors. To be able to match a returned error from the API to a
error in the Fleet logs. See
https://fleetdm.slack.com/archives/C019WG4GH0A/p1677780622769939 for
more context.

Samples with the changes in this PR:
```
curl -k -H "Authorization: Bearer $TEST_TOKEN" -H 'Content-Type:application/json' "https://localhost:8080/api/v1/fleet/sso" -d ''
{
  "message": "Bad request",
  "errors": [
    {
      "name": "base",
      "reason": "Expected JSON Body"
    }
  ],
  "uuid": "a01f6e10-354c-4ff0-b96e-1f64adb500b0"
}
```
```
curl -k -H "Authorization: Bearer $TEST_TOKEN" -H 'Content-Type:application/json' "https://localhost:8080/api/v1/fleet/sso" -d 'asd'
{
  "message": "Bad request",
  "errors": [
    {
      "name": "base",
      "reason": "json decoder error"
    }
  ],
  "uuid": "5f716a64-7550-464b-a1dd-e6a505a9f89d"
}
```
```
curl -k -X GET -H "Authorization: Bearer badtoken" "https://localhost:8080/api/latest/fleet/teams"
{
  "message": "Authentication required",
  "errors": [
    {
      "name": "base",
      "reason": "Authentication required"
    }
  ],
  "uuid": "efe45bc0-f956-4bf9-ba4f-aa9020a9aaaf"
}
```
```
curl -k -X PATCH -H "Authorization: Bearer $TEST_TOKEN" "https://localhost:8080/api/latest/fleet/users/14" -d '{"name": "Manuel2", "password": "what", "new_password": "p4ssw0rd.12345"}'
{
  "message": "Authorization header required",
  "errors": [
    {
      "name": "base",
      "reason": "Authorization header required"
    }
  ],
  "uuid": "57f78cd0-4559-464f-9df7-36c9ef7c89b3"
}
```
```
curl -k -X PATCH -H "Authorization: Bearer $TEST_TOKEN" "https://localhost:8080/api/latest/fleet/users/14" -d '{"name": "Manuel2", "password": "what", "new_password": "p4ssw0rd.12345"}'
{
  "message": "Permission Denied",
  "uuid": "7f0220ad-6de7-4faf-8b6c-8d7ff9d2ca06"
}
```

- [X] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [X] Documented any API changes (docs/Using-Fleet/REST-API.md or
docs/Contributing/API-for-contributors.md)
- ~[ ] Documented any permissions changes~
- ~[ ] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)~
- ~[ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for
new osquery data ingestion features.~
- [X] Added/updated tests
- [X] Manual QA for all new/changed functionality
  - For Orbit and Fleet Desktop changes:
- [X] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- ~[ ] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).~
2023-03-13 13:44:06 -03:00

193 lines
4.6 KiB
Go

package logging
import (
"context"
"errors"
"strings"
"sync"
"time"
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
"github.com/fleetdm/fleet/v4/server/fleet"
kitlog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
kithttp "github.com/go-kit/kit/transport/http"
)
type key int
const loggingKey key = 0
// NewContext creates a new context.Context with a LoggingContext.
func NewContext(ctx context.Context, logging *LoggingContext) context.Context {
return context.WithValue(ctx, loggingKey, logging)
}
// FromContext returns a pointer to the LoggingContext.
func FromContext(ctx context.Context) (*LoggingContext, bool) {
v, ok := ctx.Value(loggingKey).(*LoggingContext)
return v, ok
}
// WithStartTime returns a context with logging.StartTime marked as the current time
func WithStartTime(ctx context.Context) context.Context {
if logCtx, ok := FromContext(ctx); ok {
logCtx.SetStartTime()
}
return ctx
}
// WithErr returns a context with logging.Err set as the error provided
func WithErr(ctx context.Context, err ...error) context.Context {
if logCtx, ok := FromContext(ctx); ok {
logCtx.SetErrs(err...)
}
return ctx
}
// WithNoUser returns a context with logging.SkipUser set to true so user won't be logged
func WithNoUser(ctx context.Context) context.Context {
if logCtx, ok := FromContext(ctx); ok {
logCtx.SetSkipUser()
}
return ctx
}
// WithExtras returns a context with logging.Extras set as the values provided
func WithExtras(ctx context.Context, extras ...interface{}) context.Context {
if logCtx, ok := FromContext(ctx); ok {
logCtx.SetExtras(extras...)
}
return ctx
}
func WithLevel(ctx context.Context, level func(kitlog.Logger) kitlog.Logger) context.Context {
if logCtx, ok := FromContext(ctx); ok {
logCtx.SetForceLevel(level)
}
return ctx
}
// LoggingContext contains the context information for logging the current request
type LoggingContext struct {
l sync.Mutex
StartTime time.Time
Errs []error
Extras []interface{}
SkipUser bool
ForceLevel func(kitlog.Logger) kitlog.Logger
}
func (l *LoggingContext) SetForceLevel(level func(kitlog.Logger) kitlog.Logger) {
l.l.Lock()
defer l.l.Unlock()
l.ForceLevel = level
}
func (l *LoggingContext) SetExtras(extras ...interface{}) {
l.l.Lock()
defer l.l.Unlock()
l.Extras = append(l.Extras, extras...)
}
func (l *LoggingContext) SetSkipUser() {
l.l.Lock()
defer l.l.Unlock()
l.SkipUser = true
}
func (l *LoggingContext) SetStartTime() {
l.l.Lock()
defer l.l.Unlock()
l.StartTime = time.Now()
}
func (l *LoggingContext) SetErrs(err ...error) {
l.l.Lock()
defer l.l.Unlock()
l.Errs = append(l.Errs, err...)
}
// Log logs the data within the context
func (l *LoggingContext) Log(ctx context.Context, logger kitlog.Logger) {
l.l.Lock()
defer l.l.Unlock()
switch {
case len(l.Errs) > 0:
logger = level.Error(logger)
case l.ForceLevel != nil:
logger = l.ForceLevel(logger)
default:
logger = level.Debug(logger)
}
var keyvals []interface{}
if !l.SkipUser {
loggedInUser := "unauthenticated"
vc, ok := viewer.FromContext(ctx)
if ok {
loggedInUser = vc.Email()
}
keyvals = append(keyvals, "user", loggedInUser)
}
requestMethod, ok := ctx.Value(kithttp.ContextKeyRequestMethod).(string)
if !ok {
requestMethod = ""
}
requestURI, ok := ctx.Value(kithttp.ContextKeyRequestURI).(string)
if !ok {
requestURI = ""
}
keyvals = append(keyvals, "method", requestMethod, "uri", requestURI, "took", time.Since(l.StartTime))
if len(l.Extras) > 0 {
keyvals = append(keyvals, l.Extras...)
}
if len(l.Errs) > 0 {
// Going for string concatenation here instead of json.Marshal mostly to not have to deal with error handling
// within this method. kitlog doesn't support slices of strings
var (
errs string
internalErrs string
uuids []string
)
separator := " || "
for _, err := range l.Errs {
var ewi fleet.ErrWithInternal
if errors.As(err, &ewi) {
if internalErrs == "" {
internalErrs = ewi.Internal()
} else {
internalErrs += separator + ewi.Internal()
}
} else {
if errs == "" {
errs = err.Error()
} else {
errs += separator + err.Error()
}
}
var ewuuid fleet.ErrorUUIDer
if errors.As(err, &ewuuid) {
if uuid := ewuuid.UUID(); uuid != "" {
uuids = append(uuids, uuid)
}
}
}
if len(errs) > 0 {
keyvals = append(keyvals, "err", errs)
}
if len(internalErrs) > 0 {
keyvals = append(keyvals, "internal", internalErrs)
}
if len(uuids) > 0 {
keyvals = append(keyvals, "uuid", strings.Join(uuids, ","))
}
}
_ = logger.Log(keyvals...)
}