Skip to content

Commit 72a02cc

Browse files
committed
Merge remote-tracking branch 'upstream/master' into fix_base64_padding_for_kconfig
2 parents b3ddbd9 + 5c03b3b commit 72a02cc

25 files changed

+1184
-88
lines changed

.travis.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ref: https://docs.travis-ci.com/user/languages/python
22
language: python
3-
dist: trusty
3+
dist: xenial
44
sudo: required
55

66
matrix:
@@ -10,7 +10,7 @@ matrix:
1010
- python: 2.7
1111
env: TOXENV=py27-functional
1212
- python: 2.7
13-
env: TOXENV=update-pep8
13+
env: TOXENV=update-pycodestyle
1414
- python: 2.7
1515
env: TOXENV=docs
1616
- python: 2.7
@@ -25,10 +25,15 @@ matrix:
2525
env: TOXENV=py36
2626
- python: 3.6
2727
env: TOXENV=py36-functional
28+
- python: 3.7
29+
env: TOXENV=py37
30+
- python: 3.7
31+
env: TOXENV=py37-functional
2832

2933
install:
3034
- pip install tox
3135

3236
script:
3337
- ./run_tox.sh tox
38+
- ./hack/verify-boilerplate.sh
3439

OWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# See the OWNERS docs at https://go.k8s.io/owners
2+
13
approvers:
24
- mbohlool
35
- yliaog

config/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2016 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/config_exception.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2016 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/dateutil.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2017 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/dateutil_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2016 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/exec_provider.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
import json
17+
import os
18+
import subprocess
19+
import sys
20+
21+
from .config_exception import ConfigException
22+
23+
24+
class ExecProvider(object):
25+
"""
26+
Implementation of the proposal for out-of-tree client
27+
authentication providers as described here --
28+
https://github.com/kubernetes/community/blob/master/contributors/design-proposals/auth/kubectl-exec-plugins.md
29+
30+
Missing from implementation:
31+
32+
* TLS cert support
33+
* caching
34+
"""
35+
36+
def __init__(self, exec_config):
37+
"""
38+
exec_config must be of type ConfigNode because we depend on
39+
safe_get(self, key) to correctly handle optional exec provider
40+
config parameters.
41+
"""
42+
for key in ['command', 'apiVersion']:
43+
if key not in exec_config:
44+
raise ConfigException(
45+
'exec: malformed request. missing key \'%s\'' % key)
46+
self.api_version = exec_config['apiVersion']
47+
self.args = [exec_config['command']]
48+
if exec_config.safe_get('args'):
49+
self.args.extend(exec_config['args'])
50+
self.env = os.environ.copy()
51+
if exec_config.safe_get('env'):
52+
additional_vars = {}
53+
for item in exec_config['env']:
54+
name = item['name']
55+
value = item['value']
56+
additional_vars[name] = value
57+
self.env.update(additional_vars)
58+
59+
def run(self, previous_response=None):
60+
kubernetes_exec_info = {
61+
'apiVersion': self.api_version,
62+
'kind': 'ExecCredential',
63+
'spec': {
64+
'interactive': sys.stdout.isatty()
65+
}
66+
}
67+
if previous_response:
68+
kubernetes_exec_info['spec']['response'] = previous_response
69+
self.env['KUBERNETES_EXEC_INFO'] = json.dumps(kubernetes_exec_info)
70+
process = subprocess.Popen(
71+
self.args,
72+
stdout=subprocess.PIPE,
73+
stderr=subprocess.PIPE,
74+
env=self.env,
75+
universal_newlines=True)
76+
(stdout, stderr) = process.communicate()
77+
exit_code = process.wait()
78+
if exit_code != 0:
79+
msg = 'exec: process returned %d' % exit_code
80+
stderr = stderr.strip()
81+
if stderr:
82+
msg += '. %s' % stderr
83+
raise ConfigException(msg)
84+
try:
85+
data = json.loads(stdout)
86+
except ValueError as de:
87+
raise ConfigException(
88+
'exec: failed to decode process output: %s' % de)
89+
for key in ('apiVersion', 'kind', 'status'):
90+
if key not in data:
91+
raise ConfigException(
92+
'exec: malformed response. missing key \'%s\'' % key)
93+
if data['apiVersion'] != self.api_version:
94+
raise ConfigException(
95+
'exec: plugin api version %s does not match %s' %
96+
(data['apiVersion'], self.api_version))
97+
return data['status']

config/exec_provider_test.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
import unittest
19+
20+
import mock
21+
22+
from .config_exception import ConfigException
23+
from .exec_provider import ExecProvider
24+
from .kube_config import ConfigNode
25+
26+
27+
class ExecProviderTest(unittest.TestCase):
28+
29+
def setUp(self):
30+
self.input_ok = ConfigNode('test', {
31+
'command': 'aws-iam-authenticator',
32+
'args': ['token', '-i', 'dummy'],
33+
'apiVersion': 'client.authentication.k8s.io/v1beta1',
34+
'env': None
35+
})
36+
self.output_ok = """
37+
{
38+
"apiVersion": "client.authentication.k8s.io/v1beta1",
39+
"kind": "ExecCredential",
40+
"status": {
41+
"token": "dummy"
42+
}
43+
}
44+
"""
45+
46+
def test_missing_input_keys(self):
47+
exec_configs = [ConfigNode('test1', {}),
48+
ConfigNode('test2', {'command': ''}),
49+
ConfigNode('test3', {'apiVersion': ''})]
50+
for exec_config in exec_configs:
51+
with self.assertRaises(ConfigException) as context:
52+
ExecProvider(exec_config)
53+
self.assertIn('exec: malformed request. missing key',
54+
context.exception.args[0])
55+
56+
@mock.patch('subprocess.Popen')
57+
def test_error_code_returned(self, mock):
58+
instance = mock.return_value
59+
instance.wait.return_value = 1
60+
instance.communicate.return_value = ('', '')
61+
with self.assertRaises(ConfigException) as context:
62+
ep = ExecProvider(self.input_ok)
63+
ep.run()
64+
self.assertIn('exec: process returned %d' %
65+
instance.wait.return_value, context.exception.args[0])
66+
67+
@mock.patch('subprocess.Popen')
68+
def test_nonjson_output_returned(self, mock):
69+
instance = mock.return_value
70+
instance.wait.return_value = 0
71+
instance.communicate.return_value = ('', '')
72+
with self.assertRaises(ConfigException) as context:
73+
ep = ExecProvider(self.input_ok)
74+
ep.run()
75+
self.assertIn('exec: failed to decode process output',
76+
context.exception.args[0])
77+
78+
@mock.patch('subprocess.Popen')
79+
def test_missing_output_keys(self, mock):
80+
instance = mock.return_value
81+
instance.wait.return_value = 0
82+
outputs = [
83+
"""
84+
{
85+
"kind": "ExecCredential",
86+
"status": {
87+
"token": "dummy"
88+
}
89+
}
90+
""", """
91+
{
92+
"apiVersion": "client.authentication.k8s.io/v1beta1",
93+
"status": {
94+
"token": "dummy"
95+
}
96+
}
97+
""", """
98+
{
99+
"apiVersion": "client.authentication.k8s.io/v1beta1",
100+
"kind": "ExecCredential"
101+
}
102+
"""
103+
]
104+
for output in outputs:
105+
instance.communicate.return_value = (output, '')
106+
with self.assertRaises(ConfigException) as context:
107+
ep = ExecProvider(self.input_ok)
108+
ep.run()
109+
self.assertIn('exec: malformed response. missing key',
110+
context.exception.args[0])
111+
112+
@mock.patch('subprocess.Popen')
113+
def test_mismatched_api_version(self, mock):
114+
instance = mock.return_value
115+
instance.wait.return_value = 0
116+
wrong_api_version = 'client.authentication.k8s.io/v1'
117+
output = """
118+
{
119+
"apiVersion": "%s",
120+
"kind": "ExecCredential",
121+
"status": {
122+
"token": "dummy"
123+
}
124+
}
125+
""" % wrong_api_version
126+
instance.communicate.return_value = (output, '')
127+
with self.assertRaises(ConfigException) as context:
128+
ep = ExecProvider(self.input_ok)
129+
ep.run()
130+
self.assertIn(
131+
'exec: plugin api version %s does not match' %
132+
wrong_api_version,
133+
context.exception.args[0])
134+
135+
@mock.patch('subprocess.Popen')
136+
def test_ok_01(self, mock):
137+
instance = mock.return_value
138+
instance.wait.return_value = 0
139+
instance.communicate.return_value = (self.output_ok, '')
140+
ep = ExecProvider(self.input_ok)
141+
result = ep.run()
142+
self.assertTrue(isinstance(result, dict))
143+
self.assertTrue('token' in result)
144+
145+
146+
if __name__ == '__main__':
147+
unittest.main()

config/incluster_config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2016 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -85,7 +87,8 @@ def _set_config(self):
8587

8688

8789
def load_incluster_config():
88-
"""Use the service account kubernetes gives to pods to connect to kubernetes
90+
"""
91+
Use the service account kubernetes gives to pods to connect to kubernetes
8992
cluster. It's intended for clients that expect to be running inside a pod
9093
running on kubernetes. It will raise an exception if called from a process
9194
not running in a kubernetes environment."""

config/incluster_config_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2016 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

0 commit comments

Comments
 (0)