Skip to main content

torrust_tracker_deployer_lib/adapters/ssh/
credentials.rs

1//! SSH credentials for remote instance authentication
2//!
3//! This module provides the `SshCredentials` struct which manages SSH authentication
4//! information including private/public key paths and username configuration.
5//!
6//! ## Key Features
7//!
8//! - SSH key pair management (private and public keys)
9//! - Username configuration for remote connections
10//! - Integration with SSH connection establishment
11//! - Support for creating SSH connections with target IP addresses
12//!
13//! The credentials are typically configured at startup and used throughout
14//! the deployment process for secure remote access to provisioned instances.
15
16use std::path::PathBuf;
17
18use crate::shared::Username;
19use serde::{Deserialize, Serialize};
20
21/// SSH credentials for remote instance authentication.
22///
23/// Contains the static SSH authentication information that is known
24/// at program startup, before any instances are provisioned.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct SshCredentials {
27    /// Path to the SSH private key file for remote connections.
28    ///
29    /// This key will be used by the SSH client to authenticate with remote
30    /// instances created during deployment. The corresponding public key
31    /// should be authorized on the target instances.
32    pub ssh_priv_key_path: PathBuf,
33
34    /// Path to the SSH public key file for remote connections.
35    ///
36    /// This public key will be used for authorization on target instances
37    /// during the deployment process, typically injected into cloud-init
38    /// configurations or `authorized_keys` files.
39    pub ssh_pub_key_path: PathBuf,
40
41    /// Username for SSH connections to remote instances.
42    ///
43    /// This username will be used when establishing SSH connections to
44    /// deployed instances. Common values include "ubuntu", "root", or "torrust".
45    pub ssh_username: Username,
46}
47
48impl SshCredentials {
49    /// Creates new SSH credentials with the provided parameters.
50    ///
51    /// ```rust
52    /// # use std::path::PathBuf;
53    /// # use torrust_tracker_deployer_lib::shared::Username;
54    /// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
55    /// let credentials = SshCredentials::new(
56    ///     PathBuf::from("/home/user/.ssh/deploy_key"),
57    ///     PathBuf::from("/home/user/.ssh/deploy_key.pub"),
58    ///     Username::new("ubuntu").unwrap(),
59    /// );
60    /// ```
61    #[must_use]
62    pub fn new(
63        ssh_priv_key_path: PathBuf,
64        ssh_pub_key_path: PathBuf,
65        ssh_username: Username,
66    ) -> Self {
67        Self {
68            ssh_priv_key_path,
69            ssh_pub_key_path,
70            ssh_username,
71        }
72    }
73}