diff options
author | Endi Sukma Dewata <edewata@redhat.com> | 2012-09-06 16:33:48 -0500 |
---|---|---|
committer | Endi Sukma Dewata <edewata@redhat.com> | 2012-10-18 10:06:54 -0500 |
commit | 168d95446c3a7ae8643128a51fa86dd326e3a6a8 (patch) | |
tree | adcadbfcb8947ed54e56628a7e92241566e70ac1 /base/java-tools | |
parent | d6634a7505df8358322b04b8892139195031e5eb (diff) | |
download | pki-168d95446c3a7ae8643128a51fa86dd326e3a6a8.tar.gz pki-168d95446c3a7ae8643128a51fa86dd326e3a6a8.tar.xz pki-168d95446c3a7ae8643128a51fa86dd326e3a6a8.zip |
Enabled authentication for security domain REST interface.
The REST interface for security domain has been refactored and
configured such that it requires authentication. A CLI has been
added to get an installation token.
Ticket #309
Diffstat (limited to 'base/java-tools')
3 files changed, 181 insertions, 0 deletions
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 44ccf9511..bcc3bb27e 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java @@ -33,6 +33,7 @@ import org.mozilla.jss.util.Password; import com.netscape.certsrv.client.ClientConfig; import com.netscape.cmstools.cert.CertCLI; import com.netscape.cmstools.group.GroupCLI; +import com.netscape.cmstools.system.SecurityDomainCLI; import com.netscape.cmstools.user.UserCLI; /** @@ -47,6 +48,7 @@ public class MainCLI extends CLI { addModule(new CertCLI(this)); addModule(new GroupCLI(this)); + addModule(new SecurityDomainCLI(this)); addModule(new UserCLI(this)); } diff --git a/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java b/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java new file mode 100644 index 000000000..93c4c4b63 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java @@ -0,0 +1,93 @@ +// --- 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) 2012 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.system; + +import java.util.Arrays; + +import org.apache.commons.lang.StringUtils; + +import com.netscape.certsrv.system.SecurityDomainClient; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class SecurityDomainCLI extends CLI { + + public MainCLI parent; + public SecurityDomainClient client; + + public SecurityDomainCLI(MainCLI parent) { + super("securitydomain", "Security domain commands"); + this.parent = parent; + + addModule(new SecurityDomainGetInstallTokenCLI(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 { + + client = new SecurityDomainClient(parent.config); + client.setVerbose(verbose); + + 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); + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainGetInstallTokenCLI.java b/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainGetInstallTokenCLI.java new file mode 100644 index 000000000..15b8def2a --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainGetInstallTokenCLI.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) 2012 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.system; + +import java.net.InetAddress; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.system.InstallToken; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class SecurityDomainGetInstallTokenCLI extends CLI { + + public SecurityDomainCLI parent; + + public SecurityDomainGetInstallTokenCLI(SecurityDomainCLI parent) { + super("get-install-token", "Get install token"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name, options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "hostname", true, "Hostname"); + option.setArgName("hostname"); + options.addOption(option); + + option = new Option(null, "subsystem", true, "Subsystem"); + option.setArgName("subsystem"); + 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 != 0) { + printHelp(); + System.exit(1); + } + + String hostname = cmd.getOptionValue("hostname"); + if (hostname == null) { + hostname = InetAddress.getLocalHost().getHostName(); + } + + String subsystem = cmd.getOptionValue("subsystem"); + + InstallToken token = parent.client.getInstallToken(hostname, subsystem); + + MainCLI.printMessage("Install token: \"" + token.getToken() + "\""); + } +} |