Skip to content

Commit

Permalink
report helpful error message for missing sensitive properties file (#…
Browse files Browse the repository at this point in the history
…1134)

* format code

* code fix: display helpful message for missing atlas config properties files

* handle if there is exception while closing the inputstream

* Revert "handle if there is exception while closing the inputstream"

This reverts commit a00109f.

* handle if there is exception while closing the inputstream

* log the error message for missing atlas properties files

* handle file not found exception for config command

* Allow config command to create missing sensitive.properties file

* refactor code

* update the log message

* fix typo

---------

Co-authored-by: Jagdish Kunwar <[email protected]>
  • Loading branch information
2 people authored and alexgao1 committed Apr 4, 2023
1 parent 6876f7f commit 6cdb063
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,41 @@
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.carleton.gcrc.utils.PropertiesWriter;

public class AtlasProperties {

static final private Logger logger = LoggerFactory.getLogger(AtlasProperties.class);

static public AtlasProperties fromAtlasDir(File atlasDir) throws Exception {
Properties props = new Properties();
readProperties(atlasDir, props);

return fromProperties(props);
}

static public AtlasProperties fromProperties(Properties props) throws Exception {
AtlasProperties atlasProps = new AtlasProperties();

atlasProps.setAtlasName( props.getProperty("atlas.name") );
atlasProps.setCouchDbName( props.getProperty("couchdb.dbName") );
atlasProps.setCouchDbSubmissionDbName( props.getProperty("couchdb.submission.dbName") );
atlasProps.setCouchDbAdminUser( props.getProperty("couchdb.admin.user") );
atlasProps.setCouchDbAdminPassword( props.getProperty("couchdb.admin.password") );


// CouchDb password
try {
String couchDbPass = props.getProperty("couchdb.admin.password");
if(null == couchDbPass) {
throw new Exception("Couchdb password not set. Run config command");
}
atlasProps.setCouchDbAdminPassword(couchDbPass);
} catch(Exception e) {
throw new Exception("Unable to interpret couchdb password",e);
}

// CouchDb URL
try {
String urlStr = props.getProperty("couchdb.url");
Expand All @@ -40,7 +55,7 @@ static public AtlasProperties fromProperties(Properties props) throws Exception
} catch(Exception e) {
throw new Exception("Unable to decode CouchDB URL",e);
}

// Server port
try {
String portString = props.getProperty("servlet.url.port");
Expand Down Expand Up @@ -72,7 +87,7 @@ static public AtlasProperties fromProperties(Properties props) throws Exception
} catch(Exception e) {
throw new Exception("Unable to interpret server key",e);
}

// Submission DB enabled
{
String enabledString = props.getProperty("couchdb.submission.enabled","false");
Expand All @@ -96,62 +111,43 @@ static public AtlasProperties fromProperties(Properties props) throws Exception
String key = props.getProperty("google.mapapi.key","");
atlasProps.setGoogleMapApiKey(key);
}

return atlasProps;
}

static public void readProperties(File atlasDir, Properties props) throws Exception {
// install.properties
{
File installPropFile = new File(atlasDir,"config/install.properties");
if( installPropFile.exists() && installPropFile.isFile() ){
FileInputStream fis = null;
try {
fis = new FileInputStream(installPropFile);
InputStreamReader reader = new InputStreamReader(fis,"UTF-8");
props.load(reader);

} catch(Exception e) {
throw new Exception("Unable to read config properties from: "+installPropFile.getAbsolutePath(), e);

} finally {
if( null != fis ){
try{
fis.close();
} catch(Exception e) {
// Ignore
}
}
}
}
}
File installPropFile = new File(atlasDir,"config/install.properties");
readConfigFile(installPropFile, props);

// sensitive.properties
{
File sensitivePropFile = new File(atlasDir,"config/sensitive.properties");
if( sensitivePropFile.exists() && sensitivePropFile.isFile() ){
FileInputStream fis = null;
File sensitivePropFile = new File(atlasDir,"config/sensitive.properties");
readConfigFile(sensitivePropFile, props);
}

static public void readConfigFile(File configFile, Properties props) throws Exception {
CommandScenario commandToExecute = CommandScenario.getInstance();
FileInputStream fis = null;
try {
fis = new FileInputStream(configFile);
InputStreamReader reader = new InputStreamReader(fis,"UTF-8");
props.load(reader);
} catch(Exception e) {
if(!commandToExecute.getCommand().equals(CommandScenario.CONFIG_COMMAND)) {
logger.error("Unable to read config properties from: " + configFile.getAbsolutePath());
throw new Exception("Unable to read config properties from: " + configFile.getAbsolutePath(), e);
}
} finally {
if( null != fis ){
try {
fis = new FileInputStream(sensitivePropFile);
InputStreamReader reader = new InputStreamReader(fis,"UTF-8");
props.load(reader);

} catch(Exception e) {
throw new Exception("Unable to read config properties from: "+sensitivePropFile.getAbsolutePath(), e);

} finally {
if( null != fis ){
try{
fis.close();
} catch(Exception e) {
// Ignore
}
}
fis.close();
} catch (Exception e) {
// Ignore
}
}
}
}

static public void writeProperties(File atlasDir, Properties props) throws Exception {
// Create config directory, if needed
File configDir = new File(atlasDir,"config");
Expand All @@ -164,14 +160,14 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep
} catch(Exception e) {
throw new Exception("Unable to create config directory",e);
}

// Figure out which properties are saved in the sensitive file
Set<String> sensitivePropertyNames = new HashSet<String>();
{
sensitivePropertyNames.add("couchdb.admin.password");
sensitivePropertyNames.add("server.key");
sensitivePropertyNames.add("google.mapapi.key");

File sensitivePropFile = new File(atlasDir,"config/sensitive.properties");
if( sensitivePropFile.exists() && sensitivePropFile.isFile() ){
FileInputStream fis = null;
Expand All @@ -181,7 +177,7 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep
fis = new FileInputStream(sensitivePropFile);
InputStreamReader reader = new InputStreamReader(fis,"UTF-8");
sensitivePropsCopy.load(reader);

Enumeration<?> keyEnum = sensitivePropsCopy.propertyNames();
while( keyEnum.hasMoreElements() ){
Object keyObj = keyEnum.nextElement();
Expand All @@ -190,10 +186,10 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep
sensitivePropertyNames.add(key);
}
}

} catch(Exception e) {
// Just ignore

} finally {
if( null != fis ){
try{
Expand All @@ -205,11 +201,11 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep
}
}
}

// Divide public and sensitive properties
Properties publicProps = new Properties();
Properties sensitiveProps = new Properties();

Enumeration<?> namesEnum = props.propertyNames();
while( namesEnum.hasMoreElements() ){
Object keyObj = namesEnum.nextElement();
Expand All @@ -223,7 +219,7 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep
}
}
}

// Write public file
{
File installPropFile = new File(configDir,"install.properties");
Expand All @@ -233,12 +229,12 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
PropertiesWriter propWriter = new PropertiesWriter(osw);
propWriter.write(publicProps);

osw.flush();

} catch(Exception e) {
throw new Exception("Unable to write config properties to: "+installPropFile.getAbsolutePath(), e);

} finally {
if( null != fos ){
try{
Expand All @@ -249,7 +245,7 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep
}
}
}

// Write sensitive file
{
File sensitivePropFile = new File(configDir,"sensitive.properties");
Expand All @@ -259,12 +255,12 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
PropertiesWriter propWriter = new PropertiesWriter(osw);
propWriter.write(sensitiveProps);

osw.flush();

} catch(Exception e) {
throw new Exception("Unable to write config properties to: "+sensitivePropFile.getAbsolutePath(), e);

} finally {
if( null != fos ){
try{
Expand All @@ -276,7 +272,7 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep
}
}
}

private String atlasName;
private URL couchDbUrl;
private String couchDbName;
Expand All @@ -296,7 +292,7 @@ public String getAtlasName() {
public void setAtlasName(String atlasName) {
this.atlasName = atlasName;
}

public URL getCouchDbUrl() {
return couchDbUrl;
}
Expand Down Expand Up @@ -345,7 +341,7 @@ public int getServerPort() {
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}

public boolean isRestricted() {
return restricted;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ca.carleton.gcrc.couch.command;

/**
* This class is being used to set/get the nunaliit command being executed globally.
* Commands scanario can be one of: update, run or config.
*
* This class is used in the following files:
* - Main.java
* - AtlasProperties.java
*/
public class CommandScanario {

private static CommandScanario instance;

public static final String CONFIG_COMMAND = "config";
public static final String UPDATE_COMMAND = "update";
public static final String RUN_COMMAND = "run";

private String command = "";

public static CommandScanario getInstance() {
if (instance == null) {
instance = new CommandScanario();
}

return instance;
}

public String getCommand() {
return this.command;
}

public void setCommand(String command) {
this.command = command;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ca.carleton.gcrc.couch.command;

/**
* This class is being used to set/get the nunaliit command being executed globally.
* Commands scenario can be one of: update, run or config.
*
* This class is used in the following files:
* - Main.java
* - AtlasProperties.java
*/
public class CommandScenario {

private static CommandScenario instance;

public static final String CONFIG_COMMAND = "config";
public static final String UPDATE_COMMAND = "update";
public static final String RUN_COMMAND = "run";

private String command = "";

public static CommandScenario getInstance() {
if (instance == null) {
instance = new CommandScenario();
}

return instance;
}

public String getCommand() {
return this.command;
}

public void setCommand(String command) {
this.command = command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,12 @@ public void execute(GlobalSettings globalSettings, List<String> args) throws Exc
if( arguments.size() < 1 ){
throw new Exception("No command provided. Try 'help'.");
}
CommandScenario cmdScenario = CommandScenario.getInstance();
String commandKeyword = arguments.get(0);
for(Command command : getCommands()){
if( command.matchesKeyword(commandKeyword) ) {
// Found the command in question

cmdScenario.setCommand(commandKeyword);
// Check options for this command
String[] expectedOptions = command.getExpectedOptions();
options.validateExpectedOptions(expectedOptions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
couchdb.admin.password=
google.mapapi.key=
server.key=

0 comments on commit 6cdb063

Please sign in to comment.