Skip to content

Commit 47565b3

Browse files
authored
Merge pull request #87 from pinterest/prefix-redis
Add prefix to redis
2 parents a42815d + 76962f8 commit 47565b3

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

README.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Configuration
6868

6969
You can configure the following via environment variables.
7070

71-
`SECRET_KEY` this should be a unique key that's used to sign key. This should
71+
`SECRET_KEY` unique key that's used to sign key. This should
7272
be kept secret. See the `Flask Documentation`__ for more information.
7373

7474
.. __: http://flask.pocoo.org/docs/quickstart/#sessions
@@ -88,7 +88,9 @@ need to change this.
8888

8989
`SNAPPASS_REDIS_DB` is the database that you want to use on this redis server. Defaults to db 0
9090

91-
`REDIS_URL` is optional and, if set, will be used instead of `REDIS_HOST`, `REDIS_PORT`, and `SNAPPASS_REDIS_DB` to configure the Redis client object. For example: redis://username:password@localhost:6379/0
91+
`REDIS_URL` (optional) will be used instead of `REDIS_HOST`, `REDIS_PORT`, and `SNAPPASS_REDIS_DB` to configure the Redis client object. For example: redis://username:password@localhost:6379/0
92+
93+
`REDIS_PREFIX` (optional, defaults to `"snappass"`) prefix used on redis keys to prevent collisions with other potential clients
9294

9395
Docker
9496
------

snappass/main.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@
1313

1414

1515
SNEAKY_USER_AGENTS = ('Slackbot', 'facebookexternalhit', 'Twitterbot',
16-
'Facebot', 'WhatsApp', 'SkypeUriPreview',
17-
'Iframely')
16+
'Facebot', 'WhatsApp', 'SkypeUriPreview', 'Iframely')
1817
SNEAKY_USER_AGENTS_RE = re.compile('|'.join(SNEAKY_USER_AGENTS))
1918
NO_SSL = os.environ.get('NO_SSL', False)
2019
TOKEN_SEPARATOR = '~'
2120

2221

22+
# Initialize Flask Application
2323
app = Flask(__name__)
2424
if os.environ.get('DEBUG'):
2525
app.debug = True
2626
app.secret_key = os.environ.get('SECRET_KEY', 'Secret Key')
2727
app.config.update(
2828
dict(STATIC_URL=os.environ.get('STATIC_URL', 'static')))
2929

30+
# Initialize Redis
3031
if os.environ.get('MOCK_REDIS'):
3132
from mockredis import mock_strict_redis_client
3233
redis_client = mock_strict_redis_client()
@@ -38,6 +39,7 @@
3839
redis_db = os.environ.get('SNAPPASS_REDIS_DB', 0)
3940
redis_client = redis.StrictRedis(
4041
host=redis_host, port=redis_port, db=redis_db)
42+
REDIS_PREFIX = os.environ.get('REDIS_PREFIX', 'snappass')
4143

4244
TIME_CONVERSION = {'week': 604800, 'day': 86400, 'hour': 3600}
4345

@@ -97,7 +99,7 @@ def set_password(password, ttl):
9799
Returns a token comprised of the key where the encrypted password
98100
is stored, and the decryption key.
99101
"""
100-
storage_key = uuid.uuid4().hex
102+
storage_key = REDIS_PREFIX + uuid.uuid4().hex
101103
encrypted_password, encryption_key = encrypt(password)
102104
redis_client.setex(storage_key, ttl, encrypted_password)
103105
encryption_key = encryption_key.decode('utf-8')

tests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_returned_token_format(self):
3737
token_fragments = token.split(snappass.TOKEN_SEPARATOR)
3838
self.assertEqual(2, len(token_fragments))
3939
redis_key, encryption_key = token_fragments
40-
self.assertEqual(32, len(redis_key))
40+
self.assertEqual(32 + len(snappass.REDIS_PREFIX), len(redis_key))
4141
try:
4242
Fernet(encryption_key.encode('utf-8'))
4343
except ValueError:
@@ -130,7 +130,7 @@ def test_bots_denial(self):
130130
]
131131

132132
for ua in a_few_sneaky_bots:
133-
rv = self.app.get('/{0}'.format(key), headers={ 'User-Agent': ua })
133+
rv = self.app.get('/{0}'.format(key), headers={'User-Agent': ua})
134134
self.assertEqual(404, rv.status_code)
135135

136136

0 commit comments

Comments
 (0)