Skip to content

Commit

Permalink
Merge pull request #537 from kjetijor/bearer-token-load-inline
Browse files Browse the repository at this point in the history
bearer_token_file: load inline
  • Loading branch information
cben authored Mar 9, 2022
2 parents 0f35317 + 0eb2aca commit 8e76ef5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/kubeclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ def create_faraday_client
Faraday.new(url, options) do |connection|
if @auth_options[:username]
connection.request(:basic_auth, @auth_options[:username], @auth_options[:password])
elsif @auth_options[:bearer_token_file]
connection.request(:authorization, 'Bearer', lambda do
File.read(@auth_options[:bearer_token_file]).chomp
end)
elsif @auth_options[:bearer_token]
connection.request(:authorization, 'Bearer', @auth_options[:bearer_token])
end
Expand All @@ -366,7 +370,6 @@ def create_faraday_client
end

def faraday_client
refresh_bearer_token_from_file
@faraday_client ||= create_faraday_client
end

Expand Down Expand Up @@ -705,11 +708,21 @@ def return_or_yield_to_watcher(watcher, &block)
end

def http_options(uri)
refresh_bearer_token_from_file
bearer_token = nil
if @auth_options[:bearer_token_file]
bearer_token_file = @auth_options[:bearer_token_file]
if File.file?(bearer_token_file) && File.readable?(bearer_token_file)
token = File.read(bearer_token_file).chomp
bearer_token = "Bearer #{token}"
end
elsif @auth_options[:bearer_token]
bearer_token = "Bearer #{@auth_options[:bearer_token]}"
end

options = {
basic_auth_user: @auth_options[:username],
basic_auth_password: @auth_options[:password],
authorization: bearer_token,
headers: @headers,
http_proxy_uri: @http_proxy_uri,
http_max_redirects: http_max_redirects
Expand All @@ -730,11 +743,6 @@ def http_options(uri)
options.merge(@socket_options)
end

def refresh_bearer_token_from_file
return unless (file = @auth_options[:bearer_token_file])
@auth_options[:bearer_token] = File.read(file).chomp
end

def json_headers
{ 'Content-Type' => 'application/json' }
end
Expand Down
4 changes: 4 additions & 0 deletions lib/kubeclient/watch_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def build_client
)
end

if @http_options[:authorization]
client = client.auth(@http_options[:authorization])
end

client
end

Expand Down

0 comments on commit 8e76ef5

Please sign in to comment.