Browse Source

Merge "Add CLI generation of salt"

changes/92/613392/35
Zuul 3 months ago
parent
commit
56b3e10cae

+ 31
- 0
doc/source/cli/cli.rst View File

@@ -846,3 +846,34 @@ Example with length specified:
846 846
 ::
847 847
 
848 848
     ./pegleg.sh generate passphrase -l <length>
849
+
850
+Salt
851
+----
852
+
853
+Generate a salt and print to ``stdout``.
854
+
855
+**-l / --length** (Optional).
856
+
857
+Length of salt to generate.  By default length is 24.
858
+Minimum length is 24.  No maximum length.
859
+
860
+Usage:
861
+
862
+::
863
+
864
+    ./pegleg.sh generate salt -l <length>
865
+
866
+Examples
867
+^^^^^^^^
868
+
869
+Example without length specified:
870
+
871
+::
872
+
873
+    ./pegleg.sh generate salt
874
+
875
+Example with length specified:
876
+
877
+::
878
+
879
+    ./pegleg.sh generate salt -l <length>

+ 16
- 1
pegleg/cli.py View File

@@ -549,4 +549,19 @@ def generate():
549 549
          'Length is >= 24, default length is 24, no maximum length')
550 550
 def generate_passphrase(length):
551 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')
565
+def generate_salt(length):
566
+    click.echo("Generated Salt: {}".format(
567
+        engine.secrets.generate_crypto_string(length)))

+ 3
- 3
pegleg/engine/generators/passphrase_generator.py View File

@@ -18,8 +18,8 @@ import logging
18 18
 from pegleg.engine.catalogs import passphrase_catalog
19 19
 from pegleg.engine.catalogs.passphrase_catalog import PassphraseCatalog
20 20
 from pegleg.engine.generators.base_generator import BaseGenerator
21
+from pegleg.engine.util.cryptostring import CryptoString
21 22
 from pegleg.engine.util import files
22
-from pegleg.engine.util.passphrase import Passphrase
23 23
 from pegleg.engine.util.pegleg_secret_management import PeglegSecretManagement
24 24
 
25 25
 __all__ = ['PassphraseGenerator']
@@ -49,7 +49,7 @@ class PassphraseGenerator(BaseGenerator):
49 49
             sitename, save_location, author)
50 50
         self._catalog = PassphraseCatalog(
51 51
             self._sitename, documents=self._documents)
52
-        self._pass_util = Passphrase()
52
+        self._pass_util = CryptoString()
53 53
 
54 54
     def generate(self, interactive=False):
55 55
         """
@@ -67,7 +67,7 @@ class PassphraseGenerator(BaseGenerator):
67 67
                     prompt="Input passphrase for {}. Leave blank to "
68 68
                            "auto-generate:\n".format(p_name))
69 69
             if not passphrase:
70
-                passphrase = self._pass_util.get_pass(
70
+                passphrase = self._pass_util.get_crypto_string(
71 71
                     self._catalog.get_length(p_name))
72 72
             docs = list()
73 73
             storage_policy = self._catalog.get_storage_policy(p_name)

+ 5
- 5
pegleg/engine/secrets.py View File

@@ -16,9 +16,9 @@ import logging
16 16
 import os
17 17
 
18 18
 from pegleg.engine.generators.passphrase_generator import PassphraseGenerator
19
+from pegleg.engine.util.cryptostring import CryptoString
19 20
 from pegleg.engine.util import definition
20 21
 from pegleg.engine.util import files
21
-from pegleg.engine.util.passphrase import Passphrase
22 22
 from pegleg.engine.util.pegleg_secret_management import PeglegSecretManagement
23 23
 
24 24
 __all__ = ('encrypt', 'decrypt', 'generate_passphrases')
@@ -132,12 +132,12 @@ def generate_passphrases(site_name, save_location, author, interactive=False):
132 132
         interactive=interactive)
133 133
 
134 134
 
135
-def generate_passphrase(length):
135
+def 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 140
     :rtype: string
141 141
     """
142 142
 
143
-    return Passphrase().get_pass(length)
143
+    return CryptoString().get_crypto_string(length)

pegleg/engine/util/passphrase.py → pegleg/engine/util/cryptostring.py View File

@@ -17,17 +17,21 @@ from random import SystemRandom
17 17
 from rstr import Rstr
18 18
 import string
19 19
 
20
-__all__ = ['Passphrase']
20
+__all__ = ['CryptoString']
21 21
 
22 22
 
23
-class Passphrase(object):
23
+class CryptoString(object):
24 24
 
25 25
     def __init__(self):
26 26
         self._pool = string.ascii_letters + string.digits + string.punctuation
27 27
         self._rs = Rstr(SystemRandom())
28 28
 
29
-    def get_pass(self, pass_len=24):
30
-        """Create and return a random password, of the ``pass_len`` length."""
31
-        if pass_len < 24:
32
-            pass_len = 24
33
-        return self._rs.rstr(self._pool, pass_len)
29
+    def get_crypto_string(self, len=24):
30
+        """
31
+        Create and return a random cryptographic string,
32
+        of the ``len`` length.
33
+        """
34
+
35
+        if len < 24:
36
+            len = 24
37
+        return self._rs.rstr(self._pool, len)

tests/unit/engine/test_generate_passphrases.py → tests/unit/engine/test_generate_cryptostring.py View File

@@ -19,7 +19,7 @@ import mock
19 19
 import string
20 20
 import yaml
21 21
 
22
-from pegleg.engine.util.passphrase import Passphrase
22
+from pegleg.engine.util.cryptostring import CryptoString
23 23
 from pegleg.engine.generators.passphrase_generator import PassphraseGenerator
24 24
 from pegleg.engine.util import encryption
25 25
 from pegleg.engine import util
@@ -99,30 +99,30 @@ TEST_SITE_DEFINITION = {
99 99
 TEST_SITE_DOCUMENTS = [TEST_SITE_DEFINITION, TEST_PASSPHRASES_CATALOG]
100 100
 
101 101
 
102
-def test_passphrase_default_len():
103
-    p_util = Passphrase()
104
-    passphrase = p_util.get_pass()
105
-    assert len(passphrase) == 24
102
+def test_cryptostring_default_len():
103
+    s_util = CryptoString()
104
+    s = s_util.get_crypto_string()
105
+    assert len(s) == 24
106 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
 
110
-def test_passphrase_short_len():
111
-    p_util = Passphrase()
112
-    p = p_util.get_pass(0)
113
-    assert len(p) == 24
114
-    p = p_util.get_pass(23)
115
-    assert len(p) == 24
116
-    p = p_util.get_pass(-1)
117
-    assert len(p) == 24
110
+def test_cryptostring_short_len():
111
+    s_util = CryptoString()
112
+    s = s_util.get_crypto_string(0)
113
+    assert len(s) == 24
114
+    s = s_util.get_crypto_string(23)
115
+    assert len(s) == 24
116
+    s = s_util.get_crypto_string(-1)
117
+    assert len(s) == 24
118 118
 
119 119
 
120
-def test_passphrase_long_len():
121
-    p_util = Passphrase()
122
-    p = p_util.get_pass(25)
123
-    assert len(p) == 25
124
-    p = p_util.get_pass(128)
125
-    assert len(p) == 128
120
+def test_cryptostring_long_len():
121
+    s_util = CryptoString()
122
+    s = s_util.get_crypto_string(25)
123
+    assert len(s) == 25
124
+    s = s_util.get_crypto_string(128)
125
+    assert len(s) == 128
126 126
 
127 127
 
128 128
 @mock.patch.object(

+ 5
- 0
tests/unit/test_cli.py View File

@@ -389,6 +389,11 @@ class TestGenerateActions(BaseCLIActionTest):
389 389
 
390 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
 
393 398
 class TestRepoCliActions(BaseCLIActionTest):
394 399
     """Tests repo-level CLI actions."""

Loading…
Cancel
Save