Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 58c2b9d996 | |||
| 2e8bcb630d | |||
| 338711181b | |||
| 43487f17e7 | |||
| 40205bf5c7 | |||
| b95f1a5bb7 | |||
| 12f7eb722b | |||
| 217bba1b76 | |||
| 967e05ed74 | |||
| c20245b0ab | |||
| b9db0c552e | |||
| 05045bafa2 | |||
| 39f1b5de30 | |||
| b06de6fdd3 | |||
| 940d0af35e | |||
| d6d31aa2e3 | |||
| 76edfe7577 | |||
| d190029728 | |||
| b8307e7a9d | |||
| a2fdf091f5 | |||
| 1914e6f28e | |||
| 82cbce9615 | |||
| dbb779b013 | |||
| ca908ee967 | |||
| 73c697b6c5 | |||
| 3e2357380b | |||
| cc4a103bae | |||
| 53fb10fdf5 | |||
| 2df2ad18c9 | |||
| b81a0d2a6c | |||
| 1a19088cfe | |||
| 172f6e950f |
@@ -256,6 +256,56 @@ disk_monitor:
|
|||||||
operator: "<"
|
operator: "<"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### ZFS Monitor
|
||||||
|
|
||||||
|
ZFS pool health is checked automatically for every pool. A pool in any state
|
||||||
|
other than `ONLINE` (e.g. `DEGRADED`, `SUSPENDED`, `FAULTED`, `UNAVAIL`) raises
|
||||||
|
a **CRITICAL** alert by default — no configuration required.
|
||||||
|
|
||||||
|
The default threshold is equivalent to:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
zfs_monitor:
|
||||||
|
pools:
|
||||||
|
'*':
|
||||||
|
status:
|
||||||
|
warning: 1
|
||||||
|
critical: 2
|
||||||
|
operator: ">"
|
||||||
|
hysteresis: 0.0
|
||||||
|
display: "ZFS pool {pool_name} is {health}"
|
||||||
|
```
|
||||||
|
|
||||||
|
`'*'` matches every pool on the host. The notification message includes the pool
|
||||||
|
name and its current health string, e.g. `ZFS pool tank is DEGRADED`.
|
||||||
|
|
||||||
|
**Override for specific pools** — named pool entries take priority over `'*'`:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
zfs_monitor:
|
||||||
|
pools:
|
||||||
|
# Suppress health alerts for a scratch pool (not mission-critical)
|
||||||
|
scratch:
|
||||||
|
status:
|
||||||
|
enabled: false
|
||||||
|
|
||||||
|
# Capacity threshold for a specific pool
|
||||||
|
tank:
|
||||||
|
capacity:
|
||||||
|
warning: 75.0
|
||||||
|
critical: 90.0
|
||||||
|
operator: ">"
|
||||||
|
hysteresis: 0.05
|
||||||
|
```
|
||||||
|
|
||||||
|
**Alert state paths** follow the pattern `zfs_monitor.<pool_name>.status`,
|
||||||
|
so acknowledgements and silences target individual pools:
|
||||||
|
|
||||||
|
```
|
||||||
|
zfs_monitor.tank.status
|
||||||
|
zfs_monitor.backup.status
|
||||||
|
```
|
||||||
|
|
||||||
### Network Monitor
|
### Network Monitor
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
@@ -0,0 +1,781 @@
|
|||||||
|
# Gitea OAuth2 Authentication Implementation Plan
|
||||||
|
|
||||||
|
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||||
|
|
||||||
|
**Goal:** Add Gitea as an OAuth2 login provider that coexists with password auth, auto-provisioning new users on first login.
|
||||||
|
|
||||||
|
**Architecture:** A new `oauth.py` module owns all Gitea-specific logic (CSRF state, URL building, token exchange, user-info fetch). `users.py` gains one function to upsert an OAuth-sourced user. `http.py` gets two new route handlers and a small login-page change. No new dependencies — `aiohttp.ClientSession` is already used in the codebase.
|
||||||
|
|
||||||
|
**Tech Stack:** Python 3.12, aiohttp 3.x, pytest, pytest-asyncio
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## File Map
|
||||||
|
|
||||||
|
| Action | Path | Responsibility |
|
||||||
|
|--------|------|----------------|
|
||||||
|
| Modify | `hbd/server/config.py` | Add `"oauth": {}` default |
|
||||||
|
| Create | `hbd/server/oauth.py` | CSRF state, URL builder, token exchange, user-info fetch |
|
||||||
|
| Modify | `hbd/server/users.py` | Add `provision_oauth_user()` |
|
||||||
|
| Modify | `hbd/server/http.py` | Import oauth, two new routes, login page button |
|
||||||
|
| Create | `tests/test_oauth.py` | All new unit tests |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 1: Add config default and `is_enabled()`
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `hbd/server/config.py:34` (after the `"users"` line)
|
||||||
|
- Create: `hbd/server/oauth.py`
|
||||||
|
- Create: `tests/test_oauth.py`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write the failing test**
|
||||||
|
|
||||||
|
Create `tests/test_oauth.py`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pytest
|
||||||
|
from hbd.server import oauth
|
||||||
|
|
||||||
|
|
||||||
|
CFG_OFF = {}
|
||||||
|
CFG_ON = {
|
||||||
|
"oauth": {
|
||||||
|
"gitea": {
|
||||||
|
"url": "https://git.example.com",
|
||||||
|
"client_id": "cid",
|
||||||
|
"client_secret": "csec",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CFG_PARTIAL = {"oauth": {"gitea": {"url": "https://git.example.com"}}}
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_enabled_when_all_keys_present():
|
||||||
|
assert oauth.is_enabled(CFG_ON) is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_enabled_false_when_no_oauth_key():
|
||||||
|
assert oauth.is_enabled(CFG_OFF) is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_enabled_false_when_partial_config():
|
||||||
|
assert oauth.is_enabled(CFG_PARTIAL) is False
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run to confirm failure**
|
||||||
|
|
||||||
|
```
|
||||||
|
pytest tests/test_oauth.py -v
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: `ModuleNotFoundError: No module named 'hbd.server.oauth'`
|
||||||
|
|
||||||
|
- [ ] **Step 3: Add config default**
|
||||||
|
|
||||||
|
In `hbd/server/config.py`, add after the `"default_owner"` line (currently line 35):
|
||||||
|
|
||||||
|
```python
|
||||||
|
# OAuth2 providers
|
||||||
|
"oauth": {}, # oauth.gitea.{url,client_id,client_secret}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Create `hbd/server/oauth.py` with `is_enabled`**
|
||||||
|
|
||||||
|
```python
|
||||||
|
"""Gitea OAuth2 support.
|
||||||
|
|
||||||
|
Config shape (in ~/.hb.yaml):
|
||||||
|
|
||||||
|
oauth:
|
||||||
|
gitea:
|
||||||
|
url: https://git.example.com
|
||||||
|
client_id: <client-id>
|
||||||
|
client_secret: <client-secret>
|
||||||
|
|
||||||
|
Register a Gitea OAuth2 application at:
|
||||||
|
Gitea → Settings → Applications → OAuth2
|
||||||
|
Set the redirect URI to:
|
||||||
|
https://<hbd-host>/login/oauth/gitea/callback
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import secrets
|
||||||
|
import time
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
STATE_TTL = 600 # 10 minutes
|
||||||
|
|
||||||
|
# state_token -> expiry timestamp
|
||||||
|
_states: dict[str, float] = {}
|
||||||
|
|
||||||
|
|
||||||
|
class OAuthError(Exception):
|
||||||
|
"""Raised when the OAuth2 flow fails for any reason."""
|
||||||
|
|
||||||
|
|
||||||
|
def _gitea_cfg(config: dict) -> dict:
|
||||||
|
"""Return the gitea sub-dict or {} if absent/incomplete."""
|
||||||
|
return config.get("oauth", {}).get("gitea", {})
|
||||||
|
|
||||||
|
|
||||||
|
def is_enabled(config: dict) -> bool:
|
||||||
|
"""Return True when all three required Gitea OAuth keys are present."""
|
||||||
|
g = _gitea_cfg(config)
|
||||||
|
return bool(g.get("url") and g.get("client_id") and g.get("client_secret"))
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 5: Run to confirm tests pass**
|
||||||
|
|
||||||
|
```
|
||||||
|
pytest tests/test_oauth.py -v
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: 3 passed
|
||||||
|
|
||||||
|
- [ ] **Step 6: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add hbd/server/config.py hbd/server/oauth.py tests/test_oauth.py
|
||||||
|
git commit -m "feat: add oauth module skeleton and is_enabled()"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 2: CSRF state management
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `hbd/server/oauth.py` (add `make_state`, `validate_state`)
|
||||||
|
- Modify: `tests/test_oauth.py` (add state tests)
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write the failing tests**
|
||||||
|
|
||||||
|
Append to `tests/test_oauth.py`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import time as time_mod
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_state_returns_unique_tokens():
|
||||||
|
s1 = oauth.make_state()
|
||||||
|
s2 = oauth.make_state()
|
||||||
|
assert s1 != s2
|
||||||
|
assert len(s1) == 64 # 32 bytes hex
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_state_valid():
|
||||||
|
state = oauth.make_state()
|
||||||
|
assert oauth.validate_state(state) is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_state_consumed_on_use():
|
||||||
|
state = oauth.make_state()
|
||||||
|
oauth.validate_state(state)
|
||||||
|
assert oauth.validate_state(state) is False # replay rejected
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_state_unknown():
|
||||||
|
assert oauth.validate_state("notastate") is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_state_expired(monkeypatch):
|
||||||
|
state = oauth.make_state()
|
||||||
|
# Wind expiry into the past
|
||||||
|
monkeypatch.setitem(oauth._states, state, time_mod.time() - 1)
|
||||||
|
assert oauth.validate_state(state) is False
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run to confirm failure**
|
||||||
|
|
||||||
|
```
|
||||||
|
pytest tests/test_oauth.py -v -k "state"
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: `AttributeError: module 'hbd.server.oauth' has no attribute 'make_state'`
|
||||||
|
|
||||||
|
- [ ] **Step 3: Implement state functions**
|
||||||
|
|
||||||
|
Add to `hbd/server/oauth.py` after the `_states` dict definition:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def make_state() -> str:
|
||||||
|
"""Generate a CSRF state token, store it with TTL, and return it."""
|
||||||
|
_purge_states()
|
||||||
|
token = secrets.token_hex(32)
|
||||||
|
_states[token] = time.time() + STATE_TTL
|
||||||
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
def validate_state(state: str) -> bool:
|
||||||
|
"""Return True if *state* is known and unexpired; always removes it."""
|
||||||
|
expiry = _states.pop(state, None)
|
||||||
|
if expiry is None:
|
||||||
|
return False
|
||||||
|
return time.time() < expiry
|
||||||
|
|
||||||
|
|
||||||
|
def _purge_states() -> None:
|
||||||
|
now = time.time()
|
||||||
|
expired = [k for k, exp in list(_states.items()) if exp < now]
|
||||||
|
for k in expired:
|
||||||
|
del _states[k]
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Run to confirm tests pass**
|
||||||
|
|
||||||
|
```
|
||||||
|
pytest tests/test_oauth.py -v
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: 8 passed
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add hbd/server/oauth.py tests/test_oauth.py
|
||||||
|
git commit -m "feat: add OAuth2 CSRF state management"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 3: `provision_oauth_user` in users.py
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `hbd/server/users.py` (add `provision_oauth_user`)
|
||||||
|
- Modify: `tests/test_oauth.py` (add provisioning tests)
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write the failing tests**
|
||||||
|
|
||||||
|
Append to `tests/test_oauth.py`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from hbd.server import users as users_mod
|
||||||
|
from hbd.server.users import User
|
||||||
|
|
||||||
|
|
||||||
|
def _reset_users(entries=None):
|
||||||
|
users_mod.users = entries or {}
|
||||||
|
|
||||||
|
|
||||||
|
def test_provision_oauth_user_new():
|
||||||
|
_reset_users()
|
||||||
|
user = users_mod.provision_oauth_user("gituser", "Git User", "https://example.com/avatar.png")
|
||||||
|
assert user.username == "gituser"
|
||||||
|
assert user.full_name == "Git User"
|
||||||
|
assert user.avatar == "https://example.com/avatar.png"
|
||||||
|
assert user.admin is False
|
||||||
|
assert user.password_hash == ""
|
||||||
|
assert "gituser" in users_mod.users
|
||||||
|
|
||||||
|
|
||||||
|
def test_provision_oauth_user_no_password_login():
|
||||||
|
_reset_users()
|
||||||
|
user = users_mod.provision_oauth_user("gituser", "Git User", "")
|
||||||
|
assert user.check_password("anything") is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_provision_oauth_user_existing_updates_profile():
|
||||||
|
existing = User(
|
||||||
|
username="alice",
|
||||||
|
full_name="Old Name",
|
||||||
|
avatar="old.png",
|
||||||
|
password_hash="pbkdf2:sha256:1:salt:abc",
|
||||||
|
admin=True,
|
||||||
|
notification_channels=["chan1"],
|
||||||
|
)
|
||||||
|
_reset_users({"alice": existing})
|
||||||
|
user = users_mod.provision_oauth_user("alice", "New Name", "new.png")
|
||||||
|
assert user.full_name == "New Name"
|
||||||
|
assert user.avatar == "new.png"
|
||||||
|
# Preserved
|
||||||
|
assert user.admin is True
|
||||||
|
assert user.password_hash == "pbkdf2:sha256:1:salt:abc"
|
||||||
|
assert user.notification_channels == ["chan1"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_provision_oauth_user_does_not_overwrite_with_empty():
|
||||||
|
existing = User(username="bob", full_name="Bob", avatar="bob.png")
|
||||||
|
_reset_users({"bob": existing})
|
||||||
|
user = users_mod.provision_oauth_user("bob", "", "")
|
||||||
|
assert user.full_name == "Bob"
|
||||||
|
assert user.avatar == "bob.png"
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run to confirm failure**
|
||||||
|
|
||||||
|
```
|
||||||
|
pytest tests/test_oauth.py -v -k "provision"
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: `AttributeError: module 'hbd.server.users' has no attribute 'provision_oauth_user'`
|
||||||
|
|
||||||
|
- [ ] **Step 3: Implement `provision_oauth_user`**
|
||||||
|
|
||||||
|
Add to `hbd/server/users.py` after the `authenticate()` function (after line 187):
|
||||||
|
|
||||||
|
```python
|
||||||
|
def provision_oauth_user(username: str, full_name: str, avatar: str) -> "User":
|
||||||
|
"""Create or update a user sourced from an OAuth2 provider.
|
||||||
|
|
||||||
|
New users are inserted with no password_hash — they can only authenticate
|
||||||
|
via OAuth. Existing users (e.g. defined in config with a password) have
|
||||||
|
their display name and avatar refreshed; all other attributes are preserved.
|
||||||
|
"""
|
||||||
|
user = users.get(username)
|
||||||
|
if user is None:
|
||||||
|
user = User(username=username, full_name=full_name, avatar=avatar)
|
||||||
|
users[username] = user
|
||||||
|
logger.info("Provisioned OAuth user %r", username)
|
||||||
|
else:
|
||||||
|
if full_name:
|
||||||
|
user.full_name = full_name
|
||||||
|
if avatar:
|
||||||
|
user.avatar = avatar
|
||||||
|
return user
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Run to confirm tests pass**
|
||||||
|
|
||||||
|
```
|
||||||
|
pytest tests/test_oauth.py -v
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: 12 passed
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add hbd/server/users.py tests/test_oauth.py
|
||||||
|
git commit -m "feat: add provision_oauth_user() to users module"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 4: URL builder, token exchange, and user-info fetch
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `hbd/server/oauth.py` (add `authorization_url`, `exchange_code`, `fetch_user`)
|
||||||
|
- Modify: `tests/test_oauth.py` (add async tests with mocked HTTP)
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write the failing tests**
|
||||||
|
|
||||||
|
Append to `tests/test_oauth.py`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pytest
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
from urllib.parse import urlparse, parse_qs
|
||||||
|
|
||||||
|
|
||||||
|
def test_authorization_url_shape():
|
||||||
|
state = "teststate"
|
||||||
|
redirect_uri = "https://hbd.example.com/login/oauth/gitea/callback"
|
||||||
|
url = oauth.authorization_url(CFG_ON, state, redirect_uri)
|
||||||
|
parsed = urlparse(url)
|
||||||
|
qs = parse_qs(parsed.query)
|
||||||
|
assert parsed.scheme == "https"
|
||||||
|
assert parsed.netloc == "git.example.com"
|
||||||
|
assert parsed.path == "/login/oauth/authorize"
|
||||||
|
assert qs["client_id"] == ["cid"]
|
||||||
|
assert qs["state"] == ["teststate"]
|
||||||
|
assert qs["redirect_uri"] == [redirect_uri]
|
||||||
|
assert qs["scope"] == ["user:email"]
|
||||||
|
assert qs["response_type"] == ["code"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_exchange_code_returns_token():
|
||||||
|
redirect_uri = "https://hbd.example.com/login/oauth/gitea/callback"
|
||||||
|
mock_response = AsyncMock()
|
||||||
|
mock_response.status = 200
|
||||||
|
mock_response.json = AsyncMock(return_value={"access_token": "tok123"})
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.post = MagicMock(return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_response),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
))
|
||||||
|
|
||||||
|
with patch("hbd.server.oauth.aiohttp.ClientSession", return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_session),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
)):
|
||||||
|
token = await oauth.exchange_code(CFG_ON, "mycode", redirect_uri)
|
||||||
|
assert token == "tok123"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_exchange_code_raises_on_error_status():
|
||||||
|
redirect_uri = "https://hbd.example.com/login/oauth/gitea/callback"
|
||||||
|
mock_response = AsyncMock()
|
||||||
|
mock_response.status = 401
|
||||||
|
mock_response.text = AsyncMock(return_value="unauthorized")
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.post = MagicMock(return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_response),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
))
|
||||||
|
|
||||||
|
with patch("hbd.server.oauth.aiohttp.ClientSession", return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_session),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
)):
|
||||||
|
with pytest.raises(oauth.OAuthError):
|
||||||
|
await oauth.exchange_code(CFG_ON, "badcode", redirect_uri)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_fetch_user_returns_profile():
|
||||||
|
mock_response = AsyncMock()
|
||||||
|
mock_response.status = 200
|
||||||
|
mock_response.json = AsyncMock(return_value={
|
||||||
|
"login": "alice",
|
||||||
|
"full_name": "Alice Smith",
|
||||||
|
"avatar_url": "https://git.example.com/avatars/alice.png",
|
||||||
|
})
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.get = MagicMock(return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_response),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
))
|
||||||
|
|
||||||
|
with patch("hbd.server.oauth.aiohttp.ClientSession", return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_session),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
)):
|
||||||
|
profile = await oauth.fetch_user(CFG_ON, "tok123")
|
||||||
|
assert profile == {
|
||||||
|
"login": "alice",
|
||||||
|
"full_name": "Alice Smith",
|
||||||
|
"avatar_url": "https://git.example.com/avatars/alice.png",
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run to confirm failure**
|
||||||
|
|
||||||
|
```
|
||||||
|
pytest tests/test_oauth.py -v -k "url or exchange or fetch"
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: `AttributeError: module 'hbd.server.oauth' has no attribute 'authorization_url'`
|
||||||
|
|
||||||
|
- [ ] **Step 3: Implement the three functions**
|
||||||
|
|
||||||
|
Add to `hbd/server/oauth.py`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
|
|
||||||
|
def authorization_url(config: dict, state: str, redirect_uri: str) -> str:
|
||||||
|
"""Return the Gitea OAuth2 authorization URL to redirect the browser to."""
|
||||||
|
g = _gitea_cfg(config)
|
||||||
|
params = urllib.parse.urlencode({
|
||||||
|
"client_id": g["client_id"],
|
||||||
|
"redirect_uri": redirect_uri,
|
||||||
|
"response_type": "code",
|
||||||
|
"scope": "user:email",
|
||||||
|
"state": state,
|
||||||
|
})
|
||||||
|
return f"{g['url'].rstrip('/')}/login/oauth/authorize?{params}"
|
||||||
|
|
||||||
|
|
||||||
|
async def exchange_code(config: dict, code: str, redirect_uri: str) -> str:
|
||||||
|
"""Exchange an authorization *code* for a Gitea access token.
|
||||||
|
|
||||||
|
Returns the access token string. Raises OAuthError on any failure.
|
||||||
|
"""
|
||||||
|
g = _gitea_cfg(config)
|
||||||
|
url = f"{g['url'].rstrip('/')}/login/oauth/access_token"
|
||||||
|
payload = {
|
||||||
|
"client_id": g["client_id"],
|
||||||
|
"client_secret": g["client_secret"],
|
||||||
|
"code": code,
|
||||||
|
"grant_type": "authorization_code",
|
||||||
|
"redirect_uri": redirect_uri,
|
||||||
|
}
|
||||||
|
timeout = aiohttp.ClientTimeout(total=10)
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||||
|
async with session.post(url, json=payload, headers={"Accept": "application/json"}) as resp:
|
||||||
|
if resp.status != 200:
|
||||||
|
text = await resp.text()
|
||||||
|
raise OAuthError(f"Token exchange failed ({resp.status}): {text}")
|
||||||
|
data = await resp.json()
|
||||||
|
except aiohttp.ClientError as exc:
|
||||||
|
raise OAuthError(f"Token exchange network error: {exc}") from exc
|
||||||
|
token = data.get("access_token")
|
||||||
|
if not token:
|
||||||
|
raise OAuthError(f"No access_token in response: {data}")
|
||||||
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
async def fetch_user(config: dict, token: str) -> dict:
|
||||||
|
"""Fetch the authenticated user's profile from Gitea.
|
||||||
|
|
||||||
|
Returns a dict with keys: login, full_name, avatar_url.
|
||||||
|
Raises OAuthError on any failure.
|
||||||
|
"""
|
||||||
|
g = _gitea_cfg(config)
|
||||||
|
url = f"{g['url'].rstrip('/')}/api/v1/user"
|
||||||
|
timeout = aiohttp.ClientTimeout(total=10)
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||||
|
async with session.get(url, headers={"Authorization": f"token {token}"}) as resp:
|
||||||
|
if resp.status != 200:
|
||||||
|
text = await resp.text()
|
||||||
|
raise OAuthError(f"User fetch failed ({resp.status}): {text}")
|
||||||
|
data = await resp.json()
|
||||||
|
except aiohttp.ClientError as exc:
|
||||||
|
raise OAuthError(f"User fetch network error: {exc}") from exc
|
||||||
|
return {
|
||||||
|
"login": data.get("login", ""),
|
||||||
|
"full_name": data.get("full_name", ""),
|
||||||
|
"avatar_url": data.get("avatar_url", ""),
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Also add `import urllib.parse` at the top of `oauth.py` (alongside the existing imports).
|
||||||
|
|
||||||
|
- [ ] **Step 4: Run to confirm tests pass**
|
||||||
|
|
||||||
|
```
|
||||||
|
pytest tests/test_oauth.py -v
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: 17 passed
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add hbd/server/oauth.py tests/test_oauth.py
|
||||||
|
git commit -m "feat: add authorization_url, exchange_code, fetch_user to oauth module"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 5: HTTP routes — redirect and callback
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `hbd/server/http.py`
|
||||||
|
|
||||||
|
`http.py` defines all handlers inside `async def start(...)`. The two new handlers go in the same block, just before the `app = web.Application()` line (~line 900). The import goes at the top of the file.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Add the import**
|
||||||
|
|
||||||
|
In `hbd/server/http.py`, add after the existing local imports (after `from . import users as users_mod`):
|
||||||
|
|
||||||
|
```python
|
||||||
|
from . import oauth as oauth_mod
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Add the two route handlers**
|
||||||
|
|
||||||
|
In `hbd/server/http.py`, add the two handlers immediately before the `app = web.Application()` line:
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def oauth_gitea_redirect(request):
|
||||||
|
"""GET /login/oauth/gitea — kick off the Gitea OAuth2 flow."""
|
||||||
|
if not oauth_mod.is_enabled(config):
|
||||||
|
return web.Response(status=404, text="OAuth not configured")
|
||||||
|
state = oauth_mod.make_state()
|
||||||
|
redirect_uri = f"{request.url.origin()}/login/oauth/gitea/callback"
|
||||||
|
raise web.HTTPFound(oauth_mod.authorization_url(config, state, redirect_uri))
|
||||||
|
|
||||||
|
async def oauth_gitea_callback(request):
|
||||||
|
"""GET /login/oauth/gitea/callback — handle Gitea's redirect back."""
|
||||||
|
if not oauth_mod.is_enabled(config):
|
||||||
|
return web.Response(status=404, text="OAuth not configured")
|
||||||
|
code = request.rel_url.query.get("code", "")
|
||||||
|
state = request.rel_url.query.get("state", "")
|
||||||
|
if not code or not state:
|
||||||
|
return web.Response(status=400, text="Missing code or state")
|
||||||
|
if not oauth_mod.validate_state(state):
|
||||||
|
raise web.HTTPFound("/login?error=1")
|
||||||
|
redirect_uri = f"{request.url.origin()}/login/oauth/gitea/callback"
|
||||||
|
try:
|
||||||
|
token = await oauth_mod.exchange_code(config, code, redirect_uri)
|
||||||
|
profile = await oauth_mod.fetch_user(config, token)
|
||||||
|
except oauth_mod.OAuthError as exc:
|
||||||
|
logger.warning("OAuth error: %s", exc)
|
||||||
|
raise web.HTTPFound("/login?error=1")
|
||||||
|
user = users_mod.provision_oauth_user(
|
||||||
|
profile["login"],
|
||||||
|
profile["full_name"],
|
||||||
|
profile["avatar_url"],
|
||||||
|
)
|
||||||
|
session_token = users_mod.create_session(user.username)
|
||||||
|
resp = web.HTTPFound("/")
|
||||||
|
resp.set_cookie(
|
||||||
|
SESSION_COOKIE,
|
||||||
|
session_token,
|
||||||
|
max_age=users_mod.SESSION_TTL,
|
||||||
|
httponly=True,
|
||||||
|
samesite="Lax",
|
||||||
|
)
|
||||||
|
raise resp
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Register the routes**
|
||||||
|
|
||||||
|
In `hbd/server/http.py`, add to the route list after the existing auth routes (after `web.post("/api/0/auth/logout", api_logout)`):
|
||||||
|
|
||||||
|
```python
|
||||||
|
web.get("/login/oauth/gitea", oauth_gitea_redirect),
|
||||||
|
web.get("/login/oauth/gitea/callback", oauth_gitea_callback),
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Manual smoke test**
|
||||||
|
|
||||||
|
Start the server locally with OAuth configured in `~/.hb.yaml`:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
oauth:
|
||||||
|
gitea:
|
||||||
|
url: https://your-gitea-instance.example.com
|
||||||
|
client_id: your-client-id
|
||||||
|
client_secret: your-client-secret
|
||||||
|
```
|
||||||
|
|
||||||
|
Visit `http://localhost:50004/login/oauth/gitea` — confirm you are redirected to Gitea's authorization page.
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add hbd/server/http.py
|
||||||
|
git commit -m "feat: add Gitea OAuth2 redirect and callback routes"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 6: Login page — "Sign in with Gitea" button
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `hbd/server/http.py` (update `login_page` handler, ~line 625)
|
||||||
|
|
||||||
|
- [ ] **Step 1: Replace the login page HTML**
|
||||||
|
|
||||||
|
In `hbd/server/http.py`, find the `html = f"""` block inside `login_page` and replace it with:
|
||||||
|
|
||||||
|
```python
|
||||||
|
gitea_button = ""
|
||||||
|
if oauth_mod.is_enabled(config):
|
||||||
|
gitea_url = _gitea_cfg_url(config)
|
||||||
|
gitea_button = f"""
|
||||||
|
<div class="divider">or</div>
|
||||||
|
<a href="/login/oauth/gitea" class="gitea-btn">
|
||||||
|
Sign in with Gitea
|
||||||
|
</a>"""
|
||||||
|
|
||||||
|
html = f"""<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Heartbeat — Login</title>
|
||||||
|
<style>
|
||||||
|
body {{ font-family: sans-serif; background: #f5f5f5; display: flex;
|
||||||
|
justify-content: center; align-items: center; height: 100vh; margin: 0; }}
|
||||||
|
.box {{ background: #fff; padding: 2em 2.5em; border-radius: 8px;
|
||||||
|
box-shadow: 0 2px 12px rgba(0,0,0,.15); min-width: 300px; }}
|
||||||
|
h2 {{ margin: 0 0 1.2em; color: #333; font-size: 1.4em; }}
|
||||||
|
label {{ display: block; margin-bottom: .3em; font-size: .9em; color: #555; }}
|
||||||
|
input {{ width: 100%; padding: .5em .7em; border: 1px solid #ccc;
|
||||||
|
border-radius: 4px; font-size: 1em; box-sizing: border-box; }}
|
||||||
|
button {{ margin-top: 1.2em; width: 100%; padding: .6em; background: #0066cc;
|
||||||
|
color: #fff; border: none; border-radius: 4px; font-size: 1em; cursor: pointer; }}
|
||||||
|
button:hover {{ background: #0055aa; }}
|
||||||
|
.error {{ color: #c00; font-size: .9em; margin-bottom: .8em; }}
|
||||||
|
.field {{ margin-bottom: .9em; }}
|
||||||
|
.divider {{ text-align: center; margin: 1.2em 0 .8em; color: #999;
|
||||||
|
font-size: .85em; border-top: 1px solid #eee; padding-top: .8em; }}
|
||||||
|
.gitea-btn {{ display: block; width: 100%; padding: .6em; background: #609926;
|
||||||
|
color: #fff; border-radius: 4px; font-size: 1em; text-align: center;
|
||||||
|
text-decoration: none; box-sizing: border-box; }}
|
||||||
|
.gitea-btn:hover {{ background: #4e7d1e; }}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="box">
|
||||||
|
<h2>Heartbeat</h2>
|
||||||
|
{'<p class="error">Invalid username, password, or OAuth error.</p>' if error else ''}
|
||||||
|
<form method="post">
|
||||||
|
<div class="field"><label>Username</label><input name="username" autofocus></div>
|
||||||
|
<div class="field"><label>Password</label><input name="password" type="password"></div>
|
||||||
|
<button type="submit">Sign in</button>
|
||||||
|
</form>{gitea_button}
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>"""
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Add the `_gitea_cfg_url` helper**
|
||||||
|
|
||||||
|
Add this small helper in `hbd/server/http.py` just before the `login_page` handler (around line 600) so the template can read the Gitea display URL without importing internal oauth details:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def _gitea_cfg_url(config: dict) -> str:
|
||||||
|
return config.get("oauth", {}).get("gitea", {}).get("url", "")
|
||||||
|
```
|
||||||
|
|
||||||
|
Also update the `login_page` handler's `error` logic to show the error when the `?error=1` query param is present (set by the callback on OAuth failure):
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def login_page(request):
|
||||||
|
"""GET /login — show login form; POST /login — process and redirect."""
|
||||||
|
if not users_mod.users_enabled():
|
||||||
|
raise web.HTTPFound("/")
|
||||||
|
|
||||||
|
error = ""
|
||||||
|
if request.method == "POST":
|
||||||
|
form = await request.post()
|
||||||
|
username = form.get("username", "")
|
||||||
|
password = form.get("password", "")
|
||||||
|
user = users_mod.authenticate(username, password)
|
||||||
|
if user:
|
||||||
|
token = users_mod.create_session(username)
|
||||||
|
redirect_to = request.rel_url.query.get("next", "/")
|
||||||
|
resp = web.HTTPFound(redirect_to)
|
||||||
|
resp.set_cookie(
|
||||||
|
SESSION_COOKIE,
|
||||||
|
token,
|
||||||
|
max_age=users_mod.SESSION_TTL,
|
||||||
|
httponly=True,
|
||||||
|
samesite="Lax",
|
||||||
|
)
|
||||||
|
raise resp
|
||||||
|
error = "Invalid username or password."
|
||||||
|
elif request.rel_url.query.get("error"):
|
||||||
|
error = "Sign-in failed. Please try again."
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Manual verification**
|
||||||
|
|
||||||
|
Start the server with OAuth configured. Visit `/login`. Confirm:
|
||||||
|
- The "Sign in with Gitea" button appears (green, below a divider)
|
||||||
|
- Clicking it redirects to Gitea
|
||||||
|
- After authorising on Gitea, you are redirected back and land on `/` with a valid session cookie
|
||||||
|
|
||||||
|
Without OAuth configured, confirm the button does not appear.
|
||||||
|
|
||||||
|
- [ ] **Step 4: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add hbd/server/http.py
|
||||||
|
git commit -m "feat: add Sign in with Gitea button to login page"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Self-Review Notes
|
||||||
|
|
||||||
|
- All 5 spec requirements covered: coexist ✓, auto-provision ✓, regular user ✓, any Gitea user ✓, config-driven ✓
|
||||||
|
- `exchange_code` signature in Task 4 matches usage in Task 5 (`config, code, redirect_uri`) ✓
|
||||||
|
- `fetch_user` returns `{login, full_name, avatar_url}` — matched in callback handler ✓
|
||||||
|
- `validate_state` removes state on use (replay protection) ✓
|
||||||
|
- `provision_oauth_user` skips empty strings so existing avatar/name aren't erased ✓
|
||||||
|
- `_gitea_cfg_url` is a plain `def`, not `async` — safe to call in template prep ✓
|
||||||
@@ -0,0 +1,184 @@
|
|||||||
|
# Gitea OAuth2 Authentication — Design Spec
|
||||||
|
|
||||||
|
Date: 2026-05-08
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
Add Gitea as an OAuth2 login provider alongside the existing username/password
|
||||||
|
authentication. Any user on the configured Gitea instance can sign in; their
|
||||||
|
local account is auto-provisioned on first login as a regular (non-admin) user.
|
||||||
|
Password login continues to work unchanged.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Config
|
||||||
|
|
||||||
|
A new optional `oauth.gitea` block in `~/.hb.yaml`. OAuth is disabled when the
|
||||||
|
block is absent or any of the three required keys is missing.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
oauth:
|
||||||
|
gitea:
|
||||||
|
url: https://git.example.com # Gitea base URL, no trailing slash
|
||||||
|
client_id: <gitea-app-client-id>
|
||||||
|
client_secret: <gitea-app-client-secret>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Gitea setup:** Create an OAuth2 application in Gitea under
|
||||||
|
*Settings → Applications → OAuth2*. Set the redirect URI to
|
||||||
|
`https://<hbd-host>/login/oauth/gitea/callback`.
|
||||||
|
|
||||||
|
`config.py` default:
|
||||||
|
|
||||||
|
```python
|
||||||
|
"oauth": {},
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## New module: `hbd/server/oauth.py`
|
||||||
|
|
||||||
|
Owns all OAuth2 logic. No new dependencies — uses `aiohttp.ClientSession`
|
||||||
|
already present in the codebase.
|
||||||
|
|
||||||
|
### CSRF state store
|
||||||
|
|
||||||
|
```python
|
||||||
|
# state -> expires (float)
|
||||||
|
_states: dict[str, float] = {}
|
||||||
|
STATE_TTL = 600 # 10 minutes
|
||||||
|
```
|
||||||
|
|
||||||
|
`_states` is an in-memory dict. Entries are created on redirect and deleted on
|
||||||
|
use or expiry. A purge runs on every new state generation.
|
||||||
|
|
||||||
|
### Public API
|
||||||
|
|
||||||
|
| Function | Description |
|
||||||
|
|---|---|
|
||||||
|
| `is_enabled(config)` | Returns `True` when url, client_id, and client_secret are all set |
|
||||||
|
| `make_state()` | Generates a random state token, stores it with TTL, returns it |
|
||||||
|
| `validate_state(state)` | Returns `True` and removes the state if valid and unexpired |
|
||||||
|
| `authorization_url(config, state, redirect_uri)` | Builds the Gitea `/login/oauth/authorize` redirect URL with `client_id`, `redirect_uri`, `scope=user:email`, `state` |
|
||||||
|
| `exchange_code(config, code, redirect_uri)` async | POSTs to Gitea `/login/oauth/access_token` with code and redirect_uri, returns the access token string or raises `OAuthError` |
|
||||||
|
| `fetch_user(config, token)` async | GETs Gitea `/api/v1/user` with Bearer token, returns `{"login", "full_name", "avatar_url"}` or raises `OAuthError` |
|
||||||
|
|
||||||
|
### Error handling
|
||||||
|
|
||||||
|
`OAuthError(message)` is a module-level exception. The callback route catches it
|
||||||
|
and renders the login page with an error message — identical to an invalid
|
||||||
|
password error in UX terms.
|
||||||
|
|
||||||
|
Network timeouts use a 10-second `aiohttp` timeout. Any non-2xx response from
|
||||||
|
Gitea raises `OAuthError`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Change: `hbd/server/users.py`
|
||||||
|
|
||||||
|
One new function added to the public API:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def provision_oauth_user(username: str, full_name: str, avatar: str) -> User:
|
||||||
|
```
|
||||||
|
|
||||||
|
- If the username does not exist in the live `users` dict, creates a `User`
|
||||||
|
with no `password_hash` (so password login is impossible for this account)
|
||||||
|
and inserts it.
|
||||||
|
- If the username already exists (e.g. was defined in config with a password),
|
||||||
|
updates `full_name` and `avatar` from the OAuth profile and returns the
|
||||||
|
existing user unchanged in all other respects (preserving admin flag,
|
||||||
|
notification channels, etc.).
|
||||||
|
- Logs a one-line INFO message on first provision.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Changes: `hbd/server/http.py`
|
||||||
|
|
||||||
|
### Two new route handlers
|
||||||
|
|
||||||
|
**`GET /login/oauth/gitea`**
|
||||||
|
|
||||||
|
1. Checks `oauth.is_enabled(config)` — returns 404 if not.
|
||||||
|
2. Calls `oauth.make_state()`.
|
||||||
|
3. Constructs `redirect_uri` as `{request.url.origin()}/login/oauth/gitea/callback` using aiohttp's `request.url.origin()`.
|
||||||
|
4. Redirects the browser to `oauth.authorization_url(config, state, redirect_uri)`.
|
||||||
|
|
||||||
|
**`GET /login/oauth/gitea/callback`**
|
||||||
|
|
||||||
|
1. Reads `code` and `state` query params; returns 400 if either is missing.
|
||||||
|
2. Calls `oauth.validate_state(state)` — redirects to `/login` with error if
|
||||||
|
invalid (CSRF or replay protection).
|
||||||
|
3. Reconstructs the same `redirect_uri` as the redirect handler (required by OAuth2 spec for token exchange).
|
||||||
|
4. Calls `await oauth.exchange_code(config, code, redirect_uri)` to get the access token.
|
||||||
|
4. Calls `await oauth.fetch_user(config, token)` to get the Gitea user profile.
|
||||||
|
5. Calls `users_mod.provision_oauth_user(login, full_name, avatar_url)`.
|
||||||
|
6. Calls `users_mod.create_session(username)` to get a session token.
|
||||||
|
7. Sets `hbd_session` cookie (same flags as password login: httponly, Lax,
|
||||||
|
24h TTL).
|
||||||
|
8. Redirects to `/`.
|
||||||
|
9. Any `OAuthError` re-renders the login page with a generic error message.
|
||||||
|
|
||||||
|
### Login page change
|
||||||
|
|
||||||
|
When `oauth.is_enabled(config)` is `True`, the existing login form gains a
|
||||||
|
separator and a "Sign in with Gitea" link button pointing to
|
||||||
|
`/login/oauth/gitea`. The password form is always rendered regardless.
|
||||||
|
|
||||||
|
### Route registration
|
||||||
|
|
||||||
|
```python
|
||||||
|
web.get("/login/oauth/gitea", oauth_redirect),
|
||||||
|
web.get("/login/oauth/gitea/callback", oauth_callback),
|
||||||
|
```
|
||||||
|
|
||||||
|
Added alongside the existing `/login` and `/logout` routes.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Data flow
|
||||||
|
|
||||||
|
```
|
||||||
|
Browser hbd Gitea
|
||||||
|
| | |
|
||||||
|
|-- GET /login ----------->| |
|
||||||
|
|<- login page (+ button) -| |
|
||||||
|
| | |
|
||||||
|
|-- GET /login/oauth/gitea>| |
|
||||||
|
|<- 302 Gitea /authorize --| |
|
||||||
|
| | |
|
||||||
|
|-- GET /login/oauth/authorize ----------------------->|
|
||||||
|
|<- 302 /login/oauth/gitea/callback?code=..&state=.. --|
|
||||||
|
| | |
|
||||||
|
|-- GET /callback -------->| |
|
||||||
|
| |-- POST /access_token ---->|
|
||||||
|
| |<- {access_token} ---------|
|
||||||
|
| |-- GET /api/v1/user ------>|
|
||||||
|
| |<- {login, name, avatar} --|
|
||||||
|
| | provision_oauth_user() |
|
||||||
|
| | create_session() |
|
||||||
|
|<- 302 / (set cookie) ----| |
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
- `test_oauth_state`: `make_state` + `validate_state` happy path; expired state
|
||||||
|
returns False; replay (double-use) returns False.
|
||||||
|
- `test_provision_oauth_user_new`: new username creates User with no password.
|
||||||
|
- `test_provision_oauth_user_existing`: existing config user updates name/avatar,
|
||||||
|
preserves admin flag and notification_channels.
|
||||||
|
- `test_oauth_callback_invalid_state`: callback with bad state redirects to login.
|
||||||
|
- Integration: mock Gitea endpoints with `aiohttp_client` fixture; full
|
||||||
|
redirect → callback → session cookie flow.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Out of scope
|
||||||
|
|
||||||
|
- Restricting login to specific Gitea organisations or teams.
|
||||||
|
- Making OAuth users admin automatically.
|
||||||
|
- Multiple OAuth providers.
|
||||||
|
- Token refresh (Gitea access tokens are long-lived; the hbd session TTL governs
|
||||||
|
re-authentication).
|
||||||
+1
-1
@@ -14,4 +14,4 @@ Install options:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
__all__ = ["__version__"]
|
__all__ = ["__version__"]
|
||||||
__version__ = "5.2.4"
|
__version__ = "5.2.6"
|
||||||
|
|||||||
@@ -89,14 +89,24 @@ class ZFSMonitorPlugin(MonitorPlugin):
|
|||||||
name = parts[0].strip()
|
name = parts[0].strip()
|
||||||
if self._pools_filter and name not in self._pools_filter:
|
if self._pools_filter and name not in self._pools_filter:
|
||||||
continue
|
continue
|
||||||
|
health = parts[1].strip()
|
||||||
|
if health == "ONLINE":
|
||||||
|
status = 0
|
||||||
|
elif health in ("DEGRADED", "ONLINE with errors"):
|
||||||
|
status = 1
|
||||||
|
elif health in ("FAULTED", "OFFLINE", "UNAVAIL"):
|
||||||
|
status = 2
|
||||||
|
else:
|
||||||
|
status = 3 # unknown status
|
||||||
pools[name] = {
|
pools[name] = {
|
||||||
"health": parts[1].strip(),
|
"health": health,
|
||||||
"size": _int(parts[2]),
|
"status": status,
|
||||||
"alloc": _int(parts[3]),
|
"size": _int(parts[2]),
|
||||||
"free": _int(parts[4]),
|
"alloc": _int(parts[3]),
|
||||||
"capacity": _float(parts[5]),
|
"free": _int(parts[4]),
|
||||||
"frag": _float(parts[6]),
|
"capacity": _float(parts[5]),
|
||||||
"dedup": _float(parts[7]),
|
"frag": _float(parts[6]),
|
||||||
|
"dedup": _float(parts[7]),
|
||||||
}
|
}
|
||||||
return pools
|
return pools
|
||||||
|
|
||||||
|
|||||||
@@ -134,6 +134,30 @@ thresholds:
|
|||||||
hysteresis: 0.1
|
hysteresis: 0.1
|
||||||
enabled: true
|
enabled: true
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# ZFS Monitor Thresholds
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
zfs_monitor:
|
||||||
|
# Pool health check — built-in default; shown here for reference/override.
|
||||||
|
# status is 0 (ONLINE) or 1 (DEGRADED) or 2 (SUSPENDED, FAULTED, UNAVAIL…).
|
||||||
|
# Use '*' to apply the same rule to every pool, or name a specific pool.
|
||||||
|
pools:
|
||||||
|
'*':
|
||||||
|
status:
|
||||||
|
warning: 1 # Alert WARNING when pool is DEGRADED
|
||||||
|
critical: 2 # Alert CRITICAL when pool is SUSPENDED/FAULTED/UNAVAIL
|
||||||
|
operator: ">"
|
||||||
|
hysteresis: 0.0 # No hysteresis — a degraded pool is always critical
|
||||||
|
display: "ZFS pool {pool_name} is {health}"
|
||||||
|
|
||||||
|
# Per-pool capacity thresholds (optional; add pools you care about)
|
||||||
|
# tank:
|
||||||
|
# capacity:
|
||||||
|
# warning: 75.0 # Warn at 75% used
|
||||||
|
# critical: 90.0 # Critical at 90% used
|
||||||
|
# operator: ">"
|
||||||
|
# hysteresis: 0.05
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
# Network Monitor Thresholds
|
# Network Monitor Thresholds
|
||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
|
|||||||
+18
-2
@@ -34,6 +34,9 @@ SERVER_DEFAULTS = {
|
|||||||
"users": {}, # username -> {full_name, avatar, password, admin, notification_channels}
|
"users": {}, # username -> {full_name, avatar, password, admin, notification_channels}
|
||||||
"default_owner": None, # Username that owns hosts with no explicit owner
|
"default_owner": None, # Username that owns hosts with no explicit owner
|
||||||
|
|
||||||
|
# OAuth2 providers
|
||||||
|
"oauth": {}, # oauth.gitea.{url,client_id,client_secret}
|
||||||
|
|
||||||
# Host management
|
# Host management
|
||||||
"hosts": {}, # Unified host definitions
|
"hosts": {}, # Unified host definitions
|
||||||
"dyndnshosts": [], # Hosts with dynamic DNS (legacy)
|
"dyndnshosts": [], # Hosts with dynamic DNS (legacy)
|
||||||
@@ -100,8 +103,21 @@ THRESHOLD_DEFAULTS = {
|
|||||||
'status_code': {
|
'status_code': {
|
||||||
'display': '{check_name} {output}',
|
'display': '{check_name} {output}',
|
||||||
'operator': "nagios"
|
'operator': "nagios"
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
'zfs_monitor': {
|
||||||
|
'pools': {
|
||||||
|
'*': {
|
||||||
|
'status': {
|
||||||
|
'warning': 1,
|
||||||
|
'critical': 2,
|
||||||
|
'operator': '>',
|
||||||
|
'hysteresis': 0.0,
|
||||||
|
'display': 'ZFS pool {pool_name} is {health}'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+69
-1
@@ -16,6 +16,7 @@ from . import data
|
|||||||
from . import notify as notify_mod
|
from . import notify as notify_mod
|
||||||
from . import settings as settings_mod
|
from . import settings as settings_mod
|
||||||
from . import users as users_mod
|
from . import users as users_mod
|
||||||
|
from . import oauth as oauth_mod
|
||||||
from . import ws as ws_mod
|
from . import ws as ws_mod
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -538,6 +539,7 @@ async def start(
|
|||||||
"name": hostname,
|
"name": hostname,
|
||||||
"plugins": list(host.plugin_data.keys()),
|
"plugins": list(host.plugin_data.keys()),
|
||||||
"is_owner": _can_own_host(current_user, host),
|
"is_owner": _can_own_host(current_user, host),
|
||||||
|
"owner": host.owner,
|
||||||
})
|
})
|
||||||
|
|
||||||
tmpl = env.get_template("plugins.html")
|
tmpl = env.get_template("plugins.html")
|
||||||
@@ -620,6 +622,18 @@ async def start(
|
|||||||
)
|
)
|
||||||
raise resp
|
raise resp
|
||||||
error = "Invalid username or password."
|
error = "Invalid username or password."
|
||||||
|
elif request.rel_url.query.get("error"):
|
||||||
|
error = "Sign-in failed. Please try again."
|
||||||
|
|
||||||
|
gitea_button = ""
|
||||||
|
if oauth_mod.is_enabled(config):
|
||||||
|
logo_url = config.get("oauth", {}).get("gitea", {}).get("logo", "")
|
||||||
|
logo_img = f'<img src="{logo_url}" alt="" class="gitea-logo">' if logo_url else ""
|
||||||
|
gitea_button = f"""
|
||||||
|
<div class="divider">or</div>
|
||||||
|
<a href="/login/oauth/gitea" class="gitea-btn">
|
||||||
|
{logo_img}Sign in with Gitea
|
||||||
|
</a>"""
|
||||||
|
|
||||||
html = f"""<!DOCTYPE html>
|
html = f"""<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
@@ -640,6 +654,14 @@ async def start(
|
|||||||
button:hover {{ background: #0055aa; }}
|
button:hover {{ background: #0055aa; }}
|
||||||
.error {{ color: #c00; font-size: .9em; margin-bottom: .8em; }}
|
.error {{ color: #c00; font-size: .9em; margin-bottom: .8em; }}
|
||||||
.field {{ margin-bottom: .9em; }}
|
.field {{ margin-bottom: .9em; }}
|
||||||
|
.divider {{ text-align: center; margin: 1.2em 0 .8em; color: #999;
|
||||||
|
font-size: .85em; border-top: 1px solid #eee; padding-top: .8em; }}
|
||||||
|
.gitea-btn {{ display: flex; align-items: center; justify-content: center;
|
||||||
|
gap: .5em; width: 100%; padding: .6em; background: #16191d;
|
||||||
|
color: #fff; border-radius: 4px; font-size: 1em; text-align: center;
|
||||||
|
text-decoration: none; box-sizing: border-box; }}
|
||||||
|
.gitea-btn:hover {{ background: #4e7d1e; }}
|
||||||
|
.gitea-logo {{ height: 1.2em; width: auto; vertical-align: middle; }}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -650,7 +672,7 @@ async def start(
|
|||||||
<div class="field"><label>Username</label><input name="username" autofocus></div>
|
<div class="field"><label>Username</label><input name="username" autofocus></div>
|
||||||
<div class="field"><label>Password</label><input name="password" type="password"></div>
|
<div class="field"><label>Password</label><input name="password" type="password"></div>
|
||||||
<button type="submit">Sign in</button>
|
<button type="submit">Sign in</button>
|
||||||
</form>
|
</form>{gitea_button}
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>"""
|
</html>"""
|
||||||
@@ -896,6 +918,50 @@ async def start(
|
|||||||
)
|
)
|
||||||
return web.Response(text=body, content_type="text/html")
|
return web.Response(text=body, content_type="text/html")
|
||||||
|
|
||||||
|
def _oauth_redirect_uri(request) -> str:
|
||||||
|
base = config.get("base_url", "").rstrip("/") or str(request.url.origin())
|
||||||
|
return f"{base}/login/oauth/gitea/callback"
|
||||||
|
|
||||||
|
async def oauth_gitea_redirect(request):
|
||||||
|
"""GET /login/oauth/gitea — kick off the Gitea OAuth2 flow."""
|
||||||
|
if not oauth_mod.is_enabled(config):
|
||||||
|
return web.Response(status=404, text="OAuth not configured")
|
||||||
|
state = oauth_mod.make_state()
|
||||||
|
raise web.HTTPFound(oauth_mod.authorization_url(config, state, _oauth_redirect_uri(request)))
|
||||||
|
|
||||||
|
async def oauth_gitea_callback(request):
|
||||||
|
"""GET /login/oauth/gitea/callback — handle Gitea's redirect back."""
|
||||||
|
if not oauth_mod.is_enabled(config):
|
||||||
|
return web.Response(status=404, text="OAuth not configured")
|
||||||
|
code = request.rel_url.query.get("code", "")
|
||||||
|
state = request.rel_url.query.get("state", "")
|
||||||
|
if not code or not state:
|
||||||
|
return web.Response(status=400, text="Missing code or state")
|
||||||
|
if not oauth_mod.validate_state(state):
|
||||||
|
logger.warning("OAuth: invalid or expired state token from %s", request.remote)
|
||||||
|
raise web.HTTPFound("/login?error=1")
|
||||||
|
try:
|
||||||
|
token = await oauth_mod.exchange_code(config, code, _oauth_redirect_uri(request))
|
||||||
|
profile = await oauth_mod.fetch_user(config, token)
|
||||||
|
except oauth_mod.OAuthError as exc:
|
||||||
|
logger.warning("OAuth error: %s", exc)
|
||||||
|
raise web.HTTPFound("/login?error=1")
|
||||||
|
user = users_mod.provision_oauth_user(
|
||||||
|
profile["login"],
|
||||||
|
profile["full_name"],
|
||||||
|
profile["avatar_url"],
|
||||||
|
)
|
||||||
|
session_token = users_mod.create_session(user.username)
|
||||||
|
resp = web.HTTPFound("/")
|
||||||
|
resp.set_cookie(
|
||||||
|
SESSION_COOKIE,
|
||||||
|
session_token,
|
||||||
|
max_age=users_mod.SESSION_TTL,
|
||||||
|
httponly=True,
|
||||||
|
samesite="Lax",
|
||||||
|
)
|
||||||
|
raise resp
|
||||||
|
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
app.add_routes(
|
app.add_routes(
|
||||||
[
|
[
|
||||||
@@ -907,6 +973,8 @@ async def start(
|
|||||||
web.get("/logout", web_logout),
|
web.get("/logout", web_logout),
|
||||||
web.post("/api/0/auth/login", api_login),
|
web.post("/api/0/auth/login", api_login),
|
||||||
web.post("/api/0/auth/logout", api_logout),
|
web.post("/api/0/auth/logout", api_logout),
|
||||||
|
web.get("/login/oauth/gitea", oauth_gitea_redirect),
|
||||||
|
web.get("/login/oauth/gitea/callback", oauth_gitea_callback),
|
||||||
# Users
|
# Users
|
||||||
web.get("/api/0/users", api_users),
|
web.get("/api/0/users", api_users),
|
||||||
web.get("/api/0/users/me", api_user_self),
|
web.get("/api/0/users/me", api_user_self),
|
||||||
|
|||||||
@@ -141,9 +141,11 @@ def _send_pushover(channel_cfg: dict, notif: Notification) -> bool:
|
|||||||
logger.warning("pushover: missing token or user")
|
logger.warning("pushover: missing token or user")
|
||||||
return False
|
return False
|
||||||
params: dict = {"token": token, "user": user, "title": notif.title, "message": notif.body}
|
params: dict = {"token": token, "user": user, "title": notif.title, "message": notif.body}
|
||||||
|
if channel_cfg.get("sound"):
|
||||||
|
params["sound"] = channel_cfg["sound"]
|
||||||
if notif.url:
|
if notif.url:
|
||||||
params["url"] = notif.url
|
params["url"] = notif.url
|
||||||
params["url_title"] = "Plugin metrics"
|
params["url_title"] = "Heartbeat"
|
||||||
conn = http.client.HTTPSConnection("api.pushover.net:443")
|
conn = http.client.HTTPSConnection("api.pushover.net:443")
|
||||||
try:
|
try:
|
||||||
conn.request(
|
conn.request(
|
||||||
@@ -399,7 +401,7 @@ def _build_url(host_name: str) -> str:
|
|||||||
base_url = _config.get("base_url", "").rstrip("/")
|
base_url = _config.get("base_url", "").rstrip("/")
|
||||||
if not base_url:
|
if not base_url:
|
||||||
return ""
|
return ""
|
||||||
return f"{base_url}/plugins#{host_name}"
|
return f"{base_url}/alerts?filter={host_name}"
|
||||||
|
|
||||||
|
|
||||||
async def send_notification(host_name: str, notif: Notification) -> dict:
|
async def send_notification(host_name: str, notif: Notification) -> dict:
|
||||||
|
|||||||
@@ -0,0 +1,142 @@
|
|||||||
|
"""Gitea OAuth2 support.
|
||||||
|
|
||||||
|
Config shape (in ~/.hb.yaml):
|
||||||
|
|
||||||
|
oauth:
|
||||||
|
gitea:
|
||||||
|
url: https://git.example.com
|
||||||
|
client_id: <client-id>
|
||||||
|
client_secret: <client-secret>
|
||||||
|
|
||||||
|
Register a Gitea OAuth2 application at:
|
||||||
|
Gitea → Settings → Applications → OAuth2
|
||||||
|
Set the redirect URI to:
|
||||||
|
https://<hbd-host>/login/oauth/gitea/callback
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import secrets
|
||||||
|
import time
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
STATE_TTL = 600 # 10 minutes
|
||||||
|
|
||||||
|
# state_token -> expiry timestamp
|
||||||
|
_states: dict[str, float] = {}
|
||||||
|
|
||||||
|
|
||||||
|
def make_state() -> str:
|
||||||
|
"""Generate a CSRF state token, store it with TTL, and return it."""
|
||||||
|
_purge_states()
|
||||||
|
token = secrets.token_hex(32)
|
||||||
|
_states[token] = time.time() + STATE_TTL
|
||||||
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
def validate_state(state: str) -> bool:
|
||||||
|
"""Return True if *state* is known and unexpired; always removes it."""
|
||||||
|
expiry = _states.pop(state, None)
|
||||||
|
if expiry is None:
|
||||||
|
return False
|
||||||
|
return time.time() < expiry
|
||||||
|
|
||||||
|
|
||||||
|
def _purge_states() -> None:
|
||||||
|
"""Remove all expired CSRF state tokens from the in-memory store."""
|
||||||
|
now = time.time()
|
||||||
|
expired = [k for k, exp in list(_states.items()) if exp < now]
|
||||||
|
for k in expired:
|
||||||
|
del _states[k]
|
||||||
|
|
||||||
|
|
||||||
|
class OAuthError(Exception):
|
||||||
|
"""Raised when the OAuth2 flow fails for any reason."""
|
||||||
|
|
||||||
|
|
||||||
|
def _gitea_cfg(config: dict) -> dict:
|
||||||
|
"""Return the gitea sub-dict or {} if absent/incomplete."""
|
||||||
|
return config.get("oauth", {}).get("gitea", {})
|
||||||
|
|
||||||
|
|
||||||
|
def is_enabled(config: dict) -> bool:
|
||||||
|
"""Return True when all three required Gitea OAuth keys are present."""
|
||||||
|
g = _gitea_cfg(config)
|
||||||
|
return bool(g.get("url") and g.get("client_id") and g.get("client_secret"))
|
||||||
|
|
||||||
|
|
||||||
|
def authorization_url(config: dict, state: str, redirect_uri: str) -> str:
|
||||||
|
"""Return the Gitea OAuth2 authorization URL to redirect the browser to."""
|
||||||
|
g = _gitea_cfg(config)
|
||||||
|
if not (g.get("url") and g.get("client_id") and g.get("client_secret")):
|
||||||
|
raise OAuthError("Gitea OAuth2 is not configured")
|
||||||
|
params = urllib.parse.urlencode({
|
||||||
|
"client_id": g["client_id"],
|
||||||
|
"redirect_uri": redirect_uri,
|
||||||
|
"response_type": "code",
|
||||||
|
"scope": "user:email",
|
||||||
|
"state": state,
|
||||||
|
})
|
||||||
|
return f"{g['url'].rstrip('/')}/login/oauth/authorize?{params}"
|
||||||
|
|
||||||
|
|
||||||
|
async def exchange_code(config: dict, code: str, redirect_uri: str) -> str:
|
||||||
|
"""Exchange an authorization *code* for a Gitea access token.
|
||||||
|
|
||||||
|
Returns the access token string. Raises OAuthError on any failure.
|
||||||
|
"""
|
||||||
|
g = _gitea_cfg(config)
|
||||||
|
if not (g.get("url") and g.get("client_id") and g.get("client_secret")):
|
||||||
|
raise OAuthError("Gitea OAuth2 is not configured")
|
||||||
|
url = f"{g['url'].rstrip('/')}/login/oauth/access_token"
|
||||||
|
payload = {
|
||||||
|
"client_id": g["client_id"],
|
||||||
|
"client_secret": g["client_secret"],
|
||||||
|
"code": code,
|
||||||
|
"grant_type": "authorization_code",
|
||||||
|
"redirect_uri": redirect_uri,
|
||||||
|
}
|
||||||
|
timeout = aiohttp.ClientTimeout(total=10)
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||||
|
async with session.post(url, json=payload, headers={"Accept": "application/json"}) as resp:
|
||||||
|
if resp.status != 200:
|
||||||
|
text = await resp.text()
|
||||||
|
raise OAuthError(f"Token exchange failed ({resp.status}): {text}")
|
||||||
|
data = await resp.json()
|
||||||
|
token = data.get("access_token")
|
||||||
|
if not token:
|
||||||
|
raise OAuthError(f"No access_token in response: {data}")
|
||||||
|
except aiohttp.ClientError as exc:
|
||||||
|
raise OAuthError(f"Token exchange network error: {exc}") from exc
|
||||||
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
async def fetch_user(config: dict, token: str) -> dict:
|
||||||
|
"""Fetch the authenticated user's profile from Gitea.
|
||||||
|
|
||||||
|
Returns a dict with keys: login, full_name, avatar_url.
|
||||||
|
Raises OAuthError on any failure.
|
||||||
|
"""
|
||||||
|
g = _gitea_cfg(config)
|
||||||
|
if not (g.get("url") and g.get("client_id") and g.get("client_secret")):
|
||||||
|
raise OAuthError("Gitea OAuth2 is not configured")
|
||||||
|
url = f"{g['url'].rstrip('/')}/api/v1/user"
|
||||||
|
timeout = aiohttp.ClientTimeout(total=10)
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||||
|
async with session.get(url, headers={"Authorization": f"token {token}"}) as resp:
|
||||||
|
if resp.status != 200:
|
||||||
|
text = await resp.text()
|
||||||
|
raise OAuthError(f"User fetch failed ({resp.status}): {text}")
|
||||||
|
data = await resp.json()
|
||||||
|
except aiohttp.ClientError as exc:
|
||||||
|
raise OAuthError(f"User fetch network error: {exc}") from exc
|
||||||
|
return {
|
||||||
|
"login": data.get("login", ""),
|
||||||
|
"full_name": data.get("full_name", ""),
|
||||||
|
"avatar_url": data.get("avatar_url", ""),
|
||||||
|
}
|
||||||
@@ -94,6 +94,24 @@
|
|||||||
border-color: #2196f3;
|
border-color: #2196f3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.filter-input {
|
||||||
|
padding: 7px 12px;
|
||||||
|
border: 2px solid #ddd;
|
||||||
|
border-radius: 20px;
|
||||||
|
font-size: 0.9em;
|
||||||
|
outline: none;
|
||||||
|
width: 200px;
|
||||||
|
transition: border-color 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.filter-input:focus {
|
||||||
|
border-color: #2196f3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.filter-input.invalid {
|
||||||
|
border-color: #f44336;
|
||||||
|
}
|
||||||
|
|
||||||
.alerts-container {
|
.alerts-container {
|
||||||
background: white;
|
background: white;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
@@ -316,6 +334,7 @@
|
|||||||
<button class="filter-button active" onclick="filterAlerts('all')">All</button>
|
<button class="filter-button active" onclick="filterAlerts('all')">All</button>
|
||||||
<button class="filter-button" onclick="filterAlerts('critical')">Critical Only</button>
|
<button class="filter-button" onclick="filterAlerts('critical')">Critical Only</button>
|
||||||
<button class="filter-button" onclick="filterAlerts('warning')">Warning Only</button>
|
<button class="filter-button" onclick="filterAlerts('warning')">Warning Only</button>
|
||||||
|
<input id="host-filter" class="filter-input" type="text" placeholder="host filter (regex)" oninput="onHostFilterInput(this)">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="alerts-container">
|
<div class="alerts-container">
|
||||||
@@ -332,6 +351,7 @@
|
|||||||
<script>
|
<script>
|
||||||
let currentFilter = 'all';
|
let currentFilter = 'all';
|
||||||
let allAlerts = [];
|
let allAlerts = [];
|
||||||
|
let hostFilterRe = null;
|
||||||
|
|
||||||
async function loadAlerts() {
|
async function loadAlerts() {
|
||||||
try {
|
try {
|
||||||
@@ -366,10 +386,13 @@
|
|||||||
// Filter alerts based on current filter
|
// Filter alerts based on current filter
|
||||||
let filteredAlerts = alerts;
|
let filteredAlerts = alerts;
|
||||||
if (currentFilter !== 'all') {
|
if (currentFilter !== 'all') {
|
||||||
filteredAlerts = alerts.filter(alert =>
|
filteredAlerts = filteredAlerts.filter(alert =>
|
||||||
alert.level.toLowerCase() === currentFilter
|
alert.level.toLowerCase() === currentFilter
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if (hostFilterRe) {
|
||||||
|
filteredAlerts = filteredAlerts.filter(alert => hostFilterRe.test(alert.hostname));
|
||||||
|
}
|
||||||
|
|
||||||
if (filteredAlerts.length === 0) {
|
if (filteredAlerts.length === 0) {
|
||||||
if (currentFilter === 'all' && alerts.length === 0) {
|
if (currentFilter === 'all' && alerts.length === 0) {
|
||||||
@@ -538,9 +561,36 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function onHostFilterInput(input) {
|
||||||
|
const val = input.value.trim();
|
||||||
|
if (!val) {
|
||||||
|
hostFilterRe = null;
|
||||||
|
input.classList.remove('invalid');
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
hostFilterRe = new RegExp(val, 'i');
|
||||||
|
input.classList.remove('invalid');
|
||||||
|
} catch (_) {
|
||||||
|
hostFilterRe = null;
|
||||||
|
input.classList.add('invalid');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
renderAlerts(allAlerts);
|
||||||
|
}
|
||||||
|
|
||||||
// Auto-refresh every 15 seconds
|
// Auto-refresh every 15 seconds
|
||||||
setInterval(loadAlerts, 15000);
|
setInterval(loadAlerts, 15000);
|
||||||
|
|
||||||
|
// Initialise filter from URL query string (?filter=...)
|
||||||
|
(function () {
|
||||||
|
const param = new URLSearchParams(window.location.search).get('filter');
|
||||||
|
if (param) {
|
||||||
|
const input = document.getElementById('host-filter');
|
||||||
|
input.value = param;
|
||||||
|
onHostFilterInput(input);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
// Initial load
|
// Initial load
|
||||||
loadAlerts();
|
loadAlerts();
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -416,7 +416,8 @@
|
|||||||
<span class="host-name">{{ host.name }}</span>
|
<span class="host-name">{{ host.name }}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="glance-strip" id="glance-{{ host.name }}">
|
<div class="glance-strip" id="glance-{{ host.name }}" data-owner="{{ host.owner or '' }}">
|
||||||
|
{% if current_user and current_user.admin and host.owner %}<span class="glance-chip neutral">{{ host.owner }}</span>{% endif %}
|
||||||
<span class="glance-loading">—</span>
|
<span class="glance-loading">—</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -480,6 +481,7 @@
|
|||||||
const GLANCE_PLUGINS = ['cpu_monitor','memory_monitor','disk_monitor',
|
const GLANCE_PLUGINS = ['cpu_monitor','memory_monitor','disk_monitor',
|
||||||
'network_monitor','nagios_runner','os_info'];
|
'network_monitor','nagios_runner','os_info'];
|
||||||
const SKIP_FIELDS = new Set(['id','name']);
|
const SKIP_FIELDS = new Set(['id','name']);
|
||||||
|
const CURRENT_USER_ADMIN = {{ 'true' if current_user and current_user.admin else 'false' }};
|
||||||
|
|
||||||
// ── Cache ───────────────────────────────────────────────────────────────
|
// ── Cache ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
@@ -558,6 +560,12 @@
|
|||||||
|
|
||||||
const chips = [];
|
const chips = [];
|
||||||
|
|
||||||
|
// Owner (admin only, static from server)
|
||||||
|
const owner = strip.dataset.owner;
|
||||||
|
if (CURRENT_USER_ADMIN && owner) {
|
||||||
|
chips.push(`<span class="glance-chip neutral">${owner}</span>`);
|
||||||
|
}
|
||||||
|
|
||||||
// CPU
|
// CPU
|
||||||
const cpu = getCache(hostname, 'cpu_monitor');
|
const cpu = getCache(hostname, 'cpu_monitor');
|
||||||
if (cpu) {
|
if (cpu) {
|
||||||
|
|||||||
+106
-4
@@ -575,10 +575,13 @@ class ThresholdChecker:
|
|||||||
if not isinstance(threshold_config, dict):
|
if not isinstance(threshold_config, dict):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Handle nested metrics (e.g., partitions./.percent)
|
# Handle nested metrics (e.g., partitions./.percent or pools.*.status)
|
||||||
if metric_name == "partitions":
|
if metric_name == "partitions":
|
||||||
self._parse_partition_thresholds(plugin_name, threshold_config, target_dict)
|
self._parse_partition_thresholds(plugin_name, threshold_config, target_dict)
|
||||||
continue
|
continue
|
||||||
|
if metric_name == "pools":
|
||||||
|
self._parse_pool_thresholds(plugin_name, threshold_config, target_dict)
|
||||||
|
continue
|
||||||
|
|
||||||
metric_path = f"{plugin_name}.{metric_name}"
|
metric_path = f"{plugin_name}.{metric_name}"
|
||||||
|
|
||||||
@@ -663,7 +666,57 @@ class ThresholdChecker:
|
|||||||
)
|
)
|
||||||
|
|
||||||
target_dict[metric_path] = threshold
|
target_dict[metric_path] = threshold
|
||||||
|
|
||||||
|
def _parse_pool_thresholds(
|
||||||
|
self,
|
||||||
|
plugin_name: str,
|
||||||
|
pools: Dict[str, Any],
|
||||||
|
target_dict: Optional[Dict[str, ThresholdConfig]] = None,
|
||||||
|
):
|
||||||
|
"""Parse ZFS pool thresholds. Pool names may be literal or '*' (all pools).
|
||||||
|
|
||||||
|
Config shape::
|
||||||
|
|
||||||
|
zfs_monitor:
|
||||||
|
pools:
|
||||||
|
'*':
|
||||||
|
status:
|
||||||
|
warning: 1
|
||||||
|
critical: 2
|
||||||
|
operator: '>'
|
||||||
|
tank:
|
||||||
|
capacity:
|
||||||
|
warning: 80
|
||||||
|
critical: 90
|
||||||
|
"""
|
||||||
|
if target_dict is None:
|
||||||
|
target_dict = self.thresholds
|
||||||
|
|
||||||
|
for pool_name, metrics in pools.items():
|
||||||
|
if not isinstance(metrics, dict):
|
||||||
|
continue
|
||||||
|
for metric_name, threshold_config in metrics.items():
|
||||||
|
if not isinstance(threshold_config, dict):
|
||||||
|
continue
|
||||||
|
metric_path = f"{plugin_name}.{pool_name}.{metric_name}"
|
||||||
|
warning = threshold_config.get("warning")
|
||||||
|
critical = threshold_config.get("critical")
|
||||||
|
operator = threshold_config.get("operator", ">")
|
||||||
|
hysteresis = threshold_config.get("hysteresis", 0.02)
|
||||||
|
enabled = threshold_config.get("enabled", True)
|
||||||
|
display = threshold_config.get("display")
|
||||||
|
if warning is None and critical is None:
|
||||||
|
continue
|
||||||
|
target_dict[metric_path] = ThresholdConfig(
|
||||||
|
metric_path=metric_path,
|
||||||
|
warning=warning,
|
||||||
|
critical=critical,
|
||||||
|
operator=operator,
|
||||||
|
hysteresis=hysteresis,
|
||||||
|
enabled=enabled,
|
||||||
|
display=display,
|
||||||
|
)
|
||||||
|
|
||||||
def _parse_rtt_thresholds(
|
def _parse_rtt_thresholds(
|
||||||
self,
|
self,
|
||||||
rtt_thresholds: Dict[str, Any],
|
rtt_thresholds: Dict[str, Any],
|
||||||
@@ -967,6 +1020,44 @@ class ThresholdChecker:
|
|||||||
# Get host-specific thresholds
|
# Get host-specific thresholds
|
||||||
thresholds = self.get_thresholds_for_host(host_name)
|
thresholds = self.get_thresholds_for_host(host_name)
|
||||||
|
|
||||||
|
# ZFS pool health checks
|
||||||
|
if plugin_name == "zfs_monitor" and "pools" in data:
|
||||||
|
pools = data["pools"]
|
||||||
|
if isinstance(pools, dict):
|
||||||
|
for pool_name, pool_metrics in pools.items():
|
||||||
|
if not isinstance(pool_metrics, dict):
|
||||||
|
continue
|
||||||
|
# Synthesize status from health string for older clients
|
||||||
|
# that predate the status field.
|
||||||
|
pool_metrics_effective = dict(pool_metrics)
|
||||||
|
if "health" in pool_metrics and "status" not in pool_metrics:
|
||||||
|
pool_metrics_effective["status"] = 0 if pool_metrics["health"] == "ONLINE" else 1
|
||||||
|
for metric_name, value in pool_metrics_effective.items():
|
||||||
|
# Try specific pool name first, then wildcard '*'
|
||||||
|
metric_path = f"{plugin_name}.{pool_name}.{metric_name}"
|
||||||
|
wildcard_path = f"{plugin_name}.*.{metric_name}"
|
||||||
|
threshold = thresholds.get(metric_path) or thresholds.get(wildcard_path)
|
||||||
|
if threshold is None:
|
||||||
|
continue
|
||||||
|
if metric_path not in alert_states:
|
||||||
|
alert_states[metric_path] = AlertState(metric_path)
|
||||||
|
alert_state = alert_states[metric_path]
|
||||||
|
new_level = threshold.evaluate_with_hysteresis(value, alert_state.level)
|
||||||
|
threshold_value = None
|
||||||
|
if new_level == AlertLevel.CRITICAL and threshold.critical is not None:
|
||||||
|
threshold_value = threshold.critical
|
||||||
|
elif new_level == AlertLevel.WARNING and threshold.warning is not None:
|
||||||
|
threshold_value = threshold.warning
|
||||||
|
alert_state.hysteresis = threshold.hysteresis if new_level != AlertLevel.OK else None
|
||||||
|
pool_context = dict(pool_metrics_effective)
|
||||||
|
pool_context["pool_name"] = pool_name
|
||||||
|
old_level = alert_state.level
|
||||||
|
if alert_state.update(new_level, value, threshold_value, threshold.operator.value):
|
||||||
|
state_changes.append((metric_path, old_level, new_level, value))
|
||||||
|
self._apply_grace(host_name, alert_state, metric_path, old_level, new_level, value, threshold, pool_context, metric_name=pool_name)
|
||||||
|
elif new_level != AlertLevel.OK:
|
||||||
|
self._check_pending_or_renotify(host_name, alert_state, metric_path, value, threshold, pool_context, metric_name=pool_name)
|
||||||
|
|
||||||
# Look for partition data in disk_monitor
|
# Look for partition data in disk_monitor
|
||||||
if plugin_name == "disk_monitor" and "partitions" in data:
|
if plugin_name == "disk_monitor" and "partitions" in data:
|
||||||
partitions = data["partitions"]
|
partitions = data["partitions"]
|
||||||
@@ -1302,6 +1393,17 @@ class ThresholdChecker:
|
|||||||
else:
|
else:
|
||||||
self._check_renotify(host_name, alert_state, metric_path, value, threshold, plugin_data, check_name=check_name, metric_name=metric_name)
|
self._check_renotify(host_name, alert_state, metric_path, value, threshold, plugin_data, check_name=check_name, metric_name=metric_name)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _human_duration(seconds: float) -> str:
|
||||||
|
s = int(seconds)
|
||||||
|
if s < 120:
|
||||||
|
return f"{s}s"
|
||||||
|
if s < 3600:
|
||||||
|
return f"{s // 60}m {s % 60}s"
|
||||||
|
h, rem = divmod(s, 3600)
|
||||||
|
m = rem // 60
|
||||||
|
return f"{h}h {m}m" if m else f"{h}h"
|
||||||
|
|
||||||
def _check_renotify(
|
def _check_renotify(
|
||||||
self,
|
self,
|
||||||
host_name: str,
|
host_name: str,
|
||||||
@@ -1363,9 +1465,9 @@ class ThresholdChecker:
|
|||||||
check_name=check_name,
|
check_name=check_name,
|
||||||
metric_name=metric_name,
|
metric_name=metric_name,
|
||||||
)
|
)
|
||||||
body = f"{value} {threshold_info}, ongoing for {int(now - alert_state.since)}s"
|
body = f"{value} {threshold_info}, ongoing for {self._human_duration(now - alert_state.since)}"
|
||||||
else:
|
else:
|
||||||
body = f"{value} (ongoing for {int(now - alert_state.since)}s)"
|
body = f"{value} (ongoing for {self._human_duration(now - alert_state.since)})"
|
||||||
message = f"REMINDER ({alert_state.level.name}): {host_name} - {short_path} = {body}"
|
message = f"REMINDER ({alert_state.level.name}): {host_name} - {short_path} = {body}"
|
||||||
|
|
||||||
from . import hbdclass
|
from . import hbdclass
|
||||||
|
|||||||
+5
-2
@@ -373,8 +373,11 @@ def handle_datagram(msg: dict, addr, transport, ctx: dict):
|
|||||||
|
|
||||||
# If os_info reports an owner and none is configured server-side, apply it
|
# If os_info reports an owner and none is configured server-side, apply it
|
||||||
if plugin_name == "os_info":
|
if plugin_name == "os_info":
|
||||||
if not host.owner:
|
config_owner = config_mod.get_host_access(cfg, uname).get("owner")
|
||||||
host.owner = plugin_data.get("owner", config_mod.get_default_owner(cfg))
|
default_owner = config_mod.get_default_owner(cfg)
|
||||||
|
inferred_owner = plugin_data.get("owner", config_owner or default_owner)
|
||||||
|
host.owner = inferred_owner
|
||||||
|
logger.info(f"owner for {uname} is '{host.owner}")
|
||||||
if DEBUG > 1:
|
if DEBUG > 1:
|
||||||
print(f"Stored plugin data for {uname}: {plugin_name}")
|
print(f"Stored plugin data for {uname}: {plugin_name}")
|
||||||
|
|
||||||
|
|||||||
@@ -146,9 +146,14 @@ def load_users(config: dict) -> dict:
|
|||||||
Returns the new ``users`` dict.
|
Returns the new ``users`` dict.
|
||||||
"""
|
"""
|
||||||
global users
|
global users
|
||||||
|
old_users = dict(users) # snapshot before rebuild
|
||||||
users_cfg = config.get("users", {})
|
users_cfg = config.get("users", {})
|
||||||
if not isinstance(users_cfg, dict):
|
if not isinstance(users_cfg, dict):
|
||||||
users = {}
|
users = {}
|
||||||
|
# Preserve OAuth-provisioned users (password_hash == "") that aren't in config.
|
||||||
|
for username, existing_user in old_users.items():
|
||||||
|
if not existing_user.password_hash and username not in users:
|
||||||
|
users[username] = existing_user
|
||||||
return users
|
return users
|
||||||
|
|
||||||
result: dict = {}
|
result: dict = {}
|
||||||
@@ -166,6 +171,10 @@ def load_users(config: dict) -> dict:
|
|||||||
)
|
)
|
||||||
|
|
||||||
users = result
|
users = result
|
||||||
|
# Preserve OAuth-provisioned users (password_hash == "") that aren't in config.
|
||||||
|
for username, existing_user in old_users.items():
|
||||||
|
if not existing_user.password_hash and username not in users:
|
||||||
|
users[username] = existing_user
|
||||||
logger.info("Loaded %d user(s) from config", len(users))
|
logger.info("Loaded %d user(s) from config", len(users))
|
||||||
return users
|
return users
|
||||||
|
|
||||||
@@ -187,6 +196,26 @@ def authenticate(username: str, password: str) -> "User | None":
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def provision_oauth_user(username: str, full_name: str, avatar: str) -> "User":
|
||||||
|
"""Create or update a user sourced from an OAuth2 provider.
|
||||||
|
|
||||||
|
New users are inserted with no password_hash — they can only authenticate
|
||||||
|
via OAuth. Existing users (e.g. defined in config with a password) have
|
||||||
|
their display name and avatar refreshed; all other attributes are preserved.
|
||||||
|
"""
|
||||||
|
user = users.get(username)
|
||||||
|
if user is None:
|
||||||
|
user = User(username=username, full_name=full_name, avatar=avatar)
|
||||||
|
users[username] = user
|
||||||
|
logger.info("Provisioned OAuth user %r", username)
|
||||||
|
else:
|
||||||
|
if full_name:
|
||||||
|
user.full_name = full_name
|
||||||
|
if avatar:
|
||||||
|
user.avatar = avatar
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Session management
|
# Session management
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "hbd"
|
name = "hbd"
|
||||||
version = "5.2.4"
|
version = "5.2.6"
|
||||||
description = "Heartbeat monitoring system — client (hbc) and server (hbd)"
|
description = "Heartbeat monitoring system — client (hbc) and server (hbd)"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.11"
|
requires-python = ">=3.11"
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
hbc_mini
|
||||||
|
hbc_mini_dbg
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
CC ?= cc
|
||||||
|
CFLAGS = -O2 -Wall -Wextra -std=c11
|
||||||
|
LDFLAGS = -lz -lpthread -lm
|
||||||
|
TARGET = hbc_mini
|
||||||
|
SRC = hbc_mini.c
|
||||||
|
|
||||||
|
# FreeBSD/NetBSD keep zlib in base; no extra flags needed.
|
||||||
|
# On some NetBSD installs pthreads may need -lpthread from pkgsrc.
|
||||||
|
|
||||||
|
.PHONY: all clean debug
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(SRC)
|
||||||
|
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
|
||||||
|
|
||||||
|
debug: $(SRC)
|
||||||
|
$(CC) -g -fsanitize=address,undefined -o $(TARGET)_dbg $< $(LDFLAGS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(TARGET) $(TARGET)_dbg
|
||||||
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -41,7 +41,7 @@ from pathlib import Path
|
|||||||
from typing import Any, Dict, List, Optional, Tuple
|
from typing import Any, Dict, List, Optional, Tuple
|
||||||
|
|
||||||
# updated by scripts/bumpminor.sh
|
# updated by scripts/bumpminor.sh
|
||||||
__version__ = "5.2.4"
|
__version__ = "5.2.6"
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Protocol (mirrors hbd/common/proto.py)
|
# Protocol (mirrors hbd/common/proto.py)
|
||||||
|
|||||||
@@ -0,0 +1,324 @@
|
|||||||
|
import time as time_mod
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
from urllib.parse import urlparse, parse_qs
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from hbd.server import oauth
|
||||||
|
from hbd.server import users as users_mod
|
||||||
|
from hbd.server.users import User
|
||||||
|
|
||||||
|
|
||||||
|
CFG_OFF = {}
|
||||||
|
CFG_ON = {
|
||||||
|
"oauth": {
|
||||||
|
"gitea": {
|
||||||
|
"url": "https://git.example.com",
|
||||||
|
"client_id": "cid",
|
||||||
|
"client_secret": "csec",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CFG_PARTIAL = {"oauth": {"gitea": {"url": "https://git.example.com"}}}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def clear_oauth_states():
|
||||||
|
oauth._states.clear()
|
||||||
|
yield
|
||||||
|
oauth._states.clear()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def reset_users_dict():
|
||||||
|
original = dict(users_mod.users)
|
||||||
|
yield
|
||||||
|
users_mod.users = original
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_enabled_when_all_keys_present():
|
||||||
|
assert oauth.is_enabled(CFG_ON) is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_enabled_false_when_no_oauth_key():
|
||||||
|
assert oauth.is_enabled(CFG_OFF) is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_enabled_false_when_partial_config():
|
||||||
|
assert oauth.is_enabled(CFG_PARTIAL) is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_state_returns_unique_tokens():
|
||||||
|
s1 = oauth.make_state()
|
||||||
|
s2 = oauth.make_state()
|
||||||
|
assert s1 != s2
|
||||||
|
assert len(s1) == 64 # 32 bytes hex
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_state_valid():
|
||||||
|
state = oauth.make_state()
|
||||||
|
assert oauth.validate_state(state) is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_state_consumed_on_use():
|
||||||
|
state = oauth.make_state()
|
||||||
|
oauth.validate_state(state)
|
||||||
|
assert oauth.validate_state(state) is False # replay rejected
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_state_unknown():
|
||||||
|
assert oauth.validate_state("notastate") is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_state_expired(monkeypatch):
|
||||||
|
state = oauth.make_state()
|
||||||
|
# Wind expiry into the past
|
||||||
|
monkeypatch.setitem(oauth._states, state, time_mod.time() - 1000)
|
||||||
|
assert oauth.validate_state(state) is False
|
||||||
|
|
||||||
|
|
||||||
|
def _reset_users(entries=None):
|
||||||
|
users_mod.users = entries or {}
|
||||||
|
|
||||||
|
|
||||||
|
def test_provision_oauth_user_new():
|
||||||
|
_reset_users()
|
||||||
|
user = users_mod.provision_oauth_user("gituser", "Git User", "https://example.com/avatar.png")
|
||||||
|
assert user.username == "gituser"
|
||||||
|
assert user.full_name == "Git User"
|
||||||
|
assert user.avatar == "https://example.com/avatar.png"
|
||||||
|
assert user.admin is False
|
||||||
|
assert user.password_hash == ""
|
||||||
|
assert "gituser" in users_mod.users
|
||||||
|
|
||||||
|
|
||||||
|
def test_provision_oauth_user_no_password_login():
|
||||||
|
_reset_users()
|
||||||
|
user = users_mod.provision_oauth_user("gituser", "Git User", "")
|
||||||
|
assert user.check_password("anything") is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_provision_oauth_user_existing_updates_profile():
|
||||||
|
existing = User(
|
||||||
|
username="alice",
|
||||||
|
full_name="Old Name",
|
||||||
|
avatar="old.png",
|
||||||
|
password_hash="pbkdf2:sha256:1:salt:abc",
|
||||||
|
admin=True,
|
||||||
|
notification_channels=["chan1"],
|
||||||
|
)
|
||||||
|
_reset_users({"alice": existing})
|
||||||
|
user = users_mod.provision_oauth_user("alice", "New Name", "new.png")
|
||||||
|
assert user.full_name == "New Name"
|
||||||
|
assert user.avatar == "new.png"
|
||||||
|
# Preserved
|
||||||
|
assert user.admin is True
|
||||||
|
assert user.password_hash == "pbkdf2:sha256:1:salt:abc"
|
||||||
|
assert user.notification_channels == ["chan1"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_provision_oauth_user_does_not_overwrite_with_empty():
|
||||||
|
existing = User(username="bob", full_name="Bob", avatar="bob.png")
|
||||||
|
_reset_users({"bob": existing})
|
||||||
|
user = users_mod.provision_oauth_user("bob", "", "")
|
||||||
|
assert user.full_name == "Bob"
|
||||||
|
assert user.avatar == "bob.png"
|
||||||
|
|
||||||
|
|
||||||
|
def test_provision_oauth_user_survives_config_reload():
|
||||||
|
_reset_users()
|
||||||
|
users_mod.provision_oauth_user("oauthonly", "OAuth Only", "https://example.com/a.png")
|
||||||
|
assert "oauthonly" in users_mod.users
|
||||||
|
# Reload with empty config — OAuth user should survive
|
||||||
|
users_mod.load_users({})
|
||||||
|
assert "oauthonly" in users_mod.users
|
||||||
|
|
||||||
|
|
||||||
|
def test_authorization_url_shape():
|
||||||
|
state = "teststate"
|
||||||
|
redirect_uri = "https://hbd.example.com/login/oauth/gitea/callback"
|
||||||
|
url = oauth.authorization_url(CFG_ON, state, redirect_uri)
|
||||||
|
parsed = urlparse(url)
|
||||||
|
qs = parse_qs(parsed.query)
|
||||||
|
assert parsed.scheme == "https"
|
||||||
|
assert parsed.netloc == "git.example.com"
|
||||||
|
assert parsed.path == "/login/oauth/authorize"
|
||||||
|
assert qs["client_id"] == ["cid"]
|
||||||
|
assert qs["state"] == ["teststate"]
|
||||||
|
assert qs["redirect_uri"] == [redirect_uri]
|
||||||
|
assert qs["scope"] == ["user:email"]
|
||||||
|
assert qs["response_type"] == ["code"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_exchange_code_returns_token():
|
||||||
|
redirect_uri = "https://hbd.example.com/login/oauth/gitea/callback"
|
||||||
|
mock_response = AsyncMock()
|
||||||
|
mock_response.status = 200
|
||||||
|
mock_response.json = AsyncMock(return_value={"access_token": "tok123"})
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.post = MagicMock(return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_response),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
))
|
||||||
|
|
||||||
|
with patch("hbd.server.oauth.aiohttp.ClientSession", return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_session),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
)):
|
||||||
|
token = await oauth.exchange_code(CFG_ON, "mycode", redirect_uri)
|
||||||
|
assert token == "tok123"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_exchange_code_raises_on_error_status():
|
||||||
|
redirect_uri = "https://hbd.example.com/login/oauth/gitea/callback"
|
||||||
|
mock_response = AsyncMock()
|
||||||
|
mock_response.status = 401
|
||||||
|
mock_response.text = AsyncMock(return_value="unauthorized")
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.post = MagicMock(return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_response),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
))
|
||||||
|
|
||||||
|
with patch("hbd.server.oauth.aiohttp.ClientSession", return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_session),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
)):
|
||||||
|
with pytest.raises(oauth.OAuthError):
|
||||||
|
await oauth.exchange_code(CFG_ON, "badcode", redirect_uri)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_fetch_user_returns_profile():
|
||||||
|
mock_response = AsyncMock()
|
||||||
|
mock_response.status = 200
|
||||||
|
mock_response.json = AsyncMock(return_value={
|
||||||
|
"login": "alice",
|
||||||
|
"full_name": "Alice Smith",
|
||||||
|
"avatar_url": "https://git.example.com/avatars/alice.png",
|
||||||
|
})
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.get = MagicMock(return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_response),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
))
|
||||||
|
|
||||||
|
with patch("hbd.server.oauth.aiohttp.ClientSession", return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_session),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
)):
|
||||||
|
profile = await oauth.fetch_user(CFG_ON, "tok123")
|
||||||
|
assert profile == {
|
||||||
|
"login": "alice",
|
||||||
|
"full_name": "Alice Smith",
|
||||||
|
"avatar_url": "https://git.example.com/avatars/alice.png",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_exchange_code_raises_when_no_access_token():
|
||||||
|
redirect_uri = "https://hbd.example.com/login/oauth/gitea/callback"
|
||||||
|
mock_response = AsyncMock()
|
||||||
|
mock_response.status = 200
|
||||||
|
mock_response.json = AsyncMock(return_value={"error": "bad_request"})
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.post = MagicMock(return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_response),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
))
|
||||||
|
|
||||||
|
with patch("hbd.server.oauth.aiohttp.ClientSession", return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_session),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
)):
|
||||||
|
with pytest.raises(oauth.OAuthError):
|
||||||
|
await oauth.exchange_code(CFG_ON, "mycode", redirect_uri)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_fetch_user_raises_on_error_status():
|
||||||
|
mock_response = AsyncMock()
|
||||||
|
mock_response.status = 401
|
||||||
|
mock_response.text = AsyncMock(return_value="unauthorized")
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.get = MagicMock(return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_response),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
))
|
||||||
|
|
||||||
|
with patch("hbd.server.oauth.aiohttp.ClientSession", return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_session),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
)):
|
||||||
|
with pytest.raises(oauth.OAuthError):
|
||||||
|
await oauth.fetch_user(CFG_ON, "tok123")
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Integration-style tests: callback logic chain
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_callback_invalid_state_rejects():
|
||||||
|
"""Verify validate_state returns False for unknown state tokens."""
|
||||||
|
fake_state = "this-is-not-a-real-state"
|
||||||
|
assert oauth.validate_state(fake_state) is False
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_full_oauth_flow_chain():
|
||||||
|
"""Integration-style test: state → exchange → fetch → provision chain."""
|
||||||
|
redirect_uri = "https://hbd.example.com/login/oauth/gitea/callback"
|
||||||
|
|
||||||
|
# Step 1: create a state token
|
||||||
|
state = oauth.make_state()
|
||||||
|
assert oauth.validate_state(state) is True # consumed; replay would return False
|
||||||
|
|
||||||
|
# Step 2: exchange code → token (mocked)
|
||||||
|
mock_token_response = AsyncMock()
|
||||||
|
mock_token_response.status = 200
|
||||||
|
mock_token_response.json = AsyncMock(return_value={"access_token": "flow_token"})
|
||||||
|
|
||||||
|
mock_user_response = AsyncMock()
|
||||||
|
mock_user_response.status = 200
|
||||||
|
mock_user_response.json = AsyncMock(return_value={
|
||||||
|
"login": "flowuser",
|
||||||
|
"full_name": "Flow User",
|
||||||
|
"avatar_url": "https://git.example.com/avatars/flow.png",
|
||||||
|
})
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.post = MagicMock(return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_token_response),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
))
|
||||||
|
mock_session.get = MagicMock(return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_user_response),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
))
|
||||||
|
|
||||||
|
with patch("hbd.server.oauth.aiohttp.ClientSession", return_value=AsyncMock(
|
||||||
|
__aenter__=AsyncMock(return_value=mock_session),
|
||||||
|
__aexit__=AsyncMock(return_value=False),
|
||||||
|
)):
|
||||||
|
token = await oauth.exchange_code(CFG_ON, "authcode", redirect_uri)
|
||||||
|
profile = await oauth.fetch_user(CFG_ON, token)
|
||||||
|
|
||||||
|
assert token == "flow_token"
|
||||||
|
assert profile["login"] == "flowuser"
|
||||||
|
|
||||||
|
# Step 3: provision user
|
||||||
|
_reset_users()
|
||||||
|
user = users_mod.provision_oauth_user(
|
||||||
|
profile["login"], profile["full_name"], profile["avatar_url"]
|
||||||
|
)
|
||||||
|
assert user.username == "flowuser"
|
||||||
|
assert user.check_password("anything") is False
|
||||||
Reference in New Issue
Block a user