-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathchecks.py
More file actions
313 lines (251 loc) · 9.62 KB
/
Copy pathchecks.py
File metadata and controls
313 lines (251 loc) · 9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python
"""
Quick sanity checks - run before deploying.
No frameworks, no mocking, just real code paths.
Usage:
uv run python checks.py
uv run python checks.py -v # verbose
uv run python checks.py --config # configuration only, no service connections
uv run python checks.py --config --billing # include payment settings
"""
import argparse
import importlib.util
import os
import sys
from pathlib import Path
VERBOSE = "-v" in sys.argv
PASSED = 0
FAILED = 0
def check(name):
"""Decorator to register a check"""
def decorator(f):
def wrapper(app):
global PASSED, FAILED
try:
f(app)
PASSED += 1
print(f" \033[32m✓\033[0m {name}")
return True
except Exception as e:
FAILED += 1
print(f" \033[31m✗\033[0m {name}")
if VERBOSE:
print(f" → {e}")
return False
wrapper._check_name = name
return wrapper
return decorator
# =============================================================================
# CHECKS
# =============================================================================
@check("App boots without errors")
def check_app_boots(app):
assert app is not None
assert app.config["SECRET_KEY"]
@check("Database connection works")
def check_database(app):
from enferno.extensions import db
with app.app_context():
db.session.execute(db.text("SELECT 1"))
@check("User model loads")
def check_user_model(app):
from enferno.user.models import User
with app.app_context():
User.query.limit(1).all()
@check("Workspace model and relationships")
def check_workspace_model(app):
from enferno.user.models import Membership, Workspace
with app.app_context():
Workspace.query.limit(1).all()
Membership.query.limit(1).all()
@check("Workspace service imports")
def check_workspace_service(app):
from enferno.services.workspace import (
WorkspaceScoped,
WorkspaceService,
require_workspace_access,
workspace_query,
)
assert callable(require_workspace_access)
assert callable(workspace_query)
assert hasattr(WorkspaceService, "create_workspace")
assert hasattr(WorkspaceScoped, "for_current_workspace")
@check("Billing service imports")
def check_billing_service(app):
from enferno.services.billing import HostedBilling, requires_pro_plan
assert callable(requires_pro_plan)
assert hasattr(HostedBilling, "create_upgrade_session")
assert hasattr(HostedBilling, "create_portal_session")
assert hasattr(HostedBilling, "handle_successful_payment")
@check("BillingEvent model exists")
def check_billing_event_model(app):
from enferno.user.models import BillingEvent
with app.app_context():
assert hasattr(BillingEvent, "provider")
BillingEvent.query.limit(1).all()
@check("Auth decorators work")
def check_auth_decorators(app):
from enferno.services.auth import require_superadmin, require_superadmin_api
assert callable(require_superadmin)
assert callable(require_superadmin_api)
@check("All blueprints register")
def check_blueprints(app):
blueprints = list(app.blueprints.keys())
required = ["users", "public", "portal", "webhooks"]
for bp in required:
assert bp in blueprints, f"Missing blueprint: {bp}"
@check("Critical routes exist")
def check_routes(app):
rules = [r.rule for r in app.url_map.iter_rules()]
critical_routes = [
"/",
"/login",
"/dashboard/",
]
for route in critical_routes:
assert route in rules, f"Missing route: {route}"
# Billing webhook - one of these must exist based on provider
webhook_routes = ["/stripe/webhook", "/chargebee/webhook"]
assert any(r in rules for r in webhook_routes), "Missing billing webhook route"
@check("Security config is sane")
def check_security_config(app):
# Password security
assert app.config["SECURITY_PASSWORD_LENGTH_MIN"] >= 8
# Session security
assert app.config["SESSION_COOKIE_HTTPONLY"] is True
# Multi-tenant safety
assert app.config["SECURITY_EMAIL_CHANGEABLE"] is False
# =============================================================================
# RUNNER
# =============================================================================
def check_configuration(billing=False):
"""Check local settings without booting the app or contacting services."""
from dotenv import dotenv_values, load_dotenv
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
root = Path(__file__).resolve().parent
load_dotenv(root / ".env")
sample = dotenv_values(root / ".env-sample")
errors = []
def require(name, value, action):
if (
not value
or not value.strip()
or "your_" in value
or value == sample.get(name)
):
errors.append(f"{name}: missing or placeholder value. {action}")
for name in ("SECRET_KEY", "SECURITY_PASSWORD_SALT", "SECURITY_TOTP_SECRETS"):
require(
name,
os.environ.get(name),
"Generate secure values with ./setup.sh for a new install.",
)
redis_url = os.environ.get("REDIS_URL") or os.environ.get("REDIS_SESSION")
broker = os.environ.get("CELERY_BROKER_URL")
backend = os.environ.get("CELERY_RESULT_BACKEND")
if backend and not broker:
errors.append("CELERY_BROKER_URL: required when CELERY_RESULT_BACKEND is set.")
for package, needed in (
("redis", redis_url or broker or backend),
("celery", broker or backend),
):
if needed and importlib.util.find_spec(package) is None:
errors.append(
f"{package}: configured but not installed. Run uv sync --extra dev --extra full."
)
# Config can fail during import; validate its startup requirements first.
if not errors:
try:
from enferno.settings import Config
except (ValueError, TypeError):
errors.append(
"Config could not load. Check REDIS_URL/REDIS_SESSION and Redis URL options."
)
else:
try:
# Engine construction validates the URL and driver without connecting.
create_engine(Config.SQLALCHEMY_DATABASE_URI).dispose()
except (SQLAlchemyError, ValueError, ImportError, TypeError):
errors.append(
"SQLALCHEMY_DATABASE_URI: invalid database URL or unavailable dialect/driver."
)
for provider in ("GOOGLE", "GITHUB"):
if getattr(Config, f"{provider}_AUTH_ENABLED"):
for field in ("CLIENT_ID", "CLIENT_SECRET"):
name = f"{provider}_OAUTH_{field}"
require(
name,
getattr(Config, name),
f"Set it or disable {provider}_AUTH_ENABLED.",
)
provider = Config.BILLING_PROVIDER
if provider not in {"stripe", "chargebee"}:
errors.append("BILLING_PROVIDER: choose stripe or chargebee.")
elif billing:
names = (
(
"STRIPE_SECRET_KEY",
"STRIPE_PRO_PRICE_ID",
"STRIPE_WEBHOOK_SECRET",
)
if provider == "stripe"
else (
"CHARGEBEE_SITE",
"CHARGEBEE_API_KEY",
"CHARGEBEE_PRO_ITEM_PRICE_ID",
"CHARGEBEE_WEBHOOK_USERNAME",
"CHARGEBEE_WEBHOOK_PASSWORD",
)
)
for name in names:
require(
name,
getattr(Config, name),
f"Configure {provider} before using billing.",
)
else:
print("Billing: not checked (use --config --billing).")
for error in errors:
print(f"FAIL {error}")
if errors:
print(f"Configuration checks failed: {len(errors)} issue(s).")
return 1
print(
"Configuration checks passed. Service connectivity and credentials were not verified."
)
return 0
def run_checks():
from enferno.app import create_app
print("\n\033[1mRunning checks...\033[0m\n")
app = create_app()
checks = [v for v in globals().values() if hasattr(v, "_check_name")]
for check_fn in checks:
check_fn(app)
print()
if FAILED == 0:
print(f"\033[32m\033[1m✓ All {PASSED} checks passed\033[0m\n")
return 0
else:
print(f"\033[31m\033[1m✗ {FAILED}/{PASSED + FAILED} checks failed\033[0m\n")
return 1
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Check ReadyKit configuration or application health."
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Show smoke-check errors"
)
parser.add_argument(
"--config", action="store_true", help="Check settings without starting the app"
)
parser.add_argument(
"--billing",
action="store_true",
help="Also require billing settings (with --config)",
)
args = parser.parse_args()
if args.billing and not args.config:
parser.error("--billing requires --config")
VERBOSE = args.verbose
sys.exit(check_configuration(args.billing) if args.config else run_checks())