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

@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""Backfill app_fingerprint column for existing EndpointApplication records.
"""Backfill appfingerprint column for existing EndpointApplication records.
Run this ONCE after deploying the 0020_app_delta_fingerprints migration and
before deploying the updated C# agent that sends fingerprints. This populates
@@ -20,6 +20,13 @@ from pathlib import Path
# Add project root to Python path
sys.path.insert(0, str(Path(__file__).parent.parent))
from dotenv import load_dotenv
# Load .env before anything reads config: a plain `python scripts/...` run
# otherwise falls back to the hardcoded root@localhost URL and could write the
# wrong database.
load_dotenv(Path(__file__).resolve().parent.parent / '.env')
from shopdb.extensions import db
from plugins.endpoints.models import EndpointApplication
@@ -50,103 +57,122 @@ def compute_fingerprint(displayname: str, displayversion: str, publisher: str) -
def verify_fingerprint_algorithm():
"""Verify backend fingerprint algorithm matches expected behavior.
Uses test vectors to ensure consistency with agent implementation.
Any mismatch indicates algorithm drift and will cause delta logic to fail.
Fixed test vectors: SHA256 of the literal 'name|version|publisher' strings,
which is what the agent's ComputeApplicationFingerprint hashes. Any
mismatch means compute_fingerprint drifted from the agent, which would make
the delta logic delete and re-add every app on every check-in.
"""
print("Verifying fingerprint algorithm...")
# Test vectors: known inputs and their expected SHA256 outputs
# These verify the algorithm matches the C# agent implementation
test_cases = [
{
'name': 'Google Chrome',
'version': '117.0.5938.132',
'publisher': 'Google LLC',
# SHA256("Google Chrome|117.0.5938.132|Google LLC")
'expected_fp': None # Prints computed value for verification
},
{
'name': 'Microsoft Office',
'version': '16.0.16827.20166',
'publisher': 'Microsoft Corporation',
# SHA256("Microsoft Office|16.0.16827.20166|Microsoft Corporation")
'expected_fp': None # Prints computed value for verification
},
{
'name': 'Test App No Version',
'version': None,
'publisher': 'Test Publisher',
# SHA256("Test App No Version||Test Publisher")
'expected_fp': None # Prints computed value for verification
},
{
'name': 'Test App Empty Strings',
'version': '',
'publisher': '',
# SHA256("Test App Empty Strings||")
'expected_fp': None # Prints computed value for verification
},
('Google Chrome', '117.0.5938.132', 'Google LLC',
'a22de9ae7e641954872208b21aa52ac948898b78bcc8d7de5746af3a324f49ae'),
('Microsoft Office', '16.0.16827.20166', 'Microsoft Corporation',
'a180bfd16f34c52c94d01317044f7d3112bd9b88c6be570ee5cca4eff9199ae4'),
('Test App No Version', None, 'Test Publisher',
'aae1e046f90b39da80b2356713d8e6dc8579746d378ce7bc515800a4fbb85cda'),
('Test App Empty Strings', '', '',
'1a29cd2e3bd856ac3143d2675376b92eedf1563f5e8eb78f235d252943580ceb'),
]
all_passed = True
for tc in test_cases:
computed = compute_fingerprint(tc['name'], tc['version'], tc['publisher'])
# Validate format
if len(computed) != 64 or not all(c in '0123456789abcdef' for c in computed):
print(f" FAIL INVALID FORMAT: {tc['name']}")
print(f" Got: {computed}")
all_passed = False
continue
# Check against expected (if we have one)
if tc['expected_fp']:
if computed == tc['expected_fp']:
print(f" OK {tc['name']}: {computed[:16]}...")
else:
print(f" FAIL MISMATCH: {tc['name']}")
print(f" Expected: {tc['expected_fp']}")
print(f" Got: {computed}")
all_passed = False
failures = []
for name, version, publisher, expected in test_cases:
computed = compute_fingerprint(name, version, publisher)
if computed == expected:
print(f" OK {name}: {computed[:16]}...")
else:
# First run - show computed value for test vector creation
print(f" ? {tc['name']}: {computed[:16]}... (computed, no expected value)")
print(f" FAIL MISMATCH: {name}")
print(f" Expected: {expected}")
print(f" Got: {computed}")
failures.append(name)
if not all_passed:
if failures:
raise ValueError(
"Fingerprint algorithm verification FAILED! "
"Backend algorithm does not match expected behavior. "
"This will cause delta logic to delete+re-add all apps on every check-in."
"Fingerprint algorithm verification FAILED for: "
+ ", ".join(failures)
+ ". Backend algorithm does not match the agent; the delta logic "
"would delete+re-add all apps on every check-in."
)
print("OK Fingerprint algorithm verified\n")
return True
def _fingerprint_batch(apps):
"""Assign fingerprints to one batch, removing duplicate (assetid, fingerprint) rows.
The unique index on (assetid, appfingerprint) means two rows for the same
install on one asset cannot both carry the fingerprint. Of each duplicate
set the lowest endpointapplicationid is kept (fingerprinted rows already in
the table included) and the rest are deleted.
Returns the number of duplicate rows deleted.
"""
duplicates = 0
claimed = {}
for app in sorted(apps, key=lambda row: row.endpointapplicationid):
fingerprint = compute_fingerprint(app.displayname, app.displayversion,
app.publisher)
key = (app.assetid, fingerprint)
holder = claimed.get(key)
if holder is None:
holder = (EndpointApplication.query
.filter(EndpointApplication.assetid == app.assetid,
EndpointApplication.appfingerprint == fingerprint,
EndpointApplication.endpointapplicationid
!= app.endpointapplicationid)
.first())
if holder is None:
app.appfingerprint = fingerprint
claimed[key] = app
elif holder.endpointapplicationid < app.endpointapplicationid:
db.session.delete(app)
duplicates += 1
else:
# An already-fingerprinted row with a HIGHER id: drop it and let the
# older row carry the fingerprint. Flush the delete first so the
# unique index is free before the update lands.
db.session.delete(holder)
db.session.flush()
duplicates += 1
app.appfingerprint = fingerprint
claimed[key] = app
return duplicates
def backfill_fingerprints(batch_size: int = 1000, dry_run: bool = False):
"""Backfill fingerprints for existing EndpointApplication records.
Each batch commits on its own. A batch that fails is rolled back and the run
stops, so nothing half-applied is left in the session.
Args:
batch_size: Number of records to update per commit
dry_run: If True, show what would be done without making changes
Returns:
dict with processed, duplicatesremoved and failed counts.
"""
# SAFETY: Verify algorithm before touching database
verify_fingerprint_algorithm()
# Count total records needing backfill
summary = {'processed': 0, 'duplicatesremoved': 0, 'failed': 0}
total = EndpointApplication.query.filter(
EndpointApplication.app_fingerprint.is_(None)
EndpointApplication.appfingerprint.is_(None)
).count()
if total == 0:
print("No records need backfilling (all have fingerprints).")
return
return summary
print(f"Found {total:,} records without fingerprints")
if dry_run:
print("\nDRY RUN MODE - showing sample records:")
sample = EndpointApplication.query.filter(
EndpointApplication.app_fingerprint.is_(None)
EndpointApplication.appfingerprint.is_(None)
).limit(5).all()
for app in sample:
@@ -154,49 +180,54 @@ def backfill_fingerprints(batch_size: int = 1000, dry_run: bool = False):
print(f" {app.displayname:50s} -> {fp[:16]}...")
print(f"\nWould backfill {total:,} records in batches of {batch_size}")
return
return summary
print(f"Backfilling in batches of {batch_size}...")
processed = 0
batch = 0
while True:
# Fetch next batch
apps = (EndpointApplication.query
.filter(EndpointApplication.app_fingerprint.is_(None))
.filter(EndpointApplication.appfingerprint.is_(None))
.order_by(EndpointApplication.endpointapplicationid)
.limit(batch_size)
.all())
if not apps:
break # No more records
# Update fingerprints
for app in apps:
app.app_fingerprint = compute_fingerprint(
app.displayname,
app.displayversion,
app.publisher
)
# Commit batch
db.session.commit()
break
batch += 1
processed += len(apps)
print(f" Batch {batch}: {processed:,} / {total:,} ({processed*100//total}%)")
try:
duplicates = _fingerprint_batch(apps)
db.session.commit()
except Exception as error:
db.session.rollback()
summary['failed'] = len(apps)
print(f" Batch {batch} FAILED and was rolled back: {error}")
break
summary['processed'] += len(apps)
summary['duplicatesremoved'] += duplicates
print(f" Batch {batch}: {summary['processed']:,} / {total:,} "
f"({summary['processed'] * 100 // total}%), "
f"{duplicates} duplicate(s) removed")
print(f"\nBackfill finished. Processed {summary['processed']:,} records; "
f"removed {summary['duplicatesremoved']:,} duplicate row(s).")
if summary['failed']:
print("A batch failed; fix the error above and re-run (completed batches "
"are kept, the run resumes from the remaining null rows).")
return summary
print(f"\nBackfill complete! Updated {processed:,} records.")
print("\nNext steps:")
print("1. Deploy updated C# agent that computes and sends fingerprints")
print("2. Monitor collector warnings for agents without fingerprint support")
print("3. After all agents updated, consider making app_fingerprint NOT NULL")
print("3. After all agents updated, consider making appfingerprint NOT NULL")
return summary
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Backfill app_fingerprint column')
parser = argparse.ArgumentParser(description='Backfill appfingerprint column')
parser.add_argument('--dry-run', action='store_true',
help='Show what would be done without making changes')
parser.add_argument('--batch-size', type=int, default=1000,
@@ -209,10 +240,12 @@ if __name__ == '__main__':
from shopdb import create_app
app = create_app()
with app.app_context():
backfill_fingerprints(
result = backfill_fingerprints(
batch_size=args.batch_size,
dry_run=args.dry_run
)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
if result['failed']:
sys.exit(1)

View File

@@ -25,7 +25,9 @@ USE_GIT_LS_FILES=${USE_GIT_LS_FILES:-1}
if [ "$USE_GIT_LS_FILES" = "1" ] && git rev-parse --git-dir >/dev/null 2>&1; then
# Filter out paths we don't want to scan even if tracked
GIT_FILES_FILTER="grep -v '^migrations/versions/' | grep -v '^frontend/dist/' | grep -v '^staticdocs/' | grep -v '\.plugins-staged/'"
# NUL-delimited end to end (git ls-files -z | grep -z | xargs -0) so a path
# with a space or newline is scanned as one file, not split into several.
GIT_FILES_FILTER="grep -zv '^migrations/versions/' | grep -zv '^frontend/dist/' | grep -zv '^staticdocs/' | grep -zv '\.plugins-staged/'"
else
# Fallback to recursive grep with excludes for non-git directories
GIT_FILES_FILTER="cat"
@@ -113,7 +115,9 @@ drop_scratch() { grep -v '^\./devnotes/' || true; }
# lists it sat beside.
drop_generated() {
# Match with or without leading ./ for git ls-files compatibility
grep -vP '^(\./)?(.*/)?(findings\.sarif|report\.md|scan\.log|\.env|.*\.standalone\.js|.*\.min\.js):' || true
# [^:]* rather than .*: on a long minified hit line the greedy form blows
# PCRE's backtracking limit, and grep then errors instead of filtering.
grep -vP '^(\./)?([^:]*/)?(findings\.sarif|report\.md|scan\.log|\.env|[^:/]*\.standalone\.js|[^:/]*\.min\.js):' || true
}
# CONTRIBUTING.md documents the conventions by SHOWING violations as examples,
@@ -153,10 +157,10 @@ echo "==> Checking for non-ASCII characters..."
# wj wrote this as --binary-files=without-match, which is the same behaviour
# spelled long-hand; -I is kept for consistency with the rest of the file.
if [ "$USE_GIT_LS_FILES" = "1" ] && git rev-parse --git-dir >/dev/null 2>&1; then
NON_ASCII=$(git ls-files \
NON_ASCII=$(git ls-files -z \
'*.py' '*.vue' '*.js' '*.ts' '*.json' '*.md' '*.yaml' '*.yml' \
| eval "$GIT_FILES_FILTER" \
| xargs -r grep -PnI '[^\x00-\x7F]' 2>/dev/null \
| xargs -0 -r grep -PnI '[^\x00-\x7F]' 2>/dev/null \
| drop_scratch | drop_generated || true)
else
NON_ASCII=$(grep -rPnI '[^\x00-\x7F]' "${EXCLUDES[@]}" "${INCLUDES_ALL[@]}" . 2>/dev/null | drop_scratch | drop_generated || true)
@@ -177,11 +181,14 @@ echo "==> Checking for banned shorthand (standalone)..."
# Exclude C# client code (.cs files) - they're not Python and have different conventions
for word in cfg ctx mgr req res; do
if [ "$USE_GIT_LS_FILES" = "1" ] && git rev-parse --git-dir >/dev/null 2>&1; then
HITS=$(git ls-files 'shopdb/**/*.py' 'shopdb/**/*.vue' 'shopdb/**/*.js' 'shopdb/**/*.ts' \
'plugins/**/*.py' 'plugins/**/*.vue' 'plugins/**/*.js' 'plugins/**/*.ts' \
'frontend/src/**/*.py' 'frontend/src/**/*.vue' 'frontend/src/**/*.js' 'frontend/src/**/*.ts' \
| grep -v '/tests\?/' | grep -v '\.spec\.js' | grep -v '\.plugins-staged' \
| xargs -r grep -Pn "(^|[^a-zA-Z0-9_])${word}([^a-zA-Z0-9_]|\$)" 2>/dev/null \
# :(glob) is required: without it git matches pathspecs with fnmatch,
# where 'shopdb/**/*.py' needs a second slash and silently skips
# top-level files such as shopdb/__init__.py and frontend/src/main.js.
HITS=$(git ls-files -z ':(glob)shopdb/**/*.py' ':(glob)shopdb/**/*.vue' ':(glob)shopdb/**/*.js' ':(glob)shopdb/**/*.ts' \
':(glob)plugins/**/*.py' ':(glob)plugins/**/*.vue' ':(glob)plugins/**/*.js' ':(glob)plugins/**/*.ts' \
':(glob)frontend/src/**/*.py' ':(glob)frontend/src/**/*.vue' ':(glob)frontend/src/**/*.js' ':(glob)frontend/src/**/*.ts' \
| grep -zv '/tests\?/' | grep -zv '\.spec\.js' | grep -zv '\.plugins-staged' \
| xargs -0 -r grep -Pn "(^|[^a-zA-Z0-9_])${word}([^a-zA-Z0-9_]|\$)" 2>/dev/null \
| grep -v '\.cs:' \
| grep -vP "(^|[^a-zA-Z0-9_])(request_obj|response_obj)" \
| drop_scratch | drop_generated || true)
@@ -209,8 +216,8 @@ done
echo "==> Checking for snake_case DB tablenames..."
if [ "$USE_GIT_LS_FILES" = "1" ] && git rev-parse --git-dir >/dev/null 2>&1; then
SNAKE_TABLES=$(git ls-files '*.py' | eval "$GIT_FILES_FILTER" \
| xargs -r grep -Pn "__tablename__\s*=\s*['\"][^'\"]*_" 2>/dev/null \
SNAKE_TABLES=$(git ls-files -z '*.py' | eval "$GIT_FILES_FILTER" \
| xargs -0 -r grep -Pn "__tablename__\s*=\s*['\"][^'\"]*_" 2>/dev/null \
| drop_scratch | drop_contributing || true)
else
SNAKE_TABLES=$(grep -rPn "__tablename__\s*=\s*['\"][^'\"]*_" "${EXCLUDES[@]}" \
@@ -225,8 +232,8 @@ fi
echo "==> Checking for snake_case DB column attrs..."
if [ "$USE_GIT_LS_FILES" = "1" ] && git rev-parse --git-dir >/dev/null 2>&1; then
SNAKE_COLS=$(git ls-files '*.py' | eval "$GIT_FILES_FILTER" \
| xargs -r grep -Pn "^\s+[a-z]+_[a-z_]+\s*=\s*db\.Column" 2>/dev/null \
SNAKE_COLS=$(git ls-files -z '*.py' | eval "$GIT_FILES_FILTER" \
| xargs -0 -r grep -Pn "^\s+[a-z]+_[a-z_]+\s*=\s*db\.Column" 2>/dev/null \
| drop_scratch | drop_contributing || true)
else
SNAKE_COLS=$(grep -rPn "^\s+[a-z]+_[a-z_]+\s*=\s*db\.Column" "${EXCLUDES[@]}" \
@@ -241,8 +248,8 @@ fi
echo "==> Checking for snake_case ForeignKey targets..."
if [ "$USE_GIT_LS_FILES" = "1" ] && git rev-parse --git-dir >/dev/null 2>&1; then
SNAKE_FK=$(git ls-files '*.py' | eval "$GIT_FILES_FILTER" \
| xargs -r grep -Pn "ForeignKey\(['\"][^'\"]*_[^'\"]*['\"]" 2>/dev/null \
SNAKE_FK=$(git ls-files -z '*.py' | eval "$GIT_FILES_FILTER" \
| xargs -0 -r grep -Pn "ForeignKey\(['\"][^'\"]*_[^'\"]*['\"]" 2>/dev/null \
| drop_scratch || true)
else
SNAKE_FK=$(grep -rPn "ForeignKey\(['\"][^'\"]*_[^'\"]*['\"]" "${EXCLUDES[@]}" --include='*.py' . 2>/dev/null | drop_scratch || true)
@@ -256,9 +263,9 @@ fi
echo "==> Checking for snake_case API params in frontend (DB-mirrored fields)..."
if [ "$USE_GIT_LS_FILES" = "1" ] && git rev-parse --git-dir >/dev/null 2>&1; then
SNAKE_FE=$(git ls-files '*.vue' '*.js' '*.ts' | eval "$GIT_FILES_FILTER" \
| grep -v 'DEVELOPMENT-STANDARDS.md' \
| xargs -r grep -Pn "params\.(machine_id|location_id|vendor_id|type_id|business_unit_id|model_id|status_id|operating_system_id|asset_id|user_id|is_active|is_shopfloor)" 2>/dev/null \
SNAKE_FE=$(git ls-files -z '*.vue' '*.js' '*.ts' | eval "$GIT_FILES_FILTER" \
| grep -zv 'DEVELOPMENT-STANDARDS.md' \
| xargs -0 -r grep -Pn "params\.(machine_id|location_id|vendor_id|type_id|business_unit_id|model_id|status_id|operating_system_id|asset_id|user_id|is_active|is_shopfloor)" 2>/dev/null \
| drop_scratch || true)
else
SNAKE_FE=$(grep -rPn "params\.(machine_id|location_id|vendor_id|type_id|business_unit_id|model_id|status_id|operating_system_id|asset_id|user_id|is_active|is_shopfloor)" "${EXCLUDES[@]}" \
@@ -278,8 +285,8 @@ fi
echo "==> Checking for cross-plugin / escaping imports in plugin frontends..."
if [ -d plugins ]; then
if [ "$USE_GIT_LS_FILES" = "1" ] && git rev-parse --git-dir >/dev/null 2>&1; then
PLUGIN_FE_IMPORTS=$(git ls-files 'plugins/*/frontend/**/*.vue' 'plugins/*/frontend/**/*.js' \
| xargs -r grep -Pn "(import|from)\s+['\"]([^'\"]*\.\./\.\./|[^'\"]*/plugins/)" 2>/dev/null || true)
PLUGIN_FE_IMPORTS=$(git ls-files -z ':(glob)plugins/*/frontend/**/*.vue' ':(glob)plugins/*/frontend/**/*.js' \
| xargs -0 -r grep -Pn "(import|from)\s+['\"]([^'\"]*\.\./\.\./|[^'\"]*/plugins/)" 2>/dev/null || true)
else
PLUGIN_FE_IMPORTS=$(grep -rPn "(import|from)\s+['\"]([^'\"]*\.\./\.\./|[^'\"]*/plugins/)" \
--include='*.vue' --include='*.js' plugins/*/frontend/ 2>/dev/null || true)
@@ -305,9 +312,9 @@ fi
# passes what is specific to its label and nothing else.
echo "==> Checking that only the shared module imports the code libraries..."
if [ "$USE_GIT_LS_FILES" = "1" ] && git rev-parse --git-dir >/dev/null 2>&1; then
CODE_LIB_IMPORTS=$(git ls-files 'frontend/src/**/*.vue' 'frontend/src/**/*.js' 'plugins/**/*.vue' 'plugins/**/*.js' \
| grep -v '\.plugins-staged/' \
| xargs -r grep -n "from ['\"]\(qrcode\|jsbarcode\)['\"]" 2>/dev/null \
CODE_LIB_IMPORTS=$(git ls-files -z ':(glob)frontend/src/**/*.vue' ':(glob)frontend/src/**/*.js' ':(glob)plugins/**/*.vue' ':(glob)plugins/**/*.js' \
| grep -zv '\.plugins-staged/' \
| xargs -0 -r grep -n "from ['\"]\(qrcode\|jsbarcode\)['\"]" 2>/dev/null \
| grep -v 'frontend/src/utils/codes\.js' || true)
else
CODE_LIB_IMPORTS=$(grep -rn "from ['\"]\(qrcode\|jsbarcode\)['\"]" \

View File

@@ -1,133 +0,0 @@
param(
[Parameter(Mandatory=$false)]
[string]$Hostname = $env:COMPUTERNAME,
[Parameter(Mandatory=$false)]
[int]$ValidityYears = 2
)
# SECURITY: Validate hostname matches actual machine name to prevent cert for wrong host
$actualHostname = [System.Net.Dns]::GetHostName()
if ($Hostname -ne $actualHostname) {
Write-Warning "Hostname mismatch detected!"
Write-Warning " Requested: $Hostname"
Write-Warning " Actual machine: $actualHostname"
Write-Warning "Using actual hostname to prevent certificate mismatch."
$Hostname = $actualHostname
}
# Generate self-signed cert for this endpoint
$cert = New-SelfSignedCertificate `
-Subject "CN=$Hostname" `
-DnsName $Hostname `
-CertStoreLocation "Cert:\LocalMachine\My" `
-KeyExportPolicy NonExportable `
-KeyUsage DigitalSignature, KeyEncipherment `
-NotAfter (Get-Date).AddYears($ValidityYears) `
-HashAlgorithm SHA256
# SECURITY WARNING: Grant Users group read access to the private key so the tray
# (running as interactive user) can use it when shelling out to shopdbagent.exe --once.
# The service already works because it runs as SYSTEM.
#
# RISK: This grants ALL interactive users on the machine read access to the private
# key, which means any user with console/RDP access can extract the key and impersonate
# this endpoint. Only acceptable in environments where ALL interactive users are trusted.
#
# MITIGATION OPTIONS:
# 1. Best: Remove tray dependency on cert (tray calls service API, service makes HTTP)
# 2. Acceptable: Grant only to specific user account running the tray
# 3. Document: Only deploy in environments where all console users are trusted operators
try {
$rsaKey = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($cert)
$keyPath = $rsaKey.Key.UniqueName
# CNG keys are in SystemKeys, not MachineKeys
$keyFile = Join-Path "$env:ProgramData\Microsoft\Crypto\SystemKeys" $keyPath
if (-not (Test-Path $keyFile)) {
$keyFile = Join-Path "$env:ProgramData\Microsoft\Crypto\Keys" $keyPath
}
if (-not (Test-Path $keyFile)) {
$keyFile = Join-Path "$env:ProgramData\Microsoft\Crypto\RSA\MachineKeys" $keyPath
}
if (Test-Path $keyFile) {
# Grant Read + Execute to BUILTIN\Users
$acl = Get-Acl $keyFile
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
"BUILTIN\Users", "Read,ReadAndExecute", "Allow")
$acl.AddAccessRule($rule)
Set-Acl -Path $keyFile -AclObject $acl
Write-Host " Granted Users group access to private key"
} else {
Write-Host " WARNING: Could not find private key file to set permissions"
}
} catch {
Write-Host " WARNING: Could not set private key permissions: $($_.Exception.Message)"
}
Write-Host "Generated certificate:"
Write-Host " Thumbprint: $($cert.Thumbprint)"
Write-Host " Subject: $($cert.Subject)"
Write-Host " Expires: $($cert.NotAfter)"
# Compute SHA1 fingerprint to match nginx $ssl_client_fingerprint variable
# SECURITY NOTE: Nginx only provides SHA1 via $ssl_client_fingerprint variable.
# SHA1 is deprecated for cryptographic signing but remains acceptable for certificate
# fingerprinting in air-gapped environments with:
# - Short-lived certificates (2 years max)
# - Certificate revocation support (cert_revoked_at column)
# - No external CA trust (self-signed only)
# Future migration to SHA256 requires: nginx module for SHA256, or IIS deployment
# which exposes full PEM cert for server-side hashing.
$certBytes = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)
$sha1 = [System.Security.Cryptography.SHA1]::Create()
$fingerprintBytes = $sha1.ComputeHash($certBytes)
$fingerprint = ($fingerprintBytes | ForEach-Object { $_.ToString("x2") }) -join ""
Write-Host ""
Write-Host "========================================="
Write-Host "CERTIFICATE GENERATED SUCCESSFULLY"
Write-Host "========================================="
Write-Host "Fingerprint (SHA1): $fingerprint"
Write-Host "Length: 40 hex chars (nginx limitation)"
Write-Host ""
Write-Host "Register this certificate in ShopDB database:"
Write-Host ""
Write-Host " UPDATE assets"
Write-Host " SET client_cert_fingerprint = '$fingerprint',"
Write-Host " cert_issued_at = CURRENT_TIMESTAMP,"
Write-Host " cert_expires_at = '$($cert.NotAfter.ToString("yyyy-MM-dd HH:mm:ss"))',"
Write-Host " cert_revoked_at = NULL,"
Write-Host " cert_revocation_reason = NULL"
Write-Host " WHERE assetnumber = '(REPLACE_WITH_ASSET_NUMBER)';"
Write-Host ""
Write-Host "To revoke this certificate later (compromised/lost endpoint):"
Write-Host ""
Write-Host " UPDATE assets"
Write-Host " SET cert_revoked_at = CURRENT_TIMESTAMP,"
Write-Host " cert_revocation_reason = 'Compromised - TICKET-123'"
Write-Host " WHERE client_cert_fingerprint = '$fingerprint';"
Write-Host ""
Write-Host "========================================="
# Update agent config
$configPath = "C:\ProgramData\ShopDB\config.json"
if (Test-Path $configPath) {
$config = Get-Content $configPath | ConvertFrom-Json
$config | Add-Member -NotePropertyName clientCertThumbprint -NotePropertyValue $cert.Thumbprint -Force
$config | ConvertTo-Json | Set-Content $configPath
Write-Host ""
Write-Host "Updated $configPath with thumbprint"
} else {
Write-Host ""
Write-Host "WARNING: $configPath not found - agent may not be configured"
}
# Output for automation
Write-Host ""
Write-Host "---MACHINE-READABLE---"
Write-Host "THUMBPRINT=$($cert.Thumbprint)"
Write-Host "FINGERPRINT=$fingerprint"
Write-Host "HOSTNAME=$Hostname"
Write-Host "EXPIRES=$($cert.NotAfter.ToString('yyyy-MM-dd HH:mm:ss'))"