1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! When CNI goes bad.

use std::env::VarError;

use regex::Regex;
use semver::Version;
use serde_json::Value;
use thiserror::Error;

use crate::reply::ErrorReply;

/// All errors emitted by this library, plus a few others.
#[derive(Debug, Error)]
pub enum CniError {
	/// Catch-all wrapper for I/O errors.
	#[error(transparent)]
	Io(#[from] std::io::Error),

	/// Catch-all wrapper for JSON serialization and deserialization.
	#[error(transparent)]
	Json(#[from] serde_json::Error),

	/// When the CNI version requested by the runtime is not supported.
	///
	/// The [`Version`] in the error is the CNI version provided, not ours.
	///
	/// Also see [`VersionReply`][crate::reply::VersionReply].
	#[error("plugin does not understand CNI version: {0}")]
	Incompatible(Version),

	/// When nothing is provided on STDIN.
	#[error("missing input network config")]
	MissingInput,

	/// When a delegated plugin doesn’t output anything on STDOUT.
	#[error("missing plugin output")]
	MissingOutput,

	/// When a required environment variable is missing.
	#[error("missing environment variable: {var}: {err}")]
	MissingEnv {
		/// the variable name
		var: &'static str,

		/// the underlying error
		#[source]
		err: VarError,
	},

	/// When an environment variable couldn’t be parsed or is invalid.
	#[error("environment variable has invalid format: {var}: {err}")]
	InvalidEnv {
		/// the variable name
		var: &'static str,

		/// the underlying error
		#[source]
		err: Box<dyn std::error::Error>,
	},

	/// When the current working directory cannot be obtained (for delegation).
	#[error("cannot obtain current working directory")]
	NoCwd,

	/// When a delegated plugin cannot be found on `CNI_PATH`.
	#[error("missing (or not on CNI_PATH) plugin {name}: {err}")]
	MissingPlugin {
		/// the name of the plugin binary
		name: String,

		/// the underlying error
		#[source]
		err: which::Error,
	},

	/// Wrapper for errors in relation to a delegated plugin.
	#[error("with plugin {plugin}: {err}")]
	Delegated {
		/// the name of the plugin binary
		plugin: String,

		/// the underlying error
		err: Box<Self>,
	},

	/// A generic error as a string.
	///
	/// This error variant is not used in the library, but is provided for
	/// plugin implementations to make use of without needing to make their own
	/// error type.
	///
	/// # Example
	///
	/// ```
	/// # use cni_plugin::error::CniError;
	/// CniError::Generic("a total catastrophe".into());
	/// ```
	#[error("{0}")]
	Generic(String),

	/// A debug error as anything that implements [`Debug`][std::fmt::Debug].
	///
	/// This error variant is not used in the library, but is provided for
	/// plugin implementations to make use of without needing to make their own
	/// error type.
	///
	/// # Example
	///
	/// ```
	/// # use cni_plugin::error::CniError;
	/// CniError::Debug(Box::new(("hello", "world", vec![1, 2, 3])));
	/// ```
	#[error("{0:?}")]
	Debug(Box<dyn std::fmt::Debug>),

	/// When a field in configuration is missing.
	///
	/// This error variant is not used in the library, but is provided for
	/// plugin implementations to make use of without needing to make their own
	/// error type.
	///
	/// # Example
	///
	/// ```
	/// # use cni_plugin::error::CniError;
	/// CniError::MissingField("ipam.type");
	/// ```
	#[error("can't proceed without {0} field")]
	MissingField(&'static str),

	/// When a field in configuration is invalid.
	///
	/// This error variant is not used in the library, but is provided for
	/// plugin implementations to make use of without needing to make their own
	/// error type.
	///
	/// # Example
	///
	/// ```
	/// # use cni_plugin::error::CniError;
	/// # use serde_json::Value;
	/// CniError::InvalidField {
	///     field: "ipam.pool",
	///     expected: "string",
	///     value: Value::Null,
	/// };
	/// ```
	#[error("{field}: expected {expected}, got: {value:?}")]
	InvalidField {
		/// the name or path of the invalid field
		field: &'static str,

		/// the value or type the field was expected to be
		expected: &'static str,

		/// the actual value or a facsimile thereof
		value: Value,
	},
}

impl CniError {
	/// Convert a CniError into an ErrorReply.
	///
	/// [`ErrorReply`]s can be used with [`reply`][crate::reply::reply], but
	/// require `cni_version` to be set to the input configuration’s. This
	/// method makes it easier to create errors (including with the `?`
	/// operator, from foreign error types) and only populate the version field
	/// when ready to send the reply.
	///
	/// It’s recommended to add an implementation of this if you make your own
	/// error type.
	pub fn into_reply(self, cni_version: Version) -> ErrorReply<'static> {
		match self {
			Self::Io(e) => ErrorReply {
				cni_version,
				code: 5,
				msg: "I/O error",
				details: e.to_string(),
			},
			Self::Json(e) => ErrorReply {
				cni_version,
				code: 6,
				msg: "Cannot decode JSON payload",
				details: e.to_string(),
			},
			e @ Self::Incompatible(_) => ErrorReply {
				cni_version,
				code: 1,
				msg: "Incompatible CNI version",
				details: e.to_string(),
			},
			e @ Self::MissingInput => ErrorReply {
				cni_version,
				code: 7,
				msg: "Missing payload",
				details: e.to_string(),
			},
			e @ Self::MissingOutput => ErrorReply {
				cni_version,
				code: 7,
				msg: "Missing output",
				details: e.to_string(),
			},
			e @ Self::MissingEnv { .. } => ErrorReply {
				cni_version,
				code: 4,
				msg: "Missing environment variable",
				details: e.to_string(),
			},
			e @ Self::InvalidEnv { .. } => ErrorReply {
				cni_version,
				code: 4,
				msg: "Invalid environment variable",
				details: e.to_string(),
			},
			e @ Self::NoCwd => ErrorReply {
				cni_version,
				code: 5,
				msg: "Bad workdir",
				details: e.to_string(),
			},
			e @ Self::MissingPlugin { .. } => ErrorReply {
				cni_version,
				code: 5,
				msg: "Missing plugin",
				details: e.to_string(),
			},
			e @ Self::Delegated { .. } => ErrorReply {
				cni_version,
				code: 5,
				msg: "Delegated",
				details: e.to_string(),
			},
			Self::Generic(s) => ErrorReply {
				cni_version,
				code: 100,
				msg: "ERROR",
				details: s,
			},
			e @ Self::Debug { .. } => ErrorReply {
				cni_version,
				code: 101,
				msg: "DEBUG",
				details: e.to_string(),
			},
			e @ Self::MissingField(_) => ErrorReply {
				cni_version,
				code: 104,
				msg: "Missing field",
				details: e.to_string(),
			},
			e @ Self::InvalidField { .. } => ErrorReply {
				cni_version,
				code: 107,
				msg: "Invalid field",
				details: e.to_string(),
			},
		}
	}
}

/// Underlying error used for an empty value that shouldn’t be.
///
/// Used with [`CniError::InvalidEnv`].
#[derive(Clone, Copy, Debug, Error)]
#[error("must not be empty")]
pub struct EmptyValueError;

/// Underlying error used for an invalid `CNI_COMMAND`.
///
/// Used with [`CniError::InvalidEnv`].
#[derive(Clone, Copy, Debug, Error)]
#[error("must be one of ADD, DEL, CHECK, VERSION")]
pub struct InvalidCommandError;

/// Underlying error used for a value that should match a regex but doesn’t.
///
/// Used with [`CniError::InvalidEnv`].
#[derive(Clone, Debug, Error)]
#[error("must match regex: {0}")]
pub struct RegexValueError(pub Regex);