* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.aaa.cert.impl;
import java.io.BufferedWriter;
}
+ public static File toAbsoluteFile(final String fileName, final String basePath) {
+ final File file = new File(fileName);
+ return file.isAbsolute() ? file : new File(basePath + fileName);
+ }
+
public static boolean checkKeyStoreFile(final String fileName) {
- final File file = new File(KEY_STORE_PATH + fileName);
- return file.exists();
+ return toAbsoluteFile(fileName, KEY_STORE_PATH).exists();
}
public static String createDir(final String dir) {
if (certFile == null || certFile.isEmpty()) {
return null;
}
-
- final String path = KEY_STORE_PATH + certFile;
+ final File path = toAbsoluteFile(certFile, KEY_STORE_PATH);
try (FileInputStream fInputStream = new FileInputStream(path)) {
final int available = fInputStream.available();
final byte[] certBytes = new byte[available];
if (fileName == null || fileName.isEmpty()) {
return false;
}
-
+ final File path = toAbsoluteFile(fileName, KEY_STORE_PATH);
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(KEY_STORE_PATH + fileName), StandardCharsets.UTF_8))) {
+ new FileOutputStream(path), StandardCharsets.UTF_8))) {
out.write(cert);
return true;
} catch (final IOException e) {
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.aaa.cert.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
* such as create, generate, add and delete certificates.
*
* @author mserngawy
- *
*/
public class ODLKeyTool {
-
private static final Logger LOG = LoggerFactory.getLogger(ODLKeyTool.class);
private final String workingDir;
protected ODLKeyTool() {
- workingDir = KeyStoreConstant.KEY_STORE_PATH;
- KeyStoreConstant.createDir(workingDir);
+ this(KeyStoreConstant.KEY_STORE_PATH);
}
public ODLKeyTool(final String workingDirectory) {
if (keystore == null) {
return false;
}
- try (FileOutputStream fOutputStream = new FileOutputStream(workingDir + fileName)) {
+
+ final File realPath = KeyStoreConstant.toAbsoluteFile(fileName, workingDir);
+ try (FileOutputStream fOutputStream = new FileOutputStream(realPath)) {
keystore.store(fOutputStream, keystorePassword.toCharArray());
return true;
} catch (final KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
* @return keystore object otherwise return null if it fails to load.
*/
public KeyStore loadKeyStore(final String keyStoreName, final String keystorePassword) {
- try (FileInputStream fInputStream = new FileInputStream(workingDir + keyStoreName)) {
+ final File realPath = KeyStoreConstant.toAbsoluteFile(keyStoreName, workingDir);
+ try (FileInputStream fInputStream = new FileInputStream(realPath)) {
final KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(fInputStream, keystorePassword.toCharArray());
return keyStore;