Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public AppEngineWebAppContext(File appDir, String serverInfo) {

// Configure the Jetty SecurityHandler to understand our method of
// authentication (via the UserService).
EE10AppEngineAuthentication.configureSecurityHandler(
(ConstraintSecurityHandler) getSecurityHandler());
setSecurityHandler(EE10AppEngineAuthentication.newSecurityHandler());

setMaxFormContentSize(MAX_RESPONSE_SIZE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.apphosting.utils.servlet.ee10.SnapshotServlet;
import com.google.apphosting.utils.servlet.ee10.WarmupServlet;
import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.GoogleLogger;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.Filter;
import jakarta.servlet.Servlet;
Expand Down Expand Up @@ -162,8 +163,7 @@ public AppEngineWebAppContext(File appDir, String serverInfo, boolean extractWar

// Configure the Jetty SecurityHandler to understand our method of
// authentication (via the UserService).
EE10AppEngineAuthentication.configureSecurityHandler(
(ConstraintSecurityHandler) getSecurityHandler());
setSecurityHandler(EE10AppEngineAuthentication.newSecurityHandler());

setMaxFormContentSize(MAX_RESPONSE_SIZE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public JavaRuntimeAllInOneTest(String version) {
System.setProperty("appengine.use.EE10", "false");
break;
case "EE10":
//TODO System.setProperty("appengine.use.EE8", "false");
//TODO System.setProperty("appengine.use.EE10", "true");
System.setProperty("appengine.use.EE8", "false");
System.setProperty("appengine.use.EE10", "true");
break;
default:
// fall through
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<servlet>
<servlet-name>main</servlet-name>
<servlet-class>com.google.apphosting.loadtesting.allinoneee10.MainServlet</servlet-class>
<servlet-class>com.google.apphosting.loadtesting.allinone.ee10.MainServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>warmup</servlet-name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Session;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.URIUtil;

/**
* {@code AppEngineAuthentication} is a utility class that can configure a Jetty {@link
Expand Down Expand Up @@ -84,7 +83,20 @@ public class EE10AppEngineAuthentication {
* Inject custom {@link LoginService} and {@link Authenticator} implementations into the specified
* {@link ConstraintSecurityHandler}.
*/
public static void configureSecurityHandler(ConstraintSecurityHandler handler) {
public static ConstraintSecurityHandler newSecurityHandler() {
ConstraintSecurityHandler handler = new ConstraintSecurityHandler()
{
@Override
protected Constraint getConstraint(String pathInContext, Request request) {
if (request.getAttribute(SKIP_ADMIN_CHECK_ATTR) != null) {
logger.atFine().log("Returning DeferredAuthentication because of SkipAdminCheck.");
// Warning: returning ALLOWED here will bypass security restrictions!
return Constraint.ALLOWED;
}

return super.getConstraint(pathInContext, request);
}
};

LoginService loginService = new AppEngineLoginService();
LoginAuthenticator authenticator = new AppEngineAuthenticator();
Expand All @@ -96,6 +108,7 @@ public static void configureSecurityHandler(ConstraintSecurityHandler handler) {
handler.setAuthenticator(authenticator);
handler.setIdentityService(identityService);
authenticator.setConfiguration(handler);
return handler;
}

/**
Expand All @@ -105,7 +118,7 @@ public static void configureSecurityHandler(ConstraintSecurityHandler handler) {
private static class AppEngineAuthenticator extends LoginAuthenticator {

/**
* Checks if the request could to to the login page.
* Checks if the request could go to the login page.
*
* @param uri The uri requested.
* @return True if the uri starts with "/_ah/", false otherwise.
Expand All @@ -124,6 +137,18 @@ public Constraint.Authorization getConstraintAuthentication(
String pathInContext,
Constraint.Authorization existing,
Function<Boolean, Session> getSession) {

// Check this before checking if there is a user logged in, so
// that we can log out properly. Specifically, watch out for
// the case where the user logs in, but as a role that isn't
// allowed to see /*. They should still be able to log out.
if (isLoginOrErrorPage(pathInContext)) {
logger.atFine().log(
"Got %s, returning DeferredAuthentication to imply authentication is in progress.",
pathInContext);
return Constraint.Authorization.ALLOWED;
}

return super.getConstraintAuthentication(pathInContext, existing, getSession);
}

Expand All @@ -137,55 +162,27 @@ public Constraint.Authorization getConstraintAuthentication(
*
* <p>From org.eclipse.jetty.server.Authentication:
*
* @param servletRequest The request
* @param servletResponse The response
* @param mandatory True if authentication is mandatory.
* @return An Authentication. If Authentication is successful, this will be a {@link
* Authentication.User}. If a response has been sent by the Authenticator (which can be done
* for both successful and unsuccessful authentications), then the result will implement
* {@link Authentication.ResponseSent}. If Authentication is not mandatory, then a {@link
* Authentication.Deferred} may be returned.
* @throws ServerAuthException
* @param req The request
* @param res The response
* @param cb The callback
* @throws ServerAuthException if an error occurred
*/
@Override
public AuthenticationState validateRequest(Request req, Response res, Callback cb)
throws ServerAuthException {

ServletContextRequest contextRequest = Request.as(req, ServletContextRequest.class);

HttpServletRequest request = contextRequest.getServletApiRequest();
HttpServletResponse response = contextRequest.getHttpServletResponse();

// Trusted inbound ip, auth headers can be trusted.

// Use the canonical path within the context for authentication and authorization
// as this is what is used to generate response content
String uri = URIUtil.addPaths(request.getServletPath(), request.getPathInfo());

if (uri == null) {
uri = "/";
}
// Check this before checking if there is a user logged in, so
// that we can log out properly. Specifically, watch out for
// the case where the user logs in, but as a role that isn't
// allowed to see /*. They should still be able to log out.
if (isLoginOrErrorPage(uri) && !AuthenticationState.Deferred.isDeferred(res)) {
logger.atFine().log(
"Got %s, returning DeferredAuthentication to imply authentication is in progress.",
uri);
return null;
if (response == null) {
throw new ServerAuthException("validateRequest called with null response!!!");
}

if (request.getAttribute(SKIP_ADMIN_CHECK_ATTR) != null) {
logger.atFine().log("Returning DeferredAuthentication because of SkipAdminCheck.");
// Warning: returning DeferredAuthentication here will bypass security restrictions!
if (AuthenticationState.Deferred.isDeferred(res)) {
return null;
}

if (response == null) {
throw new ServerAuthException("validateRequest called with null response!!!");
}

try {
UserService userService = UserServiceFactory.getUserService();
// If the user is authenticated already, just create a
Expand All @@ -198,10 +195,6 @@ public AuthenticationState validateRequest(Request req, Response res, Callback c
}
}

if (AuthenticationState.Deferred.isDeferred(res)) {
return null;
}

try {
logger.atFine().log(
"Got %s but no one was logged in, redirecting.", request.getRequestURI());
Expand Down