@@ -58,22 +58,28 @@ MFA is required to execute database sessions
58
58
Tap any security key
59
59
Detected security key tap
60
60
61
- [mysql-db1] @@hostname
62
- [mysql-db1] mysql-db1-hostname
63
- [mysql-db2] @@hostname
64
- [mysql-db2] mysql-db2-hostname
61
+ Executing command for ' mysql-db1' :
62
+ @@hostname
63
+ mysql-db1-hostname
64
+
65
+ Executing command for ' mysql-db2' :
66
+ @@hostname
67
+ mysql-db2-hostname
65
68
```
66
69
67
70
I would like to search databases by labels, run the sql scripts in parallel, and
68
71
record the outputs to a directory:
69
72
``` bash
70
- $ tsh db exec --search-by-labels env=dev --db-user mysql --exec-query " source my_script.sql" --output -dir exec-logs --max-connections 3
73
+ $ tsh db exec --search-by-labels env=dev --db-user mysql --exec-query " source my_script.sql" --log -dir exec-logs --max-connections 3
71
74
Found 5 databases:
72
- - mysql-db1
73
- - mysql-db2
74
- - mysql-db3
75
- - mysql-db4
76
- - mysql-db5
75
+
76
+ Name Protocol Description Labels
77
+ --------- -------- ----------- -------
78
+ mysql-db1 mysql instance 1 env=dev
79
+ mysql-db2 mysql instance 2 env=dev
80
+ mysql-db3 mysql instance 3 env=dev
81
+ mysql-db4 mysql instance 4 env=dev
82
+ mysql-db5 mysql instance 5 env=dev
77
83
78
84
Tip: use --skip-confirm to skip this confirmation.
79
85
Do you want to continue? (Press any key to proceed or Ctrl+C to exit): < enter>
@@ -82,21 +88,38 @@ MFA is required to execute database sessions
82
88
Tap any security key
83
89
Detected security key tap
84
90
85
- Executing command for ' mysql-db1'
86
- Executing command for ' mysql-db2'
87
- Executing command for ' mysql-db3'
88
- Executing command for ' mysql-db4'
89
- Executing command for ' mysql-db5'
90
-
91
- $ ls exec-logs/
92
- mysql-db1.output mysql-db2.output mysql-db3.output mysql-db4.output mysql-db5.output
91
+ Executing command for ' mysql-db1' . Outputs will be saved at ' exec-logs/mysql-db1.log' .
92
+ Executing command for ' mysql-db2' . Outputs will be saved at ' exec-logs/mysql-db2.log' .
93
+ Executing command for ' mysql-db3' . Outputs will be saved at ' exec-logs/mysql-db3.log' .
94
+ Executing command for ' mysql-db4' . Outputs will be saved at ' exec-logs/mysql-db4.log' .
95
+ Executing command for ' mysql-db5' . Outputs will be saved at ' exec-logs/mysql-db5.log' .
93
96
```
97
+ (where you can expect the first 3 connections happen right away, and the other 2
98
+ connections happen after the previous ones finish.)
94
99
95
100
### Multi-session MFA
96
101
97
- TODO(greedy52) add a diagram.
102
+ Overview:
103
+ ``` mermaid
104
+ sequenceDiagram
105
+ participant user
106
+ participant tsh
107
+ participant Teleport
108
+
109
+ user -> tsh: tsh db exec
110
+ tsh -> Teleport: CreateAuthenticateChallengeRequest<br/>Scope: SCOPE_DATABASE_MULTI_SESSION<br/>Reuse: true
111
+ Teleport -> tsh: challenge
112
+ tsh -> user: prompt
113
+ user -> tsh: tap
114
+ tsh -> Teleport: WebAuthn login
115
+ Teleport -> tsh: MFA Response
116
+ loop
117
+ tsh -> Teleport: GenerateUserCerts with MFA response
118
+ Teleport -> tsh: User cert with database route
119
+ end
120
+ ```
98
121
99
- A new role option is added to preserve existing behavior if not set :
122
+ A new role option is added to decide the mode for session MFA :
100
123
``` diff
101
124
kind: role
102
125
version: v7
@@ -109,6 +132,9 @@ spec:
109
132
+ # supported for `tsh db exec` command with WebAuthn as the second factor.
110
133
+ requie_session_mfa_mode: "multi-session"
111
134
```
135
+ Mode defaults to ` per-session ` if not set. If a resource matches a role set with
136
+ some roles on ` per-session ` but others on ` multi-session ` , the stricter mode
137
+ ` per-session ` should be applied.
112
138
113
139
The multi-session MFA extends [ RFD 155 Scoped Webauthn
114
140
Credentials] ( https://github.com/gravitational/teleport/blob/master/rfd/0155-scoped-webauthn-credentials.md )
@@ -122,59 +148,71 @@ enum ChallengeScope {
122
148
}
123
149
```
124
150
125
- Similar to ` SCOPE_ADMIN_ACTION ` , the new scope will allow reuse of the MFA
126
- session data until it expires (5 minutes for WebAuthn).
151
+ Similar to ` SCOPE_ADMIN_ACTION ` , the new scope ` SCOPE_DATABASE_MULTI_SESSION `
152
+ will allow reuse of the MFA session data until it expires (5 minutes for
153
+ WebAuthn).
127
154
128
155
The MFA response will be checked upon auth call of ` GenerateUserCerts ` where
129
156
user requests a TLS user cert with database route. New logic is added to
130
157
` GenerateUserCerts ` where the new scope with reuse is allowed only if the role
131
158
set matching the requested database has ` roleset.option.requie_session_mfa_mode `
132
159
option set to ` multi-session ` .
133
160
134
- If MFA response is validated with existing non-reusable ` SCOPE_SESSION ` , the
135
- action should be allowed regardless of ` roleset.option.requie_session_mfa_mode ` .
161
+ The new scope cannot be used for ` GenerateUserCerts ` for non-database targets.
162
+ And if the MFA response is validated with existing non-reusable ` SCOPE_SESSION ` ,
163
+ the action should be allowed regardless of
164
+ ` roleset.option.requie_session_mfa_mode ` .
136
165
137
166
Here is a quick matrix:
138
167
139
- | ` session_mfa_mode ` | MFA response scope | Access |
140
- | ------------------ | ------------------ | ------ |
141
- | ` multi-session ` | ` SCOPE_SESSION ` | allow |
142
- | ` multi-session ` | ` SCOPE_DATABASE_MULTI_SESSION ` | allow |
143
- | ` per-session ` | ` SCOPE_SESSION ` | allow |
144
- | ` per-session ` | ` SCOPE_DATABASE_MULTI_SESSION ` | denied |
168
+ | ` session_mfa_mode ` | MFA response scope | Requested Target | Access |
169
+ | --------------------| --------------------------------| ------------------| --------|
170
+ | ` multi-session ` | ` SCOPE_SESSION ` | Database | Allow |
171
+ | ` multi-session ` | ` SCOPE_DATABASE_MULTI_SESSION ` | Database | Allow |
172
+ | ` multi-session ` | ` SCOPE_DATABASE_MULTI_SESSION ` | Non-Database | Denied |
173
+ | ` per-session ` | ` SCOPE_SESSION ` | Database | Allow |
174
+ | ` per-session ` | ` SCOPE_DATABASE_MULTI_SESSION ` | Database | Denied |
175
+ | ` per-session ` | ` SCOPE_DATABASE_MULTI_SESSION ` | Non-Database | Denied |
145
176
146
177
### The ` tsh db exec ` command
147
178
148
179
General flow of the command:
149
180
- Fetch databases (either specified directly or through search).
150
- - Fetch roles and use access checker to pre- determine MFA requirement.
151
- - For each database
152
- - Prompt MFA if necessary
153
- - Starts a local proxy in tunnel mode for this database
154
- - Craft a command and ` os.exec ` .
155
-
156
- For MVP, only PostgreSQL and MySQL databases will be supported .
157
-
158
- #### ` tsh db exec ` search
159
- ` tsh db exec ` supports searching database by specifying one the following flags:
181
+ - Fetch roles and use access checker to determine MFA requirement.
182
+ - For each database:
183
+ - Prompt MFA if necessary.
184
+ - Starts a local proxy in tunnel mode for this database.
185
+ - Craft a command for ` os.exec ` . The command is not interactive (e.g. does not
186
+ take in ` stdin ` input). Outputs are printed to ` stdout ` unless ` --log-dir `
187
+ is specified .
188
+ - Execute the command.
189
+
190
+ The command supports searching database by specifying one the following flags:
160
191
- ` --search ` : List of comma separated search keywords or phrases enclosed in quotations, e.g. ` --search=foo,bar `
161
192
- ` --search-by-labels ` : List of comma separated labels to filter by labels, e.g. ` key1=value1,key2=value2 `
162
193
- ` --search-by-query ` : Query by predicate language enclosed in single quotes.
163
194
164
195
The command presents the search results then asks user to confirm before
165
196
proceeding. ` --skip-confirm ` can be used to skip the confirmation.
166
197
198
+ Some other details:
199
+ - If the multi-session MFA response is expired, the command should ask for MFA
200
+ again.
201
+ - For MVP implementation, only PostgreSQL and MySQL databases will be supported.
202
+ - A warning will be printed if the target databases have different protocols
203
+ (e.g. ` postgres ` vs ` mysql ` ).
204
+ - For databases that require per-session MFA, a prompt will still be presented
205
+ per database.
206
+
207
+ #### Possible enhancements for ` tsh db exec `
208
+ - ` tsh db exec --exec-config ` to support a config file which allows specifying
209
+ different flags like ` --db-user ` , ` --db-name ` , ` --exec-query ` per target
210
+ database or per search.
211
+ - ` tsh db exec --exec-command ` to support custom command template like `$ tsh
212
+ db exec --exec-command "bash -c './myscript {{.DB_SERVICE}} {{.DB_USER}}
213
+ {{.DB_LOCAL_PORT}}'"` . An env var ` TSH_UNSTABLE_DB_EXEC_COMMAND` can be
214
+ supported for the initial MVP.
215
+
167
216
### Security
168
217
169
218
TODO
170
-
171
- ### Possible enhancements
172
- #### ` tsh db exec --exec-config `
173
- To support a config file which allows specifying different flags like
174
- ` --db-user ` , ` --db-name ` , ` --exec-query ` per target database.
175
-
176
- #### ` tsh db exec --exec-command `
177
- To support custom command template like:
178
- ```
179
- $ tsh db exec --exec-command "bash -c './myscript {{.DB_SERVICE}} {{.DB_LOCAL_PORT}}'
180
- ```
0 commit comments