From 0b7c42e81459543be3b789ab9047f585f4fbc4ab Mon Sep 17 00:00:00 2001 From: Ben Grabham Date: Sun, 20 Feb 2022 09:48:37 -0500 Subject: [PATCH] Check if environment variables are not empty strings as well as not null (#623) --- .../src/main/java/com/sismics/docs/core/dao/UserDao.java | 3 ++- .../com/sismics/docs/core/model/context/AppContext.java | 5 +++-- docs-core/src/main/java/com/sismics/util/EmailUtil.java | 6 +++--- docs-core/src/main/java/com/sismics/util/jpa/EMF.java | 3 ++- .../test/java/com/sismics/docs/rest/BaseJerseyTest.java | 3 ++- .../java/com/sismics/docs/rest/resource/AppResource.java | 8 ++++---- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/docs-core/src/main/java/com/sismics/docs/core/dao/UserDao.java b/docs-core/src/main/java/com/sismics/docs/core/dao/UserDao.java index 074a6c7c..4a2a18d8 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/dao/UserDao.java +++ b/docs-core/src/main/java/com/sismics/docs/core/dao/UserDao.java @@ -1,6 +1,7 @@ package com.sismics.docs.core.dao; import com.google.common.base.Joiner; +import com.google.common.base.Strings; import at.favre.lib.crypto.bcrypt.BCrypt; import org.joda.time.DateTime; import org.slf4j.Logger; @@ -289,7 +290,7 @@ public class UserDao { private String hashPassword(String password) { int bcryptWork = Constants.DEFAULT_BCRYPT_WORK; String envBcryptWork = System.getenv(Constants.BCRYPT_WORK_ENV); - if (envBcryptWork != null) { + if (!Strings.isNullOrEmpty(envBcryptWork)) { try { int envBcryptWorkInt = Integer.parseInt(envBcryptWork); if (envBcryptWorkInt >= 4 && envBcryptWorkInt <= 31) { diff --git a/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java b/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java index 0d7ec204..ed8b9095 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java +++ b/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java @@ -1,5 +1,6 @@ package com.sismics.docs.core.model.context; +import com.google.common.base.Strings; import com.google.common.collect.Lists; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.EventBus; @@ -106,7 +107,7 @@ public class AppContext { // Change the admin password if needed String envAdminPassword = System.getenv(Constants.ADMIN_PASSWORD_INIT_ENV); - if (envAdminPassword != null) { + if (!Strings.isNullOrEmpty(envAdminPassword)) { UserDao userDao = new UserDao(); User adminUser = userDao.getById("admin"); if (Constants.DEFAULT_ADMIN_PASSWORD.equals(adminUser.getPassword())) { @@ -117,7 +118,7 @@ public class AppContext { // Change the admin email if needed String envAdminEmail = System.getenv(Constants.ADMIN_EMAIL_INIT_ENV); - if (envAdminEmail != null) { + if (!Strings.isNullOrEmpty(envAdminEmail)) { UserDao userDao = new UserDao(); User adminUser = userDao.getById("admin"); if (Constants.DEFAULT_ADMIN_EMAIL.equals(adminUser.getEmail())) { diff --git a/docs-core/src/main/java/com/sismics/util/EmailUtil.java b/docs-core/src/main/java/com/sismics/util/EmailUtil.java index c5bd38bc..a2bccfb8 100644 --- a/docs-core/src/main/java/com/sismics/util/EmailUtil.java +++ b/docs-core/src/main/java/com/sismics/util/EmailUtil.java @@ -92,7 +92,7 @@ public class EmailUtil { // Hostname String envHostname = System.getenv(Constants.SMTP_HOSTNAME_ENV); - if (envHostname == null) { + if (Strings.isNullOrEmpty(envHostname)) { email.setHostName(ConfigUtil.getConfigStringValue(ConfigType.SMTP_HOSTNAME)); } else { email.setHostName(envHostname); @@ -101,7 +101,7 @@ public class EmailUtil { // Port int port = ConfigUtil.getConfigIntegerValue(ConfigType.SMTP_PORT); String envPort = System.getenv(Constants.SMTP_PORT_ENV); - if (envPort != null) { + if (!Strings.isNullOrEmpty(envPort)) { port = Integer.valueOf(envPort); } email.setSmtpPort(port); @@ -114,7 +114,7 @@ public class EmailUtil { // Username and password String envUsername = System.getenv(Constants.SMTP_USERNAME_ENV); String envPassword = System.getenv(Constants.SMTP_PASSWORD_ENV); - if (envUsername == null || envPassword == null) { + if (Strings.isNullOrEmpty(envUsername) || Strings.isNullOrEmpty(envPassword)) { Config usernameConfig = configDao.getById(ConfigType.SMTP_USERNAME); Config passwordConfig = configDao.getById(ConfigType.SMTP_PASSWORD); if (usernameConfig != null && passwordConfig != null) { diff --git a/docs-core/src/main/java/com/sismics/util/jpa/EMF.java b/docs-core/src/main/java/com/sismics/util/jpa/EMF.java index 7c3cf7bc..91e788d0 100644 --- a/docs-core/src/main/java/com/sismics/util/jpa/EMF.java +++ b/docs-core/src/main/java/com/sismics/util/jpa/EMF.java @@ -1,5 +1,6 @@ package com.sismics.util.jpa; +import com.google.common.base.Strings; import com.sismics.docs.core.util.DirectoryUtil; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.internal.util.config.ConfigurationHelper; @@ -83,7 +84,7 @@ public final class EMF { Map props = new HashMap<>(); Path dbDirectory = DirectoryUtil.getDbDirectory(); String dbFile = dbDirectory.resolve("docs").toAbsolutePath().toString(); - if (databaseUrl == null) { + if (Strings.isNullOrEmpty(databaseUrl)) { props.put("hibernate.connection.driver_class", "org.h2.Driver"); props.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect"); props.put("hibernate.connection.url", "jdbc:h2:file:" + dbFile + ";CACHE_SIZE=65536;LOCK_TIMEOUT=10000"); diff --git a/docs-web-common/src/test/java/com/sismics/docs/rest/BaseJerseyTest.java b/docs-web-common/src/test/java/com/sismics/docs/rest/BaseJerseyTest.java index cce2f744..1b764e73 100644 --- a/docs-web-common/src/test/java/com/sismics/docs/rest/BaseJerseyTest.java +++ b/docs-web-common/src/test/java/com/sismics/docs/rest/BaseJerseyTest.java @@ -26,6 +26,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URI; import java.util.List; +import java.util.Objects; /** * Base class of integration tests with Jersey. @@ -56,7 +57,7 @@ public abstract class BaseJerseyTest extends JerseyTest { @Override protected Application configure() { String travisEnv = System.getenv("TRAVIS"); - if (travisEnv == null || !travisEnv.equals("true")) { + if (!Objects.equals(travisEnv, "true")) { // Travis doesn't like big logs enable(TestProperties.LOG_TRAFFIC); enable(TestProperties.DUMP_ENTITY); diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java index c1dba249..e697997e 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java @@ -205,28 +205,28 @@ public class AppResource extends BaseResource { Config passwordConfig = configDao.getById(ConfigType.SMTP_PASSWORD); Config fromConfig = configDao.getById(ConfigType.SMTP_FROM); JsonObjectBuilder response = Json.createObjectBuilder(); - if (System.getenv(Constants.SMTP_HOSTNAME_ENV) == null) { + if (Strings.isNullOrEmpty(System.getenv(Constants.SMTP_HOSTNAME_ENV))) { if (hostnameConfig == null) { response.addNull("hostname"); } else { response.add("hostname", hostnameConfig.getValue()); } } - if (System.getenv(Constants.SMTP_PORT_ENV) == null) { + if (Strings.isNullOrEmpty(System.getenv(Constants.SMTP_PORT_ENV))) { if (portConfig == null) { response.addNull("port"); } else { response.add("port", Integer.valueOf(portConfig.getValue())); } } - if (System.getenv(Constants.SMTP_USERNAME_ENV) == null) { + if (Strings.isNullOrEmpty(System.getenv(Constants.SMTP_USERNAME_ENV))) { if (usernameConfig == null) { response.addNull("username"); } else { response.add("username", usernameConfig.getValue()); } } - if (System.getenv(Constants.SMTP_PASSWORD_ENV) == null) { + if (Strings.isNullOrEmpty(System.getenv(Constants.SMTP_PASSWORD_ENV))) { if (passwordConfig == null) { response.addNull("password"); } else {