Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -203,10 +206,22 @@ public class JGitAPIImpl extends LegacyCompatibleGitAPIImpl {
public SshdSessionFactory buildSshdSessionFactory(@NonNull final HostKeyVerifierFactory hostKeyVerifierFactory) {
if (Files.notExists(hostKeyVerifierFactory.getKnownHostsFile().toPath())) {
try {
Files.createDirectories(hostKeyVerifierFactory
.getKnownHostsFile()
.getParentFile()
.toPath());
if (isWindows()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here be dragons.

Don't assume that POSIX file systems == non windows systems.

You can be on Linux and have a non POSIX FS (and the inverse).

And there was (and iirc) still is a bug where when you ask for the filesystem for a path you get the default even if it should be different!

This the only reliable way to do this is to actually try, catch the exception and do a fallback unless that big is fixed.

Files.createDirectories(hostKeyVerifierFactory
.getKnownHostsFile()
.getParentFile()
.toPath());
} else {
Set<PosixFilePermission> ownerOnly = PosixFilePermissions.fromString("rwx------");
FileAttribute<Set<PosixFilePermission>> fileAttribute =
PosixFilePermissions.asFileAttribute(ownerOnly);
Files.createDirectories(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want createDirectory here (and possibly a different call to create any missing parents of this directory)?
(At least you don't want all directories up to the parent to be created with these perms do you?)

hostKeyVerifierFactory
.getKnownHostsFile()
.getParentFile()
.toPath(),
fileAttribute);
}
Files.createFile(hostKeyVerifierFactory.getKnownHostsFile().toPath());
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "could not create known hosts file", e);
Expand Down Expand Up @@ -3262,4 +3277,9 @@ public void close() {
}
}
}

/** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */
private static boolean isWindows() {
return File.pathSeparatorChar == ';';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jgit.transport.sshd.ServerKeyDatabase;
Expand All @@ -29,19 +33,28 @@ public AbstractCliGitHostKeyVerifier forCliGit(TaskListener listener) {
};
}

private void createKnownHostsFile(Path knowHostPath) throws IOException {
Path parent = knowHostPath.getParent();
if (parent == null) {
throw new IllegalArgumentException("knowHostPath parent cannot be null");
}
if (isWindows()) {
Files.createDirectories(parent);
} else {
Set<PosixFilePermission> ownerOnly = PosixFilePermissions.fromString("rwx------");
FileAttribute<Set<PosixFilePermission>> fileAttribute = PosixFilePermissions.asFileAttribute(ownerOnly);
Files.createDirectories(parent, fileAttribute);
}
Files.createFile(knowHostPath);
}

@Override
public AbstractJGitHostKeyVerifier forJGit(TaskListener listener) {
Path knowHostPath = getKnownHostsFile().toPath();
if (Files.notExists(knowHostPath)) {
try {
logHint(listener);
Path parent = knowHostPath.getParent();
if (parent != null) {
Files.createDirectories(parent);
Files.createFile(knowHostPath);
} else {
throw new IllegalArgumentException("knowHostPath parent cannot be null");
}
createKnownHostsFile(knowHostPath);
} catch (IOException e) {
LOGGER.log(Level.WARNING, e, () -> "Could not load known hosts.");
}
Expand Down Expand Up @@ -79,4 +92,9 @@ private void logHint(TaskListener listener) {
"Known hosts file {0} not found, but verifying host keys with known hosts file",
new Object[] {SshHostKeyVerificationStrategy.KNOWN_HOSTS_DEFAULT});
}

/** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */
private static boolean isWindows() {
return File.pathSeparatorChar == ';';
}
}