[java-idp-integration-tests] 04/05: Move certificate helper methods to support class

Codeberg noreply at shibboleth.net
Fri Nov 14 22:15:23 UTC 2025


This is an automated email from the git hooks/post-receive script.

codeberg pushed a commit to branch main
in repository java-idp-integration-tests.

View the commit online:
https://codeberg.org/Shibboleth/java-idp-integration-tests/commit/bae0692486adf8add1d9627ae6c09097802fb19b

commit bae0692486adf8add1d9627ae6c09097802fb19b
Author: Tom Zeller <tzeller at dragonacea.biz>
AuthorDate: Mon Nov 10 16:45:25 2025 -0600

    Move certificate helper methods to support class
---
 .../idp/integration/tests/oidc/RPContainer.java    |  93 +--------------
 .../integration/tests/util/CertificateHelper.java  | 126 +++++++++++++++++++++
 2 files changed, 128 insertions(+), 91 deletions(-)

diff --git a/src/test/java/net/shibboleth/idp/integration/tests/oidc/RPContainer.java b/src/test/java/net/shibboleth/idp/integration/tests/oidc/RPContainer.java
index e8e95af..ef15a61 100644
--- a/src/test/java/net/shibboleth/idp/integration/tests/oidc/RPContainer.java
+++ b/src/test/java/net/shibboleth/idp/integration/tests/oidc/RPContainer.java
@@ -17,22 +17,12 @@
 
 package net.shibboleth.idp.integration.tests.oidc;
 
-import java.io.IOException;
-import java.io.StringWriter;
-import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.security.Key;
-import java.security.KeyStore;
-import java.security.cert.Certificate;
-import java.security.cert.CertificateEncodingException;
-import java.util.Enumeration;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
-import org.bouncycastle.util.io.pem.PemObject;
-import org.bouncycastle.util.io.pem.PemWriter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.context.Lifecycle;
@@ -41,8 +31,8 @@ import org.testcontainers.containers.wait.strategy.Wait;
 import org.testcontainers.images.builder.ImageFromDockerfile;
 import org.testcontainers.images.builder.Transferable;
 import org.testcontainers.utility.MountableFile;
-import org.testng.Assert;
 
+import net.shibboleth.idp.integration.tests.util.CertificateHelper;
 import net.shibboleth.shared.annotation.constraint.NonnullAfterInit;
 import net.shibboleth.shared.annotation.constraint.NotEmpty;
 import net.shibboleth.shared.collection.Pair;
@@ -225,7 +215,7 @@ public class RPContainer extends AbstractIdentifiableInitializableComponent impl
         container = new GenericContainer<>(imageFromDockerfile);
 
         // Copy TLS certificate and key to container from P12 keystore
-        final Pair<String,String> certAndKey = extractCertAndKey(pathToKeyStore, keyStorePassword);        
+        final Pair<String, String> certAndKey = CertificateHelper.extractCertAndKey(pathToKeyStore, keyStorePassword);
         container.withCopyToContainer(Transferable.of(certAndKey.getFirst()), "/etc/pki/tls/certs/localhost.crt");
         container.withCopyToContainer(Transferable.of(certAndKey.getSecond()), "/etc/pki/tls/private/localhost.key");
 
@@ -415,85 +405,6 @@ public class RPContainer extends AbstractIdentifiableInitializableComponent impl
         return isRunning;
     }
 
-    /**
-     * Extract the certificate and private key from a keystore in P12 format and
-     * return them in PEM format as strings.
-     * 
-     * @param pathToKeyStore
-     *            path to keystore
-     * @param password
-     *            keystore password
-     * @return pair consisting of the certificate and private key in PEM format
-     */
-    public static Pair<String, String> extractCertAndKey(@Nonnull final Path pathToKeyStore, @Nonnull final String password) {
-        try {
-            // Load the keystore
-            final KeyStore keyStore = KeyStore.getInstance("PKCS12");
-            keyStore.load(Files.newInputStream(pathToKeyStore), password.toCharArray());
-
-            // Keystore should contain only 1 alias
-            final Enumeration<String> aliases = keyStore.aliases();
-            Assert.assertTrue(aliases.hasMoreElements());
-            final String alias = aliases.nextElement();
-            Assert.assertFalse(aliases.hasMoreElements());
-
-            // Extract certificates
-            final StringBuffer certificatesText = new StringBuffer();
-            final Certificate[] certificates = keyStore.getCertificateChain(alias);
-            for (final Certificate certificate : certificates) {
-                certificatesText.append(getCertificateAsPEM(certificate));
-            }
-
-            // Extract private key
-            final Key key = keyStore.getKey(alias, password.toCharArray());
-            final String keyText = getPrivateKeyAsPEM(key);
-
-            return new Pair<String, String>(certificatesText.toString(), keyText);
-        } catch (final Exception e) {
-            Assert.fail("Unable to extract certificate and key from P12 keystore", e);
-            throw new RuntimeException(e);
-        }
-    }
-
-    /**
-     * Get certificate as text in PEM format.
-     * 
-     * @param certificate
-     *            the certificate to convert
-     * @return certificate text in PEM format
-     * @throws CertificateEncodingException
-     *             if an error occurs
-     * @throws IOException
-     *             if an error occurs
-     */
-    public static String getCertificateAsPEM(@Nonnull final Certificate certificate)
-            throws CertificateEncodingException, IOException {
-        final StringWriter writer = new StringWriter();
-        final PemWriter pemWriter = new PemWriter(writer);
-        pemWriter.writeObject(new PemObject("CERTIFICATE", certificate.getEncoded()));
-        pemWriter.flush();
-        pemWriter.close();
-        return writer.toString();
-    }
-
-    /**
-     * Get private key as text in PEM format.
-     * 
-     * @param key
-     *            the key to convert
-     * @return the key as text in PEM format
-     * @throws IOException
-     *             if an error occurs
-     */
-    public static String getPrivateKeyAsPEM(@Nonnull final Key key) throws IOException {
-        final StringWriter writer = new StringWriter();
-        final PemWriter pemWriter = new PemWriter(writer);
-        pemWriter.writeObject(new PemObject("PRIVATE KEY", key.getEncoded()));
-        pemWriter.flush();
-        pemWriter.close();
-        return writer.toString();
-    }
-
     /**
      * Return a prefix for logging messages for this component.
      * 
diff --git a/src/test/java/net/shibboleth/idp/integration/tests/util/CertificateHelper.java b/src/test/java/net/shibboleth/idp/integration/tests/util/CertificateHelper.java
new file mode 100644
index 0000000..b4ae349
--- /dev/null
+++ b/src/test/java/net/shibboleth/idp/integration/tests/util/CertificateHelper.java
@@ -0,0 +1,126 @@
+/*
+ * 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.integration.tests.util;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.Key;
+import java.security.KeyStore;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateEncodingException;
+import java.util.Enumeration;
+
+import javax.annotation.Nonnull;
+
+import org.bouncycastle.util.io.pem.PemObject;
+import org.bouncycastle.util.io.pem.PemWriter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
+
+import net.shibboleth.shared.annotation.constraint.NotEmpty;
+import net.shibboleth.shared.collection.Pair;
+
+/**
+ * 
+ * Support for working with certificates.
+ */
+public class CertificateHelper {
+
+    /** Class logger. */
+    @Nonnull @NotEmpty protected static final Logger log = LoggerFactory.getLogger(CertificateHelper.class);
+
+    /**
+     * Extract the certificate and private key from a keystore in P12 format and return them in PEM format as strings.
+     * 
+     * @param pathToKeyStore
+     *            path to keystore
+     * @param password
+     *            keystore password
+     * @return pair consisting of the certificate and private key in PEM format
+     */
+    public static Pair<String, String> extractCertAndKey(@Nonnull final Path pathToKeyStore,
+            @Nonnull final String password) {
+        try {
+            // Load the keystore
+            final KeyStore keyStore = KeyStore.getInstance("PKCS12");
+            keyStore.load(Files.newInputStream(pathToKeyStore), password.toCharArray());
+
+            // Keystore should contain only 1 alias
+            final Enumeration<String> aliases = keyStore.aliases();
+            Assert.assertTrue(aliases.hasMoreElements());
+            final String alias = aliases.nextElement();
+            Assert.assertFalse(aliases.hasMoreElements());
+
+            // Extract certificates
+            final StringBuffer certificatesText = new StringBuffer();
+            final Certificate[] certificates = keyStore.getCertificateChain(alias);
+            for (final Certificate certificate : certificates) {
+                certificatesText.append(getCertificateAsPEM(certificate));
+            }
+
+            // Extract private key
+            final Key key = keyStore.getKey(alias, password.toCharArray());
+            final String keyText = getPrivateKeyAsPEM(key);
+
+            return new Pair<String, String>(certificatesText.toString(), keyText);
+        } catch (final Exception e) {
+            Assert.fail("Unable to extract certificate and key from P12 keystore", e);
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Get certificate as text in PEM format.
+     * 
+     * @param certificate
+     *            the certificate to convert
+     * @return certificate text in PEM format
+     * @throws CertificateEncodingException
+     *             if an error occurs
+     * @throws IOException
+     *             if an error occurs
+     */
+    public static String getCertificateAsPEM(@Nonnull final Certificate certificate)
+            throws CertificateEncodingException, IOException {
+        final StringWriter writer = new StringWriter();
+        final PemWriter pemWriter = new PemWriter(writer);
+        pemWriter.writeObject(new PemObject("CERTIFICATE", certificate.getEncoded()));
+        pemWriter.flush();
+        pemWriter.close();
+        return writer.toString();
+    }
+
+    /**
+     * Get private key as text in PEM format.
+     * 
+     * @param key
+     *            the key to convert
+     * @return the key as text in PEM format
+     * @throws IOException
+     *             if an error occurs
+     */
+    public static String getPrivateKeyAsPEM(@Nonnull final Key key) throws IOException {
+        final StringWriter writer = new StringWriter();
+        final PemWriter pemWriter = new PemWriter(writer);
+        pemWriter.writeObject(new PemObject("PRIVATE KEY", key.getEncoded()));
+        pemWriter.flush();
+        pemWriter.close();
+        return writer.toString();
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list