@Test public void lockedKeyWithEmptyPasswordRequestsPassword() { when(passwordReader.readPasswordFromConsole()).thenReturn("a".toCharArray()); final KeyDataConfig privKeyDataConfig = mock(KeyDataConfig.class); when(privKeyDataConfig.getType()).thenReturn(PrivateKeyType.LOCKED); PrivateKeyData privateKeyData = mock(PrivateKeyData.class); when(privKeyDataConfig.getPrivateKeyData()).thenReturn(privateKeyData); KeyData keyPair = new KeyData(); keyPair.setPassword(new char[0]); keyPair.setPublicKey("public"); keyPair.setConfig(privKeyDataConfig); when(keyEncryptor.decryptPrivateKey(any(), any())).thenThrow(new EncryptorException("")); this.cliKeyPasswordResolver.getSingleKeyPassword(0, keyPair, keyEncryptor); assertThat(systemOutRule.getLog()) .containsOnlyOnce( "Password for key[0] missing or invalid.\nAttempt 1 of 2. Enter a password for the key"); }
public void getSingleKeyPassword(final int keyNumber, final KeyData keyPair, final KeyEncryptor keyEncryptor) { final boolean isInline = KeyDataUtil.isInline(keyPair); final boolean isFilesystem = KeyDataUtil.isFileSystem(keyPair); if (!isInline && !isFilesystem) { return; } final boolean isLocked = KeyDataUtil.isLocked(keyPair); if (isLocked) { ConfigKeyPair configKeyPair = KeyDataUtil.unmarshal(keyPair, keyEncryptor); int currentAttemptNumber = MAX_PASSWORD_ATTEMPTS; while (currentAttemptNumber > 0) { if (Objects.isNull(configKeyPair.getPassword()) || configKeyPair.getPassword().length == 0 || Optional.ofNullable(configKeyPair.getPrivateKey()) .filter(s -> s.contains("NACL_FAILURE")) .isPresent()) { final String attemptOutput = "Attempt " + (MAX_PASSWORD_ATTEMPTS - currentAttemptNumber + 1) + " of " + MAX_PASSWORD_ATTEMPTS + "."; System.out.printf("Password for key[%s] missing or invalid.", keyNumber); System.out.println(); System.out.printf("%s Enter a password for the key", attemptOutput); System.out.println(); final char[] pass = passwordReader.readPasswordFromConsole(); configKeyPair.withPassword(pass); keyPair.setPassword(pass); } currentAttemptNumber--; } } }
CliKeyPasswordResolver implements KeyPasswordResolver { public void getSingleKeyPassword(final int keyNumber, final KeyData keyPair, final KeyEncryptor keyEncryptor) { final boolean isInline = KeyDataUtil.isInline(keyPair); final boolean isFilesystem = KeyDataUtil.isFileSystem(keyPair); if (!isInline && !isFilesystem) { return; } final boolean isLocked = KeyDataUtil.isLocked(keyPair); if (isLocked) { ConfigKeyPair configKeyPair = KeyDataUtil.unmarshal(keyPair, keyEncryptor); int currentAttemptNumber = MAX_PASSWORD_ATTEMPTS; while (currentAttemptNumber > 0) { if (Objects.isNull(configKeyPair.getPassword()) || configKeyPair.getPassword().length == 0 || Optional.ofNullable(configKeyPair.getPrivateKey()) .filter(s -> s.contains("NACL_FAILURE")) .isPresent()) { final String attemptOutput = "Attempt " + (MAX_PASSWORD_ATTEMPTS - currentAttemptNumber + 1) + " of " + MAX_PASSWORD_ATTEMPTS + "."; System.out.printf("Password for key[%s] missing or invalid.", keyNumber); System.out.println(); System.out.printf("%s Enter a password for the key", attemptOutput); System.out.println(); final char[] pass = passwordReader.readPasswordFromConsole(); configKeyPair.withPassword(pass); keyPair.setPassword(pass); } currentAttemptNumber--; } } } }
CliKeyPasswordResolver implements KeyPasswordResolver { public void getSingleKeyPassword(final int keyNumber, final KeyData keyPair, final KeyEncryptor keyEncryptor) { final boolean isInline = KeyDataUtil.isInline(keyPair); final boolean isFilesystem = KeyDataUtil.isFileSystem(keyPair); if (!isInline && !isFilesystem) { return; } final boolean isLocked = KeyDataUtil.isLocked(keyPair); if (isLocked) { ConfigKeyPair configKeyPair = KeyDataUtil.unmarshal(keyPair, keyEncryptor); int currentAttemptNumber = MAX_PASSWORD_ATTEMPTS; while (currentAttemptNumber > 0) { if (Objects.isNull(configKeyPair.getPassword()) || configKeyPair.getPassword().length == 0 || Optional.ofNullable(configKeyPair.getPrivateKey()) .filter(s -> s.contains("NACL_FAILURE")) .isPresent()) { final String attemptOutput = "Attempt " + (MAX_PASSWORD_ATTEMPTS - currentAttemptNumber + 1) + " of " + MAX_PASSWORD_ATTEMPTS + "."; System.out.printf("Password for key[%s] missing or invalid.", keyNumber); System.out.println(); System.out.printf("%s Enter a password for the key", attemptOutput); System.out.println(); final char[] pass = passwordReader.readPasswordFromConsole(); configKeyPair.withPassword(pass); keyPair.setPassword(pass); } currentAttemptNumber--; } } } CliKeyPasswordResolver();  CliKeyPasswordResolver(final PasswordReader passwordReader); }
CliKeyPasswordResolver implements KeyPasswordResolver { public void getSingleKeyPassword(final int keyNumber, final KeyData keyPair, final KeyEncryptor keyEncryptor) { final boolean isInline = KeyDataUtil.isInline(keyPair); final boolean isFilesystem = KeyDataUtil.isFileSystem(keyPair); if (!isInline && !isFilesystem) { return; } final boolean isLocked = KeyDataUtil.isLocked(keyPair); if (isLocked) { ConfigKeyPair configKeyPair = KeyDataUtil.unmarshal(keyPair, keyEncryptor); int currentAttemptNumber = MAX_PASSWORD_ATTEMPTS; while (currentAttemptNumber > 0) { if (Objects.isNull(configKeyPair.getPassword()) || configKeyPair.getPassword().length == 0 || Optional.ofNullable(configKeyPair.getPrivateKey()) .filter(s -> s.contains("NACL_FAILURE")) .isPresent()) { final String attemptOutput = "Attempt " + (MAX_PASSWORD_ATTEMPTS - currentAttemptNumber + 1) + " of " + MAX_PASSWORD_ATTEMPTS + "."; System.out.printf("Password for key[%s] missing or invalid.", keyNumber); System.out.println(); System.out.printf("%s Enter a password for the key", attemptOutput); System.out.println(); final char[] pass = passwordReader.readPasswordFromConsole(); configKeyPair.withPassword(pass); keyPair.setPassword(pass); } currentAttemptNumber--; } } } 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 { public void getSingleKeyPassword(final int keyNumber, final KeyData keyPair, final KeyEncryptor keyEncryptor) { final boolean isInline = KeyDataUtil.isInline(keyPair); final boolean isFilesystem = KeyDataUtil.isFileSystem(keyPair); if (!isInline && !isFilesystem) { return; } final boolean isLocked = KeyDataUtil.isLocked(keyPair); if (isLocked) { ConfigKeyPair configKeyPair = KeyDataUtil.unmarshal(keyPair, keyEncryptor); int currentAttemptNumber = MAX_PASSWORD_ATTEMPTS; while (currentAttemptNumber > 0) { if (Objects.isNull(configKeyPair.getPassword()) || configKeyPair.getPassword().length == 0 || Optional.ofNullable(configKeyPair.getPrivateKey()) .filter(s -> s.contains("NACL_FAILURE")) .isPresent()) { final String attemptOutput = "Attempt " + (MAX_PASSWORD_ATTEMPTS - currentAttemptNumber + 1) + " of " + MAX_PASSWORD_ATTEMPTS + "."; System.out.printf("Password for key[%s] missing or invalid.", keyNumber); System.out.println(); System.out.printf("%s Enter a password for the key", attemptOutput); System.out.println(); final char[] pass = passwordReader.readPasswordFromConsole(); configKeyPair.withPassword(pass); keyPair.setPassword(pass); } currentAttemptNumber--; } } } CliKeyPasswordResolver();  CliKeyPasswordResolver(final PasswordReader passwordReader); @Override void resolveKeyPasswords(final Config config); void getSingleKeyPassword(final int keyNumber, final KeyData keyPair, final KeyEncryptor keyEncryptor);  }