Skip to content

Commit a4423f3

Browse files
simplified/secured SSHUserPrivateKeyBinding using UnbindableDir
1 parent e340e5f commit a4423f3

File tree

1 file changed

+3
-24
lines changed

1 file changed

+3
-24
lines changed

src/main/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyBinding.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import hudson.Launcher;
3030
import hudson.model.Run;
3131
import hudson.model.TaskListener;
32-
import hudson.slaves.WorkspaceList;
3332
import hudson.util.Secret;
3433
import org.jenkinsci.Symbol;
3534
import org.jenkinsci.plugins.credentialsbinding.BindingDescriptor;
@@ -93,7 +92,8 @@ public String getPassphraseVariable() {
9392

9493
@Override public MultiEnvironment bind(Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
9594
SSHUserPrivateKey sshKey = getCredentials(build);
96-
FilePath keyFile = tempDir(workspace).child("ssh-key-" + keyFileVariable);
95+
UnbindableDir keyDir = UnbindableDir.create(workspace);
96+
FilePath keyFile = keyDir.getDirPath().child("ssh-key-" + keyFileVariable);
9797

9898
StringWriter stringWriter = new StringWriter();
9999
PrintWriter keysFileStream = new PrintWriter(stringWriter);
@@ -120,28 +120,7 @@ public String getPassphraseVariable() {
120120
map.put(usernameVariable, sshKey.getUsername());
121121
}
122122

123-
return new MultiEnvironment(map, new KeyRemover(keyFile.getRemote()));
124-
}
125-
126-
private static class KeyRemover implements MultiBinding.Unbinder {
127-
128-
private static final long serialVersionUID = 1;
129-
130-
private final String filePath;
131-
132-
KeyRemover(String filePath) {
133-
this.filePath = filePath;
134-
}
135-
136-
@Override public void unbind(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
137-
workspace.child(this.filePath).delete();
138-
}
139-
140-
}
141-
142-
// TODO 1.652 use WorkspaceList.tempDir
143-
private static FilePath tempDir(FilePath ws) {
144-
return ws.sibling(ws.getName() + System.getProperty(WorkspaceList.class.getName(), "@") + "tmp");
123+
return new MultiEnvironment(map, keyDir.getUnbinder());
145124
}
146125

147126
@Symbol("sshUserPrivateKey")

0 commit comments

Comments
 (0)