private <T extends TransportCommand<?, ?>> T configure(T command) { final MirrorCredential c = mirror.credential(); switch (mirror.remoteRepoUri().getScheme()) { case Mirror.SCHEME_GIT_HTTP: case Mirror.SCHEME_GIT_HTTPS: if (c instanceof PasswordMirrorCredential) { configureHttp(command, (PasswordMirrorCredential) c); } break; case Mirror.SCHEME_GIT_SSH: if (c instanceof PasswordMirrorCredential) { configureSsh(command, (PasswordMirrorCredential) c); } else if (c instanceof PublicKeyMirrorCredential) { configureSsh(command, (PublicKeyMirrorCredential) c); } break; } return command; }
@Test public void shouldCloseCloneCommand() throws Exception { // given File fileMock = mock(File.class); Git cloneCommand = mock(Git.class); jGitConnection.setOutputLineConsumerFactory(mock(LineConsumerFactory.class)); when(repository.getWorkTree()).thenReturn(fileMock); when(repository.getDirectory()).thenReturn(fileMock); when(repository.getConfig()).thenReturn(mock(StoredConfig.class)); doReturn(cloneCommand) .when(jGitConnection) .executeRemoteCommand( nullable(String.class), nullable(TransportCommand.class), nullable(String.class), nullable(String.class)); // when jGitConnection.clone(CloneParams.create("url").withWorkingDir("fakePath")); // then verify(cloneCommand).close(); }
/** * Configures the transport of the command to deal with things like SSH */ public static <C extends GitCommand> void configureCommand(TransportCommand<C, ?> command, CredentialsProvider credentialsProvider, final File sshPrivateKey, final File sshPublicKey) { if (sshPrivateKey != null) { final CredentialsProvider provider = credentialsProvider; command.setTransportConfigCallback(new TransportConfigCallback() { @Override public void configure(Transport transport) { if (transport instanceof SshTransport) { SshTransport sshTransport = (SshTransport) transport; SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { @Override protected void configure(OpenSshConfig.Host host, Session session) { session.setConfig("StrictHostKeyChecking", "no"); UserInfo userInfo = new CredentialsProviderUserInfo(session, provider); session.setUserInfo(userInfo); } @Override protected JSch createDefaultJSch(FS fs) throws JSchException { JSch jsch = super.createDefaultJSch(fs); jsch.removeAllIdentity(); String absolutePath = sshPrivateKey.getAbsolutePath(); if (LOG.isDebugEnabled()) { LOG.debug("Adding identity privateKey: " + sshPrivateKey + " publicKey: " + sshPublicKey); } if (sshPublicKey != null) { jsch.addIdentity(absolutePath, sshPublicKey.getAbsolutePath(), null); } else { jsch.addIdentity(absolutePath); } return jsch; } }; sshTransport.setSshSessionFactory(sshSessionFactory); } } }); } }
private void configureCommand(TransportCommand<?, ?> command) { command.setTimeout(this.timeout); if (this.transportConfigCallback != null) { command.setTransportConfigCallback(this.transportConfigCallback); } CredentialsProvider credentialsProvider = getCredentialsProvider(); if (credentialsProvider != null) { command.setCredentialsProvider(credentialsProvider); } }
/** * Setups the Git credentials if specified and needed * * @param command The git command to configure */ @SuppressWarnings("rawtypes") protected void setupCredentials(GitCommand<?> command) { GitSettings settings = lookupSettings(); if (settings != null && command instanceof TransportCommand) { TransportCommand cmd = (TransportCommand) command; cmd.setCredentialsProvider(settings.getCredentials()); } }
private static <T extends TransportCommand<?, ?>> void configureHttp(T cmd, PasswordMirrorCredential cred) { cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(cred.username(), cred.password())); }
@Override protected <T extends TransportCommand> T buildCommand(T command) { command.setTransportConfigCallback(transportConfigCallback); return command; }
@Override protected <T extends TransportCommand> T buildCommand(T command) { command.setCredentialsProvider(usernameProvider); return command; }
/** * Configures the transport of the command to deal with things like SSH */ public static <C extends GitCommand> void configureCommand(TransportCommand<C, ?> command, UserDetails userDetails) { configureCommand(command, userDetails.createCredentialsProvider(), userDetails.getSshPrivateKey(), userDetails.getSshPublicKey()); command.setCredentialsProvider(userDetails.createCredentialsProvider()); }
static RepoHelper cloneRepositoryWithChecks(String remoteURL, Path destinationPath, RepoHelperBuilder.AuthDialogResponse response, UserInfo userInfo) throws GitAPIException, IOException, CancelledAuthorizationException, NoRepoSelectedException { // Always use authentication. If authentication is unneeded (HTTP), it will still work even if the wrong // username password is used. This is what Eclipse does; in fact, it asks for username/password in the // same dialog box that the URL is entered. // // Set up both sets of credentials (username/password pair, as well // as just password. Only one of these will be used, but that is determined automatically via the JGit // callback mechanism. UsernamePasswordCredentialsProvider credentials = new UsernamePasswordCredentialsProvider(response.username, response.password); String sshPassword = response.password; // Try calling `git ls-remote ___` on the remote URL to see if it's valid TransportCommand command = Git.lsRemoteRepository().setRemote(remoteURL); ClonedRepoHelper repoHelper = new ClonedRepoHelper(destinationPath, remoteURL, sshPassword, userInfo); repoHelper.wrapAuthentication(command, credentials); try { command.call(); } catch (TransportException e) { // If the URL doesn't have a repo, a Transport Exception is thrown when this command is called. // We want the SessionController to report an InvalidRemoteException, though, because // that's the issue. System.out.println(e.getMessage()); System.out.println(e.getMessage().endsWith("not authorized") || e.getMessage().endsWith("not authorized.")); System.out.println(e.getMessage().endsWith("not found") || e.getMessage().endsWith("not found.")); /*Throwable exception = e; while (exception != null) { System.err.println("======================"); exception.printStackTrace(); exception = exception.getCause(); }*/ if (e.getMessage().endsWith("not found") || e.getMessage().endsWith("not found.")) { logger.error("Invalid remote exception thrown"); throw new InvalidRemoteException("Caught invalid repository when building a ClonedRepoHelper."); } else if (e.getMessage().endsWith("not authorized") || e.getMessage().endsWith("not authorized.")) { logger.error("Authentication error"); } } // Without the above try/catch block, the next line would run and throw the desired InvalidRemoteException, // but it would create a destination folder for the repo before stopping. By catching the error above, // we prevent unnecessary folder creation. By making it to this point, we've verified that the repo // is valid and that we can authenticate to it. if (response.protocol == AuthMethod.SSH) { repoHelper.obtainRepository(remoteURL); } else { repoHelper.setUsernamePasswordCredentials(credentials); repoHelper.obtainRepository(remoteURL); } return repoHelper; }
private void addAuth(TransportCommand command) { command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(oAuthToken, "")); }
private TransportCommand addCredentials(TransportCommand command) { if (credentials != null) command.setCredentialsProvider(credentials); return command; }
/** * Git command builder */ protected abstract <T extends TransportCommand> T buildCommand(T command);