修补 mongodump 4.2+ 中的 SSL 证书处理 (go lang)
本篇文章向大家介绍《修补 mongodump 4.2+ 中的 SSL 证书处理 (go lang)》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。
我希望将我的 mongo 数据库从版本 4 升级到版本 5。我使用 mongo 的 ssl 配置来确保与数据库的通信是加密的。为了备份数据库,我使用 mongodump。
在 mongo 4.2 中,mongodump
被用 go lang 重写,导致它导入了围绕 ssl 证书处理的常见 go 错误。具体来说,具有中间证书的 pem 文件未完全加载。该错误不会影响 mongo 服务器或客户端本身、任何版本或任何其他应用程序。仅 mongodump 受到影响。
该错误的描述如下:https://jira.mongodb.org/browse/tools-2598
由于工具的 ssl/tls 代码是从 go 驱动程序复制的,当前的实现仅解析 pem 文件中的最后一个证书
这与 mongoshell 行为存在差异,mongoshell 只加载 pem 文件中的第一个证书。
在此相关交流中:https://jira.mongodb.org/browse/tools-2996 我看不到解决方案。我已经尝试了传递给 mongodump 的参数中密钥和证书的每种排列。
我一直在查看 mongodump 的源代码,特别是 ssl 加载代码。
为了帮助 go 开发,我创建了这个 dockerfile 来立即提供用于构建此代码的工作环境,但我不熟悉 go 作为一种语言。
FROM centos:8 RUN yum -y update RUN yum -y install git-core vim-enhanced golang krb5-devel krb5-libs snappy RUN groupadd -r app -g 1000 && \ useradd -r -g app -u 1000 app -d /app && \ mkdir -p /app && \ chown -R app:app /app USER 1000 WORKDIR /app ENTRYPOINT /bin/bash
修复此代码库中的这些 pem 加载错误的可能性有多大?我的 mongo 庄园有很多客户,因此轮换证书来解决这个问题涉及高度的规划和停机时间。修补 mongodump 以接受现有证书感觉像是一个可以接受的中期权衡。
有谁能够帮助我编写适当的补丁,也许开发人员现在使用标准的 ssl 代码?有人对我如何推动此事有任何想法吗? (补丁是理想的选择!)
我提前表示歉意,由于我想要测试的证书的创建方式很复杂,因此我这里没有可重现的测试用例。
正确答案
我编写的这个补丁(基于 godriver-1753 被拒绝的 git pull 请求)解决了这些问题。
https://jira.mongodb.org/browse/GODRIVER-1753
https://github.com/mongodb/mongo-go-driver/pull/521/files
它还修复了 asn1 尾随数据问题。
diff --git a/README.md b/README.md index 20f3ffe8..4b3bed1a 100644 --- a/README.md b/README.md @@ -1,3 +1,13 @@ + +** PATCHED MONGO-TOOLS ** + +In Mongo 4.2, the mongo team rewrote many of their core tools in Go. + +This introduced a bug with the way that PEM files are handled. +Faced with downtime and complexity in a large Mongo estate, this +patched version of mongo resolves the issues. + + MongoDB Tools =================================== diff --git a/common/db/db.go b/common/db/db.go index 3e78abab..9290bb31 100644 --- a/common/db/db.go b/common/db/db.go @@ -186,9 +186,13 @@ func addClientCertFromBytes(cfg *tls.Config, data []byte, keyPasswd string) (str } if currentBlock.Type == "CERTIFICATE" { - certBlock = data[start : len(data)-len(remaining)] - certDecodedBlock = currentBlock.Bytes - start += len(certBlock) + tempCertBlock := data[start : len(data)-len(remaining)] + certBlock = append(certBlock, tempCertBlock...) //To handle usecase where multiple certs are present + + tempCertEncodedBlock := currentBlock.Bytes + certDecodedBlock = append(certDecodedBlock, tempCertEncodedBlock...) + + start += len(tempCertBlock) } else if strings.HasSuffix(currentBlock.Type, "PRIVATE KEY") { isEncrypted := x509.IsEncryptedPEMBlock(currentBlock) || strings.Contains(currentBlock.Type, "ENCRYPTED PRIVATE KEY") if isEncrypted { @@ -244,12 +248,17 @@ func addClientCertFromBytes(cfg *tls.Config, data []byte, keyPasswd string) (str // The documentation for the tls.X509KeyPair indicates that the Leaf certificate is not // retained. - crt, err := x509.ParseCertificate(certDecodedBlock) - if err != nil { - return "", err - } - - return crt.Subject.String(), nil + l := len(certDecodedBlock) + for { + crt, err := x509.ParseCertificate(certDecodedBlock[0:l]) + if err == nil { + return crt.Subject.String(), nil + } + l = l - 1 + if l == 0 { + return "", err + } + } } // create a username for x509 authentication from an x509 certificate subject. diff --git a/vendor/go.mongodb.org/mongo-driver/mongo/options/clientoptions.go b/vendor/go.mongodb.org/mongo-driver/mongo/options/clientoptions.go index d66114fa..2f3c6554 100644 --- a/vendor/go.mongodb.org/mongo-driver/mongo/options/clientoptions.go b/vendor/go.mongodb.org/mongo-driver/mongo/options/clientoptions.go @@ -941,9 +941,13 @@ func addClientCertFromBytes(cfg *tls.Config, data []byte, keyPasswd string) (str } if currentBlock.Type == "CERTIFICATE" { - certBlock = data[start : len(data)-len(remaining)] - certDecodedBlock = currentBlock.Bytes - start += len(certBlock) + tempCertBlock := data[start : len(data)-len(remaining)] + certBlock = append(certBlock, tempCertBlock...) //To handle usecase where multiple certs are present + + tempCertEncodedBlock := currentBlock.Bytes + certDecodedBlock = append(certDecodedBlock, tempCertEncodedBlock...) + + start += len(tempCertBlock) } else if strings.HasSuffix(currentBlock.Type, "PRIVATE KEY") { isEncrypted := x509.IsEncryptedPEMBlock(currentBlock) || strings.Contains(currentBlock.Type, "ENCRYPTED PRIVATE KEY") if isEncrypted { @@ -997,12 +1001,18 @@ func addClientCertFromBytes(cfg *tls.Config, data []byte, keyPasswd string) (str // The documentation for the tls.X509KeyPair indicates that the Leaf certificate is not // retained. - crt, err := x509.ParseCertificate(certDecodedBlock) - if err != nil { - return "", err - } + l := len(certDecodedBlock) + for { + crt, err := x509.ParseCertificate(certDecodedBlock[0:l]) + if err == nil { + return crt.Subject.String(), nil + } + l = l - 1 + if l == 0 { + return "", err + } + } - return x509CertSubject(crt), nil } func stringSliceContains(source []string, target string) bool {
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

- 上一篇
- Golang 工作池实现意外工作

- 下一篇
- 为什么 golang 中需要方法?
-
- Golang · Go问答 | 1年前 |
- 在读取缓冲通道中的内容之前退出
- 139浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 戈兰岛的全球 GOPRIVATE 设置
- 204浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将结构作为参数传递给 xml-rpc
- 325浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何用golang获得小数点以下两位长度?
- 477浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何通过 client-go 和 golang 检索 Kubernetes 指标
- 486浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将多个“参数”映射到单个可变参数的习惯用法
- 439浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将 HTTP 响应正文写入文件后出现 EOF 错误
- 357浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 结构中映射的匿名列表的“复合文字中缺少类型”
- 352浏览 收藏
-
- Golang · Go问答 | 1年前 |
- NATS Jetstream 的性能
- 101浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将复杂的字符串输入转换为mapstring?
- 440浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 相当于GoLang中Java将Object作为方法参数传递
- 212浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何确保所有 goroutine 在没有 time.Sleep 的情况下终止?
- 143浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 笔灵AI生成答辩PPT
- 探索笔灵AI生成答辩PPT的强大功能,快速制作高质量答辩PPT。精准内容提取、多样模板匹配、数据可视化、配套自述稿生成,让您的学术和职场展示更加专业与高效。
- 28次使用
-
- 知网AIGC检测服务系统
- 知网AIGC检测服务系统,专注于检测学术文本中的疑似AI生成内容。依托知网海量高质量文献资源,结合先进的“知识增强AIGC检测技术”,系统能够从语言模式和语义逻辑两方面精准识别AI生成内容,适用于学术研究、教育和企业领域,确保文本的真实性和原创性。
- 42次使用
-
- AIGC检测-Aibiye
- AIbiye官网推出的AIGC检测服务,专注于检测ChatGPT、Gemini、Claude等AIGC工具生成的文本,帮助用户确保论文的原创性和学术规范。支持txt和doc(x)格式,检测范围为论文正文,提供高准确性和便捷的用户体验。
- 39次使用
-
- 易笔AI论文
- 易笔AI论文平台提供自动写作、格式校对、查重检测等功能,支持多种学术领域的论文生成。价格优惠,界面友好,操作简便,适用于学术研究者、学生及论文辅导机构。
- 51次使用
-
- 笔启AI论文写作平台
- 笔启AI论文写作平台提供多类型论文生成服务,支持多语言写作,满足学术研究者、学生和职场人士的需求。平台采用AI 4.0版本,确保论文质量和原创性,并提供查重保障和隐私保护。
- 42次使用
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- Golang取得代码运行时间的问题
- 2023-02-24 501浏览
-
- 请问 go 代码如何实现在代码改动后不需要Ctrl+c,然后重新 go run *.go 文件?
- 2023-01-08 501浏览
-
- 如何从同一个 io.Reader 读取多次
- 2023-04-11 501浏览