@Test public void usesDefaultEncryptorConfigIfNoneInConfig() throws Exception { final EncryptorConfig encryptorConfig = new EncryptorConfig(); final Map<String, String> properties = new HashMap<>(); encryptorConfig.setType(EncryptorType.NACL); encryptorConfig.setProperties(properties); final EncryptorOptions encryptorOptions = mock(EncryptorOptions.class); when(encryptorOptions.parseEncryptorConfig()).thenReturn(encryptorConfig); command.encryptorOptions = encryptorOptions; final KeyGenerator keyGenerator = mock(KeyGenerator.class); when(keyGeneratorFactory.create(any(), any())).thenReturn(keyGenerator); CliResult result = command.call(); verify(keyGeneratorFactory).create(null, encryptorConfig); assertThat(result).isEqualToComparingFieldByField(wantResult); verify(encryptorOptions).parseEncryptorConfig(); verify(keyGenerator).generate(anyString(), any(), any()); verifyNoMoreInteractions(encryptorOptions, keyGenerator); }
@Override public CliResult call() throws IOException { if (Objects.nonNull(fileUpdateOptions) && Objects.isNull(fileUpdateOptions.getConfig())) { throw new CliException("Missing required argument(s): --configfile=<config>"); } final EncryptorConfig encryptorConfig = this.encryptorConfig().orElse(EncryptorConfig.getDefault()); final KeyVaultOptions keyVaultOptions = this.keyVaultOptions().orElse(null); final KeyVaultConfig keyVaultConfig = this.keyVaultConfig().orElse(null); KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); final KeyGenerator generator = factory.create(keyVaultConfig, encryptorConfig); final List<String> newKeyNames = new ArrayList<>(); if (Objects.isNull(keyOut) || keyOut.isEmpty()) { newKeyNames.add(""); } else { newKeyNames.addAll(keyOut); } final List<ConfigKeyPair> newConfigKeyPairs = newKeyNames.stream() .map(name -> generator.generate(name, argonOptions, keyVaultOptions)) .collect(Collectors.toList()); final List<char[]> newPasswords = newConfigKeyPairs.stream() .filter(Objects::nonNull) .map(ConfigKeyPair::getPassword) .collect(Collectors.toList()); final List<KeyData> newKeyData = newConfigKeyPairs.stream().map(pair -> keyDataMarshaller.marshal(pair)).collect(Collectors.toList()); if (Objects.isNull(fileUpdateOptions)) { return new CliResult(0, true, null); } if (Objects.isNull(fileUpdateOptions.getConfig().getKeys())) { fileUpdateOptions.getConfig().setKeys(new KeyConfiguration()); } if (Objects.isNull(fileUpdateOptions.getConfig().getKeys().getKeyData())) { fileUpdateOptions.getConfig().getKeys().setKeyData(new ArrayList<>()); } if (Objects.nonNull(fileUpdateOptions.getConfigOut())) { if (Objects.nonNull(fileUpdateOptions.getPwdOut())) { passwordFileUpdaterWriter.updateAndWrite( newPasswords, fileUpdateOptions.getConfig(), fileUpdateOptions.getPwdOut()); fileUpdateOptions.getConfig().getKeys().setPasswordFile(fileUpdateOptions.getPwdOut()); } configFileUpdaterWriter.updateAndWrite( newKeyData, keyVaultConfig, fileUpdateOptions.getConfig(), fileUpdateOptions.getConfigOut()); } else { configFileUpdaterWriter.updateAndWriteToCLI(newKeyData, keyVaultConfig, fileUpdateOptions.getConfig()); } return new CliResult(0, true, null); }
KeyGenCommand implements Callable<CliResult> { @Override public CliResult call() throws IOException { if (Objects.nonNull(fileUpdateOptions) && Objects.isNull(fileUpdateOptions.getConfig())) { throw new CliException("Missing required argument(s): --configfile=<config>"); } final EncryptorConfig encryptorConfig = this.encryptorConfig().orElse(EncryptorConfig.getDefault()); final KeyVaultOptions keyVaultOptions = this.keyVaultOptions().orElse(null); final KeyVaultConfig keyVaultConfig = this.keyVaultConfig().orElse(null); KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); final KeyGenerator generator = factory.create(keyVaultConfig, encryptorConfig); final List<String> newKeyNames = new ArrayList<>(); if (Objects.isNull(keyOut) || keyOut.isEmpty()) { newKeyNames.add(""); } else { newKeyNames.addAll(keyOut); } final List<ConfigKeyPair> newConfigKeyPairs = newKeyNames.stream() .map(name -> generator.generate(name, argonOptions, keyVaultOptions)) .collect(Collectors.toList()); final List<char[]> newPasswords = newConfigKeyPairs.stream() .filter(Objects::nonNull) .map(ConfigKeyPair::getPassword) .collect(Collectors.toList()); final List<KeyData> newKeyData = newConfigKeyPairs.stream().map(pair -> keyDataMarshaller.marshal(pair)).collect(Collectors.toList()); if (Objects.isNull(fileUpdateOptions)) { return new CliResult(0, true, null); } if (Objects.isNull(fileUpdateOptions.getConfig().getKeys())) { fileUpdateOptions.getConfig().setKeys(new KeyConfiguration()); } if (Objects.isNull(fileUpdateOptions.getConfig().getKeys().getKeyData())) { fileUpdateOptions.getConfig().getKeys().setKeyData(new ArrayList<>()); } if (Objects.nonNull(fileUpdateOptions.getConfigOut())) { if (Objects.nonNull(fileUpdateOptions.getPwdOut())) { passwordFileUpdaterWriter.updateAndWrite( newPasswords, fileUpdateOptions.getConfig(), fileUpdateOptions.getPwdOut()); fileUpdateOptions.getConfig().getKeys().setPasswordFile(fileUpdateOptions.getPwdOut()); } configFileUpdaterWriter.updateAndWrite( newKeyData, keyVaultConfig, fileUpdateOptions.getConfig(), fileUpdateOptions.getConfigOut()); } else { configFileUpdaterWriter.updateAndWriteToCLI(newKeyData, keyVaultConfig, fileUpdateOptions.getConfig()); } return new CliResult(0, true, null); } }
KeyGenCommand implements Callable<CliResult> { @Override public CliResult call() throws IOException { if (Objects.nonNull(fileUpdateOptions) && Objects.isNull(fileUpdateOptions.getConfig())) { throw new CliException("Missing required argument(s): --configfile=<config>"); } final EncryptorConfig encryptorConfig = this.encryptorConfig().orElse(EncryptorConfig.getDefault()); final KeyVaultOptions keyVaultOptions = this.keyVaultOptions().orElse(null); final KeyVaultConfig keyVaultConfig = this.keyVaultConfig().orElse(null); KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); final KeyGenerator generator = factory.create(keyVaultConfig, encryptorConfig); final List<String> newKeyNames = new ArrayList<>(); if (Objects.isNull(keyOut) || keyOut.isEmpty()) { newKeyNames.add(""); } else { newKeyNames.addAll(keyOut); } final List<ConfigKeyPair> newConfigKeyPairs = newKeyNames.stream() .map(name -> generator.generate(name, argonOptions, keyVaultOptions)) .collect(Collectors.toList()); final List<char[]> newPasswords = newConfigKeyPairs.stream() .filter(Objects::nonNull) .map(ConfigKeyPair::getPassword) .collect(Collectors.toList()); final List<KeyData> newKeyData = newConfigKeyPairs.stream().map(pair -> keyDataMarshaller.marshal(pair)).collect(Collectors.toList()); if (Objects.isNull(fileUpdateOptions)) { return new CliResult(0, true, null); } if (Objects.isNull(fileUpdateOptions.getConfig().getKeys())) { fileUpdateOptions.getConfig().setKeys(new KeyConfiguration()); } if (Objects.isNull(fileUpdateOptions.getConfig().getKeys().getKeyData())) { fileUpdateOptions.getConfig().getKeys().setKeyData(new ArrayList<>()); } if (Objects.nonNull(fileUpdateOptions.getConfigOut())) { if (Objects.nonNull(fileUpdateOptions.getPwdOut())) { passwordFileUpdaterWriter.updateAndWrite( newPasswords, fileUpdateOptions.getConfig(), fileUpdateOptions.getPwdOut()); fileUpdateOptions.getConfig().getKeys().setPasswordFile(fileUpdateOptions.getPwdOut()); } configFileUpdaterWriter.updateAndWrite( newKeyData, keyVaultConfig, fileUpdateOptions.getConfig(), fileUpdateOptions.getConfigOut()); } else { configFileUpdaterWriter.updateAndWriteToCLI(newKeyData, keyVaultConfig, fileUpdateOptions.getConfig()); } return new CliResult(0, true, null); } KeyGenCommand(
            KeyGeneratorFactory keyGeneratorFactory,
            ConfigFileUpdaterWriter configFileUpdaterWriter,
            PasswordFileUpdaterWriter passwordFileUpdaterWriter); }
KeyGenCommand implements Callable<CliResult> { @Override public CliResult call() throws IOException { if (Objects.nonNull(fileUpdateOptions) && Objects.isNull(fileUpdateOptions.getConfig())) { throw new CliException("Missing required argument(s): --configfile=<config>"); } final EncryptorConfig encryptorConfig = this.encryptorConfig().orElse(EncryptorConfig.getDefault()); final KeyVaultOptions keyVaultOptions = this.keyVaultOptions().orElse(null); final KeyVaultConfig keyVaultConfig = this.keyVaultConfig().orElse(null); KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); final KeyGenerator generator = factory.create(keyVaultConfig, encryptorConfig); final List<String> newKeyNames = new ArrayList<>(); if (Objects.isNull(keyOut) || keyOut.isEmpty()) { newKeyNames.add(""); } else { newKeyNames.addAll(keyOut); } final List<ConfigKeyPair> newConfigKeyPairs = newKeyNames.stream() .map(name -> generator.generate(name, argonOptions, keyVaultOptions)) .collect(Collectors.toList()); final List<char[]> newPasswords = newConfigKeyPairs.stream() .filter(Objects::nonNull) .map(ConfigKeyPair::getPassword) .collect(Collectors.toList()); final List<KeyData> newKeyData = newConfigKeyPairs.stream().map(pair -> keyDataMarshaller.marshal(pair)).collect(Collectors.toList()); if (Objects.isNull(fileUpdateOptions)) { return new CliResult(0, true, null); } if (Objects.isNull(fileUpdateOptions.getConfig().getKeys())) { fileUpdateOptions.getConfig().setKeys(new KeyConfiguration()); } if (Objects.isNull(fileUpdateOptions.getConfig().getKeys().getKeyData())) { fileUpdateOptions.getConfig().getKeys().setKeyData(new ArrayList<>()); } if (Objects.nonNull(fileUpdateOptions.getConfigOut())) { if (Objects.nonNull(fileUpdateOptions.getPwdOut())) { passwordFileUpdaterWriter.updateAndWrite( newPasswords, fileUpdateOptions.getConfig(), fileUpdateOptions.getPwdOut()); fileUpdateOptions.getConfig().getKeys().setPasswordFile(fileUpdateOptions.getPwdOut()); } configFileUpdaterWriter.updateAndWrite( newKeyData, keyVaultConfig, fileUpdateOptions.getConfig(), fileUpdateOptions.getConfigOut()); } else { configFileUpdaterWriter.updateAndWriteToCLI(newKeyData, keyVaultConfig, fileUpdateOptions.getConfig()); } return new CliResult(0, true, null); } KeyGenCommand(
            KeyGeneratorFactory keyGeneratorFactory,
            ConfigFileUpdaterWriter configFileUpdaterWriter,
            PasswordFileUpdaterWriter passwordFileUpdaterWriter); @Override CliResult call(); }
KeyGenCommand implements Callable<CliResult> { @Override public CliResult call() throws IOException { if (Objects.nonNull(fileUpdateOptions) && Objects.isNull(fileUpdateOptions.getConfig())) { throw new CliException("Missing required argument(s): --configfile=<config>"); } final EncryptorConfig encryptorConfig = this.encryptorConfig().orElse(EncryptorConfig.getDefault()); final KeyVaultOptions keyVaultOptions = this.keyVaultOptions().orElse(null); final KeyVaultConfig keyVaultConfig = this.keyVaultConfig().orElse(null); KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); final KeyGenerator generator = factory.create(keyVaultConfig, encryptorConfig); final List<String> newKeyNames = new ArrayList<>(); if (Objects.isNull(keyOut) || keyOut.isEmpty()) { newKeyNames.add(""); } else { newKeyNames.addAll(keyOut); } final List<ConfigKeyPair> newConfigKeyPairs = newKeyNames.stream() .map(name -> generator.generate(name, argonOptions, keyVaultOptions)) .collect(Collectors.toList()); final List<char[]> newPasswords = newConfigKeyPairs.stream() .filter(Objects::nonNull) .map(ConfigKeyPair::getPassword) .collect(Collectors.toList()); final List<KeyData> newKeyData = newConfigKeyPairs.stream().map(pair -> keyDataMarshaller.marshal(pair)).collect(Collectors.toList()); if (Objects.isNull(fileUpdateOptions)) { return new CliResult(0, true, null); } if (Objects.isNull(fileUpdateOptions.getConfig().getKeys())) { fileUpdateOptions.getConfig().setKeys(new KeyConfiguration()); } if (Objects.isNull(fileUpdateOptions.getConfig().getKeys().getKeyData())) { fileUpdateOptions.getConfig().getKeys().setKeyData(new ArrayList<>()); } if (Objects.nonNull(fileUpdateOptions.getConfigOut())) { if (Objects.nonNull(fileUpdateOptions.getPwdOut())) { passwordFileUpdaterWriter.updateAndWrite( newPasswords, fileUpdateOptions.getConfig(), fileUpdateOptions.getPwdOut()); fileUpdateOptions.getConfig().getKeys().setPasswordFile(fileUpdateOptions.getPwdOut()); } configFileUpdaterWriter.updateAndWrite( newKeyData, keyVaultConfig, fileUpdateOptions.getConfig(), fileUpdateOptions.getConfigOut()); } else { configFileUpdaterWriter.updateAndWriteToCLI(newKeyData, keyVaultConfig, fileUpdateOptions.getConfig()); } return new CliResult(0, true, null); } KeyGenCommand(
            KeyGeneratorFactory keyGeneratorFactory,
            ConfigFileUpdaterWriter configFileUpdaterWriter,
            PasswordFileUpdaterWriter passwordFileUpdaterWriter); @Override CliResult call(); @CommandLine.Option(
            names = {"--keyout", "-filename"},
            split = ",",
            arity = "0..1",
            description =
                    "Comma-separated list of paths to save generated key files. Can also be used with keyvault. Number of args determines number of key-pairs generated (default = ${DEFAULT-VALUE})")
    public List<String> keyOut; @CommandLine.Option(
            names = {"--argonconfig", "-keygenconfig"},
            description =
                    "File containing Argon2 encryption config used to secure the new private key when storing to the filesystem")
    public ArgonOptions argonOptions; @CommandLine.Mixin public EncryptorOptions encryptorOptions; @CommandLine.Mixin public DebugOptions debugOptions; }