Skip to content

Commit

Permalink
Add PKI Services to v2 APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarco76 committed Aug 19, 2024
1 parent 3d749e6 commit cd070b8
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public PKIEngine getPKIEngine() {
}

public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}

public void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}

@Override
Expand All @@ -37,4 +42,17 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
throw new ServletException(e);
}
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
post(request, response);

} catch (ServletException | IOException e) {
throw e;

} catch (Exception e) {
throw new ServletException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package org.dogtagpki.server.rest;
package org.dogtagpki.server.rest.v1;

import java.io.File;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

package org.dogtagpki.server.rest;
package org.dogtagpki.server.rest.v1;

import javax.ws.rs.core.Response;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

package org.dogtagpki.server.rest;
package org.dogtagpki.server.rest.v1;

import javax.servlet.http.HttpSession;
import javax.ws.rs.core.Response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

package org.dogtagpki.server.rest;
package org.dogtagpki.server.rest.v1;

import java.util.LinkedHashSet;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// Copyright Red Hat, Inc.
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package org.dogtagpki.server.rest.v2;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;

import org.dogtagpki.common.AppInfo;
import org.dogtagpki.server.PKIServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netscape.cmscore.apps.CMS;

/**
* @author Marco Fargetta {@literal <[email protected]>}
* @author Endi S. Dewata
*/
@WebServlet("/v2/apps")
public class AppServlet extends PKIServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(AppServlet.class);

@Override
public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {

logger.info("PKI applications:");
Collection<AppInfo> apps = new ArrayList<>();

// get <instance>/conf folder
File instanceDir = new File(CMS.getInstanceDir());
File confDir = new File(instanceDir, "conf");

// get all folders under <instance>/conf
File[] appConfDirs = confDir.listFiles(File::isDirectory);
Arrays.sort(appConfDirs);

for (File appConfDir : appConfDirs) {
String id = appConfDir.getName();

// get app info if the app is deployed
AppInfo info = getAppInfo(request.getServletContext(), id);
if (info == null) continue;

logger.info("- ID: {}", info.getID());
logger.info(" Name: {}", info.getName());
logger.info(" Path: {}", info.getPath());

apps.add(info);
}

response.setContentType(MediaType.APPLICATION_JSON);

PrintWriter out = response.getWriter();
ObjectMapper mapper = new ObjectMapper();
out.println(mapper.writeValueAsString(apps));
}

public AppInfo getAppInfo(ServletContext context, String id) {

// get path of the application
String path = "/" + id;

// find context of the path
ServletContext ctx = context.getContext(path);

if (ctx == null) {
// context not available
return null;
}

if (!path.equals(ctx.getContextPath())) {
// path belongs to a different context,
// so the application is not deployed
return null;
}

// get display name from web.xml
String displayName = ctx.getServletContextName();

AppInfo info = new AppInfo();
info.setID(id);
info.setName(displayName);
info.setPath(path);

return info;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package org.dogtagpki.server.v2;
package org.dogtagpki.server.rest.v2;

import java.io.PrintWriter;

Expand All @@ -24,6 +24,7 @@ public class InfoServlet extends PKIServlet {

private static final long serialVersionUID = 1L;

@Override
public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {

PKIEngine engine = getPKIEngine();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Copyright Red Hat, Inc.
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package org.dogtagpki.server.rest.v2;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.dogtagpki.server.PKIServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Marco Fargetta {@literal <[email protected]>}
* @author Endi S. Dewata
*/
@WebServlet("/v2/login")
public class LoginServlet extends PKIServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(LoginServlet.class);



@Override
public void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("LoginService.login(): session: {}", session.getId());

// mark banner displayed in this session
session.setAttribute("bannerDisplayed", "true");
response.setStatus(HttpServletResponse.SC_NO_CONTENT);;
}
}
2 changes: 1 addition & 1 deletion base/tomcat-9.0/conf/Catalina/localhost/rewrite.config
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ RewriteRule ^/acme/revoke-cert$ /acme/v1/revoke-cert
RewriteRule ^/.well-known/est/(.*)$ /est/v1/$1

# REST APIs
RewriteRule ^/(ca|tps|tks|ocsp|kra)/rest/(.*)$ /$1/v1/$2
RewriteRule ^/(pki|ca|tps|tks|ocsp|kra)/rest/(.*)$ /$1/v1/$2

0 comments on commit cd070b8

Please sign in to comment.