Mongodb Auto encryption with Java spring boot

120 Views Asked by At

Use case: need to auto encrypt while saving and auto decrypt while reading data from mongodb

Current system Setup:

  1. Using mongo db version of 5.0.20 which is in remote server.
  2. Spring boot version 2.3.12
  3. Java 1.8
  4. Mongodb driver version 4.4+
  5. Mongo-ctypt 1.7.1

Configuration Created __keyVault collection in db1 in above mentioned remote server where we have student collection which has emailAddress field to be encrypted.

Has master key generated in local and kept in masterKey.txt which is being read during mongo config.

Without mongocryptd in my system environmental variable it gave mongocryptd not found. Downloaded mongo libraries from mongodb resources site and added mongocryptd to path variable. Then Application is up.but, while saving data using mongo update, getting mongo client exception Error Exception in encryption library: Exception in encryption library: Command failed with error 51093 (Location51093): 'A non-static (JSONPointer) keyId is not supported.' on server localhost:27020. The full response is {"ok": 0.0, "errmsg": "A non-static (JSONPointer) keyId is not supported.", "code": 51093, "codeName": "Location51093"}

If we remove encyptmetadata and keyId from jsonschema it's giving encrypted metadata not found.

Sample Jsonschema snippet:

Document jsonSchema = new Document()
  .append("bsonType", "object")
  .append("encryptMetadata", new Document()
    .append("keyId", "/studentEmailId"))
    .append("properties", new Document()
    .append("student", new Document()
    .append("bsonType", "object")
    .append("properties", new Document()
      .append("demographic", new Document()
        .append("bsonType", "object")
        .append("properties", new Document()
          .append("emailAddress", new Document()
            .append("encrypt", new Document()
              .append("bsonType", "string")
              .append("algorithm","AEAD_AES_256_CBC_HMAC_SHA_512-Random")
            )
          )
        )
      )
    )
  )
);

HashMap<String, BsonDocument> schemaMap = new HashMap<String, BsonDocument>();

schemaMap.put("db1.users", BsonDocument.parse(jsonSchema.toJson()));

Any suggestions on how to proceed further.

Tried by changing mongo-crypt version. Removed /. Removed encrypted metadata. Added data in __keyvault collection with same keyId in jsonschema along with master key and keyAlt names. But nothing worked

0

There are 0 best solutions below