Repository navigation
feat: add announcement and scheduled maintenance banner system (#3733) #4004
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package io.terrakube.api.plugin.announcement; | ||
|
|
||
| import io.terrakube.api.plugin.announcement.PublicAnnouncementController.PublicAnnouncementDto; | ||
| import io.terrakube.api.repository.AnnouncementRepository; | ||
| import io.terrakube.api.rs.announcement.Announcement; | ||
| import io.terrakube.api.rs.announcement.AnnouncementSeverity; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.MessageDigest; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.Date; | ||
| import java.util.HexFormat; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * Per-pod in-memory cache for active login-screen global announcements so steady-state | ||
| * unauthenticated traffic to {@code /public/v1/announcements} avoids hitting the database | ||
| * and does not expose internal announcement UUIDs. | ||
| */ | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PublicAnnouncementCacheService { | ||
|
|
||
| static final Duration CACHE_TTL = Duration.ofSeconds(30); | ||
|
|
||
| private final AnnouncementRepository announcementRepository; | ||
|
|
||
| private volatile CachedSnapshot cachedSnapshot; | ||
|
Check warning on line 33 in api/src/main/java/io/terrakube/api/plugin/announcement/PublicAnnouncementCacheService.java
|
||
|
|
||
| public List<PublicAnnouncementDto> getActiveLoginAnnouncements(Instant now) { | ||
| CachedSnapshot snapshot = getOrRefreshSnapshot(); | ||
| Date nowDate = Date.from(now); | ||
| return snapshot.entries().stream() | ||
| .filter(entry -> isWithinScheduleWindow(entry, nowDate)) | ||
| .map(entry -> new PublicAnnouncementDto( | ||
| entry.key(), | ||
| entry.title(), | ||
| entry.message(), | ||
| entry.severity(), | ||
| entry.startsAt(), | ||
| entry.endsAt(), | ||
| entry.updatedDate())) | ||
| .toList(); | ||
| } | ||
|
|
||
| public void evict() { | ||
| cachedSnapshot = null; | ||
| } | ||
|
|
||
| private CachedSnapshot getOrRefreshSnapshot() { | ||
| CachedSnapshot current = cachedSnapshot; | ||
| Instant now = Instant.now(); | ||
| if (current != null && now.isBefore(current.loadedAt().plus(CACHE_TTL))) { | ||
| return current; | ||
| } | ||
| synchronized (this) { | ||
| current = cachedSnapshot; | ||
| now = Instant.now(); | ||
| if (current != null && now.isBefore(current.loadedAt().plus(CACHE_TTL))) { | ||
| return current; | ||
| } | ||
| List<CachedEntry> entries = announcementRepository.findActiveLoginGlobalAnnouncements().stream() | ||
|
Contributor
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. This anonymous endpoint still hits the DB ( |
||
| .map(CachedEntry::from) | ||
| .toList(); | ||
| CachedSnapshot refreshed = new CachedSnapshot(now, entries); | ||
| cachedSnapshot = refreshed; | ||
| return refreshed; | ||
| } | ||
| } | ||
|
|
||
| private static boolean isWithinScheduleWindow(CachedEntry entry, Date now) { | ||
| if (entry.startsAt() != null && entry.startsAt().after(now)) { | ||
| return false; | ||
| } | ||
| return entry.endsAt() == null || entry.endsAt().after(now); | ||
| } | ||
|
|
||
| private static String computeOpaqueKey(UUID id, Date updatedDate) { | ||
|
Check warning on line 83 in api/src/main/java/io/terrakube/api/plugin/announcement/PublicAnnouncementCacheService.java
|
||
| String raw = id + ":" + (updatedDate != null ? updatedDate.getTime() : 0L); | ||
| try { | ||
| MessageDigest digest = MessageDigest.getInstance("SHA-256"); | ||
| byte[] hash = digest.digest(raw.getBytes(StandardCharsets.UTF_8)); | ||
| return HexFormat.of().formatHex(hash); | ||
| } catch (NoSuchAlgorithmException ex) { | ||
| throw new IllegalStateException("SHA-256 unavailable", ex); | ||
| } | ||
| } | ||
|
|
||
| private record CachedSnapshot(Instant loadedAt, List<CachedEntry> entries) {} | ||
|
|
||
| private record CachedEntry( | ||
| String key, | ||
| String title, | ||
| String message, | ||
| AnnouncementSeverity severity, | ||
| Date startsAt, | ||
| Date endsAt, | ||
| Date updatedDate) { | ||
|
|
||
| static CachedEntry from(Announcement announcement) { | ||
| return new CachedEntry( | ||
| computeOpaqueKey(announcement.getId(), announcement.getUpdatedDate()), | ||
| announcement.getTitle(), | ||
| announcement.getMessage(), | ||
| announcement.getSeverity(), | ||
| announcement.getStartsAt(), | ||
| announcement.getEndsAt(), | ||
| announcement.getUpdatedDate()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package io.terrakube.api.plugin.announcement; | ||
|
|
||
| import io.terrakube.api.rs.announcement.AnnouncementSeverity; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/public/v1/announcements") | ||
| @RequiredArgsConstructor | ||
| public class PublicAnnouncementController { | ||
|
|
||
| private final PublicAnnouncementCacheService publicAnnouncementCacheService; | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<PublicAnnouncementsResponse> getActiveLoginAnnouncements() { | ||
| Instant now = Instant.now(); | ||
| List<PublicAnnouncementDto> announcements = publicAnnouncementCacheService.getActiveLoginAnnouncements(now); | ||
|
|
||
| return ResponseEntity.ok() | ||
| .header("Cache-Control", "public, max-age=30") | ||
|
Contributor
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.
|
||
| .header(ServerTimeResponseFilter.SERVER_TIME_HEADER, now.toString()) | ||
| .body(new PublicAnnouncementsResponse(now, announcements)); | ||
| } | ||
|
|
||
| public record PublicAnnouncementsResponse(Instant serverTime, List<PublicAnnouncementDto> announcements) {} | ||
|
|
||
| public record PublicAnnouncementDto( | ||
| String key, | ||
| String title, | ||
| String message, | ||
| AnnouncementSeverity severity, | ||
| Date startsAt, | ||
| Date endsAt, | ||
| Date updatedDate) {} | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package io.terrakube.api.plugin.announcement; | ||
|
|
||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.filter.OncePerRequestFilter; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.Instant; | ||
|
|
||
| @Component | ||
| public class ServerTimeResponseFilter extends OncePerRequestFilter { | ||
|
|
||
| public static final String SERVER_TIME_HEADER = "X-Server-Time"; | ||
|
|
||
| @Override | ||
| protected void doFilterInternal( | ||
| HttpServletRequest request, | ||
| HttpServletResponse response, | ||
| FilterChain filterChain) throws ServletException, IOException { | ||
| String uri = request.getRequestURI(); | ||
| if (uri != null | ||
| && (uri.contains("/api/v1/announcement") | ||
| || uri.contains("/graphql/api/v1") | ||
| || uri.contains("/public/v1/announcements"))) { | ||
| response.setHeader(SERVER_TIME_HEADER, Instant.now().toString()); | ||
| } | ||
| filterChain.doFilter(request, response); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,6 +125,16 @@ public boolean canManagePolicies(Team team) { | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Organization-scoped announcement management is intentionally restricted to teams with the | ||
| * {@code admin} role. Unlike workspace/module/policy permissions, {@link Team} does not define a | ||
| * dedicated announcement flag column, so {@code custom} (and legacy null) roles are excluded. | ||
| */ | ||
| @Override | ||
| public boolean canManageAnnouncements(Team team) { | ||
| return ROLE_ADMIN.equals(normalizeRole(team.getRole())); | ||
|
Contributor
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. Every other |
||
| } | ||
|
|
||
| @Override | ||
| public boolean canPlanJob(Team team) { | ||
| String role = normalizeRole(team.getRole()); | ||
|
|
||
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.
Minor race:
evict()writescachedSnapshot = nullwithout the monitor. A refresh already insidesynchronized (this)may have read the DB before the commit and assign its (stale) snapshot after the evict, so the login page keeps serving the old/deactivated announcement until the TTL expires.Suggestion: take the same lock in
evict()and add a generation counter so a refresh started before an evict doesn't publish its result.