From c1521dbf3b9012f090aa744cdd5420c1cd715489 Mon Sep 17 00:00:00 2001 From: pkaras Date: Wed, 7 Nov 2018 10:37:55 +0100 Subject: [PATCH] sonar fixes and junits Change-Id: I105406b73b55d0c4befad1c8120752079837abd3 Issue-ID: DCAEGEN2-957 Signed-off-by: piotr.karas --- .../onap/dcaegen2/services/prh/ssl/SslFactory.java | 32 +++++++++-- .../dcaegen2/services/prh/ssl/SslFactoryTest.java | 62 +++++++++++++++++++++ prh-commons/src/test/resources/keystore.password | 1 + prh-commons/src/test/resources/org.onap.dcae.jks | Bin 0 -> 4512 bytes .../src/test/resources/org.onap.dcae.trust.jks | Bin 0 -> 1413 bytes prh-commons/src/test/resources/truststore.password | 1 + 6 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 prh-commons/src/test/java/org/onap/dcaegen2/services/prh/ssl/SslFactoryTest.java create mode 100644 prh-commons/src/test/resources/keystore.password create mode 100644 prh-commons/src/test/resources/org.onap.dcae.jks create mode 100644 prh-commons/src/test/resources/org.onap.dcae.trust.jks create mode 100644 prh-commons/src/test/resources/truststore.password diff --git a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/ssl/SslFactory.java b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/ssl/SslFactory.java index 891bcb73..60e1224e 100644 --- a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/ssl/SslFactory.java +++ b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/ssl/SslFactory.java @@ -24,9 +24,12 @@ import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; +import java.security.GeneralSecurityException; import java.security.KeyStore; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLException; @@ -38,6 +41,15 @@ public class SslFactory { private static final Logger LOGGER = LoggerFactory.getLogger(SslFactory.class); + /** + * Function for creating secure ssl context. + * + * @param keyStorePath - path to file with keystore + * @param keyStorePasswordPath - path to file with keystore password + * @param trustStorePath - path to file with truststore + * @param trustStorePasswordPath - path to file with truststore password + * @return configured ssl context + */ public SslContext createSecureContext(String keyStorePath, String keyStorePasswordPath, String trustStorePath, @@ -49,11 +61,16 @@ public class SslFactory { .keyManager(keyManagerFactory(keyStorePath, loadPasswordFromFile(keyStorePasswordPath))) .trustManager(trustManagerFactory(trustStorePath, loadPasswordFromFile(trustStorePasswordPath))) .build(); - } catch (Exception ex) { + } catch (GeneralSecurityException | IOException ex) { throw new SSLException(ex); } } + /** + * Function for creating insecure ssl context. + * + * @return configured insecure ssl context + */ public SslContext createInsecureContext() throws SSLException { LOGGER.info("Creating insecure ssl context"); return SslContextBuilder @@ -62,30 +79,33 @@ public class SslFactory { .build(); } - private KeyManagerFactory keyManagerFactory(String path, String password) throws Exception { + private KeyManagerFactory keyManagerFactory(String path, String password) + throws GeneralSecurityException, IOException { KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(loadKeyStoreFromFile(path, password), password.toCharArray()); return kmf; } - private TrustManagerFactory trustManagerFactory(String path, String password) throws Exception { + private TrustManagerFactory trustManagerFactory(String path, String password) + throws GeneralSecurityException, IOException { TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(loadKeyStoreFromFile(path, password)); return tmf; } - private KeyStore loadKeyStoreFromFile(String path, String keyStorePassword) throws Exception { + private KeyStore loadKeyStoreFromFile(String path, String keyStorePassword) + throws GeneralSecurityException, IOException { KeyStore ks = KeyStore.getInstance("jks"); ks.load(getResource(path), keyStorePassword.toCharArray()); return ks; } - private InputStream getResource(String path) throws Exception { + private InputStream getResource(String path) throws FileNotFoundException { return new FileInputStream(path); } - private String loadPasswordFromFile(String path) throws Exception { + private String loadPasswordFromFile(String path) throws IOException { return new String(Files.readAllBytes(Paths.get(path))); } } diff --git a/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/ssl/SslFactoryTest.java b/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/ssl/SslFactoryTest.java new file mode 100644 index 00000000..dbd63911 --- /dev/null +++ b/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/ssl/SslFactoryTest.java @@ -0,0 +1,62 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.ssl; + +import javax.net.ssl.SSLException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + +class SslFactoryTest { + + private static final String KEY_STORE = "org.onap.dcae.jks"; + private static final String KEYSTORE_PASSWORD = "keystore.password"; + private static final String TRUSTSTORE_PASSWORD = "truststore.password"; + private static final String TRUST_STORE = "org.onap.dcae.trust.jks"; + private SslFactory sslFactory = new SslFactory(); + + @Test + void shouldCreateInsecureContext() throws SSLException { + Assertions.assertNotNull(sslFactory.createInsecureContext()); + } + + @Test + void shouldCreateSecureContext() throws SSLException { + Assertions.assertNotNull(sslFactory.createSecureContext( + getPath(KEY_STORE), + getPath(KEYSTORE_PASSWORD), + getPath(TRUST_STORE), + getPath(TRUSTSTORE_PASSWORD))); + } + + @Test + void shouldThrowSslExceptionWhenKeystorePasswordIsIncorrect() { + Assertions.assertThrows(SSLException.class, () -> sslFactory.createSecureContext( + getPath(KEY_STORE), + getPath(TRUSTSTORE_PASSWORD), + getPath(TRUST_STORE), + getPath(TRUSTSTORE_PASSWORD))); + } + + private String getPath(String fileName) { + return this.getClass().getClassLoader().getResource(fileName).getPath(); + } +} \ No newline at end of file diff --git a/prh-commons/src/test/resources/keystore.password b/prh-commons/src/test/resources/keystore.password new file mode 100644 index 00000000..39823872 --- /dev/null +++ b/prh-commons/src/test/resources/keystore.password @@ -0,0 +1 @@ +mYHC98!qX}7h?W}jRv}MIXTJ \ No newline at end of file diff --git a/prh-commons/src/test/resources/org.onap.dcae.jks b/prh-commons/src/test/resources/org.onap.dcae.jks new file mode 100644 index 0000000000000000000000000000000000000000..e74ce64f4b680b9f7b226086ff7632ad594a7174 GIT binary patch literal 4512 zcmb`JcT|(f8piXbQ6oqwB1M!UC}F=)MWspaC?HMg2?>ORE{I|bg33~qvVwF7h|(2M z5Gk^t2!cvcY2vydWx;?-lX8RH-Ghqvo_o&y<4fj!X685byw9votxiE85GeBlzJ5IJ zZZ0^@^&dG3*~LeW;^)Dv!bjeXScE_j07!$cfZLGl`;h<~*azJX00;=21|L_t_`qVp zF)iQ!rfR-6Vh5CR(YcaWapx;pi9Z;&XZ6g{qR%us7uIYpoCJ)ca_DQf1y@1}D-zzR zvKU2%2D>_#Fr2S~o@r)veDO||BZu<1anFD8jY^pT-Z-3NWQ>I!5|CiCOY_cgm_k!a zTrH{>r_9P$e=|QzcjwWF%`_?8{qg`y1TOiaXWm4)*!ke@xMpXO-3PA*pXt8Cp)8E% zzuFupiKi!z;Xhu-EedK#NBS$M2&;@J3|!)>nqcAnBBhv_WWLa^Z9v^=Z)w;R#p3;0 zDlv8Zd_n~M8Gdph?-1F8H%crm{ZT|UnRAFK$;ld0}iXw=UB ztx@G8q6L+c(k+1hEFR6^E+3rXW8Xd_;Z<|tRbs%rc#qz^FWS_v?0GY`uXss%xcglz zB~AHNlMM`LR)eNr)a{fs)(k5bd&!bn`~LUE(jJ(9^ZEVD5=x?H7Zzemr6xsRiTN+t zv=Lfi^kzLaUD+?gVc~%#ekt5KE^p~pd~?>sPq8akGL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z(OWE{rZ!#hCiM`;846aD8w2o~S`p0Zs^a7=Iez$ehsj>Uo9xSXJ7ke8_>s-$_47 z=nO?Z7^6B5PHcaFtk7ws2z~O4^GDvyxX+H;T=3T+H;TztnicLI(-hkEG zpjvw31c+3-Cmnynt3~%3-q&sCDT7g)UBBYdLwowU%yZ7D3q7vouNm?kt0{*3?fGz9 zW;(Y5Y0$$Pp3lsq-+BH2l!rGA9aNQ9#9|fYvFnB|udpfn|Ggakn_cG~wxLGGHdYcE zhYcA`v)8zcEo&~?FWU*{E|>NrW0c#&=%dJhG?d7%PmSxJ%WfZ-)r?tGev%$}8e$&W zpl052_YjbA=2r5AYF(dMhTH&^(@xa(pyq6>bSf)6522WTElphY2m4D81)@X- zDO5hLoYTQhPm|NKD)k@nS7w+XIr6V4o+{O{(-B+@5aVF&0IOm)>>3ZcZ}P>hycXQG zVa7W^ek$*je{-rhwT&TI!)9B%HdAE2uT}}mp027p>R(1QL(w7<%3K_}Rbyra=nF0tJk1SPb)*py80!*>M)K~G z$L~2jF3a8FX4H=kwjSIq04DBGn~Ov3mwu3$VI)*>r0-p7@4bBfqT?;{C-KvT*z*ROK8*C-Zi@7=zXq)aUjT z)$WcyyS5^yvI1&`sq_^&9(TAG4bgMrCU_m0s*%JsVp~WN`^h3&T-WfcI&#Li1{3Fk zJx26cr-QDNf0_dOY@{W)-6?y!PH997e75vvA(Sq75a!m}-F3%>zW;#sIy+Xv^Xe*nQMV&dJvy>S3--w=2d(pP|M-nU(|jnQgO(o|{-mQr-*~Z{;Y!IJnmS{4 T=H#ViMFn!SC%a)<6wki^WqwD} literal 0 HcmV?d00001 diff --git a/prh-commons/src/test/resources/org.onap.dcae.trust.jks b/prh-commons/src/test/resources/org.onap.dcae.trust.jks new file mode 100644 index 0000000000000000000000000000000000000000..10103cfbcd527d081f04dacd1a06f6ddffb72d3c GIT binary patch literal 1413 zcmezO_TO6u1_mY|W(3n*$%*kf`N@en@diMlw6^W7zZqB~^h^yb85mfd44PQ&44R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s)*<1kt literal 0 HcmV?d00001 diff --git a/prh-commons/src/test/resources/truststore.password b/prh-commons/src/test/resources/truststore.password new file mode 100644 index 00000000..168e64bd --- /dev/null +++ b/prh-commons/src/test/resources/truststore.password @@ -0,0 +1 @@ +*TQH?Lnszprs4LmlAj38yds( \ No newline at end of file -- 2.16.6