@@ -5,64 +5,77 @@ sidebar_position: 2
5
5
6
6
# 👐 RUN : Authentication challenge
7
7
8
- > Hint! Remember to add the ` --projectId {project_id} `
8
+ :::info
9
+ The provided scripts are incomplete. Replace all ` <CODE_BLOCK> ` with the correct code to complete the lab.
10
+ :::
9
11
10
- > Docs : atlas [ dbusers ] ( https://www.mongodb.com/docs/atlas/cli/current/command/atlas-dbusers-create/ )
12
+ > Hint: Remember to add ` --projectId {project_id} `
11
13
12
14
13
- ### 1. Let's create a SCRAM user: 'myUser' with User/Password authentication and assign it the 'readWriteAnyDatabase' role.
15
+ ### 1. Create a SCRAM user
14
16
15
17
``` python
16
- # CODE_BLOCK_7
17
- ! atlas dbusers create < CODE_BLOCK_7 >
18
+ # create a SCRAM user with username: "myUser", password: "mySecurePassword" and role: "readWriteAnyDatabase"
19
+ username = " myUser"
20
+ password = " mySecurePassword"
21
+ ! atlas dbusers create < CODE_BLOCK >
18
22
```
23
+ > Refer to documentations: [ atlas dbusers] ( https://www.mongodb.com/docs/atlas/cli/current/command/atlas-dbusers-create/ )
19
24
20
25
:::tip
21
26
<details >
22
27
<summary > Answer </summary >
28
+ ```python
29
+ # create a SCRAM user with username: "myUser", password: "mySecurePassword" and role: "readWriteAnyDatabase"
30
+ username = "myUser"
31
+ password = "mySecurePassword"
32
+ !atlas dbusers create --username { username } --password { password } --role readWriteAnyDatabase --projectId { project_id }
23
33
```
24
- !atlas dbusers create --username myUser --password mySecurePassword --role readWriteAnyDatabase --projectId { project_id }
25
- ```
26
34
</details >
27
35
:::
28
36
29
37
### 2. Lets test our SCRAM user successful creation by performing the authentication process
38
+
30
39
``` python
31
- # CODE_BLOCK_8
32
40
! pip install pymongo dnspython
33
41
```
42
+
34
43
``` python
35
- # CODE_BLOCK_9
44
+ # retrieve connection string
36
45
connection = ! atlas clusters connectionStrings describe MyNewCluster -- projectId {project_id}
37
46
38
- username = ' myUser'
39
- password = ' mySecurePassword'
40
-
47
+ # add username and password to connection string
41
48
new_connection = connection[1 ].replace(' mongodb+srv://' , f ' mongodb+srv:// { username} : { password} @ ' )
42
49
print (new_connection)
43
50
51
+ # make the connection get the list of databases
44
52
from pymongo import MongoClient
45
53
client = MongoClient(new_connection)
46
-
47
54
client.list_database_names()
48
55
```
49
56
50
- ### 3. Let's create X509 user: 'myX509User' with User/Password authentication and assign it the 'readWriteAnyDatabase' role.
57
+ ### 3. Create a X509 user and certificate
51
58
52
59
``` python
53
- # CODE_BLOCK_10
54
- ! atlas dbusers create ...
55
-
56
- ! atlas dbusers certs create < CODE_BLOCK_10 > > / tmp/ cert.pem
60
+ # create a Atlas-managed X509 user with username: "myX509User" and role: "readAnyDatabase"
61
+ ! atlas dbusers create < CODE_BLOCK >
62
+ ```
63
+ > Refer to documentations: [ atlas dbusers] ( https://www.mongodb.com/docs/atlas/cli/current/command/atlas-dbusers-create/ )
64
+ ``` python
65
+ # Generate a certification for "myX509user", set monthsUntilExpiration to 1, and save it to /tmp/cert.pem
66
+ ! atlas dbusers certs create < CODE_BLOCK > > / tmp/ cert.pem
57
67
```
68
+ > Refer to documentations: [ atlas dbusers certs] ( https://www.mongodb.com/docs/atlas/cli/current/command/atlas-dbusers-certs-create/ )
58
69
59
70
:::tip
60
71
<details >
61
72
<summary > Answer </summary >
73
+ ```python
74
+ # create a Atlas-managed X509 user with username: "myX509User" and role: "readAnyDatabase"
75
+ !atlas dbusers create --username myX509User --role readAnyDatabase --x509Type MANAGED --projectId { project_id }
62
76
```
63
- # Generate an X509 certificate for a new user
64
- !atlas dbusers create --username myX509User --x509Type MANAGED --role readAnyDatabase --projectId { project_id }
65
- # Generate and save the certificate
77
+ ```python
78
+ # Generate a certification for "myX509user", set monthsUntilExpiration to 1, and save it to /tmp/cert.pem
66
79
!atlas dbusers certs create --username myX509User --monthsUntilExpiration 1 --projectId { project_id } > /tmp/cert.pem
67
80
```
68
81
</details >
@@ -71,9 +84,10 @@ client.list_database_names()
71
84
### 4. Let's test our X509 User
72
85
73
86
``` python
74
- # CODE_BLOCK_11
75
- username= ' myX509User'
87
+ # Get connection string
76
88
connection = ! atlas clusters connectionStrings describe MyNewCluster -- projectId {project_id}
89
+
90
+ # Modify connection string to use X509 as authentication mechanism
77
91
new_connection = connection[1 ].replace(' .net' , ' .net?authSource=%24e xternal&authMechanism=MONGODB-X509' )
78
92
print (new_connection)
79
93
@@ -84,7 +98,6 @@ client = MongoClient(new_connection,
84
98
85
99
# Access the database
86
100
client.list_database_names()
87
-
88
101
```
89
102
## Next Steps
90
103
0 commit comments