Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 47 additions & 47 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ name: "CodeQL Advanced"

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]
schedule:
- cron: '42 18 * * 2'
- cron: "42 18 * * 2"

jobs:
analyze:
Expand All @@ -43,12 +43,12 @@ jobs:
fail-fast: false
matrix:
include:
- language: actions
build-mode: none
- language: java-kotlin
build-mode: none # This mode only analyzes Java. Set this to 'autobuild' or 'manual' to analyze Kotlin too.
- language: javascript-typescript
build-mode: none
- language: actions
build-mode: none
- language: java-kotlin
build-mode: none # This mode only analyzes Java. Set this to 'autobuild' or 'manual' to analyze Kotlin too.
- language: javascript-typescript
build-mode: none
# CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift'
# Use `c-cpp` to analyze code written in C, C++ or both
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
Expand All @@ -58,46 +58,46 @@ jobs:
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Checkout repository
uses: actions/checkout@v4

# Add any setup steps before running the `github/codeql-action/init` action.
# This includes steps like installing compilers or runtimes (`actions/setup-node`
# or others). This is typically only required for manual builds.
# - name: Setup runtime (example)
# uses: actions/setup-example@v1
# Add any setup steps before running the `github/codeql-action/init` action.
# This includes steps like installing compilers or runtimes (`actions/setup-node`
# or others). This is typically only required for manual builds.
# - name: Setup runtime (example)
# uses: actions/setup-example@v1

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
queries: security-extended
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
queries: security-extended

# If the analyze step fails for one of the languages you are analyzing with
# "We were unable to automatically build your code", modify the matrix above
# to set the build mode to "manual" for that language. Then modify this step
# to build your code.
# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
- name: Run manual build steps
if: matrix.build-mode == 'manual'
shell: bash
run: |
echo 'If you are using a "manual" build mode for one or more of the' \
'languages you are analyzing, replace this with the commands to build' \
'your code, for example:'
echo ' make bootstrap'
echo ' make release'
exit 1
# If the analyze step fails for one of the languages you are analyzing with
# "We were unable to automatically build your code", modify the matrix above
# to set the build mode to "manual" for that language. Then modify this step
# to build your code.
# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
- name: Run manual build steps
if: matrix.build-mode == 'manual'
shell: bash
run: |
echo 'If you are using a "manual" build mode for one or more of the' \
'languages you are analyzing, replace this with the commands to build' \
'your code, for example:'
echo ' make bootstrap'
echo ' make release'
exit 1

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v4
with:
category: "/language:${{matrix.language}}"
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v4
with:
category: "/language:${{matrix.language}}"
23 changes: 22 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ FORMATTER_VERSION = 1.17.0
FORMATTER_JAR = google-java-format-$(FORMATTER_VERSION)-all-deps.jar
FORMATTER_URL = https://github.com/google/google-java-format/releases/download/v$(FORMATTER_VERSION)/$(FORMATTER_JAR)
JAVA_FILES = $(shell find src -name "*.java")
YML_FILES = $(shell find . -name "*.yml" -not -path "./target/*" -not -path "./node_modules/*")

# Default target
.PHONY: help
help:
@echo "Available targets:"
@echo " fmt - Format all Java and YAML files"
@echo " format - Format all Java files using Google Java Format"
@echo " check - Check if files need formatting without changing them"
@echo " fmt-yml - Format all YAML files using Prettier"
@echo " fmt-ips - Format IP whitelist in ingress YAML"
@echo " clean - Remove the formatter jar"
@echo ""
@echo "Note: Keep this Makefile locally, don't commit it to your repository"
Expand All @@ -22,6 +25,24 @@ $(FORMATTER_JAR):
@echo "Downloading Google Java Format..."
@curl -L $(FORMATTER_URL) -o $(FORMATTER_JAR)

# Format all Java and YAML files
.PHONY: fmt
fmt: format fmt-yml fmt-ips

# Format YAML files using Prettier
.PHONY: fmt-yml
fmt-yml:
@echo "Formatting YAML files..."
@npx --yes prettier --ignore-path /dev/null --write $(YML_FILES)
@echo "YAML formatting complete!"

# Format IP whitelist in ingress YAML
.PHONY: fmt-ips
fmt-ips:
@echo "Formatting IP whitelist..."
@python3 scripts/fmt-ips.py
@echo "IP formatting complete!"

# Format all Java files
.PHONY: format
format: $(FORMATTER_JAR)
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3'
version: "3"
services:
mongodb:
image: mongo:latest
Expand Down
Binary file modified kubernetes/feedback-viewer-ingress.yml.gpg
Binary file not shown.
44 changes: 44 additions & 0 deletions scripts/fmt-ips.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
import re
import sys

LINE_WIDTH = 120
INDENT = " "
FILE = "kubernetes/feedback-viewer-ingress.yml"

with open(FILE) as f:
content = f.read()

pattern = r'(nginx\.ingress\.kubernetes\.io/whitelist-source-range:\s*")(.*?)(")'
match = re.search(pattern, content, re.DOTALL)
if not match:
print("Could not find whitelist-source-range annotation")
sys.exit(1)

ips = [ip.strip() for ip in match.group(2).split(",") if ip.strip()]

lines = []
current = []
current_len = 0

for ip in ips:
seg = (", " if current else "") + ip
if current and current_len + len(seg) > LINE_WIDTH:
lines.append(INDENT + ", ".join(current))
current = [ip]
current_len = len(ip)
else:
current.append(ip)
current_len += len(seg)

if current:
lines.append(INDENT + ", ".join(current))

formatted = "\n" + ",\n".join(lines) + '"'

new_content = content[: match.start(2)] + formatted + content[match.end(3) :]

with open(FILE, "w") as f:
f.write(new_content)

print(f"Formatted {len(ips)} IPs across {len(lines)} lines")
15 changes: 5 additions & 10 deletions src/main/java/ca/gc/tbs/config/CacheConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@ public class CacheConfig {

@Bean
public CacheManager cacheManager() {
CaffeineCacheManager manager = new CaffeineCacheManager(
"problemDates",
"distinctUrls",
"processedProblems",
"dashboardStats",
"gcIpCache"
);
manager.setCaffeine(Caffeine.newBuilder()
.expireAfterWrite(24, TimeUnit.HOURS)
.maximumSize(1000));
CaffeineCacheManager manager =
new CaffeineCacheManager(
"problemDates", "distinctUrls", "processedProblems", "dashboardStats", "gcIpCache");
manager.setCaffeine(
Caffeine.newBuilder().expireAfterWrite(24, TimeUnit.HOURS).maximumSize(1000));
return manager;
}
}
98 changes: 50 additions & 48 deletions src/main/java/ca/gc/tbs/config/CachePreloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import ca.gc.tbs.service.DashboardService;
import ca.gc.tbs.service.ProblemCacheService;
import ca.gc.tbs.service.ProblemDateService;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
Expand All @@ -11,64 +14,63 @@
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;

@Component
public class CachePreloader implements ApplicationRunner {

private static final Logger LOGGER = LoggerFactory.getLogger(CachePreloader.class);
private static final Logger LOGGER = LoggerFactory.getLogger(CachePreloader.class);

private final ProblemCacheService problemCacheService;
private final ProblemDateService problemDateService;
private final DashboardService dashboardService;
private final CacheManager cacheManager;
private final ProblemCacheService problemCacheService;
private final ProblemDateService problemDateService;
private final DashboardService dashboardService;
private final CacheManager cacheManager;

public CachePreloader(ProblemCacheService problemCacheService,
ProblemDateService problemDateService,
DashboardService dashboardService,
CacheManager cacheManager) {
this.problemCacheService = problemCacheService;
this.problemDateService = problemDateService;
this.dashboardService = dashboardService;
this.cacheManager = cacheManager;
}
public CachePreloader(
ProblemCacheService problemCacheService,
ProblemDateService problemDateService,
DashboardService dashboardService,
CacheManager cacheManager) {
this.problemCacheService = problemCacheService;
this.problemDateService = problemDateService;
this.dashboardService = dashboardService;
this.cacheManager = cacheManager;
}

@Override
public void run(ApplicationArguments args) {
LOGGER.info("Preloading caches before web server starts...");
long start = System.currentTimeMillis();

@Override
public void run(ApplicationArguments args) {
LOGGER.info("Preloading caches before web server starts...");
long start = System.currentTimeMillis();
// 1. Parallel load of base data (lowest tier)
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
CompletableFuture<Void> problems =
CompletableFuture.runAsync(problemCacheService::getProcessedProblems, executor);
CompletableFuture<Void> urls =
CompletableFuture.runAsync(
problemCacheService::getDistinctProcessedUrlsForCache, executor);
CompletableFuture<Void> dates =
CompletableFuture.runAsync(problemDateService::getProblemDates, executor);

// 1. Parallel load of base data (lowest tier)
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
CompletableFuture<Void> problems =
CompletableFuture.runAsync(problemCacheService::getProcessedProblems, executor);
CompletableFuture<Void> urls =
CompletableFuture.runAsync(problemCacheService::getDistinctProcessedUrlsForCache, executor);
CompletableFuture<Void> dates =
CompletableFuture.runAsync(problemDateService::getProblemDates, executor);
CompletableFuture.allOf(problems, urls, dates).join();
}

LOGGER.info("DB caches loaded in {}ms.", System.currentTimeMillis() - start);

CompletableFuture.allOf(problems, urls, dates).join();
}
// 2. Sequential load of derived data (Dashboard tier)
// This MUST happen after base data is fully joined to avoid triggering redundant re-calculatons
// and ensure the dashboard tier is also warm.
dashboardService.getDashboardStats();

LOGGER.info("DB caches loaded in {}ms.", System.currentTimeMillis() - start);
LOGGER.info("All caches warm. Total time: {}ms", System.currentTimeMillis() - start);

// 2. Sequential load of derived data (Dashboard tier)
// This MUST happen after base data is fully joined to avoid triggering redundant re-calculatons
// and ensure the dashboard tier is also warm.
dashboardService.getDashboardStats();

LOGGER.info("All caches warm. Total time: {}ms", System.currentTimeMillis() - start);

// Verify caches are populated
Stream.of("processedProblems", "distinctUrls", "dashboardStats", "problemDates").forEach(name -> {
Cache cache = cacheManager.getCache(name);
if (cache != null) {
// Verify caches are populated
Stream.of("processedProblems", "distinctUrls", "dashboardStats", "problemDates")
.forEach(
name -> {
Cache cache = cacheManager.getCache(name);
if (cache != null) {
Object val = cache.get("all");
LOGGER.info("Cache '{}' verification: {}", name, val != null ? "HIT ✓" : "MISS ✗");
}
});
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/
package ca.gc.tbs.config;

import java.io.IOException;
import java.net.URI;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/ca/gc/tbs/config/PasswordEncoderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
@Configuration
public class PasswordEncoderConfig {

@Bean
public PasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public PasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
Loading
Loading