Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit abc5d2b

Browse files
committed
Implement exec auth plugin
1 parent e8c0d98 commit abc5d2b

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

Diff for: config/exec_provider.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import json
15+
import os
16+
import subprocess
17+
import sys
18+
19+
from .config_exception import ConfigException
20+
21+
22+
class ExecProvider(object):
23+
def __init__(self, exec_config):
24+
for key in ['command', 'apiVersion']:
25+
if key not in exec_config:
26+
raise ConfigException(
27+
'exec: malformed request. missing key \'%s\'' % key)
28+
self.api_version = exec_config['apiVersion']
29+
self.args = [exec_config['command']]
30+
if 'args' in exec_config:
31+
self.args.extend(exec_config['args'])
32+
self.env = os.environ.copy()
33+
if 'env' in exec_config:
34+
additional_vars = {}
35+
for item in exec_config['env']:
36+
name = item['name']
37+
value = item['value']
38+
additional_vars[name] = value
39+
self.env.update(additional_vars)
40+
41+
def run(self, previous_response=None):
42+
kubernetes_exec_info = {
43+
'apiVersion': self.api_version,
44+
'kind': 'ExecCredential',
45+
'spec': {
46+
'interactive': True
47+
}
48+
}
49+
if previous_response:
50+
kubernetes_exec_info['spec']['response'] = previous_response
51+
self.env['KUBERNETES_EXEC_INFO'] = json.dumps(kubernetes_exec_info)
52+
process = subprocess.Popen(
53+
self.args,
54+
stdout=subprocess.PIPE,
55+
stderr=subprocess.PIPE,
56+
env=self.env,
57+
universal_newlines=True)
58+
(stdout, stderr) = process.communicate()
59+
exit_code = process.wait()
60+
if exit_code != 0:
61+
msg = 'exec: process returned %d' % exit_code
62+
stderr = stderr.strip()
63+
if stderr:
64+
msg += '. %s' % stderr
65+
raise ConfigException(msg)
66+
stdout = stdout
67+
try:
68+
data = json.loads(stdout)
69+
except json.decoder.JSONDecodeError as de:
70+
raise ConfigException(
71+
'exec: failed to decode process output: %s' % de)
72+
if not all(k in data for k in ('apiVersion', 'kind', 'status')):
73+
raise ConfigException(
74+
'exec: malformed response. plugin returned: %s' % stdout)
75+
if data['apiVersion'] != self.api_version:
76+
raise ConfigException(
77+
'exec: plugin api version %s does not match %s' %
78+
(data['apiVersion'], self.api_version))
79+
return data['status']

Diff for: config/kube_config.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
from .config_exception import ConfigException
3333
from .dateutil import UTC, format_rfc3339, parse_rfc3339
34+
from .exec_provider import ExecProvider
3435

3536
EXPIRY_SKEW_PREVENTION_DELAY = datetime.timedelta(minutes=5)
3637
KUBE_CONFIG_DEFAULT_LOCATION = os.environ.get('KUBECONFIG', '~/.kube/config')
@@ -170,18 +171,19 @@ def _load_authentication(self):
170171
section of kube-config and stops if it finds a valid authentication
171172
method. The order of authentication methods is:
172173
173-
1. GCP auth-provider
174-
2. token_data
175-
3. token field (point to a token file)
176-
4. oidc auth-provider
177-
5. username/password
174+
1. auth-provider (gcp, azure, oidc)
175+
2. token field (point to a token file)
176+
3. exec provided plugin
177+
4. username/password
178178
"""
179179
if not self._user:
180180
return
181181
if self._load_auth_provider_token():
182182
return
183183
if self._load_user_token():
184184
return
185+
if self._load_from_exec_plugin():
186+
return
185187
self._load_user_pass_token()
186188

187189
def _load_auth_provider_token(self):
@@ -319,6 +321,15 @@ def _refresh_oidc(self, provider):
319321
provider['config'].value['id-token'] = refresh['id_token']
320322
provider['config'].value['refresh-token'] = refresh['refresh_token']
321323

324+
def _load_from_exec_plugin(self):
325+
if 'exec' not in self._user:
326+
return
327+
status = ExecProvider(self._user['exec']).run()
328+
if 'token' not in status:
329+
return
330+
self.token = "Bearer %s" % status['token']
331+
return True
332+
322333
def _load_user_token(self):
323334
token = FileOrData(
324335
self._user, 'tokenFile', 'token',

0 commit comments

Comments
 (0)