2017-10-18 18:58:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import os
|
|
|
|
import shutil
|
2017-10-19 00:09:27 +00:00
|
|
|
import stat
|
2017-10-18 18:58:36 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
|
|
|
from tests.support.case import ShellCase
|
2017-10-18 22:03:06 +00:00
|
|
|
from tests.support.paths import TMP, INTEGRATION_TEST_DIR
|
2017-10-18 18:58:36 +00:00
|
|
|
|
|
|
|
# Import 3rd-party libs
|
|
|
|
|
|
|
|
# Import Salt libs
|
|
|
|
import salt.utils.files
|
|
|
|
|
|
|
|
|
2017-10-19 00:09:27 +00:00
|
|
|
# all read, only owner write
|
|
|
|
autosign_file_permissions = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR
|
|
|
|
autosign_file_path = os.path.join(TMP, 'rootdir', 'autosign_file')
|
|
|
|
|
|
|
|
|
2017-10-18 18:58:36 +00:00
|
|
|
class AutosignGrainsTest(ShellCase):
|
|
|
|
'''
|
|
|
|
Test autosigning minions based on grain values.
|
|
|
|
'''
|
|
|
|
|
2017-10-18 22:03:06 +00:00
|
|
|
def setUp(self):
|
|
|
|
shutil.copyfile(
|
|
|
|
os.path.join(INTEGRATION_TEST_DIR, 'files', 'autosign_grains', 'autosign_file'),
|
2017-10-19 00:09:27 +00:00
|
|
|
autosign_file_path
|
2017-10-18 22:03:06 +00:00
|
|
|
)
|
2017-10-19 00:09:27 +00:00
|
|
|
os.chmod(autosign_file_path, autosign_file_permissions)
|
|
|
|
|
2017-10-18 19:29:17 +00:00
|
|
|
self.run_key('-d minion -y')
|
2017-10-19 00:14:36 +00:00
|
|
|
self.run_call('test.ping -l quiet') # get minon to try to authenticate itself again
|
2017-10-18 18:58:36 +00:00
|
|
|
|
2017-10-18 22:03:06 +00:00
|
|
|
if 'minion' in self.run_key('-l acc'):
|
|
|
|
self.skipTest('Could not deauthorize minion')
|
|
|
|
if 'minion' not in self.run_key('-l un'):
|
2017-10-19 00:09:27 +00:00
|
|
|
self.skipTest('minion did not try to reauthenticate itself')
|
2017-10-18 18:58:36 +00:00
|
|
|
|
2017-10-18 22:03:06 +00:00
|
|
|
self.autosign_grains_dir = os.path.join(self.master_opts['autosign_grains_dir'])
|
|
|
|
if not os.path.isdir(self.autosign_grains_dir):
|
|
|
|
os.makedirs(self.autosign_grains_dir)
|
2017-10-18 18:58:36 +00:00
|
|
|
|
2017-10-18 22:03:06 +00:00
|
|
|
def tearDown(self):
|
|
|
|
shutil.copyfile(
|
|
|
|
os.path.join(INTEGRATION_TEST_DIR, 'files', 'autosign_file'),
|
2017-10-19 00:09:27 +00:00
|
|
|
autosign_file_path
|
2017-10-18 22:03:06 +00:00
|
|
|
)
|
2017-10-19 00:09:27 +00:00
|
|
|
os.chmod(autosign_file_path, autosign_file_permissions)
|
|
|
|
|
2017-10-19 00:14:36 +00:00
|
|
|
self.run_call('test.ping -l quiet') # get minon to try to authenticate itself again
|
2017-10-18 22:03:06 +00:00
|
|
|
self.run_key('-a minion -y')
|
2017-10-18 18:58:36 +00:00
|
|
|
|
2017-10-18 22:03:06 +00:00
|
|
|
if os.path.isdir(self.autosign_grains_dir):
|
|
|
|
shutil.rmtree(self.autosign_grains_dir)
|
2017-10-18 18:58:36 +00:00
|
|
|
|
2017-10-18 22:03:06 +00:00
|
|
|
def test_autosign_grains_accept(self):
|
|
|
|
with salt.utils.files.fopen(os.path.join(self.autosign_grains_dir, 'test_grain'), 'w') as f:
|
|
|
|
f.write('#invalid_value\ncheese')
|
2017-10-18 18:58:36 +00:00
|
|
|
|
2017-10-18 22:03:06 +00:00
|
|
|
self.run_call('test.ping') # get minon to try to authenticate itself again
|
|
|
|
self.assertIn('minion', self.run_key('-l acc'))
|
|
|
|
|
|
|
|
def test_autosign_grains_fail(self):
|
|
|
|
with salt.utils.files.fopen(os.path.join(self.autosign_grains_dir, 'test_grain'), 'w') as f:
|
2017-10-18 18:58:36 +00:00
|
|
|
f.write('#cheese\ninvalid_value')
|
|
|
|
|
2017-10-18 19:29:17 +00:00
|
|
|
self.run_call('test.ping') # get minon to try to authenticate itself again
|
2017-10-18 22:03:06 +00:00
|
|
|
self.assertNotIn('minion', self.run_key('-l acc'))
|
|
|
|
self.assertIn('minion', self.run_key('-l un'))
|