Search in sources :

Example 1 with UnsupportedCredentialItem

use of org.eclipse.jgit.errors.UnsupportedCredentialItem in project fabric8 by jboss-fuse.

the class OpenShiftDeployAgent method onConfigurationChanged.

protected void onConfigurationChanged() {
    LOGGER.info("Configuration has changed; so checking the Fabric managed Java cartridges on OpenShift are up to date");
    Container[] containers = fabricService.get().getContainers();
    for (Container container : containers) {
        Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService.get(), container.getOverlayProfile());
        Map<String, String> openshiftConfiguration = effectiveProfile.getConfiguration(OpenShiftConstants.OPENSHIFT_PID);
        if (openshiftConfiguration != null) {
            DeploymentUpdater deployTask = null;
            try {
                deployTask = createDeployTask(container, openshiftConfiguration);
            } catch (MalformedURLException e) {
                LOGGER.error("Failed to create DeploymentUpdater. " + e, e);
            }
            if (deployTask != null && OpenShiftUtils.isFabricManaged(openshiftConfiguration)) {
                String containerId = container.getId();
                IOpenShiftConnection connection = OpenShiftUtils.createConnection(container);
                CreateOpenshiftContainerOptions options = OpenShiftUtils.getCreateOptions(container);
                if (connection == null || options == null) {
                    LOGGER.warn("Ignoring container which has no openshift connection or options. connection: " + connection + " options: " + options);
                } else {
                    try {
                        IApplication application = OpenShiftUtils.getApplication(container, connection);
                        if (application != null) {
                            final String gitUrl = application.getGitUrl();
                            if (gitUrl != null) {
                                LOGGER.info("Git URL is " + gitUrl);
                                final CartridgeGitRepository repo = new CartridgeGitRepository(containerId);
                                final List<IOpenShiftSSHKey> sshkeys = application.getDomain().getUser().getSSHKeys();
                                final CredentialsProvider credentials = new CredentialsProvider() {

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

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

                                    @Override
                                    public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
                                        LOGGER.info("Credential request " + uri + " items " + Arrays.asList(items));
                                        int i = -1;
                                        for (CredentialItem item : items) {
                                            if (item instanceof CredentialItem.StringType) {
                                                CredentialItem.StringType stringType = (CredentialItem.StringType) item;
                                                int idx = ++i < sshkeys.size() ? i : 0;
                                                if (idx < sshkeys.size()) {
                                                    IOpenShiftSSHKey sshKey = sshkeys.get(idx);
                                                    String passphrase = sshKey.getPublicKey();
                                                    LOGGER.info("For item " + item + " index " + i + " using passphrase: " + passphrase);
                                                    stringType.setValue(passphrase);
                                                } else {
                                                    LOGGER.warn("No ssh keys we can pass into git!");
                                                }
                                                continue;
                                            } else {
                                                LOGGER.warn("Unknown CredentialItem " + item);
                                            }
                                        }
                                        return true;
                                    }
                                };
                                final DeploymentUpdater finalDeployTask = deployTask;
                                SshSessionFactoryUtils.useOpenShiftSessionFactory(new Callable<Object>() {

                                    @Override
                                    public Object call() throws Exception {
                                        repo.cloneOrPull(gitUrl, credentials);
                                        finalDeployTask.updateDeployment(repo.getGit(), repo.getLocalRepo(), credentials);
                                        return null;
                                    }
                                });
                            }
                        }
                    } catch (Exception e) {
                        LOGGER.error("Failed to update container " + containerId + ". Reason: " + e, e);
                    } finally {
                        OpenShiftUtils.close(connection);
                    }
                }
            }
        }
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) MalformedURLException(java.net.MalformedURLException) CreateOpenshiftContainerOptions(io.fabric8.openshift.CreateOpenshiftContainerOptions) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) IOpenShiftSSHKey(com.openshift.client.IOpenShiftSSHKey) Profile(io.fabric8.api.Profile) MalformedURLException(java.net.MalformedURLException) Container(io.fabric8.api.Container) IApplication(com.openshift.client.IApplication) IOpenShiftConnection(com.openshift.client.IOpenShiftConnection)

Example 2 with UnsupportedCredentialItem

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

the class AwsCodeCommitCredentialsProviderTests method testUriWithCurlyBracesReturnsTrue.

@Test
public void testUriWithCurlyBracesReturnsTrue() throws UnsupportedCredentialItem, URISyntaxException {
    GitCredentialsProviderFactory factory = new GitCredentialsProviderFactory();
    provider = (AwsCodeCommitCredentialProvider) factory.createFor(CURLY_BRACES_REPO, USER, PASSWORD, null);
    CredentialItem[] credentialItems = makeCredentialItems();
    assertTrue(provider.get(new URIish(CURLY_BRACES_REPO), credentialItems));
}
Also used : URIish(org.eclipse.jgit.transport.URIish) GitCredentialsProviderFactory(org.springframework.cloud.config.server.support.GitCredentialsProviderFactory) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) Test(org.junit.Test)

Example 3 with UnsupportedCredentialItem

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

the class AwsCodeCommitCredentialsProviderTests method testThrowsUnsupportedCredentialException.

@Test
public void testThrowsUnsupportedCredentialException() throws URISyntaxException {
    CredentialItem[] goodCredentialItems = makeCredentialItems();
    CredentialItem[] badCredentialItems = new CredentialItem[] { goodCredentialItems[0], goodCredentialItems[1], new CredentialItem.YesNoType("OK?") };
    try {
        provider.get(new URIish(AWS_REPO), badCredentialItems);
        fail("Expected UnsupportedCredentialItem exception");
    } catch (UnsupportedCredentialItem e) {
        assertNotNull(e.getMessage());
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) Test(org.junit.Test)

Example 4 with UnsupportedCredentialItem

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

the class EGitCredentialsProvider method get.

@Override
public boolean get(final URIish uri, final CredentialItem... items) throws UnsupportedCredentialItem {
    if (items.length == 0) {
        return true;
    }
    CredentialItem.Username userItem = null;
    CredentialItem.Password passwordItem = null;
    boolean isSpecial = false;
    for (CredentialItem item : items) {
        if (item instanceof CredentialItem.Username)
            userItem = (CredentialItem.Username) item;
        else if (item instanceof CredentialItem.Password)
            passwordItem = (CredentialItem.Password) item;
        else
            isSpecial = true;
    }
    if (!isSpecial && (userItem != null || passwordItem != null)) {
        UserPasswordCredentials credentials = null;
        if ((user != null) && (password != null))
            credentials = new UserPasswordCredentials(user, password);
        else
            credentials = SecureStoreUtils.getCredentials(uri);
        if (credentials == null) {
            credentials = getCredentialsFromUser(uri);
            if (credentials == null)
                return false;
        }
        if (userItem != null)
            userItem.setValue(credentials.getUser());
        if (passwordItem != null)
            passwordItem.setValue(credentials.getPassword().toCharArray());
        return true;
    }
    // special handling for non-user,non-password type items
    final boolean[] result = new boolean[1];
    PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

        @Override
        public void run() {
            Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
            if (items.length == 1) {
                CredentialItem item = items[0];
                result[0] = getSingleSpecial(shell, uri, item);
            } else {
                result[0] = getMultiSpecial(shell, uri, items);
            }
        }
    });
    return result[0];
}
Also used : Shell(org.eclipse.swt.widgets.Shell) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) UserPasswordCredentials(org.eclipse.egit.core.securestorage.UserPasswordCredentials)

Example 5 with UnsupportedCredentialItem

use of org.eclipse.jgit.errors.UnsupportedCredentialItem in project Android-Password-Store by zeapo.

the class SshConfigSessionFactory method configure.

@Override
protected void configure(OpenSshConfig.Host hc, Session session) {
    session.setConfig("StrictHostKeyChecking", "no");
    session.setConfig("PreferredAuthentications", "publickey,password");
    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) {
                if (item instanceof CredentialItem.Username) {
                    ((CredentialItem.Username) item).setValue(username);
                    continue;
                }
                if (item instanceof CredentialItem.StringType) {
                    ((CredentialItem.StringType) item).setValue(passphrase);
                }
            }
            return true;
        }
    };
    UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
    session.setUserInfo(userInfo);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) UserInfo(com.jcraft.jsch.UserInfo) CredentialsProviderUserInfo(org.eclipse.jgit.transport.CredentialsProviderUserInfo) CredentialsProviderUserInfo(org.eclipse.jgit.transport.CredentialsProviderUserInfo) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider)

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