On this page本页内容
New in version 4.2.版本4.2中的新功能。
MongoDB supports using expansion directives in configuration files to load externally sourced values. Expansion directives can load values for specific configuration file options or load the entire configuration file. Expansion directives help obscure confidential information like security certificates and passwords.
__rest
expansion, on Linux/macOS, the read access to the configuration file must be limited to the user running the mongod
/mongos
process only.__exec
expansion, on Linux/macOS, the write access to the configuration file must be limited to the user running the mongod
/mongos
process only.To use expansion directives, you must specify the --configExpand
command-line option with the complete list of expansion directives used:
If you omit the --configExpand
option or if you do not specify the complete list of expansion directives used in the configuration file, the mongod
/mongos
returns an error and terminates. You can only specify the --configExpand
option on the command line.
__rest
Expansion Directive¶The __rest
expansion directive loads configuration file values from a REST
endpoint. __rest
supports loading specific values in the configuration file or loading the entire configuration file.
The following configuration file uses the __rest
expansion directive to load the setting net.tls.certificateKeyFilePassword
value from an external REST
endpoint:
__rest
expansion, on Linux/macOS, the read access to the configuration file must be limited to the user running the mongod
/mongos
process only.To parse the __rest
blocks, start the mongod
/mongos
with the --configExpand "rest"
option.
The mongod
/mongos
issues a GET
request against specified URL. If successful, the mongod
/mongos
replaces the value of certificateKeyFilePassword
with the returned value. If the URL fails to resolve or if the REST
endpoint returns an invalid value, the mongod
/mongos
throws an error and terminates.
The following configuration file uses the __rest
expansion directive to load the configuration file from an external REST
endpoint. The expansion directive and its options must be the only values specified in the configuration file.
__rest
expansion, on Linux/macOS, the read access to the configuration file must be limited to the user running the mongod
/mongos
process only.To parse the __rest
blocks, start the mongod
/mongos
with the --configExpand "rest"
option.
The mongod
/mongos
issues a GET
request against the specified URL. If successful, the mongod
/mongos
parses the returned configuration yaml
file and uses it during startup. If the URL fails to resolve or return a properly formatted yaml
file, the mongod
/mongos
throws an error and terminates.
__exec
Expansion Directive¶The __exec
expansion directive loads configuration file values from a shell or terminal command. __exec
supports loading specific values in the configuration file or loading the entire configuration file.
The following example configuration file uses the __exec
expansion directive to to load the setting net.tls.certificateKeyFilePassword
value from the output of a shell or terminal command:
__exec
expansion, on Linux/macOS, the write access to the configuration file must be limited to the user running the mongod
/mongos
process only.To parse the __exec
blocks, start the mongod
/mongos
with the --configExpand "exec"
option.
The mongod
/mongos
attempts to execute the specified operation. If the command executes successfully, the mongod
/mongos
replaces the value of certificateKeyFilePassword
with the returned value. If the command fails or returns an invalid value for the configuration file setting, the mongod
/mongos
throws an error and terminates.
The following example configuration file uses the __exec
expansion directive to load the configuration file from the output of a shell or terminal command. The __exec
expansion directive and its options must be the only values specified in the configuration file.
__exec
expansion, on Linux/macOS, the write access to the configuration file must be limited to the user running the mongod
/mongos
process only.To parse the __exec
blocks, start the mongod
/mongos
with the --configExpand "rest"
option.
If the command executes successfully, the mongod
/mongos
parses the returned configuration yaml
file and uses it during startup. If the command fails or returns an invalid yaml
file, the mongod
/mongos
throws an error and terminates.
__rest
¶The __rest
expansion directive loads configuration file values from a REST
endpoint. __rest
supports loading specific values in the configuration file or
loading the entire configuration file. The mongod
/mongos
then starts using the externally sourced values as part of its configuration.
The __rest
expansion directive has the following syntax:语法如下所示:
REST
endpoint for a specific configuration file setting or settings:
REST
endpoint for the entire configuration file:
If specifying the entire configuration file via REST
endpoint, the expansion directive and its options must be the only values specified in the configuration file.
__rest
takes the following fields:
__rest | string | Required The URL against which the For non-localhost If the For localhost |
type |
string | Optional Controls how Possible values are:
|
trim |
string | Optional Specify whitespace to direct __rest to trim any leading or trailing whitespace, specifically occurrences of " " , "\r" , "\n" , "\t" , "\v" , and "\f" . Defaults to none , or no trimming. |
digest | string | Optional. The SHA-256 digest of the expansion result. If specified, you must also specify the digest_key. |
digest_key | string | Optional. The hexadecimal string representation of the secret used to calculate the SHA-256 digest. If specified, you must also specify the digest. |
Note
For examples, see Use the __rest Expansion Directive.
__exec
¶The __exec
expansion directive loads configuration file values from the output of a shell or terminal command. __exec
supports loading specific values in the configuration file or loading the entire configuration file. The mongod
/mongos
then starts using the externally sourced values as part of its configuration.
The __exec
expansion directive has the following syntax:语法如下所示:
If specifying the entire configuration file via a terminal or shell command, the expansion directive and its options must be the only values specified in the configuration file.
__exec
takes the following fields:
__exec |
string | Required The string which the On Linux and OSX hosts, execution is handled via POSIX |
type |
string | Optional Controls how Possible values are:
|
trim |
string | Optional Specify whitespace to direct __exec to trim any leading or trailing whitespace, specifically occurrences of " " , "\r" , "\n" , "\t" , "\v" , and "\f" . Defaults to none , or no trimming. |
digest | string | Optional. The SHA-256 digest of the expansion result. If specified, you must also specify the digest_key |
digest_key | string | Optional. The hexadecimal string representation of the secret used to calculate the SHA-256 digest. If specified, you must also specify the digest |
Note
__exec
expansion, on Linux/macOS, the write access to the configuration file must be limited to the user running the mongod
/mongos
process only.__exec
expansion directives, start the mongod
/mongos
with the --configExpand "exec"
option.For examples, see Use the __exec Expansion Directive.
You can test the final output of a configuration file that specifies one or more expansion directives by starting the mongod
/mongos
with the --outputConfig
option. A mongod
/mongos
started with --outputConfig
outputs the resolved YAML configuration document to stdout
and halts. If any expansion directive specified in the configuration file returns additional expansion directives, the mongod
/mongos
throws an error and terminates.
Warning
The --outputConfig
option returns the resolved values for any field using an expansion directive. This includes any private or sensitive information previously obscured by using an external source for the configuration option.
For example, the following configuration file mongod.conf
contains a __rest
expansion directive:
The string recorded at the specified URL is 20128
If the configuration file includes the __rest
expansion, on Linux/macOS, the read access to the configuration file must be limited to the user running the mongod
/mongos
process only.
Start the mongod
with the --configExpand "rest"
and --outputConfig
options:
The mongod
outputs the following to stdout
before terminating: