{"id":207790,"date":"2025-07-08T12:02:16","date_gmt":"2025-07-08T04:02:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207790\/"},"modified":"2025-07-08T12:02:16","modified_gmt":"2025-07-08T04:02:16","slug":"%e4%bf%ae%e8%a1%a5-mongodump-4-2-%e4%b8%ad%e7%9a%84-ssl-%e8%af%81%e4%b9%a6%e5%a4%84%e7%90%86-go-lang","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207790\/","title":{"rendered":"\u4fee\u8865 mongodump 4.2+ \u4e2d\u7684 SSL \u8bc1\u4e66\u5904\u7406 (go lang)"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4fee\u8865 mongodump 4.2+ \u4e2d\u7684 SSL \u8bc1\u4e66\u5904\u7406 (go lang)<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 17:12:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u4fee\u8865 mongodump 4.2+ \u4e2d\u7684 SSL \u8bc1\u4e66\u5904\u7406 (go lang)\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5e0c\u671b\u5c06\u6211\u7684 mongo \u6570\u636e\u5e93\u4ece\u7248\u672c 4 \u5347\u7ea7\u5230\u7248\u672c 5\u3002\u6211\u4f7f\u7528 mongo \u7684 ssl \u914d\u7f6e\u6765\u786e\u4fdd\u4e0e\u6570\u636e\u5e93\u7684\u901a\u4fe1\u662f\u52a0\u5bc6\u7684\u3002\u4e3a\u4e86\u5907\u4efd\u6570\u636e\u5e93\uff0c\u6211\u4f7f\u7528 mongodump\u3002<\/p>\n<p>\u5728 mongo 4.2 \u4e2d\uff0c<code>mongodump<\/code> \u88ab\u7528 go lang \u91cd\u5199\uff0c\u5bfc\u81f4\u5b83\u5bfc\u5165\u4e86\u56f4\u7ed5 ssl \u8bc1\u4e66\u5904\u7406\u7684\u5e38\u89c1 go \u9519\u8bef\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5177\u6709\u4e2d\u95f4\u8bc1\u4e66\u7684 pem \u6587\u4ef6\u672a\u5b8c\u5168\u52a0\u8f7d\u3002\u8be5\u9519\u8bef\u4e0d\u4f1a\u5f71\u54cd mongo \u670d\u52a1\u5668\u6216\u5ba2\u6237\u7aef\u672c\u8eab\u3001\u4efb\u4f55\u7248\u672c\u6216\u4efb\u4f55\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u3002\u4ec5 mongodump \u53d7\u5230\u5f71\u54cd\u3002<\/p>\n<p>\u8be5\u9519\u8bef\u7684\u63cf\u8ff0\u5982\u4e0b\uff1ahttps:\/\/jira.mongodb.org\/browse\/tools-2598<\/p>\n<p> \u7531\u4e8e\u5de5\u5177\u7684 ssl\/tls \u4ee3\u7801\u662f\u4ece go \u9a71\u52a8\u7a0b\u5e8f\u590d\u5236\u7684\uff0c\u5f53\u524d\u7684\u5b9e\u73b0\u4ec5\u89e3\u6790 pem \u6587\u4ef6\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u8bc1\u4e66 <\/p>\n<p> \u8fd9\u4e0e mongoshell \u884c\u4e3a\u5b58\u5728\u5dee\u5f02\uff0cmongoshell \u53ea\u52a0\u8f7d pem \u6587\u4ef6\u4e2d\u7684\u7b2c\u4e00\u4e2a\u8bc1\u4e66\u3002 <\/p>\n<p>\u5728\u6b64\u76f8\u5173\u4ea4\u6d41\u4e2d\uff1ahttps:\/\/jira.mongodb.org\/browse\/tools-2996 \u6211\u770b\u4e0d\u5230\u89e3\u51b3\u65b9\u6848\u3002\u6211\u5df2\u7ecf\u5c1d\u8bd5\u4e86\u4f20\u9012\u7ed9 mongodump \u7684\u53c2\u6570\u4e2d\u5bc6\u94a5\u548c\u8bc1\u4e66\u7684\u6bcf\u79cd\u6392\u5217\u3002<\/p>\n<p>\u6211\u4e00\u76f4\u5728\u67e5\u770b mongodump \u7684\u6e90\u4ee3\u7801\uff0c\u7279\u522b\u662f ssl \u52a0\u8f7d\u4ee3\u7801\u3002<\/p>\n<p>\u4e3a\u4e86\u5e2e\u52a9 go \u5f00\u53d1\uff0c\u6211\u521b\u5efa\u4e86\u8fd9\u4e2a dockerfile \u6765\u7acb\u5373\u63d0\u4f9b\u7528\u4e8e\u6784\u5efa\u6b64\u4ee3\u7801\u7684\u5de5\u4f5c\u73af\u5883\uff0c\u4f46\u6211\u4e0d\u719f\u6089 go \u4f5c\u4e3a\u4e00\u79cd\u8bed\u8a00\u3002<\/p>\n<pre>FROM centos:8\nRUN yum -y update\nRUN yum -y install git-core vim-enhanced golang krb5-devel krb5-libs snappy\nRUN groupadd -r app -g 1000 &amp;&amp; \\\n    useradd -r -g app -u 1000 app -d \/app &amp;&amp; \\\n    mkdir -p \/app &amp;&amp; \\\n    chown -R app:app \/app\nUSER 1000\nWORKDIR \/app\nENTRYPOINT \/bin\/bash<\/pre>\n<p>\u4fee\u590d\u6b64\u4ee3\u7801\u5e93\u4e2d\u7684\u8fd9\u4e9b pem \u52a0\u8f7d\u9519\u8bef\u7684\u53ef\u80fd\u6027\u6709\u591a\u5927\uff1f\u6211\u7684 mongo \u5e84\u56ed\u6709\u5f88\u591a\u5ba2\u6237\uff0c\u56e0\u6b64\u8f6e\u6362\u8bc1\u4e66\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6d89\u53ca\u9ad8\u5ea6\u7684\u89c4\u5212\u548c\u505c\u673a\u65f6\u95f4\u3002\u4fee\u8865 mongodump \u4ee5\u63a5\u53d7\u73b0\u6709\u8bc1\u4e66\u611f\u89c9\u50cf\u662f\u4e00\u4e2a\u53ef\u4ee5\u63a5\u53d7\u7684\u4e2d\u671f\u6743\u8861\u3002<\/p>\n<p>\u6709\u8c01\u80fd\u591f\u5e2e\u52a9\u6211\u7f16\u5199\u9002\u5f53\u7684\u8865\u4e01\uff0c\u4e5f\u8bb8\u5f00\u53d1\u4eba\u5458\u73b0\u5728\u4f7f\u7528\u6807\u51c6\u7684 ssl \u4ee3\u7801\uff1f\u6709\u4eba\u5bf9\u6211\u5982\u4f55\u63a8\u52a8\u6b64\u4e8b\u6709\u4efb\u4f55\u60f3\u6cd5\u5417\uff1f \uff08\u8865\u4e01\u662f\u7406\u60f3\u7684\u9009\u62e9\uff01\uff09<\/p>\n<p>\u6211\u63d0\u524d\u8868\u793a\u6b49\u610f\uff0c\u7531\u4e8e\u6211\u60f3\u8981\u6d4b\u8bd5\u7684\u8bc1\u4e66\u7684\u521b\u5efa\u65b9\u5f0f\u5f88\u590d\u6742\uff0c\u56e0\u6b64\u6211\u8fd9\u91cc\u6ca1\u6709\u53ef\u91cd\u73b0\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u7f16\u5199\u7684\u8fd9\u4e2a\u8865\u4e01\uff08\u57fa\u4e8e godriver-1753 \u88ab\u62d2\u7edd\u7684 git pull \u8bf7\u6c42\uff09\u89e3\u51b3\u4e86\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/p>\n<p>\u5b83\u8fd8\u4fee\u590d\u4e86 asn1 \u5c3e\u968f\u6570\u636e\u95ee\u9898\u3002<\/p>\n<pre>diff --git a\/README.md b\/README.md\nindex 20f3ffe8..4b3bed1a 100644\n--- a\/README.md\n b\/README.md\n@@ -1,3 +1,13 @@\n+\n+** PATCHED MONGO-TOOLS **\n+\n+In Mongo 4.2, the mongo team rewrote many of their core tools in Go.\n+\n+This introduced a bug with the way that PEM files are handled.\n+Faced with downtime and complexity in a large Mongo estate, this\n+patched version of mongo resolves the issues.\n+\n+\n MongoDB Tools\n ===================================\n \ndiff --git a\/common\/db\/db.go b\/common\/db\/db.go\nindex 3e78abab..9290bb31 100644\n--- a\/common\/db\/db.go\n b\/common\/db\/db.go\n@@ -186,9 +186,13 @@ func addClientCertFromBytes(cfg *tls.Config, data []byte, keyPasswd string) (str\n                }\n \n                if currentBlock.Type == \"CERTIFICATE\" {\n-           certBlock = data[start : len(data)-len(remaining)]\n-           certDecodedBlock = currentBlock.Bytes\n-           start += len(certBlock)\n+           tempCertBlock := data[start : len(data)-len(remaining)]\n+           certBlock = append(certBlock, tempCertBlock...) \/\/To handle usecase where multiple certs are present\n+\n+           tempCertEncodedBlock := currentBlock.Bytes\n+           certDecodedBlock = append(certDecodedBlock, tempCertEncodedBlock...)\n+\n+           start += len(tempCertBlock)\n                } else if strings.HasSuffix(currentBlock.Type, \"PRIVATE KEY\") {\n                        isEncrypted := x509.IsEncryptedPEMBlock(currentBlock) || strings.Contains(currentBlock.Type, \"ENCRYPTED PRIVATE KEY\")\n                        if isEncrypted {\n@@ -244,12 +248,17 @@ func addClientCertFromBytes(cfg *tls.Config, data []byte, keyPasswd string) (str\n \n        \/\/ The documentation for the tls.X509KeyPair indicates that the Leaf certificate is not\n        \/\/ retained.\n-   crt, err := x509.ParseCertificate(certDecodedBlock)\n-   if err != nil {\n-       return \"\", err\n-   }\n-\n-   return crt.Subject.String(), nil\n+        l := len(certDecodedBlock)\n+        for {\n+          crt, err := x509.ParseCertificate(certDecodedBlock[0:l])\n+          if err == nil {\n+            return crt.Subject.String(), nil\n+          }\n+          l = l - 1\n+          if l == 0 {\n+            return \"\", err\n+          }\n+        }\n }\n \n \/\/ create a username for x509 authentication from an x509 certificate subject.\ndiff --git a\/vendor\/go.mongodb.org\/mongo-driver\/mongo\/options\/clientoptions.go b\/vendor\/go.mongodb.org\/mongo-driver\/mongo\/options\/clientoptions.go\nindex d66114fa..2f3c6554 100644\n--- a\/vendor\/go.mongodb.org\/mongo-driver\/mongo\/options\/clientoptions.go\n b\/vendor\/go.mongodb.org\/mongo-driver\/mongo\/options\/clientoptions.go\n@@ -941,9 +941,13 @@ func addClientCertFromBytes(cfg *tls.Config, data []byte, keyPasswd string) (str\n                }\n \n                if currentBlock.Type == \"CERTIFICATE\" {\n-           certBlock = data[start : len(data)-len(remaining)]\n-           certDecodedBlock = currentBlock.Bytes\n-           start += len(certBlock)\n+           tempCertBlock := data[start : len(data)-len(remaining)]\n+           certBlock = append(certBlock, tempCertBlock...) \/\/To handle usecase where multiple certs are present\n+\n+           tempCertEncodedBlock := currentBlock.Bytes\n+           certDecodedBlock = append(certDecodedBlock, tempCertEncodedBlock...)\n+\n+           start += len(tempCertBlock)\n                } else if strings.HasSuffix(currentBlock.Type, \"PRIVATE KEY\") {\n                        isEncrypted := x509.IsEncryptedPEMBlock(currentBlock) || strings.Contains(currentBlock.Type, \"ENCRYPTED PRIVATE KEY\")\n                        if isEncrypted {\n@@ -997,12 +1001,18 @@ func addClientCertFromBytes(cfg *tls.Config, data []byte, keyPasswd string) (str\n \n        \/\/ The documentation for the tls.X509KeyPair indicates that the Leaf certificate is not\n        \/\/ retained.\n-   crt, err := x509.ParseCertificate(certDecodedBlock)\n-   if err != nil {\n-       return \"\", err\n-   }\n+        l := len(certDecodedBlock)\n+        for {\n+          crt, err := x509.ParseCertificate(certDecodedBlock[0:l])\n+          if err == nil {\n+            return crt.Subject.String(), nil\n+          }\n+          l = l - 1\n+          if l == 0 {\n+            return \"\", err\n+          }\n+        }\n \n-   return x509CertSubject(crt), nil\n }\n \n func stringSliceContains(source []string, target string) bool {<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207790","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207790"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207790\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}