MongoDB 3.6 Authentication IP Restrictions

來源:互聯網
上載者:User

標籤:cal   div   cidr   mongodb   針對   was   acl   custom   上海   

前言

好了,看了這個標題,我不知道你有沒有一種潛意識,如果有,沒錯,那就是這個了,MongoDB 終於開放了我覺得對於一個資料庫的安全來說,比較重要的一個模組了 --- IP 白名單。

眾所周知,MySQL、Oracle、SqlServer等知名的資料庫都是有在安全方面作出相當的防護的。

  • IP白名單

  • 針對庫、表的許可權區分

  • 針對不同的增刪改查、事務、admin等的許可權分配

  • 甚至於Oracle還提供了針對於不同許可權所組合而成的邏輯角色,MySQL在這方面也做了相應的調整。

但是回過頭來看看MongoDB,從最一開始的最最簡單的使用者名稱+密碼的方式,到了之後引入了Bult-in Role、Custom Role、Privilege等的概念推出,再到即將推出的3.6中,明確了將bind_ip調整成了localhost,這也是受比特幣案的影響吧。

這一次,3.6中,新加入了authenticationRestrictions,就是用來解決IP白名單的缺陷。

那麼接下來就讓我們一起來看一下,這個非常吸引我的特性是如何?的吧。

講道理

首先還是先來看看MongoDB的官方文檔吧。https://docs.mongodb.com/master/reference/method/db.createUser/#authentication-restrictions

Field Name Value Description
clientSource Array of IP addresses and/or CIDR ranges If present, when authenticating a user, the server verifies that the client’s IP address is either in the given list or belongs to a CIDR range in the list. If the client’s IP address is not present, the server does not authenticate the user.
serverAddress Array of IP addresses and/or CIDR ranges A list of IP addresses or CIDR ranges to which the client can connect. If present, the server will verify that the client’s connection was accepted via an IP address in the given list. If the connection was accepted via an unrecognized IP address, the server does not authenticate the user.

簡單來說,clientSource 就是針對用戶端的IP 做白名單控制。serverAddress 就是針對服務端的IP 做白名單控制。

那麼這裡問題來了,用戶端IP 好理解,無非就是哪裡連過來的串連麼,這和我們理解上的都一致,那麼服務端地IP呢?什麼意思?這裡,服務端的IP 指的是用戶端在串連過來的時候指定的host 地址,比如:mongo --host=192.168.56.101,那麼serverAddress 就必須包含192.168.56.101,這裡的包含是什麼意思?和MySQL一樣,同樣可以指定B、C網段,來達到多個地址地開放,只是寫法有些許出入,MySQL中是:192.168.56.*,MongoDB 中是:192.168.56.0/24。那麼如果是使用驅動的話,也是一樣的,在host參數中指定對應的IP即可。

那麼接下來我們就來操作一把。

擺事實

1.建立一個應用帳號miracle

use admindb.createUser(   {     user: "root",     pwd: "root",     roles: [{role: ‘root‘, db: ‘admin‘} ]   })db.createUser(   {     user: "miracle",     pwd: "young",     roles: [ {role: ‘readWrite‘, db: ‘young‘} ],     authenticationRestrictions: [ {        clientSource: ["192.168.31.246"],        serverAddress: ["192.168.31.246"]     } ]   })

2.重啟資料庫,開啟許可權認證

3.進入miracle 資料庫,並驗證

4.使用符合要求的格式重新串連資料庫

5.第四步中出錯的原因是因為預設3.6 開啟了bind_ip=localhost,而由於一開始忽略了這個問題,折騰了我好久。重啟資料庫加上--bind_ip_all

6.重新串連

總結

至此,MongoDB的IP白名單功能驗證完畢,希望能夠協助到大家在實際的維護中更安全的控制好開發人員的許可權。

我是上海小胖[MiracleYoung],專註MongoDB、MySQL、Redis等開來源資料庫的 DevOps,擁抱開源,接受收費。

上海小胖[MiracleYoung] 原創地址: https://segmentfault.com/u/shanghaixiaopang/articles

連絡方式:[email protected]

歡迎各位大神前來評論。

每周五,敬請期待,上海小胖[MiracleYoung] 獨更。

如果夏雨荷還在大明湖畔等著我的話,我就不更了。

MongoDB 3.6 Authentication IP Restrictions

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.