dbeaver/pro#6703 use local host address by default (#3695)

* dbeaver/pro#6703 use local host address by default

* dbeaver/pro#6703 rename method

---------

Co-authored-by: Daria Marutkina <125263541+dariamarutkina@users.noreply.github.com>
This commit is contained in:
Alexander Skoblikov
2025-08-27 12:20:18 +02:00
committed by GitHub
co-authored by Daria Marutkina
parent 3877e914c4
commit ac48e13039
6 changed files with 23 additions and 26 deletions
@@ -292,7 +292,7 @@ public class ServletAppUtils {
}
@NotNull
public static String getOriginFromRequestOrThrow(HttpServletRequest request) throws DBWebException {
public static String getOriginFromRequest(@NotNull HttpServletRequest request) {
String origin = request.getHeader(HEADER_ORIGIN);
if (CommonUtils.isEmpty(origin)) {
origin = request.getHeader(HEADER_X_ORIGIN);
@@ -103,7 +103,6 @@ public abstract class CBApplication<T extends CBServerConfig>
// Persistence
protected SMAdminController securityController;
private boolean configurationMode = false;
private String localHostAddress;
protected String containerId;
private final List<InetAddress> localInetAddresses = new ArrayList<>();
@@ -226,15 +225,6 @@ public abstract class CBApplication<T extends CBServerConfig>
eventController.setForceSkipEvents(isConfigurationMode()); // do not send events if configuration mode is on
// Determine address for local host
localHostAddress = System.getenv(CBConstants.VAR_CB_LOCAL_HOST_ADDR);
if (CommonUtils.isEmpty(localHostAddress)) {
localHostAddress = System.getProperty(CBConstants.VAR_CB_LOCAL_HOST_ADDR);
}
if (CommonUtils.isEmpty(localHostAddress) || CBConstants.HOST_127_0_0_1.equals(localHostAddress) || "::0".equals(
localHostAddress)) {
localHostAddress = CBConstants.HOST_LOCALHOST;
}
Location instanceLoc = Platform.getInstanceLocation();
try {
@@ -529,7 +519,7 @@ public abstract class CBApplication<T extends CBServerConfig>
}
public String getLocalHostAddress() {
return localHostAddress;
return getServerConfigurationController().getLocalHostAddress();
}
public List<InetAddress> getLocalInetAddresses() {
@@ -44,8 +44,6 @@ import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.IOUtils;
import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -66,6 +64,7 @@ public abstract class CBServerConfigurationController<T extends CBServerConfig>
protected final Path homeDirectory;
private final Map<String, String> externalProperties = new LinkedHashMap<>();
private final Map<String, Object> originalConfigurationProperties = new LinkedHashMap<>();
private String localHostAddress;
protected CBServerConfigurationController(@NotNull T serverConfiguration, @NotNull Path homeDirectory) {
super(homeDirectory);
@@ -80,6 +79,15 @@ public abstract class CBServerConfigurationController<T extends CBServerConfig>
@Override
public void loadServerConfiguration(Path configPath) throws DBException {
log.debug("Using configuration [" + configPath + "]");
// Determine address for local host
localHostAddress = System.getenv(CBConstants.VAR_CB_LOCAL_HOST_ADDR);
if (CommonUtils.isEmpty(localHostAddress)) {
localHostAddress = System.getProperty(CBConstants.VAR_CB_LOCAL_HOST_ADDR);
}
if (CommonUtils.isEmpty(localHostAddress) || CBConstants.HOST_127_0_0_1.equals(localHostAddress) || "::0".equals(
localHostAddress)) {
localHostAddress = CBConstants.HOST_LOCALHOST;
}
if (!Files.exists(configPath)) {
log.error("Configuration file " + configPath + " doesn't exist. Use defaults.");
@@ -162,12 +170,7 @@ public abstract class CBServerConfigurationController<T extends CBServerConfig>
if (config.getServerURL() == null) {
String hostName = config.getServerHost();
if (CommonUtils.isEmpty(hostName)) {
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
log.debug("Error resolving localhost address: " + e.getMessage());
hostName = CBConstants.HOST_LOCALHOST;
}
hostName = getLocalHostAddress();
}
config.setServerURL("http://" + hostName + ":" + config.getServerPort());
}
@@ -669,4 +672,8 @@ public abstract class CBServerConfigurationController<T extends CBServerConfig>
public void validateFinalServerConfiguration() throws DBException {
}
public String getLocalHostAddress() {
return localHostAddress;
}
}
@@ -67,7 +67,7 @@ public class RequestHostFilter implements Filter {
CBServerConfig serverConfig = application.getServerConfiguration();
URI originUri;
try {
String origin = ServletAppUtils.getOriginFromRequestOrThrow(httpRequest);
String origin = ServletAppUtils.getOriginFromRequest(httpRequest);
originUri = URI.create(origin);
} catch (Exception e) {
log.error("Failed to get origin from request", e);
@@ -494,7 +494,7 @@ public class WebServiceAdmin implements DBWServiceAdmin {
@NotNull WebSession webSession,
@Nullable String providerId
) throws DBWebException {
String origin = ServletAppUtils.getOriginFromRequestOrThrow(request);
String origin = ServletAppUtils.getOriginFromRequest(request);
List<WebAuthProviderConfiguration> result = new ArrayList<>();
for (SMAuthProviderCustomConfiguration cfg : CBApplication.getInstance().getAppConfiguration().getAuthCustomConfigurations()) {
if (providerId != null && !providerId.equals(cfg.getProvider())) {
@@ -544,7 +544,7 @@ public class WebServiceAdmin implements DBWServiceAdmin {
providerConfig.getProvider(),
webSession.getUserId()
));
return new WebAuthProviderConfiguration(authProvider, providerConfig, ServletAppUtils.getOriginFromRequestOrThrow(request));
return new WebAuthProviderConfiguration(authProvider, providerConfig, ServletAppUtils.getOriginFromRequest(request));
}
@Override
@@ -111,7 +111,7 @@ public class WebServiceAuthImpl implements DBWServiceAuth {
}
try {
Map<String, Object> authParameters = new HashMap<>();
authParameters.put(SMConstants.USER_ORIGIN, ServletAppUtils.getOriginFromRequestOrThrow(httpRequest));
authParameters.put(SMConstants.USER_ORIGIN, ServletAppUtils.getOriginFromRequest(httpRequest));
var smAuthInfo = initiateAuthentication(webSession, providerId, providerConfigurationId, authParameters, forceSessionsLogout);
if (smAuthInfo.getAuthStatus() != SMAuthStatus.IN_PROGRESS) {
@@ -227,7 +227,7 @@ public class WebServiceAuthImpl implements DBWServiceAuth {
List<WebAuthInfo> removedInfos = webSession.removeAuthInfo(providerId);
List<String> logoutUrls = new ArrayList<>();
var cbApp = CBApplication.getInstance();
String origin = ServletAppUtils.getOriginFromRequestOrThrow(httpRequest);
String origin = ServletAppUtils.getOriginFromRequest(httpRequest);
for (WebAuthInfo removedInfo : removedInfos) {
if (removedInfo.getAuthProviderDescriptor()
.getInstance() instanceof SMSignOutLinkProvider provider
@@ -297,7 +297,7 @@ public class WebServiceAuthImpl implements DBWServiceAuth {
@Override
public WebAuthProviderInfo[] getAuthProviders(@NotNull HttpServletRequest request) throws DBWebException {
String origin = ServletAppUtils.getOriginFromRequestOrThrow(request);
String origin = ServletAppUtils.getOriginFromRequest(request);
return WebAuthProviderRegistry.getInstance().getAuthProviders()
.stream()
.map(descriptor -> new WebAuthProviderInfo(descriptor, origin))