Skip to content

Commit

Permalink
Configure for PythonAnywhere deployment with SQLite and optimized set…
Browse files Browse the repository at this point in the history
…tings
  • Loading branch information
Anon23261 committed Nov 15, 2024
1 parent 3470d08 commit ee2ab72
Show file tree
Hide file tree
Showing 14 changed files with 813 additions and 93 deletions.
17 changes: 17 additions & 0 deletions create_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import sys

def create_log_dirs():
log_dirs = [
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logs'),
]

for directory in log_dirs:
try:
os.makedirs(directory, exist_ok=True)
print(f"Created directory: {directory}")
except Exception as e:
print(f"Error creating {directory}: {e}", file=sys.stderr)

if __name__ == '__main__':
create_log_dirs()
3 changes: 3 additions & 0 deletions ghostsec/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Core app initialization
"""
13 changes: 13 additions & 0 deletions ghostsec/core/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.apps import AppConfig

class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'ghostsec.core'
label = 'core'
verbose_name = 'GhostSec Core'

def ready(self):
try:
import ghostsec.core.signals # noqa
except ImportError:
pass
197 changes: 147 additions & 50 deletions ghostsec/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@

import os
from pathlib import Path
from datetime import timedelta

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY', 'django-insecure-your-secret-key-here')
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')

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

ALLOWED_HOSTS = []

# Custom user model
AUTH_USER_MODEL = 'users.User'

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
Expand All @@ -24,11 +28,22 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ghostsec.apps.GhostSecConfig',

# Third-party apps
'rest_framework',
'corsheaders',
'crispy_forms',
'crispy_bootstrap4',
'django_extensions',
'debug_toolbar',
'axes',
'two_factor',

# Local apps
'ghostsec.core.apps.CoreConfig',
'ghostsec.ctf.apps.CTFConfig',
'ghostsec.users.apps.UsersConfig',
'ghostsec.forum.apps.ForumConfig',
]

MIDDLEWARE = [
Expand All @@ -40,6 +55,9 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'axes.middleware.AxesMiddleware',
'csp.middleware.CSPMiddleware',
]

ROOT_URLCONF = 'ghostsec.urls'
Expand All @@ -61,67 +79,146 @@
]

WSGI_APPLICATION = 'ghostsec.wsgi.application'
ASGI_APPLICATION = 'ghostsec.asgi.application'

# Password validation
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
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Security Settings
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
X_FRAME_OPTIONS = 'DENY'
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True

# Static files (CSS, JavaScript, Images)
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
# Content Security Policy
CSP_DEFAULT_SRC = ("'self'",)
CSP_STYLE_SRC = ("'self'", "'unsafe-inline'")
CSP_SCRIPT_SRC = ("'self'", "'unsafe-inline'", "'unsafe-eval'")
CSP_IMG_SRC = ("'self'", "data:", "https:")
CSP_FONT_SRC = ("'self'", "https:", "data:")

# Authentication
AUTHENTICATION_BACKENDS = [
'axes.backends.AxesStandaloneBackend',
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
]

# Default primary key field type
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Authentication Settings
AUTH_PASSWORD_VALIDATORS = [
{'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'},
{'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {'min_length': 12}},
{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'},
{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'},
]

# Crispy Forms
CRISPY_TEMPLATE_PACK = 'bootstrap4'
# Django-Axes Configuration
AXES_FAILURE_LIMIT = 5 # Number of login attempts before lockout
AXES_COOLOFF_TIME = 1 # Hours before allowing login attempts again
AXES_LOCKOUT_PARAMETERS = ['username', 'ip_address'] # Lock out based on both username and IP

# Rest Framework
# REST Framework Settings
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
},
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
],
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
],
}

# CORS settings
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOWED_ORIGINS = [
"http://localhost:8000",
"http://127.0.0.1:8000",
]
# Cache Configuration for Development
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}

# Security Settings
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
X_FRAME_OPTIONS = 'DENY'
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
# Cache Middleware Settings
CACHE_MIDDLEWARE_ALIAS = 'default'
CACHE_MIDDLEWARE_SECONDS = 0 # No caching in development
CACHE_MIDDLEWARE_KEY_PREFIX = 'ghostsec_dev'

# Email Configuration
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = os.environ.get('EMAIL_HOST', 'smtp.gmail.com')
EMAIL_PORT = int(os.environ.get('EMAIL_PORT', 587))
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')

# Logging Configuration
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
'style': '{',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
'file': {
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(BASE_DIR, 'logs', 'ghostsec.log'),
'maxBytes': 1024 * 1024 * 5, # 5 MB
'backupCount': 5,
'formatter': 'verbose',
},
},
'loggers': {
'django': {
'handlers': ['console', 'file'],
'level': 'INFO',
'propagate': True,
},
'ghostsec': {
'handlers': ['console', 'file'],
'level': 'INFO',
'propagate': True,
},
'axes': {
'handlers': ['console', 'file'],
'level': 'WARNING',
'propagate': True,
},
},
}

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

# Media files
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Crispy Forms
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap4"
CRISPY_TEMPLATE_PACK = "bootstrap4"

# Debug Toolbar Configuration
INTERNAL_IPS = ['127.0.0.1']
48 changes: 48 additions & 0 deletions ghostsec/settings/development.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Development settings for GhostSec project.
"""
from .base import *

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-development-key-do-not-use-in-production'

ALLOWED_HOSTS = ['*']

# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}

# Cache
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}

# Email
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Static files
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'

# Media files
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

# Debug toolbar - only add if not already in INSTALLED_APPS
if 'debug_toolbar' not in INSTALLED_APPS:
INSTALLED_APPS += ['debug_toolbar']
MIDDLEWARE += ['debug_toolbar.middleware.DebugToolbarMiddleware']
INTERNAL_IPS = ['127.0.0.1']

# Disable password validation in development
AUTH_PASSWORD_VALIDATORS = []
Loading

0 comments on commit ee2ab72

Please sign in to comment.