BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
eaf9088b
Commit
eaf9088b
authored
Jan 06, 2018
by
Stan Hu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Save user ID and username in Grape API log (api_json.log)
This will enable admins to identify who actually made the API request. Relates to #36960
parent
7b2f9af4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
38 additions
and
1 deletion
+38
-1
sh-store-user-in-api-logs.yml
changelogs/unreleased/sh-store-user-in-api-logs.yml
+5
-0
api.rb
lib/api/api.rb
+2
-1
helpers.rb
lib/api/helpers.rb
+7
-0
user_logger.rb
lib/gitlab/grape_logging/loggers/user_logger.rb
+18
-0
helpers_spec.rb
spec/requests/api/helpers_spec.rb
+6
-0
No files found.
changelogs/unreleased/sh-store-user-in-api-logs.yml
0 → 100644
View file @
eaf9088b
---
title
:
Save user ID and username in Grape API log (api_json.log)
merge_request
:
author
:
type
:
changed
lib/api/api.rb
View file @
eaf9088b
...
...
@@ -13,7 +13,8 @@ module API
formatter:
Gitlab
::
GrapeLogging
::
Formatters
::
LogrageWithTimestamp
.
new
,
include:
[
GrapeLogging
::
Loggers
::
FilterParameters
.
new
,
GrapeLogging
::
Loggers
::
ClientEnv
.
new
GrapeLogging
::
Loggers
::
ClientEnv
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
UserLogger
.
new
]
allow_access_with_scope
:api
...
...
lib/api/helpers.rb
View file @
eaf9088b
...
...
@@ -5,6 +5,7 @@ module API
SUDO_HEADER
=
"HTTP_SUDO"
.
freeze
SUDO_PARAM
=
:sudo
API_USER_ENV
=
'gitlab.api.user'
.
freeze
def
declared_params
(
options
=
{})
options
=
{
include_parent_namespaces:
false
}.
merge
(
options
)
...
...
@@ -48,10 +49,16 @@ module API
validate_access_token!
(
scopes:
scopes_registered_for_endpoint
)
unless
sudo?
save_current_user_in_env
(
@current_user
)
if
@current_user
@current_user
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
def
save_current_user_in_env
(
user
)
env
[
API_USER_ENV
]
=
{
user_id:
user
.
id
,
username:
user
.
username
}
end
def
sudo?
initial_current_user
!=
current_user
end
...
...
lib/gitlab/grape_logging/loggers/user_logger.rb
0 → 100644
View file @
eaf9088b
# This grape_logging module (https://github.com/aserafin/grape_logging) makes it
# possible to log the user who performed the Grape API action by retrieving
# the user context from the request environment.
module
Gitlab
module
GrapeLogging
module
Loggers
class
UserLogger
<
::
GrapeLogging
::
Loggers
::
Base
def
parameters
(
request
,
_
)
params
=
request
.
env
[
::
API
::
Helpers
::
API_USER_ENV
]
return
{}
unless
params
params
.
slice
(
:user_id
,
:username
)
end
end
end
end
end
spec/requests/api/helpers_spec.rb
View file @
eaf9088b
...
...
@@ -68,6 +68,12 @@ describe API::Helpers do
end
it
{
is_expected
.
to
eq
(
user
)
}
it
'sets the environment with data of the current user'
do
subject
expect
(
env
[
API
::
Helpers
::
API_USER_ENV
]).
to
eq
({
user_id:
subject
.
id
,
username:
subject
.
username
})
end
end
context
"HEAD request"
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment