Repository navigation
feat(python): add a customer directory example #1621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+2,035
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Customer directory | ||
|
|
||
| This is an internal example of the `stackencrypt` Python SDK. It is a FastAPI service that stores customers in PostgreSQL. | ||
|
|
||
| | Field | Stored as | | ||
| |---|---| | ||
| | `email` | Encrypted, with exact-match search | | ||
| | `name` | Encrypted, with no search | | ||
| | `plan` | Plaintext | | ||
| | `created_at` | Plaintext | | ||
|
|
||
| The SDK is not released, so its API can change. This example installs the SDK from this checkout. | ||
|
|
||
| ## Before you start | ||
|
|
||
| You need Docker, mise, and a Rust toolchain. mise installs the toolchain. | ||
|
|
||
| You also need CipherStash credentials. Set `CS_CLIENT_ACCESS_KEY`, `CS_WORKSPACE_CRN`, `CS_CLIENT_ID`, and `CS_CLIENT_KEY`. Or log in with `stash auth login`. | ||
|
|
||
| ## Run it | ||
|
|
||
| Run these commands from this folder. | ||
|
|
||
| `mise run setup` deletes this example's database and creates it again. Nothing else is deleted. | ||
|
|
||
| ```sh | ||
| mise run setup | ||
| mise run dev | ||
| ``` | ||
|
|
||
| Add a customer, then find the customer by exact email: | ||
|
|
||
| ```sh | ||
| curl -X POST http://127.0.0.1:8000/customers \ | ||
| -H 'content-type: application/json' \ | ||
| -d '{"email":"ada@example.com","name":"Ada Lovelace","plan":"free"}' | ||
|
|
||
| curl -X POST http://127.0.0.1:8000/customers/search \ | ||
| -H 'content-type: application/json' \ | ||
| -d '{"email":"ada@example.com"}' | ||
|
|
||
| curl 'http://127.0.0.1:8000/customers?limit=10' | ||
| ``` | ||
|
|
||
| The search is a `POST` so that the email stays out of URLs and access logs. | ||
|
|
||
| ## The SQLAlchemy version | ||
|
|
||
| `app_sqlalchemy.py` is the same service with the model declared in SQLAlchemy. It uses the same table, so the two apps share rows. You can run both at the same time. | ||
|
|
||
| ```sh | ||
| mise run dev:sqlalchemy # http://127.0.0.1:8001 | ||
| ``` | ||
|
|
||
| A SQLAlchemy object contains plaintext, and the columns must receive ciphertext. So the app calls `encrypt` before an `INSERT` and `decrypt` after a `SELECT`. It does not use `Session`. | ||
|
|
||
| ## Run the tests | ||
|
|
||
| The tests use the real ZeroKMS service and the example's PostgreSQL. They run against both apps. They empty the `customers` table. CI does not run them. | ||
|
|
||
| ```sh | ||
| mise run test | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """A customer directory: a FastAPI service that stores encrypted customers. | ||
|
|
||
| `email` is encrypted and searchable by exact match. `name` is encrypted with | ||
| no search. `plan` and `created_at` are stored as plaintext. | ||
|
|
||
| The handlers are plain `def` functions. FastAPI runs them on a thread pool, so | ||
| requests share the one `Cipher` below at the same time. | ||
| """ | ||
|
|
||
| import os | ||
| from datetime import datetime, timezone | ||
| from typing import Annotated, List, Optional | ||
|
|
||
| import psycopg | ||
| import stackencrypt | ||
| from fastapi import FastAPI, HTTPException, Query, Request | ||
| from fastapi.responses import JSONResponse | ||
| from psycopg.rows import dict_row | ||
| from pydantic import BaseModel, ConfigDict, Field | ||
| from stackencrypt import Passthrough, Text, TextEq | ||
|
|
||
| from errors import install_error_handlers, log_error, problem | ||
|
|
||
| DATABASE_URL = os.environ["DATABASE_URL"] | ||
| SELECT_CUSTOMER = "SELECT email, name, plan, created_at FROM customers" | ||
|
|
||
|
|
||
| def utc_now() -> datetime: | ||
| return datetime.now(timezone.utc) | ||
|
|
||
|
|
||
| @stackencrypt.model("customers") | ||
| class Customer(BaseModel): | ||
| # A Pydantic error prints the input by default, and the input is plaintext. | ||
| model_config = ConfigDict(hide_input_in_errors=True) | ||
|
|
||
| email: Annotated[str, TextEq] | ||
| name: Annotated[str, Text] | ||
| plan: Annotated[str, Passthrough] | ||
| created_at: Annotated[datetime, Passthrough] = Field(default_factory=utc_now) | ||
|
|
||
|
|
||
| class Search(BaseModel): | ||
| model_config = ConfigDict(hide_input_in_errors=True) | ||
|
|
||
| email: str | ||
|
|
||
|
|
||
| # One Cipher for the process: making one checks the model and the credentials. | ||
| cipher = stackencrypt.Cipher(Customer) | ||
|
|
||
| app = FastAPI(title="Customer directory") | ||
|
|
||
|
|
||
| install_error_handlers(app) | ||
|
|
||
|
|
||
| @app.exception_handler(psycopg.errors.UniqueViolation) | ||
| def duplicate(request: Request, error: psycopg.errors.UniqueViolation) -> JSONResponse: | ||
| log_error(error) | ||
| return problem(409, "A customer with this email already exists.") | ||
|
|
||
|
|
||
| @app.post("/customers", status_code=201, response_model=Customer) | ||
| def add_customer(customer: Customer) -> Customer: | ||
| row = cipher.encrypt([customer])[0] | ||
| with psycopg.connect(DATABASE_URL) as conn: | ||
| conn.execute( | ||
| "INSERT INTO customers (email, name, plan, created_at)" | ||
| " VALUES (%(email)s, %(name)s, %(plan)s, %(created_at)s)", | ||
| row, | ||
| ) | ||
| return customer | ||
|
|
||
|
|
||
| @app.post("/customers/search", response_model=Customer) | ||
| def find_customer(search: Search) -> Customer: | ||
| # The email is in the body so that it stays out of URLs and access logs. | ||
| term = cipher.query("email", search.email) | ||
| with psycopg.connect(DATABASE_URL) as conn: | ||
| with conn.cursor(row_factory=dict_row) as cur: | ||
| cur.execute(f"{SELECT_CUSTOMER} WHERE email = %s::eql_v4.query_text_eq", (term,)) | ||
| found: Optional[dict] = cur.fetchone() | ||
| if found is None: | ||
| raise HTTPException(status_code=404, detail="No customer has this email.") | ||
| return cipher.decrypt([found])[0] | ||
|
|
||
|
|
||
| @app.get("/customers", response_model=List[Customer]) | ||
| def list_customers(limit: int = Query(default=100, ge=1, le=500)) -> List[Customer]: | ||
| with psycopg.connect(DATABASE_URL) as conn: | ||
| with conn.cursor(row_factory=dict_row) as cur: | ||
| cur.execute(f"{SELECT_CUSTOMER} ORDER BY id LIMIT %s", (limit,)) | ||
| return cipher.decrypt(cur.fetchall()) |
132 changes: 132 additions & 0 deletions
132
languages/python/examples/customer-directory/app_sqlalchemy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| """The customer directory again, with the model declared in SQLAlchemy. | ||
|
|
||
| It uses the table of `app.py`, so the two apps share rows. | ||
|
|
||
| A SQLAlchemy object contains plaintext, and the columns must receive | ||
| ciphertext. So the app calls `encrypt` before an INSERT and `decrypt` after a | ||
| SELECT. It does not use `Session`. | ||
| """ | ||
|
|
||
| import os | ||
| from datetime import datetime, timezone | ||
| from typing import Annotated, List, Optional | ||
|
|
||
| import sqlalchemy as sa | ||
| import stackencrypt | ||
| from fastapi import FastAPI, HTTPException, Query, Request | ||
| from fastapi.responses import JSONResponse | ||
| from psycopg.errors import UniqueViolation | ||
| from pydantic import BaseModel, ConfigDict, Field | ||
| from sqlalchemy.exc import IntegrityError | ||
| from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column | ||
| from sqlalchemy.types import UserDefinedType | ||
| from stackencrypt import Ignore, Passthrough, Text, TextEq | ||
|
|
||
| from errors import install_error_handlers, log_error, problem | ||
|
|
||
| # hide_parameters leaves the bound values out of SQLAlchemy's error text. | ||
| # PostgreSQL's own detail can still quote the start of a rejected value. | ||
| engine = sa.create_engine( | ||
| os.environ["DATABASE_URL"].replace("postgresql://", "postgresql+psycopg://", 1), | ||
|
yujiyokoo marked this conversation as resolved.
Resolved
|
||
| hide_parameters=True, | ||
| ) | ||
|
|
||
|
|
||
| class EqlType(UserDefinedType): | ||
| """An EQL column type. The tests fail with `sa.Text`, `sa.String`, or `sa.JSON`.""" | ||
|
|
||
| cache_ok = True | ||
|
|
||
| def __init__(self, sql_name: str) -> None: | ||
| self.sql_name = sql_name | ||
|
|
||
| def get_col_spec(self, **kwargs) -> str: | ||
| return self.sql_name | ||
|
|
||
|
|
||
| class Base(DeclarativeBase): | ||
| pass | ||
|
|
||
|
|
||
| @stackencrypt.model("customers") | ||
| class Customer(Base): | ||
| __tablename__ = "customers" | ||
|
|
||
| id: Mapped[Annotated[int, Ignore]] = mapped_column( | ||
| sa.BigInteger, sa.Identity(always=True), primary_key=True | ||
| ) | ||
| email: Mapped[Annotated[str, TextEq]] = mapped_column(EqlType("public.eql_v4_text_eq")) | ||
| name: Mapped[Annotated[str, Text]] = mapped_column(EqlType("public.eql_v4_text")) | ||
| plan: Mapped[Annotated[str, Passthrough]] | ||
| created_at: Mapped[Annotated[datetime, Passthrough]] = mapped_column( | ||
| sa.DateTime(timezone=True) | ||
| ) | ||
|
|
||
|
|
||
| def utc_now() -> datetime: | ||
| return datetime.now(timezone.utc) | ||
|
|
||
|
|
||
| class CustomerBody(BaseModel): | ||
| """The JSON shape of a customer, for requests and responses.""" | ||
|
|
||
| # A Pydantic error prints the input by default, and the input is plaintext. | ||
| model_config = ConfigDict(hide_input_in_errors=True, from_attributes=True) | ||
|
|
||
| email: str | ||
| name: str | ||
| plan: str | ||
| created_at: datetime = Field(default_factory=utc_now) | ||
|
|
||
|
|
||
| class Search(BaseModel): | ||
| model_config = ConfigDict(hide_input_in_errors=True) | ||
|
|
||
| email: str | ||
|
|
||
|
|
||
| # `id` is left out: the model marks it Ignore, and `decrypt` does not want it. | ||
| COLUMNS = (Customer.email, Customer.name, Customer.plan, Customer.created_at) | ||
|
|
||
| # One Cipher for the process: making one checks the model and the credentials. | ||
| cipher = stackencrypt.Cipher(Customer) | ||
|
|
||
| app = FastAPI(title="Customer directory (SQLAlchemy)") | ||
| install_error_handlers(app) | ||
|
|
||
|
|
||
| @app.exception_handler(IntegrityError) | ||
| def integrity_error(request: Request, error: IntegrityError) -> JSONResponse: | ||
| log_error(error) | ||
| if isinstance(error.orig, UniqueViolation): | ||
| return problem(409, "A customer with this email already exists.") | ||
| return problem(500, "The request failed.") | ||
|
|
||
|
|
||
| @app.post("/customers", status_code=201, response_model=CustomerBody) | ||
| def add_customer(body: CustomerBody) -> CustomerBody: | ||
| rows = cipher.encrypt([Customer(**body.model_dump())]) | ||
| with engine.begin() as conn: | ||
| conn.execute(sa.insert(Customer.__table__), rows) | ||
| return body | ||
|
|
||
|
|
||
| @app.post("/customers/search", response_model=CustomerBody) | ||
| def find_customer(search: Search) -> Customer: | ||
| # The email is in the body so that it stays out of URLs and access logs. | ||
| term = sa.literal(cipher.query("email", search.email), sa.Text) | ||
| query = sa.select(*COLUMNS).where( | ||
| Customer.email == sa.cast(term, EqlType("eql_v4.query_text_eq")) | ||
| ) | ||
| with engine.connect() as conn: | ||
| found: Optional[sa.RowMapping] = conn.execute(query).mappings().first() | ||
| if found is None: | ||
| raise HTTPException(status_code=404, detail="No customer has this email.") | ||
| return cipher.decrypt([found])[0] | ||
|
|
||
|
|
||
| @app.get("/customers", response_model=List[CustomerBody]) | ||
| def list_customers(limit: int = Query(default=100, ge=1, le=500)) -> List[Customer]: | ||
| query = sa.select(*COLUMNS).order_by(Customer.id).limit(limit) | ||
| with engine.connect() as conn: | ||
| return cipher.decrypt(conn.execute(query).mappings().all()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| services: | ||
| db: | ||
| image: postgres:17 | ||
| environment: | ||
| POSTGRES_PASSWORD: postgres | ||
| POSTGRES_DB: customer_directory | ||
| ports: | ||
| - 127.0.0.1:5438:5432 | ||
| healthcheck: | ||
| test: [CMD-SHELL, pg_isready -U postgres -d customer_directory] | ||
| interval: 1s | ||
| timeout: 3s | ||
| retries: 30 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """The error handling that both apps share.""" | ||
|
|
||
| import logging | ||
|
|
||
| import stackencrypt | ||
| from fastapi import FastAPI, Request | ||
| from fastapi.exceptions import RequestValidationError | ||
| from fastapi.responses import JSONResponse | ||
|
|
||
| log = logging.getLogger("customer_directory") | ||
|
|
||
|
|
||
| def log_error(error: Exception) -> None: | ||
| """Log what identifies an error, and never the error's own text. | ||
|
|
||
| An SDK error's message never contains a value, so `code` and `field` are | ||
| safe. PostgreSQL's primary message has no value in it. Its detail can | ||
| quote the value, so the detail is not logged. Any other exception's text | ||
| could contain plaintext, so only its type is logged. | ||
| """ | ||
| # SQLAlchemy wraps the driver's error and keeps it in `orig`. | ||
| cause = getattr(error, "orig", error) | ||
| diag = getattr(cause, "diag", None) | ||
| log.error( | ||
| "customer_error type=%s code=%s field=%s sqlstate=%s message=%s", | ||
| type(error).__name__, | ||
| getattr(error, "code", None) if cause is error else None, | ||
| getattr(error, "field", None), | ||
| getattr(cause, "sqlstate", None), | ||
| getattr(diag, "message_primary", None), | ||
| ) | ||
|
|
||
|
|
||
| def problem(status: int, detail: str) -> JSONResponse: | ||
| return JSONResponse(status_code=status, content={"detail": detail}) | ||
|
|
||
|
|
||
| def install_error_handlers(app: FastAPI) -> None: | ||
| """Give an app fixed error responses, with the details in the log.""" | ||
|
|
||
| @app.middleware("http") | ||
| async def fixed_500(request: Request, call_next): | ||
| # This catches the error here because an `Exception` handler would | ||
| # still re-raise it, and Uvicorn would then log its text. | ||
| try: | ||
| return await call_next(request) | ||
| except Exception as error: | ||
| log_error(error) | ||
| return problem(500, "The request failed.") | ||
|
|
||
| @app.exception_handler(RequestValidationError) | ||
| def invalid_request(request: Request, error: RequestValidationError) -> JSONResponse: | ||
| # FastAPI's default response repeats the rejected input. | ||
| log_error(error) | ||
| return problem(422, "A field has an invalid value.") | ||
|
|
||
| @app.exception_handler(stackencrypt.InvalidValueError) | ||
| def invalid_value(request: Request, error: stackencrypt.InvalidValueError) -> JSONResponse: | ||
| log_error(error) | ||
| return problem(422, "A field has an invalid value.") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.