Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
ecbe1b30
Commit
ecbe1b30
authored
Sep 08, 2020
by
alinamihaila
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use CSRF token verification
parent
aa7f162b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
25 deletions
+29
-25
doc/development/telemetry/usage_ping.md
doc/development/telemetry/usage_ping.md
+1
-7
lib/api/usage_data.rb
lib/api/usage_data.rb
+3
-1
spec/requests/api/usage_data_spec.rb
spec/requests/api/usage_data_spec.rb
+25
-17
No files found.
doc/development/telemetry/usage_ping.md
View file @
ecbe1b30
...
...
@@ -330,16 +330,10 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
Return 200 if tracking failed for any reason.
-
`40
1 Unauthorized`
if not authoriz
ed
-
`40
3 Forbidden`
if invalid CSRF token is provid
ed
-
`400 Bad request`
if name parameter is missing
-
`200`
if event was tracked or any errors
Example usage:
```
shell
curl
--header
"Authorization: Bearer OAUTH-TOKEN"
"https://gitlab.example.com/api/v4/usage_data/increment_unique_users"
--data
"name=event_name&values[]=value1&values[]=value2"
```
1.
Track event using base module
`Gitlab::UsageDataCounters::HLLRedisCounter.track_event(entity_id, event_name)`
.
Arguments:
...
...
lib/api/usage_data.rb
View file @
ecbe1b30
...
...
@@ -2,7 +2,9 @@
module
API
class
UsageData
<
Grape
::
API
::
Instance
before
{
authenticate!
}
before
do
forbidden!
(
'Invalid CSRF token is provided'
)
unless
verified_request?
end
namespace
'usage_data'
do
desc
'Track usage data events'
do
...
...
spec/requests/api/usage_data_spec.rb
View file @
ecbe1b30
...
...
@@ -10,35 +10,43 @@ RSpec.describe API::UsageData do
let
(
:known_event
)
{
'g_compliance_dashboard'
}
let
(
:unknown_event
)
{
'unknown'
}
context
'when unauthenticated'
do
it
'retruns 401 response'
do
context
'without CSRF token'
do
it
'returns 401 response'
do
allow
(
Gitlab
::
RequestForgeryProtection
).
to
receive
(
:verified?
).
and_return
(
false
)
post
api
(
endpoint
),
params:
{
values:
[
user
.
id
]
}
expect
(
response
).
to
have_gitlab_http_status
(
:
unauthorized
)
expect
(
response
).
to
have_gitlab_http_status
(
:
forbidden
)
end
end
context
'when name is missing from params'
do
it
'returns bad request'
do
post
api
(
endpoint
,
user
),
params:
{
values:
[
user
.
id
]
}
context
'without CSRF token'
do
before
do
allow
(
Gitlab
::
RequestForgeryProtection
).
to
receive
(
:verified?
).
and_return
(
true
)
end
context
'when name is missing from params'
do
it
'returns bad request'
do
post
api
(
endpoint
),
params:
{
values:
[
user
.
id
]
}
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
end
end
end
context
'with correct params'
do
it
'returns status ok'
do
post
api
(
endpoint
,
user
),
params:
{
name:
known_event
,
values:
[
user
.
id
]
}
context
'with correct params'
do
it
'returns status ok'
do
post
api
(
endpoint
),
params:
{
name:
known_event
,
values:
[
user
.
id
]
}
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
end
end
end
context
'with unknown event'
do
it
'returns status ok'
do
post
api
(
endpoint
,
user
),
params:
{
name:
unknown_event
,
values:
[
user
.
id
]
}
context
'with unknown event'
do
it
'returns status ok'
do
post
api
(
endpoint
),
params:
{
name:
unknown_event
,
values:
[
user
.
id
]
}
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
end
end
end
end
...
...
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