Skip to content

Commit

Permalink
Little fixes for PM
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Libanori committed Jan 9, 2024
1 parent 644a36b commit 5aee20a
Showing 1 changed file with 61 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
*/
public class DataEncryptionInitializer implements InitializerIFace {

private static final String TENANT = "DEFAULT_TENANT";

private static final Logger LOGGER = LogManager.getLogger(DataEncryptionInitializer.class);

public static final String DEFAULT_JOB_GROUP = "Encryption";
Expand Down Expand Up @@ -75,7 +77,8 @@ public void init(SourceBean config) {
String genAlgo = getValue(PROPERTY_GENERIC_ALGO);
String genPwd = getValue(PROPERTY_GENERIC_PWD);

LOGGER.warn("Reading encryption configuration: the system properties will take precedence over environment variables");
LOGGER.warn(
"Reading encryption configuration: the system properties will take precedence over environment variables");

if (ObjectUtils.anyNotNull(pmUrl, pmUser, pmPwd, pmApp, pmAlgo, genAlgo, genPwd)) {
LOGGER.warn("Found some encryption configuration");
Expand All @@ -84,11 +87,13 @@ public void init(SourceBean config) {
EncryptionConfiguration cfg = null;

if (anyNull(pmUrl, pmUser, pmPwd, pmApp, pmAlgo)) {
LOGGER.error("Failing to read Privacy Manager configuration from both system properties and system environment: you must provide all the configuration values listed in the documentation.");
LOGGER.error(
"Failing to read Privacy Manager configuration from both system properties and system environment: you must provide all the configuration values listed in the documentation.");
LOGGER.error("Trying with a generic algorithm");

if (anyNull(genAlgo, genPwd)) {
LOGGER.error("Failing to read generic encryption algorithm configuration from both system properties and system environment: you must provide all the configuration values listed in the documentation.");
LOGGER.error(
"Failing to read generic encryption algorithm configuration from both system properties and system environment: you must provide all the configuration values listed in the documentation.");
} else {
cfg = configureGenericDecryption(genAlgo, genPwd, cfgKey);
}
Expand All @@ -97,8 +102,7 @@ public void init(SourceBean config) {
}

if (cfg != null) {
EncryptionPreferencesRegistry.getInstance()
.addConfiguration(cfgKey, cfg);
EncryptionPreferencesRegistry.getInstance().addConfiguration(cfgKey, cfg);

if (PRIVACY_MANAGER.equals(cfg.getType())) {
scheduleJobToRetrieveThePassword(cfg);
Expand All @@ -122,49 +126,70 @@ private void scheduleJobToRetrieveThePassword(EncryptionConfiguration cfg) {
ISchedulerDAO schedulerDAO = DAOFactory.getSchedulerDAO();

// The following job is cross tenant but we need to set this anyway
schedulerDAO.setTenant("DEFAULT_TENANT");

Job jobDetail = schedulerDAO.loadJob(DEFAULT_JOB_GROUP, DEFAULT_JOB_NAME);
if (jobDetail == null) {
String pmUrl = cfg.getPmUrl();
String pmUser = cfg.getPmUser();
String pmApplication = cfg.getPmApplication();
String pmPwd = cfg.getPmPwd();

jobDetail = new Job();
jobDetail.setName(DEFAULT_JOB_NAME);
jobDetail.setGroupName(DEFAULT_JOB_GROUP);
jobDetail.setDescription(String.format(DEFAULT_JOB_DESC, pmUrl, pmUser, pmApplication));
jobDetail.setDurable(true);
jobDetail.setVolatile(false);
jobDetail.setRequestsRecovery(true);
jobDetail.setJobClass(GetPasswordFromPrivacyManagerJob.class);
jobDetail.addParameter(GetPasswordFromPrivacyManagerJob.PARAM_PM_URL, pmUrl);
jobDetail.addParameter(GetPasswordFromPrivacyManagerJob.PARAM_PM_USER, pmUser);
jobDetail.addParameter(GetPasswordFromPrivacyManagerJob.PARAM_PM_PWD, pmPwd);
jobDetail.addParameter(GetPasswordFromPrivacyManagerJob.PARAM_PM_APP, pmApplication);

schedulerDAO.insertJob(jobDetail);
LOGGER.debug("Added job with name " + DEFAULT_JOB_NAME + " in group " + DEFAULT_JOB_GROUP);
}
schedulerDAO.setTenant(TENANT);

schedulerDAO.deleteTrigger(DEFAULT_TRIGGER_NAME, DEFAULT_TRIGGER_GROUP);
deleteJobForPrivacyManager(schedulerDAO);
Job jobDetail = createJobForPrivacyManager(schedulerDAO, cfg);

deleteTriggerForPrivacyManager(schedulerDAO);
createTriggerForPrivacyManager(schedulerDAO, jobDetail);
}

private void createTriggerForPrivacyManager(ISchedulerDAO schedulerDAO, Job jobDetail) {
LOGGER.debug("Adding trigger with name {} in group {}", DEFAULT_TRIGGER_NAME, DEFAULT_TRIGGER_GROUP);
Trigger simpleTrigger = new Trigger();
simpleTrigger.setName(DEFAULT_TRIGGER_NAME);
simpleTrigger.setGroupName(DEFAULT_TRIGGER_GROUP);
simpleTrigger.setJob(jobDetail);
simpleTrigger.setRunImmediately(true);

schedulerDAO.insertTrigger(simpleTrigger);
LOGGER.debug("Added trigger with name " + DEFAULT_TRIGGER_NAME + " in group " + DEFAULT_TRIGGER_GROUP);
LOGGER.debug("Added trigger with name {} in group {}", DEFAULT_TRIGGER_NAME, DEFAULT_TRIGGER_GROUP);
}

private void deleteTriggerForPrivacyManager(ISchedulerDAO schedulerDAO) {
LOGGER.debug("Deleting trigger with name {} in group {}", DEFAULT_TRIGGER_NAME, DEFAULT_TRIGGER_GROUP);
schedulerDAO.deleteTrigger(DEFAULT_TRIGGER_NAME, DEFAULT_TRIGGER_GROUP);
LOGGER.debug("Deleted trigger with name {} in group {}", DEFAULT_TRIGGER_NAME, DEFAULT_TRIGGER_GROUP);
}

private void deleteJobForPrivacyManager(ISchedulerDAO schedulerDAO) {
LOGGER.debug("Deleting job with name {} in group {}", DEFAULT_JOB_NAME, DEFAULT_JOB_GROUP);
schedulerDAO.deleteJob(DEFAULT_JOB_NAME, DEFAULT_JOB_GROUP);
LOGGER.debug("Deleted job with name {} in group {}", DEFAULT_JOB_NAME, DEFAULT_JOB_GROUP);
}

private Job createJobForPrivacyManager(ISchedulerDAO schedulerDAO, EncryptionConfiguration cfg) {
LOGGER.debug("Adding job with name {} in group {}", DEFAULT_JOB_NAME, DEFAULT_JOB_GROUP);
Job jobDetail = null;

String pmUrl = cfg.getPmUrl();
String pmUser = cfg.getPmUser();
String pmApplication = cfg.getPmApplication();
String pmPwd = cfg.getPmPwd();

jobDetail = new Job();
jobDetail.setName(DEFAULT_JOB_NAME);
jobDetail.setGroupName(DEFAULT_JOB_GROUP);
jobDetail.setDescription(String.format(DEFAULT_JOB_DESC, pmUrl, pmUser, pmApplication));
jobDetail.setDurable(true);
jobDetail.setVolatile(false);
jobDetail.setRequestsRecovery(true);
jobDetail.setJobClass(GetPasswordFromPrivacyManagerJob.class);
jobDetail.addParameter(GetPasswordFromPrivacyManagerJob.PARAM_PM_URL, pmUrl);
jobDetail.addParameter(GetPasswordFromPrivacyManagerJob.PARAM_PM_USER, pmUser);
jobDetail.addParameter(GetPasswordFromPrivacyManagerJob.PARAM_PM_PWD, pmPwd);
jobDetail.addParameter(GetPasswordFromPrivacyManagerJob.PARAM_PM_APP, pmApplication);

schedulerDAO.insertJob(jobDetail);
LOGGER.debug("Created job with name {} in group {}", DEFAULT_JOB_NAME, DEFAULT_JOB_GROUP);
return jobDetail;
}

private String getValue(String key) {
String ret = null;

ret = Optional.ofNullable(getProperty(key))
.orElse(getenv(key));
ret = Optional.ofNullable(getProperty(key)).orElse(getenv(key));

return ret;
}
Expand All @@ -189,7 +214,8 @@ private EncryptionConfiguration configureGenericDecryption(String genAlgo, Strin
return cfg;
}

private EncryptionConfiguration configurePrivacyManager(String pmUrl, String pmUser, String pmPwd, String pmApp, String pmAlgo) {
private EncryptionConfiguration configurePrivacyManager(String pmUrl, String pmUser, String pmPwd, String pmApp,
String pmAlgo) {
EncryptionConfiguration cfg;
cfg = new EncryptionConfiguration(PRIVACY_MANAGER);

Expand All @@ -205,4 +231,3 @@ private EncryptionConfiguration configurePrivacyManager(String pmUrl, String pmU
}

}

0 comments on commit 5aee20a

Please sign in to comment.