Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
#208 fix deploying REST stuff with digest authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcassel committed Apr 17, 2014
1 parent 3faef1a commit 361dd65
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
9 changes: 8 additions & 1 deletion deploy/lib/MLClient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def build_request_params(url, verb)
}
end

def go(url, verb, headers = {}, params = nil, body = nil)
# Parameters:
# - auth_method: this can be used to specify whether to use digest or basic authentication to respond to a 401
# The default behavior is to digest.
def go(url, verb, headers = {}, params = nil, body = nil, auth_method = nil)
password_prompt
request_params = build_request_params(url, verb)
# configure headers
Expand All @@ -90,6 +93,10 @@ def go(url, verb, headers = {}, params = nil, body = nil)
request_params[:request].body = body
end

if (auth_method)
request_params[:auth_method] = auth_method
end

response = get_http.request request_params
response.value
response
Expand Down
15 changes: 13 additions & 2 deletions deploy/lib/RoxyHttp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require "net/https"
require "uri"
require 'digest/md5'
require 'base64'
require 'logger'

module Net
Expand Down Expand Up @@ -70,6 +71,12 @@ def digest_auth(user, password, response)

@header['Authorization'] = header
end

def basic_auth(user, password)
encoded = Base64.encode64("#{user}:#{password}").chomp

@header['Authorization'] = ["Basic #{encoded}"]
end
end
end

Expand Down Expand Up @@ -366,9 +373,13 @@ def request(request_params, &block)
setup_streaming(request)

response = @http.request(request, &block)

if (response.code.to_i == 401)
# TODO: looks like we get this every time. Why not just use digest the first time?
request.digest_auth(@user_name, @password, response)
if request_params[:auth_method] == "basic"
request.basic_auth(@user_name, @password)
else
request.digest_auth(@user_name, @password, response)
end
response = @http.request(request, &block)
if (response.code.to_i == 302)
@logger.debug("request redirected: #{response['location']}")
Expand Down
7 changes: 4 additions & 3 deletions deploy/lib/ml_rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(options)
if !@port or @port == ""
@port = options[:app_port]
end
@auth_method = options[:auth_method]
@http = Roxy::Http.new({
:logger => @logger
})
Expand Down Expand Up @@ -47,7 +48,7 @@ def install_properties(path)
url = "http://#{@hostname}:#{@port}/v1/config/properties"

@logger.debug "url: #{url}"
r = go url, "put", headers, nil, contents
r = go url, "put", headers, nil, contents, @auth_method
if (r.code.to_i < 200 && r.code.to_i > 206)
@logger.error("code: #{r.code.to_i} body:#{r.body}")
end
Expand Down Expand Up @@ -105,7 +106,7 @@ def install_extensions(path)
@logger.debug "loading: #{d}"

@logger.debug "url: #{url}"
r = go url, "put", headers, nil, contents
r = go url, "put", headers, nil, contents, @auth_method
if (r.code.to_i < 200 && r.code.to_i > 206)
@logger.error("code: #{r.code.to_i} body:#{r.body}")
end
Expand Down Expand Up @@ -195,7 +196,7 @@ def install_transforms(path)
@logger.debug "loading: #{d}"

@logger.debug "url: #{url}"
r = go url, "put", headers, nil, contents
r = go url, "put", headers, nil, contents, @auth_method
if (r.code.to_i < 200 && r.code.to_i > 206)
@logger.error("code: #{r.code.to_i} body:#{r.body}")
end
Expand Down
3 changes: 2 additions & 1 deletion deploy/lib/server_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,8 @@ def mlRest
:server => @hostname,
:app_port => @properties["ml.app-port"],
:rest_port => @properties["ml.rest-port"],
:logger => @logger
:logger => @logger,
:auth_method => @properties["ml.authentication-method"]
})
else
@mlRest
Expand Down

0 comments on commit 361dd65

Please sign in to comment.