-
Notifications
You must be signed in to change notification settings - Fork 5
Email notification for cert expiry #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
garaimanoj
wants to merge
10
commits into
stfc:master
Choose a base branch
from
garaimanoj:GT-1437-email-notification-for-cert-expiry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9880286
Email notification for cert expiry
garaimanoj 7068a1f
Add email subject
garaimanoj 74dbe7e
Add tests and get schedule time from property file
garaimanoj 1158d37
Catch up with failed cert reminder
garaimanoj 048587e
Update java doc for sendEmailReminderToUserOnCertExpiry
garaimanoj 2bcee65
Add comment
garaimanoj cccf4c5
Update src/main/webapp/WEB-INF/freemarker/email/emailUserCertExpiryRe…
garaimanoj e5828e9
Update src/main/webapp/WEB-INF/freemarker/email/emailUserCertExpiryRe…
garaimanoj 3eb7f63
Add reminder summary log
garaimanoj 4608284
Format code
garaimanoj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright (C) 2015 STFC | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.ac.ngs; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.scheduling.annotation.EnableScheduling; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.scheduling.annotation.SchedulingConfigurer; | ||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; | ||
| import org.springframework.scheduling.config.ScheduledTaskRegistrar; | ||
|
|
||
| import uk.ac.ngs.service.CertificateService; | ||
|
|
||
| @Configuration | ||
| @EnableScheduling | ||
| public class TaskConfig implements SchedulingConfigurer { | ||
| public static final Logger log = LoggerFactory.getLogger(TaskConfig.class); | ||
|
|
||
| private CertificateService certificateService; | ||
|
|
||
| public TaskConfig(CertificateService certificateService) { | ||
| this.certificateService = certificateService; | ||
| } | ||
|
|
||
|
|
||
| @Scheduled(cron = "${cert.expiry.reminder.cron:0 0 7 * * ?}") | ||
| public void runDailyCertificateExpiryReminderJob() { | ||
|
|
||
| log.info("Starting daily certificate expiry reminder job"); | ||
|
|
||
| long startTime = System.currentTimeMillis(); | ||
|
|
||
| try { | ||
| certificateService.sendCertificateExpiryReminders(); | ||
|
|
||
| long durationMs = System.currentTimeMillis() - startTime; | ||
| log.info("Completed daily certificate expiry reminder job successfully in {} ms", | ||
| durationMs); | ||
|
|
||
| } catch (Exception ex) { | ||
| log.error("Daily certificate expiry reminder job failed", ex); | ||
| throw ex; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | ||
| taskRegistrar.setScheduler(taskScheduler()); | ||
| } | ||
|
|
||
|
|
||
| @Bean | ||
| public ThreadPoolTaskScheduler taskScheduler() { | ||
| ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); | ||
| scheduler.setPoolSize(5); | ||
| scheduler.setThreadNamePrefix("cert-expiry-scheduler-"); | ||
| scheduler.setWaitForTasksToCompleteOnShutdown(true); | ||
| scheduler.setAwaitTerminationSeconds(30); | ||
| return scheduler; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,9 @@ | |
| import java.text.DateFormat; | ||
| import java.text.ParseException; | ||
| import java.text.SimpleDateFormat; | ||
| import java.time.LocalDate; | ||
| import java.time.ZoneOffset; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.*; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
@@ -458,6 +461,52 @@ | |
| } | ||
|
|
||
|
|
||
| /** | ||
| * Retrieves all valid certificates that are expiring exactly in the given | ||
| * number of days. | ||
| * | ||
| * <p> | ||
| * This method is intended for reminder notifications (e.g. 7-day expiry | ||
| * reminder). | ||
| * A certificate is considered matching if its {@code notafter} timestamp falls | ||
| * within the full UTC day that is {@code daysToExpire} days from now. | ||
| * </p> | ||
| * | ||
| * @param daysToExpire number of days from today (e.g. 7 for a 7-day reminder) | ||
| * @return list of certificates expiring exactly in the specified number of days | ||
| */ | ||
| public List<CertificateRow> getValidCertificatesExpiringInDays(LocalDate processingDate, int daysToExpire) { | ||
|
|
||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneOffset.UTC); | ||
|
|
||
| LocalDate targetDate = processingDate.plusDays(daysToExpire); | ||
|
|
||
| // yyyyMMddHHmmss produces only digits → safe to parse as long | ||
| long startOfDay = Long.parseLong( | ||
| formatter.format(targetDate.atStartOfDay(ZoneOffset.UTC))); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Comment on lines
+485
to
+486
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yyyyMMddHHmmss produces only digits → safe to parse as long |
||
|
|
||
| // yyyyMMddHHmmss produces only digits → safe to parse as long | ||
| long endOfDay = Long.parseLong( | ||
| formatter.format(targetDate.plusDays(1) | ||
| .atStartOfDay(ZoneOffset.UTC))); | ||
|
Comment on lines
+489
to
+491
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yyyyMMddHHmmss produces only digits → safe to parse as long |
||
|
|
||
| Map<String, Object> params = Map.of( | ||
| "startOfDay", startOfDay, | ||
| "endOfDay", endOfDay); | ||
|
|
||
| String sql = """ | ||
| SELECT cert_key, 'data' as data, dn, cn, email, status, role, notafter | ||
| FROM certificate | ||
| WHERE status = 'VALID' | ||
| AND notafter >= :startOfDay | ||
| AND notafter < :endOfDay | ||
| ORDER BY notafter ASC | ||
| """; | ||
|
|
||
| return jdbcTemplate.query(sql, params, new CertificateRowMapper()); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Build up the query using the given where by parameters in the map | ||
| * and return the query and the named parameter map for subsequent parameter-binding/execution. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright (C) 2015 STFC | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.ac.ngs.dao; | ||
|
|
||
| import org.springframework.dao.EmptyResultDataAccessException; | ||
| import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
|
|
||
| @Repository | ||
| public class JobExecutionDao { | ||
| private NamedParameterJdbcTemplate jdbcTemplate; | ||
| private static final Log log = LogFactory.getLog(JobExecutionDao.class); | ||
|
|
||
| public JobExecutionDao(NamedParameterJdbcTemplate jdbcTemplate) { | ||
| this.jdbcTemplate = jdbcTemplate; | ||
| } | ||
|
|
||
| public LocalDate getLastRunDate(String jobName) { | ||
|
|
||
| String sql = """ | ||
| SELECT last_run_date | ||
| FROM job_execution_tracker | ||
| WHERE job_name = :jobName | ||
| """; | ||
|
|
||
| Map<String, Object> params = Map.of("jobName", jobName); | ||
|
|
||
| try { | ||
| LocalDate result = jdbcTemplate.queryForObject(sql, params, | ||
| (rs, rowNum) -> rs.getDate("last_run_date").toLocalDate()); | ||
|
|
||
| log.info("Last run date for job '" + jobName + "' is " + result); | ||
| return result; | ||
|
|
||
| } catch (EmptyResultDataAccessException ex) { | ||
| log.warn("No last run date found for job '" + jobName + "'. Assuming first run."); | ||
| return null; // first run case | ||
| } | ||
| } | ||
|
|
||
| public void updateLastRunDate(String jobName, LocalDate lastRunDate) { | ||
| // This query inserts a new job record if it doesn’t exist, | ||
| // or updates its last run date if it already exists, using PostgreSQL’s UPSERT mechanism. | ||
| String sql = """ | ||
| INSERT INTO job_execution_tracker (job_name, last_run_date) | ||
| VALUES (:jobName, :lastRunDate) | ||
| ON CONFLICT (job_name) | ||
| DO UPDATE SET last_run_date = EXCLUDED.last_run_date | ||
| """; | ||
|
|
||
| Map<String, Object> params = Map.of( | ||
| "jobName", jobName, | ||
| "lastRunDate", java.sql.Date.valueOf(lastRunDate)); | ||
|
|
||
| int rows = jdbcTemplate.update(sql, params); | ||
|
|
||
| log.info("Updated last run date for job '" + jobName + "' to " + lastRunDate + " (rows affected: " + rows + ")"); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a summary of number of SMTP mail pushed for 30 day and 7 day reminder as well so that we can spot the anomaly by comparing this number and expected number from DB. Thanks
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Summary for 7-day expiry reminder (processingDate=2026-05-19): total=1, sent=0, failed=1