diff options
author | Endi S. Dewata <edewata@redhat.com> | 2013-09-19 10:15:02 -0400 |
---|---|---|
committer | Endi S. Dewata <edewata@redhat.com> | 2013-09-20 12:37:08 -0400 |
commit | 5874cad1abe832a4a74cb37a4c22f0e18cf9bd8e (patch) | |
tree | ad0e88129530f4abcfb588a516c17ab5e0573eb9 | |
parent | 4c17e821a99318a1cf62ca0862ce9ee404ea5f6a (diff) | |
download | pki-5874cad1abe832a4a74cb37a4c22f0e18cf9bd8e.tar.gz pki-5874cad1abe832a4a74cb37a4c22f0e18cf9bd8e.tar.xz pki-5874cad1abe832a4a74cb37a4c22f0e18cf9bd8e.zip |
Added TPS config resource.
A new REST service and clients have been added to manage the TPS
configuration in CS.cfg. When the configuration is updated, the
previous configuration will be stored as a backup.
Ticket #652
15 files changed, 1073 insertions, 98 deletions
diff --git a/base/common/src/com/netscape/certsrv/base/IConfigStore.java b/base/common/src/com/netscape/certsrv/base/IConfigStore.java index d96fddb05..92bf7b2f6 100644 --- a/base/common/src/com/netscape/certsrv/base/IConfigStore.java +++ b/base/common/src/com/netscape/certsrv/base/IConfigStore.java @@ -19,6 +19,7 @@ package com.netscape.certsrv.base; import java.math.BigInteger; import java.util.Enumeration; +import java.util.Map; /** * An interface represents a configuration store. @@ -294,4 +295,9 @@ public interface IConfigStore extends ISourceConfigStore { * Return the number of items in this substore */ public int size(); + + /** + * Get properties as a map. + */ + public Map<String, String> getProperties() throws EBaseException; } diff --git a/base/common/src/com/netscape/certsrv/tps/TPSClient.java b/base/common/src/com/netscape/certsrv/tps/TPSClient.java index 8381b1ac2..e30858bfe 100644 --- a/base/common/src/com/netscape/certsrv/tps/TPSClient.java +++ b/base/common/src/com/netscape/certsrv/tps/TPSClient.java @@ -25,6 +25,7 @@ import com.netscape.certsrv.group.GroupClient; import com.netscape.certsrv.logging.ActivityClient; import com.netscape.certsrv.tps.authenticator.AuthenticatorClient; import com.netscape.certsrv.tps.cert.TPSCertClient; +import com.netscape.certsrv.tps.config.ConfigClient; import com.netscape.certsrv.tps.connection.ConnectionClient; import com.netscape.certsrv.tps.token.TokenClient; import com.netscape.certsrv.user.UserClient; @@ -42,6 +43,7 @@ public class TPSClient extends SubsystemClient { public void init() throws URISyntaxException { addClient(new ActivityClient(client, name)); addClient(new AuthenticatorClient(client, name)); + addClient(new ConfigClient(client, name)); addClient(new ConnectionClient(client, name)); addClient(new GroupClient(client, name)); addClient(new TokenClient(client, name)); diff --git a/base/common/src/com/netscape/certsrv/tps/config/ConfigClient.java b/base/common/src/com/netscape/certsrv/tps/config/ConfigClient.java new file mode 100644 index 000000000..c3876bf83 --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/config/ConfigClient.java @@ -0,0 +1,60 @@ +//--- BEGIN COPYRIGHT BLOCK --- +//This program is free software; you can redistribute it and/or modify +//it under the terms of the GNU General Public License as published by +//the Free Software Foundation; version 2 of the License. +// +//This program is distributed in the hope that it will be useful, +//but WITHOUT ANY WARRANTY; without even the implied warranty of +//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +//GNU General Public License for more details. +// +//You should have received a copy of the GNU General Public License along +//with this program; if not, write to the Free Software Foundation, Inc., +//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +//(C) 2013 Red Hat, Inc. +//All rights reserved. +//--- END COPYRIGHT BLOCK --- +package com.netscape.certsrv.tps.config; + +import java.net.URISyntaxException; + +import org.jboss.resteasy.client.ClientResponse; + +import com.netscape.certsrv.client.Client; +import com.netscape.certsrv.client.PKIClient; + +/** + * @author Endi S. Dewata + */ +public class ConfigClient extends Client { + + public ConfigResource resource; + + public ConfigClient(PKIClient client) throws URISyntaxException { + this(client, client.getSubsystem()); + } + + public ConfigClient(PKIClient client, String subsystem) throws URISyntaxException { + super(client, subsystem, "config"); + init(); + } + + public void init() throws URISyntaxException { + resource = createProxy(ConfigResource.class); + } + + public ConfigCollection findConfigs() { + return resource.findConfigs(); + } + + public ConfigData getConfig(String configID) { + return resource.getConfig(configID); + } + + public ConfigData updateConfig(String configID, ConfigData configData) { + @SuppressWarnings("unchecked") + ClientResponse<ConfigData> response = (ClientResponse<ConfigData>)resource.updateConfig(configID, configData); + return client.getEntity(response); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/config/ConfigCollection.java b/base/common/src/com/netscape/certsrv/tps/config/ConfigCollection.java new file mode 100644 index 000000000..a36f61fd1 --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/config/ConfigCollection.java @@ -0,0 +1,65 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.certsrv.tps.config; + +import java.util.ArrayList; +import java.util.Collection; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementRef; +import javax.xml.bind.annotation.XmlRootElement; + +import org.jboss.resteasy.plugins.providers.atom.Link; + + +/** + * @author Endi S. Dewata + */ +@XmlRootElement(name="Configurations") +public class ConfigCollection { + + Collection<ConfigData> configs = new ArrayList<ConfigData>(); + Collection<Link> links = new ArrayList<Link>(); + + @XmlElementRef + public Collection<ConfigData> getConfigs() { + return configs; + } + + public void setConfigs(Collection<ConfigData> configs) { + this.configs = configs; + } + + public void addConfig(ConfigData configData) { + configs.add(configData); + } + + @XmlElement(name="Link") + public Collection<Link> getLinks() { + return links; + } + + public void setLink(Collection<Link> links) { + this.links = links; + } + + public void addLink(Link link) { + links.add(link); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/config/ConfigData.java b/base/common/src/com/netscape/certsrv/tps/config/ConfigData.java new file mode 100644 index 000000000..1b3688f5e --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/config/ConfigData.java @@ -0,0 +1,231 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.certsrv.tps.config; + +import java.io.StringReader; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; +import javax.xml.bind.annotation.adapters.XmlAdapter; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import org.jboss.resteasy.plugins.providers.atom.Link; + +/** + * @author Endi S. Dewata + */ +@XmlRootElement(name="Configuration") +public class ConfigData { + + public static Marshaller marshaller; + public static Unmarshaller unmarshaller; + + static { + try { + marshaller = JAXBContext.newInstance(ConfigData.class).createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + unmarshaller = JAXBContext.newInstance(ConfigData.class).createUnmarshaller(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + String id; + String displayName; + Map<String, String> properties = new LinkedHashMap<String, String>(); + Link link; + + @XmlAttribute(name="id") + public String getID() { + return id; + } + + public void setID(String id) { + this.id = id; + } + + @XmlElement(name="DisplayName") + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + @XmlElement(name="Properties") + @XmlJavaTypeAdapter(MapAdapter.class) + public Map<String, String> getProperties() { + return properties; + } + + public void setProperties(Map<String, String> properties) { + this.properties.clear(); + this.properties.putAll(properties); + } + + public Collection<String> getPropertyNames() { + return properties.keySet(); + } + + public String getProperty(String name) { + return properties.get(name); + } + + public void setProperty(String name, String value) { + properties.put(name, value); + } + + public String removeProperty(String name) { + return properties.remove(name); + } + + public static class MapAdapter extends XmlAdapter<PropertyList, Map<String, String>> { + + public PropertyList marshal(Map<String, String> map) { + PropertyList list = new PropertyList(); + for (Map.Entry<String, String> entry : map.entrySet()) { + Property property = new Property(); + property.name = entry.getKey(); + property.value = entry.getValue(); + list.properties.add(property); + } + return list; + } + + public Map<String, String> unmarshal(PropertyList list) { + Map<String, String> map = new LinkedHashMap<String, String>(); + for (Property property : list.properties) { + map.put(property.name, property.value); + } + return map; + } + } + + public static class PropertyList { + @XmlElement(name="Property") + public List<Property> properties = new ArrayList<Property>(); + } + + public static class Property { + + @XmlAttribute + public String name; + + @XmlValue + public String value; + } + + @XmlElement(name="Link") + public Link getLink() { + return link; + } + + public void setLink(Link link) { + this.link = link; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((displayName == null) ? 0 : displayName.hashCode()); + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((link == null) ? 0 : link.hashCode()); + result = prime * result + ((properties == null) ? 0 : properties.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ConfigData other = (ConfigData) obj; + if (displayName == null) { + if (other.displayName != null) + return false; + } else if (!displayName.equals(other.displayName)) + return false; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (link == null) { + if (other.link != null) + return false; + } else if (!link.equals(other.link)) + return false; + if (properties == null) { + if (other.properties != null) + return false; + } else if (!properties.equals(other.properties)) + return false; + return true; + } + + public String toString() { + try { + StringWriter sw = new StringWriter(); + marshaller.marshal(this, sw); + return sw.toString(); + + } catch (Exception e) { + return super.toString(); + } + } + + public static ConfigData valueOf(String string) throws Exception { + try { + return (ConfigData)unmarshaller.unmarshal(new StringReader(string)); + } catch (Exception e) { + return null; + } + } + + public static void main(String args[]) throws Exception { + + ConfigData before = new ConfigData(); + before.setID("test"); + before.setDisplayName("Test Config"); + before.setProperty("param1", "value1"); + before.setProperty("param2", "value2"); + + String string = before.toString(); + System.out.println(string); + + ConfigData after = ConfigData.valueOf(string); + System.out.println(before.equals(after)); + } +} diff --git a/base/common/src/com/netscape/certsrv/tps/config/ConfigResource.java b/base/common/src/com/netscape/certsrv/tps/config/ConfigResource.java new file mode 100644 index 000000000..a15730a4b --- /dev/null +++ b/base/common/src/com/netscape/certsrv/tps/config/ConfigResource.java @@ -0,0 +1,53 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package com.netscape.certsrv.tps.config; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.jboss.resteasy.annotations.ClientResponseType; + + +/** + * @author Endi S. Dewata + */ +@Path("configs") +public interface ConfigResource { + + @GET + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public ConfigCollection findConfigs(); + + @GET + @Path("{configID}") + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public ConfigData getConfig(@PathParam("configID") String configID); + + @PUT + @Path("{configID}") + @ClientResponseType(entityType=ConfigData.class) + @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public Response updateConfig(@PathParam("configID") String configID, ConfigData configData); +} diff --git a/base/common/src/com/netscape/cmscore/base/FileConfigStore.java b/base/common/src/com/netscape/cmscore/base/FileConfigStore.java index a91acdbcb..b77f86d78 100644 --- a/base/common/src/com/netscape/cmscore/base/FileConfigStore.java +++ b/base/common/src/com/netscape/cmscore/base/FileConfigStore.java @@ -24,8 +24,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; -import java.util.Enumeration; -import java.util.Vector; +import java.util.Map; import com.netscape.certsrv.apps.CMS; import com.netscape.certsrv.base.EBaseException; @@ -157,66 +156,20 @@ public class FileConfigStore extends PropConfigStore implements */ public void save(String fileName) throws EBaseException { try { + Map<String, String> map = getProperties(); + FileOutputStream fo = new FileOutputStream(fileName); PrintWriter writer = new PrintWriter(new OutputStreamWriter(fo)); - printSubStore(writer, this, ""); + for (String name : map.keySet()) { + String value = map.get(name); + writer.println(name + "=" + value); + } + writer.close(); fo.close(); } catch (IOException e) { throw new EBaseException("output stream error " + fileName, e); } } - - private void printSubStore(PrintWriter writer, IConfigStore store, - String name) throws EBaseException, - IOException { - // print keys - Enumeration<String> e0 = store.getPropertyNames(); - Vector<String> v = new Vector<String>(); - - while (e0.hasMoreElements()) { - v.addElement(e0.nextElement()); - } - - // sorting them lexicographically - while (v.size() > 0) { - String pname = v.firstElement(); - int j = 0; - - for (int i = 1; i < v.size(); i++) { - String s = v.elementAt(i); - - if (pname.compareTo(s) > 0) { - j = i; - pname = v.elementAt(i); - } - } - v.removeElementAt(j); - writer.println(name + pname + "=" + store.getString(pname)); - } - - // print substores - Enumeration<String> e1 = store.getSubStoreNames(); - - while (e1.hasMoreElements()) { - v.addElement(e1.nextElement()); - } - while (v.size() > 0) { - String pname = v.firstElement(); - int j = 0; - - for (int i = 1; i < v.size(); i++) { - String s = v.elementAt(i); - - if (pname.compareTo(s) > 0) { - j = i; - pname = v.elementAt(i); - } - } - v.removeElementAt(j); - printSubStore(writer, store.getSubStore(pname), name + - pname + "."); - } - } } diff --git a/base/common/src/com/netscape/cmscore/base/PropConfigStore.java b/base/common/src/com/netscape/cmscore/base/PropConfigStore.java index 161f549f2..eb3f6c312 100644 --- a/base/common/src/com/netscape/cmscore/base/PropConfigStore.java +++ b/base/common/src/com/netscape/cmscore/base/PropConfigStore.java @@ -26,6 +26,8 @@ import java.io.PrintStream; import java.math.BigInteger; import java.util.Enumeration; import java.util.Hashtable; +import java.util.Map; +import java.util.TreeMap; import java.util.Vector; import org.mozilla.jss.util.Base64OutputStream; @@ -138,7 +140,7 @@ public class PropConfigStore implements IConfigStore, Cloneable { * @param name property name * @return property value */ - private Object nakedGet(String name) { + private String nakedGet(String name) { return mSource.get(name); } @@ -172,42 +174,39 @@ public class PropConfigStore implements IConfigStore, Cloneable { * @see java.util.Enumeration */ public Enumeration<String> keys() { - Hashtable<String, Object> h = new Hashtable<String, Object>(); - + Hashtable<String, String> h = new Hashtable<String, String>(); enumerate(h); return h.keys(); } /** - * Retrieves the hashtable where all the properties are kept. + * Retrieves lexicographically sorted properties as a map. * - * @return hashtable + * @return map */ - public Hashtable<String, Object> hashtable() { - Hashtable<String, Object> h = new Hashtable<String, Object>(); - - enumerate(h); - return h; + public Map<String, String> getProperties() { + Map<String, String> map = new TreeMap<String, String>(); + enumerate(map); + return map; } /** * Return the number of items in this substore */ public int size() { - Hashtable<String, Object> h = new Hashtable<String, Object>(); - + Hashtable<String, String> h = new Hashtable<String, String>(); enumerate(h); return h.size(); } /** - * Fills the given hash table with all key/value pairs in the current + * Fills the given map with all key/value pairs in the current * config store, removing the config store name prefix * <P> * - * @param h the hashtable + * @param map the map */ - private synchronized void enumerate(Hashtable<String, Object> h) { + private synchronized void enumerate(Map<String, String> map) { Enumeration<String> e = mSource.keys(); // We only want the keys which match the current substore name // without the current substore prefix. This code works even @@ -219,7 +218,7 @@ public class PropConfigStore implements IConfigStore, Cloneable { String key = e.nextElement(); if (key.startsWith(fullName)) { - h.put(key.substring(kIndex), nakedGet(key)); + map.put(key.substring(kIndex), nakedGet(key)); } } } @@ -648,24 +647,17 @@ public class PropConfigStore implements IConfigStore, Cloneable { */ public Enumeration<String> getPropertyNames() { // XXX - this operation is expensive!!! - Hashtable<String, Object> h = new Hashtable<String, Object>(); + Map<String, String> map = getProperties(); - enumerate(h); - Enumeration<String> e = h.keys(); Vector<String> v = new Vector<String>(); + for (String name : map.keySet()) { + int i = name.indexOf('.'); // substores have "." + if (i >= 0) continue; + if (v.contains(name)) continue; - while (e.hasMoreElements()) { - String pname = e.nextElement(); - int i = pname.indexOf('.'); // substores have "." - - if (i == -1) { - String n = pname; - - if (!v.contains(n)) { - v.addElement(n); - } - } + v.addElement(name); } + return v.elements(); } @@ -677,24 +669,19 @@ public class PropConfigStore implements IConfigStore, Cloneable { */ public Enumeration<String> getSubStoreNames() { // XXX - this operation is expensive!!! - Hashtable<String, Object> h = new Hashtable<String, Object>(); + Map<String, String> map = getProperties(); - enumerate(h); - Enumeration<String> e = h.keys(); Vector<String> v = new Vector<String>(); + for (String name : map.keySet()) { + int i = name.indexOf('.'); // substores have "." + if (i < 0) continue; - while (e.hasMoreElements()) { - String pname = e.nextElement(); - int i = pname.indexOf('.'); // substores have "." - - if (i != -1) { - String n = pname.substring(0, i); + name = name.substring(0, i); + if (v.contains(name)) continue; - if (!v.contains(n)) { - v.addElement(n); - } - } + v.addElement(name); } + return v.elements(); } diff --git a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java index 8adf09ede..56d0bff19 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java @@ -24,6 +24,7 @@ import com.netscape.cmstools.group.GroupCLI; import com.netscape.cmstools.logging.ActivityCLI; import com.netscape.cmstools.tps.authenticator.AuthenticatorCLI; import com.netscape.cmstools.tps.cert.TPSCertCLI; +import com.netscape.cmstools.tps.config.ConfigCLI; import com.netscape.cmstools.tps.connection.ConnectionCLI; import com.netscape.cmstools.tps.token.TokenCLI; import com.netscape.cmstools.user.UserCLI; @@ -40,6 +41,7 @@ public class TPSCLI extends SubsystemCLI { addModule(new ActivityCLI(this)); addModule(new AuthenticatorCLI(this)); + addModule(new ConfigCLI(this)); addModule(new ConnectionCLI(this)); addModule(new GroupCLI(this)); addModule(new TokenCLI(this)); diff --git a/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigCLI.java new file mode 100644 index 000000000..9d913517e --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigCLI.java @@ -0,0 +1,92 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.tps.config; + +import java.io.IOException; +import java.util.Arrays; + +import org.jboss.resteasy.plugins.providers.atom.Link; + +import com.netscape.certsrv.tps.config.ConfigClient; +import com.netscape.certsrv.tps.config.ConfigData; +import com.netscape.cmstools.cli.CLI; + +/** + * @author Endi S. Dewata + */ +public class ConfigCLI extends CLI { + + public ConfigClient configClient; + + public ConfigCLI(CLI parent) { + super("config", "Configuration management commands", parent); + + addModule(new ConfigFindCLI(this)); + addModule(new ConfigModifyCLI(this)); + addModule(new ConfigShowCLI(this)); + } + + public void execute(String[] args) throws Exception { + + client = parent.getClient(); + configClient = (ConfigClient)parent.getClient("config"); + + if (args.length == 0) { + printHelp(); + System.exit(1); + } + + String command = args[0]; + String[] commandArgs = Arrays.copyOfRange(args, 1, args.length); + + if (command == null) { + printHelp(); + System.exit(1); + } + + CLI module = getModule(command); + if (module != null) { + module.execute(commandArgs); + + } else { + System.err.println("Error: Invalid command \"" + command + "\""); + printHelp(); + System.exit(1); + } + } + + public static void printConfigData(ConfigData configData, boolean showProperties) throws IOException { + + System.out.println(" Config ID: " + configData.getID()); + System.out.println(" Display Name: " + configData.getDisplayName()); + + if (showProperties) { + System.out.println(" Properties:"); + for (String name : configData.getPropertyNames()) { + String value = configData.getProperty(name); + System.out.println(" " + name + ": " + value); + } + } + + Link link = configData.getLink(); + if (verbose && link != null) { + System.out.println(" Link: " + link.getHref()); + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigFindCLI.java new file mode 100644 index 000000000..c7ab18738 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigFindCLI.java @@ -0,0 +1,86 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.tps.config; + +import java.util.Collection; + +import org.apache.commons.cli.CommandLine; + +import com.netscape.certsrv.tps.config.ConfigCollection; +import com.netscape.certsrv.tps.config.ConfigData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConfigFindCLI extends CLI { + + public ConfigCLI configCLI; + + public ConfigFindCLI(ConfigCLI configCLI) { + super("find", "Find configurations", configCLI); + this.configCLI = configCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName(), options); + } + + public void execute(String[] args) throws Exception { + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length != 0) { + printHelp(); + System.exit(1); + } + + ConfigCollection result = configCLI.configClient.findConfigs(); + + Collection<ConfigData> entries = result.getConfigs(); + + MainCLI.printMessage(entries.size() + " entries matched"); + boolean first = true; + + for (ConfigData configData : entries) { + + if (first) { + first = false; + } else { + System.out.println(); + } + + ConfigCLI.printConfigData(configData, false); + } + + MainCLI.printMessage("Number of entries returned " + entries.size()); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigModifyCLI.java new file mode 100644 index 000000000..3344a8c9e --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigModifyCLI.java @@ -0,0 +1,116 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.tps.config; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.tps.config.ConfigData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConfigModifyCLI extends CLI { + + public ConfigCLI configCLI; + + public ConfigModifyCLI(ConfigCLI configCLI) { + super("mod", "Modify configuration", configCLI); + this.configCLI = configCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Config ID> [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "input", true, "Input configuration file."); + option.setArgName("file"); + option.setRequired(true); + options.addOption(option); + + option = new Option(null, "output", true, "Output configuration file."); + option.setArgName("file"); + options.addOption(option); + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length != 1) { + printHelp(); + System.exit(1); + } + + String configID = args[0]; + String input = cmd.getOptionValue("input"); + String output = cmd.getOptionValue("output"); + + if (input == null) { + System.err.println("Error: Input file is required."); + printHelp(); + System.exit(1); + } + + ConfigData configData; + + try (BufferedReader in = new BufferedReader(new FileReader(input)); + StringWriter sw = new StringWriter(); + PrintWriter out = new PrintWriter(sw, true)) { + + String line; + while ((line = in.readLine()) != null) { + out.println(line); + } + + configData = ConfigData.valueOf(sw.toString()); + } + + configData = configCLI.configClient.updateConfig(configID, configData); + + MainCLI.printMessage("Updated configuration"); + + if (output == null) { + ConfigCLI.printConfigData(configData, true); + + } else { + try (PrintWriter out = new PrintWriter(new FileWriter(output))) { + out.println(configData); + } + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigShowCLI.java new file mode 100644 index 000000000..77f77c94f --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigShowCLI.java @@ -0,0 +1,87 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.tps.config; + +import java.io.FileWriter; +import java.io.PrintWriter; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.tps.config.ConfigData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConfigShowCLI extends CLI { + + public ConfigCLI configCLI; + + public ConfigShowCLI(ConfigCLI configCLI) { + super("show", "Show configuration", configCLI); + this.configCLI = configCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Config ID>", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "output", true, "Output file to store config attributes."); + option.setArgName("file"); + options.addOption(option); + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length != 1) { + printHelp(); + System.exit(1); + } + + String configID = cmdArgs[0]; + String output = cmd.getOptionValue("output"); + + ConfigData configData = configCLI.configClient.getConfig(configID); + + if (output == null) { + MainCLI.printMessage("Configuration"); + ConfigCLI.printConfigData(configData, true); + + } else { + try (PrintWriter out = new PrintWriter(new FileWriter(output))) { + out.println(configData); + } + MainCLI.printMessage("Stored configuration into " + output); + } + } +} diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java index ff66e6b75..c3c6195f5 100644 --- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java +++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java @@ -24,6 +24,7 @@ import javax.ws.rs.core.Application; import org.dogtagpki.server.tps.authenticator.AuthenticatorService; import org.dogtagpki.server.tps.cert.TPSCertService; +import org.dogtagpki.server.tps.config.ConfigService; import org.dogtagpki.server.tps.connection.ConnectionService; import org.dogtagpki.server.tps.logging.ActivityService; import org.dogtagpki.server.tps.token.TokenService; @@ -72,6 +73,9 @@ public class TPSApplication extends Application { // authenticators classes.add(AuthenticatorService.class); + // config + classes.add(ConfigService.class); + // connections classes.add(ConnectionService.class); diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConfigService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConfigService.java new file mode 100644 index 000000000..41a8c1714 --- /dev/null +++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConfigService.java @@ -0,0 +1,231 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2013 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package org.dogtagpki.server.tps.config; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Request; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.jboss.resteasy.plugins.providers.atom.Link; + +import com.netscape.certsrv.apps.CMS; +import com.netscape.certsrv.base.BadRequestException; +import com.netscape.certsrv.base.IConfigStore; +import com.netscape.certsrv.base.PKIException; +import com.netscape.certsrv.base.ResourceNotFoundException; +import com.netscape.certsrv.tps.config.ConfigCollection; +import com.netscape.certsrv.tps.config.ConfigData; +import com.netscape.certsrv.tps.config.ConfigResource; +import com.netscape.cms.servlet.base.PKIService; + +/** + * @author Endi S. Dewata + */ +public class ConfigService extends PKIService implements ConfigResource { + + @Context + private UriInfo uriInfo; + + @Context + private HttpHeaders headers; + + @Context + private Request request; + + @Context + private HttpServletRequest servletRequest; + + public final static int DEFAULT_SIZE = 20; + + public ConfigService() { + CMS.debug("ConfigService.<init>()"); + } + + public Collection<String> getPatterns(String configID, Map<String, String> map) { + Collection<String> patterns = new ArrayList<String>(); + + String pattern = map.get("target." + configID + ".pattern"); + if (pattern != null) { + // replace \| with | + pattern = pattern.replace("\\|", "|"); + + String list = map.get("target." + configID + ".list"); + if (list == null) { + patterns.add(pattern); + + } else { + for (String value : list.split(",")) { + patterns.add(pattern.replace("$name", value)); + } + } + } + + return patterns; + } + + public ConfigData createConfigData(String configID, Map<String, String> map) throws UnsupportedEncodingException { + + String displayName = map.get("target." + configID + ".displayname"); + if (displayName == null) { + throw new ResourceNotFoundException("Configuration " + configID + " not found."); + } + + ConfigData configData = new ConfigData(); + configData.setID(configID); + configData.setDisplayName(displayName); + + // add properties that fit the patterns + Collection<String> patterns = getPatterns(configID, map); + for (String pattern : patterns) { + for (String name : map.keySet()) { + if (!name.matches(pattern)) continue; + + String value = map.get(name); + configData.setProperty(name, value); + } + } + + configID = URLEncoder.encode(configID, "UTF-8"); + URI uri = uriInfo.getBaseUriBuilder().path(ConfigResource.class).path("{configID}").build(configID); + configData.setLink(new Link("self", uri)); + + return configData; + } + + @Override + public ConfigCollection findConfigs() { + + CMS.debug("ConfigService.findConfigs()"); + + try { + IConfigStore configStore = CMS.getConfigStore(); + Map<String, String> map = configStore.getProperties(); + + ConfigCollection result = new ConfigCollection(); + + Collection<String> configIDs = new LinkedHashSet<String>(); + configIDs.add("Generals"); + + String list = map.get("target.configure.list"); + if (list != null) { + configIDs.addAll(Arrays.asList(list.split(","))); + } + + list = map.get("target.agent_approve.list"); + if (list != null) { + configIDs.addAll(Arrays.asList(list.split(","))); + } + + for (String configID : configIDs) { + ConfigData configData = createConfigData(configID, map); + result.addConfig(configData); + } + + return result; + + } catch (PKIException e) { + throw e; + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public ConfigData getConfig(String configID) { + + CMS.debug("ConfigService.getConfig()"); + + try { + IConfigStore configStore = CMS.getConfigStore(); + Map<String, String> map = configStore.getProperties(); + + return createConfigData(configID, map); + + } catch (PKIException e) { + throw e; + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } + + @Override + public Response updateConfig(String configID, ConfigData newConfigData) { + + CMS.debug("ConfigService.updateConfig()"); + + try { + IConfigStore configStore = CMS.getConfigStore(); + Map<String, String> map = configStore.getProperties(); + + // verify that new properties fit the patterns + Collection<String> patterns = getPatterns(configID, map); + for (String pattern : patterns) { + for (String name : newConfigData.getPropertyNames()) { + if (name.matches(pattern)) continue; + throw new BadRequestException("Invalid property: " + name); + } + } + + // remove old properties + ConfigData configData = createConfigData(configID, map); + for (String name : configData.getPropertyNames()) { + configStore.remove(name); + } + + // store new properties + for (String name : newConfigData.getPropertyNames()) { + String value = newConfigData.getProperty(name); + configStore.put(name, value); + } + + configStore.commit(true); + + newConfigData = getConfig(configID); + + return Response + .ok(newConfigData) + .type(MediaType.APPLICATION_XML) + .build(); + + } catch (PKIException e) { + throw e; + + } catch (Exception e) { + e.printStackTrace(); + throw new PKIException(e.getMessage()); + } + } +} |