使用 x.509 证书对 Client 端进行身份验证

在本页面

MongoDB 支持与安全TLS/SSL connection一起使用的 x.509 证书身份验证。 x.509Client 端身份验证允许Client 端使用证书对服务器进行身份验证而不是用户名和密码。以下教程概述了使用 x.509 进行 Client 端身份验证的步骤。

See also

要将 x.509 身份验证用于副本集/分片群集成员的内部身份验证,请参阅使用 x.509 证书进行会员身份验证

Prerequisites

Important

TLS/SSL,PKI(公钥基础结构)证书(尤其是 x.509 证书)和证书颁发机构的完整描述不在本文档的范围之内。本教程假定您具有 TLS/SSL 的先验知识以及对有效 x.509 证书的访问权限。

Certificate Authority

对于生产用途,您的 MongoDB 部署应使用由单个证书颁发机构生成和签名的有效证书。您或您的组织可以生成和维护独立的证书颁发机构,也可以使用第三方 TLS/SSL 供应商生成的证书。获取和 Management 证书超出了本文档的范围。

Warning

如果未指定--sslCAFile选项及其目标文件,则 x.509Client 端和成员身份验证将不起作用。分片系统中的mongodmongos将无法针对发出证书的可信证书颁发机构(CA)验证与其连接的进程的证书,从而破坏证书链。

从 2.6.4 版开始,如果未指定 CA 文件,则mongod将不会启用 x.509 身份验证。

Client 端 x.509 证书

Note

您必须具有有效的 x.509 证书。

从 MongoDB 3.6.6 开始,如果在使用 x.509 身份验证时指定--sslAllowInvalidCertificatesssl.allowInvalidCertificates: true,则无效的证书仅足以构建 TLS/SSL 连接,而对于身份验证则“不足”。

Client 端证书必须具有以下属性:

  • 单个证书颁发机构(CA)必须同时为 Client 端和服务器颁发证书。

  • Client 端证书必须包含以下字段:

keyUsage = digitalSignature
extendedKeyUsage = clientAuth
  • 每个唯一的 MongoDB 用户必须具有唯一的证书。

  • 包含专有名称(DN)的 Client 端 x.509 证书的主题必须与会员 x.509 证书的主题“不同”。具体而言,主题必须在以下属性中的至少一项方面有所不同:组织(O),组织单位(OU)或域组件(DC)。

Warning

如果 Client 端 x.509 证书的主题与会员 x.509 证书具有相同的OOUDC组合,则该 Client 端将被标识为群集成员,并被授予对系统的完全许可权。

为 x.509 配置的 MongoDB 部署

Command-Options
Configuration File

You can configure mongod/mongos for x.509 authentication from the command-line. For example, if running a replica set, each member would include the following options:

mongod --clusterAuthMode x509 --sslMode requireSSL --sslPEMKeyFile <path to TLS/SSL certificate and key PEM file> --sslCAFile <path to root CA PEM file> --replSet <name> --bind_ip <hostnames>

Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the --bind_ip . For more information, see Localhost Binding Compatibility Changes.

The x.509 configuration requires:

OptionNotes
--clusterAuthMode.If the deployment is a replica set or a sharded cluster, specify x509 for all members of the replica set/sharded cluter.
Omit for standalone.
--sslModeSpecify requireSSL .
--sslPEMKeyFileThe instance's x.509 certificate.
--sslCAFileCertificate Authority file to verify the certificate presented to the instance.

You can configure mongod/mongos for x.509 authentication in the configuration file. For example, if running a replica set, each member would include the following options:

security:
   clusterAuthMode: x509
net:
   ssl:
      mode: requireSSL
      PEMKeyFile: <path to TLS/SSL certificate and key PEM file>
      CAFile: <path to root CA PEM file>

Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the net.bindIp setting. For more information, see Localhost Binding Compatibility Changes.

The x.509 configuration requires:

OptionNotes
security.clusterAuthModeIf the deployment is a replica set or a sharded cluster, specify x509 for all members of the replica set/sharded cluter.
Omit for standalone.
net.ssl.modeSpecify requireSSL .
net.ssl.PEMKeyFileThe instance's x.509 certificate.
net.ssl.CAFileCertificate Authority file to verify the certificate presented to the instance.

Procedures

将 x.509 证书主题添加为用户

要使用 Client 端证书进行身份验证,您必须首先将来自 Client 端证书的subject的值作为 MongoDB 用户添加到$external数据库中。每个唯一的 x.509Client 端证书都对应一个 MongoDB 用户;也就是说,您不能使用单个 Client 端证书来认证一个以上的 MongoDB 用户。

在版本 3.6.3 中更改:要与$external个身份验证用户(即 Kerberos,LDAP,x.509 用户)一起使用会话,用户名不能超过 10k 字节。

Note

subject字符串中的 RDN 必须与RFC2253标准兼容。

  • 您可以使用以下命令从 Client 端证书中检索格式为RFC2253的格式subject
openssl x509 -in <pathToClientPEM> -inform PEM -subject -nameopt RFC2253

该命令返回subject字符串以及证书:

subject= CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry
-----BEGIN CERTIFICATE-----
# ...
-----END CERTIFICATE-----
  • subjectRFC2253兼容值添加为用户。根据需要省略空间。

例如,以下代码添加了一个用户,并授予了test数据库中的用户readWrite角色和userAdminAnyDatabase角色:

db.getSiblingDB("$external").runCommand(
  {
    createUser: "CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry",
    roles: [
         { role: "readWrite", db: "test" },
         { role: "userAdminAnyDatabase", db: "admin" }
    ],
    writeConcern: { w: "majority" , wtimeout: 5000 }
  }
)

有关添加具有角色的用户的详细信息,请参见Management 用户和角色

使用 x.509 证书进行身份验证

拥有将 x.509Client 端证书主题添加为相应的 MongoDB 用户之后,可以使用 Client 端证书进行身份验证。

Connect with Authentication
Authenticate after Connection

To authenticate during connection:

mongo --ssl --sslPEMKeyFile <path to CA signed client PEM file> --sslCAFile <path to root CA PEM file>  --authenticationDatabase '$external' --authenticationMechanism MONGODB-X509
OptionNotes
--ssl
--sslPEMKeyFileClient's x.509 file.
--sslCAFileCertificate Authority file to verify the certificate presented by mongod/mongos instance.
--authenticationDatabaseSpecify '$external' .
--authenticationMechanismSpecify MONGODB-X509 .

You can connect without authentication and use the db.auth() method to authenticate after connection.

For example, if using the mongo shell,

  • Connect mongo shell to the mongod set up for TLS/SSL:
mongo --ssl --sslPEMKeyFile <path to CA signed client PEM file> --sslCAFile <path to root CA PEM file>
OptionNotes
--ssl
--sslPEMKeyFileClient's x.509 file.
--sslCAFileCertificate Authority file to verify the certificate presented by mongod/mongos instance.
  • To perform the authentication, use the db.auth() method in the $external database. For the mechanism field, specify "MONGODB-X509" .
db.getSiblingDB("$external").auth(
  {
    mechanism: "MONGODB-X509"
  }
)