signstar_crypto/secret_file/
error.rs

1//! Error type for secret file writing and reading.
2
3use std::{path::PathBuf, process::ExitStatus, string::FromUtf8Error};
4
5use signstar_common::common::SECRET_FILE_MODE;
6
7/// An error that may occur when working with cryptographic types for Signstar.
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10    /// An error specific to Signstar config handling.
11    /// Applying permissions to a file or directory failed.
12    #[error("Unable to apply permissions from mode {mode} to {path}:\n{source}")]
13    ApplyPermissions {
14        /// The path to a file for which permissions can not be applied.
15        path: PathBuf,
16        /// The file mode that should be applied for `path`.
17        mode: u32,
18        /// The source error.
19        source: std::io::Error,
20    },
21
22    /// The ownership of a path can not be changed.
23    #[error("Changing ownership of {path} to user {user} failed:\n{source}")]
24    Chown {
25        /// The path to a file for which ownership can not be changed.
26        path: PathBuf,
27        /// The system user that should be the new owner of `path`.
28        user: String,
29        /// The source error.
30        source: std::io::Error,
31    },
32
33    /// Unable to attach to stdin of a command.
34    #[error("Unable to attach to stdin of command \"{command}\"")]
35    CommandAttachToStdin {
36        /// The command for which attaching to stdin failed.
37        command: String,
38    },
39
40    /// A command exited unsuccessfully.
41    #[error("The command \"{command}\" could not be started in the background:\n{source}")]
42    CommandBackground {
43        /// The command that could not be started in the background.
44        command: String,
45        /// The source error.
46        source: std::io::Error,
47    },
48
49    /// A command could not be executed.
50    #[error("The command \"{command}\" could not be executed:\n{source}")]
51    CommandExec {
52        /// The command that could not be executed.
53        command: String,
54        /// The source error.
55        source: std::io::Error,
56    },
57
58    /// A command exited unsuccessfully.
59    #[error(
60        "The command \"{command}\" exited with non-zero status code \"{exit_status}\":\nstderr:\n{stderr}"
61    )]
62    CommandNonZero {
63        /// The command that exited with a non-zero exit code.
64        command: String,
65        /// The exit status of `command`.
66        exit_status: ExitStatus,
67        /// The stderr of `command`.
68        stderr: String,
69    },
70
71    /// Unable to write to stdin of a command.
72    #[error("Unable to write to stdin of command \"{command}\"")]
73    CommandWriteToStdin {
74        /// The command for which writing to stdin failed.
75        command: String,
76        /// The source error.
77        source: std::io::Error,
78    },
79
80    /// An I/O error occurred at a path.
81    #[error("I/O error at {path} while {context}: {source}")]
82    IoPath {
83        /// The path to the file for which the error occurred.
84        path: PathBuf,
85
86        /// The context in which the error occurs.
87        ///
88        /// This is meant to complete the sentence "I/O error at path {path} while ".
89        context: &'static str,
90
91        /// The error source.
92        source: std::io::Error,
93    },
94
95    /// The current user is an unprivileged user, but should be root.
96    #[error("Not running as root while {context}")]
97    NotRunningAsRoot {
98        /// The context in which the error occurred.
99        ///
100        /// This is meant to complete the sentence "Not running as root while ".
101        context: String,
102    },
103
104    /// The current user is root, but should be an unprivileged user.
105    #[error("Running as root instead of user {target_user} while {context}")]
106    RunningAsRoot {
107        /// The unprivileged system user which should have been used instead.
108        target_user: String,
109
110        /// The context in which the error occurred.
111        ///
112        /// This is meant to complete the sentence "Running as root instead of user {target_user}
113        /// while ".
114        context: String,
115    },
116
117    /// A passphrase directory can not be created.
118    #[error("Passphrase directory {path} for user {system_user} can not be created:\n{source}")]
119    SecretsDirCreate {
120        /// The path to a secrets directory that could not be created.
121        path: PathBuf,
122        /// The system user in whose home directory `path` could not be created.
123        system_user: String,
124        /// The source error.
125        source: std::io::Error,
126    },
127
128    /// A secrets file can not be created.
129    #[error("The secrets file {path} can not be created for user {system_user}:\n{source}")]
130    SecretsFileCreate {
131        /// The path to a secrets file that could not be created.
132        path: PathBuf,
133        /// The system user in whose home directory `path` could not be created.
134        system_user: String,
135        /// The source error.
136        source: std::io::Error,
137    },
138
139    /// The file metadata of a secrets file cannot be retrieved.
140    #[error("File metadata of secrets file {path} cannot be retrieved")]
141    SecretsFileMetadata {
142        /// The path to a secrets file for which metadata could not be retrieved.
143        path: PathBuf,
144        /// The source error.
145        source: std::io::Error,
146    },
147
148    /// A secrets file does not exist.
149    #[error("Secrets file not found: {path}")]
150    SecretsFileMissing {
151        /// The path to a secrets file that is missing.
152        path: PathBuf,
153    },
154
155    /// A secrets file is not a file.
156    #[error("Secrets file is not a file: {path}")]
157    SecretsFileNotAFile {
158        /// The path to a secrets file that is not a file.
159        path: PathBuf,
160    },
161
162    /// A secrets file does not have the correct permissions.
163    #[error("Secrets file {path} has permissions {mode}, but {SECRET_FILE_MODE} is required")]
164    SecretsFilePermissions {
165        /// The path to a secrets file for which permissions could not be set.
166        path: PathBuf,
167        /// The file mode that should be applied to the file at `path`.
168        mode: u32,
169    },
170
171    /// A secrets file cannot be read.
172    #[error("Failed reading secrets file {path}:\n{source}")]
173    SecretsFileRead {
174        /// The path to a secrets file that could not be read.
175        path: PathBuf,
176        /// The source error.
177        source: std::io::Error,
178    },
179
180    /// A secrets file can not be written to.
181    #[error("The secrets file {path} can not be written to for user {system_user}: {source}")]
182    SecretsFileWrite {
183        /// The path to a secrets file that could not be written to.
184        path: PathBuf,
185        /// The system user in whose home directory `path` resides.
186        system_user: String,
187        /// The source error.
188        source: std::io::Error,
189    },
190
191    /// A UTF-8 error occurred when trying to convert a byte vector to a string.
192    #[error("Converting contents of {path} to string failed while {context}:\n{source}")]
193    Utf8String {
194        /// The path to a file for which conversion to UTF-8 string failed.
195        path: PathBuf,
196        /// The context in which the error occurred.
197        ///
198        /// Should complete the sentence "Converting contents of `path` to string failed while "
199        context: String,
200        /// The source error.
201        source: FromUtf8Error,
202    },
203}