Skip to content

Commit

Permalink
Merge pull request #4593 from grahamc/builder-host-key-stable
Browse files Browse the repository at this point in the history
(Backport #4574) distributed builds: load remote builder host key from the machines file
  • Loading branch information
Ericson2314 authored Oct 26, 2023
2 parents 12ac3f6 + b4abe56 commit 619e975
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/build-remote/build-remote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ static int _main(int argc, char * * argv)
storeParams["log-fd"] = "4";
if (bestMachine->sshKey != "")
storeParams["ssh-key"] = bestMachine->sshKey;
if (bestMachine->sshPublicHostKey != "")
storeParams["base64-ssh-public-host-key"] = bestMachine->sshPublicHostKey;
}

sshStore = openStore(bestMachine->storeUri, storeParams);
Expand Down
2 changes: 2 additions & 0 deletions src/libstore/legacy-ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct LegacySSHStore : public Store
{
const Setting<int> maxConnections{this, 1, "max-connections", "maximum number of concurrent SSH connections"};
const Setting<Path> sshKey{this, "", "ssh-key", "path to an SSH private key"};
const Setting<std::string> sshPublicHostKey{this, "", "base64-ssh-public-host-key", "The public half of the host's SSH key"};
const Setting<bool> compress{this, false, "compress", "whether to compress the connection"};
const Setting<Path> remoteProgram{this, "nix-store", "remote-program", "path to the nix-store executable on the remote system"};
const Setting<std::string> remoteStore{this, "", "remote-store", "URI of the store on the remote system"};
Expand Down Expand Up @@ -48,6 +49,7 @@ struct LegacySSHStore : public Store
, master(
host,
sshKey,
sshPublicHostKey,
// Use SSH master only if using more than 1 connection.
connections->capacity() > 1,
compress,
Expand Down
2 changes: 2 additions & 0 deletions src/libstore/ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SSHStore : public RemoteStore
public:

const Setting<Path> sshKey{(Store*) this, "", "ssh-key", "path to an SSH private key"};
const Setting<std::string> sshPublicHostKey{(Store*) this, "", "base64-ssh-public-host-key", "The public half of the host's SSH key"};
const Setting<bool> compress{(Store*) this, false, "compress", "whether to compress the connection"};

SSHStore(const std::string & host, const Params & params)
Expand All @@ -24,6 +25,7 @@ class SSHStore : public RemoteStore
, master(
host,
sshKey,
sshPublicHostKey,
// Use SSH master only if using more than 1 connection.
connections->capacity() > 1,
compress)
Expand Down
17 changes: 14 additions & 3 deletions src/libstore/ssh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@

namespace nix {

SSHMaster::SSHMaster(const std::string & host, const std::string & keyFile, bool useMaster, bool compress, int logFD)
SSHMaster::SSHMaster(const std::string & host, const std::string & keyFile, const std::string & sshPublicHostKey, bool useMaster, bool compress, int logFD)
: host(host)
, fakeSSH(host == "localhost")
, keyFile(keyFile)
, sshPublicHostKey(sshPublicHostKey)
, useMaster(useMaster && !fakeSSH)
, compress(compress)
, logFD(logFD)
{
if (host == "" || hasPrefix(host, "-"))
throw Error("invalid SSH host name '%s'", host);

auto state(state_.lock());
state->tmpDir = std::make_unique<AutoDelete>(createTempDir("", "nix", true, true, 0700));
}

void SSHMaster::addCommonSSHOpts(Strings & args)
{
auto state(state_.lock());

for (auto & i : tokenizeString<Strings>(getEnv("NIX_SSHOPTS")))
args.push_back(i);
if (!keyFile.empty())
args.insert(args.end(), {"-i", keyFile});
if (!sshPublicHostKey.empty()) {
Path fileName = (Path) *state->tmpDir + "/host-key";
auto p = host.rfind("@");
string thost = p != string::npos ? string(host, p + 1) : host;
writeFile(fileName, thost + " " + base64Decode(sshPublicHostKey) + "\n");
args.insert(args.end(), {"-oUserKnownHostsFile=" + fileName});
}
if (compress)
args.push_back("-C");
}
Expand Down Expand Up @@ -87,8 +100,6 @@ Path SSHMaster::startMaster()

if (state->sshMaster != -1) return state->socketPath;

state->tmpDir = std::make_unique<AutoDelete>(createTempDir("", "nix", true, true, 0700));

state->socketPath = (Path) *state->tmpDir + "/ssh.sock";

Pipe out;
Expand Down
3 changes: 2 additions & 1 deletion src/libstore/ssh.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ private:
const std::string host;
bool fakeSSH;
const std::string keyFile;
const std::string sshPublicHostKey;
const bool useMaster;
const bool compress;
const int logFD;
Expand All @@ -29,7 +30,7 @@ private:

public:

SSHMaster(const std::string & host, const std::string & keyFile, bool useMaster, bool compress, int logFD = -1);
SSHMaster(const std::string & host, const std::string & keyFile, const std::string & sshPublicHostKey, bool useMaster, bool compress, int logFD = -1);

struct Connection
{
Expand Down

0 comments on commit 619e975

Please sign in to comment.