|
4 | 4 | Secure Your Data
|
5 | 5 | ================
|
6 | 6 |
|
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: ldap, authorize, ecs, aws, authenticate |
| 19 | + :description: Learn how to use the PHP library to secure your data. |
| 20 | + |
7 | 21 | .. toctree::
|
8 | 22 | :titlesonly:
|
9 | 23 | :maxdepth: 1
|
10 | 24 |
|
11 | 25 | /security/authentication
|
12 | 26 | /security/in-use-encryption
|
| 27 | + |
| 28 | +Overview |
| 29 | +-------- |
| 30 | + |
| 31 | +MongoDB supports multiple mechanisms that you can use to authenticate your application. |
| 32 | +This page contains code examples that demonstrate each of these mechanisms. |
| 33 | + |
| 34 | +.. tip:: |
| 35 | + |
| 36 | + To learn more about any of the mechanisms shown on this page, see the link |
| 37 | + provided in each section. |
| 38 | + |
| 39 | +To use an authentication example from this page, copy the code example into the |
| 40 | +:ref:`sample application <php-auth-sample>` or your own application. |
| 41 | +Make sure to replace all placeholders in the code examples, such as ``<hostname>``, with |
| 42 | +the relevant values for your MongoDB deployment. |
| 43 | + |
| 44 | +.. _php-auth-sample: |
| 45 | + |
| 46 | +.. include:: /includes/usage-examples/sample-app-intro.rst |
| 47 | + |
| 48 | +.. literalinclude:: /includes/usage-examples/connect-sample-app.php |
| 49 | + :language: php |
| 50 | + :copyable: true |
| 51 | + :linenos: |
| 52 | + :emphasize-lines: 5-7 |
| 53 | + |
| 54 | +SCRAM-SHA-256 |
| 55 | +------------- |
| 56 | + |
| 57 | +The following code shows how to authenticate by using the ``SCRAM-SHA-256`` |
| 58 | +authentication mechanism: |
| 59 | + |
| 60 | +.. tabs:: |
| 61 | + |
| 62 | + .. tab:: MongoDB\\Client |
| 63 | + :tabid: Client |
| 64 | + |
| 65 | + .. literalinclude:: /includes/authentication.php |
| 66 | + :language: php |
| 67 | + :dedent: |
| 68 | + :start-after: start-scram-sha-256-client |
| 69 | + :end-before: end-scram-sha-256-client |
| 70 | + |
| 71 | + .. tab:: Connection URI |
| 72 | + :tabid: connectionstring |
| 73 | + |
| 74 | + .. literalinclude:: /includes/authentication.php |
| 75 | + :language: php |
| 76 | + :dedent: |
| 77 | + :start-after: start-scram-sha-256-uri |
| 78 | + :end-before: end-scram-sha-256-uri |
| 79 | + |
| 80 | +To learn more about SCRAM-SHA-256 authentication, see :ref:`php-scram-sha-256` in |
| 81 | +the Authentication guide. |
| 82 | + |
| 83 | +SCRAM-SHA-1 |
| 84 | +----------- |
| 85 | + |
| 86 | +The following code shows how to authenticate by using the ``SCRAM-SHA-1`` |
| 87 | +authentication mechanism: |
| 88 | + |
| 89 | +.. tabs:: |
| 90 | + |
| 91 | + .. tab:: MongoDB\\Client |
| 92 | + :tabid: Client |
| 93 | + |
| 94 | + .. literalinclude:: /includes/authentication.php |
| 95 | + :language: php |
| 96 | + :dedent: |
| 97 | + :start-after: start-scram-sha-1-client |
| 98 | + :end-before: end-scram-sha-1-client |
| 99 | + |
| 100 | + .. tab:: Connection URI |
| 101 | + :tabid: connectionstring |
| 102 | + |
| 103 | + .. literalinclude:: /includes/authentication.php |
| 104 | + :language: php |
| 105 | + :dedent: |
| 106 | + :start-after: start-scram-sha-1-uri |
| 107 | + :end-before: end-scram-sha-1-uri |
| 108 | + |
| 109 | +To learn more about SCRAM-SHA-1 authentication, see :ref:`php-scram-sha-1` in |
| 110 | +the Authentication guide. |
| 111 | + |
| 112 | +MONGODB X.509 |
| 113 | +------------- |
| 114 | + |
| 115 | +The following code shows how to create a connection URI to authenticate by using |
| 116 | +the ``X.509`` authentication mechanism: |
| 117 | + |
| 118 | +.. tabs:: |
| 119 | + |
| 120 | + .. tab:: MongoDB\\Client |
| 121 | + :tabid: Client |
| 122 | + |
| 123 | + .. literalinclude:: /includes/authentication.php |
| 124 | + :language: php |
| 125 | + :dedent: |
| 126 | + :start-after: start-mongodb-X509-client |
| 127 | + :end-before: end-mongodb-X509-client |
| 128 | + |
| 129 | + .. tab:: Connection URI |
| 130 | + :tabid: connectionstring |
| 131 | + |
| 132 | + .. literalinclude:: /includes/authentication.php |
| 133 | + :language: php |
| 134 | + :dedent: |
| 135 | + :start-after: start-mongodb-X509-uri |
| 136 | + :end-before: end-mongodb-X509-uri |
| 137 | + |
| 138 | +To learn more about X.509 authentication, see :ref:`php-x509` in |
| 139 | +the Authentication guide. |
| 140 | + |
| 141 | +MONGODB-AWS |
| 142 | +----------- |
| 143 | + |
| 144 | +The following sections show how to connect to MongoDB by using the ``MONGODB-AWS`` |
| 145 | +authentication mechanism. When you use the ``MONGODB-AWS`` mechanism, the {+php-library+} |
| 146 | +attempts to retrieve your AWS credentials from the following sources, in the order listed: |
| 147 | + |
| 148 | +1. Options passed to the ``MongoDB\Client`` constructor, either as part of the connection |
| 149 | + string or the ``$uriOptions`` array parameter |
| 150 | +#. Environment variables |
| 151 | +#. AWS EKS ``AssumeRoleWithWebIdentity`` request |
| 152 | +#. ECS container metadata |
| 153 | +#. EC2 instance metadata |
| 154 | + |
| 155 | +Each section shows how to authenticate with ``MONGODB-AWS`` when retrieving your |
| 156 | +AWS credentials from options passed to your client or the alternative external sources. |
| 157 | + |
| 158 | +To learn more about authenticating with AWS, see :ref:`php-mongo-aws` in the |
| 159 | +Authentication guide. |
| 160 | + |
| 161 | +MongoDB\\Client Credentials |
| 162 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 163 | + |
| 164 | +The following code shows how to pass AWS credentials to the ``MongoDB\Client`` constructor |
| 165 | +to authenticate with ``MONGODB-AWS``: |
| 166 | + |
| 167 | +.. tabs:: |
| 168 | + |
| 169 | + .. tab:: MongoDB\\Client |
| 170 | + :tabid: Client |
| 171 | + |
| 172 | + .. literalinclude:: /includes/authentication.php |
| 173 | + :language: php |
| 174 | + :dedent: |
| 175 | + :start-after: start-mongodb-aws-client |
| 176 | + :end-before: end-mongodb-aws-client |
| 177 | + |
| 178 | + .. tab:: Connection URI |
| 179 | + :tabid: connectionstring |
| 180 | + |
| 181 | + .. literalinclude:: /includes/authentication.php |
| 182 | + :language: php |
| 183 | + :dedent: |
| 184 | + :start-after: start-mongodb-aws-uri |
| 185 | + :end-before: end-mongodb-aws-uri |
| 186 | + |
| 187 | +External Credentials |
| 188 | +~~~~~~~~~~~~~~~~~~~~ |
| 189 | + |
| 190 | +The following code shows how to authenticate with ``MONGODB-AWS`` when |
| 191 | +obtaining credentials from environment variables, an ``AssumeRoleWithWebIdentity`` |
| 192 | +request, ECS metadata, or EC2 instance metadata: |
| 193 | + |
| 194 | +.. tabs:: |
| 195 | + |
| 196 | + .. tab:: MongoDB\\Client |
| 197 | + :tabid: Client |
| 198 | + |
| 199 | + .. literalinclude:: /includes/authentication.php |
| 200 | + :language: php |
| 201 | + :dedent: |
| 202 | + :start-after: start-mongodb-aws-env-client |
| 203 | + :end-before: end-mongodb-aws-env-client |
| 204 | + |
| 205 | + .. tab:: Connection URI |
| 206 | + :tabid: connectionstring |
| 207 | + |
| 208 | + .. literalinclude:: /includes/authentication.php |
| 209 | + :language: php |
| 210 | + :dedent: |
| 211 | + :start-after: start-mongodb-aws-env-uri |
| 212 | + :end-before: end-mongodb-aws-env-uri |
| 213 | + |
| 214 | +To learn more about authenticating with AWS by obtaining external |
| 215 | +credentials, see the following sections in the Authentication guide: |
| 216 | + |
| 217 | +- :ref:`php-mongo-aws-environment` |
| 218 | +- :ref:`php-mongo-aws-assume-role` |
| 219 | +- :ref:`php-mongo-aws-ecs` |
| 220 | +- :ref:`php-mongo-aws-ec2` |
0 commit comments