Skip to content

Add AES-256 encryption for Redis cache values - #53

Merged
pditommaso merged 12 commits into
masterfrom
feat/encrypted-redis-cache
Mar 7, 2026
Merged

pditommaso merged 12 commits into
masterfrom
feat/encrypted-redis-cache

Conversation

@pditommaso

Copy link
Copy Markdown
Contributor

Summary

  • Convert all 7 lib-crypto Groovy source files to Java (tests remain Groovy/Spock)
  • Add configurable per-cache AES-256-CBC encryption to micronaut-cache-redis
  • CacheValueEncryptor delegates to CryptoHelper static methods — no crypto code duplication

Encryption design

  • Enabled via encryption.enabled: true + encryption.secret in cache config
  • Key derived once at startup via PBKDF2WithHmacSHA256 (65536 iterations)
  • Salt derived deterministically from cache name (SHA-256 → 16 bytes)
  • Random 16-byte IV per encrypt, prepended to ciphertext
  • Transparent to application code (@Cacheable, SyncCache, AsyncCache unchanged)
  • Decryption failures treated as cache misses (supports key rotation)

Test plan

  • Unit tests for CacheValueEncryptor (roundtrip, random IV, wrong password, different cache names)
  • Integration tests for encrypted cache operations (put/get, invalidate, backward compatibility)
  • Test fail-fast when encryption enabled without secret
  • All existing lib-crypto Groovy tests pass against Java sources
  • ./gradlew :lib-crypto:test :micronaut-cache-redis:test all green

🤖 Generated with Claude Code

pditommaso and others added 12 commits March 7, 2026 00:03
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Remove explicit encryption-salt config property. The salt is now
derived deterministically from the cache name via SHA-256 truncated
to 16 bytes. This simplifies config to just encryption-password.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use encryption.enabled and encryption.secret instead of flat
encryption-secret. This uses a nested @ConfigurationProperties
inner class for cleaner YAML structure.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add input validation in CacheValueEncryptor.decrypt() for
  corrupted/truncated data
- Catch decryption failures in RedisCache.decryptValue() and treat
  as cache miss (enables graceful key rotation)
- Clear PBEKeySpec password after key derivation
- Remove unused lib-crypto dependency from build.gradle

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ptor

Add CryptoHelper.deriveKey(password, salt) static method for PBKDF2
key derivation. CacheValueEncryptor now delegates to it instead of
duplicating the logic. Restores lib-crypto dependency.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add static encrypt/decrypt methods to CryptoHelper for pre-derived keys
- Refactor CryptoHelper instance methods to delegate to deriveKey (fixes missing clearPassword)
- Simplify CacheValueEncryptor to delegate to CryptoHelper + Sealed (removes duplicated AES logic)
- Add bounds check in Sealed.deserialize for null/short input
- Fix decryptValue to return empty byte[] instead of null on failure (prevents NPE in serializer)
- Fix cipher string casing inconsistency (PKCS5PADDING → PKCS5Padding)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@pditommaso

Copy link
Copy Markdown
Contributor Author

Summary of changes

lib-crypto: Groovy → Java conversion

All 7 source files converted to Java, keeping the existing Groovy/Spock tests unchanged:

  • CryptoHelper — symmetric encryption + PBKDF2 hashing, now with static encrypt(SecretKeySpec, byte[]) / decrypt(SecretKeySpec, Sealed) methods
  • Sealed — encrypted data model with serialize()/deserialize(), added bounds check
  • AsymmetricCipher — RSA hybrid encryption
  • EncryptedPacket — RSA encrypted bundle model
  • HmacSha1Signature — HMAC-SHA1 signatures
  • SecureRandomKey — random key generation
  • TokenHelper — hex token generation

micronaut-cache-redis: per-cache value encryption

  • CacheValueEncryptor — thin wrapper that derives key at startup via CryptoHelper.deriveKey(), delegates encrypt/decrypt to CryptoHelper static methods + Sealed.serialize()/deserialize() (no duplicated crypto logic)
  • RedisCacheConfiguration.EncryptionConfiguration — nested @ConfigurationProperties("encryption") with enabled + secret
  • RedisCache — wires encryptor in constructor, encryptValue()/decryptValue() applied transparently in get/put flow; decryption failure returns empty bytes (treated as cache miss)
  • README updated with config examples and design considerations

Config

redis:
  caches:
    my-cache:
      encryption:
        enabled: true
        secret: "my-passphrase"

@pditommaso
pditommaso merged commit 0a1aa18 into master Mar 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant