>From c9072b0908eae0fc6e2c1b735cda67147cf0e13f Mon Sep 17 00:00:00 2001 From: Fraser Tweedale Date: Fri, 19 Sep 2014 02:45:48 -0400 Subject: [PATCH 10/10] Change ProfileClient to use Properties for the "raw" format Also update the 'ca-profile-edit' command to exit prior to editing when the profile is not disabled, so that the user doesn't find this out *after* editing the profile. --- .../netscape/certsrv/profile/ProfileClient.java | 43 ++++++++++++++++++---- .../netscape/cmstools/profile/ProfileAddCLI.java | 12 ++---- .../netscape/cmstools/profile/ProfileEditCLI.java | 19 +++++++--- .../cmstools/profile/ProfileModifyCLI.java | 13 +++---- .../netscape/cmstools/profile/ProfileShowCLI.java | 7 ++-- 5 files changed, 61 insertions(+), 33 deletions(-) diff --git a/base/common/src/com/netscape/certsrv/profile/ProfileClient.java b/base/common/src/com/netscape/certsrv/profile/ProfileClient.java index f3ffd29e03c1da39518e3928eab7a781ec38eaff..7f0b08f0e40d9a5314bcf741c2dc2fc514802ad5 100644 --- a/base/common/src/com/netscape/certsrv/profile/ProfileClient.java +++ b/base/common/src/com/netscape/certsrv/profile/ProfileClient.java @@ -17,10 +17,15 @@ //--- END COPYRIGHT BLOCK --- package com.netscape.certsrv.profile; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.net.URISyntaxException; +import java.util.Properties; import javax.ws.rs.core.Response; +import com.netscape.certsrv.base.PKIException; import com.netscape.certsrv.client.Client; import com.netscape.certsrv.client.PKIClient; @@ -45,9 +50,9 @@ public class ProfileClient extends Client { return client.getEntity(response, ProfileData.class); } - public byte[] retrieveProfileRaw(String id) { + public Properties retrieveProfileRaw(String id) { Response response = profileClient.retrieveProfileRaw(id); - return client.getEntity(response, byte[].class); + return byteArrayToProperties(client.getEntity(response, byte[].class)); } public ProfileDataInfos listProfiles(Integer start, Integer size) { @@ -70,9 +75,10 @@ public class ProfileClient extends Client { return client.getEntity(response, ProfileData.class); } - public byte[] createProfileRaw(byte[] data) { - Response response = profileClient.createProfileRaw(data); - return client.getEntity(response, byte[].class); + public Properties createProfileRaw(Properties properties) { + Response response = + profileClient.createProfileRaw(propertiesToByteArray(properties)); + return byteArrayToProperties(client.getEntity(response, byte[].class)); } public ProfileData modifyProfile(ProfileData data) { @@ -80,13 +86,34 @@ public class ProfileClient extends Client { return client.getEntity(response, ProfileData.class); } - public byte[] modifyProfileRaw(String profileId, byte[] data) { - Response response = profileClient.modifyProfileRaw(profileId, data); - return client.getEntity(response, byte[].class); + public Properties modifyProfileRaw(String profileId, Properties properties) { + Response response = + profileClient.modifyProfileRaw(profileId, propertiesToByteArray(properties)); + return byteArrayToProperties(client.getEntity(response, byte[].class)); } public void deleteProfile(String id) { Response response = profileClient.deleteProfile(id); client.getEntity(response, Void.class); } + + private Properties byteArrayToProperties(byte[] data) throws PKIException { + Properties properties = new Properties(); + try { + properties.load(new ByteArrayInputStream(data)); + } catch (IOException e) { + throw new PKIException("Failed to decode profile Properties: " + e.toString()); + } + return properties; + } + + private byte[] propertiesToByteArray(Properties properties) throws PKIException { + ByteArrayOutputStream data = new ByteArrayOutputStream(); + try { + properties.store(data, null); + } catch (IOException e) { + throw new PKIException("Failed to encode profile Properties: " + e.toString()); + } + return data.toByteArray(); + } } diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java index 74690810c20d5b8a2271f46ba2877f15aba6e001..82adf644dc2044480ed79f92e569e376195799bd 100644 --- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java @@ -1,6 +1,5 @@ package com.netscape.cmstools.profile; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -69,18 +68,15 @@ public class ProfileAddCLI extends CLI { try { if (cmd.hasOption("raw")) { - byte[] data = Files.readAllBytes(Paths.get(filename)); - Properties cs = new Properties(); - cs.load(new ByteArrayInputStream(data)); - String profileId = cs.getProperty("profileId"); + Properties properties = new Properties(); + properties.load(Files.newInputStream(Paths.get(filename))); + String profileId = properties.getProperty("profileId"); if (profileId == null) { System.err.println("Error: Missing profileId property in profile data."); System.exit(-1); } - byte[] profileConfig = - profileCLI.profileClient.createProfileRaw(data); - System.out.println(new String(profileConfig)); + profileCLI.profileClient.createProfileRaw(properties).store(System.out, null); MainCLI.printMessage("Added profile " + profileId); } else { ProfileData data = ProfileCLI.readProfileFromFile(filename); diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java index 5d8b9a50eb1fd23199b4e893906d5170e1ff791e..f8ce89fb18544f774de48c9027a014bd8d450e02 100644 --- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java @@ -22,6 +22,7 @@ import java.lang.ProcessBuilder; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; +import java.util.Properties; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.ParseException; @@ -70,9 +71,14 @@ public class ProfileEditCLI extends CLI { String profileId = cmdArgs[0]; // read profile into temporary file - byte[] orig = profileCLI.profileClient.retrieveProfileRaw(profileId); + Properties orig = profileCLI.profileClient.retrieveProfileRaw(profileId); + String enabled = orig.getProperty("enable"); + if (enabled == null || !enabled.equalsIgnoreCase("false")) { + System.err.println("Error: Cannot edit profile. Profile must be disabled."); + System.exit(-1); + } Path tempFile = Files.createTempFile("pki", ".cfg"); - Files.write(tempFile, orig); + orig.store(Files.newOutputStream(tempFile), null); // invoke editor on temporary file String editor = System.getenv("EDITOR"); @@ -91,12 +97,13 @@ public class ProfileEditCLI extends CLI { } // read data from temporary file and modify if changed - byte[] cur = Files.readAllBytes(tempFile); + Properties cur = new Properties(); + cur.load(Files.newInputStream(tempFile)); Files.delete(tempFile); - String curString = new String(cur); - if (!curString.equals(new String(orig))) { + + if (!cur.equals(orig)) { profileCLI.profileClient.modifyProfileRaw(profileId, cur); } - System.out.println(curString); + cur.store(System.out, null); } } diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java index 1cf1991d6a22887ce141f317094bdf9209311a44..6234bb8c6f1b7d3592bd2b9cd629c4717e8a847e 100644 --- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java @@ -1,6 +1,5 @@ package com.netscape.cmstools.profile; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -69,18 +68,16 @@ public class ProfileModifyCLI extends CLI { try { if (cmd.hasOption("raw")) { - byte[] data = Files.readAllBytes(Paths.get(filename)); - Properties cs = new Properties(); - cs.load(new ByteArrayInputStream(data)); - String profileId = cs.getProperty("profileId"); + Properties properties = new Properties(); + properties.load(Files.newInputStream(Paths.get(filename))); + String profileId = properties.getProperty("profileId"); if (profileId == null) { System.err.println("Error: Missing profileId property in profile data."); System.exit(-1); } - byte[] profileConfig = - profileCLI.profileClient.modifyProfileRaw(profileId, data); - System.out.println(new String(profileConfig)); + profileCLI.profileClient.modifyProfileRaw(profileId, properties).store(System.out, null); + MainCLI.printMessage("Modified profile " + profileId); } else { ProfileData data = ProfileCLI.readProfileFromFile(filename); data = profileCLI.profileClient.modifyProfile(data); diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java index 7776cbd86a0ede6d2b19a9adb3618dc4652bd8ae..2dd627145b66a7ece890cba9922e8d4ec2e27503 100644 --- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java @@ -2,6 +2,7 @@ package com.netscape.cmstools.profile; import java.io.FileOutputStream; import java.util.Arrays; +import java.util.Properties; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; @@ -77,13 +78,13 @@ public class ProfileShowCLI extends CLI { MainCLI.printMessage("Profile \"" + profileId + "\""); if (cmd.hasOption("raw")) { - byte[] profileConfig = profileCLI.profileClient.retrieveProfileRaw(profileId); + Properties profileConfig = profileCLI.profileClient.retrieveProfileRaw(profileId); if (filename != null) { - (new FileOutputStream(filename)).write(profileConfig); + profileConfig.store(new FileOutputStream(filename), null); MainCLI.printMessage("Saved profile " + profileId + " to " + filename); } else { - System.out.println(new String(profileConfig)); + profileConfig.store(System.out, null); } } else { ProfileData profileData = profileCLI.profileClient.retrieveProfile(profileId); -- 1.9.3