Files
ServerKit/backend/app/bootstrap/persistence.py
T
6e096fa286 Add real preview deployments and strengthen core boundaries (#184)
* fix(security): honour the failed-login time window (#180)

check_failed_logins computed a cutoff from since_hours and never used
it, so the Security -> Events "Failed login attempts (24h)" card and
its alert counted every failure still in /var/log/auth.log or
/var/log/secure: up to a week of SSH attempts under the usual weekly
log rotation.

Each matching line is now dated from its stamp, either RFC 3339
(rsyslog's high-precision format) or the traditional BSD syslog stamp
(no year, so a December line read in January is placed in the previous
year), and lines older than the cutoff are skipped. A line whose stamp
is not recognised is still counted rather than hidden. The log paths
move to SecurityService.AUTH_LOGS so the tests can point at a temp
file.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* refactor: separate backend startup phases and guard ordering

* refactor: route file operations through capability-checked adapters

* chore: bump version to 1.11.25 [skip ci]

* refactor: split File Manager state and views with regression guards

* test: enforce core boundaries and document extension rules

* test: cover File Manager tree races and listing failures

* fix: preserve File Manager deep links on initial mount

* refactor: keep File Manager download operations in the action hook

* fix: start agent maintenance only in the guarded runtime phase

* test: capture nginx rendering contract before extraction

* refactor: separate nginx configuration rendering from host operations

* test: enforce pure nginx configuration boundaries

* fix: require provisioned runtime before advertising previews

* feat: expose value-free effective environment provenance through app access boundary

* feat: add effective environment inspector with browser regression checks

* feat: provision isolated generic previews with retryable resource cleanup

* feat: queue preview automation and preserve cleanup ownership through app deletion

* feat: add pinned preview deployment controls and queued lifecycle feedback

* fix: preserve container port semantics for previews of slot deployments

* fix: preserve search focus and ignore stale list responses

* feat: run approved assistant deployments through persistent recipe operations

* test: run search and assistant deployment regressions in CI

* fix: refresh preview API and controller boundary inventories

* docs: refresh controller migration count after preview extraction

* fix: bind file editor previews to their storage target

* chore: bump version to 1.11.26 [skip ci]

---------

Co-authored-by: Max Freedom Pollard <272618364+MaxFreedomPollard@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-10-24 15:31:39 -04:00

75 lines
3.7 KiB
Python

"""Database preparation and idempotent installation data upgrades."""
def prepare_persistence(app):
from app.services.migration_service import MigrationService
MigrationService.check_and_prepare(app)
# Initialize default settings and migrate legacy roles
from app.services.settings_service import SettingsService
SettingsService.initialize_defaults()
SettingsService.migrate_legacy_roles()
if not app.config.get('TESTING'):
# Size-aware retention defaults, once per install.
# Skipped under test: the result would depend on the test host's disk.
try:
from app.services import storage_profile_service
storage_profile_service.apply_to_existing()
except Exception as e:
app.logger.warning(f'Storage profile not applied: {e}')
# Encrypt any legacy plaintext provider secrets at rest (idempotent —
# DNS-provider api keys and storage credentials predate encryption).
try:
from app.services.dns_provider_service import DNSProviderService
from app.services.storage_provider_service import StorageProviderService
n_dns = DNSProviderService.encrypt_legacy_secrets()
n_store = StorageProviderService.encrypt_legacy_secrets()
n_settings = SettingsService.migrate_legacy_secrets()
# Migrate DNS zones with an inline Cloudflare token onto the canonical
# connection store (idempotent), so every zone resolves creds the same way.
from app.services.dns_zone_service import DNSZoneService
n_zones = DNSZoneService.link_legacy_zones()
# Fold a legacy single-row email relay config into the unified
# EmailProviderConnection table; idempotent, best-effort.
try:
from app.services.email_relay_service import EmailRelayService
EmailRelayService.migrate_legacy_config()
except Exception as _relay_exc: # never block boot on this
import logging as _logging
_logging.getLogger(__name__).warning(
f'Email relay legacy migration skipped: {_relay_exc}')
if n_dns or n_store or n_settings or n_zones:
import logging as _logging
_logging.getLogger(__name__).info(
f'Encrypted legacy secrets at rest: {n_dns} DNS provider(s), '
f'{n_store} storage field(s), '
f'{n_settings} system setting(s); linked {n_zones} DNS zone(s) '
f'to a connection')
except Exception as e:
import logging as _logging
_logging.getLogger(__name__).warning(f'Legacy secret encryption skipped: {e}')
# Record this boot's hardware and compare it to the last one.
# Boot is the right hook: a VPS resize requires a power-off, so the
# change and the restart are the same event — and the in-memory spec
# cache that used to be the only "before" value dies with the process.
# Skipped under TESTING: create_app() runs per test session, and a
# capture there would write rows and emit notifications into fixtures.
if not app.config.get('TESTING'):
try:
from app.services import host_snapshot_service
host_snapshot_service.record_snapshot()
except Exception as e:
import logging
logging.getLogger(__name__).warning(f'Host snapshot skipped: {e}')
# A panel with no account yet logs its one-time setup code, so the
# owner can read it from the service log (setup_code_service).
if not app.config.get('TESTING'):
try:
from app.services import setup_code_service
setup_code_service.ensure()
except Exception as e:
import logging
logging.getLogger(__name__).warning(f'Setup code unavailable: {e}')