gpt4 book ai didi

mongodb - 使用来自 Grijjy 的 DelphiMongoDB 对 MongoDB 进行身份验证

转载 作者:行者123 更新时间:2023-12-03 19:01:57 27 4
gpt4 key购买 nike

我正在使用来自 Grijjy ( DelphiMongoDB ) 的 DelphiMongoDB,到目前为止它工作得非常酷。但我找不到任何功能来对 MongoDB 进行身份验证。有没有人得到这项工作或想出如何去做?
谢谢和最好的问候

最佳答案

更新:最新版本的 Grijjy 驱动程序现在支持 TLS、X.509 客户端证书身份验证、SCRAM SHA-1 和 SHA-256 身份验证。我们还在 Azure 上针对 MongoDB Atlas 实例对其进行了测试。

这是一个如何使用身份验证的简单示例。

var
Settings: TgoMongoClientSettings;
Client: IgoMongoClient;
Database: IgoMongoDatabase;
Collection: IgoMongoCollection;
Doc: TgoBsonDocument;

begin
Settings := TgoMongoClientSettings.Create;
Settings.Secure := True;
Settings.AuthMechanism := TgoMongoAuthMechanism.SCRAM_SHA_1;
Settings.AuthDatabase := 'admin';
Settings.Username := 'username';
Settings.Password := 'password';
//Settings.QueryFlags := [TgoMongoQueryFlag.SlaveOk];
Client := TgoMongoClient.Create('my.mongodb.server.com', 27017, Settings);
Database := Client.GetDatabase('mydatabase');
Collection := Database.GetCollection('mycollection');

for Doc in Collection.Find() do
Writeln(Doc.ToJson(TgoJsonWriterSettings.Pretty));
end;

旧版:是的,已发布的 Grijjy 驱动程序不支持身份验证,但我们已经在内部对其进行了测试,并且可能会在不久的将来将此功能添加到 Github。如果您想调整以下更改,也欢迎您提出拉取请求:

MongoDB 目前支持 2 种类型的身份验证,SCRAM 和 x.509 证书身份验证。在内部我们已经测试了 x.509 证书身份验证,但是 Github 上的当前驱动程序并没有体现这种能力。我们还没有尝试过 SCRAM。

要使其与我们在 Github 上发布的 MongoDB 驱动程序一起使用,您可能需要进行一些更改。
  • 您需要为 MongoDB 服务器创建自签名 CA 和证书。
  • 您需要配置 MongoDB 服务器以使用证书。
  • 您需要为您的 MongoDB 客户端或客户端创建自签名证书。您可以为所有客户端使用相同的证书。
  • 您需要启用 SSL/TLS 连接并将您的客户端证书与 MongoDB 驱动程序一起使用。

  • 1 要创建所有证书,您需要现有 CA 或创建自签名 CA。您可以使用 openssl.exe 二进制文件来完成大部分操作:

    创建根证书颁发机构(ca.pem 和 privkey.pem):
    openssl req -out ca.pem -new -x509 -days 3650 -subj "/C=US/ST=California/O=Company/CN=root/emailAddress=root@domain.com"

    为 MongoDB 服务器 (server.pem) 创建自签名证书:
    openssl genrsa -out server.key 2048
    openssl req -key server.key -new -out server.req -subj "/C=US/ST=California/O=Company/CN=db.myserver.com/emailAddress=user@domain.com"
    openssl x509 -req -in server.req -CA ca.pem -CAkey privkey.pem -CAcreateserial -out server.crt -days 3650
    type server.key server.crt > server.pem
    openssl verify -CAfile ca.pem server.pem

    2 要将 MongoDB 配置为在 Windows 版本上使用证书(在其他版本上类似),请编辑 c:\data\mongod.cfg:
      systemLog:
    destination: file
    path: c:\data\log\mongod.log
    storage:
    dbPath: c:\data\db
    net:
    port: 27017
    bindIp: 127.0.0.1
    ssl:
    mode: requireSSL
    PEMKeyFile: c:\data\server.pem
    CAFile: c:\data\ca.pem
    {allowConnectionsWithoutCertificates: true }
    {allowInvalidHostnames: true }

    如果您使用自签名证书,您可能需要将 allowInvalidHostnames 设置为 True。

    3 要为 MongoDB 客户端 (client1.pem) 创建自签名证书:
    openssl genrsa -out client1.key 2048
    openssl req -key client1.key -new -out client1.req -subj "/C=US/ST=California/O=Company/CN=client1/emailAddress=user@domain.com"
    openssl x509 -req -in client1.req -CA ca.pem -CAkey privkey.pem -CAserial ca.srl -out client1.crt -days 3650
    type client1.key client1.crt > client1.pem
    openssl verify -CAfile ca.pem client1.pem

    注意:您还需要在用于管理 MongoDB 服务器的任何工具中使用客户端证书。

    4 要为 MongoDB 驱动程序启用 SSL/TLS 连接,您可能必须更改源文件。我们的单元 Grijjy.Http 展示了如何启用“https”驱动程序。本质上,您需要对 Grijjy.MongoDB.Protocol 单元内的 Connection 做两件事,可能在 TgoMongoProtocol.Connect 方法中:
  • 设置 Connection.SSL := True;
  • 将 Connection.Certificate 设置为您创建的 client1.pem。

  • 您将不得不进行一些测试,但我希望它能为您指明正确的方向以使其正常工作。如果可以的话,我会喜欢你对开源项目的贡献。

    关于mongodb - 使用来自 Grijjy 的 DelphiMongoDB 对 MongoDB 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54891843/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com