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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
#include "lix/libstore/legacy-ssh-store.hh"
#include "lix/libutil/archive.hh"
#include "lix/libutil/async-io.hh"
#include "lix/libutil/async.hh"
#include "lix/libutil/pool.hh"
#include "lix/libstore/remote-store.hh"
#include "lix/libstore/serve-protocol.hh"
#include "lix/libstore/serve-protocol-impl.hh"
#include "lix/libstore/build-result.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libstore/path-with-outputs.hh"
#include "lix/libstore/ssh.hh"
#include "lix/libstore/ssh-store.hh"
#include "lix/libutil/result.hh"
#include "lix/libutil/serialise.hh"
#include "lix/libutil/strings.hh"
#include "lix/libstore/derivations.hh"
#include "lix/libutil/config-impl.hh"
#include "lix/libutil/abstract-setting-to-json.hh"
#include "path-info.hh"
#include "path.hh"
#include <cstdint>
#include <optional>
namespace nix {
namespace {
struct QueryPathInfoResult
{
std::string p;
UnkeyedValidPathInfo info;
};
struct BuildPathsResult
{
BuildResult result;
std::optional<Error> error;
};
}
template<>
DECLARE_SERVE_SERIALISER(QueryPathInfoResult);
template<>
DECLARE_SERVE_SERIALISER(BuildPathsResult);
QueryPathInfoResult ServeProto::Serialise<QueryPathInfoResult>::read(ServeProto::ReadConn conn)
{
auto p = readString(conn.from);
if (p.empty()) {
return {"", UnkeyedValidPathInfo{Hash::dummy}};
}
auto info = ServeProto::Serialise<UnkeyedValidPathInfo>::read(conn);
if (info.narHash == Hash::dummy) {
throw Error("NAR hash is now mandatory");
}
auto s = readString(conn.from);
assert(s == "");
return {std::move(p), std::move(info)};
}
BuildPathsResult ServeProto::Serialise<BuildPathsResult>::read(ServeProto::ReadConn conn)
{
BuildResult result;
result.status = (BuildResult::Status) readNum<unsigned>(conn.from);
if (!result.success()) {
result.errorMsg = readString(conn.from);
return {result, Error(result.status, result.errorMsg)};
}
return {result, std::nullopt};
}
// writers are intentionally not defined
struct LegacySSHStoreConfig : CommonSSHStoreConfig
{
using CommonSSHStoreConfig::CommonSSHStoreConfig;
const Setting<Path> remoteProgram{this, "nix-store", "remote-program",
"Path to the `nix-store` executable on the remote machine."};
const Setting<int> maxConnections{this, 1, "max-connections",
"Maximum number of concurrent SSH connections."};
const std::string name() override { return "SSH Store"; }
std::string doc() override
{
return
#include "legacy-ssh-store.md"
;
}
};
struct LegacySSHStoreConfigWithLog : LegacySSHStoreConfig
{
using LegacySSHStoreConfig::LegacySSHStoreConfig;
// Hack for getting remote build log output.
// Intentionally not in `LegacySSHStoreConfig` so that it doesn't appear in
// the documentation
const Setting<int> logFD{this, -1, "log-fd", "file descriptor to which SSH's stderr is connected"};
};
struct LegacySSHStore final : public Store
{
LegacySSHStoreConfigWithLog config_;
LegacySSHStoreConfigWithLog & config() override { return config_; }
const LegacySSHStoreConfigWithLog & config() const override { return config_; }
struct Connection
{
ref<IoBuffer> fromBuf{make_ref<IoBuffer>()};
std::unique_ptr<SSH::Connection> sshConn;
ServeProto::Version remoteVersion;
Store * store = nullptr;
bool good = true;
/*
* Coercion to `ServeProto::WriteConn`. This makes it easy to use the
* factored out serve protocol searlizers with a
* `LegacySSHStore::Connection`.
*
* The serve protocol connection types are unidirectional, unlike
* this type.
*/
operator ServeProto::WriteConn ()
{
return ServeProto::WriteConn {
.store = *store,
.version = remoteVersion,
};
}
template<typename Arg>
kj::Promise<Result<void>>
sendArg(AsyncOutputStream & stream, StringSink & buffer, Arg && arg)
try {
buffer << std::forward<Arg>(arg);
return {result::success()};
} catch (...) {
return {result::current_exception()};
}
kj::Promise<Result<void>>
sendArg(AsyncOutputStream & stream, StringSink & buffer, box_ptr<AsyncInputStream> && arg)
try {
TRY_AWAIT(stream.writeFull(buffer.s.data(), buffer.s.size()));
buffer = {};
TRY_AWAIT(arg->drainInto(stream));
co_return result::success();
} catch (...) {
co_return result::current_exception();
}
template<typename R = void, typename... Args>
kj::Promise<Result<R>> sendCommandUninterruptible(Args &&... args)
try {
// invalidate this connection if we're cancelled early, e.g. by a user ^C.
// regular exceptions must be handled elsewhere due the subframe requests.
// this also invalidates connections if a request was sent while unwinding
// the stack, but that's sufficiently suspect to warrant being as careful.
auto invalidateOnCancel = kj::defer([&] {
if (std::uncaught_exceptions() == 0) {
good = false;
}
});
AsyncFdIoStream stream(AsyncFdIoStream::shared_fd{}, sshConn->socket.get());
{
StringSink buffer;
// can't use TRY_AWAIT here because macros break with variadic templates. sigh.
((co_await sendArg(stream, buffer, std::forward<Args>(args))).value(), ...);
TRY_AWAIT(stream.writeFull(buffer.s.data(), buffer.s.size()));
}
if constexpr (std::is_void_v<R>) {
invalidateOnCancel.cancel();
co_return result::success();
} else {
AsyncBufferedInputStream from{stream, fromBuf};
auto result = TRY_AWAIT(ServeProto::readAsync(
from, *store, remoteVersion, ServeProto::Serialise<R>::read
));
invalidateOnCancel.cancel();
co_return result;
}
} catch (...) {
good = false;
co_return result::current_exception();
}
template<typename R = void, typename... Args>
kj::Promise<Result<R>> sendCommand(Args &&... args)
{
return makeInterruptible(sendCommandUninterruptible<R>(std::forward<Args>(args)...));
}
};
std::string host;
ref<Pool<Connection>> connections;
SSH ssh;
static std::set<std::string> uriSchemes() { return {"ssh"}; }
LegacySSHStore(
const std::string & scheme, const std::string & host, LegacySSHStoreConfigWithLog config
)
: Store(config)
, config_(std::move(config))
, host(host)
, connections(make_ref<Pool<Connection>>(
std::max(1, (int) config_.maxConnections),
[this]() { return openConnection(); },
[](const ref<Connection> & r) { return r->good; }
))
, ssh(
host,
config_.port,
config_.sshKey,
config_.sshPublicHostKey,
config_.compress,
config_.logFD)
{
}
kj::Promise<Result<ref<Connection>>> openConnection()
try {
auto conn = make_ref<Connection>();
conn->sshConn = ssh.startCommand(
fmt("%s --serve --write", config_.remoteProgram)
+ (config_.remoteStore.get() == ""
? ""
: " --store " + shellEscape(config_.remoteStore.get()))
);
FdSink to(conn->sshConn->socket.get());
FdSource from(conn->sshConn->socket.get(), conn->fromBuf);
conn->store = this;
try {
to << SERVE_MAGIC_1 << SERVE_PROTOCOL_VERSION;
to.flush();
uint64_t magic = readNum<uint64_t>(from);
if (magic != SERVE_MAGIC_2)
throw Error("'nix-store --serve' protocol mismatch from '%s'", host);
conn->remoteVersion = readNum<unsigned>(from);
if (GET_PROTOCOL_MAJOR(conn->remoteVersion) != 0x200)
throw Error("unsupported 'nix-store --serve' protocol version on '%s'", host);
/* No longer support protocols this old*/
if (GET_PROTOCOL_MINOR(conn->remoteVersion) < 4) {
throw Error(
"remote '%s' is too old (protocol version %x)", host, conn->remoteVersion
);
}
} catch (EndOfFile & e) {
throw Error("cannot connect to '%1%'", host);
}
return {conn};
} catch (...) {
return {result::current_exception()};
};
std::string getUri() override
{
return *uriSchemes().begin() + "://" + host;
}
kj::Promise<Result<std::shared_ptr<const ValidPathInfo>>>
queryPathInfoUncached(const StorePath & path, const Activity * context) override
try {
auto conn(TRY_AWAIT(connections->get()));
debug("querying remote host '%s' for info on '%s'", host, printStorePath(path));
auto result = TRY_AWAIT(conn->sendCommand<QueryPathInfoResult>(
ServeProto::Command::QueryPathInfos, PathSet{printStorePath(path)}
));
if (result.p.empty()) {
co_return result::success(nullptr);
}
auto path2 = parseStorePath(result.p);
assert(path == path2);
co_return std::make_shared<ValidPathInfo>(path, std::move(result.info));
} catch (...) {
co_return result::current_exception();
}
kj::Promise<Result<void>> addToStore(
const ValidPathInfo & info,
AsyncInputStream & source,
RepairFlag repair,
CheckSigsFlag checkSigs,
const Activity * context
) override
try {
debug("adding path '%s' to remote host '%s'", printStorePath(info.path), host);
auto conn(TRY_AWAIT(connections->get()));
unsigned result;
if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 5) {
result = TRY_AWAIT(conn->sendCommand<unsigned>(
ServeProto::Command::AddToStoreNar,
printStorePath(info.path),
(info.deriver ? printStorePath(*info.deriver) : ""),
info.narHash.to_string(HashFormat::Base16, false),
ServeProto::write(*conn, info.references),
info.registrationTime,
info.narSize,
info.ultimate,
info.sigs,
renderContentAddress(info.ca),
copyNAR(source)
));
} else {
result = TRY_AWAIT(conn->sendCommand<unsigned>(
ServeProto::Command::ImportPaths,
1,
copyNAR(source),
exportMagic,
printStorePath(info.path),
ServeProto::write(*conn, info.references),
(info.deriver ? printStorePath(*info.deriver) : ""),
0,
0
));
}
if (result != 1) {
throw Error(
"failed to add path '%s' to remote host '%s'", printStorePath(info.path), host
);
}
co_return result::success();
} catch (...) {
co_return result::current_exception();
}
kj::Promise<Result<box_ptr<AsyncInputStream>>>
narFromPath(const StorePath & path, const Activity * context) override
try {
auto conn(TRY_AWAIT(connections->get()));
struct NarStream : AsyncInputStream
{
Pool<Connection>::Handle conn;
AsyncFdIoStream stream{AsyncFdIoStream::shared_fd{}, conn->sshConn->socket.get()};
AsyncBufferedInputStream buffered{stream, conn->fromBuf};
box_ptr<AsyncInputStream> copier{copyNAR(buffered)};
NarStream(Pool<Connection>::Handle conn) : conn(std::move(conn)) {}
kj::Promise<Result<std::optional<size_t>>> read(void * buffer, size_t size) override
{
return copier->read(buffer, size);
}
};
TRY_AWAIT(conn->sendCommand(ServeProto::Command::DumpStorePath, printStorePath(path)));
co_return make_box_ptr<NarStream>(std::move(conn));
} catch (...) {
co_return result::current_exception();
}
kj::Promise<Result<std::optional<StorePath>>>
queryPathFromHashPart(const std::string & hashPart) override
try {
unsupported("queryPathFromHashPart");
} catch (...) {
return {result::current_exception()};
}
kj::Promise<Result<StorePath>> addToStoreRecursive(
std::string_view name,
const PreparedDump & source,
HashType hashAlgo,
RepairFlag repair) override
try { throw Error("addToStoreRecursive"); } catch (...) { return {result::current_exception()}; }
kj::Promise<Result<StorePath>> addToStoreFlat(
std::string_view name,
const Path & srcPath,
HashType hashAlgo,
RepairFlag repair) override
try { throw Error("addToStoreFlat"); } catch (...) { return {result::current_exception()}; }
kj::Promise<Result<StorePath>> addTextToStore(
std::string_view name,
std::string_view s,
const StorePathSet & references,
RepairFlag repair) override
try { unsupported("addTextToStore"); } catch (...) { return {result::current_exception()}; }
private:
WireFormatGenerator putBuildSettings(Connection & conn)
{
co_yield settings.maxSilentTime;
co_yield settings.buildTimeout;
co_yield settings.maxLogSize;
co_yield 0; // buildRepeat hasn't worked for ages anyway
co_yield 0;
if (GET_PROTOCOL_MINOR(conn.remoteVersion) >= 7) {
co_yield ((int) settings.keepFailed);
}
}
public:
kj::Promise<Result<BuildResult>> buildDerivation(
const StorePath & drvPath, const BasicDerivation & drv, BuildMode buildMode
) override
try {
auto conn(TRY_AWAIT(connections->get()));
co_return TRY_AWAIT(conn->sendCommand<BuildResult>(
ServeProto::Command::BuildDerivation,
printStorePath(drvPath),
serializeDerivation(*this, drv),
putBuildSettings(*conn)
));
} catch (...) {
co_return result::current_exception();
}
kj ::Promise<Result<void>> buildPaths(
const std::vector<DerivedPath> & drvPaths,
BuildMode buildMode,
std::shared_ptr<Store> evalStore
) override
try {
if (evalStore && evalStore.get() != this)
throw Error("building on an SSH store is incompatible with '--eval-store'");
auto conn(TRY_AWAIT(connections->get()));
Strings ss;
for (auto & p : drvPaths) {
auto sOrDrvPath = StorePathWithOutputs::tryFromDerivedPath(p);
std::visit(overloaded {
[&](const StorePathWithOutputs & s) {
ss.push_back(s.to_string(*this));
},
[&](const StorePath & drvPath) {
throw Error("wanted to fetch '%s' but the legacy ssh protocol doesn't support merely substituting drv files via the build paths command. It would build them instead. Try using ssh-ng://", printStorePath(drvPath));
},
[&](std::monostate) {
throw Error("wanted build derivation that is itself a build product, but the legacy ssh protocol doesn't support that. Try using ssh-ng://");
},
}, sOrDrvPath);
}
auto result = TRY_AWAIT(conn->sendCommand<BuildPathsResult>(
ServeProto::Command::BuildPaths, ss, putBuildSettings(*conn)
));
if (result.error) {
throw *result.error;
}
co_return result::success();
} catch (...) {
co_return result::current_exception();
}
kj::Promise<Result<void>> ensurePath(const StorePath & path) override
try { unsupported("ensurePath"); } catch (...) { return {result::current_exception()}; }
virtual ref<FSAccessor> getFSAccessor() override
{ unsupported("getFSAccessor"); }
/**
* The default instance would schedule the work on the client side, but
* for consistency with `buildPaths` and `buildDerivation` it should happen
* on the remote side.
*
* We make this fail for now so we can add implement this properly later
* without it being a breaking change.
*/
kj::Promise<Result<void>> repairPath(const StorePath & path) override
try { unsupported("repairPath"); } catch (...) { return {result::current_exception()}; }
kj::Promise<Result<void>> computeFSClosure(const StorePathSet & paths,
StorePathSet & out, bool flipDirection = false,
bool includeOutputs = false, bool includeDerivers = false) override
try {
if (flipDirection || includeDerivers) {
TRY_AWAIT(
Store::computeFSClosure(paths, out, flipDirection, includeOutputs, includeDerivers)
);
co_return result::success();
}
auto conn(TRY_AWAIT(connections->get()));
out.merge(TRY_AWAIT(conn->sendCommand<StorePathSet>(
ServeProto::Command::QueryClosure, includeOutputs, ServeProto::write(*conn, paths)
)));
co_return result::success();
} catch (...) {
co_return result::current_exception();
}
kj::Promise<Result<StorePathSet>> queryValidPaths(const StorePathSet & paths,
SubstituteFlag maybeSubstitute = NoSubstitute) override
try {
auto conn(TRY_AWAIT(connections->get()));
co_return TRY_AWAIT(conn->sendCommand<StorePathSet>(
ServeProto::Command::QueryValidPaths,
false, // lock
maybeSubstitute,
ServeProto::write(*conn, paths)
));
} catch (...) {
co_return result::current_exception();
}
kj::Promise<Result<void>> connect() override
try {
auto conn(TRY_AWAIT(connections->get()));
co_return result::success();
} catch (...) {
co_return result::current_exception();
}
kj::Promise<Result<unsigned int>> getProtocol() override
try {
auto conn(TRY_AWAIT(connections->get()));
co_return conn->remoteVersion;
} catch (...) {
co_return result::current_exception();
}
/**
* The legacy ssh protocol doesn't support checking for trusted-user.
* Try using ssh-ng:// instead if you want to know.
*/
kj::Promise<Result<std::optional<TrustedFlag>>> isTrustedClient() override
{
return {result::success(std::nullopt)};
}
};
void registerLegacySSHStore() {
StoreImplementations::add<LegacySSHStore, LegacySSHStoreConfig>();
}
}
|