Skip to content

Commit 3307ad9

Browse files
authored
feat: configurable TUF cache dir (#278)
Signed-off-by: Brian DeHamer <[email protected]>
1 parent 70bac1b commit 3307ad9

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ resolved, and other properties, as they are determined.
175175
* `verifyAttestations` A boolean that will make pacote verify Sigstore
176176
attestations, if present. There must be a configured `_keys` entry in the
177177
config that is scoped to the registry the manifest is being fetched from.
178+
* `tufCache` Where to store metadata/target files when retrieving the package
179+
attestation key material via TUF. Defaults to the same cache directory that
180+
npm will use by default, based on platform and environment.
178181

179182
### Advanced API
180183

lib/fetcher.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class FetcherBase {
6161
// by adding/modifying the integrity value.
6262
this.opts = { ...opts }
6363

64-
this.cache = opts.cache || cacheDir()
64+
this.cache = opts.cache || cacheDir().cacache
65+
this.tufCache = opts.tufCache || cacheDir().tufcache
6566
this.resolved = opts.resolved || null
6667

6768
// default to caching/verifying with sha512, that's what we usually have

lib/registry.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ class RegistryFetcher extends Fetcher {
295295
//
296296
// Publish attestations are signed with a keyid so we need to
297297
// specify a public key from the keys endpoint: `registry-host.tld/-/npm/v1/keys`
298-
const options = { keySelector: publicKey ? () => publicKey.pemkey : undefined }
298+
const options = {
299+
tufCachePath: this.tufCache,
300+
keySelector: publicKey ? () => publicKey.pemkey : undefined,
301+
}
299302
await sigstore.verify(bundle, null, options)
300303
} catch (e) {
301304
throw Object.assign(new Error(

lib/util/cache-dir.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ module.exports = (fakePlatform = false) => {
88
const platform = fakePlatform || process.platform
99
const cacheExtra = platform === 'win32' ? 'npm-cache' : '.npm'
1010
const cacheRoot = (platform === 'win32' && process.env.LOCALAPPDATA) || home
11-
return resolve(cacheRoot, cacheExtra, '_cacache')
11+
return {
12+
cacache: resolve(cacheRoot, cacheExtra, '_cacache'),
13+
tufcache: resolve(cacheRoot, cacheExtra, '_tuf'),
14+
}
1215
}

test/util/cache-dir.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@ const cacheDir = require('../../lib/util/cache-dir.js')
1818
// on all platforms.
1919
t.ok(cacheDir(), 'a cache dir is ok')
2020

21-
t.equal(cacheDir(posix), '/home/isaacs/.npm/_cacache')
22-
t.equal(cacheDir(windows), '/home/isaacs/npm-cache/_cacache')
21+
t.equal(cacheDir(posix).cacache, '/home/isaacs/.npm/_cacache')
22+
t.equal(cacheDir(windows).cacache, '/home/isaacs/npm-cache/_cacache')
23+
t.equal(cacheDir(posix).tufcache, '/home/isaacs/.npm/_tuf')
24+
t.equal(cacheDir(windows).tufcache, '/home/isaacs/npm-cache/_tuf')
2325

2426
os.homedir = () => null
25-
t.equal(cacheDir(posix), '/tmp/npm-69420/.npm/_cacache')
26-
t.equal(cacheDir(windows), '/tmp/npm-69420/npm-cache/_cacache')
27+
t.equal(cacheDir(posix).cacache, '/tmp/npm-69420/.npm/_cacache')
28+
t.equal(cacheDir(windows).cacache, '/tmp/npm-69420/npm-cache/_cacache')
29+
t.equal(cacheDir(posix).tufcache, '/tmp/npm-69420/.npm/_tuf')
30+
t.equal(cacheDir(windows).tufcache, '/tmp/npm-69420/npm-cache/_tuf')
2731

2832
process.env.LOCALAPPDATA = '/%LOCALAPPDATA%'
29-
t.equal(cacheDir(windows), '/%LOCALAPPDATA%/npm-cache/_cacache')
33+
t.equal(cacheDir(windows).cacache, '/%LOCALAPPDATA%/npm-cache/_cacache')
34+
t.equal(cacheDir(windows).tufcache, '/%LOCALAPPDATA%/npm-cache/_tuf')
3035

3136
process.getuid = null
32-
t.equal(cacheDir(posix), `/tmp/npm-${process.pid}/.npm/_cacache`)
37+
t.equal(cacheDir(posix).cacache, `/tmp/npm-${process.pid}/.npm/_cacache`)
38+
t.equal(cacheDir(posix).tufcache, `/tmp/npm-${process.pid}/.npm/_tuf`)

0 commit comments

Comments
 (0)