private void checkAuthenticated() { if (!isAuthenticated()) { throw new IllegalStateException("Not authenticated"); } }
@Test public void shouldHandlePasswordChangeWithWrongPasswordOnFirstAttempt() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); sshClient.authPassword("jeroen", new PasswordFinder() { @Override public char[] reqPassword(Resource<?> resource) { return "changeme".toCharArray(); } @Override public boolean shouldRetry(Resource<?> resource) { return false; } }, new StaticPasswordUpdateProvider("bad", "jeroen")); assertThat("Should have been authenticated", sshClient.isAuthenticated()); }
@Test public void shouldNotHandlePasswordChangeIfNoPasswordUpdateProviderSet() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); expectedException.expect(UserAuthException.class); sshClient.authPassword("jeroen", "changeme"); assertThat("Should not have authenticated", !sshClient.isAuthenticated()); }
@Test public void shouldHandlePasswordChange() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); sshClient.authPassword("jeroen", new PasswordFinder() { @Override public char[] reqPassword(Resource<?> resource) { return "changeme".toCharArray(); } @Override public boolean shouldRetry(Resource<?> resource) { return false; } }, new StaticPasswordUpdateProvider("jeroen")); assertThat("Should be authenticated", sshClient.isAuthenticated()); }
@Test public void shouldHandlePasswordChangeWithWrongPassword() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); expectedException.expect(UserAuthException.class); sshClient.authPassword("jeroen", new PasswordFinder() { @Override public char[] reqPassword(Resource<?> resource) { return "changeme".toCharArray(); } @Override public boolean shouldRetry(Resource<?> resource) { return false; } }, new StaticPasswordUpdateProvider("bad")); assertThat("Should not have authenticated", !sshClient.isAuthenticated()); }
@Test public void authenticated() throws Exception { AuthGssApiWithMic authMethod = new AuthGssApiWithMic( new LoginContext(LOGIN_CONTEXT_NAME, null, null, new TestAuthConfiguration()), Collections.singletonList(BogusGSSManager.KRB5_MECH), new BogusGSSManager()); SSHClient defaultClient = fixture.setupConnectedDefaultClient(); defaultClient.auth("user", authMethod); assertTrue(defaultClient.isAuthenticated()); }
@Test public void authenticated() throws UserAuthException, TransportException { fixture.getClient().authPassword("dummy", "dummy"); assertTrue(fixture.getClient().isAuthenticated()); }
public boolean isAuthenticated() { return ssh != null && ssh.isAuthenticated(); }
public boolean isAuthenticated() { return ssh != null && ssh.isAuthenticated(); }
private void checkAuthenticated() { if (!isAuthenticated()) { throw new IllegalStateException("Not authenticated"); } }
private void checkAuthenticated() { if (!isAuthenticated()) { throw new IllegalStateException("Not authenticated"); } }
private synchronized void checkConnection() { if (sshClient == null || !sshClient.isConnected() || !sshClient.isAuthenticated()) { log.info("Reconnecting the session for " + user + "@" + ip); close(); establishConnection(); log.info("Reconnected the session for " + user + "@" + ip); } }
public SSHClientMocker setAuthed(boolean isAuthed) { Mockito.when(client.isAuthenticated()).thenReturn(isAuthed); return this; }
@Override public R run() throws RaspiQueryException { if (delegate.getSSHClient() == null) { throw new IllegalStateException( "You must establish a connection first."); } if (!delegate.getSSHClient().isConnected() || !delegate.getSSHClient().isAuthenticated()) { throw new IllegalStateException( "You must establish a connection first."); } return delegate.run(); } }
@Override public Boolean authenticate(final Host bookmark, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException { if(log.isDebugEnabled()) { log.debug(String.format("Login using none authentication with credentials %s", bookmark.getCredentials())); } try { session.getClient().auth(bookmark.getCredentials().getUsername(), new AuthNone()); return session.getClient().isAuthenticated(); } catch(IOException e) { throw new SFTPExceptionMappingService().map(e); } }
@Test(expected = IllegalStateException.class) public void query_no_auth() throws RaspiQueryException { Mockito.when(sshClient.isConnected()).thenReturn(true); Mockito.when(sshClient.isAuthenticated()).thenReturn(false); raspiQuery.queryVcgencmd(); }
@Override public final String queryDistributionName() throws RaspiQueryException { LOGGER.info("Querying distribution name..."); if (client != null) { if (client.isConnected() && client.isAuthenticated()) { Session session; try { session = client.startSession(); final Command cmd = session.exec(DISTRIBUTION_CMD); cmd.join(30, TimeUnit.SECONDS); return this.parseDistribution(IOUtils.readFully(cmd.getInputStream()).toString().trim()); } catch (IOException e) { throw RaspiQueryException.createTransportFailure(hostname, e); } } else { throw new IllegalStateException( "You must establish a connection first."); } } else { throw new IllegalStateException( "You must establish a connection first."); } }
@Override public final List<DiskUsageBean> queryDiskUsage() throws RaspiQueryException { LOGGER.info("Querying disk usage..."); if (client != null) { if (client.isConnected() && client.isAuthenticated()) { Session session; try { session = client.startSession(); final Command cmd = session.exec(DISK_USAGE_CMD); cmd.join(30, TimeUnit.SECONDS); return this.parseDiskUsage(IOUtils .readFully(cmd.getInputStream()).toString().trim()); } catch (IOException e) { throw RaspiQueryException.createTransportFailure(hostname, e); } } else { throw new IllegalStateException("You must establish a connection first."); } } else { throw new IllegalStateException("You must establish a connection first."); } }
@Override public final List<ProcessBean> queryProcesses(boolean showRootProcesses) throws RaspiQueryException { LOGGER.info("Querying running processes..."); if (client != null) { if (client.isConnected() && client.isAuthenticated()) { Session session; try { session = client.startSession(); final Command cmd = session.exec(showRootProcesses ? PROCESS_ALL : PROCESS_NO_ROOT_CMD); cmd.join(30, TimeUnit.SECONDS); return this.parseProcesses(IOUtils.readFully(cmd.getInputStream()).toString().trim()); } catch (IOException e) { throw RaspiQueryException.createTransportFailure(hostname, e); } } else { throw new IllegalStateException( "You must establish a connection first."); } } else { throw new IllegalStateException( "You must establish a connection first."); } }
@Override public Boolean authenticate(final Host bookmark, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException { if(log.isDebugEnabled()) { log.debug(String.format("Login using agent %s for %s", agent, bookmark)); } for(Identity identity : agent.getIdentities()) { try { session.getClient().auth(bookmark.getCredentials().getUsername(), new AuthAgent(agent.getProxy(), identity)); // Successfully authenticated break; } catch(UserAuthException e) { cancel.verify(); // Continue; } catch(Buffer.BufferException e) { throw new DefaultIOExceptionMappingService().map(e); } catch(TransportException e) { throw new SFTPExceptionMappingService().map(e); } } return session.getClient().isAuthenticated(); }