Skip to content

Commit 444a4db

Browse files
committed
feat: parse kubeconfig for bearer_token_file
1 parent 796ff19 commit 444a4db

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

lib/kubeclient/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ def fetch_user_key_data(user)
173173

174174
def fetch_user_auth_options(user)
175175
options = {}
176-
if user.key?('token')
176+
if user.key?('tokenFile')
177+
options[:bearer_token_file] = user['tokenFile']
178+
elsif user.key?('token')
177179
options[:bearer_token] = user['token']
178180
elsif user.key?('exec_result') && user['exec_result'].key?('token')
179181
options[:bearer_token] = user['exec_result']['token']

test/config/userauth.kubeconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ contexts:
99
namespace: default
1010
user: system:admin:token
1111
name: localhost/system:admin:token
12+
- context:
13+
cluster: localhost:6443
14+
namespace: default
15+
user: system:admin:token-file
16+
name: localhost/system:admin:token-file
1217
- context:
1318
cluster: localhost:6443
1419
namespace: default
@@ -21,6 +26,9 @@ users:
2126
- name: system:admin:token
2227
user:
2328
token: 0123456789ABCDEF0123456789ABCDEF
29+
- name: system:admin:token-file
30+
user:
31+
tokenFile: /path/to/secret/token
2432
- name: system:admin:userpass
2533
user:
2634
username: admin

test/test_config.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,35 @@ def test_nouser
9898
def test_user_token
9999
config = Kubeclient::Config.read(config_file('userauth.kubeconfig'))
100100
assert_equal(
101-
['localhost/system:admin:token', 'localhost/system:admin:userpass'],
101+
['localhost/system:admin:token',
102+
'localhost/system:admin:token-file',
103+
'localhost/system:admin:userpass'],
102104
config.contexts
103105
)
104106
context = config.context('localhost/system:admin:token')
105107
check_context(context, ssl: true, custom_ca: false, client_cert: false)
106108
assert_equal('0123456789ABCDEF0123456789ABCDEF', context.auth_options[:bearer_token])
107109
end
108110

111+
def test_user_token_file
112+
config = Kubeclient::Config.read(config_file('userauth.kubeconfig'))
113+
assert_equal(
114+
['localhost/system:admin:token',
115+
'localhost/system:admin:token-file',
116+
'localhost/system:admin:userpass'],
117+
config.contexts
118+
)
119+
context = config.context('localhost/system:admin:token-file')
120+
check_context(context, ssl: true, custom_ca: false, client_cert: false)
121+
assert_equal('/path/to/secret/token', context.auth_options[:bearer_token_file])
122+
end
123+
109124
def test_user_password
110125
config = Kubeclient::Config.read(config_file('userauth.kubeconfig'))
111126
assert_equal(
112-
['localhost/system:admin:token', 'localhost/system:admin:userpass'],
127+
['localhost/system:admin:token',
128+
'localhost/system:admin:token-file',
129+
'localhost/system:admin:userpass'],
113130
config.contexts
114131
)
115132
context = config.context('localhost/system:admin:userpass')

0 commit comments

Comments
 (0)