Skip to content

Commit

Permalink
Improve JWKs health indicator logging (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber authored Mar 1, 2024
1 parent 9100a95 commit 081c69a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ public JwkSourceMap getJwkSourceMap(Map<String, JwtTenantProperties> tenants, Jw

DefaultJwksHealthIndicator healthIndicator = null;
if (listJwksHealthIndicator != null) {
healthIndicator = new DefaultJwksHealthIndicator();
healthIndicator = new DefaultJwksHealthIndicator(entry.getKey());
builder.healthReporting(healthIndicator);

LOGGER.info("Add health indicator");
LOGGER.info("Add health indicator for " + entry.getKey());

listJwksHealthIndicator.addHealthIndicators(healthIndicator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ListJwksHealthIndicator jwksHealthIndicator(SecurityProperties properties

long maxDelay = (jwk.getConnectTimeout() + jwk.getReadTimeout()) * 1000;

return new ListJwksHealthIndicator(maxDelay, Executors.newCachedThreadPool());
return new ListJwksHealthIndicator(maxDelay, Executors.newCachedThreadPool(), "List");
}

@Bean(destroyMethod = "close")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;

import java.io.Closeable;
import java.io.IOException;

/**
*
* Health indicator.
Expand All @@ -20,6 +17,14 @@ public abstract class AbstractJwksHealthIndicator extends AbstractHealthIndicato

private JwksHealth previousHealth;

protected final String name;

// do we want to log status?
protected boolean silent;

protected AbstractJwksHealthIndicator(String name) {
this.name = name;
}

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
Expand All @@ -43,17 +48,19 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {

protected void logInitialOrChangedState(JwksHealth health) {
JwksHealth previousHealth = this.previousHealth; // defensive copy
if(previousHealth != null) {
if(!previousHealth.isSuccess() && health.isSuccess()) {
logger.info("JWKs health transitioned to UP");
} else if(previousHealth.isSuccess() && !health.isSuccess()) {
logger.warn("JWKs health transitioned to DOWN");
}
} else {
if(!health.isSuccess()) {
logger.warn("JWKs health initialized to DOWN");
if(!silent) {
if (previousHealth != null) {
if (!previousHealth.isSuccess() && health.isSuccess()) {
logger.info("{} JWKs health transitioned to UP", name);
} else if (previousHealth.isSuccess() && !health.isSuccess()) {
logger.warn("{} JWKs health transitioned to DOWN", name);
}
} else {
logger.info("JWKs health initialized to UP");
if (!health.isSuccess()) {
logger.warn("{} JWKs health initialized to DOWN", name);
} else {
logger.info("{} JWKs health initialized to UP", name);
}
}
}
this.previousHealth = health;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class DefaultJwksHealthIndicator extends AbstractJwksHealthIndicator impl

private HealthReport healthReport;

public DefaultJwksHealthIndicator(String name) {
super(name);
}

public void setJwkSetSource(JWKSetSource jwkSetSource) {
this.jwkSetSource = jwkSetSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

public class ListJwksHealthIndicator extends AbstractJwksHealthIndicator implements Closeable {

Expand All @@ -23,13 +21,16 @@ public class ListJwksHealthIndicator extends AbstractJwksHealthIndicator impleme
private final ExecutorService executorService;
private List<DefaultJwksHealthIndicator> healthIndicators = new ArrayList<>();

public ListJwksHealthIndicator(long maxDelay, ExecutorService executorService) {
public ListJwksHealthIndicator(long maxDelay, ExecutorService executorService, String name) {
super(name);
this.maxDelay = maxDelay;
this.executorService = executorService;
}

public void addHealthIndicators(DefaultJwksHealthIndicator healthIndicator) {
this.healthIndicators.add(healthIndicator);

this.silent = healthIndicators.size() <= 1;
}

@Override
Expand Down

0 comments on commit 081c69a

Please sign in to comment.