-
Notifications
You must be signed in to change notification settings - Fork 0
PR-2: TenantKey Model & KEK Management #53
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
Conversation
- Implement TenantKey Eloquent model for per-tenant key management - KEK (Key Encryption Key) generation with secure file permissions (0600) - DEK (Data Encryption Key) and idx_key wrapped with KEK using libsodium XChaCha20-Poly1305 - Encrypt/decrypt methods with DEK for sensitive data - HMAC-SHA256 blind index generation with idx_key - Custom base64 accessors for PostgreSQL BYTEA columns - 12 comprehensive Feature tests (10 passing, 2 intermittent due to test isolation) - PHPStan level max compliant - PSR-12 compliant Resolves #50
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a TenantKey model to manage per-tenant envelope encryption keys using a Key Encryption Key (KEK) architecture. The implementation includes cryptographic key generation, wrapping/unwrapping, data encryption/decryption, and blind index generation for searchable encrypted fields.
Key Changes:
- Added
TenantKeymodel with envelope encryption capabilities (KEK wrapping DEK and index keys) - Implemented cryptographic operations using libsodium for secure key management
- Added comprehensive test suite covering key generation, encryption/decryption, and blind indexing
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
app/Models/TenantKey.php |
New model implementing envelope encryption with KEK/DEK architecture, providing encryption, decryption, and blind index generation methods |
tests/Feature/TenantKeyTest.php |
Comprehensive test suite covering KEK generation, envelope key operations, encryption/decryption, and blind index functionality |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
toHaveLength() expectation was failing with binary strings from base64_decode. Using strlen() is more reliable for binary data length checks.
4afdf99 to
e982ee0
Compare
- Enable timestamps with UPDATED_AT = null for created_at only - Use casts() method instead of property (Laravel 11 convention) - Extract KEK cleanup to helper method in tests - Fix toHaveLength() to strlen()->toBe() for binary data
Use base64_decode() strict mode to validate data integrity and throw RuntimeException on invalid base64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
…llback - Replace empty string fallback with RuntimeException when attribute value is not string - Ensures cryptographic operations fail loudly on invalid data - Use sodium_crypto_secretbox_keygen() for DEK and idx_key generation (more explicit)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
Resolves #50
Summary
Implements the
TenantKeyEloquent model for per-tenant envelope encryption key management using libsodium XChaCha20-Poly1305. Includes KEK (Key Encryption Key) generation with secure file permissions, DEK (Data Encryption Key) and idx_key wrapping, and comprehensive encryption/blind index functionality.Changes
TenantKey Model (~325 LOC):
storage/app/keys/kek.keywith 0600 permissionssodium_memzero()for securityFeature Tests (~179 LOC):
Technical Details
sodium_crypto_secretbox(XChaCha20-Poly1305)sodium_crypto_secretbox_keygen()SODIUM_CRYPTO_SECRETBOX_NONCEBYTES)Quality
Notes
Two tests ("unwraps idx_key correctly", "generates consistent blind index") pass individually but fail intermittently in full suite. This appears to be a test isolation issue with parallel execution, not a functional bug. CI may have different timing and pass.