Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated config file to .secrets #317

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,8 @@ dmypy.json

# --- USER-ADDED IGNORES --- #

# The settings file.
config/settings.py

# The server configuration file.
server.conf
.secrets

# The migrations folder.
# bco_api/api/migrations/
Expand Down
14 changes: 14 additions & 0 deletions .secrets.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[DJANGO_KEYS]
SECRET_KEY=
ANON_KEY=

[SERVER]
PRODUCTION=
SERVER_VERSION=
HOSTNAME=
HUMAN_READABLE_HOSTNAME=
PUBLIC_HOSTNAME=
SERVER_URL=
#DATABASE=
DATABASE=
EMAIL_BACKEND=
19 changes: 4 additions & 15 deletions authentication/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from django.contrib.auth.models import User, Permission
from authentication.models import Authentication, NewUser
from rest_framework.authtoken.models import Token

from prefix.selectors import get_user_prefixes
from biocompute.selectors import get_authorized_bcos

def get_anon()-> User:
"""Get AnonymosUser
Expand Down Expand Up @@ -73,27 +74,15 @@ def get_user_info(user: User) -> dict:
other_info = {
"permissions": {},
"account_creation": "",
"account_expiration": "",
}
user_perms = {"user": [], "groups": []}

for permission in user.user_permissions.all():
if permission.name not in user_perms["user"]:
user_perms["user"].append(permission.name)

for group in user.groups.all():
if group.name not in user_perms["groups"]:
user_perms["groups"].append(group.name)
for permission in Permission.objects.filter(group=group):
if permission.name not in user_perms["user"]:
user_perms["user"].append(permission.name)
user_perms = {"prefixes": get_user_prefixes(user), "BCOs": get_authorized_bcos(user)}

other_info["permissions"] = user_perms

other_info["account_creation"] = user.date_joined

return {
"hostname": settings.ALLOWED_HOSTS[0],
"hostname": settings.HOSTNAME,
"human_readable_hostname": settings.HUMAN_READABLE_HOSTNAME,
"public_hostname": settings.PUBLIC_HOSTNAME,
"token": token.key,
Expand Down
2 changes: 1 addition & 1 deletion biocompute/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
),
"authorized_users": openapi.Schema(
type=openapi.TYPE_ARRAY,
description="Users which can access the BCO draft.",
description="Users that can access the BCO draft.",
items=openapi.Schema(type=openapi.TYPE_STRING, example="tester")
),
"contents": openapi.Schema(
Expand Down
2 changes: 1 addition & 1 deletion biocompute/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Bco(models.Model):
owner = ForeignKey(User)
String representing the django.contrib.auth.models.User that 'owns' the object
authorized_users: ManyToManyField(User)
String representing the User that has access to the object
String representing the Users that have access to the object
prefix: str
Prefix for the BCO
state:str
Expand Down
22 changes: 22 additions & 0 deletions biocompute/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from datetime import datetime
from django.conf import settings
from django.contrib.auth. models import User
from django.db.models import Q
from prefix.selectors import (
user_can_view_prefix,
user_can_modify_prefix,
Expand Down Expand Up @@ -225,6 +226,27 @@ def retrieve_bco(bco_accession:str, user:User, bco_version:str=None) -> bool:

return bco_instance

def get_authorized_bcos(user: User):
"""
Retrieve all BioCompute Objects (BCOs) that a specific user is authorized
to access, excluding those in 'DELETE' state.

Parameters:
- user (User):
The Django User instance for whom to retrieve authorized BCOs.

Returns:
- QuerySet:
A Django QuerySet containing the BCOs the user is authorized to access.
"""

bcos = Bco.objects.filter(
Q(owner=user) | Q(authorized_users=user)
).exclude(state='DELETE').values_list('object_id', flat=True).distinct()


return bcos

def object_id_deconstructor(object_id=str) -> list:
"""
Deconstructs a BioCompute Object (BCO) identifier into its constituent
Expand Down
103 changes: 17 additions & 86 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,80 +9,30 @@

# --- SECURITY SETTINGS --- #
# Load the server config file.
server_config = configparser.ConfigParser()
server_config.read(BASE_DIR + "/server.conf")

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# Is this a production server?
PRODUCTION = server_config["PRODUCTION"]["production"]
secrets = configparser.ConfigParser()
secrets.read(BASE_DIR + "/.secrets")
PRODUCTION = secrets["SERVER"]["PRODUCTION"]
DEBUG = PRODUCTION

# Set the anonymous user's key.
ANON_KEY = server_config["KEYS"]["anon"]
ANON_KEY = secrets["DJANGO_KEYS"]["ANON_KEY"]

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "$vz@#@^q(od&$rf&*6^z!m5nh6qw2*cq*j6fha#^h9(r7$xqy4"
SECRET_KEY = secrets["DJANGO_KEYS"]["SECRET_KEY"]

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = PRODUCTION

# The publicly accessible hostname.
HOSTNAME = secrets["SERVER"]["HOSTNAME"]
# The human-readable hostname.
HUMAN_READABLE_HOSTNAME = server_config["HRHOSTNAME"]["hrnames"]

if server_config["GROUP_PREFIX"]["allow_all_creation"] == "True":
GROUP = True
PREFIX = True
elif server_config["GROUP_PREFIX"]["allow_group_creation"] == "True":
GROUP = True
elif server_config["GROUP_PREFIX"]["allow_prefix_creation"] == "True":
PREFIX = True

HUMAN_READABLE_HOSTNAME = secrets["SERVER"]["HUMAN_READABLE_HOSTNAME"]
# The publicly accessible hostname.
if server_config["PRODUCTION"]["production"] == "True":
PUBLIC_HOSTNAME = server_config["PUBLICHOSTNAME"]["prod_name"]
elif server_config["PRODUCTION"]["production"] == "False":
PUBLIC_HOSTNAME = server_config["PUBLICHOSTNAME"]["name"]
PUBLIC_HOSTNAME = secrets["SERVER"]["PUBLIC_HOSTNAME"]
# import pdb; pdb.set_trace()

# Source: https://dzone.com/articles/how-to-fix-django-cors-error

# Check for open (public) access to the API.
if server_config["REQUESTS_FROM"]["public"].strip() == "false":

# Process the requester groups.

# configparser automatically strips white space off the
# ends of arguments.
requesters = [
server_config["REQUESTS_FROM"][i].strip()
for i in server_config["REQUESTS_FROM"]
]
requesters.remove("false")
requesters = [i.split(",") for i in requesters]

# Flatten the list.
# Source: https://stackabuse.com/python-how-to-flatten-list-of-lists/
flattened = [item.strip() for sublist in requesters for item in sublist]

if server_config["PRODUCTION"]["production"] == "True":
ALLOWED_HOSTS = [
i.strip() for i in server_config["HOSTNAMES"]["prod_names"].split(",")
]
elif server_config["PRODUCTION"]["production"] == "False":
ALLOWED_HOSTS = [
i.strip() for i in server_config["HOSTNAMES"]["names"].split(",")
]

CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = tuple(flattened)

elif server_config["REQUESTS_FROM"]["public"].strip() == "true":
if server_config["PRODUCTION"]["production"] == "True":
ALLOWED_HOSTS = [server_config["HOSTNAMES"]["prod_names"].split(",")[0], "*"]
CORS_ORIGIN_ALLOW_ALL = True
elif server_config["PRODUCTION"]["production"] == "False":
ALLOWED_HOSTS = [server_config["HOSTNAMES"]["names"].split(",")[0], "*"]
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = ["*"]

# Use the REST framework
REST_FRAMEWORK = {
Expand All @@ -94,8 +44,6 @@
],
"DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"],
"DEFAULT_SCHEMA_CLASS": "rest_framework.schemas.coreapi.AutoSchema",


}

JWT_AUTH = {
Expand Down Expand Up @@ -197,7 +145,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": server_config["DATABASES"]["path"],
"NAME": secrets["SERVER"]["DATABASE"],
}
}

Expand All @@ -219,8 +167,8 @@
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = "/api/static/"
# STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = "/var/www/bcoeditor/bco_api/bco_api/static/"
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
# STATIC_ROOT = "/var/www/bcoeditor/bco_api/bco_api/static/"

# ----- CUSTOM VARIABLES AND METHODS ----- #
# Load request and validation templates (definitions).
Expand All @@ -229,26 +177,9 @@
# First, the request definitions.

# Make the object naming accessible as a dictionary.
OBJECT_NAMING = {}

if server_config["PRODUCTION"]["production"] == "True":

for i in server_config["OBJECT_NAMING"]:
if i.split("_")[0] == "prod":

# Strip out the production flag.
STRIPPED = "_".join(i.split("_")[1:])

OBJECT_NAMING[STRIPPED] = server_config["OBJECT_NAMING"][i]

elif server_config["PRODUCTION"]["production"] == "False":

for i in server_config["OBJECT_NAMING"]:
if i.split("_")[0] != "prod":
OBJECT_NAMING[i] = server_config["OBJECT_NAMING"][i]

# emailing notifications
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
EMAIL_BACKEND = secrets["SERVER"]["EMAIL_BACKEND"]
EMAIL_HOST = "localhost"
EMAIL_PORT = 25
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
74 changes: 0 additions & 74 deletions server.conf

This file was deleted.

Loading
Loading