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 `validate_ssh_host` refused a url.
94#[derive(Debug, thiserror::Error)]
95pub enum SshHostError {
96    /// Not one of the ssh forms integrates stores, or a host past the DNS limits.
97    #[error("Invalid ssh url: expected user@host:path or ssh://user@host[:port]/path")]
98    Malformed,
99    #[error("Could not resolve the hostname {host}: {source}")]
100    Unresolved {
101        host: String,
102        #[source]
103        source: io::Error,
104    },
105    #[error("No IP addresses resolved for hostname: {host}")]
106    NoAddresses { host: String },
107    #[error("SSH host validation failed for {host}: {reason}")]
108    Local {
109        host: String,
110        reason: crate::local_address::LocalAddress,
111    },
112}
113
114/// Why no session came out of STS for a role.
115#[derive(Debug, thiserror::Error)]
116pub enum StsError {
117    /// STS answered with an error, as it does for a role that does not trust us.
118    #[error("STS refused the role {arn}: {code}: {message}")]
119    Refused {
120        arn: String,
121        code: String,
122        message: String,
123    },
124    #[error("STS took too long to connect for the role {arn}: {reason}")]
125    ConnectTimeout { arn: String, reason: String },
126    /// STS took the request and did not answer in time.
127    #[error("STS took too long to answer the role {arn}: {reason}")]
128    ReadTimeout { arn: String, reason: String },
129    /// STS closed the connection before it answered.
130    #[error("STS closed the connection for the role {arn}: {reason}")]
131    ConnectionClosed { arn: String, reason: String },
132    /// No connection opened: no network, or a host that does not resolve or refuses it.
133    #[error("STS could not be connected to for the role {arn}: {reason}")]
134    NoConnection { arn: String, reason: String },
135    /// Any other request that got no answer, as one without local credentials to sign it.
136    #[error("STS could not be asked for the role {arn}: {reason}")]
137    Unreachable { arn: String, reason: String },
138    #[error("STS answered the role {arn} without credentials")]
139    NoCredentials { arn: String },
140}
141
142#[derive(Debug, thiserror::Error)]
143pub enum GitCallError {
144    #[error("git {command} could not start: {source}")]
145    NotStarted {
146        command: &'static str,
147        source: io::Error,
148    },
149    #[error("git {command} did not finish within {after:?}")]
150    TimedOut {
151        command: &'static str,
152        after: Duration,
153    },
154    /// git ran and refused, as it does for a missing repository or revision.
155    #[error("git {command} exited with {status}: {stderr}")]
156    Refused {
157        command: &'static str,
158        status: ExitStatus,
159        stderr: String,
160    },
161    /// No process can receive a NUL, so std turns the call down before it starts.
162    #[error("git {command} got an argument with an embedded null byte")]
163    NullByte { command: &'static str },
164    #[error("git {command} printed something unreadable: {reason}")]
165    Malformed {
166        command: &'static str,
167        reason: String,
168    },
169}
170
171/// Why `download_and_extract_repo` failed, by the step that failed.
172#[derive(Debug, thiserror::Error)]
173pub enum DownloadError {
174    /// The parent of the destination or the archive could not be created or
175    /// written.
176    #[error("writing the archive: {0}")]
177    Write(#[source] io::Error),
178    #[error(transparent)]
179    Fetch(#[from] FetchError),
180    #[error(transparent)]
181    Extract(#[from] ExtractError),
182    /// `git reset --hard` did not restore the working tree.
183    #[error(transparent)]
184    Reset(#[from] GitCallError),
185    #[error("pruning the working tree: {0}")]
186    Prune(#[from] PruneError),
187    /// The repository could not take the place of the destination, or the
188    /// work directory beside it could not be made.
189    #[error("placing the repository: {0}")]
190    Place(#[source] io::Error),
191}
192
193/// How the request for the archive failed.
194#[derive(Debug, thiserror::Error)]
195pub enum FetchError {
196    #[error("invalid url {url}: {reason}")]
197    InvalidUrl { url: String, reason: String },
198    #[error("not an http url: {0}")]
199    NonHttpUrl(String),
200    /// DNS, a refused connection or a TLS handshake that failed.
201    #[error("could not connect: {0}")]
202    Connect(#[source] reqwest::Error),
203    #[error("connecting timed out: {0}")]
204    ConnectTimeout(#[source] reqwest::Error),
205    /// The total timeout ran out before the response headers arrived.
206    #[error("no response in time: {0}")]
207    Timeout(#[source] reqwest::Error),
208    /// The server closed the connection before it answered.
209    #[error("the server disconnected: {0}")]
210    Disconnected(#[source] reqwest::Error),
211    /// The socket failed while the request was sent; `errno` is the one of
212    /// the socket call.
213    #[error("the socket failed: {source}")]
214    Socket {
215        reset: bool,
216        errno: Option<i32>,
217        source: reqwest::Error,
218    },
219    #[error("malformed response: {0}")]
220    Malformed(#[source] reqwest::Error),
221    #[error("the server answered {0}")]
222    Status(reqwest::StatusCode),
223    /// The body ended before its length, or the connection broke mid-read.
224    #[error("the body was cut: {0}")]
225    BodyCut(#[source] reqwest::Error),
226    /// The total timeout ran out while the body was read.
227    #[error("the body did not arrive in time: {0}")]
228    BodyTimeout(#[source] reqwest::Error),
229    #[error("the request failed: {0}")]
230    Other(#[source] reqwest::Error),
231}
232
233/// How turning the archive into a repository failed.
234#[derive(Debug, thiserror::Error)]
235pub enum ExtractError {
236    /// The archive is not gzip, its tar or deflate data is broken, or its
237    /// checksum does not match.
238    #[error("corrupt archive: {0}")]
239    Corrupt(#[source] io::Error),
240    /// The gzip stream ends before its trailer.
241    #[error("truncated archive: {0}")]
242    Truncated(#[source] io::Error),
243    /// The archive holds no top directory with a real `.git` directory.
244    #[error("the archive holds no repository: {0}")]
245    NoRepository(String),
246    #[error("extracting the archive: {0}")]
247    Io(#[source] io::Error),
248}