Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c73aab60edf55bce965dc398657c445999d5d15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
 * Copyright (C) 2018, 2019 Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.core.internal;

import static java.text.MessageFormat.format;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.egit.core.Activator;
import org.eclipse.jgit.annotations.NonNull;

/**
 * Mirrors the Eclipse ssh-related preferences. The values are mirrored in
 * formats suitable for the Apache MINA sshd client implementation, and are
 * updated when the preferences change.
 * <p>
 * All operations are thread-safe.
 * </p>
 */
public class SshPreferencesMirror {

	// No listener support: it's probably not a good idea or even impossible to
	// reconfigure ongoing ssh sessions. Our session factory simply gets the
	// current values whenever a new session is started.

	private static final String PREFERENCES_NODE = "org.eclipse.jsch.core"; //$NON-NLS-1$

	/** The singleton instance of the {@link SshPreferencesMirror}. */
	public static final SshPreferencesMirror INSTANCE = new SshPreferencesMirror();

	private IEclipsePreferences preferences;

	private IPreferenceChangeListener listener = event -> reloadPreferences();

	private File sshDirectory;

	private List<String> defaultIdentities;

	private String defaultMechanisms;

	private boolean started;

	private SshPreferencesMirror() {
		// This is a singleton.
	}

	/** Starts mirroring the ssh preferences. */
	public void start() {
		if (started) {
			return;
		}
		started = true;
		preferences = InstanceScope.INSTANCE.getNode(PREFERENCES_NODE);
		if (preferences != null) {
			preferences.addPreferenceChangeListener(listener);
		}
		reloadPreferences();
	}

	/** Stops mirroring the ssh preferences. */
	public void stop() {
		started = false;
		if (preferences != null) {
			preferences.removePreferenceChangeListener(listener);
		}
	}

	private void reloadPreferences() {
		synchronized (this) {
			setSshDirectory();
			setDefaultIdentities();
			setPreferredAuthentications();
		}
	}

	private String get(@NonNull String key) {
		return Platform.getPreferencesService().getString(PREFERENCES_NODE, key,
				null, null);
	}

	private void setSshDirectory() {
		String sshDir = get("SSH2HOME"); //$NON-NLS-1$
		if (sshDir != null) {
			try {
				sshDirectory = Paths.get(sshDir).toFile();
				return;
			} catch (InvalidPathException e) {
				Activator.logWarning(
						format(CoreText.SshPreferencesMirror_invalidDirectory,
								sshDir),
						null);
			}
		}
		sshDirectory = null;
	}

	private void setDefaultIdentities() {
		String defaultKeys = get("PRIVATEKEY"); //$NON-NLS-1$
		if (defaultKeys == null || defaultKeys.isEmpty()) {
			defaultIdentities = null;
			return;
		}
		defaultIdentities = Arrays.stream(defaultKeys.trim().split("\\s*,\\s*")) //$NON-NLS-1$
				.map(s -> {
					if (s.isEmpty()) {
						return null;
					}
					try {
						Paths.get(s);
						return s;
					} catch (InvalidPathException e) {
						Activator.logWarning(
								format(CoreText.SshPreferencesMirror_invalidKeyFile,
										s),
								null);
						return null;
					}
				}).filter(Objects::nonNull).collect(Collectors.toList());
	}

	private void setPreferredAuthentications() {
		String mechanisms = get("CVSSSH2PreferencePage.PREF_AUTH_METHODS"); //$NON-NLS-1$
		if (mechanisms == null || mechanisms.isEmpty()) {
			defaultMechanisms = null;
		} else {
			defaultMechanisms = mechanisms;
		}
	}

	/**
	 * Gets the ssh directory.
	 *
	 * @return the configured ssh directory, or {@code null} if the
	 *         configuration is invalid
	 */
	public File getSshDirectory() {
		synchronized (this) {
			return sshDirectory;
		}
	}

	/**
	 * Gets the configured default key files.
	 *
	 * @param sshDir
	 *            the directory that represents ~/.ssh/
	 * @return a possibly empty list of paths containing the configured default
	 *         identities (private keys), or {@code null} if the user didn't
	 *         configure any. An empty list indicates that user did configure
	 *         something invalid.
	 */
	public List<Path> getDefaultIdentities(@NonNull File sshDir) {
		synchronized (this) {
			if (defaultIdentities == null) {
				return null;
			}
			return defaultIdentities.stream()
					.map(s -> {
						File f = new File(s);
						if (!f.isAbsolute()) {
							f = new File(sshDir, s);
						}
						return f.toPath();
					}).filter(Files::exists).collect(Collectors.toList());
		}
	}

	/**
	 * Gets the configured default authentication mechanisms.
	 *
	 * @return the default authentication mechanisms as a single comma-separated
	 *         string
	 */
	public String getPreferredAuthentications() {
		synchronized (this) {
			return defaultMechanisms;
		}
	}
}

Back to the top