Files
shopdb-flask/scripts/dev/seed_shared_applications.py
cproudlock 784964c907 Bring the wilm branch into main: endpoints plugin, agent, policies, CSV import
Ports the corporate-GitHub wilm branch onto main, keeping the two branches
separate. Thirteen upstream commits arrive with their authorship intact; the
review fixes and the portability work below are on top.

What arrives:

  endpoints plugin  - a ShopDB agent reporting endpoint state, telemetry,
                      services and interfaces, with a command channel the agent
                      long-polls. Ingest stays on the generic collector route
                      (ADR-006); the agent key is a UI-managed single-scope
                      token whose revocation is an instant kill switch.
  computers policies - Configuration and Business Policies (RMM-style), scope
                      trees validated at write time, actions checked against
                      the endpoints command allowlist. Feature-gated: every
                      route 404s while the setting is off, so enabling it can
                      never half-expose the surface.
  asset CSV import  - templates, a collector script carrying the site path, and
                      a validate-then-commit import. A plugin can own a virtual
                      column plus a post-write row hook, which is how a PC's
                      machine number becomes a controls link without a
                      hand-built relationships sheet.
  agent/            - the Windows agent and its installer.

Contract 0.21.0 -> 0.24.0, additive throughout: register_import_hook (0.22),
mint/list/revoke_service_token (0.23), collector_authorized (0.24). The stability
table had no rows for any of them and now does.

MySQL 5.6 portability. The branch stored three policy columns as sa.JSON, the
only JSON columns in the product, and 5.6 has no JSON type: the migration failed
with a bare syntax error near the word JSON. They are TEXT now, through a
JSONText type decorator, so all seventeen call sites still read and write dicts.
The convention elsewhere is already TEXT (guides.blocksjson,
geenforce.manifestjson).

Review fixes carried on top:

  - six policy GETs were jwt_required(optional=True), which authenticates a
    caller without checking anything; they require computers.view now, and six
    redundant jwt_required decorators are gone
  - the agent installer took a default BaseUrl and CollectorKey, so a mistyped
    run silently pointed an endpoint at the wrong instance; both are mandatory
    and a non-https BaseUrl throws unless -AllowInsecureHttp is passed
  - a committing CSV import wrote no audit row

Documentation: the 34 new routes are described in docs/api-inventory.json, which
is hand-maintained on purpose, and the generated spec now covers 331 paths and
477 operations across 22 surfaces. DEVELOPMENT-SETUP's 1071 troubleshooting row
said "use MySQL 8 for dev" and now gives the actual fix, including the trap that
5.6 downgrades ROW_FORMAT=DYNAMIC to Compact with a warning instead of an error.

Full suite 2123 passed, vitest 260 passed, all seventeen plugin chains migrate
clean on MySQL 5.6 and the naming, docs, parity and read-surface gates are green.
2026-08-28 16:17:17 -04:00

103 lines
3.4 KiB
Python

"""Seed the org's shared shopfloor applications (DEV ONLY).
Inserts the applications that ship as common across the organization so the
Configuration Policy builder has real apps to scope against and the software
inventory has canonical rows to match collected installs to. Each app records
its executable and (where relevant) its Windows service name in
applicationnotes, since the Application model has no dedicated process/service
column - these identifiers are what a generic service_*/process_* policy action
targets.
Shipped set:
* UDC - UDC.exe
* MTConnect - agent.exe process, "MTConnect UDC Agent" service
* eDNC - DncMain.exe process
Idempotent: matches on appname and updates in place, so re-running does not
duplicate. Marked installable so they appear in installable-app pickers.
DEV ONLY: lives under scripts/dev/ and must never be imported by application
code. Run inside the API container.
Usage (in the API container):
docker exec shopdb-flask-wilm-api-1 python scripts/dev/seed_shared_applications.py
docker exec shopdb-flask-wilm-api-1 python scripts/dev/seed_shared_applications.py --clear
"""
import argparse
import os
import sys
# Repo root on sys.path so `shopdb` imports when run as a script.
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
from shopdb import create_app # noqa: E402
from shopdb.api import db # noqa: E402
# appname -> seed fields. 'notes' captures the process image and service name so
# a generic policy action (params.target) has a canonical identifier to bind to.
SHARED_APPS = [
{
'appname': 'UDC',
'appdescription': 'Universal Data Collector',
'notes': 'process=UDC.exe',
},
{
'appname': 'MTConnect',
'appdescription': 'MTConnect UDC agent',
'notes': 'process=agent.exe; service=MTConnect UDC Agent',
},
{
'appname': 'eDNC',
'appdescription': 'eDNC distributed NC file transfer',
'notes': 'process=DncMain.exe',
},
]
def _seed(Application):
created = 0
updated = 0
for spec in SHARED_APPS:
app_row = Application.query.filter_by(appname=spec['appname']).first()
if app_row is None:
app_row = Application(appname=spec['appname'])
db.session.add(app_row)
created += 1
else:
updated += 1
app_row.appdescription = spec['appdescription']
app_row.applicationnotes = spec['notes']
app_row.isinstallable = True
db.session.commit()
print('Seeded shared applications: {} created, {} updated.'
.format(created, updated))
def _clear(Application):
names = [spec['appname'] for spec in SHARED_APPS]
removed = (Application.query
.filter(Application.appname.in_(names))
.delete(synchronize_session=False))
db.session.commit()
print('Removed {} shared application row(s).'.format(removed))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--clear', action='store_true',
help='Remove the seeded shared applications.')
args = parser.parse_args()
app = create_app(os.environ.get('FLASK_ENV', 'production'))
with app.app_context():
from shopdb.api import Application
if args.clear:
_clear(Application)
else:
_seed(Application)
if __name__ == '__main__':
main()