Adjust for a non-null MongoClient API (#14247)
This commit is contained in:
parent
bac109cdc8
commit
f4402b33e1
@ -1,6 +1,8 @@
|
|||||||
package com.baeldung.boot.csfle.service;
|
package com.baeldung.boot.csfle.service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.bson.BsonBinary;
|
import org.bson.BsonBinary;
|
||||||
@ -26,22 +28,32 @@ public class CitizenService {
|
|||||||
public static final String DETERMINISTIC_ALGORITHM = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic";
|
public static final String DETERMINISTIC_ALGORITHM = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic";
|
||||||
public static final String RANDOM_ALGORITHM = "AEAD_AES_256_CBC_HMAC_SHA_512-Random";
|
public static final String RANDOM_ALGORITHM = "AEAD_AES_256_CBC_HMAC_SHA_512-Random";
|
||||||
|
|
||||||
@Autowired
|
private final MongoTemplate mongo;
|
||||||
private MongoTemplate mongo;
|
private final EncryptionConfig encryptionConfig;
|
||||||
|
private final ClientEncryption clientEncryption;
|
||||||
|
|
||||||
@Autowired
|
public CitizenService(MongoTemplate mongo, EncryptionConfig encryptionConfig, ClientEncryption clientEncryption) {
|
||||||
private EncryptionConfig encryptionConfig;
|
this.mongo = mongo;
|
||||||
|
this.encryptionConfig = encryptionConfig;
|
||||||
@Autowired
|
this.clientEncryption = clientEncryption;
|
||||||
private ClientEncryption clientEncryption;
|
}
|
||||||
|
|
||||||
public Object save(Citizen citizen) {
|
public Object save(Citizen citizen) {
|
||||||
if (encryptionConfig.isAutoEncryption()) {
|
if (encryptionConfig.isAutoEncryption()) {
|
||||||
return mongo.save(citizen);
|
return mongo.save(citizen);
|
||||||
} else {
|
} else {
|
||||||
EncryptedCitizen encryptedCitizen = new EncryptedCitizen(citizen.getName());
|
EncryptedCitizen encryptedCitizen = new EncryptedCitizen(citizen.getName());
|
||||||
|
if (citizen.getEmail() != null) {
|
||||||
encryptedCitizen.setEmail(encrypt(citizen.getEmail(), DETERMINISTIC_ALGORITHM));
|
encryptedCitizen.setEmail(encrypt(citizen.getEmail(), DETERMINISTIC_ALGORITHM));
|
||||||
|
} else {
|
||||||
|
encryptedCitizen.setEmail(null);
|
||||||
|
|
||||||
|
}
|
||||||
|
if (citizen.getBirthYear() != null) {
|
||||||
encryptedCitizen.setBirthYear(encrypt(citizen.getBirthYear(), RANDOM_ALGORITHM));
|
encryptedCitizen.setBirthYear(encrypt(citizen.getBirthYear(), RANDOM_ALGORITHM));
|
||||||
|
} else {
|
||||||
|
encryptedCitizen.setBirthYear(null);
|
||||||
|
}
|
||||||
|
|
||||||
return mongo.save(encryptedCitizen);
|
return mongo.save(encryptedCitizen);
|
||||||
}
|
}
|
||||||
@ -78,8 +90,8 @@ public class CitizenService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Binary encrypt(BsonValue bsonValue, String algorithm) {
|
public Binary encrypt(BsonValue bsonValue, String algorithm) {
|
||||||
if (bsonValue == null)
|
Objects.requireNonNull(bsonValue);
|
||||||
return null;
|
Objects.requireNonNull(algorithm);
|
||||||
|
|
||||||
EncryptOptions options = new EncryptOptions(algorithm);
|
EncryptOptions options = new EncryptOptions(algorithm);
|
||||||
options.keyId(encryptionConfig.getDataKeyId());
|
options.keyId(encryptionConfig.getDataKeyId());
|
||||||
@ -89,42 +101,38 @@ public class CitizenService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Binary encrypt(String value, String algorithm) {
|
public Binary encrypt(String value, String algorithm) {
|
||||||
if (value == null)
|
Objects.requireNonNull(value);
|
||||||
return null;
|
Objects.requireNonNull(algorithm);
|
||||||
|
|
||||||
return encrypt(new BsonString(value), algorithm);
|
return encrypt(new BsonString(value), algorithm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Binary encrypt(Integer value, String algorithm) {
|
public Binary encrypt(Integer value, String algorithm) {
|
||||||
if (value == null)
|
Objects.requireNonNull(value);
|
||||||
return null;
|
Objects.requireNonNull(algorithm);
|
||||||
|
|
||||||
return encrypt(new BsonInt32(value), algorithm);
|
return encrypt(new BsonInt32(value), algorithm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BsonValue decryptProperty(Binary value) {
|
public BsonValue decryptProperty(Binary value) {
|
||||||
if (value == null)
|
Objects.requireNonNull(value);
|
||||||
return null;
|
|
||||||
|
|
||||||
return clientEncryption.decrypt(new BsonBinary(value.getType(), value.getData()));
|
return clientEncryption.decrypt(new BsonBinary(value.getType(), value.getData()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Citizen decrypt(EncryptedCitizen encrypted) {
|
private Citizen decrypt(EncryptedCitizen encrypted) {
|
||||||
if (encrypted == null)
|
Objects.requireNonNull(encrypted);
|
||||||
return null;
|
|
||||||
|
|
||||||
Citizen citizen = new Citizen(encrypted.getName());
|
Citizen citizen = new Citizen(encrypted.getName());
|
||||||
|
|
||||||
BsonValue decryptedBirthYear = decryptProperty(encrypted.getBirthYear());
|
BsonValue decryptedBirthYear = encrypted.getBirthYear() != null ? decryptProperty(encrypted.getBirthYear()) : null;
|
||||||
if (decryptedBirthYear != null) {
|
if (decryptedBirthYear != null) {
|
||||||
citizen.setBirthYear(decryptedBirthYear.asInt32()
|
citizen.setBirthYear(decryptedBirthYear.asInt32().intValue());
|
||||||
.intValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BsonValue decryptedEmail = decryptProperty(encrypted.getEmail());
|
BsonValue decryptedEmail = encrypted.getEmail() != null ? decryptProperty(encrypted.getEmail()) : null;
|
||||||
if (decryptedEmail != null) {
|
if (decryptedEmail != null) {
|
||||||
citizen.setEmail(decryptedEmail.asString()
|
citizen.setEmail(decryptedEmail.asString().getValue());
|
||||||
.getValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return citizen;
|
return citizen;
|
||||||
|
@ -17,8 +17,11 @@ import com.baeldung.boot.csfle.service.CitizenService;
|
|||||||
@RequestMapping("/citizen")
|
@RequestMapping("/citizen")
|
||||||
public class CitizenController {
|
public class CitizenController {
|
||||||
|
|
||||||
@Autowired
|
private final CitizenService service;
|
||||||
private CitizenService service;
|
|
||||||
|
public CitizenController(CitizenService service) {
|
||||||
|
this.service = service;
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Citizen> get() {
|
public List<Citizen> get() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user