spring-projects / spring-projects/spring-data-mongodb

Supply more useful variables to @Encrypted keyId SPEL expression

Open
#4,304 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: ideal-for-contribution type: enhancement
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

The keyId SPEL expression for the @Encrypted annotation is great for avoiding hard-coding of encryption key ids. See docs here. Example:

@Document
@Encrypted(keyId = "#{mongocrypt.keyId(#target)}")
static class Patient {

    @Id String id;
    String name;

    @Encrypted(algorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Random")
    String bloodType;

    @Encrypted(algorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")
    Integer ssn;
}

However the value bound to #target isn't particularly useful, it's just the simple class name (minus the package!) of the annotated class. See this line:

ctx.setVariable("target", getType().getSimpleName());

This limits the usefulness of the SPEL expression, especially in scenarios where users might want to use different encryption keys for different collections.

A simple but impactful enhancement would be to bind collection as a variable:

ctx.setVariable("collection", getCollection());

Curious to hear people's thoughts on this.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at BasicMongoPersistentEntity.java around the shown ctx.setVariable("target", getType().getSimpleName()) line and inspect how getCollection() is available there. Bind the collection name as an additional SPEL variable, then verify that @Encrypted keyId expressions can use it for collection-specific key selection.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb, spring
Domain
databases, security
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.