Skip to content

Commit

Permalink
Fixes installation with space in HSM Label (#3)
Browse files Browse the repository at this point in the history
- This is linked to the `pki` bug-fix ticket-3054

Pagure ticket: https://pagure.io/tomcatjss/issue/12

Signed-off-by: Dinesh Prasanth M K <[email protected]>
  • Loading branch information
SilleBille authored Aug 31, 2018
1 parent b841b2e commit 6705223
Showing 1 changed file with 90 additions and 8 deletions.
98 changes: 90 additions & 8 deletions src/org/apache/tomcat/util/net/jss/PlainPasswordFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,96 @@

package org.apache.tomcat.util.net.jss;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.Properties;

public class PlainPasswordFile implements IPasswordStore {
private String mPwdPath = "";
private Properties mPwdStore;
private static final String PASSWORD_WRITER_HEADER = "";
private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(PlainPasswordFile.class);

public PlainPasswordFile() {
mPwdStore = new Properties();
}

/**
* Initialization method to read passwords(key and element pairs) from a file.
* <p>
* Every property occupies one line of the input stream. Each line is terminated by a line terminator (
* <code>\n</code> or <code>\r</code> or <code>\r\n</code>). Lines are processed until end of
* file is reached.
* <p>
* A line that contains only whitespace or whose first non-whitespace character is an ASCII <code>#</code>
* is ignored (thus, <code>#</code> indicates comment line).
* <p>
* Every line other than a blank line or a comment line describes one property to be added to the table.
* The characters before the delimiter <code>=</code> forms the <code>key</code> and the characters after
* the <code>=</code> is assigned as <code>value</code> to the key.
* <p>
* As an example, each of the following lines specify the key <code>"Truth"</code> and the associated element
* value <code>"Beauty"</code>:
* <p>
*
* <pre>
* Truth = Beauty
* Truth= Beauty
* Truth =Beauty
* </pre>
*
* <p>
* Note that the space appearing before/after <code>=</code> is ignored. However, the space appearing in between are
* stored.
* <p>
* Example:
*
* <pre>
* Welcome Message = Hello World
* </pre>
*
* assigns value <code>Hello World</code> to key <code>Welcome Message</code>
* <p>
*
* If the line doesn't have the delimiter <code>=</code>, the method throws an IOException
*
* @param pwdPath the input file path.
* @exception IOException if an error occurred when reading from the
* input stream.
*/
public void init(String pwdPath) throws IOException {
mPwdStore = new Properties();
logger.debug("PlainPasswordFile: Initializing PlainPasswordFile");
// initialize mPwdStore
mPwdPath = pwdPath;

FileInputStream file = new FileInputStream(mPwdPath);
mPwdStore.load(file);
try (FileInputStream file = new FileInputStream(mPwdPath);
InputStreamReader isr = new InputStreamReader(file);
BufferedReader br = new BufferedReader(isr)) {

String line;
int index = 1;
while ((line = br.readLine()) != null) {
// Remove any leading or trailing spaces
line = line.trim();

if (line.startsWith("#") || line.isEmpty())
continue;

String[] parts = line.split("=", 2);
if (parts.length < 2) {
throw new IOException("Missing delimiter '=' in file " + mPwdPath + " in line " + index);
}

// Load key value into the password store
mPwdStore.put(parts[0].trim(), parts[1].trim());
index++;
}
}
}

public String getPassword(String tag) {
Expand All @@ -60,9 +129,22 @@ public Object putPassword(String tag, String password) {
return mPwdStore.setProperty(tag, password);
}

public void commit() throws IOException, ClassCastException,
NullPointerException {
FileOutputStream file = new FileOutputStream(mPwdPath);
mPwdStore.store(file, PASSWORD_WRITER_HEADER);
public synchronized void commit()
throws IOException, ClassCastException, NullPointerException {
try (FileOutputStream file = new FileOutputStream(mPwdPath);
OutputStreamWriter osw = new OutputStreamWriter(file);
BufferedWriter bw = new BufferedWriter(osw)) {

for (Enumeration<?> e = mPwdStore.keys(); e.hasMoreElements();) {
String key = ((String) e.nextElement()).trim();
String val = ((String) mPwdStore.get(key)).trim();
bw.write(key + "=" + val);
bw.newLine();
}
}
}

public int getSize() {
return mPwdStore.size();
}
}

0 comments on commit 6705223

Please sign in to comment.