|
| 1 | +# Threat Classifier - SageMaker Project |
| 2 | + |
| 3 | +Standalone Amazon SageMaker solution for classifying security alert text by severity. Designed for easy integration into any security analytics platform. |
| 4 | + |
| 5 | +## Features |
| 6 | +- Train DistilBERT on synthetic alert data *(implementation in progress)* |
| 7 | +- Deploy real-time endpoint with data capture, alarms, rollback *(scaffolding ready)* |
| 8 | +- API contract: { id, text } → { label, score, model, ts } |
| 9 | +- Cost guardrails, security best practices |
| 10 | +- Majority-class heuristic baseline with uplift reporting for interview storytelling |
| 11 | +- Ready for CI/CD, unit tests, and future SOC platform integration |
| 12 | + |
| 13 | +## Quickstart |
| 14 | +1. Install Poetry and the AWS CLI v2.27.50 (see [AWS CLI setup](docs/aws-cli-setup.md)). |
| 15 | +2. `poetry install` |
| 16 | +3. `cp .env.example .env` and tailor the environment variables for your profile/region. |
| 17 | +4. `make lint` – run Ruff checks via Nox. |
| 18 | +5. `make test` – execute pytest with coverage gates. |
| 19 | +6. `make package` – build source + wheel artifacts. |
| 20 | + |
| 21 | +CI runs automatically via GitHub Actions on pushes and pull requests. The pipeline installs Poetry-managed deps, enforces Ruff + Black via Nox, executes the pytest suite with coverage, and synthesizes the CDK app (dev environment) so we catch infrastructure regressions early. A separate `Deploy` workflow is manually triggered when you need to promote a build; it reuses the guarded deployment script, honors environment protection rules, and requires the `THREAT_DEPLOY_CONFIRM` acknowledgement before any `cdk deploy` executes. See [`docs/release-process.md`](docs/release-process.md) for the full release, tagging, and approval model we walk through in interviews. |
| 22 | + |
| 23 | +> Training, deployment, and inference flows are being implemented in phases. |
| 24 | +> Placeholder CLI hooks surface `NotImplementedError` until their respective tasks land. |
| 25 | +
|
| 26 | +## Baseline heuristic & uplift tracking |
| 27 | + |
| 28 | +Every training run now captures a deterministic majority-class baseline so we can articulate |
| 29 | +clear business value during interviews. Metrics JSON includes both the baseline scores and the |
| 30 | +uplift delivered by the TF-IDF + LogisticRegression pipeline. That data feeds into |
| 31 | +SageMaker Model Monitor baselines or CloudWatch dashboards, reinforcing the governance story |
| 32 | +while keeping compute costs predictable. |
| 33 | + |
| 34 | +Two storyboard notebooks (`notebooks/01-eda.ipynb` and `notebooks/02-offline-eval.ipynb`) provide |
| 35 | +structured TODOs for running these analyses inside VPC-bound SageMaker notebooks. They emphasize |
| 36 | +data access guardrails, reproducible feature engineering, and ranking metrics like MAP@k/NDCG@k that |
| 37 | +roll into CI checks or rollback triggers. |
| 38 | + |
| 39 | +### Environment configuration |
| 40 | + |
| 41 | +All environment-specific settings (AWS accounts, VPC subnets, SageMaker instance types, FinOps tags) live under `config/*.yml`. |
| 42 | + |
| 43 | +- Set `THREAT_ENV` to `dev`, `prod`, or another environment name to switch defaults. |
| 44 | +- Optionally override the entire configuration with `THREAT_CONFIG_PATH=/abs/path/to/config.yml` for ad-hoc testing. |
| 45 | +- Use `extends: relative/or/absolute/path.yml` inside a config file to layer overrides on top of shared baselines without duplicating metadata. |
| 46 | +- The loader validates that every deployment remains VPC-only, encrypted with customer-managed KMS keys, and tagged for cost allocation. |
| 47 | + |
| 48 | +The CLI automatically consumes these files so training and future deployment flows always source a single, audited configuration. |
| 49 | + |
| 50 | +See `docs/` and `SageMaker-Project-Plan.md` for the broader architecture roadmap. |
| 51 | + |
| 52 | +## AWS CLI & IAM Requirements |
| 53 | + |
| 54 | +- The project standardizes on **AWS CLI v2.27.50** to align with CDK tooling. |
| 55 | +- Use dedicated least-privilege profiles (e.g., `sagemaker-dev`) with MFA/SAML enforced. |
| 56 | +- Apply cost allocation tags (`App`, `Env`, `CostCenter`, `Owner`) to every resource; the CDK stack inherits them automatically. |
| 57 | +- Detailed installation and configuration instructions live in [`docs/aws-cli-setup.md`](docs/aws-cli-setup.md). |
| 58 | + |
| 59 | +## Repository Layout |
| 60 | + |
| 61 | +``` |
| 62 | +├── data/ # Raw, synthetic, and monitoring datasets (no PII) |
| 63 | +├── docs/ # Project documentation and future ADRs/runbooks |
| 64 | +├── infra/ # AWS CDK application scaffolding |
| 65 | +├── notebooks/ # Exploratory analysis & demo notebooks |
| 66 | +├── scripts/ # Utility scripts (checklist automation, artifact packaging) |
| 67 | +├── src/ # Python packages for training, inference, CLI |
| 68 | +├── tests/ # Pytest suite executed by Nox/CI |
| 69 | +└── Makefile # Convenience targets wrapping Poetry + Nox |
| 70 | +This keeps deployment-ready artifacts reproducible without ad-hoc shell gymnastics-useful when interviewers probe for MLOps governance stories. |
| 71 | +
|
| 72 | +## Model Monitor baseline generation |
| 73 | +
|
| 74 | +Establish the statistics/constraints pair that SageMaker Model Monitor consumes with the new baseline helper: |
| 75 | +
|
| 76 | +```bash |
| 77 | +poetry run python scripts/create_monitor_baseline.py --env dev --dataset data/sample.csv |
| 78 | +``` |
| 79 | + |
| 80 | +- Uploads the provided dataset (or uses the pre-staged S3 object from `config/*.yml`) to the environment's baseline prefix. |
| 81 | +- Launches a data-quality processing job using the Model Monitor container and writes `statistics.json` & `constraints.json` alongside the dataset. |
| 82 | +- Honors least-privilege IAM assumptions by reusing the configured monitoring role and VPC-bound resources. |
| 83 | + |
| 84 | +Re-run the script any time you refresh the synthetic dataset or introduce new features. Interview callout: this demonstrates proactive data drift detection and shows how the platform self-polices over its lifetime. |
| 85 | + |
| 86 | +Use `make lint`/`make test` before pushing changes. When adding new directories, drop a short README describing intent so contributors stay aligned. |
| 87 | + |
| 88 | +## Packaging trained artifacts |
| 89 | + |
| 90 | +Bundle the latest training outputs into a SageMaker-compatible archive with the Poetry CLI harness: |
| 91 | + |
| 92 | +```bash |
| 93 | +poetry run package-model |
| 94 | +``` |
| 95 | + |
| 96 | +Or call the lower-level script directly when you need custom arguments: |
| 97 | + |
| 98 | +```bash |
| 99 | +poetry run python scripts/package_model_artifacts.py --env dev |
| 100 | +``` |
| 101 | + |
| 102 | +- Defaults to the environment's configured training output directory and writes `dist/<env>/model.tar.gz`. |
| 103 | +- Pass `--upload` to push the tarball to the environment's model artifacts bucket; uploads use the project KMS key and inherit FinOps tags so budgets stay accurate. |
| 104 | +- Override paths or S3 keys (`--source-dir`, `--output`, `--s3-key`) when replaying historical models or promoting a hotfix. |
| 105 | +- The CLI keeps uploads opt-in: set `THREAT_PACKAGE_UPLOAD=true` when you *intentionally* want to push to S3. Optional overrides include `THREAT_PACKAGE_SOURCE_DIR`, `THREAT_PACKAGE_OUTPUT_PATH`, and `THREAT_PACKAGE_S3_KEY`. |
| 106 | + |
| 107 | +This keeps deployment-ready artifacts reproducible without ad-hoc shell gymnastics-useful when interviewers probe for MLOps governance stories. |
0 commit comments