Skip to content

Commit

Permalink
feat: configurable TUF cache dir (#278)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian DeHamer <bdehamer@github.com>
  • Loading branch information
bdehamer committed May 3, 2023
1 parent 70bac1b commit 3307ad9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -175,6 +175,9 @@ resolved, and other properties, as they are determined.
* `verifyAttestations` A boolean that will make pacote verify Sigstore
attestations, if present. There must be a configured `_keys` entry in the
config that is scoped to the registry the manifest is being fetched from.
* `tufCache` Where to store metadata/target files when retrieving the package
attestation key material via TUF. Defaults to the same cache directory that
npm will use by default, based on platform and environment.

### Advanced API

Expand Down
3 changes: 2 additions & 1 deletion lib/fetcher.js
Expand Up @@ -61,7 +61,8 @@ class FetcherBase {
// by adding/modifying the integrity value.
this.opts = { ...opts }

this.cache = opts.cache || cacheDir()
this.cache = opts.cache || cacheDir().cacache
this.tufCache = opts.tufCache || cacheDir().tufcache
this.resolved = opts.resolved || null

// default to caching/verifying with sha512, that's what we usually have
Expand Down
5 changes: 4 additions & 1 deletion lib/registry.js
Expand Up @@ -295,7 +295,10 @@ class RegistryFetcher extends Fetcher {
//
// Publish attestations are signed with a keyid so we need to
// specify a public key from the keys endpoint: `registry-host.tld/-/npm/v1/keys`
const options = { keySelector: publicKey ? () => publicKey.pemkey : undefined }
const options = {
tufCachePath: this.tufCache,
keySelector: publicKey ? () => publicKey.pemkey : undefined,
}
await sigstore.verify(bundle, null, options)
} catch (e) {
throw Object.assign(new Error(
Expand Down
5 changes: 4 additions & 1 deletion lib/util/cache-dir.js
Expand Up @@ -8,5 +8,8 @@ module.exports = (fakePlatform = false) => {
const platform = fakePlatform || process.platform
const cacheExtra = platform === 'win32' ? 'npm-cache' : '.npm'
const cacheRoot = (platform === 'win32' && process.env.LOCALAPPDATA) || home
return resolve(cacheRoot, cacheExtra, '_cacache')
return {
cacache: resolve(cacheRoot, cacheExtra, '_cacache'),
tufcache: resolve(cacheRoot, cacheExtra, '_tuf'),
}
}
18 changes: 12 additions & 6 deletions test/util/cache-dir.js
Expand Up @@ -18,15 +18,21 @@ const cacheDir = require('../../lib/util/cache-dir.js')
// on all platforms.
t.ok(cacheDir(), 'a cache dir is ok')

t.equal(cacheDir(posix), '/home/isaacs/.npm/_cacache')
t.equal(cacheDir(windows), '/home/isaacs/npm-cache/_cacache')
t.equal(cacheDir(posix).cacache, '/home/isaacs/.npm/_cacache')
t.equal(cacheDir(windows).cacache, '/home/isaacs/npm-cache/_cacache')
t.equal(cacheDir(posix).tufcache, '/home/isaacs/.npm/_tuf')
t.equal(cacheDir(windows).tufcache, '/home/isaacs/npm-cache/_tuf')

os.homedir = () => null
t.equal(cacheDir(posix), '/tmp/npm-69420/.npm/_cacache')
t.equal(cacheDir(windows), '/tmp/npm-69420/npm-cache/_cacache')
t.equal(cacheDir(posix).cacache, '/tmp/npm-69420/.npm/_cacache')
t.equal(cacheDir(windows).cacache, '/tmp/npm-69420/npm-cache/_cacache')
t.equal(cacheDir(posix).tufcache, '/tmp/npm-69420/.npm/_tuf')
t.equal(cacheDir(windows).tufcache, '/tmp/npm-69420/npm-cache/_tuf')

process.env.LOCALAPPDATA = '/%LOCALAPPDATA%'
t.equal(cacheDir(windows), '/%LOCALAPPDATA%/npm-cache/_cacache')
t.equal(cacheDir(windows).cacache, '/%LOCALAPPDATA%/npm-cache/_cacache')
t.equal(cacheDir(windows).tufcache, '/%LOCALAPPDATA%/npm-cache/_tuf')

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

0 comments on commit 3307ad9

Please sign in to comment.