2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-05-05 14:09:23 +00:00
|
|
|
# Import python libs
|
2012-10-30 16:54:23 +00:00
|
|
|
import os
|
2013-10-15 21:09:37 +00:00
|
|
|
import yaml
|
2012-09-05 21:31:12 +00:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2013-06-27 12:24:18 +00:00
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
2013-10-16 18:13:16 +00:00
|
|
|
from salttesting import skipIf
|
2013-06-27 12:24:18 +00:00
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
2012-04-21 23:31:46 +00:00
|
|
|
# Import salt libs
|
2013-06-27 12:24:18 +00:00
|
|
|
import integration
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2012-04-21 23:31:46 +00:00
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
class KeyTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
2012-04-21 23:31:46 +00:00
|
|
|
'''
|
|
|
|
Test salt-key script
|
|
|
|
'''
|
2012-08-04 21:55:15 +00:00
|
|
|
|
|
|
|
_call_binary_ = 'salt-key'
|
|
|
|
|
2013-12-31 09:32:04 +00:00
|
|
|
def test_list_accepted_args(self):
|
|
|
|
'''
|
|
|
|
test salt-key -l for accepted arguments
|
|
|
|
'''
|
|
|
|
for key in ('acc', 'pre', 'un', 'rej'):
|
|
|
|
# These should not trigger any error
|
|
|
|
data = self.run_key('-l {0}'.format(key), catch_stderr=True)
|
|
|
|
self.assertNotIn('error:', '\n'.join(data[1]))
|
|
|
|
data = self.run_key('-l foo-{0}'.format(key), catch_stderr=True)
|
|
|
|
self.assertIn('error:', '\n'.join(data[1]))
|
|
|
|
|
|
|
|
def test_list_all(self):
|
2012-04-21 23:31:46 +00:00
|
|
|
'''
|
|
|
|
test salt-key -L
|
|
|
|
'''
|
|
|
|
data = self.run_key('-L')
|
2014-07-09 16:14:23 +00:00
|
|
|
expect = None
|
|
|
|
if self.master_opts['transport'] == 'zeromq':
|
|
|
|
expect = [
|
|
|
|
'Accepted Keys:',
|
|
|
|
'minion',
|
|
|
|
'sub_minion',
|
|
|
|
'Unaccepted Keys:',
|
|
|
|
'Rejected Keys:'
|
|
|
|
]
|
|
|
|
elif self.master_opts['transport'] == 'raet':
|
|
|
|
expect = [
|
|
|
|
'Accepted Keys:',
|
|
|
|
'master',
|
|
|
|
'minion',
|
|
|
|
'sub_minion',
|
|
|
|
'Unaccepted Keys:',
|
|
|
|
'Rejected Keys:'
|
|
|
|
]
|
2012-04-21 23:31:46 +00:00
|
|
|
self.assertEqual(data, expect)
|
2012-04-21 23:47:04 +00:00
|
|
|
|
2012-05-26 22:22:24 +00:00
|
|
|
def test_list_json_out(self):
|
|
|
|
'''
|
|
|
|
test salt-key -L --json-out
|
|
|
|
'''
|
2012-11-17 17:39:06 +00:00
|
|
|
data = self.run_key('-L --out json')
|
2014-07-09 16:14:23 +00:00
|
|
|
expect = None
|
|
|
|
if self.master_opts['transport'] == 'zeromq':
|
|
|
|
expect = [
|
|
|
|
'{',
|
|
|
|
' "minions_rejected": [], ',
|
|
|
|
' "minions_pre": [], ',
|
|
|
|
' "minions": [',
|
|
|
|
' "minion", ',
|
|
|
|
' "sub_minion"',
|
|
|
|
' ]',
|
|
|
|
'}',
|
|
|
|
]
|
|
|
|
elif self.master_opts['transport'] == 'raet':
|
|
|
|
expect = [
|
|
|
|
'{',
|
|
|
|
' "accepted": [',
|
|
|
|
' "master", ',
|
|
|
|
' "minion", ',
|
|
|
|
' "sub_minion"',
|
|
|
|
' ], ',
|
|
|
|
' "rejected": [], ',
|
|
|
|
' "pending": []',
|
|
|
|
'}'
|
|
|
|
]
|
2012-05-26 22:22:24 +00:00
|
|
|
self.assertEqual(data, expect)
|
|
|
|
|
|
|
|
def test_list_yaml_out(self):
|
|
|
|
'''
|
|
|
|
test salt-key -L --yaml-out
|
|
|
|
'''
|
2012-11-17 17:39:06 +00:00
|
|
|
data = self.run_key('-L --out yaml')
|
2014-07-09 16:14:23 +00:00
|
|
|
expect = []
|
|
|
|
if self.master_opts['transport'] == 'zeromq':
|
|
|
|
expect = [
|
|
|
|
'minions:',
|
|
|
|
'- minion',
|
|
|
|
'- sub_minion',
|
|
|
|
'minions_pre: []',
|
|
|
|
'minions_rejected: []',
|
|
|
|
]
|
|
|
|
elif self.master_opts['transport'] == 'raet':
|
|
|
|
expect = [
|
|
|
|
'accepted:',
|
|
|
|
'- master',
|
|
|
|
'- minion',
|
|
|
|
'- sub_minion',
|
|
|
|
'pending: []',
|
|
|
|
'rejected: []'
|
|
|
|
]
|
2012-05-26 22:22:24 +00:00
|
|
|
self.assertEqual(data, expect)
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2012-05-26 22:22:24 +00:00
|
|
|
def test_list_raw_out(self):
|
|
|
|
'''
|
|
|
|
test salt-key -L --raw-out
|
|
|
|
'''
|
2012-11-17 17:39:06 +00:00
|
|
|
data = self.run_key('-L --out raw')
|
2014-07-09 16:14:23 +00:00
|
|
|
expect = None
|
|
|
|
if self.master_opts['transport'] == 'zeromq':
|
|
|
|
expect = [
|
|
|
|
"{'minions_rejected': [], 'minions_pre': [], "
|
|
|
|
"'minions': ['minion', 'sub_minion']}"
|
|
|
|
]
|
|
|
|
elif self.master_opts['transport'] == 'raet':
|
|
|
|
expected_txt = (
|
|
|
|
'{\'accepted\': '
|
|
|
|
'[\'master\', \'minion\', \'sub_minion\'], '
|
|
|
|
'\'rejected\': [], \'pending\': []}'
|
|
|
|
)
|
|
|
|
expect = [expected_txt]
|
2012-05-26 22:22:24 +00:00
|
|
|
self.assertEqual(data, expect)
|
|
|
|
|
2012-04-21 23:47:04 +00:00
|
|
|
def test_list_acc(self):
|
|
|
|
'''
|
|
|
|
test salt-key -l
|
|
|
|
'''
|
|
|
|
data = self.run_key('-l acc')
|
2014-07-09 16:14:23 +00:00
|
|
|
if self.master_opts['transport'] == 'zeromq':
|
|
|
|
self.assertEqual(
|
|
|
|
data,
|
|
|
|
['Accepted Keys:', 'minion', 'sub_minion']
|
|
|
|
)
|
|
|
|
elif self.master_opts['transport'] == 'raet':
|
|
|
|
self.assertEqual(
|
|
|
|
data,
|
|
|
|
[
|
|
|
|
'minions:',
|
|
|
|
' - master',
|
|
|
|
' - minion',
|
|
|
|
' - sub_minion'
|
|
|
|
]
|
|
|
|
)
|
2012-04-21 23:47:04 +00:00
|
|
|
|
|
|
|
def test_list_un(self):
|
|
|
|
'''
|
|
|
|
test salt-key -l
|
|
|
|
'''
|
|
|
|
data = self.run_key('-l un')
|
2014-07-09 16:14:23 +00:00
|
|
|
expect = None
|
|
|
|
if self.master_opts['transport'] == 'zeromq':
|
|
|
|
expect = ['Unaccepted Keys:']
|
|
|
|
elif self.master_opts['transport'] == 'raet':
|
|
|
|
expect = ['minions_pre:']
|
2012-04-21 23:47:04 +00:00
|
|
|
self.assertEqual(
|
2012-10-30 16:54:23 +00:00
|
|
|
data,
|
2014-07-09 16:14:23 +00:00
|
|
|
expect
|
2012-10-30 16:54:23 +00:00
|
|
|
)
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2012-09-05 21:31:12 +00:00
|
|
|
def test_keys_generation(self):
|
2013-08-27 15:31:58 +00:00
|
|
|
tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
|
2012-10-30 16:54:23 +00:00
|
|
|
arg_str = '--gen-keys minibar --gen-keys-dir {0}'.format(tempdir)
|
|
|
|
self.run_key(arg_str)
|
2012-09-05 21:31:12 +00:00
|
|
|
try:
|
2014-07-09 16:14:23 +00:00
|
|
|
key_names = None
|
|
|
|
if self.master_opts['transport'] == 'zeromq':
|
|
|
|
key_names = ('minibar.pub', 'minibar.pem')
|
|
|
|
elif self.master_opts['transport'] == 'raet':
|
|
|
|
key_names = ('minibar.key',)
|
|
|
|
for fname in key_names:
|
2012-10-30 16:54:23 +00:00
|
|
|
self.assertTrue(os.path.isfile(os.path.join(tempdir, fname)))
|
2012-09-05 21:31:12 +00:00
|
|
|
finally:
|
|
|
|
shutil.rmtree(tempdir)
|
|
|
|
|
2013-10-16 16:27:05 +00:00
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
2012-09-05 21:31:12 +00:00
|
|
|
def test_keys_generation_no_configdir(self):
|
2013-08-27 15:31:58 +00:00
|
|
|
tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
|
2012-10-30 16:54:23 +00:00
|
|
|
arg_str = '--gen-keys minibar --gen-keys-dir {0}'.format(tempdir)
|
|
|
|
self.run_script('salt-key', arg_str)
|
2012-09-05 21:31:12 +00:00
|
|
|
try:
|
2014-07-09 16:14:23 +00:00
|
|
|
key_names = None
|
|
|
|
if self.master_opts['transport'] == 'zeromq':
|
|
|
|
key_names = ('minibar.pub', 'minibar.pem')
|
|
|
|
elif self.master_opts['transport'] == 'raet':
|
|
|
|
key_names = ('minibar.key',)
|
|
|
|
for fname in key_names:
|
2012-10-30 16:54:23 +00:00
|
|
|
self.assertTrue(os.path.isfile(os.path.join(tempdir, fname)))
|
2012-09-05 21:31:12 +00:00
|
|
|
finally:
|
|
|
|
shutil.rmtree(tempdir)
|
|
|
|
|
|
|
|
def test_keys_generation_keysize_minmax(self):
|
2013-08-27 15:31:58 +00:00
|
|
|
tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
|
2012-09-05 21:31:12 +00:00
|
|
|
arg_str = '--gen-keys minion --gen-keys-dir {0}'.format(tempdir)
|
|
|
|
try:
|
|
|
|
data, error = self.run_key(
|
|
|
|
arg_str + ' --keysize=1024', catch_stderr=True
|
|
|
|
)
|
|
|
|
self.assertIn(
|
|
|
|
'salt-key: error: The minimum value for keysize is 2048', error
|
|
|
|
)
|
|
|
|
|
|
|
|
data, error = self.run_key(
|
|
|
|
arg_str + ' --keysize=32769', catch_stderr=True
|
|
|
|
)
|
|
|
|
self.assertIn(
|
2013-06-24 22:53:59 +00:00
|
|
|
'salt-key: error: The maximum value for keysize is 32768',
|
|
|
|
error
|
2012-09-05 21:31:12 +00:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(tempdir)
|
|
|
|
|
2013-10-15 21:09:37 +00:00
|
|
|
def test_issue_7754(self):
|
|
|
|
old_cwd = os.getcwd()
|
|
|
|
config_dir = os.path.join(integration.TMP, 'issue-7754')
|
|
|
|
if not os.path.isdir(config_dir):
|
|
|
|
os.makedirs(config_dir)
|
|
|
|
|
|
|
|
os.chdir(config_dir)
|
|
|
|
|
|
|
|
config_file_name = 'master'
|
|
|
|
config = yaml.load(
|
|
|
|
open(self.get_config_file_path(config_file_name), 'r').read()
|
|
|
|
)
|
|
|
|
config['log_file'] = 'file:///dev/log/LOG_LOCAL3'
|
|
|
|
open(os.path.join(config_dir, config_file_name), 'w').write(
|
|
|
|
yaml.dump(config, default_flow_style=False)
|
|
|
|
)
|
2013-10-15 23:05:04 +00:00
|
|
|
ret = self.run_script(
|
2013-10-15 21:09:37 +00:00
|
|
|
self._call_binary_,
|
|
|
|
'--config-dir {0} -L'.format(
|
|
|
|
config_dir
|
|
|
|
),
|
|
|
|
timeout=15
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
self.assertIn('minion', '\n'.join(ret))
|
|
|
|
self.assertFalse(os.path.isdir(os.path.join(config_dir, 'file:')))
|
|
|
|
finally:
|
|
|
|
os.chdir(old_cwd)
|
|
|
|
if os.path.isdir(config_dir):
|
|
|
|
shutil.rmtree(config_dir)
|
|
|
|
|
2012-09-05 21:31:12 +00:00
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(KeyTest)
|