Skip to content

Commit

Permalink
Make v2 WebAnnotation and ACLFilter paths relatives
Browse files Browse the repository at this point in the history
All paths defined in WebAnnotation and ACLFilter for routing and
chacking the ACL are relatives to the servlet context
  • Loading branch information
fmarco76 committed Jul 18, 2024
1 parent f074823 commit 31a4a1b
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class AgentCertRequestServlet extends CAServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(AgentCertRequestServlet.class);

@WebAction(method = HttpMethod.GET, paths = {"/"})
@WebAction(method = HttpMethod.GET, paths = {""})
public void listRequests(HttpServletRequest request, HttpServletResponse response) throws Exception {
PrintWriter out = response.getWriter();
int maxTime = request.getParameter("maxTime") == null ?
Expand All @@ -88,7 +88,7 @@ public void listRequests(HttpServletRequest request, HttpServletResponse respons
throw new PKIException(message, e);
}
}
@WebAction(method = HttpMethod.GET, paths = {"/{}"})
@WebAction(method = HttpMethod.GET, paths = {"{}"})
public void reviewRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
RequestId id;
try {
Expand All @@ -111,8 +111,8 @@ public void reviewRequest(HttpServletRequest request, HttpServletResponse respon
}

@WebAction(method = HttpMethod.POST, paths = {
"/{}/approve", "/{}/reject", "/{}/cancel",
"/{}/update", "/{}/validate", "/{}/unassign", "/{}/assign"})
"{}/approve", "{}/reject", "{}/cancel",
"{}/update", "{}/validate", "{}/unassign", "{}/assign"})
public void postRequestOperation(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("AgentCertRequestServlet.postRequestOperation(): session: {}", session.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class AgentCertServlet extends CAServlet{
private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(AgentCertServlet.class);

@WebAction(method = HttpMethod.GET, paths = { "/{}"})
@WebAction(method = HttpMethod.GET, paths = { "{}"})
public void reviewCert(HttpServletRequest request, HttpServletResponse response) throws Exception {
CertId id;
try {
Expand All @@ -100,21 +100,21 @@ public void reviewCert(HttpServletRequest request, HttpServletResponse response)
}
}

@WebAction(method = HttpMethod.POST, paths = { "/{}/revoke-ca"})
@WebAction(method = HttpMethod.POST, paths = { "{}/revoke-ca"})
public void revokeCACert(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("AgentCertRequestServlet.revokeCACert(): session: {}", session.getId());
revoke(request, response, true);
}

@WebAction(method = HttpMethod.POST, paths = { "/{}/revoke"})
@WebAction(method = HttpMethod.POST, paths = { "{}/revoke"})
public void revokeCert(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("AgentCertRequestServlet.revokeCert(): session: {}", session.getId());
revoke(request, response, false);
}

@WebAction(method = HttpMethod.POST, paths = { "/{}/unrevoke"})
@WebAction(method = HttpMethod.POST, paths = { "{}/unrevoke"})
public void unrevokeCert(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("AgentCertRequestServlet.unrevokeCert(): session: {}", session.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class CAInfoServlet extends CAServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(CAInfoServlet.class);

@WebAction(method = HttpMethod.GET, paths = {"/"})
@WebAction(method = HttpMethod.GET, paths = {""})
public void getInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("CAInfoServlet.get(): session: {}", session.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class CertRequestServlet extends CAServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(CertRequestServlet.class);

@WebAction(method = HttpMethod.GET, paths = {"/{}"})
@WebAction(method = HttpMethod.GET, paths = {"{}"})
public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("CertRequestServlet.get(): session: {}", session.getId());
Expand All @@ -93,7 +93,7 @@ public void get(HttpServletRequest request, HttpServletResponse response) throws
out.println(info.toJSON());
}

@WebAction(method = HttpMethod.GET, paths = {"/profiles/{}"})
@WebAction(method = HttpMethod.GET, paths = {"profiles/{}"})
public void getEnrollmentTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {

HttpSession session = request.getSession();
Expand All @@ -105,7 +105,7 @@ public void getEnrollmentTemplate(HttpServletRequest request, HttpServletRespons
out.println(req.toJSON());
}

@WebAction(method = HttpMethod.POST, paths = {"/"})
@WebAction(method = HttpMethod.POST, paths = {""})
public void enrollCert(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("CertServlet.enrollCert(): session: {}", session.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class CertServlet extends CAServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(CertServlet.class);

@WebAction(method = HttpMethod.GET, paths = { "/"})
@WebAction(method = HttpMethod.GET, paths = {""})
public void listCerts(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("CertServlet.listCerts(): session: {}", session.getId());
Expand All @@ -84,7 +84,7 @@ public void listCerts(HttpServletRequest request, HttpServletResponse response)
out.println(infos.toJSON());
}

@WebAction(method = HttpMethod.GET, paths = { "/{}"})
@WebAction(method = HttpMethod.GET, paths = {"{}"})
public void getCert(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("CertServlet.getCert(): session: {}", session.getId());
Expand All @@ -107,7 +107,7 @@ public void getCert(HttpServletRequest request, HttpServletResponse response) th
}
}

@WebAction(method = HttpMethod.POST, paths = { "/search"})
@WebAction(method = HttpMethod.POST, paths = {"search"})
public void searchCerts(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("CertServlet.searchCerts(): session: {}", session.getId());
Expand Down
16 changes: 16 additions & 0 deletions base/server/src/main/java/com/netscape/certsrv/base/WebAction.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//
// Copyright Red Hat, Inc.
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package com.netscape.certsrv.base;

import static java.lang.annotation.ElementType.METHOD;
Expand All @@ -8,6 +13,17 @@

import org.dogtagpki.server.rest.v2.PKIServlet;

/**
* Implement basic routing for REST APIs
*
* If a servlet extends PKIServlet this annotation can specify the method to use for handle a
* specific REST API. It has two parameter associating the HTTP operation and the path to be
* handled by this method. The paths are relative and inside the servlet context.
*
*
* @see PKIServlet
* @author Marco Fargetta {@literal <[email protected]>}
*/
@Target(METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface WebAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class AccountServlet extends PKIServlet {
private static final long serialVersionUID = 1L;
public static final Logger logger = LoggerFactory.getLogger(AccountServlet.class);

@WebAction(method = HttpMethod.GET, paths = {"/login"})
@WebAction(method = HttpMethod.GET, paths = {"login"})
public void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.info("AccountServlet: Creating session {}", session.getId());
Expand All @@ -36,7 +36,7 @@ public void login(HttpServletRequest request, HttpServletResponse response) thro
PrintWriter out = response.getWriter();
out.println(account.toJSON());
}
@WebAction(method = HttpMethod.GET, paths = {"/logout"})
@WebAction(method = HttpMethod.GET, paths = {"logout"})
public void logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession(false);
if (session != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class JobServlet extends PKIServlet {
private static final long serialVersionUID = 1L;
public static final Logger logger = LoggerFactory.getLogger(JobServlet.class);

@WebAction(method = HttpMethod.GET, paths = {"/"})
@WebAction(method = HttpMethod.GET, paths = {""})
public void getJobs(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("JobServlet.getJobs(): session: {}", session.getId());
Expand All @@ -38,7 +38,7 @@ public void getJobs(HttpServletRequest request, HttpServletResponse response) th
out.println(jobs.toJSON());
}

@WebAction(method = HttpMethod.GET, paths = {"/{}"})
@WebAction(method = HttpMethod.GET, paths = {"{}"})
public void getJob(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("JobServlet.getJob(): session: {}", session.getId());
Expand All @@ -50,7 +50,7 @@ public void getJob(HttpServletRequest request, HttpServletResponse response) thr
out.println(job.toJSON());
}

@WebAction(method = HttpMethod.POST, paths = {"/{}/start"})
@WebAction(method = HttpMethod.POST, paths = {"{}/start"})
public void postJobStart(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("JobServlet.postJobStart(): session: {}", session.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@

/**
* Implement the basic class to handle REST APIs
*
* API are routed to method in subclasses implementing specific actions annotated with WebAction
*
* @see WebAction
* @author Marco Fargetta {@literal <[email protected]>}
*/
public abstract class PKIServlet extends HttpServlet {
Expand Down Expand Up @@ -178,9 +182,9 @@ private void doOperation(HttpMethod method, HttpServletRequest request, HttpServ
public Method getActionMethod(HttpMethod met, String path) {
final String reqMethod;
if (path == null) {
reqMethod = met.toString() + ":/";
reqMethod = met.toString() + ":";
} else {
reqMethod = met.toString() + ":" + path;
reqMethod = met.toString() + ":" + (path.startsWith("/") ? path.substring(1) : path);
}
String keyPath = webActions.keySet().stream().
filter( key -> {
Expand All @@ -193,7 +197,12 @@ public Method getActionMethod(HttpMethod met, String path) {
}

public String getAllowedMethods(String path) {
final String matchingPath = path == null ? "/" : path;
final String matchingPath;
if (path == null) {
matchingPath = "";
} else {
matchingPath = path.startsWith("/") ? path.substring(1) : path;
}
List<String> keyPaths = webActions.keySet().stream().
filter( key -> {
String keyRegex = key.substring(key.indexOf(":") + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SelfTestServlet extends PKIServlet {
private static final long serialVersionUID = 1L;
public static final Logger logger = LoggerFactory.getLogger(SelfTestServlet.class);

@WebAction(method = HttpMethod.GET, paths = {"/"})
@WebAction(method = HttpMethod.GET, paths = {""})
public void findTests(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("SelfTestServlet.findTests(): session: {}", session.getId());
Expand All @@ -44,7 +44,7 @@ public void findTests(HttpServletRequest request, HttpServletResponse response)
out.println(tests.toJSON());
}

@WebAction(method = HttpMethod.GET, paths = {"/{}"})
@WebAction(method = HttpMethod.GET, paths = {"{}"})
public void getTest(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("SelfTestServlet.getTest(): session: {}", session.getId());
Expand All @@ -56,7 +56,7 @@ public void getTest(HttpServletRequest request, HttpServletResponse response) th
out.println(test.toJSON());
}

@WebAction(method = HttpMethod.POST, paths = {"/"})
@WebAction(method = HttpMethod.POST, paths = {""})
public void executeTests(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("SelfTestServlet.executeTests(): session: {}", session.getId());
Expand All @@ -66,7 +66,7 @@ public void executeTests(HttpServletRequest request, HttpServletResponse respons
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}

@WebAction(method = HttpMethod.POST, paths = {"/run"})
@WebAction(method = HttpMethod.POST, paths = {"run"})
public void runTests(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("SelfTestServlet.runTests(): session: {}", session.getId());
Expand All @@ -77,7 +77,7 @@ public void runTests(HttpServletRequest request, HttpServletResponse response) t

}

@WebAction(method = HttpMethod.POST, paths = {"/{}/run"})
@WebAction(method = HttpMethod.POST, paths = {"{}/run"})
public void runTest(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("SelfTestServlet.runTest(): session: {}", session.getId());
Expand Down
Loading

0 comments on commit 31a4a1b

Please sign in to comment.