@Test public void readablePasswordFileAssignsPasswords() throws IOException { final Path passes = Files.createTempDirectory("testdirectory").resolve("passwords.txt"); Files.write(passes, "q".getBytes()); final KeyData keypair = new KeyData(); final KeyConfiguration keyConfig = new KeyConfiguration(passes, null, singletonList(keypair), null, null); final Config config = new Config(); config.setKeys(keyConfig); this.cliKeyPasswordResolver.resolveKeyPasswords(config); assertThat(keyConfig.getKeyData()).hasSize(1); final KeyData returned = keyConfig.getKeyData().get(0); assertThat(String.valueOf(returned.getPassword())).isEqualTo("q"); }
@Override public void resolveKeyPasswords(final Config config) { final KeyConfiguration input = config.getKeys(); if (input == null) { return; } final List<char[]> allPasswords = new ArrayList<>(); if (input.getPasswords() != null) { allPasswords.addAll( input.getPasswords().stream().map(String::toCharArray).collect(Collectors.toList()) ); } else if (input.getPasswordFile() != null) { try { allPasswords.addAll( Files.readAllLines(input.getPasswordFile(), StandardCharsets.UTF_8) .stream() .map(String::toCharArray) .collect(Collectors.toList()) ); } catch (final IOException ex) { System.err.println("Could not read the password file"); } } List<KeyData> keyPairs = input.getKeyData(); IntStream.range(0, input.getKeyData().size()) .forEachOrdered( i -> { if (i < allPasswords.size()) { keyPairs.get(i).setPassword(allPasswords.get(i)); } }); EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()) .orElse( new EncryptorConfig() { { setType(EncryptorType.NACL); } }); final KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); IntStream.range(0, input.getKeyData().size()) .forEachOrdered( keyNumber -> getSingleKeyPassword(keyNumber, input.getKeyData().get(keyNumber), keyEncryptor)); }
CliKeyPasswordResolver implements KeyPasswordResolver { @Override public void resolveKeyPasswords(final Config config) { final KeyConfiguration input = config.getKeys(); if (input == null) { return; } final List<char[]> allPasswords = new ArrayList<>(); if (input.getPasswords() != null) { allPasswords.addAll( input.getPasswords().stream().map(String::toCharArray).collect(Collectors.toList()) ); } else if (input.getPasswordFile() != null) { try { allPasswords.addAll( Files.readAllLines(input.getPasswordFile(), StandardCharsets.UTF_8) .stream() .map(String::toCharArray) .collect(Collectors.toList()) ); } catch (final IOException ex) { System.err.println("Could not read the password file"); } } List<KeyData> keyPairs = input.getKeyData(); IntStream.range(0, input.getKeyData().size()) .forEachOrdered( i -> { if (i < allPasswords.size()) { keyPairs.get(i).setPassword(allPasswords.get(i)); } }); EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()) .orElse( new EncryptorConfig() { { setType(EncryptorType.NACL); } }); final KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); IntStream.range(0, input.getKeyData().size()) .forEachOrdered( keyNumber -> getSingleKeyPassword(keyNumber, input.getKeyData().get(keyNumber), keyEncryptor)); } }
CliKeyPasswordResolver implements KeyPasswordResolver { @Override public void resolveKeyPasswords(final Config config) { final KeyConfiguration input = config.getKeys(); if (input == null) { return; } final List<char[]> allPasswords = new ArrayList<>(); if (input.getPasswords() != null) { allPasswords.addAll( input.getPasswords().stream().map(String::toCharArray).collect(Collectors.toList()) ); } else if (input.getPasswordFile() != null) { try { allPasswords.addAll( Files.readAllLines(input.getPasswordFile(), StandardCharsets.UTF_8) .stream() .map(String::toCharArray) .collect(Collectors.toList()) ); } catch (final IOException ex) { System.err.println("Could not read the password file"); } } List<KeyData> keyPairs = input.getKeyData(); IntStream.range(0, input.getKeyData().size()) .forEachOrdered( i -> { if (i < allPasswords.size()) { keyPairs.get(i).setPassword(allPasswords.get(i)); } }); EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()) .orElse( new EncryptorConfig() { { setType(EncryptorType.NACL); } }); final KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); IntStream.range(0, input.getKeyData().size()) .forEachOrdered( keyNumber -> getSingleKeyPassword(keyNumber, input.getKeyData().get(keyNumber), keyEncryptor)); } CliKeyPasswordResolver();  CliKeyPasswordResolver(final PasswordReader passwordReader); }
CliKeyPasswordResolver implements KeyPasswordResolver { @Override public void resolveKeyPasswords(final Config config) { final KeyConfiguration input = config.getKeys(); if (input == null) { return; } final List<char[]> allPasswords = new ArrayList<>(); if (input.getPasswords() != null) { allPasswords.addAll( input.getPasswords().stream().map(String::toCharArray).collect(Collectors.toList()) ); } else if (input.getPasswordFile() != null) { try { allPasswords.addAll( Files.readAllLines(input.getPasswordFile(), StandardCharsets.UTF_8) .stream() .map(String::toCharArray) .collect(Collectors.toList()) ); } catch (final IOException ex) { System.err.println("Could not read the password file"); } } List<KeyData> keyPairs = input.getKeyData(); IntStream.range(0, input.getKeyData().size()) .forEachOrdered( i -> { if (i < allPasswords.size()) { keyPairs.get(i).setPassword(allPasswords.get(i)); } }); EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()) .orElse( new EncryptorConfig() { { setType(EncryptorType.NACL); } }); final KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); IntStream.range(0, input.getKeyData().size()) .forEachOrdered( keyNumber -> getSingleKeyPassword(keyNumber, input.getKeyData().get(keyNumber), keyEncryptor)); } CliKeyPasswordResolver();  CliKeyPasswordResolver(final PasswordReader passwordReader); @Override void resolveKeyPasswords(final Config config); void getSingleKeyPassword(final int keyNumber, final KeyData keyPair, final KeyEncryptor keyEncryptor); }
CliKeyPasswordResolver implements KeyPasswordResolver { @Override public void resolveKeyPasswords(final Config config) { final KeyConfiguration input = config.getKeys(); if (input == null) { return; } final List<char[]> allPasswords = new ArrayList<>(); if (input.getPasswords() != null) { allPasswords.addAll( input.getPasswords().stream().map(String::toCharArray).collect(Collectors.toList()) ); } else if (input.getPasswordFile() != null) { try { allPasswords.addAll( Files.readAllLines(input.getPasswordFile(), StandardCharsets.UTF_8) .stream() .map(String::toCharArray) .collect(Collectors.toList()) ); } catch (final IOException ex) { System.err.println("Could not read the password file"); } } List<KeyData> keyPairs = input.getKeyData(); IntStream.range(0, input.getKeyData().size()) .forEachOrdered( i -> { if (i < allPasswords.size()) { keyPairs.get(i).setPassword(allPasswords.get(i)); } }); EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()) .orElse( new EncryptorConfig() { { setType(EncryptorType.NACL); } }); final KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); IntStream.range(0, input.getKeyData().size()) .forEachOrdered( keyNumber -> getSingleKeyPassword(keyNumber, input.getKeyData().get(keyNumber), keyEncryptor)); } CliKeyPasswordResolver();  CliKeyPasswordResolver(final PasswordReader passwordReader); @Override void resolveKeyPasswords(final Config config); void getSingleKeyPassword(final int keyNumber, final KeyData keyPair, final KeyEncryptor keyEncryptor);  }