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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
bb4fcb78
Commit
bb4fcb78
authored
Mar 03, 2018
by
Lin Jen-Shin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move constants and update for feedback
parent
6c5a7d53
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
15 additions
and
18 deletions
+15
-18
lib/gitlab/middleware/read_only.rb
lib/gitlab/middleware/read_only.rb
+0
-2
lib/gitlab/middleware/read_only/controller.rb
lib/gitlab/middleware/read_only/controller.rb
+6
-3
spec/lib/gitlab/middleware/read_only_spec.rb
spec/lib/gitlab/middleware/read_only_spec.rb
+7
-7
spec/lib/gitlab/middleware/release_env_spec.rb
spec/lib/gitlab/middleware/release_env_spec.rb
+2
-6
No files found.
lib/gitlab/middleware/read_only.rb
View file @
bb4fcb78
module
Gitlab
module
Middleware
class
ReadOnly
DISALLOWED_METHODS
=
%w(POST PATCH PUT DELETE)
.
freeze
APPLICATION_JSON
=
'application/json'
.
freeze
API_VERSIONS
=
(
3
..
4
)
def
self
.
internal_routes
...
...
lib/gitlab/middleware/read_only/controller.rb
View file @
bb4fcb78
...
...
@@ -2,6 +2,10 @@ module Gitlab
module
Middleware
class
ReadOnly
class
Controller
DISALLOWED_METHODS
=
%w(POST PATCH PUT DELETE)
.
freeze
APPLICATION_JSON
=
'application/json'
.
freeze
ERROR_MESSAGE
=
'You cannot perform write operations on a read-only instance'
.
freeze
def
initialize
(
app
,
env
)
@app
=
app
@env
=
env
...
...
@@ -10,12 +14,11 @@ module Gitlab
def
call
if
disallowed_request?
&&
Gitlab
::
Database
.
read_only?
Rails
.
logger
.
debug
(
'GitLab ReadOnly: preventing possible non read-only operation'
)
error_message
=
'You cannot do writing operations on a read-only GitLab instance'
if
json_request?
return
[
403
,
{
'Content-Type'
=>
'application/json'
},
[{
'message'
=>
error_message
}.
to_json
]]
return
[
403
,
{
'Content-Type'
=>
APPLICATION_JSON
},
[{
'message'
=>
ERROR_MESSAGE
}.
to_json
]]
else
rack_flash
.
alert
=
error_message
rack_flash
.
alert
=
ERROR_MESSAGE
rack_session
[
'flash'
]
=
rack_flash
.
to_session_value
return
[
301
,
{
'Location'
=>
last_visited_url
},
[]]
...
...
spec/lib/gitlab/middleware/read_only_spec.rb
View file @
bb4fcb78
...
...
@@ -14,14 +14,14 @@ describe Gitlab::Middleware::ReadOnly do
alert
=
middleware
.
env
[
'rack.session'
].
to_hash
.
dig
(
'flash'
,
'flashes'
,
'alert'
)
alert
&
.
include?
(
'You cannot
do writing
operations'
)
alert
&
.
include?
(
'You cannot
perform write
operations'
)
end
end
RSpec
::
Matchers
.
define
:disallow_request_in_json
do
match
do
|
response
|
json_response
=
JSON
.
parse
(
response
.
body
)
response
.
body
.
include?
(
'You cannot
do writing
operations'
)
&&
json_response
.
key?
(
'message'
)
response
.
body
.
include?
(
'You cannot
perform write
operations'
)
&&
json_response
.
key?
(
'message'
)
end
end
...
...
@@ -47,13 +47,13 @@ describe Gitlab::Middleware::ReadOnly do
end
end
let
(
:request
)
{
Rack
::
MockRequest
.
new
(
rack_stack
)
}
subject
do
app
=
described_class
.
new
(
fake_app
)
described_class
.
new
(
fake_app
).
tap
do
|
app
|
app
.
extend
(
observe_env
)
app
end
let
(
:request
)
{
Rack
::
MockRequest
.
new
(
rack_stack
)
}
end
context
'normal requests to a read-only Gitlab instance'
do
let
(
:fake_app
)
{
lambda
{
|
env
|
[
200
,
{
'Content-Type'
=>
'text/plain'
},
[
'OK'
]]
}
}
...
...
spec/lib/gitlab/middleware/release_env_spec.rb
View file @
bb4fcb78
require
'spec_helper'
describe
Gitlab
::
Middleware
::
ReleaseEnv
do
let
(
:inner_app
)
{
double
(
:app
)
}
let
(
:inner_app
)
{
double
(
:app
,
call:
'yay'
)
}
let
(
:app
)
{
described_class
.
new
(
inner_app
)
}
let
(
:env
)
{
{
'action_controller.instance'
=>
'something'
}
}
before
do
expect
(
inner_app
).
to
receive
(
:call
).
with
(
env
).
and_return
(
'yay'
)
end
describe
'#call'
do
it
'calls the app and
delete the controller
'
do
it
'calls the app and
clears the env
'
do
result
=
app
.
call
(
env
)
expect
(
result
).
to
eq
(
'yay'
)
...
...
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