Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 5 #8

Merged
merged 5 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ The pepper can be expressed as `SecureString` as well.
Password4j makes available a portable way to configure the library.

With the property file `psw4j.properties` put in your classpath, you can define the parameters of all the supported CHFs or just the CHF(s) you need.
Alternatively you can specify a custom path with the system property `-Dpsw4j.configuration`

```shell script
java -Dpsw4j.configuration=/my/path/to/some.properties ...
```

Here's a basic configuration (please do not use it in production, but instead start a benchmark session in your target environment<sup>see [Performance section](#Performance)</sup>)
```properties
Expand Down
121 changes: 101 additions & 20 deletions src/main/java/com/password4j/PropertyReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
package com.password4j;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
Expand All @@ -28,32 +31,18 @@ class PropertyReader

private static final Logger LOG = LoggerFactory.getLogger(PropertyReader.class);

private static final String FILE_NAME = "psw4j.properties";
private static final String FILE_NAME = "/psw4j.properties";

private static final String CONFIGURATION_KEY = "psw4j.configuration";

private static final String MESSAGE = "{}. Default value is used ({}). Please set property {} in your " + FILE_NAME + " file.";


static final Properties PROPERTIES;
protected static Properties properties;

static
{
InputStream in = PropertyReader.class.getResourceAsStream("/" + FILE_NAME);
Properties props = new Properties();


if (in != null)
{
try
{
props.load(in);
}
catch (IOException e)
{
//
}
}

PROPERTIES = props;
init();
}

static int readInt(String key, int defaultValue, String message)
Expand Down Expand Up @@ -116,9 +105,101 @@ private static String readString(String key)
{
throw new BadParametersException("Key cannot be null");
}
return PROPERTIES.getProperty(key);
return properties.getProperty(key);
}

static void init()
{
String customPath = System.getProperty(CONFIGURATION_KEY, null);

InputStream in;
if (StringUtils.isEmpty(customPath))
{
in = getResource(FILE_NAME);
}
else
{
in = getResource(customPath);
}

Properties props = new Properties();


if (in != null)
{
try
{
props.load(in);
}
catch (IOException e)
{
//
}
}
else
{
LOG.debug("Cannot find any properties file.");
}

properties = props;
}


static InputStream getResource(String resource)
{
ClassLoader classLoader;
InputStream in;

try
{
classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader != null)
{
in = classLoader.getResourceAsStream(resource);
if (in != null)
{
return in;
}
}


// Try with the class loader that loaded this class
classLoader = PropertyReader.class.getClassLoader();
if (classLoader != null)
{
in = classLoader.getResourceAsStream(resource);
if (in != null)
{
return in;
}
}
}
catch (Exception e)
{
LOG.warn("",e);
}

// Get the resource from the class path in case that the class is loaded
// by the Extension class loader which the parent of the system class loader.
in = ClassLoader.getSystemResourceAsStream(resource);
if(in != null)
{
return in;
}


try
{
return new FileInputStream(resource);
}
catch (FileNotFoundException e)
{
return PropertyReader.class.getResourceAsStream(resource);
}

}


private PropertyReader()
{
//
Expand Down
4 changes: 2 additions & 2 deletions src/test/com/password4j/PasswordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,15 @@ public void testConfigurablePepper()
public void testSecureNeverNull() throws ClassNotFoundException
{
// GIVEN
PropertyReader.PROPERTIES.put("global.random.strong", "true");
PropertyReader.properties.put("global.random.strong", "true");

// WHEN
SecureRandom sr = AlgorithmFinder.getSecureRandom();

// THEN
Assert.assertNotNull(sr);

PropertyReader.PROPERTIES.put("global.random.strong", "false");
PropertyReader.properties.put("global.random.strong", "false");

}

Expand Down
77 changes: 77 additions & 0 deletions src/test/com/password4j/PropertyReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,31 @@
*/
package com.password4j;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;


public class PropertyReaderTest
{

@After
@Before
public void setup()
{
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
System.clearProperty("psw4j.configuration");
PropertyReader.init();
}

@Test
public void testInt()
{
Expand Down Expand Up @@ -91,4 +110,62 @@ public void testNull()
{
PropertyReader.readString(null, "null", null);
}

@Test
public void testInitInvalidPath()
{
// GIVEN
System.setProperty("psw4j.configuration", "/my/improbable/path/xyz.properties");

// WHEN
PropertyReader.init();

// THEN
Assert.assertTrue(PropertyReader.properties.isEmpty());
}

@Test
public void testInitCustomPath() throws Exception
{
// GIVEN
String path = new File(".").getCanonicalPath() + "/src/test/my/custom/path/to/some.properties";
System.out.println(path);
System.setProperty("psw4j.configuration", path);

// WHEN
PropertyReader.init();

// THEN
Assert.assertFalse(PropertyReader.properties.isEmpty());
Assert.assertEquals("hello!!", PropertyReader.readString("check.this.out", "kappa", null));
}

@Test
public void testNoThreadClassLoader() throws Exception
{
// GIVEN
String path = new File(".").getCanonicalPath() + "/src/test/my/custom/path/to/some.properties";
System.out.println(path);
System.setProperty("psw4j.configuration", path);
Thread.currentThread().setContextClassLoader(null);

// WHEN
PropertyReader.init();

// THEN
Assert.assertFalse(PropertyReader.properties.isEmpty());
Assert.assertEquals("hello!!", PropertyReader.readString("check.this.out", "kappa", null));
}

@Test
public void testResource1()
{
// GIVEN

// WHEN
InputStream in = PropertyReader.getResource("PropertyReader.class");
// THEN
Assert.assertNotNull(in);
}

}
1 change: 1 addition & 0 deletions src/test/my/custom/path/to/some.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
check.this.out=hello!!