mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 17:33:54 +00:00
commit
5db2670d44
28
salt/key.py
28
salt/key.py
@ -26,11 +26,25 @@ class KeyCLI(object):
|
||||
Print out the keys under a named status
|
||||
'''
|
||||
keys = self.key.list_keys()
|
||||
if status.startswith('pre') or status.startswith('un'):
|
||||
if status.startswith('acc'):
|
||||
salt.output.display_output(
|
||||
{'minions': keys['minions']},
|
||||
'key',
|
||||
self.opts
|
||||
)
|
||||
elif status.startswith('pre') or status.startswith('un'):
|
||||
salt.output.display_output(
|
||||
{'minions_pre': keys['minions_pre']},
|
||||
'key',
|
||||
self.opts)
|
||||
self.opts
|
||||
)
|
||||
elif status.startswith('rej'):
|
||||
salt.output.display_output(
|
||||
{'minions_rejected': keys['minions_rejected']},
|
||||
'key',
|
||||
self.opts
|
||||
)
|
||||
|
||||
|
||||
def list_all(self):
|
||||
'''
|
||||
@ -236,7 +250,7 @@ class Key(object):
|
||||
matches = self.list_keys()
|
||||
ret = {}
|
||||
for status, keys in matches.items():
|
||||
for key in keys:
|
||||
for key in salt.utils.isorted(keys):
|
||||
if fnmatch.fnmatch(key, match):
|
||||
if not status in ret:
|
||||
ret[status] = []
|
||||
@ -248,7 +262,7 @@ class Key(object):
|
||||
Return a dict of local keys
|
||||
'''
|
||||
ret = {'local': []}
|
||||
for fn_ in os.listdir(self.opts['pki_dir']):
|
||||
for fn_ in salt.utils.isorted(os.listdir(self.opts['pki_dir'])):
|
||||
if fn_.endswith('.pub') or fn_.endswith('.pem'):
|
||||
path = os.path.join(self.opts['pki_dir'], fn_)
|
||||
if os.path.isfile(path):
|
||||
@ -263,7 +277,7 @@ class Key(object):
|
||||
ret = {}
|
||||
for dir_ in acc, pre, rej:
|
||||
ret[os.path.basename(dir_)] = []
|
||||
for fn_ in os.listdir(dir_):
|
||||
for fn_ in salt.utils.isorted(os.listdir(dir_)):
|
||||
ret[os.path.basename(dir_)].append(fn_)
|
||||
return ret
|
||||
|
||||
@ -282,7 +296,7 @@ class Key(object):
|
||||
ret = {}
|
||||
for status, keys in self.name_match(match).items():
|
||||
ret[status] = {}
|
||||
for key in keys:
|
||||
for key in salt.utils.isorted(keys):
|
||||
path = os.path.join(self.opts['pki_dir'], status, key)
|
||||
with open(path, 'r') as fp_:
|
||||
ret[status][key] = fp_.read()
|
||||
@ -295,7 +309,7 @@ class Key(object):
|
||||
ret = {}
|
||||
for status, keys in self.list_keys().items():
|
||||
ret[status] = {}
|
||||
for key in keys:
|
||||
for key in salt.utils.isorted(keys):
|
||||
path = os.path.join(self.opts['pki_dir'], status, key)
|
||||
with open(path, 'r') as fp_:
|
||||
ret[status][key] = fp_.read()
|
||||
|
@ -653,3 +653,16 @@ def istextfile(fp_, blocksize=512):
|
||||
nontext = block.translate(None, text_characters)
|
||||
return float(len(nontext)) / len(block) <= 0.30
|
||||
|
||||
|
||||
def isorted(to_sort):
|
||||
"""
|
||||
Sort a list of strings ignoring case.
|
||||
|
||||
>>> L = ['foo', 'Foo', 'bar', 'Bar']
|
||||
>>> sorted(L)
|
||||
['Bar', 'Foo', 'bar', 'foo']
|
||||
>>> sorted(L, key=lambda x: x.lower())
|
||||
['bar', 'Bar', 'foo', 'Foo']
|
||||
>>>
|
||||
"""
|
||||
return sorted(to_sort, key=lambda x: x.lower())
|
||||
|
@ -22,12 +22,14 @@ class CallTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
||||
def test_default_output(self):
|
||||
out = self.run_call('test.fib 3')
|
||||
self.assertEqual(
|
||||
"local: !!python/tuple\n- [0, 1, 1, 2]", '\n'.join(out[:-3])
|
||||
'local: !!python/tuple\n- [0, 1, 1, 2]', '\n'.join(out[:-2])
|
||||
)
|
||||
|
||||
def test_text_output(self):
|
||||
out = self.run_call('--text-out test.fib 3')
|
||||
self.assertEqual("local: ([0, 1, 1, 2]", ''.join(out).rsplit(",", 1)[0])
|
||||
self.assertEqual(
|
||||
'local: ([0, 1, 1, 2]', ''.join(out).rsplit(",", 1)[0]
|
||||
)
|
||||
|
||||
@skipIf(sys.platform.startswith('win'), 'This test does not apply on Win')
|
||||
def test_user_delete_kw_output(self):
|
||||
|
@ -1,4 +1,5 @@
|
||||
# Import python libs
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
import tempfile
|
||||
@ -9,7 +10,8 @@ import integration
|
||||
from integration import TestDaemon
|
||||
|
||||
|
||||
class KeyTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
||||
class KeyTest(integration.ShellCase,
|
||||
integration.ShellCaseCommonTestsMixIn):
|
||||
'''
|
||||
Test salt-key script
|
||||
'''
|
||||
@ -22,11 +24,13 @@ class KeyTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
||||
'''
|
||||
data = self.run_key('-L')
|
||||
expect = [
|
||||
'Unaccepted Keys:',
|
||||
'Accepted Keys:',
|
||||
'minion',
|
||||
'sub_minion',
|
||||
'Rejected:', '']
|
||||
'Unaccepted Keys:',
|
||||
'Rejected Keys:',
|
||||
''
|
||||
]
|
||||
self.assertEqual(data, expect)
|
||||
|
||||
def test_list_json_out(self):
|
||||
@ -34,14 +38,15 @@ class KeyTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
||||
test salt-key -L --json-out
|
||||
'''
|
||||
data = self.run_key('-L --json-out')
|
||||
|
||||
expect = [
|
||||
'{',
|
||||
' "unaccepted": [], ',
|
||||
' "accepted": [',
|
||||
' "minions_rejected": [], ',
|
||||
' "minions_pre": [], ',
|
||||
' "minions": [',
|
||||
' "minion", ',
|
||||
' "sub_minion"',
|
||||
' ], ',
|
||||
' "rejected": []',
|
||||
' ]',
|
||||
'}',
|
||||
''
|
||||
]
|
||||
@ -53,11 +58,10 @@ class KeyTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
||||
'''
|
||||
data = self.run_key('-L --yaml-out')
|
||||
expect = [
|
||||
'accepted: [minion, sub_minion]',
|
||||
'rejected: []',
|
||||
'unaccepted: []',
|
||||
'minions: [minion, sub_minion]',
|
||||
'minions_pre: []',
|
||||
'minions_rejected: []',
|
||||
'',
|
||||
''
|
||||
]
|
||||
self.assertEqual(data, expect)
|
||||
|
||||
@ -67,9 +71,10 @@ class KeyTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
||||
'''
|
||||
data = self.run_key('-L --raw-out')
|
||||
expect = [
|
||||
"{'accepted': ['minion', 'sub_minion'], 'rejected': [], 'unaccepted': []}",
|
||||
''
|
||||
]
|
||||
"{'minions': ['minion', 'sub_minion'],",
|
||||
" 'minions_pre': [],",
|
||||
" 'minions_rejected': []}",
|
||||
'']
|
||||
self.assertEqual(data, expect)
|
||||
|
||||
def test_list_acc(self):
|
||||
@ -79,11 +84,7 @@ class KeyTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
||||
data = self.run_key('-l acc')
|
||||
self.assertEqual(
|
||||
data,
|
||||
[
|
||||
'minion',
|
||||
'sub_minion',
|
||||
''
|
||||
]
|
||||
['Accepted Keys:', 'minion', 'sub_minion', '']
|
||||
)
|
||||
|
||||
def test_list_un(self):
|
||||
@ -93,25 +94,26 @@ class KeyTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
||||
data = self.run_key('-l un')
|
||||
self.assertEqual(
|
||||
data,
|
||||
['']
|
||||
['Unaccepted Keys:', '']
|
||||
)
|
||||
|
||||
def test_keys_generation(self):
|
||||
tempdir = tempfile.mkdtemp()
|
||||
arg_str = '--gen-keys minion --gen-keys-dir {0}'.format(tempdir)
|
||||
data = self.run_key(arg_str)
|
||||
arg_str = '--gen-keys minibar --gen-keys-dir {0}'.format(tempdir)
|
||||
self.run_key(arg_str)
|
||||
try:
|
||||
self.assertIn('Keys generation complete', data)
|
||||
for fname in ('minibar.pub', 'minibar.pem'):
|
||||
self.assertTrue(os.path.isfile(os.path.join(tempdir, fname)))
|
||||
finally:
|
||||
shutil.rmtree(tempdir)
|
||||
|
||||
|
||||
def test_keys_generation_no_configdir(self):
|
||||
tempdir = tempfile.mkdtemp()
|
||||
arg_str = '--gen-keys minion --gen-keys-dir {0}'.format(tempdir)
|
||||
data = self.run_script('salt-key', arg_str)
|
||||
arg_str = '--gen-keys minibar --gen-keys-dir {0}'.format(tempdir)
|
||||
self.run_script('salt-key', arg_str)
|
||||
try:
|
||||
self.assertIn('Keys generation complete', data)
|
||||
for fname in ('minibar.pub', 'minibar.pem'):
|
||||
self.assertTrue(os.path.isfile(os.path.join(tempdir, fname)))
|
||||
finally:
|
||||
shutil.rmtree(tempdir)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user