1use crate::cli::CliError;
13use crate::cli::commands::apply_objects;
14use crate::cli::commands::grants;
15use crate::cli::executor::ObjectAction;
16use crate::cli::executor::{
17 ApplyPlan, ApplyResult, DeploymentExecutor, ObjectResult, compile_apply_project_and_connect,
18};
19use crate::client::Client;
20use crate::config::Settings;
21use crate::project;
22use crate::project::ast::Statement;
23use crate::project::ir::compiled;
24use crate::project::ir::object_id::ObjectId;
25use crate::secret_resolver::SecretResolver;
26use mz_sql_parser::ast::{
27 AlterConnectionAction, AlterConnectionStatement, ConnectionOption, ConnectionOptionName, Raw,
28 Statement as ParserStatement,
29};
30use mz_sql_parser::parser::parse_statements;
31use std::collections::{BTreeMap, BTreeSet};
32
33const PHASE_NAME: &str = "connections";
34const GRANT_KIND: grants::GrantObjectKind = grants::GrantObjectKind::Connection;
35
36fn matches(stmt: &Statement) -> bool {
37 matches!(stmt, Statement::CreateConnection(_))
38}
39
40struct Connections {
41 resolver: SecretResolver,
42}
43
44impl Connections {
45 fn new(settings: &Settings) -> Result<Self, CliError> {
46 Ok(Connections {
47 resolver: SecretResolver::new(&settings.profile_config.security),
48 })
49 }
50
51 async fn handle_existing(
52 &self,
53 client: &Client,
54 executor: &DeploymentExecutor<'_>,
55 obj_id: &ObjectId,
56 typed_obj: &compiled::DatabaseObject,
57 ) -> Result<ObjectAction, CliError> {
58 let Statement::CreateConnection(ref create_stmt) = typed_obj.stmt else {
59 unreachable!("filtered for CreateConnection");
60 };
61
62 let resolved_stmt = match self
63 .resolver
64 .resolve_statement_for_cli(&typed_obj.stmt)
65 .await?
66 {
67 Statement::CreateConnection(s) => s,
68 _ => unreachable!(),
69 };
70
71 let live_sql = client
72 .introspection()
73 .get_connection_create_sql(obj_id.expect_database(), obj_id.schema(), obj_id.object())
74 .await
75 .map_err(CliError::Connection)?;
76
77 let action = match live_sql {
78 None => {
79 executor.execute_sql(&resolved_stmt).await?;
82 ObjectAction::Created
83 }
84 Some(sql) => {
85 let live_create = parse_create_connection_sql(&sql)?;
86 let (to_set, to_drop) =
87 diff_connection_options(&resolved_stmt.values, &live_create.values);
88
89 if to_set.is_empty() && to_drop.is_empty() {
90 ObjectAction::UpToDate
91 } else {
92 let actions: Vec<AlterConnectionAction<Raw>> = to_set
93 .into_iter()
94 .map(AlterConnectionAction::SetOption)
95 .chain(to_drop.into_iter().map(AlterConnectionAction::DropOption))
96 .collect();
97
98 let alter_stmt = AlterConnectionStatement::<Raw> {
99 name: create_stmt.name.clone(),
100 if_exists: false,
101 actions,
102 with_options: vec![],
103 };
104 executor.execute_sql(&alter_stmt).await?;
105 ObjectAction::Altered
106 }
107 }
108 };
109
110 apply_objects::reconcile_grants_and_comments(
111 client,
112 executor,
113 obj_id,
114 typed_obj,
115 &GRANT_KIND,
116 )
117 .await?;
118
119 Ok(action)
120 }
121
122 async fn handle_new(
123 &self,
124 client: &Client,
125 executor: &DeploymentExecutor<'_>,
126 obj_id: &ObjectId,
127 typed_obj: &compiled::DatabaseObject,
128 ) -> Result<ObjectAction, CliError> {
129 let resolved_stmt = match self
130 .resolver
131 .resolve_statement_for_cli(&typed_obj.stmt)
132 .await?
133 {
134 Statement::CreateConnection(s) => s,
135 _ => unreachable!(),
136 };
137
138 executor.execute_sql(&resolved_stmt).await?;
139
140 apply_objects::reconcile_grants_and_comments(
141 client,
142 executor,
143 obj_id,
144 typed_obj,
145 &GRANT_KIND,
146 )
147 .await?;
148
149 Ok(ObjectAction::Created)
150 }
151}
152
153pub async fn plan(
155 settings: &Settings,
156 client: &Client,
157 executor: &DeploymentExecutor<'_>,
158 planned_project: &project::ir::graph::Project,
159 apply_plan: &mut ApplyPlan,
160) -> Result<ApplyResult, CliError> {
161 let connections = Connections::new(settings)?;
162 let mut target_ids = BTreeSet::new();
163 for obj in planned_project.iter_objects() {
164 if matches(&obj.typed_object.stmt) {
165 target_ids.insert(obj.id.clone());
166 }
167 }
168
169 if target_ids.is_empty() {
170 return Ok(ApplyResult {
171 phase: PHASE_NAME.to_string(),
172 results: vec![],
173 });
174 }
175
176 let target_objects = planned_project.get_sorted_objects_filtered(&target_ids)?;
177 let existing = client
178 .introspection()
179 .check_catalog_objects_exist(&target_ids, GRANT_KIND.catalog_table())
180 .await
181 .map_err(CliError::Connection)?;
182
183 let schemas: BTreeSet<_> = target_objects
184 .iter()
185 .filter(|(obj_id, _)| !existing.contains(obj_id))
186 .map(|(obj_id, _)| {
187 project::SchemaQualifier::new(
188 obj_id.expect_database().to_string(),
189 obj_id.schema().to_string(),
190 )
191 })
192 .collect();
193 apply_plan
194 .prepare_schemas(executor, planned_project, &schemas)
195 .await?;
196
197 let mut results = Vec::new();
198
199 for (obj_id, typed_obj) in target_objects {
200 executor.take_statements();
201 let action = if existing.contains(&obj_id) {
202 connections
203 .handle_existing(client, executor, &obj_id, typed_obj)
204 .await?
205 } else {
206 connections
207 .handle_new(client, executor, &obj_id, typed_obj)
208 .await?
209 };
210 results.push(ObjectResult {
211 object: obj_id.to_string(),
212 action,
213 statements: executor.take_statements(),
214 redacted_statements: vec![],
215 transaction_group: None,
216 post_statements: vec![],
217 });
218 }
219
220 Ok(ApplyResult {
221 phase: PHASE_NAME.to_string(),
222 results,
223 })
224}
225
226pub async fn run(settings: &Settings, dry_run: bool) -> Result<ApplyPlan, CliError> {
228 let (planned_project, client) = compile_apply_project_and_connect(settings).await?;
229 let mut apply_plan = ApplyPlan::new();
230 let executor = DeploymentExecutor::new_dry_run(&client);
231 let phase = plan(
232 settings,
233 &client,
234 &executor,
235 &planned_project,
236 &mut apply_plan,
237 )
238 .await?;
239 apply_plan.add_phase(phase);
240
241 if !dry_run {
242 apply_plan.execute(&client).await?;
243 }
244
245 Ok(apply_plan)
246}
247
248fn parse_create_connection_sql(
250 sql: &str,
251) -> Result<mz_sql_parser::ast::CreateConnectionStatement<Raw>, CliError> {
252 let stmts = parse_statements(sql).map_err(|e| {
253 CliError::Message(format!(
254 "failed to parse SHOW CREATE CONNECTION output: {}",
255 e.error
256 ))
257 })?;
258
259 let stmt = stmts
260 .into_iter()
261 .next()
262 .ok_or_else(|| CliError::Message("SHOW CREATE CONNECTION returned empty SQL".into()))?;
263
264 match stmt.ast {
265 ParserStatement::CreateConnection(c) => Ok(c),
266 other => Err(CliError::Message(format!(
267 "expected CREATE CONNECTION, got: {}",
268 other
269 ))),
270 }
271}
272
273fn diff_connection_options(
279 project_opts: &[ConnectionOption<Raw>],
280 live_opts: &[ConnectionOption<Raw>],
281) -> (Vec<ConnectionOption<Raw>>, Vec<ConnectionOptionName>) {
282 let generated = |name: ConnectionOptionName| {
288 matches!(
289 name,
290 ConnectionOptionName::PublicKey1 | ConnectionOptionName::PublicKey2
291 )
292 };
293 let project_map: BTreeMap<ConnectionOptionName, &ConnectionOption<Raw>> = project_opts
294 .iter()
295 .filter(|o| !generated(o.name))
296 .map(|o| (o.name, o))
297 .collect();
298 let live_map: BTreeMap<ConnectionOptionName, &ConnectionOption<Raw>> = live_opts
299 .iter()
300 .filter(|o| !generated(o.name))
301 .map(|o| (o.name, o))
302 .collect();
303
304 let mut to_set = Vec::new();
305 let mut to_drop = Vec::new();
306
307 for (name, project_opt) in &project_map {
309 match live_map.get(name) {
310 None => to_set.push((*project_opt).clone()),
311 Some(live_opt) => {
312 if *project_opt != *live_opt {
313 to_set.push((*project_opt).clone());
314 }
315 }
316 }
317 }
318
319 for name in live_map.keys() {
321 if !project_map.contains_key(name) {
322 to_drop.push(*name);
323 }
324 }
325
326 (to_set, to_drop)
327}
328
329#[cfg(test)]
330mod tests {
331 use super::*;
332 use mz_sql_parser::ast::WithOptionValue;
333
334 fn make_option(name: ConnectionOptionName, value: &str) -> ConnectionOption<Raw> {
335 ConnectionOption {
336 name,
337 value: Some(WithOptionValue::Value(mz_sql_parser::ast::Value::String(
338 value.to_string(),
339 ))),
340 }
341 }
342
343 #[mz_ore::test]
344 fn test_diff_no_changes() {
345 let opts = vec![
346 make_option(ConnectionOptionName::Host, "localhost"),
347 make_option(ConnectionOptionName::Port, "5432"),
348 ];
349 let (to_set, to_drop) = diff_connection_options(&opts, &opts);
350 assert!(to_set.is_empty());
351 assert!(to_drop.is_empty());
352 }
353
354 #[mz_ore::test]
355 fn test_diff_ignores_generated_public_keys() {
356 let declared = vec![make_option(ConnectionOptionName::Host, "bastion")];
357 let live = vec![
358 make_option(ConnectionOptionName::Host, "bastion"),
359 make_option(ConnectionOptionName::PublicKey1, "ssh-ed25519 a"),
360 make_option(ConnectionOptionName::PublicKey2, "ssh-ed25519 b"),
361 ];
362 for project in [&declared, &live] {
364 let (to_set, to_drop) = diff_connection_options(project, &live);
365 assert!(to_set.is_empty());
366 assert!(to_drop.is_empty());
367 }
368 }
369
370 #[mz_ore::test]
371 fn test_diff_option_changed() {
372 let project = vec![make_option(
373 ConnectionOptionName::Host,
374 "new-host.example.com",
375 )];
376 let live = vec![make_option(
377 ConnectionOptionName::Host,
378 "old-host.example.com",
379 )];
380 let (to_set, to_drop) = diff_connection_options(&project, &live);
381 assert_eq!(to_set.len(), 1);
382 assert_eq!(to_set[0].name, ConnectionOptionName::Host);
383 assert!(to_drop.is_empty());
384 }
385
386 #[mz_ore::test]
387 fn test_diff_option_added() {
388 let project = vec![
389 make_option(ConnectionOptionName::Host, "localhost"),
390 make_option(ConnectionOptionName::Database, "mydb"),
391 ];
392 let live = vec![make_option(ConnectionOptionName::Host, "localhost")];
393 let (to_set, to_drop) = diff_connection_options(&project, &live);
394 assert_eq!(to_set.len(), 1);
395 assert_eq!(to_set[0].name, ConnectionOptionName::Database);
396 assert!(to_drop.is_empty());
397 }
398
399 #[mz_ore::test]
400 fn test_diff_option_dropped() {
401 let project = vec![make_option(ConnectionOptionName::Host, "localhost")];
402 let live = vec![
403 make_option(ConnectionOptionName::Host, "localhost"),
404 make_option(ConnectionOptionName::Database, "mydb"),
405 ];
406 let (to_set, to_drop) = diff_connection_options(&project, &live);
407 assert!(to_set.is_empty());
408 assert_eq!(to_drop.len(), 1);
409 assert_eq!(to_drop[0], ConnectionOptionName::Database);
410 }
411
412 #[mz_ore::test]
413 fn test_diff_multiple_changes() {
414 let project = vec![
415 make_option(ConnectionOptionName::Host, "new-host"),
416 make_option(ConnectionOptionName::Port, "5433"),
417 ];
418 let live = vec![
419 make_option(ConnectionOptionName::Host, "old-host"),
420 make_option(ConnectionOptionName::Database, "mydb"),
421 ];
422 let (to_set, to_drop) = diff_connection_options(&project, &live);
423 assert_eq!(to_set.len(), 2);
424 assert!(to_set.iter().any(|o| o.name == ConnectionOptionName::Host));
425 assert!(to_set.iter().any(|o| o.name == ConnectionOptionName::Port));
426 assert_eq!(to_drop.len(), 1);
427 assert_eq!(to_drop[0], ConnectionOptionName::Database);
428 }
429
430 #[mz_ore::test]
431 fn test_diff_secret_option_compared_structurally() {
432 use mz_sql_parser::ast::{Ident, RawItemName, UnresolvedItemName};
433
434 let secret_ref =
435 WithOptionValue::Secret(RawItemName::Name(UnresolvedItemName::qualified(&[
436 Ident::new_unchecked("db"),
437 Ident::new_unchecked("public"),
438 Ident::new_unchecked("my_pass"),
439 ])));
440 let project = vec![ConnectionOption {
441 name: ConnectionOptionName::SaslPassword,
442 value: Some(secret_ref.clone()),
443 }];
444 let live = vec![ConnectionOption {
445 name: ConnectionOptionName::SaslPassword,
446 value: Some(secret_ref),
447 }];
448 let (to_set, to_drop) = diff_connection_options(&project, &live);
449 assert!(to_set.is_empty());
450 assert!(to_drop.is_empty());
451 }
452}