诚实守信的人 發表於 2020-5-11 12:17:00

MongoDB最佳安全实践

<div id="output_wrapper_id" class="output_wrapper" style="font-size: 16px; color: rgba(62, 62, 62, 1); line-height: 1.6; word-spacing: 0; letter-spacing: 0; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif">
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0; color: rgba(129, 145, 152, 1); border-left: 6px solid rgba(220, 230, 240, 1); background-color: rgba(242, 247, 251, 1); overflow: auto; word-wrap: normal; word-break: normal">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 0">在前文中提了一句MongoDB的安全,有小伙伴留心了,在公众号后台问。所以今天专门开个文,写一下关于MongoDB的安全。</p>
</blockquote>
<h1 id="hmongodb" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em; text-align: left"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">一、我的一次MongoDB被黑经历</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">近几年,MongoDB应用越来越多,MongoDB也越来越火。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">从2015年开始,MongoDB被一些「非法组织/黑客」盯上了。他们的做法也很简单,连到你的数据库上,把你的数据拿走,然后把你的库清空,留一个消息给你,索要比特币。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">跟最近流行的勒赎病毒一个套路。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">我在某个云上有一台服务器,主要用来做各种研究和测试,随时可以格了重装的那种。上面跑着一个MongoDB,是用默认的参数简单启动的一个单实例。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">早上起来,跑程序测试时,程序直接报错。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">跟踪代码,发现是头天写进去的数据不见了。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">进到数据库,发现数据库都在,但里面的表全被清空了,多了一个Readme的表。查看这个表的内容:</p>
<pre><code class="shell language-shell hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(91, 218, 237, 1); word-wrap: inherit !important; word-break: inherit !important">&gt;</span><span class="bash" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important">&nbsp;db.Readme.find().pretty()</span><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"_id"&nbsp;:&nbsp;"5c18d077fd42b92d8f6271c3",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"BitCoin"&nbsp;:&nbsp;"3639hBBC8M7bwqWKj297Jc61pk9cUSKH5N",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"eMail"&nbsp;:&nbsp;"mongodb@tfwno.gf",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Exchange"&nbsp;:&nbsp;"https://localbitcoins.com",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Solution"&nbsp;:&nbsp;"Your&nbsp;database&nbsp;is&nbsp;downloaded&nbsp;and&nbsp;backed&nbsp;up&nbsp;on&nbsp;our&nbsp;secured&nbsp;servers.&nbsp;To&nbsp;recover&nbsp;your&nbsp;lost&nbsp;data:&nbsp;Send&nbsp;0.2&nbsp;BTC&nbsp;to&nbsp;our&nbsp;BitCoin&nbsp;address&nbsp;and&nbsp;Contact&nbsp;us&nbsp;by&nbsp;eMail&nbsp;with&nbsp;your&nbsp;server&nbsp;IP&nbsp;address&nbsp;and&nbsp;a&nbsp;proof&nbsp;of&nbsp;Payment.&nbsp;Any&nbsp;eMail&nbsp;without&nbsp;your&nbsp;IP&nbsp;address&nbsp;and&nbsp;a&nbsp;proof&nbsp;of&nbsp;Payment&nbsp;will&nbsp;be&nbsp;ignored.&nbsp;Your&nbsp;are&nbsp;welcome!"<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">简单来说,这是一个通知:你的数据被我们绑架了,想要赎回去,需要0.2个比特币。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">一身冷汗。如果这是一个生产环境,如果这是一个系统的运营数据,后果不堪设想。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">究其原因,这个数据库在启动时,用了默认的参数,未加任何防护。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">所以,</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">一定不要用默认的设置运行MongoDB数据库!</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">一定不要用默认的设置运行MongoDB数据库!</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">一定不要用默认的设置运行MongoDB数据库!</p>
<h1 id="h" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em; text-align: left"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">二、安全实践</span></h1>
<h2 id="h1" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em; text-align: left"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">1. 修改端口</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">MongoDB启动时,使用了几个默认的端口:</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">27017: 用于一般的单实例(mongod),或者集群中路由服务器(mongos)</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">27018: 用于集群中的分片服务器</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">27019: 用于集群中的配置服务器</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">实际布署时可以把默认端口换成别的端口。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">命令行:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">$&nbsp;./mongod&nbsp;--port&nbsp;port_number<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">配置文件:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">port=port_number<br></code></pre>
<h2 id="h2ip" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em; text-align: left"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">2. 绑定IP</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">这个要区分一下MongoDB的版本。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">查询MongoDB版本的命令:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">$&nbsp;./mongod&nbsp;--version<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">在MongoDB Version 3.6之前,MongoDB启动时默认绑定到服务器的所有IP上。换句话说,通过所有的IP都可以访问数据库,这儿的安全隐患在于外网IP。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">在3.6之后,MongoDB启动默认绑定127.0.0.1,从外网无法访问,去掉了这个隐患。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">设置绑定IP,命令行:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">$&nbsp;./mongod&nbsp;--bind_ip&nbsp;your_ip&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">#单IP绑定</span><br>或<br>$&nbsp;./mongod&nbsp;--bind_ip&nbsp;your_ip1,your_ip2&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">#多IP绑定</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">配置文件:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">bind_ip=your_ip&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">#单IP绑定</span><br>或<br>bind_ip=your_ip1,your_ip2&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">#多IP绑定</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">MongoDB还提供了一个一次绑定所有IP的参数。命令行:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">$&nbsp;./mongod&nbsp;--bind_ip_all<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">配置文件:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">bind_ip_all=<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">true</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">另外,绑定时,your_ip也可以换成域名your_host,效果是一样的。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">在生产环境中,出于安全的需要,通常可以设置数据库绑定到服务器的内网IP,供数据层操作数据库就好。如果有特殊需要,可以临时绑定到外网IP,操作完成后再去掉。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold">数据库切换绑定IP和端口,对数据库本身没有任何影响。</strong></p>
<h2 id="h3" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em; text-align: left"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">3. 数据库服务器内部身份认证</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">数据库服务器的内部身份认证,是更高一个层次的安全策略,用于保证主从/复制集/集群中各个数据库服务器的安全合法接入。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">内部身份认证,首先需要有一个数字密钥。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">数字密钥可以使用机构签发的证书来生成,也可以使用自生成的密钥。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"><em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-style: italic">当然在低安全级别的情况下,你也可以随手写一个密钥来使用。</em></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">自生成密钥的生成命令:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">$&nbsp;openssl&nbsp;rand&nbsp;-base64&nbsp;756&nbsp;&gt;&nbsp;path_to_keyfile<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">然后设置密钥文件的读写权限:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">$&nbsp;chmod&nbsp;400&nbsp;path_to_keyfile<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">看一下密钥文件:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">$&nbsp;ls&nbsp;-l<br>-rw-r--r--&nbsp;&nbsp;1&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">test</span>&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">test</span>&nbsp;&nbsp;1024&nbsp;&nbsp;5&nbsp;10&nbsp;17:51&nbsp;test.key<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">下面,为数据库启用密钥文件。命令行:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">$&nbsp;./mongod&nbsp;--keyFile&nbsp;path_to_keyfile<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">配置文件:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">keyFile=path_to_keyfile<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">注意:</p>
<ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0 0 0 32px; list-style-type: disc; text-align: left">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">内部认证用在多于一个服务器的情况,例如:主从/复制集/集群上,做服务器之间的互相认证。单个服务器可做可不做,实际上无效。</p>
</li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">内部认证要求认证的服务器使用相同的密钥文件。也就是说,所有的服务器使用同一个密钥文件。</p>
</li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">密钥文件有安全要求,文件权限必须是400,否则数据库启动时会有报错。</p>
</li>
</ul>
<h2 id="h4" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em; text-align: left"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">4. 用户和角色鉴权</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">MongoDB支持为数据库创建用户和分配角色,用用户和角色来管理和使用数据库。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">MongoDB创建用户操作和上面不同。上边的内容,是在数据库运行以前进行,而创建用户,是在数据库运行以后。</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">$&nbsp;./mongo&nbsp;your_ip:your_port<br>&gt;&nbsp;use&nbsp;admin<br>switched&nbsp;to&nbsp;db&nbsp;admin<br>&gt;&nbsp;db.createUser({<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"user"</span>&nbsp;:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"user_name"</span>,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"pwd"</span>&nbsp;:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"user_password"</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"roles"</span>&nbsp;:&nbsp;[{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"role"</span>&nbsp;:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"userAdminAnyDatabase"</span>,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"db"</span>&nbsp;:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"admin"</span>}]})<br>Successfully&nbsp;added&nbsp;user:&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"user"</span>&nbsp;:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"user_name"</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"roles"</span>&nbsp;:&nbsp;[<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"role"</span>&nbsp;:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"userAdminAnyDatabase"</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"db"</span>&nbsp;:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"admin"</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;]<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">这样我们就加入了一个用户。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">MongoDB内建的角色分以下几类:</p>
<ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0 0 0 32px; list-style-type: disc; text-align: left">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">超级用户:root</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">数据库用户角色:read、readWrite</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">数据库管理角色:dbAdmin、dbOwner、userAdmin</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">可操作所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">备份、恢复角色:backup、restore</span></li>
</ul>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">角色不详细解释了,角色名称的英文写的很明白。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">在实际操作中,通常会将用户建在admin中,用roles里的db来指定用户可以使用或管理的数据库名称。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">通过这一通操作,我们已经在数据库中创建好了用户。下面需要服务器启用鉴权。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">命令行:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">./mongod&nbsp;--auth<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">配置文件:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">auth=<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">true</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">这个用于mongod启动的数据库。对于集群的router,即mongos,会默认启用auth,所以不需要显式启用。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">当MongoDB启用鉴权后,再用mongo客户端连接数据库,就需要输入用户帐号信息了。</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">$&nbsp;./mongo&nbsp;-u&nbsp;user_name&nbsp;-p&nbsp;user_password&nbsp;your_ip:your_port/admin<br>或<br>$&nbsp;./mongo&nbsp;-u&nbsp;user_name&nbsp;-p&nbsp;your_ip:your_port/admin&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">#提示输入密码</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">同样,在代码中,数据库连接串也同步变成了:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"MongoConnection"</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"mongodb://user_name:user_password@localhost:27017/admin?wtimeoutMS=2000"</span><br></code></pre>
<h1 id="h-1" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em; text-align: left"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">三、总结</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">一般来说,做完上面的安全处理,就可以完全满足生产环境的安全要求了。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; text-align: left">再高的要求,可以通过启用TLS来强化。这会是另一个文章。</p>
<p>&nbsp;</p>
<hr>
<p>&nbsp;</p>
<table border="0">
<tbody>
<tr>
<td><img src="https://img2020.cnblogs.com/blog/907112/202005/907112-20200527230728396-985375280.jpg"></td>
<td>
<p>微信公众号:老王Plus</p>
<p>扫描二维码,关注个人公众号,可以第一时间得到最新的个人文章和内容推送</p>
<p>本文版权归作者所有,转载请保留此声明和原文链接</p>
</td>
</tr>
</tbody>
</table>
</div><br><br>
来源:https://www.cnblogs.com/tiger-wang/p/12864772.html
頁: [1]
查看完整版本: MongoDB最佳安全实践