195 lines
5.8 KiB
Rust
195 lines
5.8 KiB
Rust
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
//! The enrollment code (mandatory, tests first): it round-trips, typos are caught before anything
|
|
//! is sent, and it is single use: a second redeem is refused, as are expired and unknown codes.
|
|
|
|
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
|
|
|
mod common;
|
|
|
|
use common::*;
|
|
use legatus_protocol::{
|
|
CODE_TTL_SECONDS, CodeError, EnrollError, EnrollmentBook, EnrollmentCode, Roles,
|
|
};
|
|
|
|
const ROOT: [u8; 32] = [7; 32];
|
|
const SECRET: [u8; 16] = [0xa5; 16];
|
|
|
|
fn code() -> EnrollmentCode {
|
|
EnrollmentCode::new("https://fabrica.example:8443", ROOT, SECRET).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn a_code_round_trips_and_says_where_to_go() {
|
|
let c = code();
|
|
let text = c.encode();
|
|
assert!(text.starts_with("lgt1-"), "{text}");
|
|
assert!(text.len() < 200, "short enough to copy: {}", text.len());
|
|
assert!(
|
|
text[5..]
|
|
.bytes()
|
|
.all(|b| b.is_ascii_lowercase() || (b'2'..=b'7').contains(&b))
|
|
);
|
|
let back = EnrollmentCode::parse(&text).unwrap();
|
|
assert_eq!(back, c);
|
|
assert_eq!(back.server(), "https://fabrica.example:8443");
|
|
assert_eq!(back.root_sha256(), &ROOT);
|
|
assert_eq!(back.secret_hex(), "a5".repeat(16));
|
|
// Surrounding spaces from a copy are fine.
|
|
assert_eq!(EnrollmentCode::parse(&format!(" {text}\n")).unwrap(), c);
|
|
}
|
|
|
|
#[test]
|
|
fn the_secret_is_never_printed() {
|
|
let dbg = format!("{:?}", code());
|
|
assert!(!dbg.contains(&"a5".repeat(4)), "{dbg}");
|
|
assert!(dbg.contains("fabrica.example"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_typo_is_caught_before_anything_is_sent() {
|
|
let text = code().encode();
|
|
// Change one character in the middle.
|
|
let mut b = text.clone().into_bytes();
|
|
let i = b.len() / 2;
|
|
b[i] = if b[i] == b'a' { b'b' } else { b'a' };
|
|
assert_eq!(
|
|
EnrollmentCode::parse(&String::from_utf8(b).unwrap()),
|
|
Err(CodeError::Checksum)
|
|
);
|
|
// Cut short.
|
|
assert!(EnrollmentCode::parse(&text[..text.len() - 6]).is_err());
|
|
// Not a code at all.
|
|
assert_eq!(EnrollmentCode::parse("lgt2-aaaa"), Err(CodeError::Prefix));
|
|
assert_eq!(EnrollmentCode::parse("lgt1-!!!!"), Err(CodeError::Encoding));
|
|
assert_eq!(EnrollmentCode::parse("lgt1-"), Err(CodeError::Length));
|
|
assert_eq!(EnrollmentCode::parse(""), Err(CodeError::Prefix));
|
|
}
|
|
|
|
#[test]
|
|
fn only_a_plain_https_server_url_goes_in_a_code() {
|
|
for bad in [
|
|
"http://fabrica.example",
|
|
"https://",
|
|
"https://fabrica.example/path",
|
|
"https://user@fabrica.example",
|
|
"https://fabrica.example?x=1",
|
|
"https://fabrica.example#x",
|
|
"https://fabrica example",
|
|
"https://fabrica.example:99999",
|
|
"https://fabrica.example:",
|
|
"ftp://fabrica.example",
|
|
] {
|
|
assert!(
|
|
matches!(
|
|
EnrollmentCode::new(bad, ROOT, SECRET),
|
|
Err(CodeError::BadServer(_))
|
|
),
|
|
"{bad}"
|
|
);
|
|
}
|
|
let long = format!("https://{}.example", "a".repeat(200));
|
|
assert!(EnrollmentCode::new(&long, ROOT, SECRET).is_err());
|
|
for good in [
|
|
"https://fabrica.example",
|
|
"https://10.0.0.5:8443",
|
|
"https://[::1]:8443",
|
|
] {
|
|
EnrollmentCode::new(good, ROOT, SECRET).unwrap();
|
|
}
|
|
}
|
|
|
|
fn book() -> EnrollmentBook {
|
|
let mut b = EnrollmentBook::new();
|
|
let roles = Roles {
|
|
build: false,
|
|
deploy: true,
|
|
};
|
|
b.issue(SECRET, "acme", "web-1", roles, NOW, CODE_TTL_SECONDS)
|
|
.unwrap();
|
|
b
|
|
}
|
|
|
|
#[test]
|
|
fn a_code_is_single_use() {
|
|
let mut b = book();
|
|
let secret = code().secret_hex();
|
|
let g = b.redeem(&secret, NOW + 10).unwrap();
|
|
assert_eq!((g.team.as_str(), g.agent.as_str()), ("acme", "web-1"));
|
|
assert_eq!(
|
|
g.roles,
|
|
Roles {
|
|
build: false,
|
|
deploy: true
|
|
}
|
|
);
|
|
// The same code again, a second later or from another machine: refused.
|
|
assert_eq!(b.redeem(&secret, NOW + 11), Err(EnrollError::Used));
|
|
assert_eq!(b.redeem(&secret, NOW + 12).unwrap_err().code(), "code_used");
|
|
}
|
|
|
|
#[test]
|
|
fn an_expired_or_unknown_code_is_refused() {
|
|
let mut b = book();
|
|
let secret = code().secret_hex();
|
|
assert_eq!(
|
|
b.redeem(&secret, NOW + CODE_TTL_SECONDS),
|
|
Err(EnrollError::Expired)
|
|
);
|
|
// Still expired later, never accepted.
|
|
assert_eq!(
|
|
b.redeem(&secret, NOW + CODE_TTL_SECONDS + 60),
|
|
Err(EnrollError::Expired)
|
|
);
|
|
assert_eq!(b.redeem(&"00".repeat(16), NOW), Err(EnrollError::Unknown));
|
|
assert_eq!(b.redeem("not hex", NOW), Err(EnrollError::Unknown));
|
|
assert_eq!(
|
|
b.redeem(&"A5".repeat(16), NOW),
|
|
Err(EnrollError::Unknown),
|
|
"lowercase only"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn the_book_keeps_only_a_digest_of_the_secret() {
|
|
let b = book();
|
|
let stored = b.records().next().unwrap();
|
|
assert_eq!(stored.digest.len(), 64);
|
|
assert!(!stored.digest.contains(&"a5".repeat(16)));
|
|
assert_eq!(
|
|
stored.digest,
|
|
legatus_protocol::secret_digest(&"a5".repeat(16)).unwrap()
|
|
);
|
|
assert_eq!(stored.used_at, None);
|
|
}
|
|
|
|
#[test]
|
|
fn a_code_cannot_be_issued_twice_or_for_bad_ids() {
|
|
let mut b = book();
|
|
assert!(
|
|
b.issue(SECRET, "acme", "web-2", Roles::default(), NOW, 60)
|
|
.is_err(),
|
|
"same secret"
|
|
);
|
|
assert!(
|
|
b.issue([1; 16], "Acme", "web-2", Roles::default(), NOW, 60)
|
|
.is_err()
|
|
);
|
|
assert!(
|
|
b.issue([1; 16], "acme", "web 2", Roles::default(), NOW, 60)
|
|
.is_err()
|
|
);
|
|
assert!(
|
|
b.issue([1; 16], "acme", "web-2", Roles::default(), NOW, 0)
|
|
.is_err(),
|
|
"no lifetime"
|
|
);
|
|
assert!(
|
|
b.issue([1; 16], "acme", "web-2", Roles::default(), NOW, 86_401)
|
|
.is_err(),
|
|
"a day at most"
|
|
);
|
|
}
|