← All articles

Engineering note · Flexi-Ride

Locking down Spring Boot Actuator

Project
Flexi-Ride — Internal project, Zone01 Kisumu
Issue
#79 OBS-001: Add cross-cutting observability
My role
Author of PR #208
Stack
Java · Spring Boot · Spring Security · gRPC · Micrometer
Status
Merged 5 Oct 2026

The problem

Flexi-Ride has 13 backend services and 2 BFF gateways. The issue asked for the same observability in all of them: JSON logs, standard Actuator endpoints, Prometheus metrics, and gRPC request count and latency.

Adding the endpoints was the easy part. Exposing env, metrics and loggers also exposes things an anonymous caller should not see or change. The first review of my PR found exactly that. An anonymous POST /actuator/loggers/... could turn a logger off. My new booking security chain had no authentication mechanism at all, so it rejected valid credentials. A second review found that in some services the protected endpoints still fell through to permitAll(), while in others they were locked with no way to log in.

What I did

The root cause was that each service's application SecurityConfig also tried to handle /actuator/**, and each one did it differently. So I took Actuator out of those chains and gave every app a separate ManagementSecurityConfig.

return http.securityMatcher("/actuator/**")
    .authorizeHttpRequests(auth ->
        auth.requestMatchers("/actuator/health", "/actuator/health/**",
                "/actuator/info", "/actuator/prometheus").permitAll()
            .anyRequest().hasRole(ROLE))
    .httpBasic(Customizer.withDefaults())
    .build();
  • The chain has @Order(Ordered.HIGHEST_PRECEDENCE), so it runs first and owns every Actuator request.
  • Health, info and Prometheus stay public for probes and scraping. Everything else needs HTTP Basic and the ACTUATOR role, so a normal user JWT can't reach it.
  • Credentials come from FLEXIRIDE_MANAGEMENT_USERNAME and FLEXIRIDE_MANAGEMENT_PASSWORD. If no password is set, the chain logs a warning and the protected endpoints stay locked.
  • The chain uses its own password encoder, because auth registers a BCrypt bean that would otherwise apply to the management user.
  • loggers is read-only, and health details are only shown to authorised callers.
  • I restored HTTP Basic on booking and leads.

For gRPC I added a GrpcMetricsInterceptor to every server. It counts grpc.requests.total and times grpc.request.duration with a status tag. Review pointed out that cancelled calls, deadline expiry and lost connections end in onCancel() without close(), so they had no latency recorded. I now record in both places, behind an AtomicBoolean so a call is counted exactly once. I also added a gRPC health indicator and a service.health.status gauge.

How I checked it

Every module has a ManagementSecurityConfigTest. It checks anonymous reads of the public endpoints, 401 with missing or wrong credentials, 200 with the right ones, the locked state with no password, and that anonymous log-level changes are rejected. These tests pass in all 16 modules. Two interceptor tests cover a cancel without close, and a close followed by a cancel. The PR also lists curl checks for each case, so a reviewer can repeat them against a running service. The final review approved the PR after its own live checks on both gateways and sacco.

What I learned

Security rules for one path family are much easier to reason about in one place. While Actuator rules lived inside 15 different application chains, I fixed one gap and found another. A single chain with its own securityMatcher, plus the same test in every module, made the behaviour consistent. I also learned to test the real security chain, not standalone MockMvc. The booking regression slipped through because the existing controller tests never ran the actual filter chain.