Skip to content

Commit

Permalink
Add CA KRAConnectorService to v2 APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarco76 committed Aug 7, 2024
1 parent c628f79 commit 53eedbd
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//
// Copyright Red Hat, Inc.
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package org.dogtagpki.server.ca.rest.v2;

import java.io.PrintWriter;
import java.util.stream.Collectors;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.PKIException;
import com.netscape.certsrv.base.WebAction;
import com.netscape.certsrv.system.KRAConnectorInfo;
import com.netscape.certsrv.util.JSONSerializer;
import com.netscape.cms.servlet.admin.KRAConnectorProcessor;

/**
* @author Marco Fargetta {@literal <[email protected]>}
* @author Ade Lee
*/
@WebServlet(
name = "caKraConnector",
urlPatterns = "/v2/admin/kraconnector/*")
public class KRAConnectorServlet extends CAServlet {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(KRAConnectorServlet.class);

@WebAction(method = HttpMethod.GET, paths = {""})
public void getConnectorInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("KRAConnectorServlet.getConnectorInfo(): session: {}", session.getId());
KRAConnectorInfo connector = null;
PrintWriter out = response.getWriter();
try {
KRAConnectorProcessor processor = new KRAConnectorProcessor(request.getLocale());
processor.setCMSEngine(engine);
processor.init();
connector = processor.getConnectorInfo();
} catch (EBaseException e) {
String message = "Unable to get KRA connector: " + e.getMessage();
logger.error(message, e);
throw new PKIException(message, e);
}
out.println(connector.toJSON());
}

@WebAction(method = HttpMethod.POST, paths = {"add"})
public void addConnector(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("KRAConnectorServlet.addConnector(): session: {}", session.getId());
String requestData = request.getReader().lines().collect(Collectors.joining());
KRAConnectorInfo connector = JSONSerializer.fromJSON(requestData, KRAConnectorInfo.class);
try {
KRAConnectorProcessor processor = new KRAConnectorProcessor(request.getLocale());
processor.setCMSEngine(engine);
processor.init();
processor.addConnector(connector);
} catch (EBaseException e) {
String message = "Unable to add KRA connector: " + e.getMessage();
logger.error(message, e);
throw new PKIException(message, e);
}
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}

@WebAction(method = HttpMethod.POST, paths = {"remove"})
public void removeConnector(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("KRAConnectorServlet.removeConnector(): session: {}", session.getId());
String host = request.getParameter("host");
String port = request.getParameter("port");

try {
KRAConnectorProcessor processor = new KRAConnectorProcessor(request.getLocale());
processor.setCMSEngine(engine);
processor.init();
processor.removeConnector(host, port);
} catch (EBaseException e) {
String message = "Unable to remove KRA connector: " + e.getMessage();
logger.error(message, e);
throw new PKIException(message, e);
}
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}

@WebAction(method = HttpMethod.POST, paths = {"addHost"})
public void addHost(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("KRAConnectorServlet.addHost(): session: {}", session.getId());
String host = request.getParameter("host");
String port = request.getParameter("port");
try {
KRAConnectorProcessor processor = new KRAConnectorProcessor(request.getLocale());
processor.setCMSEngine(engine);
processor.init();
processor.addHost(host, port);
} catch (EBaseException e) {
String message = "Unable to add KRA connector: " + e.getMessage();
logger.error(message, e);
throw new PKIException(message, e);
}
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.dogtagpki.server.ca.rest.v2.filters;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;

import org.dogtagpki.server.rest.v2.filters.ACLFilter;

@WebFilter(servletNames = "caKraConnector")
public class KRAConnectorACL extends ACLFilter {
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
setAcl("kraconnectors");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.dogtagpki.server.ca.rest.v2.filters;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;

import org.dogtagpki.server.rest.v2.filters.AuthMethodFilter;

@WebFilter(servletNames = "caKraConnector")
public class KRAConnectorAuthMethod extends AuthMethodFilter {
private static final long serialVersionUID = 1L;

@Override
public void init() throws ServletException {
setAuthMethod("kraconnectors");
}

}

0 comments on commit 53eedbd

Please sign in to comment.