public DaemonOperation(DaemonOperationOptions options) { super(null); this.options = options; this.pidFile = new File(UserConfig.getUserConfigDir(), PID_FILE); }
private static void initUserConfig() { UserConfig.init(); }
public static String decrypt(String encryptedHexString) throws CipherException { byte[] encryptedBytes = StringUtil.fromHex(encryptedHexString); byte[] decryptedBytes = CipherUtil.decrypt(new ByteArrayInputStream(encryptedBytes), UserConfig.getConfigEncryptionKey()); return new String(decryptedBytes); }
public static void init() { if (userConfigDir == null) { initUserAppDirs(); initUserConfig(); initUserTrustStore(); initUserKeyStore(); } }
public static void generateNewKeyPairAndCertificate(String certificateCommonName) { try { logger.log(Level.INFO, "(Re-)generating keypair and certificate for hostname " + certificateCommonName + " ..."); // Generate key pair and certificate KeyPair keyPair = CipherUtil.generateRsaKeyPair(); X509Certificate certificate = CipherUtil.generateSelfSignedCertificate(certificateCommonName, keyPair); // Add key and certificate to key store UserConfig.getUserKeyStore().setKeyEntry(CipherParams.CERTIFICATE_IDENTIFIER, keyPair.getPrivate(), new char[0], new Certificate[]{certificate}); UserConfig.storeUserKeyStore(); // Add certificate to trust store (for CLI->API connection) UserConfig.getUserTrustStore().setCertificateEntry(CipherParams.CERTIFICATE_IDENTIFIER, certificate); UserConfig.storeTrustStore(); } catch (Exception e) { throw new RuntimeException("Unable to read key store or generate self-signed certificate.", e); } }
File purgefilePath = new File(UserConfig.getUserConfigDir(), PURGEFILE_FILENAME); logger.log(Level.INFO, "Plugin can NOT be uninstalled because class location not in " + UserConfig.getUserPluginLibDir()); result.setResultCode(PluginResultCode.NOK);
SSLContext sslContext = UserConfig.createUserSSLContext();
private boolean canUninstall(File pluginJarFile) { File globalUserPluginDir = UserConfig.getUserPluginLibDir(); return pluginJarFile != null && pluginJarFile.getAbsolutePath().startsWith(globalUserPluginDir.getAbsolutePath()); }
private static void initUserTrustStore() { userTrustStoreFile = new File(userConfigDir, USER_TRUSTSTORE_FILE); userTrustStore = initKeyStore(userTrustStoreFile); }
private boolean certificateCommonNameChanged(String certificateCommonName) { try { KeyStore userKeyStore = UserConfig.getUserKeyStore(); X509Certificate currentCertificate = (X509Certificate) userKeyStore.getCertificate(CipherParams.CERTIFICATE_IDENTIFIER); if (currentCertificate != null) { X500Name currentCertificateSubject = new JcaX509CertificateHolder(currentCertificate).getSubject(); RDN currentCertificateSubjectCN = currentCertificateSubject.getRDNs(BCStyle.CN)[0]; String currentCertificateSubjectCnStr = IETFUtils.valueToString(currentCertificateSubjectCN.getFirst().getValue()); if (!certificateCommonName.equals(currentCertificateSubjectCnStr)) { logger.log(Level.INFO, "- Certificate regeneration necessary: Cert common name in daemon config changed from " + currentCertificateSubjectCnStr + " to " + certificateCommonName + "."); return true; } } else { logger.log(Level.INFO, "- Certificate regeneration necessary, because no certificate found in key store."); return true; } return false; } catch (Exception e) { throw new RuntimeException("Cannot (re-)generate server certificate for hostname: " + certificateCommonName, e); } }
private void initLogHandlers(OptionSet options, OptionSpec<String> optionLog, OptionSpec<Void> optionLogPrint, OptionSpec<Void> optionDebug) throws SecurityException, IOException { // --log=<file> String logFilePattern = null; if (options.has(optionLog)) { if (!"-".equals(options.valueOf(optionLog))) { logFilePattern = options.valueOf(optionLog); } } else if (config != null && config.getLogDir().exists()) { logFilePattern = config.getLogDir() + File.separator + LOG_FILE_PATTERN; } else { logFilePattern = UserConfig.getUserLogDir() + File.separator + LOG_FILE_PATTERN; } if (logFilePattern != null) { Handler fileLogHandler = new FileHandler(logFilePattern, LOG_FILE_LIMIT, LOG_FILE_COUNT, true); fileLogHandler.setFormatter(new LogFormatter()); Logging.addGlobalHandler(fileLogHandler); } // --debug, add console handler if (options.has(optionDebug) || options.has(optionLogPrint) || (options.has(optionLog) && "-".equals(options.valueOf(optionLog)))) { Handler consoleLogHandler = new ConsoleHandler(); consoleLogHandler.setFormatter(new LogFormatter()); Logging.addGlobalHandler(consoleLogHandler); } }
SSLContext sslContext = UserConfig.createUserSSLContext();
private File installPlugin(File pluginJarFile, PluginInfo pluginInfo) throws IOException { File globalUserPluginDir = UserConfig.getUserPluginLibDir(); globalUserPluginDir.mkdirs(); File targetPluginJarFile = new File(globalUserPluginDir, String.format("syncany-plugin-%s-%s.jar", pluginInfo.getPluginId(), pluginInfo.getPluginVersion())); logger.log(Level.INFO, "Installing plugin from " + pluginJarFile + " to " + targetPluginJarFile + " ..."); FileUtils.copyFile(pluginJarFile, targetPluginJarFile); return targetPluginJarFile; }
private static void initUserKeyStore() { userKeyStoreFile = new File(userConfigDir, USER_KEYSTORE_FILE); userKeyStore = initKeyStore(userKeyStoreFile); }
public void enterLoop() throws IOException, ServiceAlreadyStartedException { File userAppDir = UserConfig.getUserConfigDir(); userAppDir.mkdirs(); controlFile.delete(); controlFile.createNewFile(); controlFile.deleteOnExit(); logger.log(Level.INFO, "Monitoring control file for commands at " + controlFile + " ..."); logger.log(Level.INFO, " (Note: This is a blocking operation. The 'main' thread is now blocked until '" + ControlCommand.SHUTDOWN + "' is received.)"); controlFileTailer.run(); // This blocks! }
public static void cleanUserConfig() throws Exception { Field userConfigDir = UserConfig.class.getDeclaredField("userConfigDir"); userConfigDir.setAccessible(true); userConfigDir.set(null, null); File tempFolder = TestFileUtil.createTempDirectoryInSystemTemp(); Field USER_APP_DIR_WINDOWS = UserConfig.class.getDeclaredField("USER_APP_DIR_WINDOWS"); USER_APP_DIR_WINDOWS.setAccessible(true); USER_APP_DIR_WINDOWS.set(null, tempFolder); Field USER_APP_DIR_UNIX_LIKE = UserConfig.class.getDeclaredField("USER_APP_DIR_UNIX_LIKE"); USER_APP_DIR_UNIX_LIKE.setAccessible(true); USER_APP_DIR_UNIX_LIKE.set(null, tempFolder); UserConfig.init(); } }
public static String encrypt(String decryptedPlainString) throws CipherException { InputStream plaintextInputStream = IOUtils.toInputStream(decryptedPlainString); byte[] encryptedBytes = CipherUtil.encrypt(plaintextInputStream, CipherSpecs.getDefaultCipherSpecs(), UserConfig.getConfigEncryptionKey()); return StringUtil.toHex(encryptedBytes); } }
public ControlServer() { this.controlFile = new File(UserConfig.getUserConfigDir(), CONTROL_FILE); this.controlFileTailer = new Tailer(controlFile, this, 1000, true); this.eventBus = LocalEventBus.getInstance(); }
File updatefilePath = new File(UserConfig.getUserConfigDir(), UPDATE_FILENAME);
private int runCommand(Command command, String commandName, String[] commandArgs) { File portFile = null; if (config != null) { portFile = config.getPortFile(); } File daemonPidFile = new File(UserConfig.getUserConfigDir(), DaemonOperation.PID_FILE); boolean localDirHandledInDaemonScope = portFile != null && portFile.exists(); boolean daemonRunning = PidFileUtil.isProcessRunning(daemonPidFile); boolean needsToRunInInitializedScope = command.getRequiredCommandScope() == CommandScope.INITIALIZED_LOCALDIR; boolean sendToRest = daemonRunning & localDirHandledInDaemonScope && needsToRunInInitializedScope; command.setOut(out); if (sendToRest) { if (command.canExecuteInDaemonScope()) { return sendToRest(command, commandName, commandArgs, portFile); } else { logger.log(Level.SEVERE, "Command not allowed when folder is daemon-managed: " + command.toString()); return showErrorAndExit("Command not allowed when folder is daemon-managed"); } } else { return runLocally(command, commandArgs); } }