Skip to content

Commit

Permalink
Merge pull request #98 from kobotoolbox/allow-empty-redis-password
Browse files Browse the repository at this point in the history
Allow empty redis password
  • Loading branch information
jnm authored Jun 11, 2020
2 parents abedd94 + 6e10091 commit 3041ffb
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
21 changes: 19 additions & 2 deletions helpers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys
import time
from datetime import datetime
from random import choice, randint
from random import choice

from helpers.cli import CLI
from helpers.network import Network
Expand Down Expand Up @@ -1332,11 +1332,28 @@ def __questions_raven(self):
def __questions_redis(self):
CLI.colored_print("Redis password?", CLI.COLOR_SUCCESS)
self.__config["redis_password"] = CLI.get_response(
r"~^.{8,}$",
r"~^.{8,}|$",
self.__config.get("redis_password"),
to_lower=False,
error_msg='Too short. 8 characters minimum.')

if not self.__config["redis_password"]:
CLI.colored_print("╔═════════════════════════════════════════════════╗",
CLI.COLOR_WARNING)
CLI.colored_print("║ WARNING! it's STRONGLY recommended to set a ║",
CLI.COLOR_WARNING)
CLI.colored_print("║ password for Redis as well. ║",
CLI.COLOR_WARNING)
CLI.colored_print("╚═════════════════════════════════════════════════╝",
CLI.COLOR_WARNING)

CLI.colored_print("Do you want to continue?", CLI.COLOR_SUCCESS)
CLI.colored_print("\t1) Yes")
CLI.colored_print("\t2) No")

if CLI.get_response([Config.TRUE, Config.FALSE], Config.FALSE) == Config.FALSE:
self.__questions_redis()

def __questions_reverse_proxy(self):

if self.is_secure:
Expand Down
55 changes: 54 additions & 1 deletion helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fnmatch
import json
import os
import re
import stat
import sys
try:
Expand Down Expand Up @@ -285,7 +286,7 @@ def __read_unique_id(destination_directory):
def __write_templates(template_variables_, root_, destination_directory_, filenames_):
for filename in fnmatch.filter(filenames_, '*.tpl'):
with open(os.path.join(root_, filename), "r") as template:
t = PyTemplate(template.read())
t = ExtendedPyTemplate(template.read(), template_variables_)
with open(os.path.join(destination_directory_, filename[:-4]), "w") as f:
f.write(t.substitute(template_variables_))

Expand All @@ -306,3 +307,55 @@ def __write_unique_id(cls, destination_directory, unique_id):
return False

return True


class ExtendedPyTemplate(PyTemplate):
"""
Basic class to add conditional substitution to `string.Template`
Usage example:
```
{
"host": "redis-cache.kobo.local",
"port": "6379"{% if REDIS_PASSWORD %},{% endif REDIS_PASSWORD %}
{% if REDIS_PASSWORD %}
"password": ${REDIS_PASSWORD}
{% endif REDIS_PASSWORD %}
}
```
If `REDIS_PASSWORD` equals '123456', output would be:
```
{
"host": "redis-cache.kobo.local",
"port": "6379",
"password": '123456'
}
```
If `REDIS_PASSWORD` equals '' (or `False` or `None`), output would be:
```
{
"host": "redis-cache.kobo.local",
"port": "6379"
}
```
"""
IF_PATTERN = '{{% if {} %}}'
ENDIF_PATTERN = '{{% endif {} %}}'

def __init__(self, template, template_variables_):
for key, value in template_variables_.items():
if self.IF_PATTERN.format(key) in template:
if value:
if_pattern = r'{}\s*'.format(self.IF_PATTERN.format(key))
endif_pattern = r'\s*{}'.format(self.ENDIF_PATTERN.format(key))
template = re.sub(if_pattern, '', template)
template = re.sub(endif_pattern, '', template)
else:
pattern = r'{}(.|\s)*?{}'.format(self.IF_PATTERN.format(key),
self.ENDIF_PATTERN.format(key))
template = re.sub(pattern, '', template)
super(ExtendedPyTemplate, self).__init__(template)
8 changes: 6 additions & 2 deletions templates/kobo-deployments/enketo_express/config.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@
"redis": {
"cache": {
"host": "redis-cache.${PRIVATE_DOMAIN_NAME}",
"port": "${REDIS_CACHE_PORT}",
"port": "${REDIS_CACHE_PORT}"{% if REDIS_PASSWORD %},{% endif REDIS_PASSWORD %}
{% if REDIS_PASSWORD %}
"password": ${REDIS_PASSWORD_JS_ENCODED}
{% endif REDIS_PASSWORD %}
},
"main": {
"host": "redis-main.${PRIVATE_DOMAIN_NAME}",
"port": "${REDIS_MAIN_PORT}",
"port": "${REDIS_MAIN_PORT}"{% if REDIS_PASSWORD %},{% endif REDIS_PASSWORD %}
{% if REDIS_PASSWORD %}
"password": ${REDIS_PASSWORD_JS_ENCODED}
{% endif REDIS_PASSWORD %}
}
},
"google": {
Expand Down
4 changes: 2 additions & 2 deletions templates/kobo-deployments/envfiles/databases.txt.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ ${USE_BACKUP}POSTGRES_BACKUP_SCHEDULE=${POSTGRES_BACKUP_SCHEDULE}
# Default Redis backup schedule is weekly at 03:00 AM UTC on Sunday.
${USE_BACKUP}REDIS_BACKUP_SCHEDULE=${REDIS_BACKUP_SCHEDULE}

REDIS_SESSION_URL=redis://:${REDIS_PASSWORD_URL_ENCODED}@redis-cache.${PRIVATE_DOMAIN_NAME}:${REDIS_CACHE_PORT}/2
REDIS_LOCK_URL=redis://:${REDIS_PASSWORD_URL_ENCODED}@redis-cache.${PRIVATE_DOMAIN_NAME}:${REDIS_CACHE_PORT}/3
REDIS_SESSION_URL=redis://{% if REDIS_PASSWORD %}:${REDIS_PASSWORD_URL_ENCODED}@{% endif REDIS_PASSWORD %}redis-cache.${PRIVATE_DOMAIN_NAME}:${REDIS_CACHE_PORT}/2
REDIS_LOCK_URL=redis://{% if REDIS_PASSWORD %}:${REDIS_PASSWORD_URL_ENCODED}@{% endif REDIS_PASSWORD %}redis-cache.${PRIVATE_DOMAIN_NAME}:${REDIS_CACHE_PORT}/3
REDIS_PASSWORD=${REDIS_PASSWORD}
2 changes: 1 addition & 1 deletion templates/kobo-deployments/envfiles/kobocat.txt.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ${USE_X_FORWARDED_HOST}USE_X_FORWARDED_HOST=True
DJANGO_SETTINGS_MODULE=onadata.settings.kc_environ
ENKETO_VERSION=Express

KOBOCAT_BROKER_URL=redis://:${REDIS_PASSWORD_URL_ENCODED}@redis-main.${PRIVATE_DOMAIN_NAME}:${REDIS_MAIN_PORT}/2
KOBOCAT_BROKER_URL=redis://{% if REDIS_PASSWORD %}:${REDIS_PASSWORD_URL_ENCODED}@{% endif REDIS_PASSWORD %}redis-main.${PRIVATE_DOMAIN_NAME}:${REDIS_MAIN_PORT}/2
KOBOCAT_CELERY_LOG_FILE=/srv/logs/celery.log

# Default KoBoCAT media backup schedule is weekly at 12:00 AM UTC on Sunday.
Expand Down
2 changes: 1 addition & 1 deletion templates/kobo-deployments/envfiles/kpi.txt.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ${USE_X_FORWARDED_HOST}USE_X_FORWARDED_HOST=True

ENKETO_VERSION=Express
KPI_PREFIX=/
KPI_BROKER_URL=redis://:${REDIS_PASSWORD_URL_ENCODED}@redis-main.${PRIVATE_DOMAIN_NAME}:${REDIS_MAIN_PORT}/1
KPI_BROKER_URL=redis://{% if REDIS_PASSWORD %}:${REDIS_PASSWORD_URL_ENCODED}@{% endif REDIS_PASSWORD %}redis-main.${PRIVATE_DOMAIN_NAME}:${REDIS_MAIN_PORT}/1

KPI_MONGO_HOST=mongo.${PRIVATE_DOMAIN_NAME}
KPI_MONGO_PORT=${MONGO_PORT}
Expand Down

0 comments on commit 3041ffb

Please sign in to comment.