定義
createRoleCreates a role and specifies its privileges. The role applies to the database on which you run the command. The
createRolecommand returns a duplicate role error if the role already exists in the database.Tip
In
mongosh, this command can also be run through thedb.createRole()helper method.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
重要
このコマンドは、MongoDB Atlas クラスターではサポートされていません。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
構文
このコマンドの構文は、次のとおりです。
db.adminCommand( { createRole: "<new role>", privileges: [ { resource: { <resource> }, actions: [ "<action>", ... ] }, ... ], roles: [ { role: "<role>", db: "<database>" } | "<role>", ... ], authenticationRestrictions: [ { clientSource: ["<IP>" | "<CIDR range>", ...], serverAddress: ["<IP>" | "<CIDR range>", ...] }, ... ], writeConcern: <write concern document>, comment: <any> } )
コマンドフィールド
The createRole command has the following fields:
フィールド | タイプ | 説明 |
|---|---|---|
| string | 新しいロールの名前。 |
| 配列 | ロールに付与する特権。特権は、リソースと許可されたアクションから構成されます。権限の構文については、
|
| 配列 | このロールが特権を継承するロールの配列。
|
| 配列 | 任意。 サーバーがロールに強制する認証制限。このロールを付与されたユーザーが接続できる、または接続元として使用できる IP アドレスと CIDR 範囲のリストを指定します。 |
| ドキュメント | 任意。 操作の 書込み保証( write concern ) のレベル。 詳しくは、 書込み保証(write concern) の仕様を参照してください。 |
| any | 任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。
コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。 |
ロール
rolesフィールドでは、組み込みロールとユーザー定義ロールの両方を指定できます。
To specify a role that exists in the same database where createRole runs, you can either specify the role with the name of the role:
"readWrite"
または、次のように、ドキュメントを使用してロールを指定することもできます。
{ role: "<role>", db: "<database>" }
別のデータベースに存在するロールを指定するには、 ドキュメントを使用してロールを指定します。
認証の制限
authenticationRestrictionsドキュメントには、次のフィールドのみを含めることができます。 authenticationRestrictionsドキュメントに認識されないフィールドが含まれている場合、サーバーはエラーをスローします。
フィールド名 | 値 | 説明 |
|---|---|---|
| IP アドレスおよび/またはCIDR範囲の配列 | 存在する場合、サーバーはユーザーを認証する際に、クライアントの IP アドレスが指定されたリストに含まれているか、リスト内の CIDR 範囲に属していることを確認します。 クライアントの IP アドレスが存在しない場合、サーバーはユーザーを認証しません。 |
| IP アドレスおよび/またはCIDR範囲の配列 | クライアントが接続できる IP アドレスまたは CIDR 範囲のリスト。 存在する場合、サーバーはクライアントの接続が指定されたリスト内の IP アドレス経由で受け入れられたことを確認します。 認識されない IP アドレス経由で接続が受け入れられた場合、サーバーはユーザーを認証しません。 |
重要
ユーザーが互換性のない認証制限を持つ複数のロールを継承した場合、そのユーザーは使用できなくなります。
たとえば、ユーザーが、 clientSourceフィールドが["198.51.100.0"]であるロールと、 clientSourceフィールドが["203.0.113.0"]である別のロールを継承した場合、サーバーはユーザーを認証できません。
MongoDB での認証の詳細については、「自己管理型配置での認証 」を参照してください。
動作
ロールの特権は、ロールが作成されたデータベースに適用されます。 ロールはデータベース内の他のロールから特権を継承することができます。 adminデータベースで作成されたロールには、すべてのデータベースまたはクラスターに適用する特権を含めることができ、他のデータベースのロールから特権を継承できます。
必要なアクセス権
データベースにロールを作成するには、以下が必要です。
組み込みロール userAdmin と userAdminAnyDatabase は、それぞれのリソース上で createRole および grantRole のアクションを提供します。
authenticationRestrictions を指定してロールを作成するには、ロールが作成されるデータベース リソース上のsetAuthenticationRestriction アクションが必要です。
例
The following createRole command creates the myClusterwideAdmin role on the admin database:
db.adminCommand({ createRole: "myClusterwideAdmin", privileges: [ { resource: { cluster: true }, actions: [ "addShard" ] }, { resource: { db: "config", collection: "" }, actions: [ "find", "update", "insert", "remove" ] }, { resource: { db: "users", collection: "usersCollection" }, actions: [ "update", "insert", "remove" ] }, { resource: { db: "", collection: "" }, actions: [ "find" ] } ], roles: [ { role: "read", db: "admin" } ], writeConcern: { w: "majority" , wtimeout: 5000 } })