-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGitSSHPrivateKeyBinding.java
More file actions
219 lines (192 loc) · 8.69 KB
/
GitSSHPrivateKeyBinding.java
File metadata and controls
219 lines (192 loc) · 8.69 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package jenkins.plugins.git;
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.GitTool;
import hudson.util.ListBoxModel;
import hudson.Extension;
import jenkins.model.Jenkins;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.credentialsbinding.BindingDescriptor;
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.jenkinsci.plugins.credentialsbinding.impl.AbstractOnDiskBinding;
import org.jenkinsci.plugins.credentialsbinding.impl.UnbindableDir;
import org.jenkinsci.plugins.gitclient.CliGitAPIImpl;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.interceptor.RequirePOST;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Collections;
import java.util.Map;
import java.util.List;
import java.util.Set;
public class GitSSHPrivateKeyBinding extends MultiBinding<SSHUserPrivateKey> implements GitCredentialBindings, SSHKeyUtils {
final private String gitToolName;
private transient boolean unixNodeType;
@DataBoundConstructor
public GitSSHPrivateKeyBinding(String gitToolName, String credentialsId) {
super(credentialsId);
this.gitToolName = gitToolName;
//Variables could be added if needed
}
public String getGitToolName() {
return this.gitToolName;
}
private void setUnixNodeType(boolean value) {
this.unixNodeType = value;
}
@Override
public MultiEnvironment bind(@NonNull Run<?, ?> run, @Nullable FilePath filePath,
@Nullable Launcher launcher, @NonNull TaskListener taskListener) throws IOException, InterruptedException {
final Map<String, String> secretValues = new LinkedHashMap<>();
final Map<String, String> publicValues = new LinkedHashMap<>();
SSHUserPrivateKey credentials = getCredentials(run);
GitTool cliGitTool = getCliGitTool(run, this.gitToolName, taskListener);
if (cliGitTool != null && filePath != null && launcher != null) {
setUnixNodeType(isCurrentNodeOSUnix(launcher));
final UnbindableDir unbindTempDir = UnbindableDir.create(filePath);
final GitClient git = getGitClientInstance(cliGitTool.getGitExe(), unbindTempDir.getDirPath(), new EnvVars(), taskListener);
final String sshExePath = getSSHPath(git);
setGitEnvironmentVariables(git, publicValues);
if (isGitVersionAtLeast(git, 2, 3, 0, 0)) {
secretValues.put("GIT_SSH_COMMAND", getSSHCmd(credentials, unbindTempDir.getDirPath(), sshExePath));
} else {
SSHScriptFile sshScript = new SSHScriptFile(credentials, sshExePath, unixNodeType);
secretValues.put("GIT_SSH", sshScript.write(credentials, unbindTempDir.getDirPath()).getRemote());
}
return new MultiEnvironment(secretValues, publicValues, unbindTempDir.getUnbinder());
} else {
taskListener.getLogger().println("JGit and JGitApache type Git tools are not supported by this binding");
return new MultiEnvironment(secretValues, publicValues);
}
}
@Override
public Set<String> variables(@NonNull Run<?, ?> run) {
return Collections.emptySet();
}
/*package*/void setGitEnvironmentVariables(@NonNull GitClient git, Map<String, String> publicValues) throws IOException, InterruptedException {
setGitEnvironmentVariables(git, null, publicValues);
}
@Override
public void setCredentialPairBindings(@NonNull StandardCredentials credentials, Map<String, String> secretValues, Map<String, String> publicValues) {
//Private Key credentials not required to bind with environment variables
}
@Override
public void setGitEnvironmentVariables(@NonNull GitClient git, Map<String, String> secretValues, Map<String, String> publicValues) throws IOException, InterruptedException {
if (unixNodeType && isGitVersionAtLeast(git, 2, 3, 0, 0)) {
publicValues.put("GIT_TERMINAL_PROMPT", "false");
} else {
publicValues.put("GCM_INTERACTIVE", "false");
}
}
@Override
public GitClient getGitClientInstance(String gitToolExe, FilePath repository, EnvVars env, TaskListener listener) throws IOException, InterruptedException {
Git gitInstance = Git.with(listener, env).using(gitToolExe);
return gitInstance.getClient();
}
@Override
protected Class<SSHUserPrivateKey> type() {
return SSHUserPrivateKey.class;
}
private boolean isGitVersionAtLeast(GitClient git, int major, int minor, int rev, int bugfix) {
return ((CliGitAPIImpl) git).isCliGitVerAtLeast(major, minor, rev, bugfix);
}
private String getSSHPath(GitClient git) {
if (unixNodeType) {
return "ssh";
} else {
//Use getSSHExePathInWin(GitClient git), when support for finding ssh executable is provided for agents.
//ssh, will work only if OpenSSH is installed on the system being used to execute the build
return "ssh";
}
}
private String getSSHCmd(SSHUserPrivateKey credentials, FilePath tempDir, String sshExePath) {
if (unixNodeType) {
return sshExePath
+ " -i "
+ getPrivateKeyFile(credentials, tempDir).getRemote()
+ " -o StrictHostKeyChecking=no";
} else {
return "\"" + sshExePath + "\""
+ " -i "
+ "\""
+ getPrivateKeyFile(credentials, tempDir).getRemote()
+ "\""
+ " -o StrictHostKeyChecking=no";
}
}
protected final class SSHScriptFile extends AbstractOnDiskBinding<SSHUserPrivateKey> {
private final String sshExePath;
private final boolean unixNodeType;
protected SSHScriptFile(SSHUserPrivateKey credentials, String sshExePath, boolean unixNodeType) {
super(SSHKeyUtils.getSinglePrivateKey(credentials) + ":" + SSHKeyUtils.getPassphraseAsString(credentials), credentials.getId());
this.sshExePath = sshExePath;
this.unixNodeType = unixNodeType;
}
@Override
protected FilePath write(SSHUserPrivateKey credentials, FilePath workspace) throws IOException, InterruptedException {
FilePath tempFile;
if (unixNodeType) {
tempFile = workspace.createTempFile("gitSSHScript", ".sh");
tempFile.write(
"ssh -i "
+ getPrivateKeyFile(credentials, workspace).getRemote()
+ " -o StrictHostKeyChecking=no $@", null);
tempFile.chmod(0500);
} else {
tempFile = workspace.createTempFile("gitSSHScript", ".bat");
tempFile.write("@echo off\r\n"
+ "\""
+ this.sshExePath
+ "\""
+ " -i "
+ "\""
+ getPrivateKeyFile(credentials, workspace).getRemote()
+ "\""
+ " -o StrictHostKeyChecking=no", null);
}
return tempFile;
}
@Override
protected Class<SSHUserPrivateKey> type() {
return SSHUserPrivateKey.class;
}
}
@Symbol("gitSshPrivateKey")
@Extension
public static final class DescriptorImpl extends BindingDescriptor<SSHUserPrivateKey> {
@NonNull
@Override
public String getDisplayName() {
return Messages.GitSSHPrivateKeyBinding_DisplayName();
}
@RequirePOST
public ListBoxModel doFillGitToolNameItems() {
ListBoxModel items = new ListBoxModel();
List<GitTool> toolList = Jenkins.get().getDescriptorByType(GitSCM.DescriptorImpl.class).getGitTools();
for (GitTool t : toolList) {
if (t.getClass().equals(GitTool.class)) {
items.add(t.getName());
}
}
return items;
}
@Override
protected Class<SSHUserPrivateKey> type() {
return SSHUserPrivateKey.class;
}
@Override
public boolean requiresWorkspace() {
return true;
}
}
}