private void resetDefaultRealm() throws IOException { InputStream templateResource = new FileInputStream( getKrb5conf().getAbsolutePath()); String content = IOUtil.readInput(templateResource); content = content.replaceAll("default_realm = .*\n", "default_realm = " + getRealm() + "\n"); IOUtil.writeFile(content, getKrb5conf()); }
private void persistToFile() throws KrbException { String newJsonContent = gson.toJson(identities); try { File newJsonKdbFile = File.createTempFile("kerby-kdb", ".json", jsonKdbFile.getParentFile()); IOUtil.writeFile(newJsonContent, newJsonKdbFile); boolean delete = jsonKdbFile.delete(); if (!delete) { throw new RuntimeException("File delete error!"); } boolean rename = newJsonKdbFile.renameTo(jsonKdbFile); if (!rename) { throw new RuntimeException("File rename error!"); } kdbFileUpdateTime = jsonKdbFile.lastModified(); } catch (IOException e) { LOG.error("Error occurred while writing identities to file: " + jsonKdbFile); throw new KrbException("Failed to write file", e); } }
/** * Read an input stream and return the content as string assuming UTF8. * @param in The input stream * @return The read String * @return The content * @throws IOException e */ public static String readInput(InputStream in) throws IOException { byte[] content = readInputStream(in); return Utf8.toString(content); }
private void load() throws KrbException { LOG.info("Loading the identities from json file."); long nowTimeStamp = jsonKdbFile.lastModified(); String reloadedJsonContent; if (lock.tryLock()) { try { try { reloadedJsonContent = IOUtil.readFile(jsonKdbFile); } catch (IOException e) { throw new KrbException("Failed to read file", e); } Map<String, KrbIdentity> reloadedEntries = gson.fromJson(reloadedJsonContent, new TypeToken<HashMap<String, KrbIdentity>>() { }.getType()); if (reloadedEntries != null) { identities.clear(); identities.putAll(reloadedEntries); } kdbFileUpdateTime = nowTimeStamp; } finally { lock.unlock(); } } }
private void persistToFile() throws KrbException { String newJsonContent = gson.toJson(identities); try { File newJsonKdbFile = File.createTempFile("kerby-kdb", ".json", jsonKdbFile.getParentFile()); IOUtil.writeFile(newJsonContent, newJsonKdbFile); boolean delete = jsonKdbFile.delete(); if (!delete) { throw new RuntimeException("File delete error!"); } boolean rename = newJsonKdbFile.renameTo(jsonKdbFile); if (!rename) { throw new RuntimeException("File rename error!"); } kdbFileUpdateTime = jsonKdbFile.lastModified(); } catch (IOException e) { LOG.error("Error occurred while writing identities to file: " + jsonKdbFile); throw new KrbException("Failed to write file", e); } }
/** * Read an input stream and return the content as string assuming UTF8. * @param in The input stream * @return The read String * @return The content * @throws IOException e */ public static String readInput(InputStream in) throws IOException { byte[] content = readInputStream(in); return Utf8.toString(content); }
private void load() throws KrbException { LOG.info("Loading the identities from json file."); long nowTimeStamp = jsonKdbFile.lastModified(); String reloadedJsonContent; if (lock.tryLock()) { try { try { reloadedJsonContent = IOUtil.readFile(jsonKdbFile); } catch (IOException e) { throw new KrbException("Failed to read file", e); } Map<String, KrbIdentity> reloadedEntries = gson.fromJson(reloadedJsonContent, new TypeToken<HashMap<String, KrbIdentity>>() { }.getType()); if (reloadedEntries != null) { identities.clear(); identities.putAll(reloadedEntries); } kdbFileUpdateTime = nowTimeStamp; } finally { lock.unlock(); } } }
private File createClientSSLConfig(String trustStorePath, String trustStorePassword, String keyStorePassword) throws HasException { String resourcePath = "/ssl-client.conf.template"; try (InputStream templateResource = getClass().getResourceAsStream(resourcePath)) { File sslConfigFile = new File("ssl-client.conf"); String content = IOUtil.readInput(templateResource); content = content.replaceAll("_location_", trustStorePath); content = content.replaceAll("_password_", trustStorePassword); content = content.replaceAll("_keyPassword_", keyStorePassword); IOUtil.writeFile(content, sslConfigFile); return sslConfigFile; } catch (IOException e) { throw new HasException("Failed to create client ssl configuration file", e); } }
@BeforeClass public static void setup() throws IOException { KRB5_CONF_ROOT_DIR.mkdir(); File krb5ConfFile = new File(KRB5_CONF_ROOT_DIR, "krb5.conf"); krb5ConfFile.createNewFile(); String content = "[libdefaults]\n" + " default_realm = APACHE.ORG\n" + " udp_preference_limit = 1\n"+ " extra_addresses = 127.0.0.1\n" + "[realms]\n" + " APACHE.ORG = {\n" + " admin_server = localhost:88\n" + " kdc = localhost:88\n}\n" + "[domain_realm]\n" + " localhost = APACHE.ORG"; IOUtil.writeFile(content, krb5ConfFile); System.setProperty(KRB5_CONF, krb5ConfFile.getAbsolutePath()); }
/** * Read a file and return the content as string assuming UTF8. * @param file The file to read * @return The content as a UTF-8 String * @throws IOException If an error occurred */ public static String readFile(File file) throws IOException { long len = 0; if (file.length() >= Integer.MAX_VALUE) { throw new IOException("Too large file, unexpected!"); } else { len = file.length(); } byte[] buf = new byte[(int) len]; InputStream is = Files.newInputStream(file.toPath()); readInputStream(is, buf); return Utf8.toString(buf); }
private File createClientSSLConfig(String trustStorePath, String trustStorePassword, String keyStorePassword) throws HasException { String resourcePath = "/ssl-client.conf.template"; try (InputStream templateResource = getClass().getResourceAsStream(resourcePath)) { File sslConfigFile = new File("ssl-client.conf"); String content = IOUtil.readInput(templateResource); content = content.replaceAll("_location_", trustStorePath); content = content.replaceAll("_password_", trustStorePassword); content = content.replaceAll("_keyPassword_", keyStorePassword); IOUtil.writeFile(content, sslConfigFile); return sslConfigFile; } catch (IOException e) { throw new HasException("Failed to create client ssl configuration file", e); } }
/** * Read a file and return the content as string assuming UTF8. * @param file The file to read * @return The content as a UTF-8 String * @throws IOException If an error occurred */ public static String readFile(File file) throws IOException { long len = 0; if (file.length() >= Integer.MAX_VALUE) { throw new IOException("Too large file, unexpected!"); } else { len = file.length(); } byte[] buf = new byte[(int) len]; InputStream is = Files.newInputStream(file.toPath()); readInputStream(is, buf); return Utf8.toString(buf); }
/** * Create ssl configuration file for client. * */ private void createClientSSLConfig(String password) throws HasException { String resourcePath = "/ssl-client.conf.template"; try (InputStream templateResource = getClass().getResourceAsStream(resourcePath)) { String content = IOUtil.readInput(templateResource); content = content.replaceAll("_location_", clientConfigFolder.getAbsolutePath() + "/truststore.jks"); content = content.replaceAll("_password_", password); IOUtil.writeFile(content, new File(clientConfigFolder + "/ssl-client.conf")); } catch (IOException e) { throw new HasException("Failed to create client ssl configuration file. " + e.getMessage()); } } }
/** * Create ssl configuration file for client. * */ private void createClientSSLConfig(String password) throws HasException { String resourcePath = "/ssl-client.conf.template"; try (InputStream templateResource = getClass().getResourceAsStream(resourcePath)) { String content = IOUtil.readInput(templateResource); content = content.replaceAll("_location_", clientConfigFolder.getAbsolutePath() + "/truststore.jks"); content = content.replaceAll("_password_", password); IOUtil.writeFile(content, new File(clientConfigFolder + "/ssl-client.conf")); } catch (IOException e) { throw new HasException("Failed to create client ssl configuration file. " + e.getMessage()); } } }
String content; try (InputStream templateResource = getClass().getResourceAsStream(resourcePath)) { content = IOUtil.readInput(templateResource); } catch (IOException e) { throw new HasException("Read template resource failed. " + e.getMessage()); IOUtil.writeFile(content, confFile); } catch (IOException e) { throw new HasException("Write content to conf file failed. " + e.getMessage());
/** * Read in has-server.conf and create has-client.conf. * * @return has conf file * @throws IOException e * @throws HasException e */ public File generateHasConf() throws HasException, IOException { Map<String, String> kdcConf = getKdcConf(); String servers = kdcConf.get("servers"); File confFile = new File(getConfDir().getAbsolutePath(), "has-server.conf"); HasConfig hasConfig = HasUtil.getHasConfig(confFile); if (hasConfig != null) { String defaultValue = hasConfig.getHttpsHost(); String content; try (InputStream templateResource = new FileInputStream(confFile)) { content = IOUtil.readInput(templateResource); } content = content.replaceFirst(Pattern.quote(defaultValue), servers); File hasFile = new File(confDir, "has-client.conf"); IOUtil.writeFile(content, hasFile); return hasFile; } else { throw new HasException("has-server.conf not found. "); } }
String content; try (InputStream templateResource = getClass().getResourceAsStream(resourcePath)) { content = IOUtil.readInput(templateResource); } catch (IOException e) { throw new HasException("Read template resource failed. " + e.getMessage()); IOUtil.writeFile(content, confFile); } catch (IOException e) { throw new HasException("Write content to conf file failed. " + e.getMessage());
/** * Read in has-server.conf and create has-client.conf. * * @return has conf file * @throws IOException e * @throws HasException e */ public File generateHasConf() throws HasException, IOException { Map<String, String> kdcConf = getKdcConf(); String servers = kdcConf.get("servers"); File confFile = new File(getConfDir().getAbsolutePath(), "has-server.conf"); HasConfig hasConfig = HasUtil.getHasConfig(confFile); if (hasConfig != null) { String defaultValue = hasConfig.getHttpsHost(); String content; try (InputStream templateResource = new FileInputStream(confFile)) { content = IOUtil.readInput(templateResource); } content = content.replaceFirst(Pattern.quote(defaultValue), servers); File hasFile = new File(confDir, "has-client.conf"); IOUtil.writeFile(content, hasFile); return hasFile; } else { throw new HasException("has-server.conf not found. "); } }