NPM 从私人仓库添加失败,当它来自现有项目时,权限被拒绝

NPM add from private repo fails with permission denied when it's from an existing project

我一直在尝试调试这个超级奇怪的问题。有一个项目,我正在尝试使用 npm 命令安装私有存储库。

这在现有项目中不起作用,但在使用 npm init.

创建的新创建项目时起作用

现有项目在/app,新项目在/opt(测试用)

运行 npm add git+ssh://git@github.com:company/repository.git/app returns 中:

npm ERR! code 128
npm ERR! An unknown git error occurred
npm ERR! command git --no-replace-objects ls-remote ssh://git@github.com/company/repository.git
npm ERR! git@github.com: Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-11-24T00_25_10_559Z-debug.log

运行 来自 /opt 项目的完全相同的命令可以正确安装包,没有任何问题。

我 运行 这个来自高山 docker 盒子,安装了 openssh

这是Dockerfile

FROM node:16.13-alpine

RUN apk add --no-cache openssh-client git python2

RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts && ln -s /run/secrets/ssh_key ~/.ssh/id_rsa && ln -s /run/secrets/ssh_pub_key ~/.ssh/id_rsa.pub

RUN mkdir /app

WORKDIR /app

更多信息

/app # node -v
v16.13.0
/app # npm -v
8.1.4
/app # ssh -T git@github.com
Hi AzaZPPL! You've successfully authenticated, but GitHub does not provide shell access.

package.json

{
    "name": "project",
    "version": "1.0.0",
    "private": true,
    "scripts": {
        "dev": "nuxt",
        "build": "nuxt build",
        "start": "nuxt start",
        "generate": "nuxt generate",
        "generate-schema": "node apollo/generate-schema.js",
        "lint": "eslint --ext .js,.vue --ignore-path .eslintignore .",
        "lintfix": "eslint --fix --ext .js,.vue --ignore-path .eslintignore ."
    },
    "dependencies": {
        "@nuxtjs/apollo": "^4.0.1-rc.4",
        "@nuxtjs/auth": "^4.9.1",
        "@nuxtjs/axios": "^5.12.2",
        "@nuxtjs/dayjs": "^1.2.1",
        "@nuxtjs/style-resources": "^1.0.0",
        "apollo-cache-inmemory": "^1.6.6",
        "copy-to-clipboard": "^3.3.1",
        "core-js": "^3.6.5",
        "date-fns": "^2.19.0",
        "dotenv": "^8.2.0",
        "filepond": "^4.27.1",
        "filepond-plugin-file-validate-type": "^1.2.6",
        "filepond-plugin-image-preview": "^4.6.6",
        "graphql-tag": "^2.11.0",
        "js-file-download": "^0.4.12",
        "jwt-decode": "^3.1.2",
        "lodash": "^4.17.20",
        "nuxt": "^2.14.6",
        "nuxt-buefy": "^0.4.10",
        "nuxt-i18n": "^6.15.4",
        "vee-validate": "^3.4.3",
        "vue": "^2.6.12",
        "vue-filepond": "^6.0.3"
    },
    "devDependencies": {
        "@babel/eslint-parser": "^7.16.3",
        "eslint": "^7.12.1",
        "eslint-config-prettier": "^6.15.0",
        "eslint-loader": "^4.0.2",
        "eslint-plugin-prettier": "^3.1.4",
        "eslint-plugin-vue": "^7.1.0",
        "prettier": "^2.1.2",
        "sass": "^1.32.8",
        "sass-loader": "^12.3.0"
    }
}

对这里发生的事情有什么想法吗?

尝试在您的 Dockerfile 中设置

ENV GIT_SSH_COMMAND='ssh -Tv'

这应该会给您更多关于为什么在 npm add 期间考虑或不考虑您的 SSH 密钥的线索。

在@VonC 的回答的帮助下,我能够弄清楚发生了什么。

仔细查看日志,发现其实有一个叫node的用户,是在运行宁npm时使用的。在 Alpine 图像的 Dockerfile 中创建了这个用户,npm 被配置为使用这个用户。

所以发生的事情是每当我登录到 docker 容器时,我以 root 用户身份登录,并且在 Dockerfile 中设置的所有 ssh 密钥都被 运行由 root 用户。

运行 ssh -T git@github.com 有效,因为 root 用户设置正确,但 node 用户设置不正确

我仍然无法理解的是为什么 运行ning /opt 文件夹中的命令有效?无论如何,这是不同日子的谜。

这是我更新的 Dockerfile。我将 ssh 密钥设置为 node 用户并以 node 用户

身份登录
FROM node:16.13-alpine

RUN apk add --no-cache openssh-client git

RUN mkdir /app && chown node:node /app

USER node

RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts

RUN ln -s /run/secrets/ssh_key ~/.ssh/id_rsa

WORKDIR /app