Repository navigation
[authentication/2.6.1] Commit enrollment decisions transactionally #71
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
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add the internal transactional enrollment writer with current approval checks, | ||
| atomic completion records, and SQLite crash recovery. Public enrollment still | ||
| stops at private prepared state until client activation support is connected. |
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,123 @@ | ||
| """One durable enrollment result, committed only after private client readiness.""" | ||
|
|
||
| from collections.abc import Callable | ||
| from contextlib import closing | ||
| from datetime import datetime | ||
| import json | ||
| from pathlib import Path | ||
| import sqlite3 | ||
|
|
||
| from cryptography.hazmat.primitives import serialization | ||
|
|
||
| from .authority import _read_record | ||
| from .credentials import Identity, Role, verify_certificate | ||
| from .enrollment import ApprovedRequest | ||
| from .enrollment_receipt import result_fields | ||
| from .generations import ACTIVE, _control, _database | ||
| from .prepared import PREPARED_TAG | ||
|
|
||
|
|
||
| def activate( | ||
| root: Path, | ||
| approval: ApprovedRequest, | ||
| raw: bytes, | ||
| accounts: Callable[[], set[tuple[str, str]]], | ||
| check: Callable[[], None], | ||
| now: datetime, | ||
| ) -> bytes: | ||
| """Instance, request and responder ownership span this bounded worker call. | ||
|
|
||
| The exact result contains the identity, all certificates, recipient key and | ||
| approved grants. No second representation of those authorities is published. | ||
| """ | ||
| ids, fields = result_fields(raw) | ||
| ad = approval.request.advertisement | ||
| offset = len(PREPARED_TAG) + 144 | ||
| if ( | ||
| raw[offset : offset + 22] != ad.selector.encode() | ||
| or raw[offset + 22 : offset + 54] != bytes.fromhex(ad.digest) | ||
| or raw[offset + 54 : offset + 86] != approval.request.revision.encode() | ||
| or int.from_bytes(raw[offset + 86 : offset + 94], "big") <= now.timestamp() | ||
| or fields[5] != ad.recipient_spki | ||
| or tuple(tuple(pair) for pair in json.loads(fields[8])) != approval.grants | ||
| ): | ||
| raise ValueError("Enrollment activation unavailable") | ||
| active = _control(root / ACTIVE) | ||
| if active is None: | ||
| raise ValueError("Enrollment activation unavailable") | ||
| path = _database(root, active["active"]) | ||
| authority = _read_record(path) | ||
| if ids[:2] != [authority.server_id, authority.owner_id]: | ||
| raise ValueError("Enrollment activation unavailable") | ||
| for index, identity, issuer, issuer_id, public in ( | ||
| (3, Identity(Role.PORTABLE, ids[2]), fields[2], ids[1], ad.portable_spki), | ||
| ( | ||
| 4, | ||
| Identity(Role.ACCESS, ids[2], ids[0], ids[3]), | ||
| fields[0], | ||
| ids[0], | ||
| ad.access_spki, | ||
| ), | ||
| ): | ||
| cert = verify_certificate( | ||
| fields[index], identity, issuer_der=issuer, issuer_id=issuer_id, now=now | ||
| ) | ||
| if ( | ||
| cert.public_key().public_bytes( | ||
| serialization.Encoding.DER, | ||
| serialization.PublicFormat.SubjectPublicKeyInfo, | ||
| ) | ||
| != public | ||
| ): | ||
| raise ValueError("Enrollment activation unavailable") | ||
| with closing(sqlite3.connect(path, timeout=0, isolation_level=None)) as db: | ||
| db.execute("PRAGMA synchronous=FULL") | ||
| db.execute("BEGIN IMMEDIATE") | ||
| try: | ||
| stored = dict(db.execute("SELECT role, certificate_der FROM credentials")) | ||
| if ( | ||
| fields[0] != stored[Role.SERVER_AUTHORITY.value] | ||
| or fields[2] != stored[Role.OWNER.value] | ||
| ): | ||
| raise ValueError("Enrollment activation unavailable") | ||
| db.execute( | ||
| "CREATE TABLE IF NOT EXISTS enrollments (enrollment_id TEXT PRIMARY KEY, " | ||
| "prepared BLOB NOT NULL CHECK(length(prepared)<=65519), " | ||
| "denied INTEGER NOT NULL CHECK(denied IN (0,1)), " | ||
| "inactive_after INTEGER NOT NULL, selector TEXT NOT NULL UNIQUE, " | ||
| "revision TEXT NOT NULL)" | ||
| ) | ||
| existing = db.execute( | ||
| "SELECT prepared, denied, inactive_after FROM enrollments " | ||
| "WHERE enrollment_id=? OR selector=?", | ||
| (ids[3], ad.selector), | ||
| ).fetchone() | ||
| if existing is not None: | ||
| if ( | ||
| bytes(existing[0]) != raw | ||
| or existing[1] | ||
| or existing[2] <= now.timestamp() | ||
| ): | ||
| raise ValueError("Enrollment activation unavailable") | ||
| else: | ||
| db.execute( | ||
| "INSERT INTO enrollments VALUES (?, ?, 0, ?, ?, ?)", | ||
| ( | ||
| ids[3], | ||
| raw, | ||
| int(now.timestamp()) + 180 * 86400, | ||
| ad.selector, | ||
| approval.request.revision, | ||
| ), | ||
| ) | ||
| if not set(approval.grants) <= accounts(): | ||
| raise ValueError("An approved account is no longer configured") | ||
| # Account evaluation can invalidate or outlive the original request. | ||
| # Consume its authorization only after that work, under the live | ||
| # caller's attempt/approval locks. No such lock spans SQLite I/O. | ||
| check() | ||
| db.commit() | ||
| except BaseException: | ||
| db.rollback() | ||
| raise | ||
| return raw |
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
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.