Search in sources :

Example 6 with UnsupportedCredentialItem

use of org.eclipse.jgit.errors.UnsupportedCredentialItem in project bndtools by bndtools.

the class GitCredentialsProvider method get.

@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
    Mapping mapping = repo.findMapping(uri.toString());
    if (mapping != null) {
        for (CredentialItem item : items) {
            if (item instanceof CredentialItem.Username) {
                ((CredentialItem.Username) item).setValue(mapping.user);
                continue;
            }
            if (item instanceof CredentialItem.Password) {
                ((CredentialItem.Password) item).setValue(mapping.pass);
                continue;
            }
            // Usually Passphrase
            if (item instanceof CredentialItem.StringType && item.isValueSecure()) {
                ((CredentialItem.StringType) item).setValue(new String(mapping.pass));
                continue;
            }
        }
        return true;
    }
    if (isInteractive()) {
        JComponent[] inputs = getSwingUI(items);
        int result = JOptionPane.showConfirmDialog(null, inputs, "Enter credentials for " + repo.getName(), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result != JOptionPane.OK_OPTION) {
            return false;
        }
        updateCredentialItems(inputs);
        return true;
    }
    return false;
}
Also used : JComponent(javax.swing.JComponent) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) Mapping(bndtools.bndplugins.repo.git.GitOBRRepo.Mapping)

Example 7 with UnsupportedCredentialItem

use of org.eclipse.jgit.errors.UnsupportedCredentialItem in project cas by apereo.

the class GitRepositoryTests method verifyPushPull.

@Test
public void verifyPushPull() throws Exception {
    val props = casProperties.getServiceRegistry().getGit();
    props.setRepositoryUrl("https://github.com/mmoayyed/sample-data.git");
    props.setBranchesToClone("master");
    props.setStrictHostKeyChecking(false);
    props.setClearExistingIdentities(true);
    props.getCloneDirectory().setLocation(ResourceUtils.getRawResourceFrom(FileUtils.getTempDirectoryPath() + File.separator + UUID.randomUUID()));
    val repo = GitRepositoryBuilder.newInstance(props).build();
    assertTrue(repo.pull());
    assertFalse(repo.getObjectsInRepository().isEmpty());
    repo.getCredentialsProvider().add(new CredentialsProvider() {

        @Override
        public boolean isInteractive() {
            return false;
        }

        @Override
        public boolean supports(final CredentialItem... items) {
            return true;
        }

        @Override
        public boolean get(final URIish uri, final CredentialItem... items) throws UnsupportedCredentialItem {
            return true;
        }
    });
    try {
        repo.commitAll("Test");
        repo.push();
        fail("Pushing changes should fail");
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : lombok.val(lombok.val) URIish(org.eclipse.jgit.transport.URIish) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with UnsupportedCredentialItem

use of org.eclipse.jgit.errors.UnsupportedCredentialItem in project omegat by omegat-org.

the class GITCredentialsProvider method get.

@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
    // get predefined if exist
    String url = uri.toString();
    String predefinedUser = predefined.get("user." + url);
    String predefinedPass = predefined.get("pass." + url);
    String predefinedFingerprint = predefined.get("fingerprint." + url);
    // get saved
    Credentials credentials = loadCredentials(uri);
    boolean ok = false;
    // theoretically, username can be unknown, but in practice it is always set, so not requested.
    for (CredentialItem i : items) {
        if (i instanceof CredentialItem.Username) {
            if (predefinedUser != null && predefinedPass != null) {
                ((CredentialItem.Username) i).setValue(predefinedUser);
                continue;
            }
            if (credentials.username == null) {
                credentials = askCredentials(uri, credentials);
                if (credentials == null) {
                    throw new UnsupportedCredentialItem(uri, OStrings.getString("TEAM_CREDENTIALS_DENIED"));
                }
                saveCredentials(uri, credentials);
                ok = true;
            }
            ((CredentialItem.Username) i).setValue(credentials.username);
            continue;
        } else if (i instanceof CredentialItem.Password) {
            if (predefinedUser != null && predefinedPass != null) {
                ((CredentialItem.Password) i).setValue(predefinedPass.toCharArray());
                continue;
            }
            if (credentials.password == null) {
                credentials = askCredentials(uri, credentials);
                if (credentials == null) {
                    throw new UnsupportedCredentialItem(uri, OStrings.getString("TEAM_CREDENTIALS_DENIED"));
                }
                saveCredentials(uri, credentials);
                ok = true;
            }
            ((CredentialItem.Password) i).setValue(credentials.password.toCharArray());
            continue;
        } else if (i instanceof CredentialItem.StringType) {
            if (i.getPromptText().equals("Password: ")) {
                if (predefinedUser != null && predefinedPass != null) {
                    ((CredentialItem.StringType) i).setValue(predefinedPass);
                    continue;
                }
                if (credentials.password == null) {
                    if (!ok) {
                        credentials = askCredentials(uri, credentials);
                        if (credentials == null) {
                            throw new UnsupportedCredentialItem(uri, OStrings.getString("TEAM_CREDENTIALS_DENIED"));
                        }
                        saveCredentials(uri, credentials);
                    }
                }
                ((CredentialItem.StringType) i).setValue(credentials.password);
                continue;
            } else if (i.getPromptText().startsWith("Passphrase for ")) {
                // Private key passphrase
                if (!ok) {
                    String passphrase = askPassphrase(i.getPromptText());
                    if (passphrase == null) {
                        throw new UnsupportedCredentialItem(uri, OStrings.getString("TEAM_CREDENTIALS_DENIED"));
                    }
                    ((CredentialItem.StringType) i).setValue(passphrase);
                    continue;
                }
            }
        } else if (i instanceof CredentialItem.YesNoType) {
            // e.g.: The authenticity of host 'mygitserver' can't be established.
            // RSA key fingerprint is e2:d3:84:d5:86:e7:68:69:a0:aa:a6:ad:a3:a0:ab:a2.
            // Are you sure you want to continue connecting?
            String storedFingerprint = loadFingerprint(uri);
            String promptText = i.getPromptText();
            String promptedFingerprint = extractFingerprint(promptText);
            if (promptedFingerprint == null) {
                throw new UnsupportedCredentialItem(uri, "Wrong fingerprint pattern");
            }
            if (predefinedFingerprint != null) {
                if (promptedFingerprint.equals(predefinedFingerprint)) {
                    ((CredentialItem.YesNoType) i).setValue(true);
                } else {
                    ((CredentialItem.YesNoType) i).setValue(false);
                }
                continue;
            }
            if (promptedFingerprint.equals(storedFingerprint)) {
                ((CredentialItem.YesNoType) i).setValue(true);
                continue;
            }
            int choice = Core.getMainWindow().showConfirmDialog(promptText, null, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if (choice == JOptionPane.YES_OPTION) {
                ((CredentialItem.YesNoType) i).setValue(true);
                saveFingerprint(uri, promptedFingerprint);
            } else {
                ((CredentialItem.YesNoType) i).setValue(false);
            }
            continue;
        } else if (i instanceof CredentialItem.InformationalMessage) {
            Core.getMainWindow().showMessageDialog(i.getPromptText());
            continue;
        }
        throw new UnsupportedCredentialItem(uri, i.getClass().getName() + ":" + i.getPromptText());
    }
    return true;
}
Also used : UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem)

Example 9 with UnsupportedCredentialItem

use of org.eclipse.jgit.errors.UnsupportedCredentialItem in project opsgenie-configuration-backup by opsgenie.

the class BaseBackup method cloneGit.

void cloneGit(final BackupProperties properties) throws GitAPIException {
    properties.setPath(properties.getPath() + "/OpsGenieBackupGitRepository");
    String rootPath = properties.getPath();
    File backupGitDirectory = new File(rootPath);
    if (backupGitDirectory.exists() && (backupGitDirectory.list() != null) && (backupGitDirectory.list().length > 0)) {
        logger.warn("Destination path " + rootPath + " already exists and is not an empty directory");
        logger.warn("Destination path " + rootPath + " will be deleted inorder to clone remote git repository");
        BackupUtils.deleteDirectory(backupGitDirectory);
        backupGitDirectory.mkdirs();
    } else {
        backupGitDirectory.mkdirs();
    }
    final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {

        @Override
        protected void configure(OpenSshConfig.Host hc, Session session) {
            session.setConfig("StrictHostKeyChecking", "no");
            CredentialsProvider provider = new CredentialsProvider() {

                @Override
                public boolean isInteractive() {
                    return false;
                }

                @Override
                public boolean supports(CredentialItem... items) {
                    return true;
                }

                @Override
                public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
                    for (CredentialItem item : items) {
                        ((CredentialItem.StringType) item).setValue(backupProperties.getPassphrase());
                    }
                    return true;
                }
            };
            UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
            session.setUserInfo(userInfo);
        }

        protected JSch createDefaultJSch(FS fs) throws JSchException {
            JSch defaultJSch = super.createDefaultJSch(fs);
            defaultJSch.removeAllIdentity();
            defaultJSch.addIdentity(properties.getSshKeyPath());
            return defaultJSch;
        }
    };
    callBack = new TransportConfigCallback() {

        public void configure(Transport transport) {
            SshTransport sshTransport = (SshTransport) transport;
            sshTransport.setSshSessionFactory(sshSessionFactory);
        }
    };
    logger.info("Cloning remote git operation started!");
    CloneCommand cloneCommand = Git.cloneRepository();
    cloneCommand.setURI(properties.getGitSshUri());
    cloneCommand.setTransportConfigCallback(callBack);
    StatusLogger.getLogger().setLevel(Level.OFF);
    cloneCommand.setDirectory(backupGitDirectory);
    git = cloneCommand.call();
    logger.info("Cloning remote git operation finished!");
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) UserInfo(com.jcraft.jsch.UserInfo) JSch(com.jcraft.jsch.JSch) FS(org.eclipse.jgit.util.FS) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 10 with UnsupportedCredentialItem

use of org.eclipse.jgit.errors.UnsupportedCredentialItem in project spring-cloud-config by spring-cloud.

the class AwsCodeCommitCredentialProvider method get.

/**
 * Get the username and password to use for the given uri.
 * @see org.eclipse.jgit.transport.CredentialsProvider#get(org.eclipse.jgit.transport.URIish, org.eclipse.jgit.transport.CredentialItem[])
 */
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
    String codeCommitPassword;
    String awsAccessKey;
    String awsSecretKey;
    try {
        AWSCredentials awsCredentials = retrieveAwsCredentials();
        StringBuilder awsKey = new StringBuilder();
        awsKey.append(awsCredentials.getAWSAccessKeyId());
        awsSecretKey = awsCredentials.getAWSSecretKey();
        if (awsCredentials instanceof AWSSessionCredentials) {
            AWSSessionCredentials sessionCreds = (AWSSessionCredentials) awsCredentials;
            if (sessionCreds.getSessionToken() != null) {
                awsKey.append('%').append(sessionCreds.getSessionToken());
            }
        }
        awsAccessKey = awsKey.toString();
    } catch (Throwable t) {
        logger.warn("Unable to retrieve AWS Credentials", t);
        return false;
    }
    try {
        codeCommitPassword = calculateCodeCommitPassword(uri, awsSecretKey);
    } catch (Throwable t) {
        logger.warn("Error calculating the AWS CodeCommit password", t);
        return false;
    }
    for (CredentialItem i : items) {
        if (i instanceof CredentialItem.Username) {
            ((CredentialItem.Username) i).setValue(awsAccessKey);
            logger.trace("Returning username " + awsAccessKey);
            continue;
        }
        if (i instanceof CredentialItem.Password) {
            ((CredentialItem.Password) i).setValue(codeCommitPassword.toCharArray());
            logger.trace("Returning password " + codeCommitPassword);
            continue;
        }
        if (i instanceof CredentialItem.StringType && i.getPromptText().equals("Password: ")) {
            // $NON-NLS-1$
            ((CredentialItem.StringType) i).setValue(codeCommitPassword);
            logger.trace("Returning password string " + codeCommitPassword);
            continue;
        }
        // $NON-NLS-1$
        throw new UnsupportedCredentialItem(uri, i.getClass().getName() + ":" + i.getPromptText());
    }
    return true;
}
Also used : UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) AWSSessionCredentials(com.amazonaws.auth.AWSSessionCredentials) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials)

Aggregations

UnsupportedCredentialItem (org.eclipse.jgit.errors.UnsupportedCredentialItem)11 CredentialItem (org.eclipse.jgit.transport.CredentialItem)10 URIish (org.eclipse.jgit.transport.URIish)5 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)3 UserInfo (com.jcraft.jsch.UserInfo)2 Test (org.junit.Test)2 Mapping (bndtools.bndplugins.repo.git.GitOBRRepo.Mapping)1 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 AWSSessionCredentials (com.amazonaws.auth.AWSSessionCredentials)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 JSch (com.jcraft.jsch.JSch)1 Session (com.jcraft.jsch.Session)1 IApplication (com.openshift.client.IApplication)1 IOpenShiftConnection (com.openshift.client.IOpenShiftConnection)1 IOpenShiftSSHKey (com.openshift.client.IOpenShiftSSHKey)1 Container (io.fabric8.api.Container)1 Profile (io.fabric8.api.Profile)1 CreateOpenshiftContainerOptions (io.fabric8.openshift.CreateOpenshiftContainerOptions)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1