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
b39935f7
Commit
b39935f7
authored
Apr 14, 2016
by
Gabriel Mazetto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Authenticate Geo requests using X-Gitlab-Token
parent
e6757060
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
49 additions
and
10 deletions
+49
-10
app/models/geo_node.rb
app/models/geo_node.rb
+2
-1
db/migrate/20160414032323_add_token_to_geo_node.rb
db/migrate/20160414032323_add_token_to_geo_node.rb
+11
-0
db/schema.rb
db/schema.rb
+2
-1
lib/api/geo.rb
lib/api/geo.rb
+3
-2
lib/api/helpers.rb
lib/api/helpers.rb
+11
-0
spec/requests/api/geo_spec.rb
spec/requests/api/geo_spec.rb
+20
-6
No files found.
app/models/geo_node.rb
View file @
b39935f7
...
...
@@ -18,7 +18,8 @@ class GeoNode < ActiveRecord::Base
host:
lambda
{
Gitlab
.
config
.
gitlab
.
host
},
port:
80
,
relative_url_root:
''
,
primary:
false
primary:
false
,
token:
lambda
{
SecureRandom
.
hex
(
20
)
}
accepts_nested_attributes_for
:geo_node_key
...
...
db/migrate/20160414032323_add_token_to_geo_node.rb
0 → 100644
View file @
b39935f7
class
AddTokenToGeoNode
<
ActiveRecord
::
Migration
def
change
add_column
:geo_nodes
,
:token
,
:string
# Add token to existing nodes
GeoNode
.
where
(
token:
nil
).
each
do
|
node
|
node
.
token
=
SecureRandom
.
hex
(
20
)
node
.
save!
end
end
end
db/schema.rb
View file @
b39935f7
...
...
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2016041
3115152
)
do
ActiveRecord
::
Schema
.
define
(
version:
2016041
4032323
)
do
# These are extensions that must be enabled in order to support this database
enable_extension
"plpgsql"
...
...
@@ -420,6 +420,7 @@ ActiveRecord::Schema.define(version: 20160413115152) do
t
.
boolean
"primary"
t
.
integer
"geo_node_key_id"
t
.
integer
"oauth_application_id"
t
.
string
"token"
end
add_index
"geo_nodes"
,
[
"geo_node_key_id"
],
name:
"index_geo_nodes_on_geo_node_key_id"
,
using: :btree
...
...
lib/api/geo.rb
View file @
b39935f7
module
API
class
Geo
<
Grape
::
API
before
{
authenticated_as_admin!
}
resource
:geo
do
# Enqueue a batch of IDs of modified projects to have their
# repositories updated
...
...
@@ -9,6 +7,7 @@ module API
# Example request:
# POST /geo/refresh_projects
post
'refresh_projects'
do
authenticated_as_admin!
required_attributes!
[
:projects
]
::
Geo
::
ScheduleRepoUpdateService
.
new
(
params
[
:projects
]).
execute
end
...
...
@@ -19,6 +18,7 @@ module API
# Example request:
# POST /geo/refresh_wikis
post
'refresh_wikis'
do
authenticated_as_admin!
required_attributes!
[
:projects
]
::
Geo
::
ScheduleWikiRepoUpdateService
.
new
(
params
[
:projects
]).
execute
end
...
...
@@ -28,6 +28,7 @@ module API
# Example request:
# POST /geo/receive_events
post
'receive_events'
do
authenticate_by_gitlab_geo_token!
required_attributes!
%w(event_name)
case
params
[
'event_name'
]
...
...
lib/api/helpers.rb
View file @
b39935f7
...
...
@@ -113,6 +113,13 @@ module API
end
end
def
authenticate_by_gitlab_geo_token!
token
=
headers
[
'X-Gitlab-Token'
].
try
(
:chomp
)
unless
token
&&
Devise
.
secure_compare
(
geo_token
,
token
)
unauthorized!
end
end
def
authenticated_as_admin!
forbidden!
unless
current_user
.
is_admin?
end
...
...
@@ -374,6 +381,10 @@ module API
File
.
read
(
Gitlab
.
config
.
gitlab_shell
.
secret_file
).
chomp
end
def
geo_token
Gitlab
::
Geo
.
current_node
.
token
end
def
handle_member_errors
(
errors
)
error!
(
errors
[
:access_level
],
422
)
if
errors
[
:access_level
].
any?
not_found!
(
errors
)
...
...
spec/requests/api/geo_spec.rb
View file @
b39935f7
...
...
@@ -4,6 +4,7 @@ describe API::API, api: true do
include
ApiHelpers
let
(
:admin
)
{
create
(
:admin
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:geo_node
)
{
build
(
:geo_node
)
}
describe
'POST /geo/refresh_projects'
do
before
(
:each
)
{
allow_any_instance_of
(
::
Geo
::
ScheduleRepoUpdateService
).
to
receive
(
:execute
)
}
...
...
@@ -20,7 +21,15 @@ describe API::API, api: true do
end
describe
'POST /geo/receive_events'
do
before
(
:each
)
{
allow_any_instance_of
(
::
Geo
::
ScheduleKeyChangeService
).
to
receive
(
:execute
)
}
before
(
:each
)
do
allow_any_instance_of
(
::
Geo
::
ScheduleKeyChangeService
).
to
receive
(
:execute
)
allow
(
Gitlab
::
Geo
).
to
receive
(
:current_node
)
{
geo_node
}
end
let
(
:geo_token_header
)
do
{
'X-Gitlab-Token'
=>
geo_node
.
token
}
end
let
(
:key_create_payload
)
do
{
'event_name'
=>
'key_create'
,
...
...
@@ -44,18 +53,23 @@ describe API::API, api: true do
end
it
'enqueues on disk key creation if admin and correct params'
do
post
api
(
'/geo/receive_events'
,
admin
),
key_create_payload
post
api
(
'/geo/receive_events'
),
key_create_payload
,
geo_token_header
expect
(
response
.
status
).
to
eq
201
end
it
'enqueues on disk key removal if admin and correct params'
do
post
api
(
'/geo/receive_events'
,
admin
),
key_destroy_payload
post
api
(
'/geo/receive_events'
),
key_destroy_payload
,
geo_token_header
expect
(
response
.
status
).
to
eq
201
end
it
'denies access if not admin'
do
post
api
(
'/geo/receive_events'
,
user
)
expect
(
response
.
status
).
to
eq
403
it
'denies access if token is not present'
do
post
api
(
'/geo/receive_events'
)
expect
(
response
.
status
).
to
eq
401
end
it
'denies access if token is invalid'
do
post
api
(
'/geo/receive_events'
),
nil
,
{
'X-Gitlab-Token'
=>
'nothing'
}
expect
(
response
.
status
).
to
eq
401
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