Search in sources :

Example 6 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.

the class GitContentRepository method pushToRemote.

@Override
public boolean pushToRemote(String siteId, String remoteName, String remoteBranch) throws ServiceLayerException, InvalidRemoteUrlException, CryptoException {
    logger.debug("Get remote data from database for remote " + remoteName + " and site " + siteId);
    Map<String, String> params = new HashMap<String, String>();
    params.put("siteId", siteId);
    params.put("remoteName", remoteName);
    RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
    logger.debug("Prepare push command.");
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(siteId, SANDBOX);
    try (Git git = new Git(repo)) {
        Iterable<PushResult> pushResultIterable = null;
        PushCommand pushCommand = git.push();
        logger.debug("Set remote " + remoteName);
        pushCommand.setRemote(remoteRepository.getRemoteName());
        logger.debug("Set branch to be " + remoteBranch);
        RefSpec r = new RefSpec();
        r = r.setSourceDestination(Constants.R_HEADS + repo.getBranch(), Constants.R_HEADS + remoteBranch);
        pushCommand.setRefSpecs(r);
        switch(remoteRepository.getAuthenticationType()) {
            case NONE:
                logger.debug("No authentication");
                pushResultIterable = pushCommand.call();
                break;
            case BASIC:
                logger.debug("Basic authentication");
                String hashedPassword = remoteRepository.getRemotePassword();
                String password = encryptor.decrypt(hashedPassword);
                pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteRepository.getRemoteUsername(), password));
                pushResultIterable = pushCommand.call();
                break;
            case TOKEN:
                logger.debug("Token based authentication");
                String hashedToken = remoteRepository.getRemoteToken();
                String remoteToken = encryptor.decrypt(hashedToken);
                pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteToken, EMPTY));
                pushResultIterable = pushCommand.call();
                break;
            case PRIVATE_KEY:
                logger.debug("Private key authentication");
                final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
                String hashedPrivateKey = remoteRepository.getRemotePrivateKey();
                String privateKey = encryptor.decrypt(hashedPrivateKey);
                tempKey.toFile().deleteOnExit();
                pushCommand.setTransportConfigCallback(new TransportConfigCallback() {

                    @Override
                    public void configure(Transport transport) {
                        SshTransport sshTransport = (SshTransport) transport;
                        sshTransport.setSshSessionFactory(getSshSessionFactory(privateKey, tempKey));
                    }
                });
                pushResultIterable = retryingRepositoryOperationFacade.call(pushCommand);
                Files.delete(tempKey);
                break;
            default:
                throw new ServiceLayerException("Unsupported authentication type " + remoteRepository.getAuthenticationType());
        }
        boolean toRet = true;
        List<RemoteRefUpdate.Status> failure = Arrays.asList(REJECTED_NODELETE, REJECTED_NONFASTFORWARD, REJECTED_REMOTE_CHANGED, REJECTED_OTHER_REASON);
        for (PushResult pushResult : pushResultIterable) {
            Collection<RemoteRefUpdate> updates = pushResult.getRemoteUpdates();
            for (RemoteRefUpdate remoteRefUpdate : updates) {
                toRet = toRet && !failure.contains(remoteRefUpdate.getStatus());
                if (!toRet)
                    break;
            }
            if (!toRet)
                break;
        }
        return toRet;
    } catch (InvalidRemoteException e) {
        logger.error("Remote is invalid " + remoteName, e);
        throw new InvalidRemoteUrlException();
    } catch (IOException | JGitInternalException | GitAPIException | CryptoException e) {
        logger.error("Error while pushing to remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
        throw new ServiceLayerException("Error while pushing to remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
    }
}
Also used : HashMap(java.util.HashMap) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RefSpec(org.eclipse.jgit.transport.RefSpec) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) Path(java.nio.file.Path) Status(org.eclipse.jgit.api.Status) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PushResult(org.eclipse.jgit.transport.PushResult) IOException(java.io.IOException) PushCommand(org.eclipse.jgit.api.PushCommand) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) SshTransport(org.eclipse.jgit.transport.SshTransport) Transport(org.eclipse.jgit.transport.Transport) CryptoException(org.craftercms.commons.crypto.CryptoException) SshTransport(org.eclipse.jgit.transport.SshTransport)

Example 7 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.

the class GitContentRepository method insertRemoteToDb.

private void insertRemoteToDb(String siteId, String remoteName, String remoteUrl, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey) throws CryptoException {
    logger.debug("Inserting remote " + remoteName + " for site " + siteId + " into database.");
    Map<String, String> params = new HashMap<String, String>();
    params.put("siteId", siteId);
    params.put("remoteName", remoteName);
    params.put("remoteUrl", remoteUrl);
    params.put("authenticationType", authenticationType);
    params.put("remoteUsername", remoteUsername);
    if (StringUtils.isNotEmpty(remotePassword)) {
        logger.debug("Encrypt password before inserting to database");
        String hashedPassword = encryptor.encrypt(remotePassword);
        params.put("remotePassword", hashedPassword);
    } else {
        params.put("remotePassword", remotePassword);
    }
    if (StringUtils.isNotEmpty(remoteToken)) {
        logger.debug("Encrypt token before inserting to database");
        String hashedToken = encryptor.encrypt(remoteToken);
        params.put("remoteToken", hashedToken);
    } else {
        params.put("remoteToken", remoteToken);
    }
    if (StringUtils.isNotEmpty(remotePrivateKey)) {
        logger.debug("Encrypt private key before inserting to database");
        String hashedPrivateKey = encryptor.encrypt(remotePrivateKey);
        params.put("remotePrivateKey", hashedPrivateKey);
    } else {
        params.put("remotePrivateKey", remotePrivateKey);
    }
    logger.debug("Insert site remote record into database");
    remoteRepositoryDAO.insertRemoteRepository(params);
    params = new HashMap<String, String>();
    params.put("siteId", siteId);
    params.put("remoteName", remoteName);
    RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
    if (remoteRepository != null) {
        insertClusterRemoteRepository(remoteRepository);
    }
}
Also used : HashMap(java.util.HashMap) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository)

Example 8 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.

the class GitContentRepository method listRemote.

@Override
public List<RemoteRepositoryInfoTO> listRemote(String siteId, String sandboxBranch) throws ServiceLayerException, CryptoException {
    List<RemoteRepositoryInfoTO> res = new ArrayList<RemoteRepositoryInfoTO>();
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    try (Repository repo = helper.getRepository(siteId, SANDBOX)) {
        try (Git git = new Git(repo)) {
            List<RemoteConfig> resultRemotes = git.remoteList().call();
            if (CollectionUtils.isNotEmpty(resultRemotes)) {
                for (RemoteConfig conf : resultRemotes) {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("siteId", siteId);
                    params.put("remoteName", conf.getName());
                    RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
                    if (remoteRepository != null) {
                        switch(remoteRepository.getAuthenticationType()) {
                            case NONE:
                                logger.debug("No authentication");
                                git.fetch().setRemote(conf.getName()).call();
                                break;
                            case BASIC:
                                logger.debug("Basic authentication");
                                String hashedPassword = remoteRepository.getRemotePassword();
                                String password = encryptor.decrypt(hashedPassword);
                                git.fetch().setRemote(conf.getName()).setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteRepository.getRemoteUsername(), password)).call();
                                break;
                            case TOKEN:
                                logger.debug("Token based authentication");
                                String hashedToken = remoteRepository.getRemoteToken();
                                String remoteToken = encryptor.decrypt(hashedToken);
                                git.fetch().setRemote(conf.getName()).setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteToken, EMPTY)).call();
                                break;
                            case PRIVATE_KEY:
                                logger.debug("Private key authentication");
                                final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
                                String hashedPrivateKey = remoteRepository.getRemotePrivateKey();
                                String privateKey = encryptor.decrypt(hashedPrivateKey);
                                tempKey.toFile().deleteOnExit();
                                git.fetch().setRemote(conf.getName()).setTransportConfigCallback(new TransportConfigCallback() {

                                    @Override
                                    public void configure(Transport transport) {
                                        SshTransport sshTransport = (SshTransport) transport;
                                        sshTransport.setSshSessionFactory(getSshSessionFactory(privateKey, tempKey));
                                    }
                                }).call();
                                Files.delete(tempKey);
                                break;
                            default:
                                throw new ServiceLayerException("Unsupported authentication type " + remoteRepository.getAuthenticationType());
                        }
                    }
                }
                List<Ref> resultRemoteBranches = git.branchList().setListMode(REMOTE).call();
                Map<String, List<String>> remoteBranches = new HashMap<String, List<String>>();
                for (Ref remoteBranchRef : resultRemoteBranches) {
                    String branchFullName = remoteBranchRef.getName().replace(R_REMOTES, "");
                    String remotePart = EMPTY;
                    String branchNamePart = EMPTY;
                    int slashIndex = branchFullName.indexOf("/");
                    if (slashIndex > 0) {
                        remotePart = branchFullName.substring(0, slashIndex);
                        branchNamePart = branchFullName.substring(slashIndex + 1);
                    }
                    if (!remoteBranches.containsKey(remotePart)) {
                        remoteBranches.put(remotePart, new ArrayList<String>());
                    }
                    remoteBranches.get(remotePart).add(branchNamePart);
                }
                String sandboxBranchName = sandboxBranch;
                if (StringUtils.isEmpty(sandboxBranchName)) {
                    sandboxBranchName = studioConfiguration.getProperty(REPO_SANDBOX_BRANCH);
                }
                for (RemoteConfig conf : resultRemotes) {
                    RemoteRepositoryInfoTO rri = new RemoteRepositoryInfoTO();
                    rri.setName(conf.getName());
                    List<String> branches = remoteBranches.get(rri.getName());
                    if (CollectionUtils.isEmpty(branches)) {
                        branches = new ArrayList<String>();
                        branches.add(sandboxBranchName);
                    }
                    rri.setBranches(branches);
                    StringBuilder sbUrl = new StringBuilder();
                    if (CollectionUtils.isNotEmpty(conf.getURIs())) {
                        for (int i = 0; i < conf.getURIs().size(); i++) {
                            sbUrl.append(conf.getURIs().get(i).toString());
                            if (i < conf.getURIs().size() - 1) {
                                sbUrl.append(":");
                            }
                        }
                    }
                    rri.setUrl(sbUrl.toString());
                    StringBuilder sbFetch = new StringBuilder();
                    if (CollectionUtils.isNotEmpty(conf.getFetchRefSpecs())) {
                        for (int i = 0; i < conf.getFetchRefSpecs().size(); i++) {
                            sbFetch.append(conf.getFetchRefSpecs().get(i).toString());
                            if (i < conf.getFetchRefSpecs().size() - 1) {
                                sbFetch.append(":");
                            }
                        }
                    }
                    rri.setFetch(sbFetch.toString());
                    StringBuilder sbPushUrl = new StringBuilder();
                    if (CollectionUtils.isNotEmpty(conf.getPushURIs())) {
                        for (int i = 0; i < conf.getPushURIs().size(); i++) {
                            sbPushUrl.append(conf.getPushURIs().get(i).toString());
                            if (i < conf.getPushURIs().size() - 1) {
                                sbPushUrl.append(":");
                            }
                        }
                    } else {
                        sbPushUrl.append(rri.getUrl());
                    }
                    rri.setPush_url(sbPushUrl.toString());
                    res.add(rri);
                }
            }
        } catch (GitAPIException | CryptoException | IOException e) {
            logger.error("Error getting remote repositories for site " + siteId, e);
        }
    }
    return res;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) ArrayList(java.util.ArrayList) List(java.util.List) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) Path(java.nio.file.Path) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) RemoteRepositoryInfoTO(org.craftercms.studio.api.v1.to.RemoteRepositoryInfoTO) SshTransport(org.eclipse.jgit.transport.SshTransport) Transport(org.eclipse.jgit.transport.Transport) CryptoException(org.craftercms.commons.crypto.CryptoException) SshTransport(org.eclipse.jgit.transport.SshTransport)

Example 9 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.

the class DbEncryptionUpgradeOperation method upgradeRemoteRepositories.

protected void upgradeRemoteRepositories(NamedParameterJdbcTemplate jdbcTemplate) throws CryptoException {
    logger.debug("Looking for remote repositories to upgrade");
    List<RemoteRepository> remotes = jdbcTemplate.query(REMOTE_REPOSITORIES_QUERY, new BeanPropertyRowMapper<>(RemoteRepository.class));
    logger.debug("Found {0} remote repositories", remotes.size());
    if (isEmpty(remotes)) {
        return;
    }
    for (RemoteRepository remote : remotes) {
        logger.debug("Upgrading remote repository with id: {0}", remote.getId());
        switch(remote.getAuthenticationType()) {
            case RemoteRepository.AuthenticationType.BASIC:
                remote.setRemotePassword(upgradeValue(remote.getRemotePassword()));
                break;
            case RemoteRepository.AuthenticationType.TOKEN:
                remote.setRemoteToken(upgradeValue(remote.getRemoteToken()));
                break;
            case RemoteRepository.AuthenticationType.PRIVATE_KEY:
                remote.setRemotePrivateKey(upgradeValue(remote.getRemotePrivateKey()));
                break;
            default:
                logger.warn("Unknown authentication type {0} for remote repository with id {1}", remote.getAuthenticationType(), remote.getId());
        }
    }
    jdbcTemplate.batchUpdate(REMOTE_REPOSITORIES_UPDATE, remotes.stream().map(BeanPropertySqlParameterSource::new).toArray(BeanPropertySqlParameterSource[]::new));
}
Also used : BeanPropertySqlParameterSource(org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository)

Example 10 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.

the class GitContentRepository method insertClusterRemoteRepository.

public void insertClusterRemoteRepository(RemoteRepository remoteRepository) {
    HierarchicalConfiguration<ImmutableNode> registrationData = studioConfiguration.getSubConfig(CLUSTERING_NODE_REGISTRATION);
    if (registrationData != null && !registrationData.isEmpty()) {
        String localAddress = registrationData.getString(CLUSTER_MEMBER_LOCAL_ADDRESS);
        ClusterMember member = clusterDao.getMemberByLocalAddress(localAddress);
        if (member != null) {
            retryingOperationFacade.addClusterRemoteRepository(member.getId(), remoteRepository.getId());
        }
    }
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode)

Aggregations

RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)13 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)8 IOException (java.io.IOException)7 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)7 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)7 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)7 Git (org.eclipse.jgit.api.Git)7 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)7 Repository (org.eclipse.jgit.lib.Repository)7 Path (java.nio.file.Path)6 CryptoException (org.craftercms.commons.crypto.CryptoException)6 HashMap (java.util.HashMap)5 ClusterMember (org.craftercms.studio.api.v2.dal.ClusterMember)4 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)4 ArrayList (java.util.ArrayList)3 TransportConfigCallback (org.eclipse.jgit.api.TransportConfigCallback)3 SshTransport (org.eclipse.jgit.transport.SshTransport)3 Transport (org.eclipse.jgit.transport.Transport)3 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)3 URISyntaxException (java.net.URISyntaxException)2