static CliResult writeToOutputFile(Config config, Path outputPath) throws IOException { System.out.printf("Saving config to %s", outputPath); System.out.println(); JaxbUtil.marshalWithNoValidation(config, System.out); System.out.println(); try (OutputStream outputStream = Files.newOutputStream(outputPath)) { JaxbUtil.marshal(config, outputStream); System.out.printf("Saved config to %s", outputPath); System.out.println(); return new CliResult(0, false, config); } catch (ConstraintViolationException validationException) { validationException.getConstraintViolations() .stream() .map(cv -> "Warning: " + cv.getMessage() + " on property " + cv.getPropertyPath()) .forEach(System.err::println); Files.write(outputPath, JaxbUtil.marshalToStringNoValidation(config).getBytes()); System.out.printf("Saved config to %s", outputPath); System.out.println(); return new CliResult(2, false, config); } }
private Optional<ArgonOptions> argonOptions(final CommandLine commandLine) throws IOException { if (commandLine.hasOption("keygenconfig")) { final String pathName = commandLine.getOptionValue("keygenconfig"); final InputStream configStream = Files.newInputStream(Paths.get(pathName)); final ArgonOptions argonOptions = JaxbUtil.unmarshal(configStream, ArgonOptions.class); return Optional.of(argonOptions); } return Optional.empty(); }
private static void output(CommandLine commandLine, Config config) throws IOException { if (commandLine.hasOption("output")) { final Path outputConfigFile = Paths.get(commandLine.getOptionValue("output")); try (OutputStream out = Files.newOutputStream(outputConfigFile, CREATE_NEW)) { JaxbUtil.marshal(config, out); } } else { JaxbUtil.marshal(config, System.out); } } }
@Override public Optional parse(final CommandLine commandLine) throws IOException { if (commandLine.hasOption("updatepassword")) { final ArgonOptions argonOptions = argonOptions(commandLine); final List<String> passwords = passwords(commandLine); final Path keypath = privateKeyPath(commandLine); final KeyDataConfig keyDataConfig = JaxbUtil.unmarshal(Files.newInputStream(keypath), KeyDataConfig.class); final PrivateKey privateKey = this.getExistingKey(keyDataConfig, passwords); final String newPassword = passwordReader.requestUserPassword(); final KeyDataConfig updatedKey; if(newPassword.isEmpty()) { final PrivateKeyData privateKeyData = new PrivateKeyData(privateKey.encodeToBase64(), null, null, null, null); updatedKey = new KeyDataConfig(privateKeyData, PrivateKeyType.UNLOCKED); } else { final PrivateKeyData privateKeyData = keyEncryptor.encryptPrivateKey(privateKey, newPassword, argonOptions); updatedKey = new KeyDataConfig(privateKeyData, PrivateKeyType.LOCKED); } //write the key to file Files.write(keypath, JaxbUtil.marshalToString(updatedKey).getBytes(UTF_8)); System.out.println("Private key at " + keypath.toString() + " updated."); } return Optional.empty(); }
public static void marshalMasked(Config object, OutputStream outputStream) { XmlProcessingCallback.execute(() -> { Marshaller marshaller = MarshallerBuilder.create() .withXmlMediaType() .withoutBeanValidation() .build(); String xmlData; try (StringWriter writer = new StringWriter()) { marshaller.marshal(object, writer); xmlData = writer.toString(); } StreamSource xmlSource = new StreamSource(new StringReader(xmlData)); try (StringWriter writer = new StringWriter()) { StreamResult xmlResult = new StreamResult(writer); createMaskingXslTransformer().transform(xmlSource, xmlResult); writer.flush(); Unmarshaller unmarshaller = UnmarshallerBuilder.create() .withXmlMediaType() .withoutBeanValidation() .build(); Config masked = (Config) unmarshaller.unmarshal(new StringReader(writer.toString())); marshalWithNoValidation(masked, outputStream); return null; } }); }
@Override public void save(Config config) { IOCallback.execute(() -> { Path temp = Files.createTempFile(UUID.randomUUID().toString(), ".tmp"); try(OutputStream fout = Files.newOutputStream(temp)){ JaxbUtil.marshalWithNoValidation(config, fout); } Files.copy(temp, path,StandardCopyOption.REPLACE_EXISTING); return null; }); } }
finalKeys.setPublicKey(publicKeyBase64); final String privateKeyJson = JaxbUtil.marshalToString(finalKeys.getConfig());
public static void marshal(Object object, OutputStream outputStream) { try { MarshallerBuilder.create().build() .marshal(object, outputStream); } catch (Throwable ex) { Optional<ConstraintViolationException> validationException = unwrapConstraintViolationException(ex); if (validationException.isPresent()) { throw validationException.get(); } throw new ConfigException(ex); } }
private static void output(CommandLine commandLine, Config config) throws IOException { if (commandLine.hasOption("output")) { final Path outputConfigFile = Paths.get(commandLine.getOptionValue("output")); try (OutputStream out = Files.newOutputStream(outputConfigFile, CREATE_NEW)) { JaxbUtil.marshal(config, out); } } else { JaxbUtil.marshal(config, System.out); } } }
@Override public Optional parse(final CommandLine commandLine) throws IOException { if (commandLine.hasOption("updatepassword")) { final ArgonOptions argonOptions = argonOptions(commandLine); final List<String> passwords = passwords(commandLine); final Path keypath = privateKeyPath(commandLine); final KeyDataConfig keyDataConfig = JaxbUtil.unmarshal(Files.newInputStream(keypath), KeyDataConfig.class); final PrivateKey privateKey = this.getExistingKey(keyDataConfig, passwords); final String newPassword = passwordReader.requestUserPassword(); final KeyDataConfig updatedKey; if(newPassword.isEmpty()) { final PrivateKeyData privateKeyData = new PrivateKeyData(privateKey.encodeToBase64(), null, null, null, null); updatedKey = new KeyDataConfig(privateKeyData, PrivateKeyType.UNLOCKED); } else { final PrivateKeyData privateKeyData = keyEncryptor.encryptPrivateKey(privateKey, newPassword, argonOptions); updatedKey = new KeyDataConfig(privateKeyData, PrivateKeyType.LOCKED); } //write the key to file Files.write(keypath, JaxbUtil.marshalToString(updatedKey).getBytes(UTF_8)); System.out.println("Private key at " + keypath.toString() + " updated."); } return Optional.empty(); }
public static void marshalMasked(Config object, OutputStream outputStream) { XmlProcessingCallback.execute(() -> { Marshaller marshaller = MarshallerBuilder.create() .withXmlMediaType() .withoutBeanValidation() .build(); String xmlData; try (StringWriter writer = new StringWriter()) { marshaller.marshal(object, writer); xmlData = writer.toString(); } StreamSource xmlSource = new StreamSource(new StringReader(xmlData)); try (StringWriter writer = new StringWriter()) { StreamResult xmlResult = new StreamResult(writer); createMaskingXslTransformer().transform(xmlSource, xmlResult); writer.flush(); Unmarshaller unmarshaller = UnmarshallerBuilder.create() .withXmlMediaType() .withoutBeanValidation() .build(); Config masked = (Config) unmarshaller.unmarshal(new StringReader(writer.toString())); marshalWithNoValidation(masked, outputStream); return null; } }); }
public static String marshalToStringNoValidation(Object object) { return IOCallback.execute(() -> { try (OutputStream out = new ByteArrayOutputStream()) { marshalWithNoValidation(object, out); return out.toString(); } }); }
public static void marshal(Object object, OutputStream outputStream) { try { MarshallerBuilder.create().build() .marshal(object, outputStream); } catch (Throwable ex) { Optional<ConstraintViolationException> validationException = unwrapConstraintViolationException(ex); if (validationException.isPresent()) { throw validationException.get(); } throw new ConfigException(ex); } }
public static String marshalToString(Object object) { return IOCallback.execute(() -> { try (OutputStream out = new ByteArrayOutputStream()) { marshal(object, out); return out.toString(); } }); }
private Optional<ArgonOptions> argonOptions(final CommandLine commandLine) throws IOException { if (commandLine.hasOption("keygenconfig")) { final String pathName = commandLine.getOptionValue("keygenconfig"); final InputStream configStream = Files.newInputStream(Paths.get(pathName)); final ArgonOptions argonOptions = JaxbUtil.unmarshal(configStream, ArgonOptions.class); return Optional.of(argonOptions); } return Optional.empty(); }
@Override public void save(Config config) { IOCallback.execute(() -> { Path temp = Files.createTempFile(UUID.randomUUID().toString(), ".tmp"); try(OutputStream fout = Files.newOutputStream(temp)){ JaxbUtil.marshalWithNoValidation(config, fout); } Files.copy(temp, path,StandardCopyOption.REPLACE_EXISTING); return null; }); } }
public static String marshalToString(Object object) { return IOCallback.execute(() -> { try (OutputStream out = new ByteArrayOutputStream()) { marshal(object, out); return out.toString(); } }); }
private void loadKeys() { this.inlineKeypair = new InlineKeypair( IOCallback.execute(() -> new String(Files.readAllBytes(this.publicKeyPath), UTF_8)), JaxbUtil.unmarshal( IOCallback.execute(() -> Files.newInputStream(privateKeyPath)), KeyDataConfig.class ) ); }
public static String marshalToStringNoValidation(Object object) { return IOCallback.execute(() -> { try (OutputStream out = new ByteArrayOutputStream()) { marshalWithNoValidation(object, out); return out.toString(); } }); }
private void loadKeys() { this.inlineKeypair = new InlineKeypair( IOCallback.execute(() -> new String(Files.readAllBytes(this.publicKeyPath), UTF_8)), JaxbUtil.unmarshal( IOCallback.execute(() -> Files.newInputStream(privateKeyPath)), KeyDataConfig.class ) ); }