-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
base/tps/src/main/java/org/dogtagpki/server/tps/rest/v2/ConfigServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
base/tps/src/main/java/org/dogtagpki/server/tps/rest/v2/filters/ConfigACL.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
base/tps/src/main/java/org/dogtagpki/server/tps/rest/v2/filters/ConfigAuthMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |