use of com.github.robozonky.common.secrets.SecretProvider in project robozonky by RoboZonky.
the class RoboZonkyInstallerListener method prepareCore.
CommandLinePart prepareCore(final char[] keystorePassword) {
final SecretProvider secrets = getSecretProvider(keystorePassword);
final String zonkoidId = "zonkoid";
final CommandLinePart cli = new CommandLinePart().setOption("-p", String.valueOf(secrets.isPersistent() ? KEYSTORE_PASSWORD : secrets.getPassword()));
if (Boolean.valueOf(Variables.IS_DRY_RUN.getValue(DATA))) {
cli.setOption("-d");
}
final boolean isZonkoidEnabled = Boolean.valueOf(Variables.IS_ZONKOID_ENABLED.getValue(DATA));
if (secrets.isPersistent() && KEYSTORE_FILE.canRead()) {
cli.setOption("-g", KEYSTORE_FILE.getAbsolutePath());
if (isZonkoidEnabled) {
cli.setOption("-x", zonkoidId);
secrets.setSecret(zonkoidId, Variables.ZONKOID_TOKEN.getValue(DATA).toCharArray());
}
} else {
cli.setOption("-u", secrets.getUsername());
if (isZonkoidEnabled) {
cli.setOption("-x", zonkoidId + ":" + Variables.ZONKOID_TOKEN.getValue(DATA));
}
}
return cli;
}
use of com.github.robozonky.common.secrets.SecretProvider in project robozonky by RoboZonky.
the class AuthenticatedTest method tokenProper.
@Test
void tokenProper() {
// prepare SUT
final SecretProvider sp = SecretProvider.fallback(UUID.randomUUID().toString(), new char[0]);
final String username = sp.getUsername();
final char[] password = sp.getPassword();
final ZonkyApiToken token = new ZonkyApiToken(UUID.randomUUID().toString(), UUID.randomUUID().toString(), 299);
final OAuth oauth = mock(OAuth.class);
when(oauth.login(eq(username), eq(password))).thenReturn(token);
final Zonky z = mock(Zonky.class);
final ApiProvider api = mockApiProvider(oauth, z);
final TokenBasedAccess a = (TokenBasedAccess) Authenticated.tokenBased(api, sp, Duration.ofSeconds(60));
// call SUT
final Function<Zonky, Collection<RawInvestment>> f = mock(Function.class);
final Collection<RawInvestment> expectedResult = Collections.emptyList();
when(f.apply(eq(z))).thenReturn(expectedResult);
final Collection<RawInvestment> result = a.call(f);
assertSoftly(softly -> {
softly.assertThat(result).isSameAs(expectedResult);
softly.assertThat(a.getSecretProvider()).isSameAs(sp);
});
verify(oauth).login(eq(username), eq(password));
verify(oauth, never()).refresh(any());
verify(z, never()).logout();
}
use of com.github.robozonky.common.secrets.SecretProvider in project robozonky by RoboZonky.
the class OperatingMode method configure.
public Optional<InvestmentMode> configure(final CommandLine cli, final Authenticated auth) {
final SecretProvider secretProvider = auth.getSecretProvider();
final boolean isDryRun = cli.getTweaksFragment().isDryRunEnabled();
// initialize SessionInfo before the robot potentially sends the first notification
Events.initialize(new SessionInfo(secretProvider.getUsername(), cli.getName(), isDryRun));
// and now initialize the chosen mode of operation
return cli.getConfirmationFragment().getConfirmationCredentials().map(value -> new Credentials(value, secretProvider)).map(credentials -> this.getZonkyProxyBuilder(credentials, secretProvider)).orElse(Optional.of(new Investor.Builder())).map(builder -> {
if (isDryRun) {
LOGGER.info("RoboZonky is doing a dry run. It will not invest any real money.");
builder.asDryRun();
}
builder.asUser(secretProvider.getUsername());
return this.getInvestmentMode(cli, auth, builder);
}).orElse(Optional.empty());
}
use of com.github.robozonky.common.secrets.SecretProvider in project robozonky by RoboZonky.
the class OperatingModeTest method withConfirmationAndNoSecret.
@Test
void withConfirmationAndNoSecret() {
final CommandLine cli = mock(CommandLine.class);
when(cli.getTweaksFragment()).thenReturn(mock(TweaksCommandLineFragment.class));
final SecretProvider secretProvider = SecretProvider.fallback("user", "pass".toCharArray());
final Authenticated auth = Authenticated.passwordBased(secretProvider);
final ConfirmationCommandLineFragment fragment = new ConfirmationCommandLineFragment();
fragment.confirmationCredentials = SERVICE;
when(cli.getConfirmationFragment()).thenReturn(fragment);
final OperatingMode mode = new DaemonOperatingMode(t -> {
});
final Optional<InvestmentMode> config = mode.configure(cli, auth);
assertThat(config).isEmpty();
assertThat(secretProvider.getSecret(SERVICE)).isEmpty();
}
use of com.github.robozonky.common.secrets.SecretProvider in project robozonky by RoboZonky.
the class OperatingModeTest method withoutConfirmation.
@Test
void withoutConfirmation() {
final CommandLine cli = mock(CommandLine.class);
when(cli.getTweaksFragment()).thenReturn(mock(TweaksCommandLineFragment.class));
when(cli.getConfirmationFragment()).thenReturn(mock(ConfirmationCommandLineFragment.class));
final SecretProvider secretProvider = SecretProvider.fallback("user", new char[0]);
final Authenticated auth = Authenticated.passwordBased(secretProvider);
final OperatingMode mode = new DaemonOperatingMode(t -> {
});
final Optional<InvestmentMode> config = mode.configure(cli, auth);
assertThat(config).isPresent();
assertThat(secretProvider.getSecret(SERVICE)).isEmpty();
}
Aggregations