diff options
Diffstat (limited to 'base/java-tools/src/com/netscape/cmstools')
9 files changed, 628 insertions, 1 deletions
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/CLI.java b/base/java-tools/src/com/netscape/cmstools/cli/CLI.java index a1fc4f7f9..40ac517af 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/CLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/CLI.java @@ -37,6 +37,8 @@ public class CLI { public static CommandLineParser parser = new PosixParser(); public static HelpFormatter formatter = new HelpFormatter(); + public CLI parent; + public String name; public String description; @@ -44,8 +46,13 @@ public class CLI { public Map<String, CLI> modules = new LinkedHashMap<String, CLI>(); public CLI(String name, String description) { + this(name, description, null); + } + + public CLI(String name, String description, CLI parent) { this.name = name; this.description = description; + this.parent = parent; } public String getName() { @@ -56,6 +63,14 @@ public class CLI { this.name = name; } + public String getFullName() { + if (parent == null) { + return name; + } else { + return parent.getName() + "-" + name; + } + } + public String getDescription() { return description; } diff --git a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java index 1c6411d79..257f0bcfb 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java @@ -76,6 +76,7 @@ public class MainCLI extends CLI { addModule(new KRAConnectorCLI(this)); addModule(new ProfileCLI(this)); addModule(new SecurityDomainCLI(this)); + addModule(new TPSCLI(this)); addModule(new UserCLI(this)); } @@ -343,9 +344,10 @@ public class MainCLI extends CLI { } // get command module + if (verbose) System.out.println("Module: " + moduleName); module = getModule(moduleName); if (module == null) - throw new Error("Invalid command \"" + command + "\"."); + throw new Error("Invalid module \"" + moduleName + "\"."); // prepare module arguments if (moduleCommand != null) { diff --git a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java new file mode 100644 index 000000000..4dd7e208c --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java @@ -0,0 +1,109 @@ +// --- 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.cli; + +import org.apache.commons.lang.StringUtils; + +import com.netscape.certsrv.tps.TPSClient; +import com.netscape.cmstools.token.TokenCLI; + +/** + * @author Endi S. Dewata + */ +public class TPSCLI extends CLI { + + public MainCLI mainCLI; + public TPSClient tpsClient; + + public TPSCLI(MainCLI mainCLI) { + super("tps", "TPS management commands", mainCLI); + this.mainCLI = mainCLI; + + addModule(new TokenCLI(this)); + } + + public void printHelp() { + + System.out.println("Commands:"); + + int leftPadding = 1; + int rightPadding = 25; + + for (CLI module : modules.values()) { + String label = name + "-" + module.getName(); + + int padding = rightPadding - leftPadding - label.length(); + if (padding < 1) + padding = 1; + + System.out.print(StringUtils.repeat(" ", leftPadding)); + System.out.print(label); + System.out.print(StringUtils.repeat(" ", padding)); + System.out.println(module.getDescription()); + } + } + + public void execute(String[] args) throws Exception { + + tpsClient = new TPSClient(mainCLI.client); + + if (args.length == 0) { + printHelp(); + System.exit(1); + } + + String command = args[0]; + String moduleName; + String moduleCommand; + + // If a command contains a '-' sign it will be + // split into module name and module command. + // Otherwise it's a single command. + int i = command.indexOf('-'); + if (i >= 0) { // <module name>-<module command> + moduleName = command.substring(0, i); + moduleCommand = command.substring(i+1); + + } else { // <command> + moduleName = command; + moduleCommand = null; + } + + // get command module + if (verbose) System.out.println("Module: " + moduleName); + CLI module = getModule(moduleName); + if (module == null) { + throw new Error("Invalid module \"" + moduleName + "\"."); + } + + // prepare module arguments + String[] moduleArgs; + if (moduleCommand != null) { + moduleArgs = new String[args.length]; + moduleArgs[0] = moduleCommand; + System.arraycopy(args, 1, moduleArgs, 1, args.length-1); + + } else { + moduleArgs = new String[args.length-1]; + System.arraycopy(args, 1, moduleArgs, 0, args.length-1); + } + + module.execute(moduleArgs); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/token/TokenAddCLI.java b/base/java-tools/src/com/netscape/cmstools/token/TokenAddCLI.java new file mode 100644 index 000000000..10405a906 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/token/TokenAddCLI.java @@ -0,0 +1,81 @@ +// --- 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.token; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.token.TokenData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class TokenAddCLI extends CLI { + + public TokenCLI tokenCLI; + + public TokenAddCLI(TokenCLI tokenCLI) { + super("add", "Add token", tokenCLI); + this.tokenCLI = tokenCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Token ID> [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "user", true, "User ID"); + option.setArgName("User ID"); + option.setRequired(true); + 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 tokenID = cmdArgs[0]; + + TokenData tokenData = new TokenData(); + tokenData.setID(tokenID); + tokenData.setUserID(cmd.getOptionValue("user")); + + tokenData = tokenCLI.tokenClient.addToken(tokenData); + + MainCLI.printMessage("Added token \"" + tokenID + "\""); + + TokenCLI.printToken(tokenData); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/token/TokenCLI.java b/base/java-tools/src/com/netscape/cmstools/token/TokenCLI.java new file mode 100644 index 000000000..539ce7bae --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/token/TokenCLI.java @@ -0,0 +1,136 @@ +// --- 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.token; + +import org.apache.commons.lang.StringUtils; +import org.jboss.resteasy.plugins.providers.atom.Link; + +import com.netscape.certsrv.token.TokenClient; +import com.netscape.certsrv.token.TokenData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.TPSCLI; + +/** + * @author Endi S. Dewata + */ +public class TokenCLI extends CLI { + + public TPSCLI tpsCLI; + public TokenClient tokenClient; + + public TokenCLI(TPSCLI tpsCLI) { + super("token", "Token management commands", tpsCLI); + this.tpsCLI = tpsCLI; + + addModule(new TokenAddCLI(this)); + addModule(new TokenFindCLI(this)); + addModule(new TokenModifyCLI(this)); + addModule(new TokenRemoveCLI(this)); + addModule(new TokenShowCLI(this)); + } + + public String getFullName() { + return parent.getName() + "-" + name; + } + + public void printHelp() { + + System.out.println("Commands:"); + + int leftPadding = 1; + int rightPadding = 25; + + for (CLI module : modules.values()) { + String label = getFullName() + "-" + module.getName(); + + int padding = rightPadding - leftPadding - label.length(); + if (padding < 1) + padding = 1; + + System.out.print(StringUtils.repeat(" ", leftPadding)); + System.out.print(label); + System.out.print(StringUtils.repeat(" ", padding)); + System.out.println(module.getDescription()); + } + } + + public void execute(String[] args) throws Exception { + + tokenClient = new TokenClient(tpsCLI.mainCLI.client); + + if (args.length == 0) { + printHelp(); + System.exit(1); + } + + String command = args[0]; + String moduleName; + String moduleCommand; + + // If a command contains a '-' sign it will be + // split into module name and module command. + // Otherwise it's a single command. + int i = command.indexOf('-'); + if (i >= 0) { // <module name>-<module command> + moduleName = command.substring(0, i); + moduleCommand = command.substring(i+1); + + } else { // <command> + moduleName = command; + moduleCommand = null; + } + + // get command module + if (verbose) System.out.println("Module: " + moduleName); + CLI module = getModule(moduleName); + if (module == null) { + throw new Error("Invalid module \"" + moduleName + "\"."); + } + + // prepare module arguments + String[] moduleArgs; + if (moduleCommand != null) { + moduleArgs = new String[args.length]; + moduleArgs[0] = moduleCommand; + System.arraycopy(args, 1, moduleArgs, 1, args.length-1); + + } else { + moduleArgs = new String[args.length-1]; + System.arraycopy(args, 1, moduleArgs, 0, args.length-1); + } + + module.execute(moduleArgs); + } + + public static void printToken(TokenData token) { + System.out.println(" Token ID: " + token.getID()); + if (token.getUserID() != null) System.out.println(" User ID: " + token.getUserID()); + if (token.getStatus() != null) System.out.println(" Status: " + token.getStatus()); + if (token.getReason() != null) System.out.println(" Reason: " + token.getReason()); + if (token.getAppletID() != null) System.out.println(" Applet ID: " + token.getAppletID()); + if (token.getKeyInfo() != null) System.out.println(" Key Info: " + token.getKeyInfo()); + if (token.getCreateTimestamp() != null) System.out.println(" Date Created: " + token.getCreateTimestamp()); + if (token.getModifyTimestamp() != null) System.out.println(" Date Modified: " + token.getModifyTimestamp()); + + Link link = token.getLink(); + if (verbose && link != null) { + System.out.println(" Link: " + link.getHref()); + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/token/TokenFindCLI.java b/base/java-tools/src/com/netscape/cmstools/token/TokenFindCLI.java new file mode 100644 index 000000000..a627d13fb --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/token/TokenFindCLI.java @@ -0,0 +1,94 @@ +// --- 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.token; + +import java.util.Collection; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.token.TokenCollection; +import com.netscape.certsrv.token.TokenData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class TokenFindCLI extends CLI { + + public TokenCLI tokenCLI; + + public TokenFindCLI(TokenCLI tokenCLI) { + super("find", "Find tokens", tokenCLI); + this.tokenCLI = tokenCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "start", true, "Page start"); + option.setArgName("start"); + options.addOption(option); + + option = new Option(null, "size", true, "Page size"); + option.setArgName("size"); + 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 s = cmd.getOptionValue("start"); + Integer start = s == null ? null : Integer.valueOf(s); + + s = cmd.getOptionValue("size"); + Integer size = s == null ? null : Integer.valueOf(s); + + TokenCollection result = tokenCLI.tokenClient.findTokens(start, size); + Collection<TokenData> tokens = result.getTokens(); + + MainCLI.printMessage(tokens.size() + " token(s) matched"); + + boolean first = true; + + for (TokenData tokenData : tokens) { + + if (first) { + first = false; + } else { + System.out.println(); + } + + TokenCLI.printToken(tokenData); + } + + MainCLI.printMessage("Number of entries returned " + tokens.size()); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/token/TokenModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/token/TokenModifyCLI.java new file mode 100644 index 000000000..29f1a0741 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/token/TokenModifyCLI.java @@ -0,0 +1,81 @@ +// --- 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.token; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.token.TokenData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class TokenModifyCLI extends CLI { + + public TokenCLI tokenCLI; + + public TokenModifyCLI(TokenCLI tokenCLI) { + super("mod", "Modify token", tokenCLI); + this.tokenCLI = tokenCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Token ID> [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "user", true, "User ID"); + option.setArgName("User ID"); + option.setRequired(true); + 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 tokenID = cmdArgs[0]; + + TokenData tokenData = new TokenData(); + tokenData.setID(tokenID); + tokenData.setUserID(cmd.getOptionValue("user")); + + tokenData = tokenCLI.tokenClient.updateToken(tokenID, tokenData); + + MainCLI.printMessage("Modified token \"" + tokenID + "\""); + + TokenCLI.printToken(tokenData); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/token/TokenRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/token/TokenRemoveCLI.java new file mode 100644 index 000000000..4e986313c --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/token/TokenRemoveCLI.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.cmstools.token; + +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class TokenRemoveCLI extends CLI { + + public TokenCLI tokenCLI; + + public TokenRemoveCLI(TokenCLI tokenCLI) { + super("del", "Remove token", tokenCLI); + this.tokenCLI = tokenCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Token ID>", options); + } + + public void execute(String[] args) throws Exception { + + if (args.length != 1) { + printHelp(); + System.exit(1); + } + + String tokenID = args[0]; + + tokenCLI.tokenClient.removeToken(tokenID); + + MainCLI.printMessage("Deleted token \"" + tokenID + "\""); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/token/TokenShowCLI.java b/base/java-tools/src/com/netscape/cmstools/token/TokenShowCLI.java new file mode 100644 index 000000000..4f074f60f --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/token/TokenShowCLI.java @@ -0,0 +1,56 @@ +// --- 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.token; + +import com.netscape.certsrv.token.TokenData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class TokenShowCLI extends CLI { + + public TokenCLI tokenCLI; + + public TokenShowCLI(TokenCLI tokenCLI) { + super("show", "Show token", tokenCLI); + this.tokenCLI = tokenCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Token ID>", options); + } + + public void execute(String[] args) throws Exception { + + if (args.length != 1) { + printHelp(); + System.exit(1); + } + + String tokenID = args[0]; + + TokenData tokenData = tokenCLI.tokenClient.getToken(tokenID); + + MainCLI.printMessage("Token \"" + tokenID + "\""); + + TokenCLI.printToken(tokenData); + } +} |