Skip to content
This repository was archived by the owner on May 10, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/splunk-sdk-ruby/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module Splunk
# * +:ssl_client_cert+ A +OpenSSL::X509::Certificate+ object to use as a client certificate.
# * +:ssl_client_key+ A +OpenSSL::PKey::RSA+ or +OpenSSL::PKey::DSA+ object to use as a client key.
# * +:token+ - a preauthenticated Splunk token (default: +nil+)
# * +:basic+ - indicates if HTTP Basic Auth is going to be used (default: +false+)
#
# If you specify a token, you need not specify a username or password, nor
# do you need to call the +login+ method.
Expand All @@ -87,6 +88,7 @@ def initialize(args)
@namespace = args.fetch(:namespace,
Splunk::namespace(:sharing => "default"))
@proxy = args.fetch(:proxy, nil)
@basic = args.fetch(:basic, False)
@path_prefix = args.fetch(:path_prefix, DEFAULT_PATH_PREFIX)
@ssl_client_cert = args.fetch(:ssl_client_cert, nil)
@ssl_client_key = args.fetch(:ssl_client_key, nil)
Expand Down Expand Up @@ -225,7 +227,9 @@ def login()
if @token # If we're already logged in, this method is a nop.
return
end

if @basic # We're using basic authentication, thus making this a nop
return
end
response = request(:namespace => Splunk::namespace(:sharing => "default"),
:method => :POST,
:resource => ["auth", "login"],
Expand Down Expand Up @@ -418,6 +422,10 @@ def request_by_url(args)
# Headers
request["User-Agent"] = "splunk-sdk-ruby/#{VERSION}"
request["Authorization"] = "Splunk #{@token}" if @token
# basic authentication supercedes Splunk authentication
if @basic then
request["Authorization"] = "Basic " + ["#{@username}:#{@password}"].pack('m').strip
end
headers.each_entry do |key, value|
request[key] = value
end
Expand Down
11 changes: 11 additions & 0 deletions test/test_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ def test_authenticate_with_token
assert_logged_in(new_service)
end

def test_authenticate_with_basic
new_arguments = @splunkrc.clone
new_arguments.delete(:username)
new_arguments.delete(:password)
new_arguments[:basic] = True

new_service = Context.new(new_arguments)
assert_logged_in(new_service)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add something like assert_true(service.apps.length() > 0) to verify that the service can access a REST endpoint?

assert_true(new_service.apps.length() > 0)
end

def test_failed_login()
args = @splunkrc.clone()
args[:username] = args[:username] + "-boris"
Expand Down