Skip to content

Commit

Permalink
allow for secure password prompting
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrez committed Jul 11, 2022
1 parent 1c66cd7 commit 3a1300e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
31 changes: 27 additions & 4 deletions src/main/java/com/ibm/jesseg/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.json.simple.parser.ParseException;

import com.github.theprez.jcmdutils.AppLogger;
import com.github.theprez.jcmdutils.ConsoleQuestionAsker;
import com.github.theprez.jcmdutils.StringUtils;

public class Config {
Expand All @@ -21,6 +22,9 @@ public class Config {
private final JSONObject m_json;
private AppLogger m_logger;
private File m_file;
private String m_password;
private String m_hostname;
private String m_username;

public static class SQLQuery {
public final String m_sql;
Expand Down Expand Up @@ -76,18 +80,37 @@ public List<SQLQuery> getSQLQueries() {
}

public String getHostName() {
if (StringUtils.isNonEmpty(m_hostname)) {
return m_hostname;
}
Object val = m_json.get("hostname");
return null == val ? null : val.toString();
if (null == val) {
return m_hostname = ConsoleQuestionAsker.get().askNonEmptyStringQuestion(m_logger, "", "Enter system name: ");
}
return m_hostname = val.toString();
}

public String getPassword() {
public String getPassword() throws IOException {
if (StringUtils.isNonEmpty(m_password)) {
return m_password;
}
Object val = m_json.get("password");
return null == val ? null : val.toString();
if (null == val) {
return m_password = ConsoleQuestionAsker.get().askUserForPwd("Password: ");
}
m_logger.printfln_warn("WARNING: Password is stored in config file %s. THIS IS NOT SECURE!", m_file.getAbsolutePath());
return m_password = val.toString();
}

public String getUsername() {
if (StringUtils.isNonEmpty(m_username)) {
return m_username;
}
Object val = m_json.get("username");
return null == val ? null : val.toString();
if (null == val) {
return m_username = ConsoleQuestionAsker.get().askNonEmptyStringQuestion(m_logger, "", "Username:");
}
return m_username = val.toString();
}

public int getPort(AppLogger _logger) {
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/com/ibm/jesseg/SQLMetricPopulator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ibm.jesseg;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
Expand All @@ -14,6 +15,7 @@

import com.github.theprez.jcmdutils.AppLogger;
import com.github.theprez.jcmdutils.StringUtils;
import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400JDBCDataSource;

import io.prometheus.client.CollectorRegistry;
Expand Down Expand Up @@ -42,13 +44,14 @@ public SQLMetricPopulator(AppLogger _logger, CollectorRegistry _registry, Config
m_logger = _logger;
m_sql = _sql;
m_interval = _interval;
final AS400 as400;
if (isIBMi()) {
m_datasource = new AS400JDBCDataSource("localhost", "*CURRENT", "*CURRENT");
as400 = new AS400("localhost", "*CURRENT", "*CURRENT");
String localhost = "unknown";
try {
localhost = InetAddress.getLocalHost().getHostName().replaceAll("\\..*", "");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (Exception e) {
m_logger.printfln_warn("WARNING: could not resolve local host name (%s)", e.getLocalizedMessage());
}
m_systemName = localhost;
} else {
Expand All @@ -64,9 +67,17 @@ public SQLMetricPopulator(AppLogger _logger, CollectorRegistry _registry, Config
if (StringUtils.isEmpty(password)) {
throw new IOException("password is required");
}
m_datasource = new AS400JDBCDataSource(hostname, username, password);
as400 = new AS400(hostname, username, password);

m_systemName = hostname;
}
try {
as400.setGuiAvailable(false);
} catch (PropertyVetoException e1) {
m_logger.printExceptionStack_verbose(e1);
}
m_datasource = new AS400JDBCDataSource(as400);

m_sqlThread = new Thread(() -> {
while (true) {
try {
Expand Down

0 comments on commit 3a1300e

Please sign in to comment.