redisctl 0.10.1

Unified CLI for Redis Cloud and Enterprise
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Connection management for Redis Cloud and Enterprise clients

use crate::error::Result as CliResult;
use anyhow::Context;
use redisctl_core::{Config, DeploymentType};
use tracing::{debug, info, trace};

/// User agent string for redisctl HTTP requests
const REDISCTL_USER_AGENT: &str = concat!("redisctl/", env!("CARGO_PKG_VERSION"));

/// Resolved Cloud connection details (without creating an HTTP client)
#[allow(dead_code)] // Used by binary target
pub struct CloudConnectionInfo {
    pub base_url: String,
    pub api_key: String,
    pub api_secret: String,
    pub user_agent: String,
}

/// Resolved Enterprise connection details (without creating an HTTP client)
#[allow(dead_code)] // Used by binary target
pub struct EnterpriseConnectionInfo {
    pub base_url: String,
    pub username: String,
    pub password: Option<String>,
    pub insecure: bool,
    pub ca_cert: Option<String>,
    pub user_agent: String,
}

/// Connection manager for creating authenticated clients
#[allow(dead_code)] // Used by binary target
#[derive(Clone)]
pub struct ConnectionManager {
    pub config: Config,
    pub config_path: Option<std::path::PathBuf>,
}

impl ConnectionManager {
    /// Create a new connection manager with the given configuration
    #[allow(dead_code)] // Used by binary target
    pub fn new(config: Config) -> Self {
        Self {
            config,
            config_path: None,
        }
    }

    /// Create a new connection manager with a custom config path
    #[allow(dead_code)] // Used by binary target
    pub fn with_config_path(config: Config, config_path: Option<std::path::PathBuf>) -> Self {
        Self {
            config,
            config_path,
        }
    }

    /// Save the configuration to the appropriate location
    #[allow(dead_code)] // Used by binary target
    pub fn save_config(&self) -> CliResult<()> {
        if let Some(ref path) = self.config_path {
            self.config
                .save_to_path(path)
                .context("Failed to save configuration")?;
        } else {
            self.config.save().context("Failed to save configuration")?;
        }
        Ok(())
    }

    /// Resolve Cloud connection info without creating an HTTP client.
    ///
    /// Follows the same credential resolution logic as `create_cloud_client`:
    /// environment variables override profile config, and --config-file disables env vars.
    #[allow(dead_code)] // Used by binary target
    pub fn resolve_cloud_connection(
        &self,
        profile_name: Option<&str>,
    ) -> CliResult<CloudConnectionInfo> {
        let (api_key, api_secret, base_url) = self.resolve_cloud_credentials(profile_name)?;
        Ok(CloudConnectionInfo {
            base_url,
            api_key,
            api_secret,
            user_agent: REDISCTL_USER_AGENT.to_string(),
        })
    }

    /// Create a Cloud client from profile credentials with environment variable override support
    ///
    /// When --config-file is explicitly specified, environment variables are ignored to provide
    /// true configuration isolation. This allows testing with isolated configs and follows the
    /// principle of "explicit wins" (CLI args > env vars > defaults).
    #[allow(dead_code)] // Used by binary target
    pub async fn create_cloud_client(
        &self,
        profile_name: Option<&str>,
    ) -> CliResult<redis_cloud::CloudClient> {
        debug!("Creating Redis Cloud client");

        let (final_api_key, final_api_secret, final_api_url) =
            self.resolve_cloud_credentials(profile_name)?;

        info!("Connecting to Redis Cloud API: {}", final_api_url);
        trace!(
            "API key: {}...",
            &final_api_key[..final_api_key.len().min(8)]
        );

        // Create and configure the Cloud client
        let client = redis_cloud::CloudClient::builder()
            .api_key(&final_api_key)
            .api_secret(&final_api_secret)
            .base_url(&final_api_url)
            .user_agent(REDISCTL_USER_AGENT)
            .build()
            .context("Failed to create Redis Cloud client")?;

        debug!("Redis Cloud client created successfully");
        Ok(client)
    }

    /// Resolve Cloud credentials from profile and/or environment variables.
    fn resolve_cloud_credentials(
        &self,
        profile_name: Option<&str>,
    ) -> CliResult<(String, String, String)> {
        trace!("Profile name: {:?}", profile_name);

        // When --config-file is explicitly specified, ignore environment variables
        let use_env_vars = self.config_path.is_none();

        debug!(
            "Config path: {:?}, use_env_vars: {}",
            self.config_path, use_env_vars
        );

        if !use_env_vars {
            info!("--config-file specified explicitly, ignoring environment variables");
        }

        // Check if all required environment variables are present (only if we're using them)
        let env_api_key = if use_env_vars {
            std::env::var("REDIS_CLOUD_API_KEY").ok()
        } else {
            None
        };
        let env_api_secret = if use_env_vars {
            std::env::var("REDIS_CLOUD_SECRET_KEY").ok()
        } else {
            None
        };
        let env_api_url = if use_env_vars {
            std::env::var("REDIS_CLOUD_API_URL").ok()
        } else {
            None
        };

        if env_api_key.is_some() {
            debug!("Found REDIS_CLOUD_API_KEY environment variable");
        }
        if env_api_secret.is_some() {
            debug!("Found REDIS_CLOUD_SECRET_KEY environment variable");
        }
        if env_api_url.is_some() {
            debug!("Found REDIS_CLOUD_API_URL environment variable");
        }

        let result = if let (Some(key), Some(secret)) = (&env_api_key, &env_api_secret) {
            // Environment variables provide complete credentials
            info!("Using Redis Cloud credentials from environment variables");
            let url = env_api_url.unwrap_or_else(|| "https://api.redislabs.com/v1".to_string());
            (key.clone(), secret.clone(), url)
        } else {
            // Resolve the profile using type-specific logic
            let resolved_profile_name = self.config.resolve_cloud_profile(profile_name)?;
            info!("Using Redis Cloud profile: {}", resolved_profile_name);

            let profile = self
                .config
                .profiles
                .get(&resolved_profile_name)
                .with_context(|| format!("Profile '{}' not found", resolved_profile_name))?;

            // Verify it's a cloud profile
            if profile.deployment_type != DeploymentType::Cloud {
                return Err(crate::error::RedisCtlError::ProfileTypeMismatch {
                    name: resolved_profile_name.to_string(),
                    actual_type: match profile.deployment_type {
                        DeploymentType::Cloud => "cloud",
                        DeploymentType::Enterprise => "enterprise",
                        DeploymentType::Database => "database",
                    }
                    .to_string(),
                    expected_type: "cloud".to_string(),
                    available_profiles: self
                        .config
                        .get_profiles_of_type(DeploymentType::Cloud)
                        .into_iter()
                        .map(String::from)
                        .collect(),
                });
            }

            // Use the new resolve method which handles keyring lookup
            let (api_key, api_secret, api_url) = profile
                .resolve_cloud_credentials()
                .context("Failed to resolve Cloud credentials")?
                .context("Profile is not configured for Redis Cloud")?;

            // Check for partial overrides before consuming the Options
            let has_overrides =
                env_api_key.is_some() || env_api_secret.is_some() || env_api_url.is_some();

            // Allow partial environment variable overrides
            let key = env_api_key.unwrap_or(api_key);
            let secret = env_api_secret.unwrap_or(api_secret);
            let url = env_api_url.unwrap_or(api_url);

            if has_overrides {
                debug!("Applied partial environment variable overrides");
            }

            (key, secret, url)
        };

        Ok(result)
    }

    /// Resolve Enterprise connection info without creating an HTTP client.
    ///
    /// Follows the same credential resolution logic as `create_enterprise_client`:
    /// environment variables override profile config, and --config-file disables env vars.
    #[allow(dead_code)] // Used by binary target
    pub fn resolve_enterprise_connection(
        &self,
        profile_name: Option<&str>,
    ) -> CliResult<EnterpriseConnectionInfo> {
        let (url, username, password, insecure, ca_cert) =
            self.resolve_enterprise_credentials(profile_name)?;
        Ok(EnterpriseConnectionInfo {
            base_url: url,
            username,
            password,
            insecure,
            ca_cert,
            user_agent: REDISCTL_USER_AGENT.to_string(),
        })
    }

    /// Create an Enterprise client from profile credentials with environment variable override support
    ///
    /// When --config-file is explicitly specified, environment variables are ignored to provide
    /// true configuration isolation. This allows testing with isolated configs and follows the
    /// principle of "explicit wins" (CLI args > env vars > defaults).
    #[allow(dead_code)] // Used by binary target
    pub async fn create_enterprise_client(
        &self,
        profile_name: Option<&str>,
    ) -> CliResult<redis_enterprise::EnterpriseClient> {
        debug!("Creating Redis Enterprise client");

        let (final_url, final_username, final_password, final_insecure, final_ca_cert) =
            self.resolve_enterprise_credentials(profile_name)?;

        info!("Connecting to Redis Enterprise: {}", final_url);
        debug!("Username: {}", final_username);
        debug!(
            "Password: {}",
            if final_password.is_some() {
                "configured"
            } else {
                "not set"
            }
        );
        debug!("Insecure mode: {}", final_insecure);
        debug!(
            "CA cert: {}",
            if final_ca_cert.is_some() {
                "configured"
            } else {
                "not set"
            }
        );

        // Build the Enterprise client
        let mut builder = redis_enterprise::EnterpriseClient::builder()
            .base_url(&final_url)
            .username(&final_username)
            .user_agent(REDISCTL_USER_AGENT);

        // Add password if provided
        if let Some(ref password) = final_password {
            builder = builder.password(password);
            trace!("Password added to client builder");
        }

        // Set insecure flag if needed
        if final_insecure {
            builder = builder.insecure(true);
            debug!("SSL certificate verification disabled");
        }

        // Add CA certificate if provided
        if let Some(ref ca_cert_path) = final_ca_cert {
            builder = builder.ca_cert(ca_cert_path);
            debug!("Using custom CA certificate: {}", ca_cert_path);
        }

        let client = builder
            .build()
            .context("Failed to create Redis Enterprise client")?;

        debug!("Redis Enterprise client created successfully");
        Ok(client)
    }

    /// Resolve Enterprise credentials from profile and/or environment variables.
    #[allow(clippy::type_complexity)]
    fn resolve_enterprise_credentials(
        &self,
        profile_name: Option<&str>,
    ) -> CliResult<(String, String, Option<String>, bool, Option<String>)> {
        trace!("Profile name: {:?}", profile_name);

        // When --config-file is explicitly specified, ignore environment variables
        let use_env_vars = self.config_path.is_none();

        debug!(
            "Config path: {:?}, use_env_vars: {}",
            self.config_path, use_env_vars
        );

        if !use_env_vars {
            info!("--config-file specified explicitly, ignoring environment variables");
        }

        // Check if all required environment variables are present (only if we're using them)
        let env_url = if use_env_vars {
            std::env::var("REDIS_ENTERPRISE_URL").ok()
        } else {
            None
        };
        let env_user = if use_env_vars {
            std::env::var("REDIS_ENTERPRISE_USER").ok()
        } else {
            None
        };
        let env_password = if use_env_vars {
            std::env::var("REDIS_ENTERPRISE_PASSWORD").ok()
        } else {
            None
        };
        let env_insecure = if use_env_vars {
            std::env::var("REDIS_ENTERPRISE_INSECURE").ok()
        } else {
            None
        };
        let env_ca_cert = if use_env_vars {
            std::env::var("REDIS_ENTERPRISE_CA_CERT").ok()
        } else {
            None
        };

        if env_url.is_some() {
            debug!("Found REDIS_ENTERPRISE_URL environment variable");
        }
        if env_user.is_some() {
            debug!("Found REDIS_ENTERPRISE_USER environment variable");
        }
        if env_password.is_some() {
            debug!("Found REDIS_ENTERPRISE_PASSWORD environment variable");
        }
        if env_insecure.is_some() {
            debug!("Found REDIS_ENTERPRISE_INSECURE environment variable");
        }
        if env_ca_cert.is_some() {
            debug!("Found REDIS_ENTERPRISE_CA_CERT environment variable");
        }

        let result = if let (Some(url), Some(user)) = (&env_url, &env_user) {
            // Environment variables provide complete credentials
            info!("Using Redis Enterprise credentials from environment variables");
            let password = env_password.clone();
            let insecure = env_insecure
                .as_ref()
                .map(|s| s.to_lowercase() == "true" || s == "1")
                .unwrap_or(false);
            let ca_cert = env_ca_cert.clone();
            (url.clone(), user.clone(), password, insecure, ca_cert)
        } else {
            // Resolve the profile using type-specific logic
            let resolved_profile_name = self.config.resolve_enterprise_profile(profile_name)?;
            info!("Using Redis Enterprise profile: {}", resolved_profile_name);

            let profile = self
                .config
                .profiles
                .get(&resolved_profile_name)
                .with_context(|| format!("Profile '{}' not found", resolved_profile_name))?;

            // Verify it's an enterprise profile
            if profile.deployment_type != DeploymentType::Enterprise {
                return Err(crate::error::RedisCtlError::ProfileTypeMismatch {
                    name: resolved_profile_name.to_string(),
                    actual_type: match profile.deployment_type {
                        DeploymentType::Cloud => "cloud",
                        DeploymentType::Enterprise => "enterprise",
                        DeploymentType::Database => "database",
                    }
                    .to_string(),
                    expected_type: "enterprise".to_string(),
                    available_profiles: self
                        .config
                        .get_profiles_of_type(DeploymentType::Enterprise)
                        .into_iter()
                        .map(String::from)
                        .collect(),
                });
            }

            // Use the new resolve method which handles keyring lookup
            let (url, username, password, insecure, profile_ca_cert) = profile
                .resolve_enterprise_credentials()
                .context("Failed to resolve Enterprise credentials")?
                .context("Profile is not configured for Redis Enterprise")?;

            // Check for partial overrides before consuming the Options
            let has_overrides = env_url.is_some()
                || env_user.is_some()
                || env_password.is_some()
                || env_insecure.is_some()
                || env_ca_cert.is_some();

            // Allow partial environment variable overrides
            let final_url = env_url.unwrap_or(url);
            let final_user = env_user.unwrap_or(username);
            let final_password = env_password.or(password);
            let final_insecure = env_insecure
                .as_ref()
                .map(|s| s.to_lowercase() == "true" || s == "1")
                .unwrap_or(insecure);
            let final_ca_cert = env_ca_cert.or(profile_ca_cert);

            if has_overrides {
                debug!("Applied partial environment variable overrides");
            }

            (
                final_url,
                final_user,
                final_password,
                final_insecure,
                final_ca_cert,
            )
        };

        Ok(result)
    }
}