mongodbのアクセス制御


組み込みロール、具体的な参考:https://docs.mongodb.com/manual/reference/built-in-roles
Read:指定データベースreadWriteの読み取りを許可する:指定データベースdbAdminの読み書きを許可する:インデックスの作成、削除、統計の表示、システムへのアクセスなど、指定データベースで管理関数を実行することを許可する.profileuserAdmin:systemにユーザーを許可する.usersコレクション書き込みでは、指定したデータベースでユーザーclusterAdminを作成、削除、管理できます.adminデータベースでのみ使用でき、ユーザーにすべてのスライスおよびレプリケーションセット関連関数の管理権限を付与します.readAnyDatabase:adminデータベースでのみ使用可能、ユーザーに付与されたすべてのデータベースの読み取り権限readWriteAnyDatabase:adminデータベースでのみ使用可能、ユーザーに付与されたすべてのデータベースの読み取りと書き込み権限userAdminAnyDatabase:adminデータベースでのみ使用可能、ユーザーに付与されたすべてのデータベースのuserAdmin権限dbAdminAnyDatabase:adminデータベースでのみ使用可能、ユーザーに付与されたすべてのデータベースのdbAdmin権限.root:adminデータベースでのみ使用できます.スーパーアカウント、スーパー権限
ユーザーファイルはadminライブラリの下にあるsystem.usersテーブルでは、デフォルトのMongoDBはパスワードにアクセスしておらず、安全ではありません
1.データベース管理者ユーザーadminUserと一般ユーザーherrywenの追加
mongo --port 27017
use admin
db.createUser(
{
user: "adminUser",
pwd: "adminPass",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

use herrywen
db.createUser(
  {
    user: "herrywen",
    pwd: "herrywen",
    roles: [ { role: "readWrite", db: "herrywen" },
             { role: "read", db: "admin" } ]
  }
)

2.192.168.255.134にプロファイルを追加し、検証を開始
cat /etc/mongod.conf
security:
  authorization: enabled

3.mongdbサービスsystemctl restart mongdbの再起動
4.アクセスできるかどうかをテストする
[root@worker1 ~]# mongo --host 192.168.255.134 --port 27017  -u adminUser -p adminPass --authenticationDatabase "admin"
MongoDB shell version v4.2.1
connecting to: mongodb://192.168.255.134:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f5114890-0b2e-43a2-8a60-a8b265e68a44") }
MongoDB server version: 4.2.1
MongoDB Enterprise > use admin;
switched to db admin
MongoDB Enterprise > show collections;
system.users
system.version
MongoDB Enterprise > exit
bye

5.直接ログインした場合、adminライブラリを切り替えると、プロンプトに権限がありません.dbを使用する必要があります.auth()検証
[root@worker1 ~]# mongo --host 192.168.255.134 --port 27017
MongoDB shell version v4.2.1
connecting to: mongodb://192.168.255.134:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9bcb1b37-7cfa-4aff-8947-6d633eee01be") }
MongoDB server version: 4.2.1
MongoDB Enterprise > use admin
switched to db admin
MongoDB Enterprise > show collections;
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
MongoDB Enterprise > show collections;
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
MongoDB Enterprise > db.auth("adminUser","adminPass")
1
MongoDB Enterprise > show collections;
system.users
system.version

6.herrywenライブラリに直接ログイン
[root@worker1 ~]# mongo --host 192.168.255.134 --port 27017  -u herrywen -p herrywen --authenticationDatabase "herrywen"
MongoDB shell version v4.2.1
connecting to: mongodb://192.168.255.134:27017/?authSource=herrywen&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9d906997-681a-43b4-b541-dbe5d197cd1f") }
MongoDB server version: 4.2.1
MongoDB Enterprise > use herrywen
switched to db herrywen
MongoDB Enterprise > show collections;
MongoDB Enterprise > db.test3.insert({title: 'MongoDB',
...     description: 'hello,world',
...     by: 'herrywen',
...     url: 'http://www.51cto.com',
...     tags: ['mongodb', 'database', 'NoSQL'],
...     likes: 100})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > show collections;

7.adminUserユーザーにherrywenライブラリの読み書き権限を追加
use admin
db.grantRolesToUser(     "adminUser",     [       { role: "readWrite", db: "herrywen" }     ] )
db.system.users.find().pretty();  

8.herrywenユーザーにherrywen 1ライブラリの読み書き権限とadminデータベースの読み書き権限を追加する
use herrywen
db.grantRolesToUser(     "herrywen",     [       { role: "readWrite", db: "herrywen1" } ,{ role: "read", db: "admin" }   ] )

9.herrywenのherrywen 1ライブラリへの読み書き権限とadminデータベースへの読み書き権限の取り消し
db.revokeRolesFromUser(
    "herrywen",
    [
                {
                        "role" : "read",
                        "db" : "admin"
                },
                {
                        "role" : "readWrite",
                        "db" : "herrywen1"
                }
     ]
)

10.現在のherrywenユーザーの権限を表示するか、dbを使用してheryrwenデータベースの下を切り替えることができます.getUser('herrywen')は表示しますが、面倒なのでshow usersを直接使うことができます.
MongoDB Enterprise > show users
{
        "_id" : "herrywen.herrywen",
        "userId" : UUID("68fc696d-9825-43b6-9afb-d4a040b480a3"),
        "user" : "herrywen",
        "db" : "herrywen",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "herrywen"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}

11.herrywenユーザのパスワードを変更するdb.changeUserPassword("herrywen","herrywen-2")12.herrywenユーザーdb.dropUser("herrywen")の削除