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

@@ -0,0 +1,157 @@
"""Server TLS certificate upload and generate routes.
The new pair must be parsed, and the key proven to match the certificate,
BEFORE the existing files are backed up or replaced: a bad upload must leave
nginx's current certificate exactly where it was. cn and san must be plain
hostname/IP syntax; anything else (newlines, '=', brackets) is refused.
"""
import io
from datetime import datetime, timedelta, timezone
import pytest
from cryptography import x509
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.x509.oid import NameOID
from plugins.endpoints.api import routes as endpoint_routes
UPLOAD_URL = '/api/endpoints/settings/server-cert/upload'
GENERATE_URL = '/api/endpoints/settings/server-cert/generate'
@pytest.fixture(autouse=True)
def cert_dir(tmp_path, monkeypatch):
directory = tmp_path / 'certs'
directory.mkdir()
monkeypatch.setattr(endpoint_routes, '_server_cert_dir', lambda: directory)
monkeypatch.setattr(endpoint_routes, '_restart_nginx', lambda: None)
return directory
def _pair(cn='old.example.test'):
key = ec.generate_private_key(ec.SECP256R1())
name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, cn)])
now = datetime.now(timezone.utc)
certificate = (x509.CertificateBuilder()
.subject_name(name).issuer_name(name)
.public_key(key.public_key())
.serial_number(x509.random_serial_number())
.not_valid_before(now - timedelta(days=1))
.not_valid_after(now + timedelta(days=30))
.sign(key, hashes.SHA256()))
cert_pem = certificate.public_bytes(serialization.Encoding.PEM)
key_pem = key.private_bytes(serialization.Encoding.PEM,
serialization.PrivateFormat.PKCS8,
serialization.NoEncryption())
return cert_pem, key_pem
def _seed_existing(cert_dir):
cert_pem, key_pem = _pair('existing.example.test')
(cert_dir / 'server.crt').write_bytes(cert_pem)
(cert_dir / 'server.key').write_bytes(key_pem)
return cert_pem, key_pem
def _upload(client, headers, cert_bytes, key_bytes):
return client.post(UPLOAD_URL, headers=headers,
content_type='multipart/form-data',
data={'cert': (io.BytesIO(cert_bytes), 'server.crt'),
'key': (io.BytesIO(key_bytes), 'server.key')})
def test_a_matching_pair_replaces_and_backs_up(client, db, auth_headers, cert_dir):
from shopdb.api import AuditLog
old_cert, old_key = _seed_existing(cert_dir)
new_cert, new_key = _pair('new.example.test')
response = _upload(client, auth_headers, new_cert, new_key)
assert response.status_code == 200, response.get_json()
assert (cert_dir / 'server.crt').read_bytes() == new_cert
assert (cert_dir / 'server.key').read_bytes() == new_key
backups = sorted(p.name for p in cert_dir.iterdir() if '.backup.' in p.name)
assert len(backups) == 2
assert not [p for p in cert_dir.iterdir() if p.name.endswith('.tmp')]
assert AuditLog.query.filter_by(
action='endpoints.server_cert.uploaded').count() == 1
def test_a_mismatched_key_touches_nothing(client, db, auth_headers, cert_dir):
old_cert, old_key = _seed_existing(cert_dir)
new_cert, _new_key = _pair('new.example.test')
_other_cert, other_key = _pair('other.example.test')
response = _upload(client, auth_headers, new_cert, other_key)
assert response.status_code == 400
assert 'does not match' in response.get_json()['data']['error']['message']
assert (cert_dir / 'server.crt').read_bytes() == old_cert
assert (cert_dir / 'server.key').read_bytes() == old_key
assert sorted(p.name for p in cert_dir.iterdir()) == ['server.crt', 'server.key']
def test_garbage_certificate_touches_nothing(client, db, auth_headers, cert_dir):
old_cert, _old_key = _seed_existing(cert_dir)
_cert, key = _pair()
garbage = b'-----BEGIN CERTIFICATE-----\nnope\n-----END CERTIFICATE-----\n'
response = _upload(client, auth_headers, garbage, key)
assert response.status_code == 400
assert (cert_dir / 'server.crt').read_bytes() == old_cert
assert len(list(cert_dir.iterdir())) == 2
@pytest.mark.parametrize('cn', [
'host\nprompt = yes',
'bad host',
'a=b',
'[alt_names]',
'a' * 65,
])
def test_generate_refuses_unsafe_cn(client, db, auth_headers, cert_dir, cn):
_seed_existing(cert_dir)
response = client.post(GENERATE_URL, headers=auth_headers,
json={'cn': cn, 'days': 30})
assert response.status_code == 400
assert len(list(cert_dir.iterdir())) == 2
@pytest.mark.parametrize('san', [
'DNS:ok.example.test,DNS:bad\nname',
'IP:999.1.1.1',
'URI:http://x',
'DNS:a b',
])
def test_generate_refuses_unsafe_san(client, db, auth_headers, cert_dir, san):
_seed_existing(cert_dir)
response = client.post(GENERATE_URL, headers=auth_headers,
json={'cn': 'ok.example.test', 'san': san,
'days': 30})
assert response.status_code == 400
assert len(list(cert_dir.iterdir())) == 2
def test_generate_installs_a_valid_pair(client, db, auth_headers, cert_dir):
from shopdb.api import AuditLog
_seed_existing(cert_dir)
response = client.post(GENERATE_URL, headers=auth_headers,
json={'cn': 'shopdb.example.test',
'san': 'DNS:shopdb.example.test,IP:10.0.0.5',
'days': 30})
assert response.status_code == 200, response.get_json()
certificate = x509.load_pem_x509_certificate(
(cert_dir / 'server.crt').read_bytes())
assert certificate.subject.get_attributes_for_oid(
NameOID.COMMON_NAME)[0].value == 'shopdb.example.test'
assert endpoint_routes._validate_certificate_pair(
(cert_dir / 'server.crt').read_bytes(),
(cert_dir / 'server.key').read_bytes()) is None
assert AuditLog.query.filter_by(
action='endpoints.server_cert.generated').count() == 1