Exemplo n.º 1
0
    def test_from_param_ctx_found(self):
        test_file = '/tmp/.test_kc/config'
        self.addCleanup(remove_file, test_file)

        cfg = Config.from_params(filepath=test_file)

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'self')

        cfg.set_cluster('other')
        cfg.set_credentials('other')
        cfg.set_context('other', cluster='other', user='******')
        cfg.set_current_context('other')
        self.assertEqual('other', cfg.current_context)

        cfg.write_file()

        cfg = Config.from_params(context_name='other', filepath=test_file)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(cfg.context, {
            'cluster': 'other',
            'user': '******'
        })
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(cfg.cluster, {})
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(cfg.user, {})

        cfg.write_file()
Exemplo n.º 2
0
    def __init__(self, config):
        """Establish session using configurations.

        :param Config config: An object of the .kube/config configuration
        """
        if isinstance(config, dict):
            config = Config(config)
        # assume type is string as the previous handled input
        # as a dict, so the only remaining option when not
        # an instance of Config is a file location.
        if not isinstance(config, Config):
            config = Config.from_file(config)
        self.kubeconfig = config

        # Check the API url
        self.base_url = self.kubeconfig.cluster.get('server',
                                                    'http://localhost:8080')
        validator.check_url(self.base_url)

        # Initialize the connection using all the .kube/config credentials
        self.session = self._connection()

        # Test the connection before proceeding
        self._test_connection(self.base_url + '/api/')

        self.api_resources = {}
        # Load API Resources
        self._load_resources('api/v1/', 'v1')
        self._load_group_resources('apis/')
Exemplo n.º 3
0
    def test_from_params_all(self):

        cfg = Config.from_params(
            api='http://localhost',
            ca='/tmp/.minikube/ca.crt',
            auth={
                'client-certificate': '/tmp/.minikube/apiserver.crt',
                'client-key': '/tmp/.minikube/apiserver.key'},
            filepath=CONFIG_FILE)

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'self')

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 1)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 1)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 1)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(cfg.context, {
            'cluster': 'self',
            'user': '******'
        })
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(cfg.cluster, {
            'server': 'http://localhost',
            'certificate-authority': '/tmp/.minikube/ca.crt'
        })
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(cfg.user, {
            'client-certificate': '/tmp/.minikube/apiserver.crt',
            'client-key': '/tmp/.minikube/apiserver.key'
        })
Exemplo n.º 4
0
    def test_from_params_auth_clientkey(self):

        cfg = Config.from_params(auth={'client-key': '/tmp/.minikube/apiserver.key'})

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'self')

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 1)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 1)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 1)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(cfg.context, {
            'cluster': 'self',
            'user': '******'
        })
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(cfg.cluster, {})
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(cfg.user, {
            'client-key': '/tmp/.minikube/apiserver.key'
        })
Exemplo n.º 5
0
    def test_from_params_auth_clientcert(self):

        cfg = Config.from_params(
            auth={'client-certificate': '/tmp/.minikube/apiserver.crt'},
            username='******'
        )

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'self')

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 1)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 1)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 1)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(cfg.context, {
            'cluster': 'self',
            'user': '******'
        })
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(cfg.cluster, {})
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(cfg.user, {
            'client-certificate': '/tmp/.minikube/apiserver.crt'
        })
Exemplo n.º 6
0
    def test_from_params_auth_token(self):

        cfg = Config.from_params(auth='qgfe3Z3nJPAtTS_OsNeThQEwzRvwSkHenxd4lS_x0RM')

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'self')

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 1)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 1)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 1)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(cfg.context, {
            'cluster': 'self',
            'user': '******'
        })
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(cfg.cluster, {})
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(cfg.user, {
            'token': 'qgfe3Z3nJPAtTS_OsNeThQEwzRvwSkHenxd4lS_x0RM'
        })
Exemplo n.º 7
0
    def test_from_params_api_ca(self):

        cfg = Config.from_params(api='http://localhost', ca='/tmp/.minikube/ca.crt')

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'self')

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 1)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 1)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 1)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(cfg.context, {
            'cluster': 'self',
            'user': '******'
        })
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(cfg.cluster, {
            'server': 'http://localhost',
            'certificate-authority': '/tmp/.minikube/ca.crt'
        })
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(cfg.user, {})
Exemplo n.º 8
0
    def test_from_params_api_verify(self):

        cfg = Config.from_params(api='http://localhost', verify=False)

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'self')

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 1)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 1)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 1)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(cfg.context, {
            'cluster': 'self',
            'user': '******'
        })
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(cfg.cluster, {
            'server': 'http://localhost',
            'insecure-skip-tls-verify': True
        })
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(cfg.user, {})
Exemplo n.º 9
0
    def test_from_params_api_only(self):

        cfg = Config.from_params(api='http://localhost')

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'self')

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 1)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 1)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 1)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(cfg.context, {
            'cluster': 'self',
            'user': '******'
        })
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(cfg.cluster, {
            'server': 'http://localhost'
        })
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(cfg.user, {})
Exemplo n.º 10
0
    def test_merge_items(self):
        cfg = Config.from_params()

        # default item exists
        self.assertEqual(len(cfg.content['users']), 1)

        # updates default with new value
        cfg.set_credentials(None, token='abc123')
        self.assertEqual(len(cfg.content['users']), 1)

        # adds a new item to the list
        cfg.set_credentials('other', token='abc123')
        self.assertEqual(len(cfg.content['users']), 2)

        # duplicate; does nothing
        cfg.set_credentials(None, token='abc123')
        self.assertEqual(len(cfg.content['users']), 2)

        # updates default with new value
        cfg.set_credentials(None, token='abc456')
        self.assertEqual(len(cfg.content['users']), 2)

        # do not erase existing data
        cfg.set_credentials(None)
        self.assertEqual(len(cfg.content['users']), 2)

        auth = None
        for u in cfg.content['users']:
            if u['name'] == 'self':
                auth = u['user']

        self.assertEqual(auth, {'token': 'abc456'})
Exemplo n.º 11
0
    def __init__(self, *args, **kwargs):

        self.kubeconfig = Config(helper.TEST_CONFIG)
        self.base_url = self.kubeconfig.cluster.get('server')

        self.session = self._connection()
        self.api_resources = {}
Exemplo n.º 12
0
    def test_set_context(self):
        cfg = Config(None)

        cfg.set_context(None, user='******', use=True)
        self.assertIsNone(cfg.context.get('user'))

        cfg.set_context(None, namespace='default')
        self.assertEqual(cfg.context.get('namespace'), 'default')
Exemplo n.º 13
0
    def test_format_session_no_context(self):

        cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'empty_kubeconfig'))

        self.assertIsNotNone(cfg)
        self.assertIsNone(cfg.current_context)
        self.assertEqual(
            cfg.format_session(),
            {
                'cert': None,
                'headers': {},
                'verify': True
            }
        )
Exemplo n.º 14
0
    def setUp(self):
        self.config = Config(helper.TEST_CONFIG)

        patched_test_connection = patch.object(KubeBase, '_test_connection', side_effect=helper.test_connection)
        self.addCleanup(patched_test_connection.stop)
        self.mock_tc = patched_test_connection.start()

        patched_get_groups = patch.object(KubeBase, '_get_groups', side_effect=helper.get_groups)
        self.addCleanup(patched_get_groups.stop)
        self.mock_groups = patched_get_groups.start()

        patched_get_resources = patch.object(KubeBase, '_get_resources', side_effect=helper.get_resources)
        self.addCleanup(patched_get_resources.stop)
        self.mock_resources = patched_get_resources.start()
Exemplo n.º 15
0
    def test_format_session_certs_no_user(self):

        cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'certs_no_user_kubeconfig'))

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'minishift')

        session = cfg.format_session()
        self.assertIsNone(session['cert'])
        self.assertEqual(session['headers'], {})
        # indicates it was a base64 encoded cert that was written
        # to a tempfile
        self.assertTrue(session['verify'].startswith('/tmp/'))

        # clean up the test file
        self.addCleanup(remove_file, session['verify'])
Exemplo n.º 16
0
    def test_format_session_token(self):

        cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'simple_kubeconfig'))

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'default/192-168-99-103:8443/admin')
        self.assertEqual(
            cfg.format_session(),
            {
                'cert': None,
                'headers': {
                    'Authorization': 'Bearer qgfe3Z3nJPAtTS_OsNeThQEwzRvwSkHenxd4lS_x0RM'
                },
                'verify': False
            }
        )
Exemplo n.º 17
0
    def test_set_cluster(self):
        cfg = Config(None)

        cfg.set_cluster('test', server='http://localhost:8080', api_version='v2')

        self.assertEqual(cfg.clusters['test'], {'server': 'http://localhost:8080'})
        self.assertEqual(cfg.content['apiVersion'], 'v2')

        cfg.set_cluster('sample', cert_authority='/tmp/.minikube/ca.crt', embed_certs=True)
        self.assertIsNotNone(cfg.clusters['sample'].get('certificate-authority'))
        self.assertIsNotNone(cfg.clusters['sample'].get('certificate-authority-data'))
Exemplo n.º 18
0
    def test_format_session_certs(self):

        cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'certs_kubeconfig'))

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'minikube')
        self.assertEqual(
            cfg.format_session(),
            {
                'cert': (
                    '/tmp/.minikube/apiserver.crt',
                    '/tmp/.minikube/apiserver.key'
                ),
                'headers': {},
                'verify': '/tmp/.minikube/ca.crt'
            }
        )
Exemplo n.º 19
0
    def test_write_file(self):

        cfg = Config.from_params(
            api='http://localhost',
            ca='/tmp/.minikube/ca.crt',
            auth={
                'client-certificate': '/tmp/.minikube/apiserver.crt',
                'client-key': '/tmp/.minikube/apiserver.key'},
            filepath=CONFIG_FILE)

        self.assertIsNotNone(cfg)

        self.assertFalse(os.path.exists(CONFIG_FILE))
        cfg.write_file()
        self.assertTrue(os.path.exists(CONFIG_FILE))
        cfg.write_file()
        self.assertTrue(os.path.exists(CONFIG_FILE))
Exemplo n.º 20
0
    def test_format_session_certs_data(self):

        cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'certs_data_kubeconfig'))

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'default/192-168-99-103:8443/admin')

        session = cfg.format_session()
        self.assertIsNone(session['cert'])
        self.assertEqual(session['headers'], {
            'Authorization': 'Bearer 10me0gx3el9uHDfF7JdksZO_rEuBt_JuAZLrT0htoe4'
        })
        # indicates it was a base64 encoded cert that was written
        # to a tempfile
        self.assertTrue(session['verify'].startswith('/tmp/'))

        # clean up the test file
        self.addCleanup(remove_file, session['verify'])
Exemplo n.º 21
0
    def test_from_file_empty_file(self):

        cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'empty_kubeconfig'))

        self.assertIsNotNone(cfg)
        self.assertIsNone(cfg.current_context)

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 0)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 0)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 0)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(len(cfg.context), 0)
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(len(cfg.cluster), 0)
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(len(cfg.user), 0)
Exemplo n.º 22
0
    def test_from_file_empty_string(self):

        cfg = Config.from_file('')

        self.assertIsNotNone(cfg)
        self.assertIsNone(cfg.current_context)

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 0)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 0)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 0)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(len(cfg.context), 0)
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(len(cfg.cluster), 0)
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(len(cfg.user), 0)
Exemplo n.º 23
0
    def test_from_param_ctx_not_found(self):
        cfg = Config.from_params(context_name='test')

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'test')

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 1)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 1)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 1)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(cfg.context, {
            'cluster': 'self',
            'user': '******'
        })
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(cfg.cluster, {})
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(cfg.user, {})
Exemplo n.º 24
0
    def test_from_file_simple(self):

        cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'simple_kubeconfig'))

        self.assertIsNotNone(cfg)
        self.assertEqual(cfg.current_context, 'default/192-168-99-103:8443/admin')

        self.assertIsInstance(cfg.contexts, dict)
        self.assertEqual(len(cfg.contexts), 1)
        self.assertIsInstance(cfg.clusters, dict)
        self.assertEqual(len(cfg.clusters), 1)
        self.assertIsInstance(cfg.users, dict)
        self.assertEqual(len(cfg.users), 1)

        self.assertIsInstance(cfg.context, dict)
        self.assertEqual(
            cfg.context,
            {
                'cluster': '192-168-99-103:8443',
                'namespace': 'default',
                'user': '******'
            }
        )
        self.assertIsInstance(cfg.cluster, dict)
        self.assertEqual(
            cfg.cluster,
            {
                'insecure-skip-tls-verify': True,
                'server': 'https://192.168.99.103:8443'
            }
        )
        self.assertIsInstance(cfg.user, dict)
        self.assertEqual(
            cfg.user,
            {
                'token': 'qgfe3Z3nJPAtTS_OsNeThQEwzRvwSkHenxd4lS_x0RM'
            }
        )
Exemplo n.º 25
0
 def setUp(self):
     self.client = FakeKubeBase(Config(helper.TEST_CONFIG))
Exemplo n.º 26
0
    def test_set_credentials(self):
        cfg = Config(None)

        cfg.set_credentials('me', username='******')
        self.assertEqual(cfg.users['me'], {'username': '******'})

        cfg.set_credentials('me', password='******')
        self.assertEqual(cfg.users['me'], {'username': '******', 'password': '******'})

        session = cfg.format_session()
        # user is not part of current context
        self.assertFalse(session.get('headers', {}).get('Authorization', '').startswith('Basic '))

        cfg.set_context(None, user='******')
        session = cfg.format_session()
        self.assertTrue(session.get('headers', {}).get('Authorization', '').startswith('Basic '))

        cfg.set_credentials('tester', client_cert='/tmp/.minikube/apiserver.crt',
                            client_key='/tmp/.minikube/apiserver.key', embed_certs=True)

        self.assertIsNotNone(cfg.users['tester'].get('client-certificate'))
        self.assertIsNotNone(cfg.users['tester'].get('client-certificate-data'))
        self.assertIsNotNone(cfg.users['tester'].get('client-key'))
        self.assertIsNotNone(cfg.users['tester'].get('client-key-data'))
Exemplo n.º 27
0
    def test_add_item(self):
        cfg = Config.from_params()

        orig = copy.deepcopy(cfg.content)
        cfg.set_cluster(None)
        cfg.set_credentials(None)
        cfg.set_context(None, cluster='self', user='******')
        cfg.set_current_context('self')
        self.assertEqual(orig, cfg.content)

        cfg.set_current_context('other')
        self.assertNotEqual('other', cfg.current_context)

        cfg.set_cluster('other')
        cfg.set_credentials('other')
        cfg.set_context('other', cluster='other', user='******')
        cfg.set_current_context('other')
        self.assertEqual('other', cfg.current_context)

        cfg = Config({'apiVersion': 'v1'})
        self.assertIsNotNone(cfg)
        self.assertIsNone(cfg.current_context)
        self.assertNotIn('contexts', cfg.content)
        self.assertNotIn('clusters', cfg.content)
        self.assertNotIn('users', cfg.content)

        cfg.set_cluster(None)
        cfg.set_credentials(None)
        cfg.set_context(None, cluster='self', user='******')
        cfg.set_current_context('self')
        self.assertIn('contexts', cfg.content)
        self.assertIn('clusters', cfg.content)
        self.assertIn('users', cfg.content)