Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-shell
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
nexedi
gitlab-shell
Commits
3042a721
Commit
3042a721
authored
Jul 11, 2018
by
Ash McKenzie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move HTTP logic out into HTTPHelper
parent
cc278dff
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
111 additions
and
104 deletions
+111
-104
lib/gitlab_net.rb
lib/gitlab_net.rb
+3
-104
lib/http_helper.rb
lib/http_helper.rb
+108
-0
No files found.
lib/gitlab_net.rb
View file @
3042a721
...
...
@@ -7,13 +7,15 @@ require_relative 'gitlab_logger'
require_relative
'gitlab_access'
require_relative
'gitlab_lfs_authentication'
require_relative
'httpunix'
require_relative
'http_helper'
class
GitlabNet
# rubocop:disable Metrics/ClassLength
include
HTTPHelper
class
ApiUnreachableError
<
StandardError
;
end
class
NotFound
<
StandardError
;
end
CHECK_TIMEOUT
=
5
READ_TIMEOUT
=
300
def
check_access
(
cmd
,
gl_repository
,
repo
,
actor
,
changes
,
protocol
,
env:
{})
changes
=
changes
.
join
(
"
\n
"
)
unless
changes
.
is_a?
(
String
)
...
...
@@ -143,107 +145,4 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
def
sanitize_path
(
repo
)
repo
.
delete
(
"'"
)
end
def
config
@config
||=
GitlabConfig
.
new
end
def
host
"
#{
config
.
gitlab_url
}
/api/v4/internal"
end
def
http_client_for
(
uri
,
options
=
{})
http
=
if
uri
.
is_a?
(
URI
::
HTTPUNIX
)
Net
::
HTTPUNIX
.
new
(
uri
.
hostname
)
else
Net
::
HTTP
.
new
(
uri
.
host
,
uri
.
port
)
end
http
.
read_timeout
=
options
[
:read_timeout
]
||
read_timeout
if
uri
.
is_a?
(
URI
::
HTTPS
)
http
.
use_ssl
=
true
http
.
cert_store
=
cert_store
http
.
verify_mode
=
OpenSSL
::
SSL
::
VERIFY_NONE
if
config
.
http_settings
[
'self_signed_cert'
]
end
http
end
def
http_request_for
(
method
,
uri
,
params
=
{})
request_klass
=
method
==
:get
?
Net
::
HTTP
::
Get
:
Net
::
HTTP
::
Post
request
=
request_klass
.
new
(
uri
.
request_uri
)
user
=
config
.
http_settings
[
'user'
]
password
=
config
.
http_settings
[
'password'
]
request
.
basic_auth
(
user
,
password
)
if
user
&&
password
request
.
set_form_data
(
params
.
merge
(
secret_token:
secret_token
))
if
uri
.
is_a?
(
URI
::
HTTPUNIX
)
# The HTTPUNIX HTTP client does not set a correct Host header. This can
# lead to 400 Bad Request responses.
request
[
'Host'
]
=
'localhost'
end
request
end
def
request
(
method
,
url
,
params
=
{},
options
=
{})
$logger
.
debug
(
'Performing request'
,
method:
method
.
to_s
.
upcase
,
url:
url
)
uri
=
URI
.
parse
(
url
)
http
=
http_client_for
(
uri
,
options
)
request
=
http_request_for
(
method
,
uri
,
params
)
begin
start_time
=
Time
.
new
response
=
http
.
start
{
http
.
request
(
request
)
}
rescue
=>
e
$logger
.
warn
(
'Failed to connect to internal API'
,
method:
method
.
to_s
.
upcase
,
url:
url
,
error:
e
)
raise
ApiUnreachableError
ensure
$logger
.
info
(
'finished HTTP request'
,
method:
method
.
to_s
.
upcase
,
url:
url
,
duration:
Time
.
new
-
start_time
)
end
if
response
.
code
==
"200"
$logger
.
debug
(
'Received response'
,
code:
response
.
code
,
body:
response
.
body
)
else
$logger
.
error
(
'API call failed'
,
method:
method
.
to_s
.
upcase
,
url:
url
,
code:
response
.
code
,
body:
response
.
body
)
end
response
end
def
get
(
url
,
options
=
{})
request
(
:get
,
url
,
{},
options
)
end
def
post
(
url
,
params
)
request
(
:post
,
url
,
params
)
end
def
cert_store
@cert_store
||=
begin
store
=
OpenSSL
::
X509
::
Store
.
new
store
.
set_default_paths
ca_file
=
config
.
http_settings
[
'ca_file'
]
store
.
add_file
(
ca_file
)
if
ca_file
ca_path
=
config
.
http_settings
[
'ca_path'
]
store
.
add_path
(
ca_path
)
if
ca_path
store
end
end
def
secret_token
@secret_token
||=
File
.
read
config
.
secret_file
end
def
read_timeout
config
.
http_settings
[
'read_timeout'
]
||
READ_TIMEOUT
end
end
lib/http_helper.rb
0 → 100644
View file @
3042a721
module
HTTPHelper
READ_TIMEOUT
=
300
protected
def
config
@config
||=
GitlabConfig
.
new
end
def
host
"
#{
config
.
gitlab_url
}
/api/v4/internal"
end
def
http_client_for
(
uri
,
options
=
{})
http
=
if
uri
.
is_a?
(
URI
::
HTTPUNIX
)
Net
::
HTTPUNIX
.
new
(
uri
.
hostname
)
else
Net
::
HTTP
.
new
(
uri
.
host
,
uri
.
port
)
end
http
.
read_timeout
=
options
[
:read_timeout
]
||
read_timeout
if
uri
.
is_a?
(
URI
::
HTTPS
)
http
.
use_ssl
=
true
http
.
cert_store
=
cert_store
http
.
verify_mode
=
OpenSSL
::
SSL
::
VERIFY_NONE
if
config
.
http_settings
[
'self_signed_cert'
]
end
http
end
def
http_request_for
(
method
,
uri
,
params
=
{})
request_klass
=
method
==
:get
?
Net
::
HTTP
::
Get
:
Net
::
HTTP
::
Post
request
=
request_klass
.
new
(
uri
.
request_uri
)
user
=
config
.
http_settings
[
'user'
]
password
=
config
.
http_settings
[
'password'
]
request
.
basic_auth
(
user
,
password
)
if
user
&&
password
request
.
set_form_data
(
params
.
merge
(
secret_token:
secret_token
))
if
uri
.
is_a?
(
URI
::
HTTPUNIX
)
# The HTTPUNIX HTTP client does not set a correct Host header. This can
# lead to 400 Bad Request responses.
request
[
'Host'
]
=
'localhost'
end
request
end
def
request
(
method
,
url
,
params
=
{},
options
=
{})
$logger
.
debug
(
'Performing request'
,
method:
method
.
to_s
.
upcase
,
url:
url
)
uri
=
URI
.
parse
(
url
)
http
=
http_client_for
(
uri
,
options
)
request
=
http_request_for
(
method
,
uri
,
params
)
begin
start_time
=
Time
.
new
response
=
http
.
start
{
http
.
request
(
request
)
}
rescue
=>
e
$logger
.
warn
(
'Failed to connect'
,
method:
method
.
to_s
.
upcase
,
url:
url
,
error:
e
)
raise
GitlabNet
::
ApiUnreachableError
ensure
$logger
.
info
(
'finished HTTP request'
,
method:
method
.
to_s
.
upcase
,
url:
url
,
duration:
Time
.
new
-
start_time
)
end
if
response
.
code
==
"200"
$logger
.
debug
(
'Received response'
,
code:
response
.
code
,
body:
response
.
body
)
else
$logger
.
error
(
'Call failed'
,
method:
method
.
to_s
.
upcase
,
url:
url
,
code:
response
.
code
,
body:
response
.
body
)
end
response
end
def
get
(
url
,
options
=
{})
request
(
:get
,
url
,
{},
options
)
end
def
post
(
url
,
params
)
request
(
:post
,
url
,
params
)
end
def
cert_store
@cert_store
||=
begin
store
=
OpenSSL
::
X509
::
Store
.
new
store
.
set_default_paths
ca_file
=
config
.
http_settings
[
'ca_file'
]
store
.
add_file
(
ca_file
)
if
ca_file
ca_path
=
config
.
http_settings
[
'ca_path'
]
store
.
add_path
(
ca_path
)
if
ca_path
store
end
end
def
secret_token
@secret_token
||=
File
.
read
config
.
secret_file
end
def
read_timeout
config
.
http_settings
[
'read_timeout'
]
||
READ_TIMEOUT
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