Skip to content

Commit 8e76ef5

Browse files
authored
Merge pull request #537 from kjetijor/bearer-token-load-inline
bearer_token_file: load inline
2 parents 0f35317 + 0eb2aca commit 8e76ef5

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

lib/kubeclient.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ def create_faraday_client
353353
Faraday.new(url, options) do |connection|
354354
if @auth_options[:username]
355355
connection.request(:basic_auth, @auth_options[:username], @auth_options[:password])
356+
elsif @auth_options[:bearer_token_file]
357+
connection.request(:authorization, 'Bearer', lambda do
358+
File.read(@auth_options[:bearer_token_file]).chomp
359+
end)
356360
elsif @auth_options[:bearer_token]
357361
connection.request(:authorization, 'Bearer', @auth_options[:bearer_token])
358362
end
@@ -366,7 +370,6 @@ def create_faraday_client
366370
end
367371

368372
def faraday_client
369-
refresh_bearer_token_from_file
370373
@faraday_client ||= create_faraday_client
371374
end
372375

@@ -705,11 +708,21 @@ def return_or_yield_to_watcher(watcher, &block)
705708
end
706709

707710
def http_options(uri)
708-
refresh_bearer_token_from_file
711+
bearer_token = nil
712+
if @auth_options[:bearer_token_file]
713+
bearer_token_file = @auth_options[:bearer_token_file]
714+
if File.file?(bearer_token_file) && File.readable?(bearer_token_file)
715+
token = File.read(bearer_token_file).chomp
716+
bearer_token = "Bearer #{token}"
717+
end
718+
elsif @auth_options[:bearer_token]
719+
bearer_token = "Bearer #{@auth_options[:bearer_token]}"
720+
end
709721

710722
options = {
711723
basic_auth_user: @auth_options[:username],
712724
basic_auth_password: @auth_options[:password],
725+
authorization: bearer_token,
713726
headers: @headers,
714727
http_proxy_uri: @http_proxy_uri,
715728
http_max_redirects: http_max_redirects
@@ -730,11 +743,6 @@ def http_options(uri)
730743
options.merge(@socket_options)
731744
end
732745

733-
def refresh_bearer_token_from_file
734-
return unless (file = @auth_options[:bearer_token_file])
735-
@auth_options[:bearer_token] = File.read(file).chomp
736-
end
737-
738746
def json_headers
739747
{ 'Content-Type' => 'application/json' }
740748
end

lib/kubeclient/watch_stream.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def build_client
6565
)
6666
end
6767

68+
if @http_options[:authorization]
69+
client = client.auth(@http_options[:authorization])
70+
end
71+
6872
client
6973
end
7074

0 commit comments

Comments
 (0)