Search in sources :

Example 1 with SecretProvider

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;
}
Also used : SecretProvider(com.github.robozonky.common.secrets.SecretProvider)

Example 2 with SecretProvider

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();
}
Also used : ApiProvider(com.github.robozonky.common.remote.ApiProvider) Collection(java.util.Collection) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) ZonkyApiToken(com.github.robozonky.api.remote.entities.ZonkyApiToken) SecretProvider(com.github.robozonky.common.secrets.SecretProvider) OAuth(com.github.robozonky.common.remote.OAuth) Zonky(com.github.robozonky.common.remote.Zonky) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Example 3 with SecretProvider

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());
}
Also used : SessionInfo(com.github.robozonky.api.notifications.SessionInfo) ToStringBuilder(com.github.robozonky.internal.api.ToStringBuilder) Investor(com.github.robozonky.app.investing.Investor) Logger(org.slf4j.Logger) Events(com.github.robozonky.app.Events) ConfirmationProviderLoader(com.github.robozonky.common.extensions.ConfirmationProviderLoader) Authenticated(com.github.robozonky.app.authentication.Authenticated) Credentials(com.github.robozonky.common.secrets.Credentials) LoggerFactory(org.slf4j.LoggerFactory) Optional(java.util.Optional) SecretProvider(com.github.robozonky.common.secrets.SecretProvider) ConfirmationProvider(com.github.robozonky.api.confirmations.ConfirmationProvider) ToStringBuilder(com.github.robozonky.internal.api.ToStringBuilder) SessionInfo(com.github.robozonky.api.notifications.SessionInfo) Credentials(com.github.robozonky.common.secrets.Credentials) SecretProvider(com.github.robozonky.common.secrets.SecretProvider)

Example 4 with SecretProvider

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();
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) DaemonInvestmentMode(com.github.robozonky.app.configuration.daemon.DaemonInvestmentMode) SecretProvider(com.github.robozonky.common.secrets.SecretProvider) Test(org.junit.jupiter.api.Test)

Example 5 with SecretProvider

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();
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) DaemonInvestmentMode(com.github.robozonky.app.configuration.daemon.DaemonInvestmentMode) SecretProvider(com.github.robozonky.common.secrets.SecretProvider) Test(org.junit.jupiter.api.Test)

Aggregations

SecretProvider (com.github.robozonky.common.secrets.SecretProvider)9 Test (org.junit.jupiter.api.Test)7 Authenticated (com.github.robozonky.app.authentication.Authenticated)5 DaemonInvestmentMode (com.github.robozonky.app.configuration.daemon.DaemonInvestmentMode)4 ZonkyApiToken (com.github.robozonky.api.remote.entities.ZonkyApiToken)3 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)3 ApiProvider (com.github.robozonky.common.remote.ApiProvider)3 OAuth (com.github.robozonky.common.remote.OAuth)3 Zonky (com.github.robozonky.common.remote.Zonky)3 Collection (java.util.Collection)3 RawInvestment (com.github.robozonky.api.remote.entities.RawInvestment)2 ConfirmationProvider (com.github.robozonky.api.confirmations.ConfirmationProvider)1 SessionInfo (com.github.robozonky.api.notifications.SessionInfo)1 Events (com.github.robozonky.app.Events)1 Investor (com.github.robozonky.app.investing.Investor)1 ConfirmationProviderLoader (com.github.robozonky.common.extensions.ConfirmationProviderLoader)1 Credentials (com.github.robozonky.common.secrets.Credentials)1 ToStringBuilder (com.github.robozonky.internal.api.ToStringBuilder)1 Optional (java.util.Optional)1 Logger (org.slf4j.Logger)1