Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: configurable TUF cache dir #278

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
wraithgar marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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`)