跳到主要内容

包的访问

这是一系列的约束,它基于特定条件允许或限制对本地存储的访问。

安全约束构建于被使用的插件上,在默认情况下,verdaccio使用htpasswd 插件。 如果你使用不同的插件,行为可能会有所不同。 默认插件自己并不处理allow_accessallow_publish,它使用内部回退功能以防止插件尚未就绪。

关于权限的更多信息,请访问维基文档的认证部分

默认值

Using the default configuration, allows all users - including non-authenticated users - to read all packages. All known and authenticated users are allowed to publish/unpublish packages. If a package is not available in Verdaccio, Verdaccio will request it from the NPM registry and store it locally (if it exists).

packages:
'@*/*':
access: $all
publish: $authenticated
unpublish: $authenticated
proxy: npmjs
'**':
access: $all
publish: $authenticated
unpublish: $authenticated
proxy: npmjs
The default htpasswd auth plugin allows anyone to register! If you want to restrict package access, you will need to either limit registration to existing users or use a different auth plugin.

If the packages section is missing in your configuration, users will not be able to access any packages nor will they be allowed to publish or unpublish packages. :::

使用

packages:
# scoped packages
'@scope/*':
access: $all
publish: $all
proxy: server2

'private-*':
access: $all
publish: $all
proxy: uplink1

'**':
# allow all users (including non-authenticated users) to read and
# publish all packages
access: $all
publish: $all
proxy: uplink2

verdaccio 处理的内部组列表如下:

'$all', '$anonymous', '@all', '@anonymous', 'all', 'undefined', 'anonymous';

所有用户都会拥有这些权限,无论是否匿名。并加上插件提供的组,如果 htpasswd 将用户名作为组返回。 例如,如果您的登录名是 npmUser,那么组的列表将是

// 不含 '$' 的组最终将被弃用
'$all', '$anonymous', '@all', '@anonymous', 'all', 'undefined', 'anonymous', 'npmUser';

如果你想要保护你所在组的特定包,你需要做如下工作。 我们来使用一个包含所有前缀为 npmuser- 的包的 Regex。 我们建议为您的包使用前缀,这样可以更容易保护它们。

packages:
'npmuser-*':
access: npmuser
publish: npmuser

重启verdaccio并在命令行中尝试安装npmuser-core

$ npm install npmuser-core
npm install npmuser-core
npm ERR! code E403
npm ERR! 403 Forbidden: npmuser-core@latest

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/user/.npm/_logs/2017-07-02T12_20_14_834Z-debug.log code E403
npm ERR! 403 Forbidden: npmuser-core@latest

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/user/.npm/_logs/2017-07-02T12_20_14_834Z-debug.log

你可以使用不同的插件认证来更改现有行为。 verdaccio只是检查试图访问或发布特定包的用户是否属于正确的组。

注意:自 npm 7.10 起,配置项 always-auth 不再生效。 您可以安全地将其从配置中删除。

设置多个组

If you want to block the access/publish to a specific group of packages. Just do not define access and publish.

'company-*':
access: admin internal
publish: admin
proxy: server1
'supersecret-*':
access: secret super-secret-area ultra-secret-area
publish: secret ultra-secret-area
proxy: server1

阻止对一组包的访问

如果您想阻止对特定软件包组的访问/发布。 只需不定义 accesspublish

packages:
'old-*':
'**':
access: $all
publish: $authenticated

阻止代理一组特定包

您可能希望阻止一个或多个软件包从远程软件源获取数据,但同时允许其他软件包访问不同的 uplinks.

让我们描述一下在上面的示例中我们想要做什么:

packages:
'jquery':
access: $all
publish: $all
'my-company-*':
access: $all
publish: $authenticated
'@my-local-scope/*':
access: $all
publish: $authenticated
'**':
access: $all
publish: $authenticated
proxy: npmjs

让我们用上面的例子来描述一下我们的需求:

  • 我想要自己的服务器上放置jquery依赖库但需要避免代理它。
  • 我想要所有和my-company-*匹配的依赖库但我需要避免代理它们。
  • 我想要在my-local-scope范围内的所有依赖库但我需要避免代理它们。
  • 我想要代理所有剩余的依赖库。

注意库定义的顺序很重要同时必须使用双通配符。 因为如果你没有包含它,verdaccio会帮你来包含它,这样你的依赖库解析会受到影响。

您可以分配多个上行链路作为代理,以便在发生故障转移时使用,或在可能有其他专用 registry 的情况下使用。

'**':
access: $all
publish: $authenticated
proxy: npmjs uplink2

取消发布包

属性 publish 处理 npm publishnpm unpublish 的权限。 但是,如果想更具体,可以在软件包访问部分使用属性 unpublish,例如:

packages:
'jquery':
access: $all
publish: $all
unpublish: root
'my-company-*':
access: $all
publish: $authenticated
unpublish:
'@my-local-scope/*':
access: $all
publish: $authenticated
# unpublish: property commented out
'**':
access: $all
publish: $authenticated
proxy: npmjs

在前面的例子中,该行为将被描述为:

  • 所有用户都可以发布 jquery 软件包,但只有 root 用户才能取消发布任何版本。
  • 只有经过身份验证的用户才能发布 my-company-* 软件包,但任何人都不得取消发布它们
  • 如果 unpublish 被注释掉,则访问权限将由 publish 定义来授予或拒绝。

配置

你可以定义多个packages,每个包都必须有一个唯一的 Regex。 该语法基于minimatch glob 表达式

属性类型必须的范例支持版本描述
accessstring$all全部定义允许访问包的组
publishstring$authenticated全部定义允许发布的组
unpublishstring$authenticated全部定义允许取消发布的组
proxystringnpmjs全部针对特定的uplink限制查找
storagestringstring/some-folder它会在存储文件夹中为每个软件包的访问创建一个子文件夹

我们强烈建议不要再使用 allow_access/allow_publishproxy_access,它们已被弃用并将很快被移除,请使用它们的简写版本(access/publish/proxy)。

如果您想了解有关如何使用 storage 属性的更多信息,请参阅此评论