自己管理型シャーディングされたクラスターをキーファイル認証に更新
Overview
シャーディングされたクラスターにアクセス制御を強制するには、以下を設定する必要があります。
Security between connecting clients and the cluster using 自己管理型配置におけるロールベースのアクセス制御.
このチュートリアルでは、シャーディングされたクラスターの各ノードが同じ内部認証メカニズムと設定を使用する必要があります。 つまり、クラスター内のそれぞれのmongos
とmongod
に内部認証を強制します。
次のチュートリアルでは、キーファイルを使用して内部認証を有効にします。
内部認証を強制すると、ユーザーのアクセス制御も強制されます。 レプリカ セットに接続するには、 mongosh
などのクライアントはユーザー アカウントを使用する必要があります。 詳しくは、 アクセス制御 を参照してください。
Cloud Manager と MongoDB Ops Manager
If Cloud Manager or Ops Manager is managing your deployment, internal authentication is automatically enforced.
To configure Access Control on a
managed deployment, see: Configure Access Control for MongoDB Deployments
in the Cloud Manager manual
or in the Ops Manager manual.
Considerations
重要
IP アドレスの変更による構成の更新を防ぐには、IP アドレスの代わりに DNS ホスト名を使用します。レプリカセット ノードまたはシャーディングされたクラスター ノードを設定するときは、IP アドレスではなく DNS ホスト名を使用することが特に重要です。
分裂されたネットワーク ホライズン全体でクラスターを構成するには、IP アドレスの代わりにホスト名を使用します。 MongoDB 5.0以降、IP アドレスのみが設定されているノードは起動時の検証に失敗し、起動しません。
IP バインディング
オペレーティング システム
このチュートリアルでは、主に mongod
プロセスについて説明します。Windows ユーザーは代わりに mongod.exe
プログラムを使用する必要があります。
キーファイルのセキュリティ
キーファイルは最低限のセキュリティであり、テストや開発環境に最適です。本番環境では、X.509 証明書を使用することをお勧めします。
アクセス制御
このチュートリアルでは、admin
データベース上のみでの最小限数の管理ユーザーの作成について説明します。ユーザー認証には、このチュートリアルではデフォルトのSCRAM認証メカニズムを使用します。チャレンジレスポンスのセキュリティ方式は、テスト環境または開発環境に最適です。本番環境では、 X.509 証明書 または 自己管理型 LDAP プロキシ認証 ( MongoDB Enterpriseでのみ使用可能)、または 自己管理型配置での Kerberos 認証 ( MongoDB Enterpriseでのみ使用可能)を使用することをお勧めします。
特定の認証メカニズムのユーザーを作成する方法の詳細については、特定の認証メカニズムのページを参照してください。
ユーザーの作成とマネジメントのベストプラクティスについては、➤ ロールベースのアクセス制御の構成を参照してください。
ユーザー
一般に、シャーディングされたクラスターのユーザーを作成するには、 mongos
に接続してシャーディングされたクラスター ユーザーを追加します。
ただし、一部のメンテナンス操作では、シャーディングされたクラスター内の特定のシャードへの直接接続が必要です。 これらの操作を実行するには、シャードに直接接続し、 シャード ローカル 管理ユーザーとして認証する必要があります。
シャード ローカル ユーザーは特定のシャードにのみ存在するため、シャード固有のメンテナンスと構成にのみ使用する必要があります。 シャード ローカル ユーザーはmongos
に接続できません。
詳細については、「自己管理型配置のユーザー」セキュリティ ドキュメントを参照してください。
ダウンタイム
Upgrading a sharded cluster to enforce access control requires downtime.
始める前に
MongoDB 8.0以降では、 directShardOperations
ロールを使用して、シャードに対してコマンドを直接実行する必要があるメンテナンス操作を実行できます。
警告
directShardOperations
ロールを使用して コマンドを実行すると、クラスターが正しく動作しなくなり、データが破損する可能性があります。 directShardOperations
ロールは、メンテナンス目的で、または MongoDB サポートのガイダンスに必ず従う必要があります。 メンテナンス操作を実行したら、 directShardOperations
ロールの使用を停止します。
手順
Enforce Keyfile Internal Authentication on Existing Sharded Cluster Deployment
キーファイルを作成します。
キーファイル 認証では、シャーディングされたクラスター内の各mongod
またはmongos
インスタンスは、配置内の他のノードを認証するための共有パスワードとしてキーファイルの内容を使用します。 正しいキーファイルを持つmongod
またはmongos
インスタンスのみがシャーディングされたクラスターに参加できます。
注意
内部メンバーシップ認証用のキーファイルでは、キーファイル内に複数のキーを含めるために YAML 形式が使用されます。YAML 形式は次のいずれかを受け入れます。
1 つのキー文字列(以前のバージョンと同じ)
キー文字列のシーケンス
YAML 形式は、テキストファイル形式を使用する既存の単一のキー キーファイルと互換性があります。
キーの長さは 6 文字から 1024 文字の間で、base64 セット内の文字のみを含めることができます。 シャーディングされたクラスターのすべてのノードは、少なくとも 1 つの共通キーを共有する必要があります。
注意
UNIX システムでは、キーファイルにグループ権限またはワールド権限があってはなりません。Windows システムでは、キーファイルの権限はチェックされません。
キーファイルは、選択した任意の方法で生成できます。たとえば、次の操作では、openssl
を使用して、共有パスワードとして使用する疑似ランダムの複雑な 1024 文字の文字列を生成します。次に、chmod
を使用してファイル権限を変更し、ファイル所有者のみに読み取り権限を付与します。
openssl rand -base64 756 > <path-to-keyfile> chmod 400 <path-to-keyfile>
キーファイルの使用に関する詳細と要件については、「キーファイル」を参照してください。
Copy the keyfile to each component in the sharded cluster.
Every server hosting a mongod
or mongos
component
of the sharded cluster must contain a copy of the keyfile.
シャーディングされたクラスターをホストしている各サーバーにキー ファイルをコピーします。 mongod
またはmongos
インスタンスを実行中のユーザーがファイルの所有者であり、キーファイルにアクセスできるようにしてください。
キーファイルを、 mongod
またはmongos
インスタンスをホストしているハードウェアから簡単に切断できるストレージメディア(Windows ドライブやネットワーク接続ストレージデバイスなど)にキーファイルを保存しないでください。
バランサーを無効にします。
sh.stopBalancer()
The balancer may not stop immediately if a migration is in progress.
The sh.stopBalancer()
method blocks the shell until the
balancer stops.
MongoDB 6.0.3以降、 自動チャンク分割は実行されません。 これはバランシング ポリシーの改善によるものです。 自動分割コマンドは引き続き存在しますが、操作は実行されません。
MongoDB バージョン 6.0.3 より前では、sh.stopBalancer()
によってシャーディングされたクラスターの自動分割も無効になります。
Use sh.getBalancerState()
to verify that the balancer has
stopped.
sh.getBalancerState()
重要
Do not proceed until the balancer has stopped running.
See シャーディングされたクラスター バランサーの管理 for tutorials on configuring sharded cluster balancer behavior.
Shut down all mongos
instances for the sharded cluster.
接続 mongosh
to each mongos
and shut
them down.
Use the db.shutdownServer()
method on the admin
database
to safely shut down the mongos
:
db.getSiblingDB("admin").shutdownServer()
Repeat until all mongos
instances in the cluster
are offline.
Once this step is complete, all mongos
instances in the cluster
should be offline.
Shut down config server mongod
instances.
接続 mongosh
to each mongod
in the
config server deployment and shut them down.
For replica set config server deployments, shut down the primary member last.
Use the db.shutdownServer()
method on the admin
database
to safely shut down the mongod
:
db.getSiblingDB("admin").shutdownServer()
Repeat until all config servers are offline.
Shut down shard replica set mongod
instances.
For each shard replica set, connect mongosh
to each
mongod
member in the replica set and shut them down. Shut down
the primary member last.
Use the db.shutdownServer()
method on the admin
database
to safely shut down the mongod
:
db.getSiblingDB("admin").shutdownServer()
Repeat this step for each shard replica set until all mongod
instances in all shard replica sets are offline.
Once this step is complete, the entire sharded cluster should be offline.
Enforce Access Control on the Config Servers.
始める each mongod
in the config server replica set.
Include the keyFile
setting. The keyFile
setting enforces
both 自己管理型内部認証とメンバーシップ認証 and
自己管理型配置におけるロールベースのアクセス制御.
mongod
設定は、構成ファイルまたはコマンドラインで指定できます。
構成ファイル
If using a 構成ファイル, for a config server replica
set, set security.keyFile
to the keyfile's path,
sharding.clusterRole
to configsvr
, and
replication.replSetName
to the name of the config
server replica set.
security: keyFile: <path-to-keyfile> sharding: clusterRole: configsvr replication: replSetName: <setname> storage: dbpath: <path>
構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを使用して配置に接続する場合や、配置ノードを異なるホスト上で実行する場合は、 net.bindIp
設定を指定します。
--config
オプションと構成ファイルへのパスを指定して、
mongod
を起動します。
mongod --config <path-to-config>
コマンドライン
If using the command line parameters, for a config server replica
set, start the mongod
with the -keyFile
,
--configsvr
, and --replSet
parameters.
mongod --keyFile <path-to-keyfile> --configsvr --replSet <setname> --dbpath <path>
構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを配置に接続する場合、または配置ノードを異なるホスト上で実行する場合は、 --bind_ip
を指定します。
For more information on command line options, see the
mongod
reference page.
Make sure to use the original replica set name when restarting each member. You cannot change the name of a replica set.
Enforce Access Control for each Shard in the Sharded Cluster.
keyFile
パラメータを使用して
mongod
を実行すると、自己管理型配置で 自己管理型内部認証とメンバーシップ認証とロールベースのアクセス制御 の両方が強制されます。
構成ファイルまたはコマンドラインのいずれかを使用して、レプリカセット内の各mongod
を起動します。
構成ファイル
If using a 構成ファイル, set the
security.keyFile
option to the keyfile's path and the
replication.replSetName
option to the original name
of the replica set.
security: keyFile: <path-to-keyfile> replication: replSetName: <setname> storage: dbPath: <path>
構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを使用して配置に接続する場合や、配置ノードを異なるホスト上で実行する場合は、 net.bindIp
設定を指定します。
--config
オプションと構成ファイルへのパスを指定して、
mongod
を起動します。
mongod --config <path-to-config-file>
コマンドライン
If using the command line parameters, start the mongod
and
specify the --keyFile
and --replSet
parameters.
mongod --keyfile <path-to-keyfile> --replSet <setname> --dbpath <path>
構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを配置に接続する場合、または配置ノードを異なるホスト上で実行する場合は、 --bind_ip
を指定します。
For more information on startup parameters,
see the mongod
reference page.
Make sure to use the original replica set name when restarting each member. You cannot change the name of a replica set.
Repeat this step until all shards in the cluster are online.
Create a Shard-Local User Administrator (Optional).
重要
The 自己管理型配置における Localhost 例外 allows clients connected over the
localhost interface to create users on a mongod
enforcing access control. After creating the first user,
the 自己管理型配置における Localhost 例外 closes.
最初のユーザーには、 userAdminAnyDatabase
を持つユーザーなど、他のユーザーを作成する特権を付与する必要があります。 これにより、自己管理型配置の Localhost 例外 が終了した後に追加のユーザーを作成できるようになります。
If at least one user does not have privileges to create users, once the localhost exception closes you may be unable to create or modify users with new privileges, and therefore unable to access certain functions or operations.
For each shard replica set in the cluster, connect
mongosh
to the primary member over the
localhost interface. You must run
mongosh
on the same machine as the target
mongod
to use the localhost interface.
Create a user with the userAdminAnyDatabase
role on the admin
database. This user can create
additional users for the shard replica set as necessary.
Creating this user also closes the 自己管理型配置における Localhost 例外.
The following example creates the shard-local user fred
on the
admin
database.
重要
パスワードは、システムのセキュリティを確保し、悪意のあるアクセスを防止または遅延させるために、ランダムで長く、複雑なものにする必要があります。
Tip
passwordPrompt()
メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo
シェルと同様に、パスワードを直接指定することもできます。
admin = db.getSiblingDB("admin") admin.createUser( { user: "fred", pwd: passwordPrompt(), // or cleartext password roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
Enforce Access Control for the mongos
servers.
keyFile
パラメータを使用して
mongod
を実行すると、自己管理型配置で 自己管理型内部認証とメンバーシップ認証とロールベースのアクセス制御 の両方が強制されます。
構成ファイルまたはコマンドラインのいずれかを使用して、レプリカセット内の各mongos
を起動します。
構成ファイル
If using a 構成ファイル, set the
security.keyFile
to the keyfile`s path and the
sharding.configDB
to the replica set name and at least
one member of the replica set in <replSetName>/<host:port>
format.
security: keyFile: <path-to-keyfile> sharding: configDB: <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,...
構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを使用して配置に接続する場合や、配置ノードを異なるホスト上で実行する場合は、 net.bindIp
設定を指定します。
--config
オプションと構成ファイルへのパスを指定して、
mongos
を起動します。
mongos --config <path-to-config-file>
コマンドライン
コマンドラインmongos
--keyFile
--configdb
パラメータを使用する場合は、 を起動し、 パラメータと パラメータを指定します。
mongos --keyFile <path-to-keyfile> --configdb <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,...
構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを配置に接続する場合、または配置ノードを異なるホスト上で実行する場合は、 --bind_ip
を指定します。
At this point, the entire sharded cluster is back online and can
communicate internally using the keyfile specified. However, external
programs like mongosh
need to use a correctly
provisioned user in order to read or write to the cluster.
Connect to the mongos
instance over the localhost interface.
mongosh
をローカルホスト インターフェース経由で
mongos
インスタンスの 1 つに接続します。mongosh
は mongos
インスタンスと同じ物理マシン上で実行する必要があります。
配置用のユーザーが作成されていないため、ローカルホスト インターフェースのみ使用できます。最初のユーザーが作成された後に、 ローカルホスト インターフェースは閉じます。
ユーザー管理者を作成します。
重要
最初のユーザーを作成すると、localhost 例外は利用できなくなります。
最初のユーザーには、 userAdminAnyDatabase
を持つユーザーなど、他のユーザーを作成する特権を付与する必要があります。 これにより、自己管理型配置の Localhost 例外 が終了した後に追加のユーザーを作成できるようになります。
少なくとも 1 人ユーザーを作成できる権限を持つユーザーが いない場合、localhost 例外が終了するとユーザーの作成や変更ができなくなり、必要な操作が実行できなくなる可能性があります。
db.createUser()
メソッドを使用してユーザーを追加します。このユーザーには、admin
データベース上で
userAdminAnyDatabase
以上のロールを付与する必要があります。
重要
パスワードは、システムのセキュリティを確保し、悪意のあるアクセスを防止または遅延させるために、ランダムで長く、複雑なものにする必要があります。
次の例では、 admin
データベースにユーザーfred
を作成しています。
Tip
passwordPrompt()
メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo
シェルと同様に、パスワードを直接指定することもできます。
admin = db.getSiblingDB("admin") admin.createUser( { user: "fred", pwd: passwordPrompt(), // or cleartext password roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
データベース管理操作に関連する組み込みロールの全リストは、「データベースユーザー ロール」を参照してください。
ユーザー管理者として認証します。
ユーザー管理者として認証し、追加のユーザーを作成するには、次のようにしdb.auth()
。
Tip
passwordPrompt()
メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo
シェルと同様に、パスワードを直接指定することもできます。
db.getSiblingDB("admin").auth("fred", passwordPrompt()) // or cleartext password
パスワードの入力を求められたら、入力します。
または、 -u <username>
、
-p <password>
、 --authenticationDatabase "admin"
パラメータを使用して、新しいmongosh
セッションを対象レプリカセット ノードに接続します。 に接続するには 、 自己管理型配置で Localhost 例外 を
mongos
使用する必要があります。
mongosh -u "fred" -p --authenticationDatabase "admin"
クラスターマネジメントの管理ユーザーの作成
The cluster administrator user has the clusterAdmin
role
for the sharded cluster and not the shard-local cluster
administrator.
次の例では、 admin
データベースにユーザーravi
を作成しています。
重要
パスワードは、システムのセキュリティを確保し、悪意のあるアクセスを防止または遅延させるために、ランダムで長く、複雑なものにする必要があります。
Tip
passwordPrompt()
メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo
シェルと同様に、パスワードを直接指定することもできます。
db.getSiblingDB("admin").createUser( { "user" : "ravi", "pwd" : passwordPrompt(), // or cleartext password roles: [ { "role" : "clusterAdmin", "db" : "admin" } ] } )
レプリカセットとシャーディングされたクラスターの操作に関連する組み込みロールの完全なリストについては、「クラスター管理ロール」を参照してください。
Authenticate as cluster admin.
To perform sharding operations, authenticate as a
clusterAdmin
user with either the
db.auth()
method or a new mongosh
session
with the username
, password
, and authenticationDatabase
parameters.
注意
これは、シャード クラスターのクラスター管理者であり、シャード ローカル クラスター管理者ではありません。
Start the balancer.
Start the balancer.
sh.startBalancer()
MongoDB 6.0.3以降、 自動チャンク分割は実行されません。 これはバランシング ポリシーの改善によるものです。 自動分割コマンドは引き続き存在しますが、操作は実行されません。
MongoDB バージョン 6.0.3 より前では、sh.startBalancer()
によってシャーディングされたクラスターの自動分割も有効になります。
Use the sh.getBalancerState()
to verify the balancer has started.
See シャーディングされたクラスター バランサーの管理 for tutorials on the sharded cluster balancer.
追加のユーザーを作成します(任意)。
ユーザーを作成して、クライアントがシャーディングされたクラスターに接続してアクセスできるようにします。 使用可能な組み込みロールについては、「 データベースユーザーのロール 」を参照してください。たとえば、 read
やreadWrite
などです。 また、管理ユーザーを追加することもできます。 ユーザーの詳細については、「自己管理型配置のユーザー 」を参照してください。
追加のユーザーを作成するには、 userAdminAnyDatabase
またはuserAdmin
ロールを持つユーザーとして認証する必要があります。
X.509 内部認証
X.509 を使用した内部認証の詳細については、「自己管理型MongoDBによるメンバーシップ認証に X.509 証明書を使用」を参照してください。
鍵ファイルによる内部認証から X.509 内部認証にアップグレードするには、「 自己管理型MongoDB を鍵ファイル認証から X. 認証にアップグレードする509 」を参照してください。
以下も参照してください。