Skip to content

Commit

Permalink
Key store loading now throws Phase4RuntimeException in case of error
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Dec 2, 2024
1 parent b26820f commit 2ca62b2
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.helger.config.IConfig;
import com.helger.config.fallback.IConfigWithFallback;
import com.helger.phase4.config.AS4Configuration;
import com.helger.phase4.util.Phase4RuntimeException;
import com.helger.security.keystore.IKeyStoreAndKeyDescriptor;
import com.helger.security.keystore.ITrustStoreDescriptor;
import com.helger.security.keystore.LoadedKey;
Expand Down Expand Up @@ -62,11 +63,11 @@ public class AS4CryptoFactoryConfiguration extends AS4CryptoFactoryInMemoryKeySt
* @return The default instance, created by reading the default properties
* from the configuration sources (application.properties, environment
* variables and Java system properties).
* @throws RuntimeException
* @throws Phase4RuntimeException
* if one of the mandatory configuration parameters is not present.
*/
@Nonnull
public static AS4CryptoFactoryConfiguration getDefaultInstance ()
public static AS4CryptoFactoryConfiguration getDefaultInstance () throws Phase4RuntimeException
{
// Don't store this in a static variable, because it may fail if the
// respective configuration properties are not present
Expand All @@ -86,7 +87,7 @@ public static AS4CryptoFactoryConfiguration getDefaultInstanceOrNull ()
{
return getDefaultInstance ();
}
catch (final RuntimeException ex)
catch (final Phase4RuntimeException ex)
{
// Use debug level only, as this is used in many default scenarios
if (LOGGER.isDebugEnabled ())
Expand All @@ -104,36 +105,51 @@ public static AS4CryptoFactoryConfiguration getDefaultInstanceOrNull ()
*
* @param aConfig
* The configuration object to be used. May not be <code>null</code>.
* @throws Phase4RuntimeException
* If loading the key store configuration from configuration fails.
*/
public AS4CryptoFactoryConfiguration (@Nonnull final IConfigWithFallback aConfig)
public AS4CryptoFactoryConfiguration (@Nonnull final IConfigWithFallback aConfig) throws Phase4RuntimeException
{
this (aConfig, CAS4Crypto.DEFAULT_CONFIG_PREFIX);
}

@Nonnull
private static IKeyStoreAndKeyDescriptor _loadKeyStore (@Nonnull final IConfigWithFallback aConfig,
@Nonnull @Nonempty final String sConfigPrefix)
@Nonnull @Nonempty final String sConfigPrefix) throws Phase4RuntimeException
{
// Load the keystore - may be null
final IKeyStoreAndKeyDescriptor aDescriptor = AS4KeyStoreDescriptor.createFromConfig (aConfig, sConfigPrefix, null);
if (aDescriptor == null)
{
final String sMsg = "Failed to load the key store configuration from properties starting with '" +
sConfigPrefix +
"'";
LOGGER.error (sMsg);
throw new Phase4RuntimeException (sMsg);
}

final LoadedKeyStore aLKS = aDescriptor.loadKeyStore ();
if (aLKS.getKeyStore () == null)
{
LOGGER.error ("Failed to load the key store from the properties starting with '" +
sConfigPrefix +
"': " +
aLKS.getErrorText (Locale.ROOT));
final String sMsg = "Failed to load the key store from the properties starting with '" +
sConfigPrefix +
"': " +
aLKS.getErrorText (Locale.ROOT);
LOGGER.error (sMsg);
throw new Phase4RuntimeException (sMsg);
}
else

final LoadedKey <PrivateKeyEntry> aLK = aDescriptor.loadKey ();
if (aLK.getKeyEntry () == null)
{
final LoadedKey <PrivateKeyEntry> aLK = aDescriptor.loadKey ();
if (aLK.getKeyEntry () == null)
{
LOGGER.error ("Failed to load the prvate key from the key store properties starting with '" +
sConfigPrefix +
"': " +
aLK.getErrorText (Locale.ROOT));
}
final String sMsg = "Failed to load the private key from the key store properties starting with '" +
sConfigPrefix +
"': " +
aLK.getErrorText (Locale.ROOT);
LOGGER.error (sMsg);
throw new Phase4RuntimeException (sMsg);
}

return aDescriptor;
}

Expand Down Expand Up @@ -166,9 +182,11 @@ private static ITrustStoreDescriptor _loadTrustStore (@Nonnull final IConfigWith
* @param sConfigPrefix
* The configuration prefix to be used. May neither be
* <code>null</code> nor empty and must end with a dot ('.').
* @throws Phase4RuntimeException
* If loading the key store configuration from configuration fails.
*/
public AS4CryptoFactoryConfiguration (@Nonnull final IConfigWithFallback aConfig,
@Nonnull @Nonempty final String sConfigPrefix)
@Nonnull @Nonempty final String sConfigPrefix) throws Phase4RuntimeException
{
this (_loadKeyStore (aConfig, sConfigPrefix), _loadTrustStore (aConfig, sConfigPrefix));
}
Expand All @@ -183,11 +201,11 @@ public AS4CryptoFactoryConfiguration (@Nonnull final IConfigWithFallback aConfig
* the global JRE CA certs list will be used.
*/
private AS4CryptoFactoryConfiguration (@Nonnull final IKeyStoreAndKeyDescriptor aKeyStoreDesc,
@Nonnull final ITrustStoreDescriptor aTrustStorDesc)
@Nullable final ITrustStoreDescriptor aTrustStoreDesc)
{
super (aKeyStoreDesc, aTrustStorDesc);
super (aKeyStoreDesc, aTrustStoreDesc);
m_aKeyStoreDesc = aKeyStoreDesc;
m_aTrustStorDesc = aTrustStorDesc;
m_aTrustStorDesc = aTrustStoreDesc;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2015-2024 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* 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 com.helger.phase4.util;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* phase4 base runtime exception. It is just a in-between exception for easy
* catching of all phase4 related runtime exceptions.
*
* @author Philip Helger
* @since 3.0.1
*/
public class Phase4RuntimeException extends RuntimeException
{
/**
* @param sMessage
* Error message
*/
public Phase4RuntimeException (@Nonnull final String sMessage)
{
super (sMessage);
}

/**
* @param aCause
* Optional causing exception
* @since 0.13.0
*/
public Phase4RuntimeException (@Nullable final Throwable aCause)
{
super (aCause);
}

/**
* @param sMessage
* Error message
* @param aCause
* Optional causing exception
*/
public Phase4RuntimeException (@Nonnull final String sMessage, @Nullable final Throwable aCause)
{
super (sMessage, aCause);
}
}

0 comments on commit 2ca62b2

Please sign in to comment.