Skip to content

Commit

Permalink
Add user@address:port support
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 authored and Ericson2314 committed Jun 21, 2023
1 parent 48fe0ed commit a997fed
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/libexpr/flake/flakeref.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ std::pair<FlakeRef, std::string> parseFlakeRefWithFragment(
.url = url,
.base = "flake:" + match.str(1),
.scheme = "flake",
.authority = "",
.authority = ParsedURLAuthority{},
.path = match[1],
};

Expand Down Expand Up @@ -161,7 +161,7 @@ std::pair<FlakeRef, std::string> parseFlakeRefWithFragment(
.url = base, // FIXME
.base = base,
.scheme = "git+file",
.authority = "",
.authority = {{}},
.path = flakeRoot,
.query = decodeQuery(match[2]),
};
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/primops/fetchTree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ std::string fixURI(std::string uri, EvalState & state, const std::string & defau
if (uri.find("://") == std::string::npos) {
const auto p = ParsedURL {
.scheme = defaultScheme,
.authority = "",
.authority = {{}},
.path = uri
};
return p.to_string();
Expand Down
4 changes: 2 additions & 2 deletions src/libfetchers/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ struct PathInputScheme : InputScheme
{
if (url.scheme != "path") return {};

if (url.authority && *url.authority != "")
throw Error("path URL '%s' should not have an authority ('%s')", url.url, *url.authority);
if (url.authority && *url.authority != ParsedURLAuthority {})
throw Error("path URL '%s' should not have an authority ('%s')", url.url, url.authority->to_string());

Input input;
input.attrs.insert_or_assign("type", "path");
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/dummy-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct DummyStoreConfig : virtual StoreConfig {

struct DummyStore : public virtual DummyStoreConfig, public virtual Store
{
DummyStore(const std::string scheme, const std::string uri, const Params & params)
DummyStore(const std::string scheme, const std::string uri, std::optional<uint16_t> port, const Params & params)
: DummyStore(params)
{ }

Expand Down
3 changes: 2 additions & 1 deletion src/libstore/http-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ class HttpBinaryCacheStore : public virtual HttpBinaryCacheStoreConfig, public v
HttpBinaryCacheStore(
const std::string & scheme,
const Path & _cacheUri,
std::optional<uint16_t> port,
const Params & params)
: StoreConfig(params)
, BinaryCacheStoreConfig(params)
, HttpBinaryCacheStoreConfig(params)
, Store(params)
, BinaryCacheStore(params)
, cacheUri(scheme + "://" + _cacheUri)
, cacheUri(scheme + "://" + _cacheUri + (port ? fmt(":%d", *port) : ""))
{
if (cacheUri.back() == '/')
cacheUri.pop_back();
Expand Down
3 changes: 2 additions & 1 deletion src/libstore/legacy-ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor

static std::set<std::string> uriSchemes() { return {"ssh"}; }

LegacySSHStore(const std::string & scheme, const std::string & host, const Params & params)
LegacySSHStore(const std::string & scheme, const std::string & host, std::optional<uint16_t> port, const Params & params)
: StoreConfig(params)
, CommonSSHStoreConfig(params)
, LegacySSHStoreConfig(params)
Expand All @@ -107,6 +107,7 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor
))
, master(
host,
port,
sshKey,
sshPublicHostKey,
// Use SSH master only if using more than 1 connection.
Expand Down
3 changes: 3 additions & 0 deletions src/libstore/local-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class LocalBinaryCacheStore : public virtual LocalBinaryCacheStoreConfig, public
LocalBinaryCacheStore(
const std::string scheme,
const Path & binaryCacheDir,
std::optional<uint16_t> port,
const Params & params)
: StoreConfig(params)
, BinaryCacheStoreConfig(params)
Expand All @@ -39,6 +40,8 @@ class LocalBinaryCacheStore : public virtual LocalBinaryCacheStoreConfig, public
, BinaryCacheStore(params)
, binaryCacheDir(binaryCacheDir)
{
if (port)
throw Error("file:// store does not accept a port number");
}

void init() override;
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ LocalStore::LocalStore(const Params & params)
}


LocalStore::LocalStore(std::string scheme, std::string path, const Params & params)
LocalStore::LocalStore(std::string scheme, std::string path, std::optional<uint16_t> port, const Params & params)
: LocalStore(params)
{
throw UnimplementedError("LocalStore");
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/local-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public:
* necessary.
*/
LocalStore(const Params & params);
LocalStore(std::string scheme, std::string path, const Params & params);
LocalStore(std::string scheme, std::string path, std::optional<uint16_t> port, const Params & params);

~LocalStore();

Expand Down
3 changes: 3 additions & 0 deletions src/libstore/s3-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ struct S3BinaryCacheStoreImpl : virtual S3BinaryCacheStoreConfig, public virtual
S3BinaryCacheStoreImpl(
const std::string & uriScheme,
const std::string & bucketName,
std::optional<uint16_t> port,
const Params & params)
: StoreConfig(params)
, BinaryCacheStoreConfig(params)
Expand All @@ -281,6 +282,8 @@ struct S3BinaryCacheStoreImpl : virtual S3BinaryCacheStoreConfig, public virtual
, bucketName(bucketName)
, s3Helper(profile, region, scheme, endpoint)
{
if (port)
throw Error("s3:// store does not accept a port number");
diskCache = getNarInfoDiskCache();
}

Expand Down
3 changes: 2 additions & 1 deletion src/libstore/ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SSHStore : public virtual SSHStoreConfig, public virtual RemoteStore
{
public:

SSHStore(const std::string & scheme, const std::string & host, const Params & params)
SSHStore(const std::string & scheme, const std::string & host, std::optional<uint16_t> port, const Params & params)
: StoreConfig(params)
, RemoteStoreConfig(params)
, CommonSSHStoreConfig(params)
Expand All @@ -42,6 +42,7 @@ class SSHStore : public virtual SSHStoreConfig, public virtual RemoteStore
, host(host)
, master(
host,
port,
sshKey,
sshPublicHostKey,
// Use SSH master only if using more than 1 connection.
Expand Down
7 changes: 5 additions & 2 deletions src/libstore/ssh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

namespace nix {

SSHMaster::SSHMaster(const std::string & host, const std::string & keyFile, const std::string & sshPublicHostKey, bool useMaster, bool compress, int logFD)
SSHMaster::SSHMaster(const std::string & host, std::optional<uint16_t> port, const std::string & keyFile, const std::string & sshPublicHostKey, bool useMaster, bool compress, int logFD)
: host(host)
, fakeSSH(host == "localhost")
, port(port)
, fakeSSH(host == "localhost" && !port)
, keyFile(keyFile)
, sshPublicHostKey(sshPublicHostKey)
, useMaster(useMaster && !fakeSSH)
Expand Down Expand Up @@ -36,6 +37,8 @@ void SSHMaster::addCommonSSHOpts(Strings & args)
}
if (compress)
args.push_back("-C");
if (port)
args.insert(args.end(), {"-p", std::to_string(*port)});

args.push_back("-oPermitLocalCommand=yes");
args.push_back("-oLocalCommand=echo started");
Expand Down
3 changes: 2 additions & 1 deletion src/libstore/ssh.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SSHMaster
private:

const std::string host;
const std::optional<int> port;
bool fakeSSH;
const std::string keyFile;
const std::string sshPublicHostKey;
Expand All @@ -32,7 +33,7 @@ private:

public:

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

struct Connection
{
Expand Down
30 changes: 14 additions & 16 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1433,26 +1433,25 @@ std::shared_ptr<Store> openFromNonUri(const std::string & uri, const Store::Para
//
// This function now ensures that a usable connection string is available:
// * If the store to be opened is not an SSH store, nothing will be done.
// * If the URL looks like `root@[::1]` (which is allowed by the URL parser and probably
// * If the URL host looks like `[::1]` (which is allowed by the URL parser and probably
// needed to pass further flags), it
// will be transformed into `root@::1` for SSH (same for `[::1]` -> `::1`).
// * If the URL looks like `root@::1` it will be left as-is.
// will be transformed into `::1` for SSH (same for `[::1]` -> `::1`).
// * If the URL host looks like `::1` it will be left as-is.
// * In any other case, the string will be left as-is.
static std::string extractConnStr(const std::string &proto, const std::string &connStr)
static std::string extractConnStr(
const std::string & proto,
const std::string & host)
{
if (proto.rfind("ssh") != std::string::npos) {
std::smatch result;
std::regex v6AddrRegex("^((.*)@)?\\[(.*)\\]$");
std::regex v6AddrRegex("^\\[(.*)\\]$");

if (std::regex_match(connStr, result, v6AddrRegex)) {
if (result[1].matched) {
return result.str(1) + result.str(3);
}
return result.str(3);
if (std::regex_match(host, result, v6AddrRegex)) {
return result.str(1);
}
}

return connStr;
return host;
}

ref<Store> openStore(const std::string & uri_,
Expand All @@ -1463,14 +1462,13 @@ ref<Store> openStore(const std::string & uri_,
auto parsedUri = parseURL(uri_);
params.insert(parsedUri.query.begin(), parsedUri.query.end());

auto baseURI = extractConnStr(
parsedUri.scheme,
parsedUri.authority.value_or("") + parsedUri.path
);
auto authHack = parsedUri.authority.value_or(ParsedURLAuthority {});
authHack.host = extractConnStr(parsedUri.scheme, authHack.host);
authHack.host += parsedUri.path;

for (auto implem : *Implementations::registered) {
if (implem.uriSchemes.count(parsedUri.scheme)) {
auto store = implem.create(parsedUri.scheme, baseURI, params);
auto store = implem.create(parsedUri.scheme, authHack.host, authHack.port, params);
if (store) {
store->init();
store->warnUnknownSettings();
Expand Down
6 changes: 3 additions & 3 deletions src/libstore/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ std::list<ref<Store>> getDefaultSubstituters();
struct StoreFactory
{
std::set<std::string> uriSchemes;
std::function<std::shared_ptr<Store> (const std::string & scheme, const std::string & uri, const Store::Params & params)> create;
std::function<std::shared_ptr<Store> (const std::string & scheme, const std::string & host, std::optional<uint16_t> optPort, const Store::Params & params)> create;
std::function<std::shared_ptr<StoreConfig> ()> getConfig;
};

Expand All @@ -982,9 +982,9 @@ struct Implementations
StoreFactory factory{
.uriSchemes = T::uriSchemes(),
.create =
([](const std::string & scheme, const std::string & uri, const Store::Params & params)
([](const std::string & scheme, const std::string & host, std::optional<uint16_t> optPort, const Store::Params & params)
-> std::shared_ptr<Store>
{ return std::make_shared<T>(scheme, uri, params); }),
{ return std::make_shared<T>(scheme, host, optPort, params); }),
.getConfig =
([]()
-> std::shared_ptr<StoreConfig>
Expand Down
3 changes: 3 additions & 0 deletions src/libstore/uds-remote-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ UDSRemoteStore::UDSRemoteStore(const Params & params)
UDSRemoteStore::UDSRemoteStore(
const std::string scheme,
std::string socket_path,
std::optional<uint16_t> port,
const Params & params)
: UDSRemoteStore(params)
{
if (port)
throw Error("unix:// store does not accept a port number");
path.emplace(socket_path);
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstore/uds-remote-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class UDSRemoteStore : public virtual UDSRemoteStoreConfig, public virtual Local
public:

UDSRemoteStore(const Params & params);
UDSRemoteStore(const std::string scheme, std::string path, const Params & params);
UDSRemoteStore(const std::string scheme, std::string path, std::optional<uint16_t> port, const Params & params);

std::string getUri() override;

Expand Down
Loading

0 comments on commit a997fed

Please sign in to comment.