package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util;
-import org.apache.commons.codec.binary.Base64;
-
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.KeyStore.TrustedCertificateEntry;
-import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
-import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
+import org.apache.commons.codec.binary.Base64;
+import static java.util.Optional.empty;
+import static java.util.Optional.of;
+
+import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
import static org.slf4j.LoggerFactory.getLogger;
/**
*/
public final class StoreLoader {
- public static final String PASSWORD = "password";
+ public static final String PASS_ALIAS = "password";
private static final String RSA = "RSA";
private static final String X_509 = "X.509";
private static final String SUN = "SUN";
if (rindex == -1 || lindex == -1) {
return "";
}
- String substring = content.substring(rindex, lindex) + end(scope);
- return substring;
+ return content.substring(rindex, lindex) + end(scope);
}
/**
*/
public static KeyStore loadStore(String pem, String storePassword, String keyPassword) {
Optional<PrivateKey> privateKey = generatePrivateKey(pem);
- Certificate[] certs = createCertificates(pem);
+ Optional<Certificate[]> certs = createCertificates(pem);
try {
KeyStore ks = KeyStore.getInstance(JKS, SUN);
ks.load(null, storePassword.toCharArray());
if (privateKey.isPresent()) {
- ks.setKeyEntry(PASSWORD, privateKey.get(), keyPassword.toCharArray(), certs);
- } else if (certs != null) {
+ ks.setKeyEntry(PASS_ALIAS, privateKey.get(), keyPassword.toCharArray(), certs.orElse(null));
+ } else if (certs.isPresent()) {
int index = 0;
- for (Certificate cert : certs) {
+ for (Certificate cert : certs.get()) {
TrustedCertificateEntry ts = new TrustedCertificateEntry(cert);
- ks.setEntry(PASSWORD + index, ts, null);
+ ks.setEntry(PASS_ALIAS + index, ts, null);
index++;
}
}
return ks;
} catch (Exception e) {
- throw new RuntimeException("Unable to create keystore", e);
+ throw new UserInvisibleError("Unable to create keystore", e);
}
}
- private static Certificate[] createCertificates(String pem) {
+ private static Optional<Certificate[]> createCertificates(String pem) {
Set<Certificate> certificates = new HashSet<>();
try {
for (String certificate : getCertifacates(pem)) {
certificates.addAll(c);
}
} catch (Exception e) {
- logger.error(e.getMessage(), e);
- throw new RuntimeException("Unable to load certificates", e);
+ throw buildFatalFailure(logger, "Unable to load certificates", e);
}
- if (certificates.size() > 0) {
- return certificates.toArray(new Certificate[certificates.size()]);
+ if (!certificates.isEmpty()) {
+ return of(certificates.toArray(new Certificate[certificates.size()]));
} else {
- return null;
+ return empty();
}
}
if (!key.isEmpty()) {
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(toDer(key, RSA_PRIVATE_KEY));
- return Optional.of(keyFactory.generatePrivate(keySpec));
+ return of(keyFactory.generatePrivate(keySpec));
}
- return Optional.empty();
- } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
- logger.error("Unable to load key", e);
- throw new RuntimeException("Unable to load key", e);
+ return empty();
+ } catch (Exception e) {
+ throw buildFatalFailure(logger, "Unable to load key", e);
}
}