2020-10-08 15:36:51 +00:00
|
|
|
|
#include "realisation.hh"
|
|
|
|
|
#include "store-api.hh"
|
2020-12-08 20:07:52 +00:00
|
|
|
|
#include <nlohmann/json.hpp>
|
2020-10-08 15:36:51 +00:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
MakeError(InvalidDerivationOutputId, Error);
|
|
|
|
|
|
|
|
|
|
DrvOutput DrvOutput::parse(const std::string &strRep) {
|
2020-12-09 15:56:56 +00:00
|
|
|
|
size_t n = strRep.find("!");
|
|
|
|
|
if (n == strRep.npos)
|
2020-10-08 15:36:51 +00:00
|
|
|
|
throw InvalidDerivationOutputId("Invalid derivation output id %s", strRep);
|
|
|
|
|
|
|
|
|
|
return DrvOutput{
|
2020-12-09 15:56:56 +00:00
|
|
|
|
.drvHash = Hash::parseAnyPrefixed(strRep.substr(0, n)),
|
|
|
|
|
.outputName = strRep.substr(n+1),
|
2020-10-08 15:36:51 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string DrvOutput::to_string() const {
|
2020-12-09 15:56:56 +00:00
|
|
|
|
return strHash() + "!" + outputName;
|
2020-10-08 15:36:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 20:07:52 +00:00
|
|
|
|
nlohmann::json Realisation::toJSON() const {
|
|
|
|
|
return nlohmann::json{
|
|
|
|
|
{"id", id.to_string()},
|
|
|
|
|
{"outPath", outPath.to_string()},
|
2021-03-08 10:56:33 +00:00
|
|
|
|
{"signatures", signatures},
|
2020-12-08 20:07:52 +00:00
|
|
|
|
};
|
2020-10-08 15:36:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 20:07:52 +00:00
|
|
|
|
Realisation Realisation::fromJSON(
|
|
|
|
|
const nlohmann::json& json,
|
|
|
|
|
const std::string& whence) {
|
2021-03-08 10:56:33 +00:00
|
|
|
|
auto getOptionalField = [&](std::string fieldName) -> std::optional<std::string> {
|
2020-12-08 20:07:52 +00:00
|
|
|
|
auto fieldIterator = json.find(fieldName);
|
|
|
|
|
if (fieldIterator == json.end())
|
2021-03-08 10:56:33 +00:00
|
|
|
|
return std::nullopt;
|
|
|
|
|
return *fieldIterator;
|
|
|
|
|
};
|
|
|
|
|
auto getField = [&](std::string fieldName) -> std::string {
|
|
|
|
|
if (auto field = getOptionalField(fieldName))
|
|
|
|
|
return *field;
|
|
|
|
|
else
|
2020-12-08 20:07:52 +00:00
|
|
|
|
throw Error(
|
|
|
|
|
"Drv output info file '%1%' is corrupt, missing field %2%",
|
|
|
|
|
whence, fieldName);
|
2020-10-08 15:36:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-03-08 10:56:33 +00:00
|
|
|
|
StringSet signatures;
|
|
|
|
|
if (auto signaturesIterator = json.find("signatures"); signaturesIterator != json.end())
|
|
|
|
|
signatures.insert(signaturesIterator->begin(), signaturesIterator->end());
|
|
|
|
|
|
2020-12-08 20:07:52 +00:00
|
|
|
|
return Realisation{
|
|
|
|
|
.id = DrvOutput::parse(getField("id")),
|
|
|
|
|
.outPath = StorePath(getField("outPath")),
|
2021-03-08 10:56:33 +00:00
|
|
|
|
.signatures = signatures,
|
2020-10-08 15:36:51 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 10:56:33 +00:00
|
|
|
|
std::string Realisation::fingerprint() const
|
|
|
|
|
{
|
|
|
|
|
auto serialized = toJSON();
|
|
|
|
|
serialized.erase("signatures");
|
|
|
|
|
return serialized.dump();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Realisation::sign(const SecretKey & secretKey)
|
|
|
|
|
{
|
|
|
|
|
signatures.insert(secretKey.signDetached(fingerprint()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Realisation::checkSignature(const PublicKeys & publicKeys, const std::string & sig) const
|
|
|
|
|
{
|
|
|
|
|
return verifyDetached(fingerprint(), sig, publicKeys);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t Realisation::checkSignatures(const PublicKeys & publicKeys) const
|
|
|
|
|
{
|
|
|
|
|
// FIXME: Maybe we should return `maxSigs` if the realisation corresponds to
|
|
|
|
|
// an input-addressed one − because in that case the drv is enough to check
|
|
|
|
|
// it − but we can't know that here.
|
|
|
|
|
|
|
|
|
|
size_t good = 0;
|
|
|
|
|
for (auto & sig : signatures)
|
|
|
|
|
if (checkSignature(publicKeys, sig))
|
|
|
|
|
good++;
|
|
|
|
|
return good;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 16:24:30 +00:00
|
|
|
|
StorePath RealisedPath::path() const {
|
2021-02-04 14:15:22 +00:00
|
|
|
|
return std::visit([](auto && arg) { return arg.getPath(); }, raw);
|
2020-12-14 16:24:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RealisedPath::closure(
|
|
|
|
|
Store& store,
|
|
|
|
|
const RealisedPath::Set& startPaths,
|
|
|
|
|
RealisedPath::Set& ret)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: This only builds the store-path closure, not the real realisation
|
|
|
|
|
// closure
|
|
|
|
|
StorePathSet initialStorePaths, pathsClosure;
|
|
|
|
|
for (auto& path : startPaths)
|
|
|
|
|
initialStorePaths.insert(path.path());
|
|
|
|
|
store.computeFSClosure(initialStorePaths, pathsClosure);
|
|
|
|
|
ret.insert(startPaths.begin(), startPaths.end());
|
|
|
|
|
ret.insert(pathsClosure.begin(), pathsClosure.end());
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 13:47:56 +00:00
|
|
|
|
void RealisedPath::closure(Store& store, RealisedPath::Set & ret) const
|
2020-12-14 16:24:30 +00:00
|
|
|
|
{
|
|
|
|
|
RealisedPath::closure(store, {*this}, ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RealisedPath::Set RealisedPath::closure(Store& store) const
|
|
|
|
|
{
|
|
|
|
|
RealisedPath::Set ret;
|
|
|
|
|
closure(store, ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 15:36:51 +00:00
|
|
|
|
} // namespace nix
|