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 /base/java-tools | |
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
Diffstat (limited to 'base/java-tools')
5 files changed, 383 insertions, 0 deletions
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); + } + } +} |