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.
storage: dbPath: "/var/lib/mongo" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: bindIp: __exec: "python /home/user/getIPAddresses.py" type: "string" trim: "whitespace" digest: 85fed8997aac3f558e779625f2e51b4d142dff11184308dc6aca06cff26ee9ad digest_key: 68656c6c30303030307365637265746d796f6c64667269656e64 tls: mode: requireTLS certificateKeyFile: "/etc/tls/mongod.pem" certificateKeyFilePassword: __rest: "https://myrestserver.example.net/api/config/myCertKeyFilePassword" type: "string" digest: b08519162ba332985ac18204851949611ef73835ec99067b85723e10113f5c26 digest_key: 6d795365637265744b65795374756666
To use expansion directives, you must specify the --configExpand command-line option with the complete list of expansion directives used:
mongod --config "/path/to/config/mongod.conf" --configExpand "rest,exec"
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 returns an error and terminates. You can only specify the --configExpand option on the command line.
Use the __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:
storage: dbPath: "/var/lib/mongo" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: bindIp: 192.51.100.24,127.0.0.1 tls: mode: requireTLS certificateKeyFile: "/etc/tls/mongod.pem" certificateKeyFilePassword: __rest: "https://myrestserver.example.net/api/config/myCertKeyFilePassword" type: "string"
- File Permission
If the configuration file includes the
__restexpansion, on Linux/macOS, the read access to the configuration file must be limited to the user running themongod/mongosprocess only.- Expansion Parsing
To parse the
__restblocks, start themongodwith the--configExpand "rest"option.The
mongodissues aGETrequest against specified URL. If successful, themongodreplaces the value ofcertificateKeyFilePasswordwith the returned value. If the URL fails to resolve or if theRESTendpoint returns an invalid value, themongodthrows 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: "https://myrestserver.example.net/api/config/fullConfig" type: "yaml"
- File Permission
If the configuration file includes the
__restexpansion, on Linux/macOS, the read access to the configuration file must be limited to the user running themongod/mongosprocess only.- Expansion Parsing
To parse the
__restblocks, start themongodwith the--configExpand "rest"option.The
mongodissues aGETrequest against the specified URL. If successful, themongodparses the returned configurationyamlfile and uses it during startup. If the URL fails to resolve or return a properly formattedyamlfile, themongodthrows an error and terminates.
Important
The value returned by the specified REST endpoint cannot include any additional expansion directives. The mongod does not perform additional processing on the returned data and will terminate with an error code if the returned data includes additional expansion directives.
Use the __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:
storage: dbPath: "/var/lib/mongo" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: bindIp: 192.51.100.24,127.0.0.1 tls: mode: requireTLS certificateKeyFile: "/etc/tls/mongod.pem" certificateKeyFilePassword: __exec: "python /home/myUserName/getPEMPassword.py" type: "string"
- File Permission
If the configuration file includes the
__execexpansion, on Linux/macOS, the write access to the configuration file must be limited to the user running themongod/mongosprocess only.- Expansion Parsing
To parse the
__execblocks, start themongodwith the--configExpand "exec"option.The
mongodattempts to execute the specified operation. If the command executes successfully, themongodreplaces the value ofcertificateKeyFilePasswordwith the returned value. If the command fails or returns an invalid value for the configuration file setting, themongodthrows 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: "python /home/myUserName/getFullConfig.py" type: "yaml"
- File Permission
If the configuration file includes the
__execexpansion, on Linux/macOS, the write access to the configuration file must be limited to the user running themongod/mongosprocess only.- Expansion Parsing
To parse the
__execblocks, start themongodwith the--configExpand "rest"option.If the command executes successfully, the
mongodparses the returned configurationyamlfile and uses it during startup. If the command fails or returns an invalidyamlfile, themongodthrows an error and terminates.
Important
The data returned by executing the specified __exec string cannot include any additional expansion directives. The mongod does not perform additional processing on the returned data and will terminate with an error code if the returned data includes additional expansion directives.
Expansion Directives Reference
__restThe
__restexpansion directive loads configuration file values from aRESTendpoint.__restsupports loading specific values in the configuration file or loading the entire configuration file. Themongodthen starts using the externally sourced values as part of its configuration.The
__restexpansion directive has the following syntax:To specify a
RESTendpoint for a specific configuration file setting or settings:<some configuration file setting>: __rest: "<string>" type: "string" trim: "none|whitespace" digest: "<string>" digest_key: "<string>" To specify a
RESTendpoint for the entire configuration file:__rest: "<string>" type: "yaml" trim: "none|whitespace" If specifying the entire configuration file via
RESTendpoint, the expansion directive and its options must be the only values specified in the configuration file.
__resttakes the following fields:FieldTypeDescriptionstring
Required The URL against which the
mongodissues aGETrequest to retrieve the externally sourced value.For non-localhost
RESTendpoints (e.g. aRESTendpoint hosted on a remote server),__restrequires encrypted (https://) URLs where both the host machine and the remote server support TLS 1.1 or later.If the
RESTendpoint specified in the URL requires authentication, encode credentials into the URL with the standard RFC 3986 User Information format.For localhost
RESTendpoints (e.g. aRESTendpoint listening on the host machine),__restallows unencrypted (http://) URLs.IMPORTANT: The value returned by the specified
RESTendpoint cannot include any additional expansion directives. Themongoddoes not perform additional processing on the returned data and will terminate with an error code if the returned data includes additional expansion directives.typestring
trimstring
Optional Specify
whitespaceto direct__restto trim any leading or trailing whitespace, specifically occurrences of" ","\r","\n","\t","\v", and"\f". Defaults tonone, or no trimming.string
Optional. The SHA-256 digest of the expansion result.
If specified, you must also specify the digest_key.
string
Note
To enable parsing of the
__restexpansion directive, start themongodwith the--configExpand "rest"option.
For examples, see Use the
__restExpansion Directive.
__execThe
__execexpansion directive loads configuration file values from the output of a shell or terminal command.__execsupports loading specific values in the configuration file or loading the entire configuration file. Themongodthen starts using the externally sourced values as part of its configuration.The
__execexpansion directive has the following syntax:To specify a shell or terminal command for a specific configuration file setting or settings:
<some configuration file setting>: __exec: "<string>" type: "string" trim: "none|whitespace" To specify a a shell or terminal command for the entire configuration file:
__exec: "<string>" type: "yaml" trim: "none|whitespace" 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.
__exectakes the following fields:FieldTypeDescription__execstring
Required The string which the
mongodexecutes on the terminal or shell to retrieve the externally sourced value.On Linux and OSX hosts, execution is handled via POSIX
popen(). On Windows hosts, execution is handled via the process control API.__execopens a read-only pipe as the same user that started themongodormongos.IMPORTANT: The data returned by executing the specified command cannot include any additional expansion directives. The
mongoddoes not perform additional processing on the returned data and will terminate with an error code if the returned data includes additional expansion directives.typestring
trimstring
Optional Specify
whitespaceto direct__execto trim any leading or trailing whitespace, specifically occurrences of" ","\r","\n","\t","\v", and"\f". Defaults tonone, or no trimming.string
Optional. The SHA-256 digest of the expansion result.
If specified, you must also specify the digest_key
string
Note
To enable parsing of the
__execexpansion directives, start themongodwith the--configExpand "exec"option.
For examples, see Use the
__execExpansion Directive.
Output the Configuration File with Resolved Expansion Directive Values
You can test the final output of a configuration file that specifies one or more expansion directives by starting the mongod with the --outputConfig option. A mongod 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 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:
storage: dbPath: "/var/lib/mongo" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: port: __rest: "https://mongoconf.example.net:8080/record/1" type: string
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:
mongod -f mongod.conf --configExpand rest --outputConfig
The mongod outputs the following to stdout before terminating:
config: mongod.conf storage: dbPath: "/var/lib/mongo" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: port: 20128 outputConfig: true