summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorParshad Patel <pars.patel@samsung.com>2018-10-26 15:24:33 +0900
committerParshad Patel <pars.patel@samsung.com>2018-10-26 15:24:47 +0900
commitdf4a7a3530a0c52c17b1cb9a412de0c5b3fd01b3 (patch)
tree5d58c61aa6c8cbfd976549412ceb7d83f4dcd831
parentf9865fbaaffa841172ca0ae8e5c05cb95d1c7d0c (diff)
Fix sonar blocker issue in sli/adapters
Fix use try-with-resources issues in ansible-adapter,salstack-adapter Issue-ID: CCSDK-629 Change-Id: Ie8457751c155aa95fdfcdf2faad4cd8089a58ad3 Signed-off-by: Parshad Patel <pars.patel@samsung.com>
-rw-r--r--ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/impl/ConnectionBuilder.java26
-rw-r--r--saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java15
2 files changed, 20 insertions, 21 deletions
diff --git a/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/impl/ConnectionBuilder.java b/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/impl/ConnectionBuilder.java
index fbc77346..645a7baf 100644
--- a/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/impl/ConnectionBuilder.java
+++ b/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/impl/ConnectionBuilder.java
@@ -79,22 +79,22 @@ public class ConnectionBuilder {
KeyManagementException, NoSuchAlgorithmException, SvcLogicException {
/* Point to the certificate */
- FileInputStream fs = new FileInputStream(certFile);
+ try(FileInputStream fs = new FileInputStream(certFile)){
+ /* Generate a certificate from the X509 */
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert = (X509Certificate) cf.generateCertificate(fs);
- /* Generate a certificate from the X509 */
- CertificateFactory cf = CertificateFactory.getInstance("X.509");
- X509Certificate cert = (X509Certificate) cf.generateCertificate(fs);
+ /* Create a keystore object and load the certificate there */
+ KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
+ keystore.load(null, null);
+ keystore.setCertificateEntry("cacert", cert);
- /* Create a keystore object and load the certificate there */
- KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
- keystore.load(null, null);
- keystore.setCertificateEntry("cacert", cert);
+ SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keystore).build();
+ SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext,
+ SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
- SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keystore).build();
- SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext,
- SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
-
- httpClient = HttpClients.custom().setSSLSocketFactory(factory).build();
+ httpClient = HttpClients.custom().setSSLSocketFactory(factory).build();
+ }
}
/**
diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java
index ef54104e..34321ea5 100644
--- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java
+++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java
@@ -260,16 +260,15 @@ public class SaltstackAdapterImpl implements SaltstackAdapter {
doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), "Input file " +
"is not of type .sls");
}
- InputStream in = new FileInputStream(file);
- byte[] data = new byte[(int) file.length()];
- in.read(data);
- String str = new String(data, "UTF-8");
- in.close();
- String slsWithoutExtn = stripExtension(slsFile);
- constructedCommand.append(parseFileParam(fileParams)).append("echo -e \"").append(str).append("\" > /srv/salt/").
+ try(InputStream in = new FileInputStream(file)){
+ byte[] data = new byte[(int) file.length()];
+ in.read(data);
+ String str = new String(data, "UTF-8");
+ String slsWithoutExtn = stripExtension(slsFile);
+ constructedCommand.append(parseFileParam(fileParams)).append("echo -e \"").append(str).append("\" > /srv/salt/").
append(slsFile).append("; ").append(COMMAND_CHANGE_DEFAULT_DIR).append(" salt '").
append(applyTo).append("' state.apply ").append(slsWithoutExtn).append(" ").append(parseEnvParam(envParams)).append(COMMAND_IN_JSON_OUT);
-
+ }
logger.info("Command to be executed on server : " + constructedCommand.toString());
} catch (FileNotFoundException e) {