Skip to content
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
1 change: 1 addition & 0 deletions kubernetes/lib/kubernetes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require 'kubernetes/api_error'
require 'kubernetes/version'
require 'kubernetes/configuration'
require 'kubernetes/loader'

# Configuration
require 'kubernetes/config/error'
Expand Down
8 changes: 8 additions & 0 deletions kubernetes/spec/config/kube_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@
expect(user['authorization']).to eq(TEST_BASIC_TOKEN)
end
end

context 'if azure user' do
it 'should return the access token' do
user = kube_config.find_user('user_azure')

expect(user['authorization']).to eq(TEST_AZURE_TOKEN)
end
end
end

context '#list_context_names' do
Expand Down
16 changes: 15 additions & 1 deletion kubernetes/spec/fixtures/config/kube_config_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
TEST_PASSWORD = 'pass'.freeze
# token for me:pass
TEST_BASIC_TOKEN = 'Basic bWU6cGFzcw=='.freeze
TEST_AZURE_TOKEN = 'Bearer some-token'.freeze

TEST_SSL_HOST = 'https://test-host'.freeze
TEST_CERTIFICATE_AUTH = 'cert-auth'.freeze
Expand Down Expand Up @@ -146,6 +147,18 @@
}
}.freeze

TEST_USER_AZURE = {
'name' => 'user_azure',
'user' => {
'auth-provider' => {
'name' => 'azure',
'config' => {
'access-token' => 'some-token'
}
}
}
}.freeze

TEST_KUBE_CONFIG = {
'current-context' => 'no_user',
'contexts' => [
Expand All @@ -167,6 +180,7 @@
TEST_USER_SIMPLE_TOKEN_FILE,
TEST_USER_USER_PASS,
TEST_USER_CERT_DATA,
TEST_USER_CERT_FILE
TEST_USER_CERT_FILE,
TEST_USER_AZURE
]
}.freeze
7 changes: 7 additions & 0 deletions kubernetes/src/kubernetes/config/kube_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require 'kubernetes/utils'

module Kubernetes
# rubocop:disable ClassLength
# The KubeConfig class represents configuration based on a YAML
# representation.
class KubeConfig
Expand Down Expand Up @@ -121,15 +122,20 @@ def load_token_file(user)
end
end

# rubocop:disable AbcSize
def setup_auth(user)
# Convert token field to http header
if user['token']
user['authorization'] = "Bearer #{user['token']}"
elsif user['username'] && user['password']
user_pass = "#{user['username']}:#{user['password']}"
user['authorization'] = "Basic #{Base64.strict_encode64(user_pass)}"
elsif user['auth-provider'] && user['auth-provider']['name'] == 'azure'
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we not add a test? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

token = user['auth-provider']['config']['access-token']
user['authorization'] = "Bearer #{token}"
end
end
# rubocop:enable AbcSize

def list_context_names
config['contexts'].map { |e| e['name'] }
Expand Down Expand Up @@ -159,4 +165,5 @@ def find_by_name(list, key, name)
obj[key].dup
end
end
# rubocop:enable ClassLength
end