简体   繁体   English

在 Node.js 中使用 Let's Encrypt 证书的最安全方法是什么?

[英]What is the most secure way to use Let's Encrypt certificates with Node.js?

I am developing a secure web server on node.js and I am using Let's Encrypt certificates with the https module.我正在 node.js 上开发一个安全的 Web 服务器,我正在使用https模块的 Let's Encrypt 证书。 I want it to run on Ubuntu/Debian machines.我希望它在 Ubuntu/Debian 机器上运行。 By default, the certificate and private key are stored in:默认情况下,证书和私钥存储在:

/etc/letsencrypt/live/domain.name.example/fullchain.pem
/etc/letsencrypt/live/domain.name.example/privkey.pem

These files permissions only allow the root user to read them, so the problem is that the node.js server can't load them normally using:这些文件权限只允许root用户读取,所以问题是node.js服务器无法正常加载使用:

const cert = fs.readFileSync("/etc/letsencrypt/live/domain.name.example/fullchain.pem");
const privKey = fs.readFileSync("/etc/letsencrypt/live/domain.name.example/privkey.pem");

(Which will throw a permission error) (这将引发权限错误)

The only solutions to this I know are:我知道的唯一解决方案是:

  1. running the node server as root so it has the permission to the files (not recommended for node).以 root 身份运行节点服务器,因此它具有文件的权限(不推荐用于节点)。
  2. Copy the files with sudo cp to a local directory and apply permissions with sudo chmod +r so they can be accessed by the server after every certificate renewal (let's encrypt does not recommend to copy these files, this is my current solution though).使用sudo cp将文件复制到本地目录并使用sudo chmod +r应用权限,以便在每次证书更新后服务器可以访问它们(let's encrypt 不建议复制这些文件,尽管这是我当前的解决方案)。
  3. running node as root, load the certificate and private key, and then change the uid to a non-root user with process.setgid() and process.setuid() , which will drop root privileges.以 root 身份运行 node,加载证书和私钥,然后使用process.setgid()process.setuid()将 uid 更改为非 root 用户,这将降低 root 权限。

My question is if there is a better solution to achieve this, or maybe one of these methods are just fine?我的问题是是否有更好的解决方案来实现这一目标,或者其中一种方法就可以了?

Use setgid .使用setgid

Set the group ownership of the directory to the group you're using to run nodejs.将目录的组所有权设置为您用来运行 nodejs 的组。 If your user and group are itay:staff for example, say this例如,如果您的用户和组是itay:staff ,请这样说

chgrp -R staff /etc/letsencrypt/live/domain.name.example

Then set the setgid bit of the directory's permissions like so.然后像这样设置目录权限的setgid位。

chmod 02755 staff /etc/letsencrypt/live/domain.name.example

Thereafter, any files written to that directory will be owned by that group, staff in this example.此后,写入该目录的任何文件都将归该组所有,在本例中为staff So, your nodejs program will be able to read them without any further ado.因此,您的 nodejs 程序将能够毫不费力地读取它们。

As per O. Jones comment, I solved this problem by using nginx as a reverse-proxy for my nodejs server.根据 O. Jones 的评论,我通过使用 nginx 作为我的 nodejs 服务器的反向代理解决了这个问题。 This way nginx handles the SSL without permission issues, and nodejs only needs to run an http server.这样 nginx 处理 SSL 没有权限问题,而 nodejs 只需要运行一个 http 服务器。

The problem was solved by following the second recommendation here by letsencrypt documentation (Quote B) that doesn't require me to create any script to move or copy the files whenever the certificate auto renews (I installed mine with the --apache plug-in, as a side note, it you have your redirect from http to https inside your virtual host, when you first run certbot use --no-redirect to avoid an error being reported during the installation of the certificates).这个问题是通过按照 letencrypt 文档(引用 B)在这里的第二个建议解决的,它不需要我在证书自动更新时创建任何脚本来移动或复制文件(我用 --apache 插件安装了我的) ,作为旁注,当您第一次运行 certbot 时,您可以在虚拟主机内从 http 重定向到 https,请使用 --no-redirect 以避免在安装证书期间报告错误)。

Despite that I found unnecessary to move or copy the pem files.尽管如此,我发现没有必要移动或复制 pem 文件。 In the certbot documentation here , I don't find that letsencrypt doesn't recommend to move the certificates, in their documentation as of now they even tell you how to do it right:此处的 certbot 文档中,我没有发现 letencrypt 不建议移动证书,截至目前,在他们的文档中,他们甚至告诉您如何正确执行:

Quote A:引用 A:

If you would like the live certificate files whose symlink location Certbot updates on each run to reside in a different location, first move them to that location, then specify the full path of each of the four files in the renewal configuration file.如果您希望 Certbot 在每次运行时更新符号链接位置的实时证书文件位于不同位置,请首先将它们移动到该位置,然后在续订配置文件中指定四个文件中每个文件的完整路径。 Since the symlinks are relative links, you must follow this with an invocation of certbot update_symlinks.由于符号链接是相对链接,您必须接着调用 certbot update_symlinks。

For example, say that a certificate's renewal configuration file previously contained the following directives:例如,假设证书的续订配置文件以前包含以下指令:

archive_dir = /etc/letsencrypt/archive/example.com
cert = /etc/letsencrypt/live/example.com/cert.pem
privkey = /etc/letsencrypt/live/example.com/privkey.pem
chain = /etc/letsencrypt/live/example.com/chain.pem
fullchain = /etc/letsencrypt/live/example.com/fullchain.pem

The following commands could be used to specify where these files are located:以下命令可用于指定这些文件所在的位置:

mv /etc/letsencrypt/archive/example.com /home/user/me/certbot/example_archive
sed -i 's,/etc/letsencrypt/archive/example.com,/home/user/me/certbot/example_archive,' /etc/letsencrypt/renewal/example.com.conf
mv /etc/letsencrypt/live/example.com/*.pem /home/user/me/certbot/
sed -i 's,/etc/letsencrypt/live/example.com,/home/user/me/certbot,g' /etc/letsencrypt/renewal/example.com.conf
certbot update_symlinks

Quote B (my solution, just because it is the simples -KISS principle)引用 B (我的解决方案,只是因为它是简单的 -KISS 原则)

Regarding permissions and group ownerships they say the following:关于权限和组所有权,他们说如下:

For historical reasons, the containing directories are created with permissions of 0700 meaning that certificates are accessible only to servers that run as the root user.由于历史原因,包含目录的创建权限为 0700,这意味着只有以 root 用户身份运行的服务器才能访问证书。 If you will never downgrade to an older version of Certbot, then you can safely fix this using chmod 0755 /etc/letsencrypt/{live,archive}.如果您永远不会降级到旧版本的 Certbot,那么您可以使用 chmod 0755 /etc/letsencrypt/{live,archive} 安全地修复此问题。

For servers that drop root privileges before attempting to read the private key file, you will also need to use chgrp and chmod 0640 to allow the server to read /etc/letsencrypt/live/$domain/privkey.pem.对于在尝试读取私钥文件之前放弃 root 权限的服务器,您还需要使用 chgrp 和 chmod 0640 来允许服务器读取 /etc/letsencrypt/live/$domain/privkey.pem。

Which is VERY interesting, they are 700 only for historical reasons.这非常有趣,它们是 700 只因为历史原因。 What they don't clarify is that the /etc/letsencrypt/live and keys folders are 700, and in 20.04 Ubuntu you can't even see that the folder exists unless you become root, yes sudo doesn't work, folder not found error.他们没有澄清的是 /etc/letsencrypt/live 和 keys 文件夹是 700,而在 20.04 Ubuntu 中,除非您成为 root,否则您甚至看不到该文件夹​​存在,是的 sudo 不起作用,找不到文件夹错误。 the -d or domain folders are 755 (/etc/letsencrypt/live.domain.com) and the symlinks themselves to the .pem files are 777. -d 或域文件夹为 755(/etc/letsencrypt/live.domain.com),指向 .pem 文件的符号链接为 777。

Letsencrypt documentation says: Letsencrypt 文档说:

the pem files in the directory mentioned above, are only symlinks: /etc/letsencrypt/archive and /etc/letsencrypt/keys contain all previous keys and certificates, while /etc/letsencrypt/live symlinks to the latest version上面提到的目录中的 pem 文件只是符号链接:/etc/letsencrypt/archive 和 /etc/letsencrypt/keys 包含所有以前的密钥和证书,而 /etc/letsencrypt/live 符号链接到最新版本

The keys themselves have permissions: 600 In my Ubuntu 20.04 system with cerbot --apache certificate and installation I find that the keys folder has 000x_key-certbot.pem files with permissions 600, and the the archive directory has the actual cert1.pem, chain1.pem, fullchain1.pem and privkey1.pem files with permissions: 644, 644, 644 and 600 respectively.密钥本身有权限: 600 在我的 Ubuntu 20.04 系统中使用 cerbot --apache 证书和安装我发现密钥文件夹有 000x_key-certbot.pem 文件,权限为 600,存档目录有实际的 cert1.pem,chain1 .pem、fullchain1.pem 和 privkey1.pem 文件的权限分别为:644、644、644 和 600。

The /etc/letsencrypt/archive/domain.com# folder has permissions 755 and /etc/letsencrypt/archive folder has permissions 700. So access is blocked by hiding the directory and and blocking the keys themselves. /etc/letsencrypt/archive/domain.com# 文件夹的权限为 755,/etc/letsencrypt/archive 文件夹的权限为 700。因此,通过隐藏目录并阻止密钥本身来阻止访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js不会加载“让我们加密证书” - Node.js won't load Let's Encrypt certificates 在 Node.js 中使用私钥/公钥加密文件的最合适和最安全的方法是什么 - What is the most appropriated and secure way to encrypt files using private/public keys in Node.js 如何使用基于Node.js图像的Docker容器的Let's Encrypt - How to use Let's Encrypt with Docker container based on the Node.js image 什么是阻止node.js脚本终止的最有效方法? - What's the most efficient way to prevent a node.js script from terminating? 使用 JavaScript 调用 Node.js 后端函数的最有效方法是什么 - What's the most efficient way to call a Node.js backend function with JavaScript 使用 Node.js 后端托管 50 多个网站(堆栈:Forever、Express、Nginx 和 Let's Encrypt) - Hosting 50+ websites with Node.js backend (stack: Forever, Express, Nginx & Let's Encrypt) Node.js HTTPS 服务器让我们加密证书文件在 Windows 服务器上的位置 - Node.js HTTPS Server Let's Encrypt Certificate Files Location on Windows Server 在新域上将Let's Encrypt与Nginx和Node.js一起使用时出现502错误网关 - 502 Bad Gateway when using Let's Encrypt with Nginx and Node.js on new domain 在node.js中进行屏幕抓取的最优雅方法是什么? - What is the most elegant way to do screen scraping in node.js? 最安全的node.js / express认证机制 - Most secure node.js / express authentication mechanism
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM