Skip to main content

fluidattacks_core_git/
errors.rs

1use std::io;
2use std::path::PathBuf;
3use std::process::ExitStatus;
4use std::time::Duration;
5
6/// A file call failed at `path`; `source` keeps the errno.
7#[derive(Debug, thiserror::Error)]
8#[error("{action} {}: {source}", path.display())]
9pub struct FileError {
10    pub action: &'static str,
11    pub path: PathBuf,
12    #[source]
13    pub source: io::Error,
14}
15
16/// Removing the symlinks or the out-of-scope files failed.
17#[derive(Debug, thiserror::Error)]
18pub enum PruneError {
19    #[error(transparent)]
20    File(#[from] FileError),
21    /// The git that lists the out-of-scope files failed.
22    #[error(transparent)]
23    Git(#[from] GitCallError),
24}
25
26/// Why `reset_repo` failed.
27#[derive(Debug, thiserror::Error)]
28pub enum ResetError {
29    /// `git reset --hard` failed, as it does for a path that is not the root.
30    #[error(transparent)]
31    Git(#[from] GitCallError),
32    #[error(transparent)]
33    Symlinks(#[from] FileError),
34}
35
36/// Why an `HttpsAccess` could not be built, worded as the python worded it.
37#[derive(Debug, PartialEq, Eq, thiserror::Error)]
38pub enum HttpsAccessError {
39    /// git reads a credential line by line: a line break could add a field, a nul cuts it short.
40    #[error("Invalid credentials: a line break or a nul byte")]
41    CredentialLineBreak,
42    #[error("expected an http or https url")]
43    NotHttp,
44    /// The url stays out of the message: it may carry a user and password.
45    #[error("Invalid url: its host or port could not be parsed")]
46    Unparsable,
47    #[error("expected an https credential")]
48    NotHttpsCredential,
49}
50
51/// Why `clone_to_temp` failed, by the step that failed.
52#[derive(Debug, thiserror::Error)]
53pub enum CloneError {
54    #[error(transparent)]
55    Access(#[from] HttpsAccessError),
56    #[error(transparent)]
57    Git(#[from] GitCallError),
58    /// The mirror cloned but its `.info.json` could not be written, so the clone was removed.
59    #[error(transparent)]
60    Info(#[from] FileError),
61    #[error(transparent)]
62    SshKey(#[from] SshKeyError),
63    #[error(transparent)]
64    Sts(#[from] StsError),
65}
66
67/// Why the SSH key could not be handed to git.
68#[derive(Debug, thiserror::Error)]
69pub enum SshKeyError {
70    #[error("the SSH key is not base64: {0}")]
71    Decode(#[from] base64::DecodeError),
72    #[error("the SSH key is empty")]
73    Empty,
74    #[error("the SSH key could not be written: {0}")]
75    File(#[source] io::Error),
76    #[error("the SSH key path cannot be quoted for the shell: {0}")]
77    Path(#[from] shlex::QuoteError),
78}
79
80/// Why an ls-remote could not ask, by the step that failed.
81#[derive(Debug, thiserror::Error)]
82pub enum LsRemoteError {
83    #[error(transparent)]
84    SshKey(#[from] SshKeyError),
85    #[error(transparent)]
86    Sts(#[from] StsError),
87    #[error(transparent)]
88    Access(#[from] HttpsAccessError),
89    #[error(transparent)]
90    Git(#[from] GitCallError),
91}
92
93/// Why a host was refused before anything connected to it.
94#[derive(Debug, thiserror::Error)]
95pub enum HostError {
96    #[error("could not resolve the hostname: {0}")]
97    Unresolved(#[source] io::Error),
98    #[error("no IP addresses resolved for the hostname")]
99    NoAddresses,
100    #[error(transparent)]
101    Local(crate::local_address::LocalAddress),
102}
103
104/// Why `validate_ssh_host` refused a url.
105#[derive(Debug, thiserror::Error)]
106pub enum SshUrlError {
107    /// Not one of the ssh forms integrates stores, or a host past the DNS limits.
108    #[error("Invalid ssh url: expected user@host:path or ssh://user@host[:port]/path")]
109    Malformed,
110    #[error("SSH host validation failed for {host}: {source}")]
111    Refused {
112        host: String,
113        #[source]
114        source: HostError,
115    },
116}
117
118/// Why no session came out of STS for a role.
119#[derive(Debug, thiserror::Error)]
120pub enum StsError {
121    /// STS answered with an error, as it does for a role that does not trust us.
122    #[error("STS refused the role {arn}: {code}: {message}")]
123    Refused {
124        arn: String,
125        code: String,
126        message: String,
127    },
128    #[error("STS took too long to connect for the role {arn}: {reason}")]
129    ConnectTimeout { arn: String, reason: String },
130    /// STS took the request and did not answer in time.
131    #[error("STS took too long to answer the role {arn}: {reason}")]
132    ReadTimeout { arn: String, reason: String },
133    /// STS closed the connection before it answered.
134    #[error("STS closed the connection for the role {arn}: {reason}")]
135    ConnectionClosed { arn: String, reason: String },
136    /// No connection opened: no network, or a host that does not resolve or refuses it.
137    #[error("STS could not be connected to for the role {arn}: {reason}")]
138    NoConnection { arn: String, reason: String },
139    /// Any other request that got no answer, as one without local credentials to sign it.
140    #[error("STS could not be asked for the role {arn}: {reason}")]
141    Unreachable { arn: String, reason: String },
142    #[error("STS answered the role {arn} without credentials")]
143    NoCredentials { arn: String },
144}
145
146#[derive(Debug, thiserror::Error)]
147pub enum GitCallError {
148    #[error("git {command} could not start: {source}")]
149    NotStarted {
150        command: &'static str,
151        source: io::Error,
152    },
153    #[error("git {command} did not finish within {after:?}")]
154    TimedOut {
155        command: &'static str,
156        after: Duration,
157    },
158    /// git ran and refused, as it does for a missing repository or revision.
159    #[error("git {command} exited with {status}: {stderr}")]
160    Refused {
161        command: &'static str,
162        status: ExitStatus,
163        stderr: String,
164    },
165    /// No process can receive a NUL, so std turns the call down before it starts.
166    #[error("git {command} got an argument with an embedded null byte")]
167    NullByte { command: &'static str },
168    #[error("git {command} printed something unreadable: {reason}")]
169    Malformed {
170        command: &'static str,
171        reason: String,
172    },
173}
174
175/// Why `download_and_extract_repo` failed, by the step that failed.
176#[derive(Debug, thiserror::Error)]
177pub enum DownloadError {
178    /// The parent of the destination or the archive could not be created or
179    /// written.
180    #[error("writing the archive: {0}")]
181    Write(#[source] io::Error),
182    #[error(transparent)]
183    Fetch(#[from] FetchError),
184    #[error(transparent)]
185    Extract(#[from] ExtractError),
186    /// `git reset --hard` did not restore the working tree.
187    #[error(transparent)]
188    Reset(#[from] GitCallError),
189    #[error("pruning the working tree: {0}")]
190    Prune(#[from] PruneError),
191    /// The repository could not take the place of the destination, or the
192    /// work directory beside it could not be made.
193    #[error("placing the repository: {0}")]
194    Place(#[source] io::Error),
195}
196
197/// How the request for the archive failed.
198#[derive(Debug, thiserror::Error)]
199pub enum FetchError {
200    #[error("invalid url {url}: {reason}")]
201    InvalidUrl { url: String, reason: String },
202    #[error("not an http url: {0}")]
203    NonHttpUrl(String),
204    /// DNS, a refused connection or a TLS handshake that failed.
205    #[error("could not connect: {0}")]
206    Connect(#[source] reqwest::Error),
207    #[error("connecting timed out: {0}")]
208    ConnectTimeout(#[source] reqwest::Error),
209    /// The total timeout ran out before the response headers arrived.
210    #[error("no response in time: {0}")]
211    Timeout(#[source] reqwest::Error),
212    /// The server closed the connection before it answered.
213    #[error("the server disconnected: {0}")]
214    Disconnected(#[source] reqwest::Error),
215    /// The socket failed while the request was sent; `errno` is the one of
216    /// the socket call.
217    #[error("the socket failed: {source}")]
218    Socket {
219        reset: bool,
220        errno: Option<i32>,
221        source: reqwest::Error,
222    },
223    #[error("malformed response: {0}")]
224    Malformed(#[source] reqwest::Error),
225    #[error("the server answered {0}")]
226    Status(reqwest::StatusCode),
227    /// The body ended before its length, or the connection broke mid-read.
228    #[error("the body was cut: {0}")]
229    BodyCut(#[source] reqwest::Error),
230    /// The total timeout ran out while the body was read.
231    #[error("the body did not arrive in time: {0}")]
232    BodyTimeout(#[source] reqwest::Error),
233    #[error("the request failed: {0}")]
234    Other(#[source] reqwest::Error),
235}
236
237/// How turning the archive into a repository failed.
238#[derive(Debug, thiserror::Error)]
239pub enum ExtractError {
240    /// The archive is not gzip, its tar or deflate data is broken, or its
241    /// checksum does not match.
242    #[error("corrupt archive: {0}")]
243    Corrupt(#[source] io::Error),
244    /// The gzip stream ends before its trailer.
245    #[error("truncated archive: {0}")]
246    Truncated(#[source] io::Error),
247    /// The archive holds no top directory with a real `.git` directory.
248    #[error("the archive holds no repository: {0}")]
249    NoRepository(String),
250    #[error("extracting the archive: {0}")]
251    Io(#[source] io::Error),
252}