Prepare Assignment 8 secure delivery and rollback
Add the locked pricing service, trusted-agent pipeline, Ansible deployments, security gates, recovery runbooks, prerequisite evidence, and preparation manual. Prepared with Codex assistance. Vincent explicitly authorized this local commit and will handle the push. Assessed releases and recovery exercises remain pending.
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
__pycache__
|
||||
.venv
|
||||
.git
|
||||
*.pyc
|
||||
@@ -0,0 +1,13 @@
|
||||
FROM python:3.13-alpine@sha256:9ef1a599f863231c3d91e941389bfdc4a2d6070557243d54df1a4e57015b960e
|
||||
ARG APP_COMMIT=development
|
||||
ENV APP_COMMIT=$APP_COMMIT PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
|
||||
WORKDIR /app
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt \
|
||||
&& python -m pip uninstall -y pip \
|
||||
&& rm -rf /usr/local/lib/python3.13/ensurepip
|
||||
COPY app.py .
|
||||
USER 10001:10001
|
||||
EXPOSE 8000
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2)"
|
||||
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "1", "--worker-tmp-dir", "/tmp", "app:app"]
|
||||
@@ -0,0 +1,33 @@
|
||||
import os
|
||||
from flask import Flask, jsonify, request
|
||||
|
||||
app = Flask(__name__)
|
||||
VERSION = "v1"
|
||||
UNIT_PRICE = 10
|
||||
COMMIT = os.environ.get("APP_COMMIT", "development")
|
||||
|
||||
|
||||
def total_price(quantity):
|
||||
if quantity < 1 or quantity > 1000:
|
||||
raise ValueError("quantity must be between 1 and 1000")
|
||||
return UNIT_PRICE * quantity
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def index():
|
||||
return jsonify(service="Startup Pricing", version=VERSION, commit=COMMIT)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return jsonify(status="ok", version=VERSION)
|
||||
|
||||
|
||||
@app.get("/price")
|
||||
def price():
|
||||
try:
|
||||
quantity = int(request.args.get("quantity", "1"))
|
||||
total = total_price(quantity)
|
||||
except ValueError as error:
|
||||
return jsonify(error=str(error)), 400
|
||||
return jsonify(quantity=quantity, total=total, currency="USD")
|
||||
@@ -0,0 +1,3 @@
|
||||
# Instructor compatibility ranges. requirements.txt locks the assessed versions.
|
||||
Flask>=3.1,<3.2
|
||||
gunicorn>=23,<24
|
||||
@@ -0,0 +1,9 @@
|
||||
blinker==1.9.0
|
||||
click==8.5.1
|
||||
Flask==3.1.3
|
||||
gunicorn==23.0.0
|
||||
itsdangerous==2.2.0
|
||||
Jinja2==3.1.6
|
||||
MarkupSafe==3.0.4
|
||||
packaging==26.3
|
||||
Werkzeug==3.1.9
|
||||
@@ -0,0 +1,25 @@
|
||||
import unittest
|
||||
from app import app, total_price
|
||||
|
||||
|
||||
class PricingTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.client = app.test_client()
|
||||
|
||||
def test_price(self):
|
||||
self.assertEqual(total_price(3), 30)
|
||||
response = self.client.get("/price?quantity=3")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json["total"], 30)
|
||||
|
||||
def test_invalid_quantity(self):
|
||||
for value in ["0", "-1", "1001", "abc", "1.5"]:
|
||||
self.assertEqual(self.client.get("/price?quantity=" + value).status_code, 400)
|
||||
|
||||
def test_release_identity(self):
|
||||
self.assertEqual(self.client.get("/").json["version"], "v1")
|
||||
self.assertEqual(self.client.get("/health").json["status"], "ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user