>From 7eca0dd3f819588d53e71dea84ce23a21186c586 Mon Sep 17 00:00:00 2001 From: Fraser Tweedale Date: Wed, 23 Jul 2014 02:40:07 -0400 Subject: [PATCH] add "pki ca profile show-raw" CLI command --- .../dogtagpki/server/ca/rest/ProfileService.java | 47 +++++----- .../netscape/certsrv/profile/ProfileClient.java | 5 + .../netscape/certsrv/profile/ProfileResource.java | 8 +- .../com/netscape/cmstools/profile/ProfileCLI.java | 1 + .../cmstools/profile/ProfileShowRawCLI.java | 102 +++++++++++++++++++++ 5 files changed, 141 insertions(+), 22 deletions(-) create mode 100644 base/java-tools/src/com/netscape/cmstools/profile/ProfileShowRawCLI.java diff --git a/base/ca/src/org/dogtagpki/server/ca/rest/ProfileService.java b/base/ca/src/org/dogtagpki/server/ca/rest/ProfileService.java index cf0d4db7f8bed44baf6d2abbf70a61406ddb4b7c..9d851145f5e681f1044f81654130a2d0114ce36d 100644 --- a/base/ca/src/org/dogtagpki/server/ca/rest/ProfileService.java +++ b/base/ca/src/org/dogtagpki/server/ca/rest/ProfileService.java @@ -18,6 +18,7 @@ package org.dogtagpki.server.ca.rest; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URI; import java.security.Principal; @@ -163,9 +164,7 @@ public class ProfileService extends PKIService implements ProfileResource { return createOKResponse(infos); } - @Override - public Response retrieveProfile(String profileId) throws ProfileNotFoundException { - ProfileData data = null; + private IProfile getProfile(String profileId) throws ProfileNotFoundException { boolean visibleOnly = true; if (profileId == null) { @@ -185,24 +184,12 @@ public class ProfileService extends PKIService implements ProfileResource { visibleOnly = false; } - Enumeration profileIds = ps.getProfileIds(); - - IProfile profile = null; - if (profileIds != null) { - while (profileIds.hasMoreElements()) { - String id = profileIds.nextElement(); - - if (id.equals(profileId)) { - - try { - profile = ps.getProfile(profileId); - } catch (EProfileException e) { - e.printStackTrace(); - throw new ProfileNotFoundException(profileId); - } - break; - } - } + IProfile profile; + try { + profile = ps.getProfile(profileId); + } catch (EProfileException e) { + e.printStackTrace(); + throw new ProfileNotFoundException(profileId); } if (profile == null) { @@ -213,6 +200,14 @@ public class ProfileService extends PKIService implements ProfileResource { throw new ProfileNotFoundException(profileId); } + return profile; + } + + @Override + public Response retrieveProfile(String profileId) throws ProfileNotFoundException { + IProfile profile = getProfile(profileId); + + ProfileData data = null; try { data = createProfileData(profileId); } catch (EBaseException e) { @@ -228,6 +223,16 @@ public class ProfileService extends PKIService implements ProfileResource { return createOKResponse(data); } + @Override + public Response retrieveProfileRaw(String profileId) + throws ProfileNotFoundException { + IProfile profile = getProfile(profileId); + ByteArrayOutputStream data = new ByteArrayOutputStream(); + profile.getConfigStore().save(data, null); + return createOKResponse(data.toByteArray()); + } + + public ProfileData createProfileData(String profileId) throws EBaseException { IProfile profile; diff --git a/base/common/src/com/netscape/certsrv/profile/ProfileClient.java b/base/common/src/com/netscape/certsrv/profile/ProfileClient.java index 51d159aca687719a2dace939da5b09c4809872ec..94b087230d46db342dff365da3c57734d98f02dc 100644 --- a/base/common/src/com/netscape/certsrv/profile/ProfileClient.java +++ b/base/common/src/com/netscape/certsrv/profile/ProfileClient.java @@ -45,6 +45,11 @@ public class ProfileClient extends Client { return client.getEntity(response, ProfileData.class); } + public byte[] retrieveProfileRaw(String id) { + Response response = profileClient.retrieveProfileRaw(id); + return client.getEntity(response, byte[].class); + } + public ProfileDataInfos listProfiles(Integer start, Integer size) { Response response = profileClient.listProfiles(start, size); return client.getEntity(response, ProfileDataInfos.class); diff --git a/base/common/src/com/netscape/certsrv/profile/ProfileResource.java b/base/common/src/com/netscape/certsrv/profile/ProfileResource.java index 87449b27e749c6088f65b53192eb5ac101263f1e..d0b84eeb61688fe4bbc107c2bdd368f848e568cd 100644 --- a/base/common/src/com/netscape/certsrv/profile/ProfileResource.java +++ b/base/common/src/com/netscape/certsrv/profile/ProfileResource.java @@ -31,6 +31,12 @@ public interface ProfileResource { @ACLMapping("profiles.read") public Response retrieveProfile(@PathParam("id") String id); + @GET + @Path("{id}/raw") + @ClientResponseType(entityType=byte[].class) + @ACLMapping("profiles.read") + public Response retrieveProfileRaw(@PathParam("id") String id); + @POST @ClientResponseType(entityType=ProfileData.class) @ACLMapping("profiles.create") @@ -53,4 +59,4 @@ public interface ProfileResource { @ClientResponseType(entityType=Void.class) @ACLMapping("profiles.delete") public Response deleteProfile(@PathParam("id") String id); -} \ No newline at end of file +} diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java index 732b597afd1dbb5b9440d451f34b2f39e20fb904..a57c741946b40f0cc02a10acaf6d895081b151d9 100644 --- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java @@ -30,6 +30,7 @@ public class ProfileCLI extends CLI { addModule(new ProfileFindCLI(this)); addModule(new ProfileShowCLI(this)); + addModule(new ProfileShowRawCLI(this)); addModule(new ProfileAddCLI(this)); addModule(new ProfileModifyCLI(this)); addModule(new ProfileRemoveCLI(this)); diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowRawCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowRawCLI.java new file mode 100644 index 0000000000000000000000000000000000000000..a15c4e08629a759a2cd66ecfa79578fd9eab6e2e --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowRawCLI.java @@ -0,0 +1,102 @@ +//--- 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) 2014 Red Hat, Inc. +//All rights reserved. +//--- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.profile; + +import java.io.FileOutputStream; +import java.util.Arrays; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.ParseException; + +import com.netscape.certsrv.profile.ProfileData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class ProfileShowRawCLI extends CLI { + + public ProfileCLI profileCLI; + + public ProfileShowRawCLI(ProfileCLI profileCLI) { + super("show-raw", "Show profiles (config-store format)", profileCLI); + this.profileCLI = profileCLI; + + createOptions(); + } + + public void printHelp() { + formatter.printHelp(getFullName() + " [OPTIONS...]", options); + } + + public void createOptions() { + Option option = new Option(null, "output", true, "Output filename"); + option.setArgName("filename"); + options.addOption(option); + } + + public void execute(String[] args) throws Exception { + // Always check for "--help" prior to parsing + if (Arrays.asList(args).contains("--help")) { + // Display usage + printHelp(); + System.exit(0); + } + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + } catch (ParseException e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(-1); + } + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length < 1) { + System.err.println("Error: No Profile ID specified."); + printHelp(); + System.exit(-1); + } + + String profileId = cmdArgs[0]; + + String filename = null; + if (cmd.hasOption("output")) { + filename = cmd.getOptionValue("output"); + + if (filename == null || filename.trim().length() == 0) { + System.err.println("Error: Missing output file name."); + printHelp(); + System.exit(-1); + } + } + + byte[] profileConfig = profileCLI.profileClient.retrieveProfileRaw(profileId); + + MainCLI.printMessage("Profile \"" + profileId + "\""); + + if (filename != null) { + (new FileOutputStream(filename)).write(profileConfig); + } else { + System.out.println(new String(profileConfig)); + } + } +} -- 1.9.3