Skip to content

Commit 3b21ea4

Browse files
committed
HttpBinaryCacheStore: Improve error message for unauthorized caches
Instead of the unhelpful warning: 'https://cache.flakehub.com' does not appear to be a binary cache you now get warning: unable to download 'https://cache.flakehub.com/nix-cache-info': HTTP error 401 response body: {"code":401,"error":"Unauthorized","message":"Unauthorized."}
1 parent ff00eeb commit 3b21ea4

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/libstore/binary-cache-store.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ BinaryCacheStore::BinaryCacheStore(const Params & params)
3939

4040
void BinaryCacheStore::init()
4141
{
42-
std::string cacheInfoFile = "nix-cache-info";
43-
44-
auto cacheInfo = getFile(cacheInfoFile);
42+
auto cacheInfo = getNixCacheInfo();
4543
if (!cacheInfo) {
4644
upsertFile(cacheInfoFile, "StoreDir: " + storeDir + "\n", "text/x-nix-cache-info");
4745
} else {
4846
for (auto & line : tokenizeString<Strings>(*cacheInfo, "\n")) {
49-
size_t colon= line.find(':');
50-
if (colon ==std::string::npos) continue;
47+
size_t colon = line.find(':');
48+
if (colon == std::string::npos) continue;
5149
auto name = line.substr(0, colon);
5250
auto value = trim(line.substr(colon + 1, std::string::npos));
5351
if (name == "StoreDir") {
@@ -63,6 +61,11 @@ void BinaryCacheStore::init()
6361
}
6462
}
6563

64+
std::optional<std::string> BinaryCacheStore::getNixCacheInfo()
65+
{
66+
return getFile(cacheInfoFile);
67+
}
68+
6669
void BinaryCacheStore::upsertFile(const std::string & path,
6770
std::string && data,
6871
const std::string & mimeType)

src/libstore/binary-cache-store.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ protected:
6464
// The prefix under which realisation infos will be stored
6565
const std::string realisationsPrefix = "realisations";
6666

67+
const std::string cacheInfoFile = "nix-cache-info";
68+
6769
BinaryCacheStore(const Params & params);
6870

6971
public:
@@ -84,6 +86,12 @@ public:
8486
*/
8587
virtual void getFile(const std::string & path, Sink & sink);
8688

89+
/**
90+
* Get the contents of /nix-cache-info. Return std::nullopt if it
91+
* doesn't exist.
92+
*/
93+
virtual std::optional<std::string> getNixCacheInfo();
94+
8795
/**
8896
* Fetch the specified file and call the specified callback with
8997
* the result. A subclass may implement this asynchronously.

src/libstore/http-binary-cache-store.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ class HttpBinaryCacheStore : public virtual HttpBinaryCacheStoreConfig, public v
194194
}
195195
}
196196

197+
std::optional<std::string> getNixCacheInfo() override
198+
{
199+
try {
200+
auto result = getFileTransfer()->download(makeRequest(cacheInfoFile));
201+
return result.data;
202+
} catch (FileTransferError & e) {
203+
if (e.error == FileTransfer::NotFound)
204+
return std::nullopt;
205+
maybeDisable();
206+
throw;
207+
}
208+
}
209+
197210
/**
198211
* This isn't actually necessary read only. We support "upsert" now, so we
199212
* have a notion of authentication via HTTP POST/PUT.

0 commit comments

Comments
 (0)