File tree Expand file tree Collapse file tree 2 files changed +28
-8
lines changed Expand file tree Collapse file tree 2 files changed +28
-8
lines changed Original file line number Diff line number Diff line change @@ -563,12 +563,20 @@ def setup_basic_auth(user, password)
563
563
end
564
564
565
565
def setup_api_key ( api_key )
566
- return { } unless ( api_key && api_key . value )
566
+ return { } unless ( api_key & .value )
567
567
568
- token = :: Base64 . strict_encode64 ( api_key . value )
568
+ token = is_base64? ( api_key . value ) ? api_key . value : Base64 . strict_encode64 ( api_key . value )
569
569
{ 'Authorization' => "ApiKey #{ token } " }
570
570
end
571
571
572
+ def is_base64? ( string )
573
+ begin
574
+ string == Base64 . strict_encode64 ( Base64 . strict_decode64 ( string ) )
575
+ rescue ArgumentError
576
+ false
577
+ end
578
+ end
579
+
572
580
def prepare_user_agent
573
581
os_name = java . lang . System . getProperty ( 'os.name' )
574
582
os_version = java . lang . System . getProperty ( 'os.version' )
Original file line number Diff line number Diff line change @@ -823,14 +823,26 @@ def synchronize_method!(object, method_name)
823
823
end
824
824
825
825
context "with ssl" do
826
- let ( :config ) { super ( ) . merge ( { 'api_key' => LogStash ::Util ::Password . new ( 'foo:bar' ) , "ssl_enabled" => true } ) }
826
+ let ( :config ) { super ( ) . merge ( "ssl_enabled" => true ) }
827
+ encoded_api_key = Base64 . strict_encode64 ( 'foo:bar' )
827
828
828
- it "should set authorization" do
829
- plugin . register
830
- client = plugin . send ( :client )
831
- auth_header = extract_transport ( client ) . options [ :transport_options ] [ :headers ] [ 'Authorization' ]
829
+ scenarios = {
830
+ 'with non-encoded api-key' => LogStash ::Util ::Password . new ( 'foo:bar' ) ,
831
+ 'with encoded api-key' => LogStash ::Util ::Password . new ( encoded_api_key )
832
+ }
833
+
834
+ scenarios . each do |description , api_key_value |
835
+ context description do
836
+ let ( :config ) { super ( ) . merge ( 'api_key' => api_key_value ) }
832
837
833
- expect ( auth_header ) . to eql "ApiKey #{ Base64 . strict_encode64 ( 'foo:bar' ) } "
838
+ it "should set authorization" do
839
+ plugin . register
840
+ client = plugin . send ( :client )
841
+ auth_header = extract_transport ( client ) . options [ :transport_options ] [ :headers ] [ 'Authorization' ]
842
+
843
+ expect ( auth_header ) . to eql "ApiKey #{ encoded_api_key } "
844
+ end
845
+ end
834
846
end
835
847
836
848
context 'user also set' do
You can’t perform that action at this time.
0 commit comments