[java-identity-provider] branch main updated: IDP-2378 Accessing metrics/updates always triggers four HTTPS requests
Rod Widdowson
rdw at steadingsoftware.com
Sat Nov 8 16:15:16 UTC 2025
This is an automated email from the git hooks/post-receive script.
rdw pushed a commit to branch main
in repository java-identity-provider.
View the commit online:
https://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=2d330cff072c65669a3cb8f9bca40f9a4d78859d
The following commit(s) were added to refs/heads/main by this push:
new 2d330cff0 IDP-2378 Accessing metrics/updates always triggers four HTTPS requests
2d330cff0 is described below
commit 2d330cff072c65669a3cb8f9bca40f9a4d78859d
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sat Nov 8 16:13:12 2025 +0000
IDP-2378 Accessing metrics/updates always triggers four HTTPS requests
https://shibboleth.atlassian.net/browse/IDP-2378
Implement caching bean (and test)
---
.../impl/InstallableComponentPropertyCache.java | 104 +++++++++++++++++++++
.../InstallableComponentPropertyCacheTest.java | 99 ++++++++++++++++++++
idp-admin-impl/src/test/resources/logback-test.xml | 1 +
3 files changed, 204 insertions(+)
diff --git a/idp-admin-impl/src/main/java/net/shibboleth/idp/admin/impl/InstallableComponentPropertyCache.java b/idp-admin-impl/src/main/java/net/shibboleth/idp/admin/impl/InstallableComponentPropertyCache.java
new file mode 100644
index 000000000..158cefed2
--- /dev/null
+++ b/idp-admin-impl/src/main/java/net/shibboleth/idp/admin/impl/InstallableComponentPropertyCache.java
@@ -0,0 +1,104 @@
+/*
+ * 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 net.shibboleth.idp.admin.impl;
+
+import java.net.URL;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.apache.hc.client5.http.classic.HttpClient;
+import org.opensaml.security.httpclient.HttpClientSecurityParameters;
+import org.slf4j.Logger;
+
+import net.shibboleth.profile.installablecomponent.InstallableComponentSupport;
+import net.shibboleth.shared.annotation.constraint.NonnullAfterInit;
+import net.shibboleth.shared.collection.Pair;
+import net.shibboleth.shared.component.AbstractIdentifiableInitializableComponent;
+import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.logic.Constraint;
+import net.shibboleth.shared.primitive.LoggerFactory;
+
+/**
+ * A caching shim on top of {@link InstallableComponentSupport#loadInfo(List, HttpClient, HttpClientSecurityParameters)}.
+ */
+public class InstallableComponentPropertyCache extends AbstractIdentifiableInitializableComponent {
+
+ /** Logger. */
+ @Nonnull private final Logger log = LoggerFactory.getLogger(InstallableComponentPropertyCache.class);
+
+ /** The cache. */
+ @Nonnull private final Map<URL, Pair<Instant, Properties> > cache = new HashMap<>();
+
+ /**
+ * How long before a cached entry expires.
+ */
+ @NonnullAfterInit private Duration cacheLife;
+
+ /** Set {@link #cacheLife}.
+ * @param howLong what to set
+ */
+ public void setCacheLife(@Nullable Duration howLong) {
+ cacheLife = Constraint.isNotNull(howLong, "Duration cache life should be non null");
+ Constraint.isTrue(!howLong.isNegative(), "Duration cache life should be positive");
+ log.debug("Setting a delay of {} minutes", cacheLife.toMinutes());
+ }
+
+ @Override
+ protected void doInitialize() throws ComponentInitializationException {
+ Constraint.isNotNull(cacheLife, "Duration cache life was not set");
+ }
+
+ /** Provide a shim on top of {@link InstallableComponentSupport#loadInfo(List, HttpClient, HttpClientSecurityParameters)}.
+ * We use the first URL in the list as the cache key.
+ * @param updateURLs where to look
+ * @param client the http client to use
+ * @param securityParameters the HttpClientSecurityParameters, if any
+ * @return the property files for the component.
+ */
+ @Nullable public Properties loadInfo(@Nonnull final List<URL> updateURLs,
+ @Nonnull final HttpClient client,
+ @Nullable final HttpClientSecurityParameters securityParameters) {
+
+ log.trace("Looking for {}", updateURLs);
+ if (updateURLs.isEmpty() || cacheLife.isZero()) {
+ log.trace("No Key or zero cachelife");
+ return InstallableComponentSupport.loadInfo(updateURLs, client, securityParameters);
+ }
+
+ final Instant now = Instant.now();
+ final Pair<Instant, Properties> cachedValue = cache.get(updateURLs.get(0));
+
+ if (cachedValue != null) {
+ log.trace("Cache hit, expired {} (now {})", cachedValue.getFirst(), now);
+ // if it expires after now
+ if (cachedValue.getFirst().isAfter(now)) {
+ return cachedValue.getSecond();
+ }
+ }
+
+ final Properties result = InstallableComponentSupport.loadInfo(updateURLs, client, securityParameters);
+ final Instant expires = now.plus(cacheLife);
+ log.trace("Caching , expires {}", expires);
+ cache.put(updateURLs.get(0), new Pair<>(expires, result));
+ return result;
+ }
+}
diff --git a/idp-admin-impl/src/test/java/net/shibboleth/idp/admin/impl/InstallableComponentPropertyCacheTest.java b/idp-admin-impl/src/test/java/net/shibboleth/idp/admin/impl/InstallableComponentPropertyCacheTest.java
new file mode 100644
index 000000000..8e813a7cd
--- /dev/null
+++ b/idp-admin-impl/src/test/java/net/shibboleth/idp/admin/impl/InstallableComponentPropertyCacheTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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 net.shibboleth.idp.admin.impl;
+
+import static org.testng.Assert.assertEquals;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.time.Duration;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.hc.client5.http.classic.HttpClient;
+import org.opensaml.security.httpclient.HttpClientSecurityParameters;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import net.shibboleth.shared.collection.CollectionSupport;
+import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.httpclient.HttpClientBuilder;
+
+ at SuppressWarnings("javadoc")
+public class InstallableComponentPropertyCacheTest {
+
+ private Path outputFile;
+ private final String KEY = "key";
+ private List<URL> urls;
+ private HttpClientSecurityParameters dummyParams;
+ private HttpClient dummyClient;
+
+ @BeforeClass public void setupURL() throws Exception {
+ outputFile = Files.createTempFile("InstallableComponentPropertyCacheTest", "tmp");
+ urls = CollectionSupport.singletonList(new URL(outputFile.toUri().toASCIIString()));
+ dummyClient = new HttpClientBuilder().buildClient();
+ dummyParams = new HttpClientSecurityParameters();
+ }
+
+ @AfterClass public void cleanupFile() throws IOException {
+ Files.delete(outputFile);
+ }
+
+ private void writeFile(final String value) throws IOException {
+ final Properties p = new Properties();
+ p.put(KEY, value);
+ FileOutputStream f =new FileOutputStream(outputFile.toFile());
+ p.store(f, "Comment");
+ f.close();
+ }
+
+ @Test public void testNone() throws IOException, ComponentInitializationException {
+
+ writeFile("none");
+ final InstallableComponentPropertyCache cache = new InstallableComponentPropertyCache();
+ cache.setCacheLife(Duration.ZERO);
+ cache.initialize();
+ assertEquals(cache.loadInfo(urls, dummyClient, dummyParams).get(KEY), "none");
+ }
+
+ @Test public void testFiveSeconds() throws IOException, ComponentInitializationException, InterruptedException {
+ writeFile("pre5secs");
+ final InstallableComponentPropertyCache cache = new InstallableComponentPropertyCache();
+ cache.setCacheLife(Duration.ofSeconds(5));
+ cache.initialize();
+ assertEquals(cache.loadInfo(urls, dummyClient, dummyParams).get(KEY), "pre5secs");
+ writeFile("post5secs");
+ assertEquals(cache.loadInfo(urls, dummyClient, dummyParams).get(KEY), "pre5secs");
+ Thread.sleep(1000*6);
+ assertEquals(cache.loadInfo(urls, dummyClient, dummyParams).get(KEY), "post5secs");
+ }
+
+ @Test public void testOneHour() throws IOException, ComponentInitializationException, InterruptedException {
+ writeFile("prehour");
+ final InstallableComponentPropertyCache cache = new InstallableComponentPropertyCache();
+ cache.setCacheLife(Duration.ofMinutes(60));
+ cache.initialize();
+ assertEquals(cache.loadInfo(urls, dummyClient, dummyParams).get(KEY), "prehour");
+ writeFile("posthour");
+ assertEquals(cache.loadInfo(urls, dummyClient, dummyParams).get(KEY), "prehour");
+ Thread.sleep(1000*6);
+ assertEquals(cache.loadInfo(urls, dummyClient, dummyParams).get(KEY), "prehour");
+ }
+}
+
diff --git a/idp-admin-impl/src/test/resources/logback-test.xml b/idp-admin-impl/src/test/resources/logback-test.xml
index e0ddb5a5e..5d2e211ba 100644
--- a/idp-admin-impl/src/test/resources/logback-test.xml
+++ b/idp-admin-impl/src/test/resources/logback-test.xml
@@ -3,6 +3,7 @@
<configuration>
<logger name="net.shibboleth.idp.plugin.impl" level="TRACE"/>
+ <logger name="net.shibboleth.idp.admin.impl" level="TRACE"/>>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list