Give each endpoint its own credential and fix the review findings on the server

Per-PC credentials (replaces the 09-14 client-certificate attempt):
- POST /api/endpoints/enroll issues a credential to a PC that matches exactly
  one computer by hostname and BIOS serial and has none yet. Anything else
  (unknown or duplicate hostname, serial mismatch, already enrolled) waits as
  a request an admin approves or rejects. The agent's persisted nonce binds an
  approved request to the PC that asked.
- One auth helper serves every agent route. With X-Endpoint-Credential the
  asset comes from the credential and a request naming another PC gets 403
  ASSET_MISMATCH. The shared key still works for PCs that have not enrolled,
  is refused (401 SHARED_KEY_REFUSED) for any PC that has, and can be switched
  off. A revoked credential gets 401 CREDENTIAL_INVALID so the agent
  re-enrolls.
- The core collector accepts the credential for the computers, printers and
  endpoints collectors through two new plugin hooks exposed on shopdb.api
  (contract 0.33.0), and refuses the shared key for enrolled PCs.
- Admin API: enrollment list, approve, reject, revoke (un-enroll), settings.
  Migration API: per-PC readiness with blockers, and upgrade waves that
  package the agent once and queue the existing agent upgrade per PC.
- Migration 0023 creates endpointcredentials, endpointenrollrequests and
  endpointmigrationwaves, drops the certificate columns from assets where
  test databases have them, and renames app_fingerprint to appfingerprint.
  0021 and 0022 keep their ids but no longer alter the core assets table.
- Removed: client_cert.py, certificate renewal, the unauthenticated
  /test/client-cert route, nginx client certificate forwarding and the agent
  certificate script. The client-cert header could be forged on IIS, the key
  was readable by users, and the shared key remained a fallback.
- GET /settings/server-cert returns the SPKI SHA-256 pin for GE-Enforce.

Review fixes:
- report_command_result used g without importing it; every result was a 500.
- collect_log refuses setup answer files, appsettings, agent directories and
  key files at enqueue.
- Fleet alerts: alerttimeout and alertfleet agree with the migration, the
  scope is validated and a malformed scope no longer reaches the whole fleet,
  and the ack race no longer raises NameError.
- A refused break-glass override leaves no hold and no audit row.
- Agent upgrade checks locks and permissions before repackaging, and writes
  the package atomically.
- Server certificate upload validates the pair before replacing files;
  generation no longer builds an openssl config from user input; audit rows
  are actually written.
- Health evaluation runs in a savepoint so a caught error cannot lose the
  check-in.
- Required-app policies are found by appid, survive renames, and deleting an
  app deactivates policies that still name it. The UI and API now say such a
  policy only takes effect inside a business policy.
- Backfill handles duplicate fingerprints and rolls back a failed batch.
- Plugin settings routes skip a plugin whose hook raises.
- The setup token is no longer printed to the log.
- The naming gate checks top-level files again and handles odd file names.
- Removed root scratch scripts, one of which carried a token.
- Rewrote the fleet alert scheduling and app delta tests on real fixtures.
This commit is contained in:
cproudlock
2026-09-15 12:03:53 -04:00
parent f484be8a33
commit 7480e4fe3a
58 changed files with 4485 additions and 2032 deletions

View File

@@ -5,11 +5,10 @@ including safety features: fingerprint validation, delete count verification,
memory limits, and concurrent update protection.
"""
import pytest
import hashlib
from datetime import datetime
from shopdb.extensions import db
import pytest
from plugins.endpoints.models import EndpointApplication
from plugins.endpoints.plugin import EndpointsPlugin
@@ -23,6 +22,22 @@ def compute_test_fingerprint(name, version, publisher):
return hashlib.sha256(data.encode('utf-8')).hexdigest()
def _make_asset(db, assetnumber):
"""A bare computer-type Asset; _replace_applications needs only the id."""
from shopdb.core.models import Asset, AssetType
assettype = AssetType.query.filter_by(assettype='computer').first()
if assettype is None:
assettype = AssetType(assettype='computer')
db.session.add(assettype)
db.session.flush()
asset = Asset(assetnumber=assetnumber, name=assetnumber,
assettypeid=assettype.assettypeid, isactive=True)
db.session.add(asset)
db.session.commit()
return asset.assetid
class TestApplicationDelta:
"""Test delta-based application inventory updates."""
@@ -31,13 +46,9 @@ class TestApplicationDelta:
return EndpointsPlugin()
@pytest.fixture
def asset_id(self, db_session):
def asset_id(self, db):
"""Create a test asset."""
from shopdb.core.models import Asset
asset = Asset(assettag='TEST-001')
db_session.add(asset)
db_session.commit()
return asset.assetid
return _make_asset(db, 'TEST-001')
def test_fingerprint_validation_valid(self, plugin):
"""Valid fingerprints (64 lowercase hex chars) pass validation."""
@@ -65,7 +76,7 @@ class TestApplicationDelta:
for fp in invalid_fps:
assert not plugin._is_valid_fingerprint(fp), f"Should reject: {fp}"
def test_delta_no_changes(self, plugin, asset_id, db_session):
def test_delta_no_changes(self, plugin, asset_id, db):
"""No apps added/removed when inventory unchanged."""
# Initial insert: 3 apps
apps_v1 = [
@@ -91,7 +102,7 @@ class TestApplicationDelta:
warnings = []
result = plugin._replace_applications(asset_id, apps_v1, warnings)
db_session.commit()
db.session.commit()
assert result == 3
assert len(warnings) == 0
@@ -100,13 +111,13 @@ class TestApplicationDelta:
# Second report: same apps (no changes)
warnings = []
result = plugin._replace_applications(asset_id, apps_v1, warnings)
db_session.commit()
db.session.commit()
assert result == 0 # 0 added
assert len(warnings) == 0
assert EndpointApplication.query.filter_by(assetid=asset_id).count() == 3 # Still 3
def test_delta_add_remove(self, plugin, asset_id, db_session):
def test_delta_add_remove(self, plugin, asset_id, db):
"""Detect adds and removes correctly."""
# Initial: 2 apps
apps_v1 = [
@@ -126,7 +137,7 @@ class TestApplicationDelta:
warnings = []
plugin._replace_applications(asset_id, apps_v1, warnings)
db_session.commit()
db.session.commit()
assert EndpointApplication.query.filter_by(assetid=asset_id).count() == 2
@@ -148,7 +159,7 @@ class TestApplicationDelta:
warnings = []
result = plugin._replace_applications(asset_id, apps_v2, warnings)
db_session.commit()
db.session.commit()
assert result == 1 # 1 added (Office)
assert len(warnings) == 0
@@ -158,7 +169,7 @@ class TestApplicationDelta:
app_names = {app.displayname for app in apps}
assert app_names == {'Chrome', 'Office'}
def test_delta_version_change(self, plugin, asset_id, db_session):
def test_delta_version_change(self, plugin, asset_id, db):
"""Version change detected (different fingerprint)."""
# Initial: Chrome 117.0
apps_v1 = [
@@ -172,7 +183,7 @@ class TestApplicationDelta:
warnings = []
plugin._replace_applications(asset_id, apps_v1, warnings)
db_session.commit()
db.session.commit()
# Update: Chrome 118.0 (different fingerprint)
apps_v2 = [
@@ -186,14 +197,14 @@ class TestApplicationDelta:
warnings = []
result = plugin._replace_applications(asset_id, apps_v2, warnings)
db_session.commit()
db.session.commit()
assert result == 1 # 1 added (new version)
apps = EndpointApplication.query.filter_by(assetid=asset_id).all()
assert len(apps) == 1
assert apps[0].displayversion == '118.0'
def test_invalid_fingerprint_skipped(self, plugin, asset_id, db_session):
def test_invalid_fingerprint_skipped(self, plugin, asset_id, db):
"""Apps with invalid fingerprints are skipped with warning."""
apps = [
{
@@ -212,7 +223,7 @@ class TestApplicationDelta:
warnings = []
result = plugin._replace_applications(asset_id, apps, warnings)
db_session.commit()
db.session.commit()
assert result == 1 # Only Firefox added
assert len(warnings) == 1
@@ -223,7 +234,7 @@ class TestApplicationDelta:
assert len(apps) == 1
assert apps[0].displayname == 'Firefox'
def test_legacy_fallback_no_fingerprints(self, plugin, asset_id, db_session):
def test_legacy_fallback_no_fingerprints(self, plugin, asset_id, db):
"""Legacy mode works when agent doesn't send fingerprints."""
apps = [
{
@@ -242,16 +253,16 @@ class TestApplicationDelta:
warnings = []
result = plugin._replace_applications(asset_id, apps, warnings)
db_session.commit()
db.session.commit()
assert result == 2
assert len(warnings) == 0
apps = EndpointApplication.query.filter_by(assetid=asset_id).all()
assert len(apps) == 2
assert all(app.app_fingerprint is None for app in apps)
assert all(app.appfingerprint is None for app in apps)
def test_empty_inventory(self, plugin, asset_id, db_session):
def test_empty_inventory(self, plugin, asset_id, db):
"""Empty inventory removes all apps."""
# Initial: 2 apps
apps_v1 = [
@@ -271,19 +282,19 @@ class TestApplicationDelta:
warnings = []
plugin._replace_applications(asset_id, apps_v1, warnings)
db_session.commit()
db.session.commit()
assert EndpointApplication.query.filter_by(assetid=asset_id).count() == 2
# Update: Empty list
warnings = []
result = plugin._replace_applications(asset_id, [], warnings)
db_session.commit()
db.session.commit()
assert result == 0
assert EndpointApplication.query.filter_by(assetid=asset_id).count() == 0
def test_missing_displayname_skipped(self, plugin, asset_id, db_session):
def test_missing_displayname_skipped(self, plugin, asset_id, db):
"""Apps without displayname are skipped."""
apps = [
{
@@ -302,13 +313,13 @@ class TestApplicationDelta:
warnings = []
result = plugin._replace_applications(asset_id, apps, warnings)
db_session.commit()
db.session.commit()
assert result == 1 # Only Firefox added
assert len(warnings) == 1
assert 'no displayname' in warnings[0].lower()
def test_string_truncation(self, plugin, asset_id, db_session):
def test_string_truncation(self, plugin, asset_id, db):
"""Long strings are truncated to column limits."""
apps = [
{
@@ -321,7 +332,7 @@ class TestApplicationDelta:
warnings = []
result = plugin._replace_applications(asset_id, apps, warnings)
db_session.commit()
db.session.commit()
assert result == 1
app = EndpointApplication.query.filter_by(assetid=asset_id).first()
@@ -338,13 +349,9 @@ class TestApplicationDeltaSafety:
return EndpointsPlugin()
@pytest.fixture
def asset_id(self, db_session):
def asset_id(self, db):
"""Create a test asset."""
from shopdb.core.models import Asset
asset = Asset(assettag='TEST-SAFETY-001')
db_session.add(asset)
db_session.commit()
return asset.assetid
return _make_asset(db, 'TEST-SAFETY-001')
def test_compute_fingerprint_consistency(self):
"""Fingerprint algorithm is deterministic."""