Nexus Connector

Nexus Connector 是一个平台无关的连接器,您可以使用它连接到 Nexus 仓库。

您可以使用 Nexus Connector 在 CICD 流水线或 Kubernetes 工作负载中安全地执行 Maven、NPM 和 PyPI 操作,而无需凭证。

此外,您可以集中管理跨命名空间的 Nexus 访问配置,避免在每个命名空间中重复配置 Nexus 凭证。

Overview

本文档涵盖:

  • 集成要求:目标 Nexus 仓库的先决条件
  • 创建 Nexus Connector
  • 高级功能:代理功能及 Maven、NPM 和 PyPI 的配置文件

Integration Requirements

Nexus 仓库先决条件

  • Nexus 服务器必须能从集群访问。
  • 支持的仓库类型:Maven(hosted/proxy/group)、NPM(hosted/proxy/group)、PyPI(hosted/proxy/group)。

客户端先决条件

对于 Maven 客户端,执行 mvn 操作前,必须信任连接器代理服务器的证书:

keytool -importcert -noprompt \
  -trustcacerts \
  -keystore $JAVA_HOME/lib/security/cacerts \
  -storepass changeit \
  -alias corp-ca \
  -file /opt/maven/ca.cert

mvn 客户端还必须使用 wagon 传输:

MAVEN_OPTS=-Dmaven.resolver.transport=wagon

Creating a Nexus Connector

以下是创建基础 Nexus Connector 的示例:

apiVersion: connectors.alauda.io/v1alpha1
kind: Connector
metadata:
  name: nexus-connector
spec:
  connectorClassName: nexus
  address: https://nexus.example.com
  auth:
    name: basicAuth

Fields Reference

spec.connectorClassName

nexus(固定值),指定用于 Nexus 集成的 ConnectorClass 名称。

spec.address

目标 Nexus 服务器地址,例如:https://nexus.example.com。这是 Nexus 服务器的根地址,不是具体仓库的 URL。

spec.auth

指定 Nexus 服务器的认证方式。

  • spec.auth.name:Nexus 连接器应为 basicAuth
  • spec.auth.secretRef:指定包含认证信息的 Secret。Secret 应创建在与连接器相同的命名空间中。如果您的 Nexus 服务器不需要认证,可以省略此字段。

可选元数据字段

  • cpaas.io/description:连接器的描述信息,例如:

    apiVersion: connectors.alauda.io/v1alpha1
    kind: Connector
    metadata:
      name: nexus-connector
      annotations:
        cpaas.io/description: "Connect to team Nexus server"

Capabilities of Nexus Connector

Authentication

Nexus Connector 支持以下认证类型:

  • basicAuth:基于用户名和密码的认证,对应的 Secret 类型为 kubernetes.io/basic-auth

使用基本认证

apiVersion: v1
stringData:
  username: your-nexus-username
  password: your-nexus-password
kind: Secret
metadata:
  name: nexus-secret
type: kubernetes.io/basic-auth

如果 Secret 不正确,连接器的 status.conditions 字段会显示错误信息。

有关完整状态信息,请参见 Connector Status Documentation

所需凭证权限

所需权限取决于您如何使用连接器:

  • 仅下载:凭证只需对目标仓库具有读取权限。
  • 上传/部署:凭证必须对目标仓库具有读写权限。

为安全起见,请创建权限最小化的凭证。

代理和配置文件

为了让客户端无需凭证即可访问 Nexus 仓库,Nexus Connector 提供了一个自动注入认证信息的代理服务器。

Nexus ConnectorClass 提供以下配置文件,可通过 Connectors CSI Driver 挂载到 Pod 中:

配置名称生成文件使用场景
settingssettings.xmlca.cert通过代理进行 Maven 操作
npmrc.npmrcNPM 包操作
yarnrc.yarnrc.ymlYarn 包操作
pipconfpip.confPyPI 包下载
pypirc.pypircPyPI 包发布

代理地址

创建连接器后,系统会自动为目标 Nexus 服务器创建代理服务。

代理端点记录在 status.proxy.httpAddress 中:

apiVersion: connectors.alauda.io/v1alpha1
kind: Connector
metadata:
  name: nexus-connector
spec:
  # connector spec fields
status:
  conditions:
    # status conditions
  proxy:
    httpAddress:
      url: http://c-nexus-connector.default.svc.cluster.local

Maven:settings.xml 配置

settings 配置提供通过 Connectors CSI Driver 挂载的 settings.xmlca.cert 文件。

生成的 settings.xml 示例:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">

  <proxies>
    <proxy>
      <id>connectors-proxy</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>c-nexus-connector.connectors-nexus-demo.svc.cluster.local</host>
      <port>80</port>
      <username>connectors-nexus-demo/nexus-connector</username>
      <password>eyJhbGciOiJEnEZaTQ</password>
      <nonProxyHosts>localhost</nonProxyHosts>
    </proxy>
  </proxies>
</settings>

可选参数mirrorRepository — 用作 Maven 镜像的 Nexus 仓库。设置后,生成的 settings.xml 会包含指向 {address}/repository/{mirrorRepository}<mirrors> 部分。

挂载 settings 配置示例:

spec:
  volumes:
  - name: settings
    csi:
      readOnly: true
      driver: connectors-csi
      volumeAttributes:
        connector.name: "nexus-connector"
        configuration.names: "settings"
        configuration.params: '{"settings": {"mirrorRepository": "maven-public"}}' # optional

NPM:.npmrc 配置

npmrc 配置提供用于 NPM 包操作的 .npmrc 文件。

必填参数registry — Nexus NPM 代理仓库名称(例如 npm-proxy)。

可选参数strictSSL — 是否要求 SSL(默认值为 "true")。

spec:
  volumes:
  - name: npmrc
    csi:
      readOnly: true
      driver: connectors-csi
      volumeAttributes:
        connector.name: "nexus-connector"
        configuration.names: "npmrc"
        configuration.params: '{"npmrc": {"registry": "npm-proxy"}}'

Yarn:.yarnrc.yml 配置

yarnrc 配置提供用于 Yarn 包操作的 .yarnrc.yml 文件。

必填参数registry — Nexus NPM 代理仓库名称。

可选参数strictSSL — 是否要求 SSL(默认值为 "true")。

spec:
  volumes:
  - name: yarnrc
    csi:
      readOnly: true
      driver: connectors-csi
      volumeAttributes:
        connector.name: "nexus-connector"
        configuration.names: "yarnrc"
        configuration.params: '{"yarnrc": {"registry": "npm-proxy"}}'

PyPI 下载:pip.conf 配置

pipconf 配置提供用于下载 PyPI 包的 pip.conf 文件。

必填参数repository — Nexus PyPI 代理仓库名称(例如 pypi-proxy)。

spec:
  volumes:
  - name: pipconf
    csi:
      readOnly: true
      driver: connectors-csi
      volumeAttributes:
        connector.name: "nexus-connector"
        configuration.names: "pipconf"
        configuration.params: '{"pipconf": {"repository": "pypi-proxy"}}'

PyPI 发布:.pypirc 配置

pypirc 配置提供用于发布 PyPI 包的 .pypirc 文件。

必填参数deployRepository — Nexus PyPI hosted 仓库名称(例如 pypi-hosted)。

spec:
  volumes:
  - name: pypirc
    csi:
      readOnly: true
      driver: connectors-csi
      volumeAttributes:
        connector.name: "nexus-connector"
        configuration.names: "pypirc"
        configuration.params: '{"pypirc": {"deployRepository": "pypi-hosted"}}'

使用 Connectors CSI Driver

CSI 卷属性参数:

  • csi.readOnly:固定值 true
  • csi.driver:固定为 connectors-csi
  • csi.volumeAttributes
    • connector.name:Nexus Connector 的名称
    • connector.namespace:Nexus Connector 所在命名空间;如果未指定,则使用 Pod 所在命名空间
    • configuration.names:要挂载的配置(例如 settingsnpmrcyarnrcpipconfpypirc
    • configuration.params:每个命名配置的运行时参数的 JSON 字符串(详见下文)

多个配置可以用逗号分隔一起挂载:

configuration.names: "settings,npmrc"
configuration.params: '{"settings": {"mirrorRepository": "maven-public"}, "npmrc": {"registry": "npm-proxy"}}'

Nexus 的 configuration.params

关于 configuration.params 的格式、校验规则及默认注入行为,请参见 Connectors CSI Driver 文档中的 configuration.params

各 Nexus 配置接受的参数如下:

配置名称参数名是否必填默认值描述
settingsmirrorRepository""用作 settings.xml 中镜像的 Nexus Maven 仓库
npmrcregistryNexus NPM 代理仓库名称
npmrcstrictSSL"true"是否要求 SSL("true""false"
yarnrcregistryNexus NPM 代理仓库名称
yarnrcstrictSSL"true"是否要求 SSL("true""false"
pipconfrepositoryNexus PyPI 代理仓库名称
pypircdeployRepository用于发布的 Nexus PyPI hosted 仓库名称

Further Reading

References