52
52
* Stores and maintains reading and writing of certificate related files
53
53
*/
54
54
public class CertificateManager {
55
+ static List <Path > SAVE_LOCATIONS = new ArrayList <>();
55
56
static {
56
57
// Workaround for JDK-8266929
57
58
// See also https://github.com/qzind/tray/issues/814
58
59
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 );
59
71
}
60
72
private static final Logger log = LogManager .getLogger (CertificateManager .class );
61
73
@@ -336,42 +348,25 @@ public Properties writeKeystore(Properties props, KeyPairWrapper.Type type) thro
336
348
return props ;
337
349
}
338
350
339
- public static File getWritableLocation (String ... subDirs ) throws IOException {
351
+ public static File getWritableLocation (String ... suffixes ) throws IOException {
340
352
// Get an array of preferred directories
341
353
ArrayList <Path > locs = new ArrayList <>();
342
354
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 );
362
357
// Last, fallback on a directory we won't ever see again :/
363
358
locs .add (TEMP_DIR );
364
359
} 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
+ }
369
364
// 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 ));
371
366
}
372
367
373
368
// Find a suitable write location
374
- File path = null ;
369
+ File path ;
375
370
for (Path loc : locs ) {
376
371
if (loc == null ) continue ;
377
372
boolean isPreferred = locs .indexOf (loc ) == 0 ;
@@ -392,20 +387,20 @@ public static File getWritableLocation(String ... subDirs) throws IOException {
392
387
393
388
public static Properties loadProperties (KeyPairWrapper ... keyPairs ) {
394
389
log .info ("Try to find SSL properties file..." );
395
- Path [] locations = { SystemUtilities . getJarParentPath (), SHARED_DIR , USER_DIR };
390
+
396
391
397
392
Properties props = null ;
398
- for (Path location : locations ) {
399
- if (location == null ) continue ;
393
+ for (Path loc : SAVE_LOCATIONS ) {
394
+ if (loc == null ) continue ;
400
395
try {
401
396
for (KeyPairWrapper keyPair : keyPairs ) {
402
- props = loadKeyPair (keyPair , location , props );
397
+ props = loadKeyPair (keyPair , loc , props );
403
398
}
404
399
// We've loaded without Exception, return
405
- log .info ("Found {}/{}.properties" , location , Constants .PROPS_FILE );
400
+ log .info ("Found {}/{}.properties" , loc , Constants .PROPS_FILE );
406
401
return props ;
407
402
} 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 );
409
404
}
410
405
}
411
406
log .info ("Could not get SSL properties from file." );
0 commit comments