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

Fix microservices proxy detection #7106

Merged
merged 1 commit into from
May 25, 2023
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
14 changes: 4 additions & 10 deletions services/src/main/java/org/fao/geonet/api/site/SiteApi.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2021 Food and Agriculture Organization of the
* Copyright (C) 2001-2023 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand Down Expand Up @@ -32,7 +32,7 @@
import jeeves.server.JeevesProxyInfo;
import jeeves.server.UserSession;
import jeeves.server.context.ServiceContext;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
Expand Down Expand Up @@ -235,17 +235,11 @@ public SettingsListResponse getSiteOrPortalDescription(
}

// Setting for OGC API Records service enabled
String microservicesTargetUri = "";
ServletRegistration microServicesProxyServlet =
request.getServletContext().getServletRegistrations().get("MicroServicesProxy");

if (microServicesProxyServlet != null) {
microservicesTargetUri = microServicesProxyServlet.getInitParameter("targetUri");
}
String microservicesTargetUri = (String) request.getServletContext().getAttribute("MicroServicesProxy.targetUri");

response.getSettings().add(
new Setting().setName(Settings.MICROSERVICES_ENABLED)
.setValue(Boolean.toString(StringUtils.isNotEmpty(microservicesTargetUri)))
.setValue(Boolean.toString(StringUtils.isNotBlank(microservicesTargetUri)))
.setDataType(SettingDataType.BOOLEAN));

return response;
Expand Down
15 changes: 4 additions & 11 deletions services/src/main/java/org/fao/geonet/guiservices/util/Env.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//=============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== Copyright (C) 2001-2023 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
Expand All @@ -26,7 +26,7 @@
import jeeves.interfaces.Service;
import jeeves.server.ServiceConfig;
import jeeves.server.context.ServiceContext;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.fao.geonet.GeonetContext;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.kernel.XmlSerializer;
Expand Down Expand Up @@ -71,17 +71,10 @@ public Element exec(Element params, ServiceContext context) throws Exception {
}

// Setting for OGC API Records service enabled
String microservicesTargetUri = "";

ServletRegistration microServicesProxyServlet =
context.getServlet().getServletContext().getServletRegistrations().get("MicroServicesProxy");

if (microServicesProxyServlet != null) {
microservicesTargetUri = microServicesProxyServlet.getInitParameter("targetUri");
}
String microservicesTargetUri = (String) context.getServlet().getServletContext().getAttribute("MicroServicesProxy.targetUri");

Element microservicesEnabled = new Element("microservicesEnabled");
microservicesEnabled.setText(Boolean.toString(StringUtils.isNotEmpty(microservicesTargetUri)));
microservicesEnabled.setText(Boolean.toString(StringUtils.isNotBlank(microservicesTargetUri)));
system.addContent(microservicesEnabled);

return (Element) system.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
</a>
</li>


<!--metadata workflow assistance external link(s)-->
<li
role="menuitem"
Expand Down
81 changes: 39 additions & 42 deletions web/src/main/java/org/fao/geonet/proxy/URITemplateProxyServlet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2022 Food and Agriculture Organization of the
* Copyright (C) 2001-2023 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand Down Expand Up @@ -67,46 +67,15 @@
* @author delawen
*/
public class URITemplateProxyServlet extends org.mitre.dsmiley.httpproxy.URITemplateProxyServlet {
public static final String P_FORWARDEDHOST = "forwardHost";
public static final String P_FORWARDEDHOSTPREFIXPATH = "forwardHostPrefixPath";
private static final Logger LOGGER = Log.createLogger("URITemplateProxyServlet");

private static final long serialVersionUID = 4847856943273604410L;
private static final String P_SECURITY_MODE = "securityMode";
public static final String P_FORWARDEDHOST = "forwardHost";
public static final String P_FORWARDEDHOSTPREFIXPATH = "forwardHostPrefixPath";
private static final String P_IS_SECURED = "isSecured";

private static final String TARGET_URI_NAME = "targetUri";

protected boolean doForwardHost = false;
protected String doForwardHostPrefixPath = "";

protected boolean isSecured = false;

private String username;

private String password;

private enum SECURITY_MODE {
NONE,
/**
* Check if the host of the requested URL is registered in
* at least one analyzed link in a metadata record.
*/
DB_LINK_CHECK;

public static SECURITY_MODE parse(String value) {
if ("DB_LINK_CHECK".equals(value)) {
return DB_LINK_CHECK;
}
return NONE;
}
}

protected SECURITY_MODE securityMode;

@Autowired
MetadataLinkRepository metadataLinkRepository;

/*
* These are the "hop-by-hop" headers that should not be copied.
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html Overriding
Expand All @@ -125,6 +94,15 @@ public static SECURITY_MODE parse(String value) {
}
}

protected boolean doForwardHost = false;
protected String doForwardHostPrefixPath = "";
protected boolean isSecured = false;
protected SECURITY_MODE securityMode;
@Autowired
MetadataLinkRepository metadataLinkRepository;
private String username;
private String password;

/**
* Init some properties from the servlet's init parameters. They try to be resolved the same way other GeoNetwork
* configuration properties are resolved. If after checking externally no configuration can be found it relies into
Expand All @@ -138,13 +116,14 @@ public static SECURITY_MODE parse(String value) {
* for example {@code mywebapp.MicroservicesProxy.targetUri}</li>
* <li> {@code GEONETWORK_${SERVLET_NAME}_TARGETURI}: Look for an environment variable starting by GEONETWORK, for example
* {@code GEONETWORK_MICROSERVICESPROXY_TARGETURI}</li>
* <li> {@code geonetwork.${SERVLET_NAME}.targetUri}: Look for a property in {@code>config.properties} starting by geonetwork,
* <li> {@code geonetwork.${SERVLET_NAME}.targetUri}: Look for a property in {@code config.properties} starting by geonetwork,
* for example {@code geonetwork.MicroservicesProxy.targetUri}</li>
* <li> {@code geonetwork.${SERVLET_NAME}.targetUri}: Look for a property in {@code config.properties} starting by geonetwork,
* for example {@code geonetwork.MicroservicesProxy.targetUri}</li>
* </ol>
* <p>
* Finally, it checks the value of {@code targetUri} in web.xml if no external config has been found.
* It stores the targetUri value found as an attribute in the ServletContext with the name $SERVLET_NAME.targetUri.
*
* @throws ServletException if the targetUri is not defined externally and the parameter is not even in web.xml.
*/
Expand All @@ -171,6 +150,8 @@ protected void initTarget() throws ServletException {
throw new ServletException(P_TARGET_URI + " is required in web.xml or set externally");
}

this.getServletContext().setAttribute(this.getServletName() + "." + P_TARGET_URI, targetUriTemplate);

this.username = getConfigValue("username");
this.password = getConfigValue("password");
if (StringUtils.isBlank(this.username)) {
Expand All @@ -185,29 +166,29 @@ protected void initTarget() throws ServletException {

}

private String getConfigValue(String sufix) {
private String getConfigValue(String suffix) {
String result;

// Property defined according to webapp name
ServletPathFinder pathFinder = new ServletPathFinder(getServletContext());
String baseUrl = pathFinder.getBaseUrl();
String webappName = "";
if (org.apache.commons.lang.StringUtils.isNotEmpty(baseUrl)) {
if (StringUtils.isNotEmpty(baseUrl)) {
webappName = baseUrl.substring(1);
}
LOGGER.info(
"Looking for " + webappName + "." + getServletName() + "." + sufix + " in Environment variables, " +
"Looking for " + webappName + "." + getServletName() + "." + suffix + " in Environment variables, " +
"System properties and config.properties entries");
result = resolveConfigValue(webappName + "." + getServletName() + "." + sufix);
result = resolveConfigValue(webappName + "." + getServletName() + "." + suffix);


if (StringUtils.isBlank(result)) {
// GEONETWORK is the default prefix

LOGGER.info(
"Looking for geonetwork." + getServletName() + "." + sufix + " in Environment variables, " +
"Looking for geonetwork." + getServletName() + "." + suffix + " in Environment variables, " +
"System properties and config.properties entries");
result = resolveConfigValue("geonetwork." + getServletName() + "." + sufix);
result = resolveConfigValue("geonetwork." + getServletName() + "." + suffix);
}
return result;
}
Expand Down Expand Up @@ -255,7 +236,7 @@ protected HttpClient createHttpClient() {
clientBuilder.disableContentCompression();
}

if (org.apache.commons.lang.StringUtils.isNotEmpty(username) && org.apache.commons.lang.StringUtils.isNotEmpty(password)) {
if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
CredentialsProvider credentialsProvider =
new BasicCredentialsProvider();
credentialsProvider.setCredentials(
Expand Down Expand Up @@ -422,4 +403,20 @@ protected void service(HttpServletRequest servletRequest, HttpServletResponse se
break;
}
}

private enum SECURITY_MODE {
NONE,
/**
* Check if the host of the requested URL is registered in
* at least one analyzed link in a metadata record.
*/
DB_LINK_CHECK;

public static SECURITY_MODE parse(String value) {
if ("DB_LINK_CHECK".equals(value)) {
return DB_LINK_CHECK;
}
return NONE;
}
}
}
1 change: 1 addition & 0 deletions web/src/main/webResources/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@
<param-name>log</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MicroServicesProxy</servlet-name>
Expand Down