|
29 | 29 | import roundup.anypy.random_ as random_ |
30 | 30 |
|
31 | 31 | from roundup.anypy.strings import us2s, b2s, s2b |
32 | | - |
| 32 | +from roundup.exceptions import RoundupException |
33 | 33 |
|
34 | 34 | try: |
35 | 35 | with warnings.catch_warnings(): |
|
41 | 41 | _bempty = b"" |
42 | 42 | _bjoin = _bempty.join |
43 | 43 |
|
| 44 | +class ConfigNotSet(RoundupException): |
| 45 | + pass |
44 | 46 |
|
45 | 47 | def bchr(c): |
46 | 48 | if bytes == str: |
@@ -190,7 +192,37 @@ def encodePassword(plaintext, scheme, other=None, config=None): |
190 | 192 | if config: |
191 | 193 | rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS |
192 | 194 | else: |
193 | | - rounds = 2000000 |
| 195 | + import os |
| 196 | + import sys |
| 197 | + if ("pytest" in sys.modules and |
| 198 | + "PYTEST_CURRENT_TEST" in os.environ): |
| 199 | + # Set rounds to 1000 if no config is passed and |
| 200 | + # we are running within a pytest test. Using |
| 201 | + # actual 2M production values makes testing |
| 202 | + # increase from 12 minutes to 1 hour in CI. |
| 203 | + rounds = 1000 |
| 204 | + else: |
| 205 | + import logging |
| 206 | + # Log and abort. Initialize rounds and log (which |
| 207 | + # will probably be ignored) with traceback in case |
| 208 | + # ConfigNotSet exception is removed in the |
| 209 | + # future. |
| 210 | + rounds = 2000000 |
| 211 | + logger = logging.getLogger('roundup') |
| 212 | + if sys.version_info[0] > 2: |
| 213 | + logger.critical( |
| 214 | + "encodePassword called without config.", |
| 215 | + stack_info = True) |
| 216 | + else: |
| 217 | + import inspect, traceback |
| 218 | + where = inspect.currentframe() |
| 219 | + trace = traceback.format_stack(where) |
| 220 | + logger.critical( |
| 221 | + "encodePassword called without config. %s", |
| 222 | + trace[:-1] |
| 223 | + ) |
| 224 | + raise ConfigNotSet("encodePassword called without config.") |
| 225 | + |
194 | 226 | if rounds < 1000: |
195 | 227 | raise PasswordValueError("invalid PBKDF2 hash (rounds too low)") |
196 | 228 | raw_digest = pbkdf2(plaintext, raw_salt, rounds, 20) |
|
0 commit comments