Search in sources :

Example 1 with FS

use of org.eclipse.jgit.util.FS 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 2 with FS

use of org.eclipse.jgit.util.FS in project egit by eclipse.

the class GitTestCase method setUp.

@Before
public void setUp() throws Exception {
    // ensure there are no shared Repository instances left
    // when starting a new test
    Activator.getDefault().getRepositoryCache().clear();
    File configFile = File.createTempFile("gitconfigtest", "config");
    MockSystemReader mockSystemReader = new MockSystemReader() {

        @Override
        public FileBasedConfig openUserConfig(Config parent, FS fs) {
            return new FileBasedConfig(parent, configFile, fs);
        }
    };
    configFile.deleteOnExit();
    SystemReader.setInstance(mockSystemReader);
    mockSystemReader.setProperty(Constants.GIT_CEILING_DIRECTORIES_KEY, ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getParentFile().getAbsoluteFile().toString());
    FileBasedConfig userConfig = mockSystemReader.openUserConfig(null, FS.DETECTED);
    // We have to set autoDetach to false for tests, because tests expect to
    // be able to clean up by recursively removing the repository, and
    // background GC might be in the middle of writing or deleting files,
    // which would disrupt this.
    userConfig.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null, ConfigConstants.CONFIG_KEY_AUTODETACH, false);
    userConfig.save();
    project = new TestProject(true);
    gitDir = new File(project.getProject().getWorkspace().getRoot().getRawLocation().toFile(), Constants.DOT_GIT);
    if (gitDir.exists())
        FileUtils.delete(gitDir, FileUtils.RECURSIVE | FileUtils.RETRY);
}
Also used : Config(org.eclipse.jgit.lib.Config) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) MockSystemReader(org.eclipse.jgit.junit.MockSystemReader) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File) FS(org.eclipse.jgit.util.FS) Before(org.junit.Before)

Example 3 with FS

use of org.eclipse.jgit.util.FS in project gitblit by gitblit.

the class SshUnitTest method startGitblit.

@BeforeClass
public static void startGitblit() throws Exception {
    generator = SecurityUtils.getKeyPairGenerator("RSA");
    started.set(GitBlitSuite.startGitblit());
    final SystemReader dsr = SystemReader.getInstance();
    SystemReader.setInstance(new SystemReader() {

        final SystemReader defaultsr = dsr;

        @Override
        public String getHostname() {
            return defaultsr.getHostname();
        }

        @Override
        public String getenv(String variable) {
            if ("GIT_SSH".equalsIgnoreCase(variable)) {
                return null;
            }
            return defaultsr.getenv(variable);
        }

        @Override
        public String getProperty(String key) {
            return defaultsr.getProperty(key);
        }

        @Override
        public FileBasedConfig openUserConfig(Config parent, FS fs) {
            return defaultsr.openUserConfig(parent, fs);
        }

        @Override
        public FileBasedConfig openSystemConfig(Config parent, FS fs) {
            return defaultsr.openSystemConfig(parent, fs);
        }

        @Override
        public long getCurrentTime() {
            return defaultsr.getCurrentTime();
        }

        @Override
        public int getTimezone(long when) {
            return defaultsr.getTimezone(when);
        }
    });
}
Also used : Config(org.eclipse.jgit.lib.Config) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) SystemReader(org.eclipse.jgit.util.SystemReader) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) FS(org.eclipse.jgit.util.FS) BeforeClass(org.junit.BeforeClass)

Example 4 with FS

use of org.eclipse.jgit.util.FS in project gerrit by GerritCodeReview.

the class SshSessionMina method getMinaSession.

private SshdSession getMinaSession() throws Exception {
    if (session == null) {
        String username = getUsername();
        URIish uri = new URIish("ssh://" + username + "@" + addr.getAddress().getHostAddress() + ":" + addr.getPort());
        // TODO(davido): Switch to memory only key resolving mode.
        File userhome = createTempDirectory("home-").toFile();
        FS fs = FS.DETECTED.setUserHome(userhome);
        File sshDir = new File(userhome, ".ssh");
        sshDir.mkdir();
        OpenSSHKeyPairResourceWriter keyPairWriter = new OpenSSHKeyPairResourceWriter();
        try (OutputStream out = new FileOutputStream(new File(sshDir, "id_ecdsa"))) {
            keyPairWriter.writePrivateKey(sshKeys.getKeyPair(account), null, null, out);
        }
        // TODO(davido): Disable programmatically host key checking: "StrictHostKeyChecking: no" mode.
        CharSink configFile = Files.asCharSink(new File(sshDir, "config"), UTF_8);
        configFile.writeLines(Arrays.asList("Host *", "StrictHostKeyChecking no"));
        JGitKeyCache keyCache = new JGitKeyCache();
        try (SshdSessionFactory factory = new SshdSessionFactory(keyCache, new DefaultProxyDataFactory())) {
            factory.setHomeDirectory(userhome);
            factory.setSshDirectory(sshDir);
            session = factory.getSession(uri, null, fs, TIMEOUT);
            session.addCloseListener(future -> {
                try {
                    MoreFiles.deleteRecursively(userhome.toPath(), ALLOW_INSECURE);
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException("Failed to cleanup userhome", e);
                }
            });
        }
    }
    return session;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) CharSink(com.google.common.io.CharSink) JGitKeyCache(org.eclipse.jgit.transport.sshd.JGitKeyCache) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DefaultProxyDataFactory(org.eclipse.jgit.transport.sshd.DefaultProxyDataFactory) IOException(java.io.IOException) FS(org.eclipse.jgit.util.FS) OpenSSHKeyPairResourceWriter(org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyPairResourceWriter) SshdSessionFactory(org.eclipse.jgit.transport.sshd.SshdSessionFactory) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 5 with FS

use of org.eclipse.jgit.util.FS in project gerrit by GerritCodeReview.

the class StandaloneSiteTest method setFakeSystemReader.

private static SystemReader setFakeSystemReader(File tempDir) {
    SystemReader oldSystemReader = SystemReader.getInstance();
    SystemReader.setInstance(new DelegateSystemReader(oldSystemReader) {

        @Override
        public FileBasedConfig openJGitConfig(Config parent, FS fs) {
            return new FileBasedConfig(parent, new File(tempDir, "jgit.config"), FS.detect());
        }

        @Override
        public FileBasedConfig openUserConfig(Config parent, FS fs) {
            return new FileBasedConfig(parent, new File(tempDir, "user.config"), FS.detect());
        }

        @Override
        public FileBasedConfig openSystemConfig(Config parent, FS fs) {
            return new FileBasedConfig(parent, new File(tempDir, "system.config"), FS.detect());
        }
    });
    return oldSystemReader;
}
Also used : DelegateSystemReader(com.google.gerrit.server.util.git.DelegateSystemReader) Config(org.eclipse.jgit.lib.Config) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) DelegateSystemReader(com.google.gerrit.server.util.git.DelegateSystemReader) SystemReader(org.eclipse.jgit.util.SystemReader) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) FS(org.eclipse.jgit.util.FS) File(java.io.File)

Aggregations

FS (org.eclipse.jgit.util.FS)14 File (java.io.File)8 JSch (com.jcraft.jsch.JSch)7 Session (com.jcraft.jsch.Session)6 Config (org.eclipse.jgit.lib.Config)6 FileBasedConfig (org.eclipse.jgit.storage.file.FileBasedConfig)6 TransportConfigCallback (org.eclipse.jgit.api.TransportConfigCallback)5 JschConfigSessionFactory (org.eclipse.jgit.transport.JschConfigSessionFactory)5 SshTransport (org.eclipse.jgit.transport.SshTransport)5 JSchException (com.jcraft.jsch.JSchException)4 IOException (java.io.IOException)4 SshSessionFactory (org.eclipse.jgit.transport.SshSessionFactory)4 UserInfo (com.jcraft.jsch.UserInfo)3 MockSystemReader (org.eclipse.jgit.junit.MockSystemReader)3 OpenSshConfig (org.eclipse.jgit.transport.OpenSshConfig)3 Transport (org.eclipse.jgit.transport.Transport)3 SystemReader (org.eclipse.jgit.util.SystemReader)3 DelegateSystemReader (com.google.gerrit.server.util.git.DelegateSystemReader)2 TransportException (org.eclipse.jgit.api.errors.TransportException)2 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)2