]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/types.rs
src/api2/types.rs: add schemas for IP/CIDR
[proxmox-backup.git] / src / api2 / types.rs
1 use anyhow::{bail};
2 use ::serde::{Deserialize, Serialize};
3
4 use proxmox::api::{api, schema::*};
5 use proxmox::const_regex;
6 use proxmox::{IPRE, IPV4RE, IPV6RE, IPV4OCTET, IPV6H16, IPV6LS32};
7
8 // File names: may not contain slashes, may not start with "."
9 pub const FILENAME_FORMAT: ApiStringFormat = ApiStringFormat::VerifyFn(|name| {
10 if name.starts_with('.') {
11 bail!("file names may not start with '.'");
12 }
13 if name.contains('/') {
14 bail!("file names may not contain slashes");
15 }
16 Ok(())
17 });
18
19 macro_rules! DNS_LABEL { () => (r"(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)") }
20 macro_rules! DNS_NAME { () => (concat!(r"(?:", DNS_LABEL!() , r"\.)*", DNS_LABEL!())) }
21
22 // we only allow a limited set of characters
23 // colon is not allowed, because we store usernames in
24 // colon separated lists)!
25 // slash is not allowed because it is used as pve API delimiter
26 // also see "man useradd"
27 macro_rules! USER_NAME_REGEX_STR { () => (r"(?:[^\s:/[:cntrl:]]+)") }
28 macro_rules! GROUP_NAME_REGEX_STR { () => (USER_NAME_REGEX_STR!()) }
29
30 macro_rules! PROXMOX_SAFE_ID_REGEX_STR { () => (r"(?:[A-Za-z0-9_][A-Za-z0-9._\-]*)") }
31
32 macro_rules! CIDR_V4_REGEX_STR { () => (concat!(r"(?:", IPV4RE!(), r"/\d{1,2})$")) }
33 macro_rules! CIDR_V6_REGEX_STR { () => (concat!(r"(?:", IPV6RE!(), r"/\d{1,3})$")) }
34
35 const_regex!{
36 pub IP_V4_REGEX = concat!(r"^", IPV4RE!(), r"$");
37 pub IP_V6_REGEX = concat!(r"^", IPV6RE!(), r"$");
38 pub IP_REGEX = concat!(r"^", IPRE!(), r"$");
39 pub CIDR_V4_REGEX = concat!(r"^", CIDR_V4_REGEX_STR!(), r"$");
40 pub CIDR_V6_REGEX = concat!(r"^", CIDR_V6_REGEX_STR!(), r"$");
41 pub CIDR_REGEX = concat!(r"^(?:", CIDR_V4_REGEX_STR!(), "|", CIDR_V6_REGEX_STR!(), r")$");
42
43 pub SHA256_HEX_REGEX = r"^[a-f0-9]{64}$"; // fixme: define in common_regex ?
44 pub SYSTEMD_DATETIME_REGEX = r"^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$"; // fixme: define in common_regex ?
45
46 pub PASSWORD_REGEX = r"^[[:^cntrl:]]*$"; // everything but control characters
47
48 /// Regex for safe identifiers.
49 ///
50 /// This
51 /// [article](https://dwheeler.com/essays/fixing-unix-linux-filenames.html)
52 /// contains further information why it is reasonable to restict
53 /// names this way. This is not only useful for filenames, but for
54 /// any identifier command line tools work with.
55 pub PROXMOX_SAFE_ID_REGEX = concat!(r"^", PROXMOX_SAFE_ID_REGEX_STR!(), r"$");
56
57 pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$";
58
59 pub HOSTNAME_REGEX = r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)$";
60
61 pub DNS_NAME_REGEX = concat!(r"^", DNS_NAME!(), r"$");
62
63 pub DNS_NAME_OR_IP_REGEX = concat!(r"^", DNS_NAME!(), "|", IPRE!(), r"$");
64
65 pub PROXMOX_USER_ID_REGEX = concat!(r"^", USER_NAME_REGEX_STR!(), r"@", PROXMOX_SAFE_ID_REGEX_STR!(), r"$");
66
67 pub PROXMOX_GROUP_ID_REGEX = concat!(r"^", GROUP_NAME_REGEX_STR!(), r"$");
68
69 pub CERT_FINGERPRINT_SHA256_REGEX = r"^(?:[0-9a-fA-F][0-9a-fA-F])(?::[0-9a-fA-F][0-9a-fA-F]){31}$";
70
71 pub ACL_PATH_REGEX = concat!(r"^(?:/|", r"(?:/", PROXMOX_SAFE_ID_REGEX_STR!(), ")+", r")$");
72 }
73
74 pub const SYSTEMD_DATETIME_FORMAT: ApiStringFormat =
75 ApiStringFormat::Pattern(&SYSTEMD_DATETIME_REGEX);
76
77 pub const IP_V4_FORMAT: ApiStringFormat =
78 ApiStringFormat::Pattern(&IP_V4_REGEX);
79
80 pub const IP_V6_FORMAT: ApiStringFormat =
81 ApiStringFormat::Pattern(&IP_V6_REGEX);
82
83 pub const IP_FORMAT: ApiStringFormat =
84 ApiStringFormat::Pattern(&IP_REGEX);
85
86 pub const PVE_CONFIG_DIGEST_FORMAT: ApiStringFormat =
87 ApiStringFormat::Pattern(&SHA256_HEX_REGEX);
88
89 pub const CERT_FINGERPRINT_SHA256_FORMAT: ApiStringFormat =
90 ApiStringFormat::Pattern(&CERT_FINGERPRINT_SHA256_REGEX);
91
92 pub const PROXMOX_SAFE_ID_FORMAT: ApiStringFormat =
93 ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
94
95 pub const SINGLE_LINE_COMMENT_FORMAT: ApiStringFormat =
96 ApiStringFormat::Pattern(&SINGLE_LINE_COMMENT_REGEX);
97
98 pub const HOSTNAME_FORMAT: ApiStringFormat =
99 ApiStringFormat::Pattern(&HOSTNAME_REGEX);
100
101 pub const DNS_NAME_FORMAT: ApiStringFormat =
102 ApiStringFormat::Pattern(&DNS_NAME_REGEX);
103
104 pub const DNS_NAME_OR_IP_FORMAT: ApiStringFormat =
105 ApiStringFormat::Pattern(&DNS_NAME_OR_IP_REGEX);
106
107 pub const PROXMOX_USER_ID_FORMAT: ApiStringFormat =
108 ApiStringFormat::Pattern(&PROXMOX_USER_ID_REGEX);
109
110 pub const PROXMOX_GROUP_ID_FORMAT: ApiStringFormat =
111 ApiStringFormat::Pattern(&PROXMOX_GROUP_ID_REGEX);
112
113 pub const PASSWORD_FORMAT: ApiStringFormat =
114 ApiStringFormat::Pattern(&PASSWORD_REGEX);
115
116 pub const ACL_PATH_FORMAT: ApiStringFormat =
117 ApiStringFormat::Pattern(&ACL_PATH_REGEX);
118
119 pub const NETWORK_INTERFACE_FORMAT: ApiStringFormat =
120 ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
121
122 pub const CIDR_V4_FORMAT: ApiStringFormat =
123 ApiStringFormat::Pattern(&CIDR_V4_REGEX);
124
125 pub const CIDR_V6_FORMAT: ApiStringFormat =
126 ApiStringFormat::Pattern(&CIDR_V6_REGEX);
127
128 pub const CIDR_FORMAT: ApiStringFormat =
129 ApiStringFormat::Pattern(&CIDR_REGEX);
130
131
132 pub const PASSWORD_SCHEMA: Schema = StringSchema::new("Password.")
133 .format(&PASSWORD_FORMAT)
134 .min_length(1)
135 .max_length(1024)
136 .schema();
137
138 pub const PBS_PASSWORD_SCHEMA: Schema = StringSchema::new("User Password.")
139 .format(&PASSWORD_FORMAT)
140 .min_length(5)
141 .max_length(64)
142 .schema();
143
144 pub const CERT_FINGERPRINT_SHA256_SCHEMA: Schema = StringSchema::new(
145 "X509 certificate fingerprint (sha256)."
146 )
147 .format(&CERT_FINGERPRINT_SHA256_FORMAT)
148 .schema();
149
150 pub const PROXMOX_CONFIG_DIGEST_SCHEMA: Schema = StringSchema::new(r#"\
151 Prevent changes if current configuration file has different SHA256 digest.
152 This can be used to prevent concurrent modifications.
153 "#
154 )
155 .format(&PVE_CONFIG_DIGEST_FORMAT)
156 .schema();
157
158
159 pub const CHUNK_DIGEST_FORMAT: ApiStringFormat =
160 ApiStringFormat::Pattern(&SHA256_HEX_REGEX);
161
162 pub const CHUNK_DIGEST_SCHEMA: Schema = StringSchema::new("Chunk digest (SHA256).")
163 .format(&CHUNK_DIGEST_FORMAT)
164 .schema();
165
166 pub const NODE_SCHEMA: Schema = StringSchema::new("Node name (or 'localhost')")
167 .format(&ApiStringFormat::VerifyFn(|node| {
168 if node == "localhost" || node == proxmox::tools::nodename() {
169 Ok(())
170 } else {
171 bail!("no such node '{}'", node);
172 }
173 }))
174 .schema();
175
176 pub const SEARCH_DOMAIN_SCHEMA: Schema =
177 StringSchema::new("Search domain for host-name lookup.").schema();
178
179 pub const FIRST_DNS_SERVER_SCHEMA: Schema =
180 StringSchema::new("First name server IP address.")
181 .format(&IP_FORMAT)
182 .schema();
183
184 pub const SECOND_DNS_SERVER_SCHEMA: Schema =
185 StringSchema::new("Second name server IP address.")
186 .format(&IP_FORMAT)
187 .schema();
188
189 pub const THIRD_DNS_SERVER_SCHEMA: Schema =
190 StringSchema::new("Third name server IP address.")
191 .format(&IP_FORMAT)
192 .schema();
193
194 pub const IP_V4_SCHEMA: Schema =
195 StringSchema::new("IPv4 address.")
196 .format(&IP_V4_FORMAT)
197 .max_length(15)
198 .schema();
199
200 pub const IP_V6_SCHEMA: Schema =
201 StringSchema::new("IPv6 address.")
202 .format(&IP_V6_FORMAT)
203 .max_length(39)
204 .schema();
205
206 pub const IP_SCHEMA: Schema =
207 StringSchema::new("IP (IPv4 or IPv6) address.")
208 .format(&IP_FORMAT)
209 .max_length(39)
210 .schema();
211
212 pub const CIDR_V4_SCHEMA: Schema =
213 StringSchema::new("IPv4 address with netmask (CIDR notation).")
214 .format(&CIDR_V4_FORMAT)
215 .max_length(18)
216 .schema();
217
218 pub const CIDR_V6_SCHEMA: Schema =
219 StringSchema::new("IPv6 address with netmask (CIDR notation).")
220 .format(&CIDR_V6_FORMAT)
221 .max_length(43)
222 .schema();
223
224 pub const CIDR_SCHEMA: Schema =
225 StringSchema::new("IP address (IPv4 or IPv6) with netmask (CIDR notation).")
226 .format(&CIDR_FORMAT)
227 .max_length(43)
228 .schema();
229
230 pub const TIME_ZONE_SCHEMA: Schema = StringSchema::new(
231 "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.")
232 .format(&SINGLE_LINE_COMMENT_FORMAT)
233 .min_length(2)
234 .max_length(64)
235 .schema();
236
237 pub const ACL_PATH_SCHEMA: Schema = StringSchema::new(
238 "Access control path.")
239 .format(&ACL_PATH_FORMAT)
240 .min_length(1)
241 .max_length(128)
242 .schema();
243
244 pub const ACL_PROPAGATE_SCHEMA: Schema = BooleanSchema::new(
245 "Allow to propagate (inherit) permissions.")
246 .default(true)
247 .schema();
248
249 pub const ACL_UGID_TYPE_SCHEMA: Schema = StringSchema::new(
250 "Type of 'ugid' property.")
251 .format(&ApiStringFormat::Enum(&["user", "group"]))
252 .schema();
253
254 pub const ACL_ROLE_SCHEMA: Schema = StringSchema::new(
255 "Role.")
256 .format(&ApiStringFormat::Enum(&[
257 "Admin",
258 "Audit",
259 "Datastore.Admin",
260 "Datastore.Audit",
261 "Datastore.User",
262 "NoAccess",
263 ]))
264 .schema();
265
266 pub const BACKUP_ARCHIVE_NAME_SCHEMA: Schema =
267 StringSchema::new("Backup archive name.")
268 .format(&PROXMOX_SAFE_ID_FORMAT)
269 .schema();
270
271 pub const BACKUP_TYPE_SCHEMA: Schema =
272 StringSchema::new("Backup type.")
273 .format(&ApiStringFormat::Enum(&["vm", "ct", "host"]))
274 .schema();
275
276 pub const BACKUP_ID_SCHEMA: Schema =
277 StringSchema::new("Backup ID.")
278 .format(&PROXMOX_SAFE_ID_FORMAT)
279 .schema();
280
281 pub const BACKUP_TIME_SCHEMA: Schema =
282 IntegerSchema::new("Backup time (Unix epoch.)")
283 .minimum(1_547_797_308)
284 .schema();
285
286 pub const UPID_SCHEMA: Schema = StringSchema::new("Unique Process/Task ID.")
287 .max_length(256)
288 .schema();
289
290 pub const DATASTORE_SCHEMA: Schema = StringSchema::new("Datastore name.")
291 .format(&PROXMOX_SAFE_ID_FORMAT)
292 .min_length(3)
293 .max_length(32)
294 .schema();
295
296 pub const REMOTE_ID_SCHEMA: Schema = StringSchema::new("Remote ID.")
297 .format(&PROXMOX_SAFE_ID_FORMAT)
298 .min_length(3)
299 .max_length(32)
300 .schema();
301
302 pub const SINGLE_LINE_COMMENT_SCHEMA: Schema = StringSchema::new("Comment (single line).")
303 .format(&SINGLE_LINE_COMMENT_FORMAT)
304 .schema();
305
306 pub const HOSTNAME_SCHEMA: Schema = StringSchema::new("Hostname (as defined in RFC1123).")
307 .format(&HOSTNAME_FORMAT)
308 .schema();
309
310 pub const DNS_NAME_OR_IP_SCHEMA: Schema = StringSchema::new("DNS name or IP address.")
311 .format(&DNS_NAME_OR_IP_FORMAT)
312 .schema();
313
314 pub const PROXMOX_AUTH_REALM_SCHEMA: Schema = StringSchema::new("Authentication domain ID")
315 .format(&PROXMOX_SAFE_ID_FORMAT)
316 .min_length(3)
317 .max_length(32)
318 .schema();
319
320 pub const PROXMOX_USER_ID_SCHEMA: Schema = StringSchema::new("User ID")
321 .format(&PROXMOX_USER_ID_FORMAT)
322 .min_length(3)
323 .max_length(64)
324 .schema();
325
326 pub const PROXMOX_GROUP_ID_SCHEMA: Schema = StringSchema::new("Group ID")
327 .format(&PROXMOX_GROUP_ID_FORMAT)
328 .min_length(3)
329 .max_length(64)
330 .schema();
331
332
333 // Complex type definitions
334
335 #[api(
336 properties: {
337 "backup-type": {
338 schema: BACKUP_TYPE_SCHEMA,
339 },
340 "backup-id": {
341 schema: BACKUP_ID_SCHEMA,
342 },
343 "last-backup": {
344 schema: BACKUP_TIME_SCHEMA,
345 },
346 "backup-count": {
347 type: Integer,
348 },
349 files: {
350 items: {
351 schema: BACKUP_ARCHIVE_NAME_SCHEMA
352 },
353 },
354 },
355 )]
356 #[derive(Serialize, Deserialize)]
357 #[serde(rename_all="kebab-case")]
358 /// Basic information about a backup group.
359 pub struct GroupListItem {
360 pub backup_type: String, // enum
361 pub backup_id: String,
362 pub last_backup: i64,
363 /// Number of contained snapshots
364 pub backup_count: u64,
365 /// List of contained archive files.
366 pub files: Vec<String>,
367 }
368
369 #[api(
370 properties: {
371 "backup-type": {
372 schema: BACKUP_TYPE_SCHEMA,
373 },
374 "backup-id": {
375 schema: BACKUP_ID_SCHEMA,
376 },
377 "backup-time": {
378 schema: BACKUP_TIME_SCHEMA,
379 },
380 files: {
381 items: {
382 schema: BACKUP_ARCHIVE_NAME_SCHEMA
383 },
384 },
385 },
386 )]
387 #[derive(Serialize, Deserialize)]
388 #[serde(rename_all="kebab-case")]
389 /// Basic information about backup snapshot.
390 pub struct SnapshotListItem {
391 pub backup_type: String, // enum
392 pub backup_id: String,
393 pub backup_time: i64,
394 /// List of contained archive files.
395 pub files: Vec<String>,
396 /// Overall snapshot size (sum of all archive sizes).
397 #[serde(skip_serializing_if="Option::is_none")]
398 pub size: Option<u64>,
399 }
400
401 #[api(
402 properties: {
403 "filename": {
404 schema: BACKUP_ARCHIVE_NAME_SCHEMA,
405 },
406 },
407 )]
408 #[derive(Serialize, Deserialize)]
409 #[serde(rename_all="kebab-case")]
410 /// Basic information about archive files inside a backup snapshot.
411 pub struct BackupContent {
412 pub filename: String,
413 /// Archive size (from backup manifest).
414 #[serde(skip_serializing_if="Option::is_none")]
415 pub size: Option<u64>,
416 }
417
418 #[api(
419 properties: {
420 "upid": {
421 optional: true,
422 schema: UPID_SCHEMA,
423 },
424 },
425 )]
426 #[derive(Clone, Serialize, Deserialize)]
427 #[serde(rename_all="kebab-case")]
428 /// Garbage collection status.
429 pub struct GarbageCollectionStatus {
430 pub upid: Option<String>,
431 /// Number of processed index files.
432 pub index_file_count: usize,
433 /// Sum of bytes referred by index files.
434 pub index_data_bytes: u64,
435 /// Bytes used on disk.
436 pub disk_bytes: u64,
437 /// Chunks used on disk.
438 pub disk_chunks: usize,
439 /// Sum of removed bytes.
440 pub removed_bytes: u64,
441 /// Number of removed chunks.
442 pub removed_chunks: usize,
443 /// Sum of pending bytes (pending removal - kept for safety).
444 pub pending_bytes: u64,
445 /// Number of pending chunks (pending removal - kept for safety).
446 pub pending_chunks: usize,
447 }
448
449 impl Default for GarbageCollectionStatus {
450 fn default() -> Self {
451 GarbageCollectionStatus {
452 upid: None,
453 index_file_count: 0,
454 index_data_bytes: 0,
455 disk_bytes: 0,
456 disk_chunks: 0,
457 removed_bytes: 0,
458 removed_chunks: 0,
459 pending_bytes: 0,
460 pending_chunks: 0,
461 }
462 }
463 }
464
465
466 #[api()]
467 #[derive(Serialize, Deserialize)]
468 /// Storage space usage information.
469 pub struct StorageStatus {
470 /// Total space (bytes).
471 pub total: u64,
472 /// Used space (bytes).
473 pub used: u64,
474 /// Available space (bytes).
475 pub avail: u64,
476 }
477
478 #[api(
479 properties: {
480 "upid": { schema: UPID_SCHEMA },
481 },
482 )]
483 #[derive(Serialize, Deserialize)]
484 /// Task properties.
485 pub struct TaskListItem {
486 pub upid: String,
487 /// The node name where the task is running on.
488 pub node: String,
489 /// The Unix PID
490 pub pid: i64,
491 /// The task start time (Epoch)
492 pub pstart: u64,
493 /// The task start time (Epoch)
494 pub starttime: i64,
495 /// Worker type (arbitrary ASCII string)
496 pub worker_type: String,
497 /// Worker ID (arbitrary ASCII string)
498 pub worker_id: Option<String>,
499 /// The user who started the task
500 pub user: String,
501 /// The task end time (Epoch)
502 #[serde(skip_serializing_if="Option::is_none")]
503 pub endtime: Option<i64>,
504 /// Task end status
505 #[serde(skip_serializing_if="Option::is_none")]
506 pub status: Option<String>,
507 }
508
509 #[api()]
510 #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
511 #[serde(rename_all = "lowercase")]
512 /// Interface configuration method
513 pub enum NetworkConfigMethod {
514 /// Configuration is done manually using other tools
515 Manual,
516 /// Define interfaces with statically allocated addresses.
517 Static,
518 /// Obtain an address via DHCP
519 DHCP,
520 /// Define the loopback interface.
521 Loopback,
522 }
523
524 pub const NETWORK_INTERFACE_NAME_SCHEMA: Schema = StringSchema::new("Network interface name.")
525 .format(&NETWORK_INTERFACE_FORMAT)
526 .min_length(1)
527 .max_length(libc::IFNAMSIZ-1)
528 .schema();
529
530 #[api(
531 properties: {
532 name: {
533 schema: NETWORK_INTERFACE_NAME_SCHEMA,
534 },
535 method_v4: {
536 type: NetworkConfigMethod,
537 optional: true,
538 },
539 method_v6: {
540 type: NetworkConfigMethod,
541 optional: true,
542 },
543 options_v4: {
544 description: "Option list (inet)",
545 type: Array,
546 items: {
547 description: "Optional attribute line.",
548 type: String,
549 },
550 },
551 options_v6: {
552 description: "Option list (inet6)",
553 type: Array,
554 items: {
555 description: "Optional attribute line.",
556 type: String,
557 },
558 },
559 }
560 )]
561 #[derive(Debug, Serialize, Deserialize)]
562 /// Network Interface configuration
563 pub struct Interface {
564 /// Autostart interface
565 pub autostart: bool,
566 /// Interface is a physical network device
567 pub exists: bool,
568 /// Interface is active (UP)
569 pub active: bool,
570 /// Interface name
571 pub name: String,
572 #[serde(skip_serializing_if="Option::is_none")]
573 pub method_v4: Option<NetworkConfigMethod>,
574 #[serde(skip_serializing_if="Option::is_none")]
575 pub method_v6: Option<NetworkConfigMethod>,
576 #[serde(skip_serializing_if="Option::is_none")]
577 /// IPv4 address with netmask
578 pub cidr_v4: Option<String>,
579 #[serde(skip_serializing_if="Option::is_none")]
580 /// IPv4 gateway
581 pub gateway_v4: Option<String>,
582 #[serde(skip_serializing_if="Option::is_none")]
583 /// IPv6 address with netmask
584 pub cidr_v6: Option<String>,
585 #[serde(skip_serializing_if="Option::is_none")]
586 /// IPv6 gateway
587 pub gateway_v6: Option<String>,
588 #[serde(skip_serializing_if="Vec::is_empty")]
589 pub options_v4: Vec<String>,
590 #[serde(skip_serializing_if="Vec::is_empty")]
591 pub options_v6: Vec<String>,
592 }
593
594 // Regression tests
595
596 #[test]
597 fn test_cert_fingerprint_schema() -> Result<(), anyhow::Error> {
598
599 let schema = CERT_FINGERPRINT_SHA256_SCHEMA;
600
601 let invalid_fingerprints = [
602 "86:88:7c:be:26:77:a5:62:67:d9:06:f5:e4::61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
603 "88:7C:BE:26:77:a5:62:67:D9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
604 "86:88:7c:be:26:77:a5:62:67:d9:06:f5:e4::14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8:ff",
605 "XX:88:7c:be:26:77:a5:62:67:d9:06:f5:e4::14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
606 "86:88:Y4:be:26:77:a5:62:67:d9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
607 "86:88:0:be:26:77:a5:62:67:d9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
608 ];
609
610 for fingerprint in invalid_fingerprints.iter() {
611 if let Ok(_) = parse_simple_value(fingerprint, &schema) {
612 bail!("test fingerprint '{}' failed - got Ok() while expection an error.", fingerprint);
613 }
614 }
615
616 let valid_fingerprints = [
617 "86:88:7c:be:26:77:a5:62:67:d9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
618 "86:88:7C:BE:26:77:a5:62:67:D9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
619 ];
620
621 for fingerprint in valid_fingerprints.iter() {
622 let v = match parse_simple_value(fingerprint, &schema) {
623 Ok(v) => v,
624 Err(err) => {
625 bail!("unable to parse fingerprint '{}' - {}", fingerprint, err);
626 }
627 };
628
629 if v != serde_json::json!(fingerprint) {
630 bail!("unable to parse fingerprint '{}' - got wrong value {:?}", fingerprint, v);
631 }
632 }
633
634 Ok(())
635 }
636
637 #[test]
638 fn test_proxmox_user_id_schema() -> Result<(), anyhow::Error> {
639
640 let schema = PROXMOX_USER_ID_SCHEMA;
641
642 let invalid_user_ids = [
643 "x", // too short
644 "xx", // too short
645 "xxx", // no realm
646 "xxx@", // no realm
647 "xx x@test", // contains space
648 "xx\nx@test", // contains control character
649 "x:xx@test", // contains collon
650 "xx/x@test", // contains slash
651 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@test", // too long
652 ];
653
654 for name in invalid_user_ids.iter() {
655 if let Ok(_) = parse_simple_value(name, &schema) {
656 bail!("test userid '{}' failed - got Ok() while expection an error.", name);
657 }
658 }
659
660 let valid_user_ids = [
661 "xxx@y",
662 "name@y",
663 "xxx@test-it.com",
664 "xxx@_T_E_S_T-it.com",
665 "x_x-x.x@test-it.com",
666 ];
667
668 for name in valid_user_ids.iter() {
669 let v = match parse_simple_value(name, &schema) {
670 Ok(v) => v,
671 Err(err) => {
672 bail!("unable to parse userid '{}' - {}", name, err);
673 }
674 };
675
676 if v != serde_json::json!(name) {
677 bail!("unable to parse userid '{}' - got wrong value {:?}", name, v);
678 }
679 }
680
681 Ok(())
682 }