summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2019-02-21 19:27:22 +0000
committerGerrit Code Review <review@openstack.org>2019-02-21 19:27:22 +0000
commit56b3e10caeccf5e2d0af4eba95c2f71c7a049eee (patch)
tree901d845d52d5c9f04a9c1c3bddcc44021c3bdfb9
parentaf8016511ae14d7e3879d1035f453c306c018189 (diff)
parent671b77f6a78655d44bc7441c0c68bd1182729748 (diff)
Merge "Add CLI generation of salt"
-rw-r--r--doc/source/cli/cli.rst31
-rw-r--r--pegleg/cli.py17
-rw-r--r--pegleg/engine/generators/passphrase_generator.py6
-rw-r--r--pegleg/engine/secrets.py10
-rw-r--r--pegleg/engine/util/cryptostring.py (renamed from pegleg/engine/util/passphrase.py)18
-rw-r--r--tests/unit/engine/test_generate_cryptostring.py (renamed from tests/unit/engine/test_generate_passphrases.py)40
-rw-r--r--tests/unit/test_cli.py5
7 files changed, 91 insertions, 36 deletions
diff --git a/doc/source/cli/cli.rst b/doc/source/cli/cli.rst
index 7279bb0..f8b8383 100644
--- a/doc/source/cli/cli.rst
+++ b/doc/source/cli/cli.rst
@@ -846,3 +846,34 @@ Example with length specified:
846:: 846::
847 847
848 ./pegleg.sh generate passphrase -l <length> 848 ./pegleg.sh generate passphrase -l <length>
849
850Salt
851----
852
853Generate a salt and print to ``stdout``.
854
855**-l / --length** (Optional).
856
857Length of salt to generate. By default length is 24.
858Minimum length is 24. No maximum length.
859
860Usage:
861
862::
863
864 ./pegleg.sh generate salt -l <length>
865
866Examples
867^^^^^^^^
868
869Example without length specified:
870
871::
872
873 ./pegleg.sh generate salt
874
875Example with length specified:
876
877::
878
879 ./pegleg.sh generate salt -l <length> \ No newline at end of file
diff --git a/pegleg/cli.py b/pegleg/cli.py
index ca38864..a1982f1 100644
--- a/pegleg/cli.py
+++ b/pegleg/cli.py
@@ -549,4 +549,19 @@ def generate():
549 'Length is >= 24, default length is 24, no maximum length') 549 'Length is >= 24, default length is 24, no maximum length')
550def generate_passphrase(length): 550def generate_passphrase(length):
551 click.echo("Generated Passhprase: {}".format( 551 click.echo("Generated Passhprase: {}".format(
552 engine.secrets.generate_passphrase(length))) 552 engine.secrets.generate_crypto_string(length)))
553
554
555@generate.command(
556 'salt',
557 help='Command to generate a salt and print out to stdout')
558@click.option(
559 '-l',
560 '--length',
561 'length',
562 default=24,
563 help='Generate a salt of the given length. '
564 'Length is >= 24, default length is 24, no maximum length')
565def generate_salt(length):
566 click.echo("Generated Salt: {}".format(
567 engine.secrets.generate_crypto_string(length)))
diff --git a/pegleg/engine/generators/passphrase_generator.py b/pegleg/engine/generators/passphrase_generator.py
index c20f7f8..77b28f4 100644
--- a/pegleg/engine/generators/passphrase_generator.py
+++ b/pegleg/engine/generators/passphrase_generator.py
@@ -18,8 +18,8 @@ import logging
18from pegleg.engine.catalogs import passphrase_catalog 18from pegleg.engine.catalogs import passphrase_catalog
19from pegleg.engine.catalogs.passphrase_catalog import PassphraseCatalog 19from pegleg.engine.catalogs.passphrase_catalog import PassphraseCatalog
20from pegleg.engine.generators.base_generator import BaseGenerator 20from pegleg.engine.generators.base_generator import BaseGenerator
21from pegleg.engine.util.cryptostring import CryptoString
21from pegleg.engine.util import files 22from pegleg.engine.util import files
22from pegleg.engine.util.passphrase import Passphrase
23from pegleg.engine.util.pegleg_secret_management import PeglegSecretManagement 23from pegleg.engine.util.pegleg_secret_management import PeglegSecretManagement
24 24
25__all__ = ['PassphraseGenerator'] 25__all__ = ['PassphraseGenerator']
@@ -49,7 +49,7 @@ class PassphraseGenerator(BaseGenerator):
49 sitename, save_location, author) 49 sitename, save_location, author)
50 self._catalog = PassphraseCatalog( 50 self._catalog = PassphraseCatalog(
51 self._sitename, documents=self._documents) 51 self._sitename, documents=self._documents)
52 self._pass_util = Passphrase() 52 self._pass_util = CryptoString()
53 53
54 def generate(self, interactive=False): 54 def generate(self, interactive=False):
55 """ 55 """
@@ -67,7 +67,7 @@ class PassphraseGenerator(BaseGenerator):
67 prompt="Input passphrase for {}. Leave blank to " 67 prompt="Input passphrase for {}. Leave blank to "
68 "auto-generate:\n".format(p_name)) 68 "auto-generate:\n".format(p_name))
69 if not passphrase: 69 if not passphrase:
70 passphrase = self._pass_util.get_pass( 70 passphrase = self._pass_util.get_crypto_string(
71 self._catalog.get_length(p_name)) 71 self._catalog.get_length(p_name))
72 docs = list() 72 docs = list()
73 storage_policy = self._catalog.get_storage_policy(p_name) 73 storage_policy = self._catalog.get_storage_policy(p_name)
diff --git a/pegleg/engine/secrets.py b/pegleg/engine/secrets.py
index 0402690..5cc2092 100644
--- a/pegleg/engine/secrets.py
+++ b/pegleg/engine/secrets.py
@@ -16,9 +16,9 @@ import logging
16import os 16import os
17 17
18from pegleg.engine.generators.passphrase_generator import PassphraseGenerator 18from pegleg.engine.generators.passphrase_generator import PassphraseGenerator
19from pegleg.engine.util.cryptostring import CryptoString
19from pegleg.engine.util import definition 20from pegleg.engine.util import definition
20from pegleg.engine.util import files 21from pegleg.engine.util import files
21from pegleg.engine.util.passphrase import Passphrase
22from pegleg.engine.util.pegleg_secret_management import PeglegSecretManagement 22from pegleg.engine.util.pegleg_secret_management import PeglegSecretManagement
23 23
24__all__ = ('encrypt', 'decrypt', 'generate_passphrases') 24__all__ = ('encrypt', 'decrypt', 'generate_passphrases')
@@ -132,12 +132,12 @@ def generate_passphrases(site_name, save_location, author, interactive=False):
132 interactive=interactive) 132 interactive=interactive)
133 133
134 134
135def generate_passphrase(length): 135def generate_crypto_string(length):
136 """ 136 """
137 Create a passphrase. 137 Create a cryptographic string.
138 138
139 :param int length: Length of passphrase. 139 :param int length: Length of cryptographic string.
140 :rtype: string 140 :rtype: string
141 """ 141 """
142 142
143 return Passphrase().get_pass(length) 143 return CryptoString().get_crypto_string(length)
diff --git a/pegleg/engine/util/passphrase.py b/pegleg/engine/util/cryptostring.py
index d81385e..d50e644 100644
--- a/pegleg/engine/util/passphrase.py
+++ b/pegleg/engine/util/cryptostring.py
@@ -17,17 +17,21 @@ from random import SystemRandom
17from rstr import Rstr 17from rstr import Rstr
18import string 18import string
19 19
20__all__ = ['Passphrase'] 20__all__ = ['CryptoString']
21 21
22 22
23class Passphrase(object): 23class CryptoString(object):
24 24
25 def __init__(self): 25 def __init__(self):
26 self._pool = string.ascii_letters + string.digits + string.punctuation 26 self._pool = string.ascii_letters + string.digits + string.punctuation
27 self._rs = Rstr(SystemRandom()) 27 self._rs = Rstr(SystemRandom())
28 28
29 def get_pass(self, pass_len=24): 29 def get_crypto_string(self, len=24):
30 """Create and return a random password, of the ``pass_len`` length.""" 30 """
31 if pass_len < 24: 31 Create and return a random cryptographic string,
32 pass_len = 24 32 of the ``len`` length.
33 return self._rs.rstr(self._pool, pass_len) 33 """
34
35 if len < 24:
36 len = 24
37 return self._rs.rstr(self._pool, len)
diff --git a/tests/unit/engine/test_generate_passphrases.py b/tests/unit/engine/test_generate_cryptostring.py
index 74f0af7..2797e8f 100644
--- a/tests/unit/engine/test_generate_passphrases.py
+++ b/tests/unit/engine/test_generate_cryptostring.py
@@ -19,7 +19,7 @@ import mock
19import string 19import string
20import yaml 20import yaml
21 21
22from pegleg.engine.util.passphrase import Passphrase 22from pegleg.engine.util.cryptostring import CryptoString
23from pegleg.engine.generators.passphrase_generator import PassphraseGenerator 23from pegleg.engine.generators.passphrase_generator import PassphraseGenerator
24from pegleg.engine.util import encryption 24from pegleg.engine.util import encryption
25from pegleg.engine import util 25from pegleg.engine import util
@@ -99,30 +99,30 @@ TEST_SITE_DEFINITION = {
99TEST_SITE_DOCUMENTS = [TEST_SITE_DEFINITION, TEST_PASSPHRASES_CATALOG] 99TEST_SITE_DOCUMENTS = [TEST_SITE_DEFINITION, TEST_PASSPHRASES_CATALOG]
100 100
101 101
102def test_passphrase_default_len(): 102def test_cryptostring_default_len():
103 p_util = Passphrase() 103 s_util = CryptoString()
104 passphrase = p_util.get_pass() 104 s = s_util.get_crypto_string()
105 assert len(passphrase) == 24 105 assert len(s) == 24
106 alphabet = set(string.punctuation + string.ascii_letters + string.digits) 106 alphabet = set(string.punctuation + string.ascii_letters + string.digits)
107 assert any(c in alphabet for c in passphrase) 107 assert any(c in alphabet for c in s)
108 108
109 109
110def test_passphrase_short_len(): 110def test_cryptostring_short_len():
111 p_util = Passphrase() 111 s_util = CryptoString()
112 p = p_util.get_pass(0) 112 s = s_util.get_crypto_string(0)
113 assert len(p) == 24 113 assert len(s) == 24
114 p = p_util.get_pass(23) 114 s = s_util.get_crypto_string(23)
115 assert len(p) == 24 115 assert len(s) == 24
116 p = p_util.get_pass(-1) 116 s = s_util.get_crypto_string(-1)
117 assert len(p) == 24 117 assert len(s) == 24
118 118
119 119
120def test_passphrase_long_len(): 120def test_cryptostring_long_len():
121 p_util = Passphrase() 121 s_util = CryptoString()
122 p = p_util.get_pass(25) 122 s = s_util.get_crypto_string(25)
123 assert len(p) == 25 123 assert len(s) == 25
124 p = p_util.get_pass(128) 124 s = s_util.get_crypto_string(128)
125 assert len(p) == 128 125 assert len(s) == 128
126 126
127 127
128@mock.patch.object( 128@mock.patch.object(
diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py
index 170d81b..6908308 100644
--- a/tests/unit/test_cli.py
+++ b/tests/unit/test_cli.py
@@ -389,6 +389,11 @@ class TestGenerateActions(BaseCLIActionTest):
389 389
390 assert result.exit_code == 0, result.output 390 assert result.exit_code == 0, result.output
391 391
392 def test_generate_salt(self):
393 result = self.runner.invoke(cli.generate, ['salt'])
394
395 assert result.exit_code == 0, result.output
396
392 397
393class TestRepoCliActions(BaseCLIActionTest): 398class TestRepoCliActions(BaseCLIActionTest):
394 """Tests repo-level CLI actions.""" 399 """Tests repo-level CLI actions."""