Skip to content

Commit

Permalink
Add TPS ConfigService to v2 APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarco76 committed Aug 27, 2024
1 parent 1236a4c commit da25f8a
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// Copyright Red Hat, Inc.
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package org.dogtagpki.server.tps.rest.v2;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
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.dogtagpki.common.ConfigData;
import org.dogtagpki.server.tps.config.ConfigDatabase;
import org.dogtagpki.server.tps.config.ConfigRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netscape.certsrv.base.BadRequestException;
import com.netscape.certsrv.base.PKIException;
import com.netscape.certsrv.base.WebAction;
import com.netscape.certsrv.logging.ILogger;
import com.netscape.certsrv.util.JSONSerializer;

@WebServlet(
name = "tpsConfig",
urlPatterns = "/v2/config/*")
public class ConfigServlet extends TPSServlet {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(ConfigServlet.class);

@WebAction(method = HttpMethod.GET, paths = {""})
public void getConfig(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("ConfigServlet.getConfig(): session: {}", session.getId());
ConfigData configData = new ConfigData();
try {
ConfigDatabase configDatabase = new ConfigDatabase();
ConfigRecord configRecord = configDatabase.getRecord("Generals");

Map<String, String> properties = configDatabase.getProperties(configRecord, null);

configData.setProperties(properties);
} catch (PKIException e) {
throw e;
} catch (Exception e) {
throw new PKIException(e.getMessage());
}
PrintWriter out = response.getWriter();
out.println(configData.toJSON());
}

@WebAction(method = HttpMethod.PATCH, paths = {""})
public void updateConfig(HttpServletRequest request, HttpServletResponse response) throws Exception {
String method = "ConfigServlet.updateConfig";
Map<String, String> auditModParams = new HashMap<>();

HttpSession session = request.getSession();
logger.debug("ConfigServlet.updateConfig(): session: {}", session.getId());
String requestData = request.getReader().lines().collect(Collectors.joining());
ConfigData configData = JSONSerializer.fromJSON(requestData, ConfigData.class);
if (configData == null) {
BadRequestException e = new BadRequestException("Config data is null.");
auditModParams.put("Info", e.toString());
auditConfigTokenGeneral(ILogger.FAILURE, method, auditModParams, e.toString());
throw e;
}
try {
ConfigDatabase configDatabase = new ConfigDatabase();
ConfigRecord configRecord = configDatabase.getRecord("Generals");

Map<String, String> newProperties = configData.getProperties();
if (newProperties != null) {
// validate new properties
configDatabase.validateProperties(configRecord, null, newProperties);

// remove old properties
configDatabase.removeProperties(configRecord, null);

// add new properties
configDatabase.addProperties(configRecord, null, newProperties);
}

configDatabase.commit();

Map<String, String> properties = configDatabase.getProperties(configRecord, null);
configData = new ConfigData();
configData.setProperties(properties);
auditConfigTokenGeneral(ILogger.SUCCESS, method,
newProperties, null);
} catch (PKIException e) {
logger.error(method +": " + e.getMessage(), e);
auditConfigTokenGeneral(ILogger.FAILURE, method,
auditModParams, e.toString());
throw e;

} catch (Exception e) {
logger.error(method +": " + e.getMessage(), e);
auditConfigTokenGeneral(ILogger.FAILURE, method,
auditModParams, e.toString());
throw new PKIException(e.getMessage());
}
PrintWriter out = response.getWriter();
out.println(configData.toJSON());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Copyright Red Hat, Inc.
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package org.dogtagpki.server.tps.rest.v2.filters;

import java.util.HashMap;
import java.util.Map;

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

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

@WebFilter(servletNames = "tpsConfig")
public class ConfigACL extends ACLFilter {
private static final long serialVersionUID = 1L;

@Override
public void init() throws ServletException {
setAcl("config.read");

Map<String, String> aclMap = new HashMap<>();
aclMap.put("PATCH:", "config.modify");
setAclMap(aclMap);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright Red Hat, Inc.
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package org.dogtagpki.server.tps.rest.v2.filters;

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

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

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

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

0 comments on commit da25f8a

Please sign in to comment.