Skip to content

Commit

Permalink
consolidated tira-application-config.*.yml and improved settings.py f…
Browse files Browse the repository at this point in the history
…urther
  • Loading branch information
TheMrSheldon committed Sep 6, 2024
1 parent c8f81e5 commit 74ce9f7
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .devcontainer/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ COPY <<EOF /etc/discourse/client-api-key
I am so secret
EOF

# Environment Variables for TIRA Configuration:
ENV TIRA_ROOT=/workspaces/tira/model/src
ENV TIRA_CONFIG=/workspaces/tira/application/config/tira-application-config.yml
ENV TIRA_DEBUG=true

########################################################################################################################
# Client #
########################################################################################################################
Expand Down
2 changes: 0 additions & 2 deletions application/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
config
!config/*.docker.yml
mock-data

.dockerignore
Expand Down
1 change: 1 addition & 0 deletions application/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ ENV PATH=/home/tira/.local/bin:$PATH
# CONFIGURE THE FOLLOWING ENVIRONMENT VARIABLES IN YOUR DOCKER-COMPOSE FILE
ENV HF_HOME=/home/tira/data/publicly-shared-datasets/huggingface/
ENV TIRA_CONFIG=/tira/config/tira-application-config.docker.yml
ENV TIRA_DEBUG=false

EXPOSE 80

Expand Down
24 changes: 0 additions & 24 deletions application/config/tira-application-config.docker.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ---task/celebrity-profiling/user/hodge20a
debug: true
debug: ${TIRA_DEBUG:false}
allowed_hosts:
- "127.0.0.1"
django_secret: "not-so-secret"
Expand Down
45 changes: 14 additions & 31 deletions application/src/django_admin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

import errno
import importlib.resources as resources
import logging
import os
Expand All @@ -22,10 +23,7 @@
BASE_DIR = Path(__file__).resolve().parent.parent

custom_settings = {}
# for cfg in (BASE_DIR / "config").glob("*.yml"):
# print(f"Load settings from {cfg}.")
# custom_settings.update(yaml.load(open(cfg, "r").read(), Loader=yaml.FullLoader))
cfgpath = os.environ.get("TIRA_CONFIG", str(BASE_DIR / "config" / "tira-application-config.dev.yml"))
cfgpath = os.environ.get("TIRA_CONFIG", str(BASE_DIR / "config" / "tira-application-config.yml"))
logging.info(f"Load settings from {cfgpath}.")
config = parse_config(cfgpath, default_value=None)
custom_settings.update(config)
Expand All @@ -37,20 +35,20 @@
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = custom_settings.get("django_secret", "not-so-secret")
SECRET_KEY = custom_settings["django_secret"]


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = custom_settings.get("debug", True)
ALLOWED_HOSTS = custom_settings.get("allowed_hosts", [])
DEBUG = custom_settings["debug"]
ALLOWED_HOSTS = custom_settings["allowed_hosts"]

TIRA_ROOT = Path(custom_settings.get("tira_root", BASE_DIR.parents[1] / "model" / "src"))
TIRA_ROOT = Path(custom_settings["tira_root"])
if not TIRA_ROOT.is_dir():
logging.warning(f"{TIRA_ROOT} does not exists and will be created now.")

(TIRA_ROOT / "state").mkdir(parents=True, exist_ok=True)

DISRAPTOR_SECRET_FILE = Path(custom_settings.get("disraptor_secret_file", "/etc/discourse/client-api-key"))
DISRAPTOR_SECRET_FILE = Path(custom_settings["disraptor_secret_file"])
HOST_GRPC_PORT = custom_settings.get("host_grpc_port", "50051")
APPLICATION_GRPC_PORT = custom_settings.get("application_grpc_port", "50052")
GRPC_HOST = custom_settings.get("grpc_host", "local") # can be local or remote
Expand Down Expand Up @@ -361,24 +359,6 @@ def logger_config(log_dir: Path):
raise PermissionError(f"Can not write to {ld} in production mode.")


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]

# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

Expand Down Expand Up @@ -415,7 +395,10 @@ def logger_config(log_dir: Path):
}

CODE_SUBMISSION_REPOSITORY_NAMESPACE = "tira-io"
try:
DISRAPTOR_API_KEY = open(DISRAPTOR_SECRET_FILE, "r").read().strip()
except Exception:
pass
if DISRAPTOR_SECRET_FILE.exists():
DISRAPTOR_API_KEY = DISRAPTOR_SECRET_FILE.read_text().strip()
elif DEBUG:
logging.warning(f"The Disraptor Secret File, {DISRAPTOR_SECRET_FILE}, could not be found.")

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This expression logs
sensitive data (secret)
as clear text.
This expression logs
sensitive data (secret)
as clear text.
DISRAPTOR_API_KEY = ""
else:
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), str(DISRAPTOR_SECRET_FILE))
43 changes: 13 additions & 30 deletions application/test/settings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

import errno
import importlib.resources as resources
import logging
import os
Expand All @@ -22,10 +23,7 @@
BASE_DIR = Path(__file__).resolve().parent.parent

custom_settings = {}
# for cfg in (BASE_DIR / "config").glob("*.yml"):
# print(f"Load settings from {cfg}.")
# custom_settings.update(yaml.load(open(cfg, "r").read(), Loader=yaml.FullLoader))
cfgpath = os.environ.get("TIRA_CONFIG", str(BASE_DIR / "config" / "tira-application-config.dev.yml"))
cfgpath = os.environ.get("TIRA_CONFIG", str(BASE_DIR / "config" / "tira-application-config.yml"))
logging.info(f"Load settings from {cfgpath}.")
config = parse_config(cfgpath, default_value=None)
custom_settings.update(config)
Expand All @@ -34,20 +32,20 @@
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = custom_settings.get("django_secret", "not-so-secret")
SECRET_KEY = custom_settings["django_secret"]


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = custom_settings.get("debug", True)
ALLOWED_HOSTS = custom_settings.get("allowed_hosts", [])
DEBUG = custom_settings["debug"]
ALLOWED_HOSTS = custom_settings["allowed_hosts"]

TIRA_ROOT = Path(custom_settings.get("tira_root", BASE_DIR / "test" / "tira-root"))
if not TIRA_ROOT.is_dir():
logging.warning(f"{TIRA_ROOT} does not exists and will be created now.")

(TIRA_ROOT / "state").mkdir(parents=True, exist_ok=True)

DISRAPTOR_SECRET_FILE = Path(custom_settings.get("disraptor_secret_file", "/etc/discourse/client-api-key"))
DISRAPTOR_SECRET_FILE = Path(custom_settings["disraptor_secret_file"])
HOST_GRPC_PORT = custom_settings.get("host_grpc_port", "50051")
APPLICATION_GRPC_PORT = custom_settings.get("application_grpc_port", "50052")
GRPC_HOST = custom_settings.get("grpc_host", "local") # can be local or remote
Expand Down Expand Up @@ -299,24 +297,6 @@ def logger_config(log_dir: Path):
raise PermissionError(f"Can not write to {ld} in production mode.")


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]

# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

Expand All @@ -341,7 +321,10 @@ def logger_config(log_dir: Path):
REFERENCE_DATASETS: dict[str, str] = {}

CODE_SUBMISSION_REPOSITORY_NAMESPACE = "tira-io"
try:
DISRAPTOR_API_KEY = open(DISRAPTOR_SECRET_FILE, "r").read().strip()
except Exception:
pass
if DISRAPTOR_SECRET_FILE.exists():
DISRAPTOR_API_KEY = DISRAPTOR_SECRET_FILE.read_text().strip()
elif DEBUG:
logging.warning(f"The Disraptor Secret File, {DISRAPTOR_SECRET_FILE}, could not be found.")

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High test

This expression logs
sensitive data (secret)
as clear text.
This expression logs
sensitive data (secret)
as clear text.
DISRAPTOR_API_KEY = ""
else:
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), str(DISRAPTOR_SECRET_FILE))
4 changes: 2 additions & 2 deletions documentation/organizers/deployment/application.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ If you set up Discourse with `Disraptor <https://www.disraptor.org>`_, this will
make TIRA work with it.

(1) Since TIRA has a legacy and a Disraptor :code:`deployment` mode first change that to :code:`disraptor` in the
development config file :code:`application/config/tira-application-config.dev.yml`.
development config file :code:`application/config/tira-application-config.yml`.
(2) When you set up Disraptor you set a :code:`Disraptor App Secret Key` that allows Disraptor to communicate with your
web application. Since TIRA has to evaluate that this secret is correct we have to supply it to it. TIRA expects
that secret in an environment variable called :code:`DISRAPTOR_APP_SECRET_KEY`, so before starting your development
Expand Down Expand Up @@ -171,7 +171,7 @@ Setup on MacOS (Monterey/M1)
.. code:: bash
brew install [email protected] pipenv pyvenv mariadb uwsgi
(2) Inside :code:`tira/application/config/tira-application-config.dev.yml` change :code:`tira_root` to the model you
(2) Inside :code:`tira/application/config/tira-application-config.yml` change :code:`tira_root` to the model you
want to use.
(3) From within :code:`tira/application` execute the makefile at least once. This copies the config and runs
:code:`manage.py index_model` once.
Expand Down
Empty file removed model/.gitignore
Empty file.
Empty file removed model/requirements.txt
Empty file.
11 changes: 4 additions & 7 deletions pipelines/src/django_tira_git/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,19 @@
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = custom_settings.get("django_secret", "not-so-secret")
SECRET_KEY = custom_settings["django_secret"]


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = custom_settings.get("debug", True)
ALLOWED_HOSTS = custom_settings.get("allowed_hosts", [])
DEBUG = custom_settings["debug"]
ALLOWED_HOSTS = custom_settings["allowed_hosts"]

TIRA_ROOT = Path(
custom_settings.get("tira_root", BASE_DIR.parents[1] / "tira-model" / "src")
)
TIRA_ROOT = Path(custom_settings["tira_root"])
if not TIRA_ROOT.is_dir():
raise FileNotFoundError(
f"TIRA_ROOT must point to an existing tira model but points to {TIRA_ROOT} instead."
)

DEPLOYMENT = custom_settings.get("deployment", "disraptor")
DISRAPTOR_SECRET_FILE = Path(
custom_settings.get("disraptor_secret_file", "/etc/discourse/client-api-key")
)
Expand Down

0 comments on commit 74ce9f7

Please sign in to comment.