Skip to content

Commit d10fc7c

Browse files
authored
Consolidate and simplify SSL cert save logic (#1223)
- Consolidate and simplify cert save logic - Moves sandbox info to about/security info
1 parent e4f3279 commit d10fc7c

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

src/qz/common/AboutInfo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.apache.logging.log4j.Logger;
1515
import qz.installer.certificate.KeyPairWrapper;
1616
import qz.installer.certificate.CertificateManager;
17+
import qz.utils.MacUtilities;
1718
import qz.utils.StringUtilities;
1819
import qz.utils.SystemUtilities;
1920
import qz.ws.PrintSocketServer;
@@ -96,7 +97,8 @@ private static JSONObject environment() throws JSONException {
9697
.put("java (location)", System.getProperty("java.home"))
9798
.put("java (vendor)", Constants.JAVA_VENDOR)
9899
.put("uptime", DurationFormatUtils.formatDurationWords(uptime, true, false))
99-
.put("uptimeMillis", uptime);
100+
.put("uptimeMillis", uptime)
101+
.put("sandbox", SystemUtilities.isMac() && MacUtilities.isSandboxed());
100102

101103
return environment;
102104
}

src/qz/installer/certificate/CertificateManager.java

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,22 @@
5252
* Stores and maintains reading and writing of certificate related files
5353
*/
5454
public class CertificateManager {
55+
static List<Path> SAVE_LOCATIONS = new ArrayList<>();
5556
static {
5657
// Workaround for JDK-8266929
5758
// See also https://github.com/qzind/tray/issues/814
5859
SystemUtilities.clearAlgorithms();
60+
61+
// Skip shared location if running from IDE or build directory
62+
// Prevents corrupting the version installed per https://github.com/qzind/tray/issues/1200
63+
if(SystemUtilities.isJar() && SystemUtilities.isInstalled()) {
64+
// Skip install location if running from sandbox (must remain sealed)
65+
if(!SystemUtilities.isMac() || !MacUtilities.isSandboxed()) {
66+
SAVE_LOCATIONS.add(SystemUtilities.getJarParentPath());
67+
}
68+
SAVE_LOCATIONS.add(SHARED_DIR);
69+
}
70+
SAVE_LOCATIONS.add(USER_DIR);
5971
}
6072
private static final Logger log = LogManager.getLogger(CertificateManager.class);
6173

@@ -336,42 +348,25 @@ public Properties writeKeystore(Properties props, KeyPairWrapper.Type type) thro
336348
return props;
337349
}
338350

339-
public static File getWritableLocation(String ... subDirs) throws IOException {
351+
public static File getWritableLocation(String ... suffixes) throws IOException {
340352
// Get an array of preferred directories
341353
ArrayList<Path> locs = new ArrayList<>();
342354

343-
// Sandbox is only supported on macOS currently
344-
boolean sandboxed = false;
345-
if(SystemUtilities.isMac()) {
346-
sandboxed = MacUtilities.isSandboxed();
347-
//todo move to about security table or delete
348-
log.debug("Running in a sandbox: {}", sandboxed);
349-
}
350-
351-
// Sandboxed installations must remain sealed, don't write to them
352-
if (subDirs.length == 0 && !sandboxed) {
353-
// Assume root directory is next to jar (e.g. qz-tray.properties)
354-
Path appPath = SystemUtilities.getJarParentPath();
355-
// Handle null path, such as running from IDE
356-
if(appPath != null) {
357-
locs.add(appPath);
358-
}
359-
// Fallback on a directory we can normally write to
360-
locs.add(SHARED_DIR);
361-
locs.add(USER_DIR);
355+
if (suffixes.length == 0) {
356+
locs.addAll(SAVE_LOCATIONS);
362357
// Last, fallback on a directory we won't ever see again :/
363358
locs.add(TEMP_DIR);
364359
} else {
365-
// Assume non-root directories are for ssl (e.g. certs, keystores)
366-
locs.add(Paths.get(SHARED_DIR.toString(), subDirs));
367-
// Fallback on a directory we can normally write to
368-
locs.add(Paths.get(USER_DIR.toString(), subDirs));
360+
// Same as above, but with suffixes added (usually "ssl")
361+
for(Path saveLocation : SAVE_LOCATIONS) {
362+
locs.add(Paths.get(saveLocation.toString(), suffixes));
363+
}
369364
// Last, fallback on a directory we won't ever see again :/
370-
locs.add(Paths.get(TEMP_DIR.toString(), subDirs));
365+
locs.add(Paths.get(TEMP_DIR.toString(), suffixes));
371366
}
372367

373368
// Find a suitable write location
374-
File path = null;
369+
File path;
375370
for(Path loc : locs) {
376371
if (loc == null) continue;
377372
boolean isPreferred = locs.indexOf(loc) == 0;
@@ -392,20 +387,20 @@ public static File getWritableLocation(String ... subDirs) throws IOException {
392387

393388
public static Properties loadProperties(KeyPairWrapper... keyPairs) {
394389
log.info("Try to find SSL properties file...");
395-
Path[] locations = {SystemUtilities.getJarParentPath(), SHARED_DIR, USER_DIR};
390+
396391

397392
Properties props = null;
398-
for(Path location : locations) {
399-
if (location == null) continue;
393+
for(Path loc : SAVE_LOCATIONS) {
394+
if (loc == null) continue;
400395
try {
401396
for(KeyPairWrapper keyPair : keyPairs) {
402-
props = loadKeyPair(keyPair, location, props);
397+
props = loadKeyPair(keyPair, loc, props);
403398
}
404399
// We've loaded without Exception, return
405-
log.info("Found {}/{}.properties", location, Constants.PROPS_FILE);
400+
log.info("Found {}/{}.properties", loc, Constants.PROPS_FILE);
406401
return props;
407402
} catch(Exception ignore) {
408-
log.warn("Properties couldn't be loaded at {}, trying fallback...", location, ignore);
403+
log.warn("Properties couldn't be loaded at {}, trying fallback...", loc, ignore);
409404
}
410405
}
411406
log.info("Could not get SSL properties from file.");

0 commit comments

Comments
 (0)