diff --git a/README.md b/README.md index 4b1e0dc..4e86aa1 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ Coulobre Panel is an open-source web hosting control panel for hosting providers and resellers. The project is in early development. +The development panel lets an administrator register and find hosting accounts. +See [Local development](docs/development.md) to run it. + ## Planned scope - Self-hosted installation on a single Ubuntu server, with Debian support planned. diff --git a/docs/development.md b/docs/development.md new file mode 100644 index 0000000..a2be3d2 --- /dev/null +++ b/docs/development.md @@ -0,0 +1,45 @@ +# Local development + +Use Python 3.12 or later. From the repository root, install the panel and its +development tools in a virtual environment. + +```sh +python3 -m venv .venv +. .venv/bin/activate +python -m pip install -e '.[dev]' +export COULOBRE_SECRET_KEY="$(python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())')" +export COULOBRE_DEBUG=1 +python manage.py migrate +python manage.py createsuperuser +python manage.py runserver +``` + +Open and sign in with the administrator account. +Under **Hosting accounts**, select **Add hosting account** and enter a name +and a primary domain such as `example.org`. The account is stored in the panel +database. Applying the account to Apache and the other hosting services is +planned for a later development step. + +The local database defaults to `db.sqlite3`. Set `COULOBRE_SQLITE_PATH` to use a +different file. Keep `COULOBRE_DEBUG` for local development. + +## MariaDB + +Create a dedicated MariaDB database and application user. Install the MariaDB or +MySQL client headers, Python headers, a C compiler, and `pkg-config`, then add +the database driver with `python -m pip install -e '.[dev,mariadb]'`. + +Set `COULOBRE_DB_NAME`, `COULOBRE_DB_USER`, and `COULOBRE_DB_PASSWORD` for your +database and rerun `python manage.py migrate`. Set `COULOBRE_DB_HOST` and +`COULOBRE_DB_PORT` if the database uses a different address from `127.0.0.1:3306`. + +## Checks + +```sh +ruff check . +ruff format --check . +python manage.py check +python manage.py makemigrations --check --dry-run +python manage.py test coulobre +git diff --check +``` diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..6c78743 --- /dev/null +++ b/manage.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 +import os +import sys + +from django.core.management import execute_from_command_line + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "coulobre.settings") + execute_from_command_line(sys.argv) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..b65b2cd --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,25 @@ +[build-system] +requires = ["setuptools>=77"] +build-backend = "setuptools.build_meta" + +[project] +name = "coulobre-panel" +version = "0.1.0.dev0" +description = "A web hosting control panel for hosting providers and resellers." +readme = "README.md" +requires-python = ">=3.12" +license = "Apache-2.0" +dependencies = [ + "Django>=5.2.8,<5.3", + "idna>=3,<4", +] + +[project.optional-dependencies] +dev = ["ruff>=0.11,<1"] +mariadb = ["mysqlclient>=2.2.1,<3"] + +[tool.ruff] +target-version = "py312" + +[tool.ruff.lint] +select = ["E4", "E7", "E9", "F", "I"] diff --git a/src/coulobre/__init__.py b/src/coulobre/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/coulobre/hosting/__init__.py b/src/coulobre/hosting/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/coulobre/hosting/admin.py b/src/coulobre/hosting/admin.py new file mode 100644 index 0000000..2e2328b --- /dev/null +++ b/src/coulobre/hosting/admin.py @@ -0,0 +1,10 @@ +from django.contrib import admin + +from .models import HostingAccount + + +@admin.register(HostingAccount) +class HostingAccountAdmin(admin.ModelAdmin): + list_display = ("name", "primary_domain", "created_at") + search_fields = ("name", "primary_domain") + ordering = ("primary_domain",) diff --git a/src/coulobre/hosting/fields.py b/src/coulobre/hosting/fields.py new file mode 100644 index 0000000..7b63e5f --- /dev/null +++ b/src/coulobre/hosting/fields.py @@ -0,0 +1,24 @@ +import idna +from django.core.exceptions import ValidationError +from django.core.validators import DomainNameValidator +from django.db import models + +validate_domain = DomainNameValidator(accept_idna=False) + + +class DomainNameField(models.CharField): + """Store public hostnames in their canonical DNS representation.""" + + def to_python(self, value): + value = super().to_python(value) + if value in self.empty_values: + return value + + try: + domain = idna.encode(value.strip(), uts46=True).decode("ascii") + except idna.IDNAError as error: + raise ValidationError(validate_domain.message, code="invalid") from error + + domain = domain.removesuffix(".") + validate_domain(domain) + return domain diff --git a/src/coulobre/hosting/migrations/0001_initial.py b/src/coulobre/hosting/migrations/0001_initial.py new file mode 100644 index 0000000..e0550d7 --- /dev/null +++ b/src/coulobre/hosting/migrations/0001_initial.py @@ -0,0 +1,36 @@ +# Generated by Django 5.2.19 on 2026-11-19 20:03 + +from django.db import migrations, models + +import coulobre.hosting.fields + + +class Migration(migrations.Migration): + initial = True + + dependencies = [] + + operations = [ + migrations.CreateModel( + name="HostingAccount", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=120)), + ( + "primary_domain", + coulobre.hosting.fields.DomainNameField( + max_length=253, unique=True + ), + ), + ("created_at", models.DateTimeField(auto_now_add=True)), + ], + ), + ] diff --git a/src/coulobre/hosting/migrations/__init__.py b/src/coulobre/hosting/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py new file mode 100644 index 0000000..a8390cc --- /dev/null +++ b/src/coulobre/hosting/models.py @@ -0,0 +1,12 @@ +from django.db import models + +from .fields import DomainNameField + + +class HostingAccount(models.Model): + name = models.CharField(max_length=120) + primary_domain = DomainNameField(max_length=253, unique=True) + created_at = models.DateTimeField(auto_now_add=True) + + def __str__(self): + return str(self.primary_domain) diff --git a/src/coulobre/hosting/tests.py b/src/coulobre/hosting/tests.py new file mode 100644 index 0000000..b898a5f --- /dev/null +++ b/src/coulobre/hosting/tests.py @@ -0,0 +1,67 @@ +from django.contrib.auth import get_user_model +from django.test import TestCase +from django.urls import reverse + +from .models import HostingAccount + + +class HostingAccountAdminTests(TestCase): + @classmethod + def setUpTestData(cls): + users = get_user_model().objects + cls.operator = users.create_superuser("operator") + cls.staff = users.create_user("staff", is_staff=True) + + def setUp(self): + self.add_url = reverse("admin:hosting_hostingaccount_add") + self.client.force_login(self.operator) + + def test_register_and_find_an_account(self): + response = self.client.post( + self.add_url, + {"name": "Example hosting", "primary_domain": "BÜCHER.example."}, + follow=True, + ) + + self.assertContains(response, "Example hosting") + self.assertContains(response, "xn--bcher-kva.example") + self.assertEqual( + HostingAccount.objects.get().primary_domain, "xn--bcher-kva.example" + ) + + list_url = reverse("admin:hosting_hostingaccount_changelist") + results = self.client.get(list_url, {"q": "xn--bcher"}) + self.assertContains(results, "Example hosting") + + def test_equivalent_domain_is_rejected(self): + HostingAccount.objects.create( + name="Existing account", primary_domain="bücher.example" + ) + response = self.client.post( + self.add_url, + {"name": "Duplicate account", "primary_domain": "XN--BCHER-KVA.Example."}, + ) + + form = response.context["adminform"].form + self.assertEqual(form.errors.as_data()["primary_domain"][0].code, "unique") + self.assertEqual(HostingAccount.objects.count(), 1) + + def test_invalid_domain_is_rejected(self): + response = self.client.post( + self.add_url, + {"name": "Invalid account", "primary_domain": "https://example.org"}, + ) + + form = response.context["adminform"].form + self.assertEqual(form.errors.as_data()["primary_domain"][0].code, "invalid") + self.assertFalse(HostingAccount.objects.exists()) + + def test_staff_without_add_permission_cannot_register_an_account(self): + self.client.force_login(self.staff) + response = self.client.post( + self.add_url, + {"name": "Example hosting", "primary_domain": "example.org"}, + ) + + self.assertEqual(response.status_code, 403) + self.assertFalse(HostingAccount.objects.exists()) diff --git a/src/coulobre/settings.py b/src/coulobre/settings.py new file mode 100644 index 0000000..7f2bff3 --- /dev/null +++ b/src/coulobre/settings.py @@ -0,0 +1,87 @@ +import os +from pathlib import Path + +from django.core.exceptions import ImproperlyConfigured + + +def required_env(name): + value = os.environ.get(name) + if not value: + raise ImproperlyConfigured(f"Set the {name} environment variable.") + return value + + +SECRET_KEY = required_env("COULOBRE_SECRET_KEY") +DEBUG = os.environ.get("COULOBRE_DEBUG") == "1" +ALLOWED_HOSTS = os.environ.get( + "COULOBRE_ALLOWED_HOSTS", "localhost,127.0.0.1,[::1]" +).split(",") + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "coulobre.hosting", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "coulobre.urls" +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +database_name = os.environ.get("COULOBRE_DB_NAME") +if database_name: + DATABASES = { + "default": { + "ENGINE": "django.db.backends.mysql", + "NAME": database_name, + "USER": required_env("COULOBRE_DB_USER"), + "PASSWORD": os.environ.get("COULOBRE_DB_PASSWORD", ""), + "HOST": os.environ.get("COULOBRE_DB_HOST", "127.0.0.1"), + "PORT": os.environ.get("COULOBRE_DB_PORT", "3306"), + "OPTIONS": {"charset": "utf8mb4"}, + }, + } +else: + DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": Path(os.environ.get("COULOBRE_SQLITE_PATH", "db.sqlite3")), + }, + } + +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"}, +] + +TIME_ZONE = "UTC" +STATIC_URL = "/static/" +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/src/coulobre/urls.py b/src/coulobre/urls.py new file mode 100644 index 0000000..979a96d --- /dev/null +++ b/src/coulobre/urls.py @@ -0,0 +1,12 @@ +from django.contrib import admin +from django.urls import path +from django.views.generic import RedirectView + +admin.site.site_header = "Coulobre Panel" +admin.site.site_title = "Coulobre Panel" +admin.site.index_title = "Administration" + +urlpatterns = [ + path("", RedirectView.as_view(pattern_name="admin:index")), + path("admin/", admin.site.urls), +]