Skip to content

Commit

Permalink
fix #2804 Disable OSDD processing when SSO is active to enhance usabi…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
marevol committed Feb 8, 2024
1 parent 479eeab commit f1894f7
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 34 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/codelibs/fess/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public class Constants extends CoreLibConstants {

public static final String STOP = "stop";

public static final String AUTO = "auto";

public static final String NONE = "none";

public static final String ITEM_LABEL = "label";

public static final String ITEM_VALUE = "value";
Expand Down Expand Up @@ -158,6 +162,8 @@ public class Constants extends CoreLibConstants {

public static final String LTR_WINDOW_SIZE_PROPERTY = "ltr.window.size";

public static final String SSO_TYPE_PROPERTY = "sso.type";

public static final String REQUEST_QUERIES = "fess.Queries";

public static final String HIGHLIGHT_QUERIES = "fess.HighlightQueries";
Expand Down
51 changes: 38 additions & 13 deletions src/main/java/org/codelibs/fess/helper/OsddHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.logging.log4j.Logger;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.fess.Constants;
import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.util.ComponentUtil;
import org.lastaflute.web.response.StreamResponse;
import org.lastaflute.web.util.LaServletContextUtil;
Expand Down Expand Up @@ -51,20 +52,44 @@ public void init() {
if (logger.isDebugEnabled()) {
logger.debug("Initialize {}", this.getClass().getSimpleName());
}
if (Constants.TRUE.equalsIgnoreCase(ComponentUtil.getFessConfig().getOsddLinkEnabled())) {
if (StringUtil.isNotBlank(osddPath)) {
final String path = LaServletContextUtil.getServletContext().getRealPath(osddPath);
osddFile = new File(path);
if (!osddFile.isFile()) {
osddFile = null;
logger.warn("{} was not found.", path);
}
} else {
logger.info("OSDD file is not found.");
}
} else {
osddFile = getOsddFile();
}

protected File getOsddFile() {
if (!isOsddLinkEnabled()) {
logger.debug("OSDD is disabled.");
return null;
}
if (StringUtil.isBlank(osddPath)) {
logger.info("OSDD file is not found.");
return null;
}
final String path = LaServletContextUtil.getServletContext().getRealPath(osddPath);
if (path == null) {
logger.warn("{} was not found.", path);
return null;
}
final File osddFile = new File(path);
if (!osddFile.isFile()) {
logger.warn("{} was not a file.", path);
return null;
}
return osddFile;
}

protected boolean isOsddLinkEnabled() {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final String osddLinkEnabled = fessConfig.getOsddLinkEnabled();
if (Constants.TRUE.equalsIgnoreCase(osddLinkEnabled)) {
return true;
}

if (!Constants.AUTO.equalsIgnoreCase(osddLinkEnabled)) {
return false;
}

final String ssoType = fessConfig.getSsoType();
return StringUtil.isBlank(ssoType) || Constants.NONE.equalsIgnoreCase(ssoType);
}

public boolean hasOpenSearchFile() {
Expand All @@ -73,7 +98,7 @@ public boolean hasOpenSearchFile() {

public StreamResponse asStream() {
if (osddFile == null) {
throw ComponentUtil.getResponseManager().new404("Unsupported OpenSearch response.");
throw ComponentUtil.getResponseManager().new404("Unsupported Open Search Description Document response.");
}

return new StreamResponse(osddFile.getName()).contentType(contentType + "; charset=" + encoding).stream(out -> {
Expand Down
17 changes: 3 additions & 14 deletions src/main/java/org/codelibs/fess/mylasta/direction/FessConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
/** The key of the configuration. e.g. __TEMPLATE__ */
String FORM_ADMIN_DEFAULT_TEMPLATE_NAME = "form.admin.default.template.name";

/** The key of the configuration. e.g. true */
/** The key of the configuration. e.g. auto */
String OSDD_LINK_ENABLED = "osdd.link.enabled";

/** The key of the configuration. e.g. true */
Expand Down Expand Up @@ -5569,18 +5569,11 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction

/**
* Get the value for the key 'osdd.link.enabled'. <br>
* The value is, e.g. true <br>
* The value is, e.g. auto <br>
* @return The value of found property. (NotNull: if not found, exception but basically no way)
*/
String getOsddLinkEnabled();

/**
* Is the property for the key 'osdd.link.enabled' true? <br>
* The value is, e.g. true <br>
* @return The determination, true or false. (if not found, exception but basically no way)
*/
boolean isOsddLinkEnabled();

/**
* Get the value for the key 'clipboard.copy.icon.enabled'. <br>
* The value is, e.g. true <br>
Expand Down Expand Up @@ -9640,10 +9633,6 @@ public String getOsddLinkEnabled() {
return get(FessConfig.OSDD_LINK_ENABLED);
}

public boolean isOsddLinkEnabled() {
return is(FessConfig.OSDD_LINK_ENABLED);
}

public String getClipboardCopyIconEnabled() {
return get(FessConfig.CLIPBOARD_COPY_ICON_ENABLED);
}
Expand Down Expand Up @@ -11147,7 +11136,7 @@ protected java.util.Map<String, String> prepareGeneratedDefaultMap() {
defaultMap.put(FessConfig.FORM_ADMIN_MAX_INPUT_SIZE, "10000");
defaultMap.put(FessConfig.FORM_ADMIN_LABEL_IN_CONFIG_ENABLED, "false");
defaultMap.put(FessConfig.FORM_ADMIN_DEFAULT_TEMPLATE_NAME, "__TEMPLATE__");
defaultMap.put(FessConfig.OSDD_LINK_ENABLED, "true");
defaultMap.put(FessConfig.OSDD_LINK_ENABLED, "auto");
defaultMap.put(FessConfig.CLIPBOARD_COPY_ICON_ENABLED, "true");
defaultMap.put(FessConfig.AUTHENTICATION_ADMIN_USERS, "admin");
defaultMap.put(FessConfig.AUTHENTICATION_ADMIN_ROLES, "admin");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,10 @@ default boolean isAzureAdUseDomainServices() {
return Constants.TRUE.equalsIgnoreCase(getSystemProperty("aad.use.ds", "true"));
}

default String getSsoType() {
return getSystemProperty(Constants.SSO_TYPE_PROPERTY, Constants.NONE);
}

//
// fess_*.properties
//
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/codelibs/fess/sso/SsoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.codelibs.fess.Constants;
import org.codelibs.fess.mylasta.action.FessUserBean;
import org.codelibs.fess.util.ComponentUtil;
import org.lastaflute.web.login.credential.LoginCredential;
Expand All @@ -28,18 +29,14 @@
public class SsoManager {
private static final Logger logger = LogManager.getLogger(SsoManager.class);

protected static final String SSO_TYPE = "sso.type";

protected static final String NONE = "none";

protected final List<SsoAuthenticator> authenticatorList = new ArrayList<>();

public boolean available() {
final String ssoType = getSsoType();
if (logger.isDebugEnabled()) {
logger.debug("sso.type: {}", ssoType);
}
return !NONE.equals(ssoType);
return !Constants.NONE.equals(ssoType);
}

public LoginCredential getLoginCredential() {
Expand Down Expand Up @@ -81,7 +78,7 @@ protected SsoAuthenticator getAuthenticator() {
}

protected String getSsoType() {
return ComponentUtil.getFessConfig().getSystemProperty(SSO_TYPE, NONE);
return ComponentUtil.getFessConfig().getSsoType();
}

public SsoAuthenticator[] getAuthenticators() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fess_config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ logging.app.packages=org.codelibs,org.dbflute,org.lastaflute
form.admin.max.input.size=10000
form.admin.label.in.config.enabled=false
form.admin.default.template.name=__TEMPLATE__
osdd.link.enabled=true
osdd.link.enabled=auto
clipboard.copy.icon.enabled=true

# ----------------------------------------------------------
Expand Down
169 changes: 169 additions & 0 deletions src/test/java/org/codelibs/fess/helper/OsddHelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* Copyright 2012-2023 CodeLibs Project and the Others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.codelibs.fess.helper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.codelibs.core.io.InputStreamUtil;
import org.codelibs.fess.Constants;
import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.unit.UnitFessTestCase;
import org.codelibs.fess.util.ComponentUtil;
import org.lastaflute.web.response.StreamResponse;
import org.lastaflute.web.servlet.request.stream.WrittenStreamOut;

public class OsddHelperTest extends UnitFessTestCase {

public void test_init_nofile() {
ComponentUtil.setFessConfig(new FessConfig.SimpleImpl() {
@Override
public String getOsddLinkEnabled() {
return "auto";
}

@Override
public String getSsoType() {
return "none";
}
});
final OsddHelper osddHelper = new OsddHelper();
osddHelper.setContentType("application/opensearchdescription+xml");
osddHelper.init();
assertFalse(osddHelper.hasOpenSearchFile());

try {
osddHelper.asStream();
fail();
} catch (final Exception e) {
assertEquals("Unsupported Open Search Description Document response.", e.getMessage());
}
}

public void test_init_osddpath() throws IOException {
ComponentUtil.setFessConfig(new FessConfig.SimpleImpl() {
@Override
public String getOsddLinkEnabled() {
return "auto";
}

@Override
public String getSsoType() {
return "none";
}
});
final OsddHelper osddHelper = new OsddHelper();
osddHelper.setOsddPath("osdd/osdd.xml");
osddHelper.setEncoding(Constants.UTF_8);
osddHelper.init();
assertTrue(osddHelper.hasOpenSearchFile());

final StreamResponse streamResponse = osddHelper.asStream();
assertEquals("text/xml; charset=UTF-8", streamResponse.getContentType());
streamResponse.getStreamCall().callback(new WrittenStreamOut() {

@Override
public void write(final InputStream ins) throws IOException {
assertEquals("""
<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Fess</ShortName>
<Description>Full Text Search for Your Documents.</Description>
<Tags>Full Text Search</Tags>
<Contact>[email protected]</Contact>
<SearchForm>http://localhost:8080/fess/</SearchForm>
<Url type="text/html" template="http://localhost:8080/fess/search?q={searchTerms}"/>
<InputEncoding>UTF-8</InputEncoding>
<OutputEncoding>UTF-8</OutputEncoding>
</OpenSearchDescription>
""", new String(InputStreamUtil.getBytes(ins)));
}

@Override
public OutputStream stream() {
return null;
}
});
}

public void test_init_osddpath_null() {
ComponentUtil.setFessConfig(new FessConfig.SimpleImpl() {
@Override
public String getOsddLinkEnabled() {
return "auto";
}

@Override
public String getSsoType() {
return "none";
}
});
final OsddHelper osddHelper = new OsddHelper();
osddHelper.setOsddPath("osdd/none.xml");
osddHelper.init();
assertFalse(osddHelper.hasOpenSearchFile());
}

public void test_init_disabled() {
ComponentUtil.setFessConfig(new FessConfig.SimpleImpl() {
@Override
public String getOsddLinkEnabled() {
return "false";
}
});
final OsddHelper osddHelper = new OsddHelper();
osddHelper.setOsddPath("osdd/osdd.xml");
osddHelper.init();
assertFalse(osddHelper.hasOpenSearchFile());
}

public void test_init_saml() {
ComponentUtil.setFessConfig(new FessConfig.SimpleImpl() {
@Override
public String getOsddLinkEnabled() {
return "auto";
}

@Override
public String getSsoType() {
return "saml";
}
});
final OsddHelper osddHelper = new OsddHelper();
osddHelper.setOsddPath("osdd/osdd.xml");
osddHelper.init();
assertFalse(osddHelper.hasOpenSearchFile());
}

public void test_init_force() throws IOException {
ComponentUtil.setFessConfig(new FessConfig.SimpleImpl() {
@Override
public String getOsddLinkEnabled() {
return "true";
}

@Override
public String getSsoType() {
return "saml";
}
});
final OsddHelper osddHelper = new OsddHelper();
osddHelper.setOsddPath("osdd/osdd.xml");
osddHelper.init();
assertTrue(osddHelper.hasOpenSearchFile());
}
}

0 comments on commit f1894f7

Please sign in to comment.