|
| 1 | +--- |
| 2 | +weight: 20 |
| 3 | +--- |
| 4 | + |
| 5 | +# NPM Connector |
| 6 | + |
| 7 | +The NPM connector is a platform-agnostic connector that you can use to connect to any NPM registry. |
| 8 | + |
| 9 | +You can use the NPM Connector to securely perform NPM operations in CICD pipelines, or use it in kubernetes workloads to perform NPM operations without credentials. |
| 10 | + |
| 11 | +Additionally, you can centralize the management of NPM access configurations across namespaces, avoiding the need to repeat the NPM credentials in each namespace. |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +This document covers: |
| 16 | + |
| 17 | +- **Integration Requirements**: Prerequisites for target NPM registries |
| 18 | +- **Creating NPM connector** |
| 19 | +- **Advanced Features**: Proxy capabilities and configuration capabilities about NPM connector |
| 20 | + |
| 21 | +## Integration Requirements |
| 22 | + |
| 23 | +**NPM Registries Prerequisites** |
| 24 | + |
| 25 | +- The NPM registry must be able to support [NPM Registry API](https://github.com/npm/registry/blob/main/docs/REGISTRY-API.md) |
| 26 | + |
| 27 | +## Creating a simple NPM connector |
| 28 | + |
| 29 | +Here's how to create a basic NPM Connector: |
| 30 | + |
| 31 | +```yaml |
| 32 | +# NPM Connector |
| 33 | +apiVersion: connectors.alauda.io/v1alpha1 |
| 34 | +kind: Connector |
| 35 | +metadata: |
| 36 | + name: npm-connector |
| 37 | +spec: |
| 38 | + connectorClassName: npm |
| 39 | + address: https://registry.npmjs.org |
| 40 | +``` |
| 41 | +
|
| 42 | +## Fields Reference |
| 43 | +
|
| 44 | +**spec.connectorClassName**: |
| 45 | +
|
| 46 | +`npm` (constant), specifies the ConnectorClass name for NPM integration. |
| 47 | + |
| 48 | +**spec.address**: |
| 49 | + |
| 50 | +Target NPM registry address, for example: `https://registry.npmjs.org`. |
| 51 | + |
| 52 | +When using Nexus as the npm registry, you need to configure the repository address, for example: `https://nexus.example.com/repository/npm-public`. |
| 53 | + |
| 54 | +**spec.auth(optional)**: |
| 55 | + |
| 56 | +specifies the authentication method of the NPM registry |
| 57 | + |
| 58 | +- `spec.auth.name`: should be `basicAuth` for NPM connector. |
| 59 | + |
| 60 | +- `spec.auth.secretRef`: specifies the secret that contains the authentication information of the NPM registry, the secret should be created in the same namespace as the connector. If your NPM registry does not require authentication, you can omit this field. |
| 61 | + |
| 62 | +**Optional Metadata fields**: |
| 63 | + |
| 64 | +- `cpaas.io/description`: Description information for the NPM connector, for example: |
| 65 | + |
| 66 | + ```yaml |
| 67 | + apiVersion: connectors.alauda.io/v1alpha1 |
| 68 | + kind: Connector |
| 69 | + metadata: |
| 70 | + name: npm-connector |
| 71 | + annotations: |
| 72 | + cpaas.io/description: "Connect to team development NPM registry" |
| 73 | + ``` |
| 74 | + |
| 75 | +## Capabilities of NPM Connector |
| 76 | + |
| 77 | +### Authentication |
| 78 | + |
| 79 | +The NPM connector supports the following authentication types: |
| 80 | + |
| 81 | +- `basicAuth`: Username and password-based authentication, corresponding secret type: `kubernetes.io/basic-auth` |
| 82 | + |
| 83 | +For example: |
| 84 | + |
| 85 | +```yaml |
| 86 | +apiVersion: v1 |
| 87 | +stringData: |
| 88 | + username: your-npm-registry-username |
| 89 | + password: your-npm-registry-password |
| 90 | +kind: Secret |
| 91 | +metadata: |
| 92 | + name: npm-secret |
| 93 | +type: kubernetes.io/basic-auth |
| 94 | +``` |
| 95 | + |
| 96 | +For comprehensive status information, see [Connector Status Documentation](../../connectors/concepts/connector.mdx#status-information). |
| 97 | + |
| 98 | +If the NPM registry does not require authentication, you can omit the `secretRef` field: |
| 99 | + |
| 100 | +```yaml |
| 101 | +apiVersion: connectors.alauda.io/v1alpha1 |
| 102 | +kind: Connector |
| 103 | +metadata: |
| 104 | + name: npm-connector |
| 105 | +spec: |
| 106 | + connectorClassName: npm |
| 107 | + address: https://registry.npmjs.org |
| 108 | + auth: |
| 109 | + name: basicAuth |
| 110 | +``` |
| 111 | + |
| 112 | +#### Credential Permissions Required \{#credential_permissions_required} |
| 113 | + |
| 114 | +The required permissions for the configured credential depend on how you intend to use it in your Pods/Pipelines. |
| 115 | + |
| 116 | +For example: |
| 117 | +- **Package operations**: If you only need to download dependencies using `npm install`, the credential only require read permissions for the target NPM repository. |
| 118 | +- **Package and Deploy operations**: If you need to publish artifacts using `npm publish`, the credentials must have both read and write permissions for the target repository. |
| 119 | + |
| 120 | +For security best practices, we recommend creating credentials with minimal required permissions. When additional privileges are needed, create separate Connectors with more privileged secret and use namespace isolation to control which users can access each Connector. |
| 121 | + |
| 122 | +### NPM Connector Proxy and Configuration with npmrc and yarnrc.yml files |
| 123 | + |
| 124 | +To provide clients with the ability to access NPM registry without credentials, the NPM connector provides a proxy server to automatically inject authentication information. |
| 125 | + |
| 126 | +Clients can use this proxy server to access NPM registry without needing to configure credentials on the client side. |
| 127 | + |
| 128 | +To simplify usage, the NPM connectorclass provides `.npmrc` and `.yarnrc.yml` files that can be mounted into Pods via CSI. In the Pod, when executing NPM operations, the proxy service can automatically inject authentication information. |
| 129 | + |
| 130 | +:::warning |
| 131 | +The `.yarnrc.yml` file is only supported in the Yarn 2.x version. |
| 132 | +::: |
| 133 | + |
| 134 | +#### Proxy Address |
| 135 | + |
| 136 | +Upon Connector creation, the system automatically provisions a proxy service for the target NPM registry. |
| 137 | + |
| 138 | +The proxy endpoint is recorded in `status.proxy.httpAddress`: |
| 139 | + |
| 140 | +For example: |
| 141 | + |
| 142 | +```yaml |
| 143 | +apiVersion: connectors.alauda.io/v1alpha1 |
| 144 | +kind: Connector |
| 145 | +metadata: |
| 146 | + name: npm-connector |
| 147 | +spec: |
| 148 | + # connector spec fields |
| 149 | +status: |
| 150 | + conditions: |
| 151 | + # status conditions |
| 152 | + proxy: |
| 153 | + httpAddress: |
| 154 | + url: http://c-npm-connector.default.svc.cluster.local |
| 155 | +``` |
| 156 | + |
| 157 | +#### .npmrc configuration file \{#npmrc-configuration-file} |
| 158 | + |
| 159 | +The NPM connector provides the following configuration: |
| 160 | + |
| 161 | +**.npmrc**: |
| 162 | + |
| 163 | +- Provides a `.npmrc` configuration file. Combined with the connector-csi-driver, this configuration file will be mounted into the Pod, allowing access to the NPM registry through the proxy without needing to configure credentials on the client side. |
| 164 | + |
| 165 | +Example of the configuration file generated in the Pod: |
| 166 | + |
| 167 | + ```yaml |
| 168 | + # NPM Registry Configuration |
| 169 | + registry=http://npm-registry.example.com/ |
| 170 | + |
| 171 | + # The authentication token is fake, because the connector will not use it, it will be used for proxy requests. |
| 172 | + //npm-registry.example.com/:_auth=fAd326jYkI123456789xxx |
| 173 | +
|
| 174 | + # Set the connector proxy URL for npm registry access |
| 175 | + https-proxy=http://connector-ns%2Fconnector-name:[email protected]/ |
| 176 | + proxy=http://connector-ns%2Fconnector-name:[email protected]/ |
| 177 | +
|
| 178 | + # Disable strict SSL verification for internal registries |
| 179 | + strict-ssl=false |
| 180 | + |
| 181 | + # Disable npm audit to avoid security warnings during CI/CD |
| 182 | + audit=false |
| 183 | + |
| 184 | + # Disable funding messages to reduce output noise |
| 185 | + fund=false |
| 186 | + ``` |
| 187 | + |
| 188 | +#### .yarnrc.yml configuration file \{#yarnrc-yml-configuration-file} |
| 189 | + |
| 190 | +- Provides a `.yarnrc.yml` configuration file. Combined with the connector-csi-driver, this configuration file will be mounted into the Pod, allowing access to the NPM registry through the proxy without needing to configure credentials on the client side. |
| 191 | + |
| 192 | + ```ini |
| 193 | + # Set the NPM registry server URL for package resolution |
| 194 | + npmRegistryServer: "http://npm-registry.example.com/" |
| 195 | + |
| 196 | + # The authentication token is fake, because the connector will not use it, it will be used for proxy requests. |
| 197 | + npmAuthIdent: "fAd326jYkI123456789xxx" |
| 198 | +
|
| 199 | + # Always authenticate to the registry |
| 200 | + # This is required for the connector to work correctly, if the npmAlwaysAuth is not set to true, the metadata request will not be authenticated. |
| 201 | + npmAlwaysAuth: true |
| 202 | +
|
| 203 | + # The unsafeHttpWhitelist is used to whitelist the host for proxy requests. |
| 204 | + unsafeHttpWhitelist: |
| 205 | + - npm-registry.example.com |
| 206 | +
|
| 207 | + # authentication for proxy requests |
| 208 | + httpProxy: "http://connector-ns%2Fconnector-name:[email protected]/" |
| 209 | + httpsProxy: "http://connector-ns%2Fconnector-name:[email protected]/" |
| 210 | + |
| 211 | + # Disable strict SSL verification for internal registries |
| 212 | + enableStrictSsl: false |
| 213 | + |
| 214 | + # Set the registry URL for package publishing |
| 215 | + # Ensures packages are published to the correct registry |
| 216 | + npmPublishRegistry: "http://npm-registry.example.com/" |
| 217 | + ``` |
| 218 | + |
| 219 | +For detailed proxy mechanics, see [How It Works](../quick_start.mdx#what-happens-under-the-hood) in the Quick Start guide. |
| 220 | + |
| 221 | +:::warning |
| 222 | + |
| 223 | +When using yarn with `HTTPS` registry, you need to configure yarn with the Connector Proxy certificate trust through environment variables, otherwise certificate errors will occur. |
| 224 | + |
| 225 | +The certificate configuration for yarn is as follows: |
| 226 | + |
| 227 | +```sh |
| 228 | +export NODE_EXTRA_CA_CERTS=/opt/yarn/ca.cert # replace with the actual path where ca.cert is mounted in the Pod |
| 229 | +``` |
| 230 | +::: |
| 231 | + |
| 232 | +#### ca.cert file |
| 233 | + |
| 234 | +The NPM connector also provides a `ca.cert` file containing the Connector Proxy's CA certificate. This file can be mounted into the Pod via Connector CSI Driver to establish TLS trust when accessing the proxy over HTTPS. |
| 235 | +#### Using Connectors CSI Driver to mount .npmrc and .yarnrc.yml file \{#using-connectors-csi-driver-to-mount-npmrc-and-yarnrc-yml-file} |
| 236 | + |
| 237 | +The NPM connector provides a `.npmrc`, `.yarnrc.yml` and `ca.cert` file that can be mounted into the Pod via Connector CSI Driver. |
| 238 | + |
| 239 | +For example: |
| 240 | + |
| 241 | +``` yaml |
| 242 | +spec: |
| 243 | +
|
| 244 | + volumes: |
| 245 | + - name: npmrc |
| 246 | + csi: |
| 247 | + readOnly: true |
| 248 | + driver: connectors-csi |
| 249 | + volumeAttributes: |
| 250 | + connector.name: "npm-connector" |
| 251 | + configuration.names: "npmrc" |
| 252 | + - name: yarnrc |
| 253 | + csi: |
| 254 | + readOnly: true |
| 255 | + driver: connectors-csi |
| 256 | + volumeAttributes: |
| 257 | + connector.name: "npm-connector" |
| 258 | + configuration.names: "yarnrc" |
| 259 | +``` |
| 260 | + |
| 261 | +parameter descriptions: |
| 262 | + |
| 263 | +- `csi.readOnly`: Fixed value `true` |
| 264 | +- `csi.driver`: The Connector CSI Driver, fixed as `connectors-csi`. |
| 265 | +- `csi.volumeAttributes`: CSI Volume attributes |
| 266 | + - `connector.name`: Name of the NPM Connector |
| 267 | + - `connector.namespace`: Namespace of the NPM Connector; if not specified, the Pod's namespace is used |
| 268 | + - `configuration.names`: Configuration name, provided by the NPM Connector. As above, `npmrc` and `yarnrc` are supported. |
| 269 | + |
| 270 | +For detailed information about how to use the `.npmrc` and `.yarnrc.yml` file in the Pod by connectors-csi-driver, please refer to [Using NPM Connectors in kubernetes jobs](../quick_start.mdx) |
| 271 | + |
| 272 | +## Further Reading |
| 273 | + |
| 274 | +- [Using NPM Connectors as Distribution Management Repository](../quick_start.mdx) |
| 275 | + |
| 276 | +## References |
| 277 | + |
| 278 | +- [Concepts of Connector](../../connectors/concepts/connector.mdx) |
| 279 | +- [Connector Proxy](../../connectors/concepts/connectors_proxy.mdx) |
| 280 | +- [Connector CSI Driver](../../connectors/concepts/connectors_csi.mdx) |
| 281 | +- [Kubernetes CSI Volume](https://kubernetes.io/docs/concepts/storage/volumes/#csi) |
0 commit comments