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

[hooks] Log the user when fetching Lua script from lakeFS #7486

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions pkg/actions/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ func (l *loggingBuffer) WriteString(s string) (n int, err error) {
return l.buf.WriteString(s)
}

// allowedFields are the logging fields that are safe to keep on the context
// passed to Lua execution. These logging fields will enter the Lua script
// and a bug might allow the script to access them.
var allowedFields = []string{logging.RepositoryFieldKey, logging.UserFieldKey}

// getAllowedFields returns only logging fields that are in allowedFields.
func getAllowedFields(fields logging.Fields) logging.Fields {
// This implementation is efficient when allowedFields is small.
ret := make(logging.Fields, len(allowedFields))
for _, f := range allowedFields {
if v, ok := fields[f]; ok {
ret[f] = v
}
}
return ret
}

func (h *LuaHook) Run(ctx context.Context, record graveler.HookRecord, buf *bytes.Buffer) error {
user, err := auth.GetUser(ctx)
if err != nil {
Expand Down Expand Up @@ -115,6 +132,7 @@ func (h *LuaHook) Run(ctx context.Context, record graveler.HookRecord, buf *byte
return err
}
req = req.WithContext(auth.WithUser(req.Context(), user))
req = req.WithContext(logging.AddFields(req.Context(), getAllowedFields(logging.GetFieldsFromContext(ctx))))
q := req.URL.Query()
q.Add("path", h.ScriptPath)
req.URL.RawQuery = q.Encode()
Expand Down
11 changes: 8 additions & 3 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,17 @@ func ContextUnavailable() Logger {
}
}

func addFromContext(log Logger, ctx context.Context) Logger {
// GetFieldsFromContext returns the logging fields on ctx or nil.
func GetFieldsFromContext(ctx context.Context) Fields {
fields := ctx.Value(LogFieldsContextKey)
if fields == nil {
return log
return nil
}
loggerFields := fields.(Fields)
return fields.(Fields)
}

func addFromContext(log Logger, ctx context.Context) Logger {
loggerFields := GetFieldsFromContext(ctx)
return log.WithFields(loggerFields)
}

Expand Down
Loading