Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Fix running server tests on Windows #9193

Merged
merged 4 commits into from
Aug 8, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.sshd.common.NamedResource;
import org.apache.sshd.common.session.SessionContext;
Expand Down Expand Up @@ -45,20 +46,18 @@ public synchronized List<KeyPair> loadKeys(SessionContext session) {
var keys = new ArrayList<KeyPair>();

try (var stream = Files.list(path)) {
stream.forEach(file -> {
if (file != null) {
try {
// Read a single key pair in the directory
Iterable<KeyPair> ids = readKeyPairs(session, file, IoUtils.EMPTY_OPEN_OPTIONS);
KeyPair kp = GenericUtils.head(ids);
if (kp != null) {
keys.addAll(Lists.newArrayList(ids));
}
}
catch (Exception e) {
warn("resolveKeyPair({}) Failed ({}) to load: {}", file, e.getClass().getSimpleName(), e.getMessage(), e);
stream.filter(Objects::nonNull).forEach(file -> {
try {
// Read a single key pair in the directory
Iterable<KeyPair> ids = readKeyPairs(session, file, IoUtils.EMPTY_OPEN_OPTIONS);
KeyPair kp = GenericUtils.head(ids);
if (kp != null) {
keys.addAll(Lists.newArrayList(ids));
}
}
catch (Exception e) {
warn("resolveKeyPair({}) Failed ({}) to load: {}", file, e.getClass().getSimpleName(), e.getMessage(), e);
}
});
}
catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,29 @@ public void run() {

Path rootDir = resolveRootDirectory(command, args);
RepositoryCache.FileKey key = RepositoryCache.FileKey.lenient(rootDir.toFile(), FS.DETECTED);
Repository repository = key.open(true /* must exist */);
User user = getServerSession().getAttribute(SshConstants.USER_KEY);
try (Repository repository = key.open(true /* must exist */)) {
User user = getServerSession().getAttribute(SshConstants.USER_KEY);

String subCommand = args[0];
if (RemoteConfig.DEFAULT_UPLOAD_PACK.equals(subCommand)) {
UploadPack uploadPack = new UploadPack(repository);
Environment environment = getEnvironment();
Map<String, String> envVars = environment.getEnv();
String protocol = MapEntryUtils.isEmpty(envVars) ? null : envVars.get(GitProtocolConstants.PROTOCOL_ENVIRONMENT_VARIABLE);
if (GenericUtils.isNotBlank(protocol)) {
uploadPack.setExtraParameters(Collections.singleton(protocol));
String subCommand = args[0];
if (RemoteConfig.DEFAULT_UPLOAD_PACK.equals(subCommand)) {
UploadPack uploadPack = new UploadPack(repository);
Environment environment = getEnvironment();
Map<String, String> envVars = environment.getEnv();
String protocol = MapEntryUtils.isEmpty(envVars) ? null : envVars.get(GitProtocolConstants.PROTOCOL_ENVIRONMENT_VARIABLE);
if (GenericUtils.isNotBlank(protocol)) {
uploadPack.setExtraParameters(Collections.singleton(protocol));
}
uploadPack.upload(getInputStream(), getOutputStream(), getErrorStream());
}
else if (RemoteConfig.DEFAULT_RECEIVE_PACK.equals(subCommand)) {
var receivePack = new ReceivePack(repository);
receivePack.setPreReceiveHook(new LocalVCPrePushHook(localVCServletService, user));
receivePack.setPostReceiveHook(new LocalVCPostPushHook(localVCServletService));
receivePack.receive(getInputStream(), getOutputStream(), getErrorStream());
}
else {
throw new IllegalArgumentException("Unknown git command: " + command);
}
uploadPack.upload(getInputStream(), getOutputStream(), getErrorStream());
}
else if (RemoteConfig.DEFAULT_RECEIVE_PACK.equals(subCommand)) {
var receivePack = new ReceivePack(repository);
receivePack.setPreReceiveHook(new LocalVCPrePushHook(localVCServletService, user));
receivePack.setPostReceiveHook(new LocalVCPostPushHook(localVCServletService));
receivePack.receive(getInputStream(), getOutputStream(), getErrorStream());
}
else {
throw new IllegalArgumentException("Unknown git command: " + command);
}

onExit(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
Expand Down Expand Up @@ -79,7 +80,11 @@ private void writePrivateKey() throws IOException, GeneralSecurityException {
writer.writePrivateKey(keyPair, sshKeyComment, new OpenSSHKeyEncryptionContext(), outputStream);
}

Files.setPosixFilePermissions(privateKeyPath, PosixFilePermissions.fromString("rw-------"));
// Avoid an UnsupportedOperationException on Windows
boolean posixSupported = FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
if (posixSupported) {
Files.setPosixFilePermissions(privateKeyPath, PosixFilePermissions.fromString("rw-------"));
}
}

/**
Expand Down
Loading