From fd5088cc4ed73b5d7e6ef1462c4a8ab52f2e1683 Mon Sep 17 00:00:00 2001 From: Ivan Fernandez Calvo Date: Mon, 23 Dec 2024 10:55:45 +0100 Subject: [PATCH] fix: JENKINS-53245 use closeable (#470) --- .../org/jenkinsci/plugins/saml/SamlProfileWrapper.java | 6 +++--- .../jenkinsci/plugins/saml/SamlRedirectActionWrapper.java | 7 ++++--- .../org/jenkinsci/plugins/saml/SamlSPMetadataWrapper.java | 7 ++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/saml/SamlProfileWrapper.java b/src/main/java/org/jenkinsci/plugins/saml/SamlProfileWrapper.java index 47f1864a..fad553eb 100644 --- a/src/main/java/org/jenkinsci/plugins/saml/SamlProfileWrapper.java +++ b/src/main/java/org/jenkinsci/plugins/saml/SamlProfileWrapper.java @@ -17,6 +17,7 @@ package org.jenkinsci.plugins.saml; +import java.io.IOException; import java.util.logging.Logger; import org.kohsuke.stapler.StaplerRequest2; import org.kohsuke.stapler.StaplerResponse2; @@ -52,8 +53,7 @@ public SamlProfileWrapper(SamlPluginConfig samlPluginConfig, StaplerRequest2 req protected SAML2Profile process() { SAML2AuthenticationCredentials credentials; SAML2Profile saml2Profile; - try { - SAML2Client client = createSAML2Client(); + try (SAML2Client client = createSAML2Client()) { WebContext context = createWebContext(); SessionStore sessionStore = createSessionStore(); CallContext ctx = new CallContext(context, sessionStore); @@ -61,7 +61,7 @@ protected SAML2Profile process() { credentials = (SAML2AuthenticationCredentials) client.validateCredentials(ctx, unvalidated).orElse(null); saml2Profile = (SAML2Profile) client.getUserProfile(ctx, credentials).orElse(null); client.destroy(); - } catch (HttpAction|SAMLException e) { + } catch (HttpAction|SAMLException|IOException e) { //if the SAMLResponse is not valid we send the user again to the IdP throw new BadCredentialsException(e.getMessage(), e); } diff --git a/src/main/java/org/jenkinsci/plugins/saml/SamlRedirectActionWrapper.java b/src/main/java/org/jenkinsci/plugins/saml/SamlRedirectActionWrapper.java index 92d2367e..561e4056 100644 --- a/src/main/java/org/jenkinsci/plugins/saml/SamlRedirectActionWrapper.java +++ b/src/main/java/org/jenkinsci/plugins/saml/SamlRedirectActionWrapper.java @@ -17,6 +17,8 @@ package org.jenkinsci.plugins.saml; +import java.io.IOException; + import org.kohsuke.stapler.StaplerRequest2; import org.kohsuke.stapler.StaplerResponse2; import org.pac4j.core.context.CallContext; @@ -44,15 +46,14 @@ public SamlRedirectActionWrapper(SamlPluginConfig samlPluginConfig, StaplerReque @SuppressWarnings("unused") @Override protected RedirectionAction process() throws IllegalStateException { - try { - SAML2Client client = createSAML2Client(); + try (SAML2Client client = createSAML2Client()) { WebContext context = createWebContext(); SessionStore sessionStore = createSessionStore(); CallContext ctx = new CallContext(context, sessionStore); RedirectionAction redirection = client.getRedirectionAction(ctx).orElse(null); client.destroy(); return redirection; - } catch (HttpAction e) { + } catch (HttpAction|IOException e) { throw new IllegalStateException(e); } } diff --git a/src/main/java/org/jenkinsci/plugins/saml/SamlSPMetadataWrapper.java b/src/main/java/org/jenkinsci/plugins/saml/SamlSPMetadataWrapper.java index e725a8a2..fc957428 100644 --- a/src/main/java/org/jenkinsci/plugins/saml/SamlSPMetadataWrapper.java +++ b/src/main/java/org/jenkinsci/plugins/saml/SamlSPMetadataWrapper.java @@ -17,6 +17,8 @@ package org.jenkinsci.plugins.saml; +import java.io.IOException; + import org.kohsuke.stapler.HttpResponse; import org.kohsuke.stapler.HttpResponses; import org.kohsuke.stapler.StaplerRequest2; @@ -41,12 +43,11 @@ public SamlSPMetadataWrapper(SamlPluginConfig samlPluginConfig, StaplerRequest2 */ @Override protected HttpResponse process() throws IllegalStateException { - SAML2Client client = createSAML2Client(); String metadata = ""; - try { + try (SAML2Client client = createSAML2Client()) { metadata = client.getServiceProviderMetadataResolver().getMetadata(); client.destroy(); - } catch (TechnicalException e) { + } catch (TechnicalException|IOException e) { throw new IllegalStateException(e); } return HttpResponses.text(metadata);