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 `redirected_url` could not tell where a repository lives.
119#[derive(Debug, thiserror::Error)]
120pub enum RedirectError {
121    /// The url, or a `Location` it answered, could not be read.
122    #[error("Invalid url: {0}")]
123    Unparsable(String),
124    #[error("expected an http or https url")]
125    NotHttp,
126    #[error("Hostname or IP address invalid")]
127    InvalidHost,
128    #[error("expected an https credential")]
129    NotHttpsCredential,
130    #[error("Invalid credentials: a line break or another character a header cannot carry")]
131    CredentialNotAHeader,
132    #[error(transparent)]
133    Local(crate::local_address::LocalAddress),
134    #[error("no response in time: {0}")]
135    Timeout(#[source] reqwest::Error),
136    /// DNS, a refused connection, a TLS handshake that failed or a server that hung up.
137    #[error("the request failed: {0}")]
138    Request(#[source] reqwest::Error),
139}
140
141/// Why no session came out of STS for a role.
142#[derive(Debug, thiserror::Error)]
143pub enum StsError {
144    /// STS answered with an error, as it does for a role that does not trust us.
145    #[error("STS refused the role {arn}: {code}: {message}")]
146    Refused {
147        arn: String,
148        code: String,
149        message: String,
150    },
151    #[error("STS took too long to connect for the role {arn}: {reason}")]
152    ConnectTimeout { arn: String, reason: String },
153    /// STS took the request and did not answer in time.
154    #[error("STS took too long to answer the role {arn}: {reason}")]
155    ReadTimeout { arn: String, reason: String },
156    /// STS closed the connection before it answered.
157    #[error("STS closed the connection for the role {arn}: {reason}")]
158    ConnectionClosed { arn: String, reason: String },
159    /// No connection opened: no network, or a host that does not resolve or refuses it.
160    #[error("STS could not be connected to for the role {arn}: {reason}")]
161    NoConnection { arn: String, reason: String },
162    /// Any other request that got no answer, as one without local credentials to sign it.
163    #[error("STS could not be asked for the role {arn}: {reason}")]
164    Unreachable { arn: String, reason: String },
165    #[error("STS answered the role {arn} without credentials")]
166    NoCredentials { arn: String },
167}
168
169#[derive(Debug, thiserror::Error)]
170pub enum GitCallError {
171    #[error("git {command} could not start: {source}")]
172    NotStarted {
173        command: &'static str,
174        source: io::Error,
175    },
176    #[error("git {command} did not finish within {after:?}")]
177    TimedOut {
178        command: &'static str,
179        after: Duration,
180    },
181    /// git ran and refused, as it does for a missing repository or revision.
182    #[error("git {command} exited with {status}: {stderr}")]
183    Refused {
184        command: &'static str,
185        status: ExitStatus,
186        stderr: String,
187    },
188    /// No process can receive a NUL, so std turns the call down before it starts.
189    #[error("git {command} got an argument with an embedded null byte")]
190    NullByte { command: &'static str },
191    #[error("git {command} printed something unreadable: {reason}")]
192    Malformed {
193        command: &'static str,
194        reason: String,
195    },
196}
197
198/// Why `download_and_extract_repo` failed, by the step that failed.
199#[derive(Debug, thiserror::Error)]
200pub enum DownloadError {
201    /// The parent of the destination or the archive could not be created or
202    /// written.
203    #[error("writing the archive: {0}")]
204    Write(#[source] io::Error),
205    #[error(transparent)]
206    Fetch(#[from] FetchError),
207    #[error(transparent)]
208    Extract(#[from] ExtractError),
209    /// `git reset --hard` did not restore the working tree.
210    #[error(transparent)]
211    Reset(#[from] GitCallError),
212    #[error("pruning the working tree: {0}")]
213    Prune(#[from] PruneError),
214    /// The repository could not take the place of the destination, or the
215    /// work directory beside it could not be made.
216    #[error("placing the repository: {0}")]
217    Place(#[source] io::Error),
218}
219
220/// How the request for the archive failed.
221#[derive(Debug, thiserror::Error)]
222pub enum FetchError {
223    #[error("invalid url {url}: {reason}")]
224    InvalidUrl { url: String, reason: String },
225    #[error("not an http url: {0}")]
226    NonHttpUrl(String),
227    /// DNS, a refused connection or a TLS handshake that failed.
228    #[error("could not connect: {0}")]
229    Connect(#[source] reqwest::Error),
230    #[error("connecting timed out: {0}")]
231    ConnectTimeout(#[source] reqwest::Error),
232    /// The total timeout ran out before the response headers arrived.
233    #[error("no response in time: {0}")]
234    Timeout(#[source] reqwest::Error),
235    /// The server closed the connection before it answered.
236    #[error("the server disconnected: {0}")]
237    Disconnected(#[source] reqwest::Error),
238    /// The socket failed while the request was sent; `errno` is the one of
239    /// the socket call.
240    #[error("the socket failed: {source}")]
241    Socket {
242        reset: bool,
243        errno: Option<i32>,
244        source: reqwest::Error,
245    },
246    #[error("malformed response: {0}")]
247    Malformed(#[source] reqwest::Error),
248    #[error("the server answered {0}")]
249    Status(reqwest::StatusCode),
250    /// The body ended before its length, or the connection broke mid-read.
251    #[error("the body was cut: {0}")]
252    BodyCut(#[source] reqwest::Error),
253    /// The total timeout ran out while the body was read.
254    #[error("the body did not arrive in time: {0}")]
255    BodyTimeout(#[source] reqwest::Error),
256    #[error("the request failed: {0}")]
257    Other(#[source] reqwest::Error),
258}
259
260/// How turning the archive into a repository failed.
261#[derive(Debug, thiserror::Error)]
262pub enum ExtractError {
263    /// The archive is not gzip, its tar or deflate data is broken, or its
264    /// checksum does not match.
265    #[error("corrupt archive: {0}")]
266    Corrupt(#[source] io::Error),
267    /// The gzip stream ends before its trailer.
268    #[error("truncated archive: {0}")]
269    Truncated(#[source] io::Error),
270    /// The archive holds no top directory with a real `.git` directory.
271    #[error("the archive holds no repository: {0}")]
272    NoRepository(String),
273    #[error("extracting the archive: {0}")]
274    Io(#[source] io::Error),
275}