2016-04-05 16:30:22 +03:00
|
|
|
#include "command.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "thread-pool.hh"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
struct CmdCopySigs : StorePathsCommand
|
|
|
|
{
|
|
|
|
Strings substituterUris;
|
|
|
|
|
|
|
|
CmdCopySigs()
|
|
|
|
{
|
2017-06-07 19:41:20 +03:00
|
|
|
mkFlag()
|
|
|
|
.longName("substituter")
|
|
|
|
.shortName('s')
|
|
|
|
.labels({"store-uri"})
|
|
|
|
.description("use signatures from specified store")
|
|
|
|
.arity(1)
|
2017-10-24 13:45:11 +03:00
|
|
|
.handler([&](std::vector<std::string> ss) { substituterUris.push_back(ss[0]); });
|
2016-04-05 16:30:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "copy path signatures from substituters (like binary caches)";
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
void run(ref<Store> store, StorePaths storePaths) override
|
2016-04-05 16:30:22 +03:00
|
|
|
{
|
|
|
|
if (substituterUris.empty())
|
2017-07-30 14:27:57 +03:00
|
|
|
throw UsageError("you must specify at least one substituter using '-s'");
|
2016-04-05 16:30:22 +03:00
|
|
|
|
|
|
|
// FIXME: factor out commonality with MixVerify.
|
|
|
|
std::vector<ref<Store>> substituters;
|
|
|
|
for (auto & s : substituterUris)
|
2016-09-02 13:35:48 +03:00
|
|
|
substituters.push_back(openStore(s));
|
2016-04-05 16:30:22 +03:00
|
|
|
|
|
|
|
ThreadPool pool;
|
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
std::string doneLabel = "done";
|
2016-04-05 16:30:22 +03:00
|
|
|
std::atomic<size_t> added{0};
|
|
|
|
|
2017-05-16 17:09:57 +03:00
|
|
|
//logger->setExpected(doneLabel, storePaths.size());
|
2016-04-05 16:30:22 +03:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
auto doPath = [&](const Path & storePathS) {
|
2017-07-30 14:27:57 +03:00
|
|
|
//Activity act(*logger, lvlInfo, format("getting signatures for '%s'") % storePath);
|
2016-04-05 16:30:22 +03:00
|
|
|
|
|
|
|
checkInterrupt();
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
auto storePath = store->parseStorePath(storePathS);
|
|
|
|
|
2016-04-05 16:30:22 +03:00
|
|
|
auto info = store->queryPathInfo(storePath);
|
|
|
|
|
|
|
|
StringSet newSigs;
|
|
|
|
|
|
|
|
for (auto & store2 : substituters) {
|
2016-04-19 19:50:15 +03:00
|
|
|
try {
|
2019-12-05 20:11:09 +02:00
|
|
|
auto info2 = store2->queryPathInfo(info->path);
|
2016-04-19 19:50:15 +03:00
|
|
|
|
|
|
|
/* Don't import signatures that don't match this
|
|
|
|
binary. */
|
|
|
|
if (info->narHash != info2->narHash ||
|
|
|
|
info->narSize != info2->narSize ||
|
|
|
|
info->references != info2->references)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto & sig : info2->sigs)
|
|
|
|
if (!info->sigs.count(sig))
|
|
|
|
newSigs.insert(sig);
|
|
|
|
} catch (InvalidPath &) {
|
|
|
|
}
|
2016-04-05 16:30:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!newSigs.empty()) {
|
|
|
|
store->addSignatures(storePath, newSigs);
|
|
|
|
added += newSigs.size();
|
|
|
|
}
|
|
|
|
|
2017-05-16 17:09:57 +03:00
|
|
|
//logger->incProgress(doneLabel);
|
2016-04-05 16:30:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto & storePath : storePaths)
|
2019-12-05 20:11:09 +02:00
|
|
|
pool.enqueue(std::bind(doPath, store->printStorePath(storePath)));
|
2016-04-05 16:30:22 +03:00
|
|
|
|
|
|
|
pool.process();
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
printInfo("imported %d signatures", added);
|
2016-04-05 16:30:22 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 17:01:35 +03:00
|
|
|
static auto r1 = registerCommand<CmdCopySigs>("copy-sigs");
|
2016-04-05 16:30:22 +03:00
|
|
|
|
2016-04-05 17:39:29 +03:00
|
|
|
struct CmdSignPaths : StorePathsCommand
|
|
|
|
{
|
|
|
|
Path secretKeyFile;
|
|
|
|
|
|
|
|
CmdSignPaths()
|
|
|
|
{
|
2017-10-24 13:45:11 +03:00
|
|
|
mkFlag()
|
|
|
|
.shortName('k')
|
|
|
|
.longName("key-file")
|
|
|
|
.label("file")
|
|
|
|
.description("file containing the secret signing key")
|
|
|
|
.dest(&secretKeyFile);
|
2016-04-05 17:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "sign the specified paths";
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
void run(ref<Store> store, StorePaths storePaths) override
|
2016-04-05 17:39:29 +03:00
|
|
|
{
|
|
|
|
if (secretKeyFile.empty())
|
2017-07-30 14:27:57 +03:00
|
|
|
throw UsageError("you must specify a secret key file using '-k'");
|
2016-04-05 17:39:29 +03:00
|
|
|
|
|
|
|
SecretKey secretKey(readFile(secretKeyFile));
|
|
|
|
|
|
|
|
size_t added{0};
|
|
|
|
|
|
|
|
for (auto & storePath : storePaths) {
|
|
|
|
auto info = store->queryPathInfo(storePath);
|
|
|
|
|
2016-04-19 19:50:15 +03:00
|
|
|
auto info2(*info);
|
2016-04-05 17:39:29 +03:00
|
|
|
info2.sigs.clear();
|
2019-12-05 20:11:09 +02:00
|
|
|
info2.sign(*store, secretKey);
|
2016-04-05 17:39:29 +03:00
|
|
|
assert(!info2.sigs.empty());
|
|
|
|
|
2016-04-19 19:50:15 +03:00
|
|
|
if (!info->sigs.count(*info2.sigs.begin())) {
|
2016-04-05 17:39:29 +03:00
|
|
|
store->addSignatures(storePath, info2.sigs);
|
|
|
|
added++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
printInfo("added %d signatures", added);
|
2016-04-05 17:39:29 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 17:01:35 +03:00
|
|
|
static auto r2 = registerCommand<CmdSignPaths>("sign-paths");
|