use std::borrow::Cow;
use std::collections::HashMap;
use std::future::{Future, ready};
use std::hint::black_box;
use std::net::{IpAddr, Ipv4Addr};
use std::sync::Arc;
use futures_rustls::pki_types::UnixTime;
use hyper::header::{AUTHORIZATION, HeaderValue, WWW_AUTHENTICATE};
use hyper::http::Extensions;
use hyper::http::request::Parts;
use hyper::{HeaderMap, StatusCode};
use ostrya::access::{AuthItem, Claims, ClientAccess, ClientGroup, IpNet, Namespace, ReadAccess};
use ostrya::remote::proto::Hello;
use ostrya::{Checksum, ReceivePolicies, ReceivePolicy};
use crate::cert::{ClientCert, ForwardedCerts};
use crate::event::{EventSink, ServerEvent};
use crate::forwarded::{Forwarding, Origin};
use crate::receive_auth::{ReceiveAuth, Refusal, RequestKind, SessionSetup};
const CHALLENGES: [&str; 2] = [r#"Bearer realm="ostrya""#, r#"Basic realm="ostrya""#];
#[derive(Clone, Debug)]
pub(crate) struct Peer {
pub(crate) tls: bool,
pub(crate) addr: IpAddr,
pub(crate) cert: Option<Arc<ClientCert>>,
}
#[cfg(test)]
thread_local! {
static COMPARES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
fn digest_eq(a: &[u8; 32], b: &[u8; 32]) -> bool {
#[cfg(test)]
COMPARES.with(|n| n.set(n.get() + 1));
let (a, _) = a.as_chunks::<8>();
let (b, _) = b.as_chunks::<8>();
let mut diff = 0u64;
for (x, y) in a.iter().zip(b) {
diff |= black_box(u64::from_ne_bytes(*x) ^ u64::from_ne_bytes(*y));
}
black_box(diff) == 0
}
#[derive(Debug)]
struct TokenEntry {
digest: [u8; 32],
client: Client,
}
#[derive(Debug)]
struct BasicEntry {
user: [u8; 32],
password: [u8; 32],
client: Client,
}
#[derive(Debug, Default)]
struct CredentialIndex {
tokens: Vec<TokenEntry>,
basics: Vec<BasicEntry>,
tls_sha256: HashMap<[u8; 32], Client>,
tls_names: HashMap<String, Client>,
tls_any: Option<Client>,
nets: Vec<(IpNet, Client)>,
anonymous: Option<Client>,
}
impl CredentialIndex {
fn new(access: &ClientAccess) -> CredentialIndex {
let mut index = CredentialIndex::default();
for item in access.items() {
let claims = item.is_tls() && access.groups_of(item).any(ClientGroup::tls_accept_claim);
let client = Client::new(item.clone(), claims);
match item {
AuthItem::Anonymous => index.anonymous = Some(client),
AuthItem::TlsAny => index.tls_any = Some(client),
AuthItem::TlsName(name) => {
index.tls_names.insert(name.clone(), client);
}
AuthItem::TlsSha256(digest) => {
index.tls_sha256.insert(*digest, client);
}
AuthItem::TokenSha256(digest) => index.tokens.push(TokenEntry {
digest: *digest,
client,
}),
AuthItem::BasicSha256 { user, digest } => index.basics.push(BasicEntry {
user: *Checksum::sha256(user.as_bytes()).as_bytes(),
password: *digest,
client,
}),
AuthItem::Ssh(_) => {}
AuthItem::Net(net) => index.nets.push((*net, client)),
}
}
index
}
fn bearer(&self, token: &[u8]) -> Option<Client> {
if token.is_empty() {
return None;
}
let digest = *Checksum::sha256(token).as_bytes();
let mut found = None;
for entry in &self.tokens {
if digest_eq(&entry.digest, &digest) && found.is_none() {
found = Some(&entry.client);
}
}
found.cloned()
}
fn basic(&self, encoded: &[u8]) -> Option<Client> {
let encoded = std::str::from_utf8(encoded).ok()?;
let decoded = ostrya::base64::decode(encoded).ok()?;
let colon = decoded.iter().position(|&b| b == b':')?;
let user = std::str::from_utf8(&decoded[..colon]).ok()?;
let password = &decoded[colon + 1..];
if password.is_empty() {
return None;
}
let user = *Checksum::sha256(user.as_bytes()).as_bytes();
let password = *Checksum::sha256(password).as_bytes();
let mut found = None;
for entry in &self.basics {
let user_eq = digest_eq(&entry.user, &user);
let password_eq = digest_eq(&entry.password, &password);
if user_eq & password_eq && found.is_none() {
found = Some(&entry.client);
}
}
found.cloned()
}
fn certificate(&self, cert: &ClientCert) -> Option<Client> {
if let Some(client) = self.tls_sha256.get(cert.digest.as_bytes()) {
return Some(client.clone());
}
if let Some(cn) = &cert.cn
&& let Some(client) = self.tls_names.get(cn)
{
return Some(client.clone());
}
self.tls_any.clone()
}
fn net(&self, addr: Option<IpAddr>) -> Option<Client> {
let addr = addr?;
self.nets
.iter()
.filter(|(net, _)| net.contains(addr))
.max_by_key(|(net, _)| net.prefix_len())
.map(|(_, client)| client.clone())
}
fn takes_credentials(&self) -> bool {
!self.tokens.is_empty() || !self.basics.is_empty()
}
}
#[derive(Debug, Clone)]
pub(crate) struct Client {
item: Arc<AuthItem>,
owner: Arc<str>,
claims: bool,
cert: Option<Arc<ClientCert>>,
}
impl Client {
fn new(item: AuthItem, claims: bool) -> Client {
Client {
owner: item.to_string().into(),
item: Arc::new(item),
claims,
cert: None,
}
}
fn with_cert(mut self, cert: &Arc<ClientCert>) -> Client {
if self.claims {
self.owner = cert.owner.clone();
self.cert = Some(cert.clone());
}
self
}
}
pub(crate) struct GroupAuth {
cleartext: bool,
policies: Arc<ReceivePolicies>,
index: CredentialIndex,
forwarding: Option<Forwarding>,
forwarded_certs: Option<ForwardedCerts>,
on_event: Option<EventSink>,
}
impl GroupAuth {
pub(crate) fn new(policies: Arc<ReceivePolicies>, cleartext: bool) -> GroupAuth {
GroupAuth {
cleartext,
index: CredentialIndex::new(policies.access()),
policies,
forwarding: None,
forwarded_certs: None,
on_event: None,
}
}
pub(crate) fn with_proxies(
mut self,
forwarding: Option<Forwarding>,
certs: Option<ForwardedCerts>,
) -> GroupAuth {
self.forwarding = forwarding;
self.forwarded_certs = certs;
self
}
pub(crate) fn with_events(mut self, on_event: Option<EventSink>) -> GroupAuth {
self.on_event = on_event;
self
}
fn identify(&self, headers: &HeaderMap, peer: &Peer) -> Result<Client, Refusal> {
self.identify_at(headers, peer, UnixTime::now())
}
fn identify_at(
&self,
headers: &HeaderMap,
peer: &Peer,
now: UnixTime,
) -> Result<Client, Refusal> {
let (origin, trusted) = match &self.forwarding {
Some(forwarding) => (
forwarding.origin(headers, peer.addr),
forwarding.trusts(peer.addr),
),
None => (
Origin {
addr: Some(peer.addr),
https: false,
},
false,
),
};
let tls = peer.tls || origin.https;
let mut values = headers.get_all(AUTHORIZATION).iter();
let value = values.next();
if values.next().is_some() {
return Err(Refusal::unauthorized(
"the request has more than one Authorization header",
));
}
if let Some(value) = value {
let value = value.as_bytes();
let (scheme, rest) = match value.iter().position(|&b| b == b' ') {
Some(space) => (&value[..space], value[space..].trim_ascii_start()),
None => (value, &value[value.len()..]),
};
let bearer = scheme.eq_ignore_ascii_case(b"Bearer");
let basic = scheme.eq_ignore_ascii_case(b"Basic");
if (bearer || basic) && !tls && !self.cleartext {
return Err(Refusal::forbidden(
"the server takes no credential over plain HTTP",
));
}
let item = if bearer {
self.index.bearer(rest)
} else if basic {
self.index.basic(rest)
} else {
None
};
return item.ok_or_else(|| {
Refusal::unauthorized(
"the credential of the request matches no auth item of the server",
)
});
}
let forwarded;
let cert = match &self.forwarded_certs {
Some(certs) if trusted => {
forwarded = certs
.certificate(headers, now)
.map_err(|bad| Refusal::forbidden(bad.message()))?;
forwarded.as_ref()
}
_ => peer.cert.as_ref(),
};
if let Some(cert) = cert {
return match self.index.certificate(cert) {
Some(client) => Ok(client.with_cert(cert)),
None => Err(Refusal::forbidden(
"the client certificate of the request matches no auth item of the server",
)),
};
}
if let Some(client) = self.index.net(origin.addr) {
return Ok(client);
}
if let Some(client) = &self.index.anonymous {
return Ok(client.clone());
}
if self.index.takes_credentials() {
Err(Refusal::unauthorized("the request has no credential"))
} else {
Err(Refusal::forbidden(
"the request matches no auth item of the server",
))
}
}
pub(crate) fn identify_read(
&self,
headers: &HeaderMap,
peer: &Peer,
) -> Result<ReadAccess, Refusal> {
let client = self.identify(headers, peer).map_err(challenge)?;
let claims = match &client.cert {
Some(cert) => Some(self.claims_of(cert)?.clone()),
None => None,
};
Ok(ReadAccess::new(
self.policies.access().clone(),
client.item.clone(),
claims,
))
}
fn claims_of<'c>(&self, cert: &'c ClientCert) -> Result<&'c Arc<Claims>, Refusal> {
cert.claims.as_ref().map_err(|fault| {
if let Some(on_event) = &self.on_event {
on_event(&ServerEvent::BadClaims {
digest: cert.digest,
san: fault.san().map(str::to_owned),
reason: fault.reason(),
});
}
Refusal::forbidden(format!(
"the claims of the client certificate do not parse: {fault}"
))
})
}
fn setup(&self, client: &Client, hello: &Hello) -> Result<SessionSetup, Refusal> {
let access = self.policies.access();
let namespace = access.namespace(hello.collection.as_deref());
let Some((group, policy)) = self.policies.select(&client.item, &namespace) else {
let covered = match &namespace {
Namespace::Local => "the local refs".to_owned(),
Namespace::Collection(id) => format!("the collection '{id}'"),
};
return Err(Refusal::forbidden(format!(
"no client group of the request covers {covered}"
)));
};
let claims = match &client.cert {
Some(cert) if group.tls_accept_claim() => Some(self.claims_of(cert)?),
_ => None,
};
let claimed = match access.effective_rules(group, &namespace, claims.map(|c| &**c)) {
Cow::Owned(rules) => Some(rules),
Cow::Borrowed(_) => None,
};
let policy = match claimed {
Some(rules) if !rules.opens_push_session() => {
return Err(Refusal::forbidden(format!(
"the claims of the client certificate open no push session in client \
group '{}'",
group.name()
)));
}
Some(rules) => {
let ReceivePolicy {
scope,
rules: _,
verify,
allow_unsigned,
signers,
allow_privileged,
summary_signers,
update_summary,
group,
} = &**policy;
Arc::new(ReceivePolicy {
scope: scope.clone(),
rules,
verify: verify.clone(),
allow_unsigned: *allow_unsigned,
signers: signers.clone(),
allow_privileged: *allow_privileged,
summary_signers: summary_signers.clone(),
update_summary: *update_summary,
group: group.clone(),
})
}
None if !policy.rules.opens_push_session() => {
return Err(Refusal::forbidden(format!(
"client group '{}' opens no push session",
group.name()
)));
}
None => policy.clone(),
};
Ok(SessionSetup {
policy,
hooks: None,
})
}
}
pub(crate) fn peer_of(extensions: &Extensions) -> Peer {
let peer = extensions.get::<Peer>().cloned();
debug_assert!(peer.is_some(), "the server puts a peer into each request");
peer.unwrap_or(Peer {
tls: false,
addr: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
cert: None,
})
}
fn challenge(refusal: Refusal) -> Refusal {
if refusal.status != StatusCode::UNAUTHORIZED {
return refusal;
}
CHALLENGES.into_iter().fold(refusal, |refusal, challenge| {
refusal.with_header(WWW_AUTHENTICATE, HeaderValue::from_static(challenge))
})
}
impl ReceiveAuth for GroupAuth {
type Principal = Client;
fn owner(principal: &Client) -> &str {
&principal.owner
}
fn authenticate(
&self,
parts: &Parts,
_kind: RequestKind,
) -> impl Future<Output = Result<Client, Refusal>> + Send {
let peer = peer_of(&parts.extensions);
ready(self.identify(&parts.headers, &peer).map_err(challenge))
}
fn open(
&self,
client: &Client,
hello: &Hello,
) -> impl Future<Output = Result<SessionSetup, Refusal>> + Send {
ready(self.setup(client, hello))
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicU32, Ordering};
use hyper::header::HeaderValue;
use super::*;
use crate::session::tests::TmpRepo;
use crate::test_pki::TestCa;
fn hex(text: &str) -> String {
Checksum::sha256(text.as_bytes()).to_hex()
}
fn auth(groups: &str) -> (GroupAuth, TmpRepo) {
static N: AtomicU32 = AtomicU32::new(0);
let tmp = TmpRepo::new(&format!("auth-{}", N.fetch_add(1, Ordering::Relaxed)));
(GroupAuth::new(tmp.policies(groups), false), tmp)
}
fn credentials() -> String {
format!(
"[ex-ostrya client \"tokens\"]\n\
auth=token-sha256:{};token-sha256:{}\nallow=w:**\n\
[ex-ostrya client \"basics\"]\n\
auth=basic-sha256:alice:{};basic-sha256:bob:{}\nallow=w:**\n",
hex("a-token"),
hex("b-token"),
hex("a-pass"),
hex("pa:ss:"),
)
}
fn peer(tls: bool, addr: &str, cert: Option<ClientCert>) -> Peer {
Peer {
tls,
addr: addr.parse().unwrap(),
cert: cert.map(Arc::new),
}
}
fn plain() -> Peer {
peer(false, "192.0.2.1", None)
}
fn tls() -> Peer {
peer(true, "192.0.2.1", None)
}
fn cert(der: &str, cn: Option<&str>) -> ClientCert {
let digest = Checksum::sha256(der.as_bytes());
ClientCert {
owner: format!("tls-sha256:{digest}").into(),
digest,
cn: cn.map(str::to_owned),
not_after: None,
claims: Ok(Arc::new(ostrya::access::Claims::default())),
}
}
fn headers(values: &[&str]) -> HeaderMap {
let mut headers = HeaderMap::new();
for value in values {
headers.append(AUTHORIZATION, HeaderValue::from_str(value).unwrap());
}
headers
}
fn basic(user: &str, password: &str) -> String {
format!(
"Basic {}",
ostrya::base64::encode(format!("{user}:{password}").as_bytes())
)
}
fn item(auth: &GroupAuth, values: &[&str], peer: &Peer) -> String {
auth.identify(&headers(values), peer)
.unwrap()
.owner
.to_string()
}
fn refused(auth: &GroupAuth, values: &[&str], peer: &Peer) -> StatusCode {
auth.identify(&headers(values), peer).unwrap_err().status
}
fn counted<T>(f: impl FnOnce() -> T) -> (T, usize) {
COMPARES.with(|n| n.set(0));
let result = f();
(result, COMPARES.with(|n| n.get()))
}
#[test]
fn digests_compare_by_every_byte() {
let a = *Checksum::sha256(b"a").as_bytes();
assert!(digest_eq(&a, &a));
for i in 0..32 {
let mut b = a;
b[i] ^= 0x80;
assert!(!digest_eq(&a, &b), "byte {i}");
}
}
#[test]
fn the_index_holds_the_items_of_each_group() {
let (auth, _tmp) = auth(&format!(
"{}[ex-ostrya client \"other\"]\nauth=ssh:u;token-sha256:{};tls:*;net:10.0.0.0/8\n\
collection=*\n",
credentials(),
hex("a-token"),
));
let index = &auth.index;
assert_eq!(index.tokens.len(), 2);
assert_eq!(index.basics.len(), 2);
assert!(index.tls_any.is_some() && index.anonymous.is_none());
assert_eq!(index.nets.len(), 1);
assert!(index.tls_names.is_empty() && index.tls_sha256.is_empty());
}
#[test]
fn a_credential_gives_its_item_and_visits_every_item() {
let (auth, _tmp) = auth(&credentials());
for (value, expected) in [
(
"Bearer a-token".to_owned(),
format!("token-sha256:{}", hex("a-token")),
),
(
"bearer b-token".to_owned(),
format!("token-sha256:{}", hex("b-token")),
),
] {
let (found, compares) = counted(|| item(&auth, &[&value], &tls()));
assert_eq!((found, compares), (expected, 2), "{value}");
}
for (user, password) in [("alice", "a-pass"), ("bob", "pa:ss:")] {
let value = basic(user, password);
let (found, compares) = counted(|| item(&auth, &[&value], &tls()));
let expected = format!("basic-sha256:{user}:{}", hex(password));
assert_eq!((found, compares), (expected, 4), "{user}");
}
let upper = basic("alice", "a-pass").replacen("Basic", "BASIC", 1);
assert!(item(&auth, &[&upper], &tls()).starts_with("basic-sha256:alice:"));
}
#[test]
fn a_credential_that_matches_no_item_is_401() {
let (auth, _tmp) = auth(&format!(
"{}[ex-ostrya client \"others\"]\nauth=anonymous;tls:*;net:0.0.0.0/0\nallow=w:**\n",
credentials()
));
let with_cert = peer(true, "192.0.2.1", Some(cert("cert", Some("host"))));
for value in [
"Bearer other".to_owned(),
"Bearer".to_owned(),
"Bearer ".to_owned(),
basic("alice", "pa:ss:"),
basic("bob", "a-pass"),
basic("carol", "a-pass"),
basic("alice", ""),
"Basic !!!".to_owned(),
format!("Basic {}", ostrya::base64::encode(b"alice")),
format!("Basic {}", ostrya::base64::encode(b"\xff:a-pass")),
"Digest a-token".to_owned(),
"a-token".to_owned(),
] {
assert_eq!(
refused(&auth, &[&value], &with_cert),
StatusCode::UNAUTHORIZED,
"{value}"
);
}
for (user, password) in [("alice", "b-pass"), ("carol", "a-pass")] {
let value = basic(user, password);
let (status, compares) = counted(|| refused(&auth, &[&value], &tls()));
assert_eq!((status, compares), (StatusCode::UNAUTHORIZED, 4));
}
let (status, compares) = counted(|| refused(&auth, &["Bearer other"], &tls()));
assert_eq!((status, compares), (StatusCode::UNAUTHORIZED, 2));
}
#[test]
fn more_than_one_authorization_header_is_401() {
let (auth, _tmp) = auth(&credentials());
let two = headers(&["Bearer a-token", "Bearer a-token"]);
for peer in [tls(), plain()] {
let refusal = auth.identify(&two, &peer).unwrap_err();
assert_eq!(refusal.status, StatusCode::UNAUTHORIZED);
assert_eq!(
refusal.message,
"the request has more than one Authorization header"
);
}
}
#[test]
fn a_credential_over_plain_http_is_403() {
let (mut auth, _tmp) = auth(&format!(
"{}[ex-ostrya client \"anonymous\"]\nauth=anonymous\nallow=w:**\n",
credentials()
));
for value in [
"Bearer a-token".to_owned(),
"BEARER a-token".to_owned(),
basic("alice", "a-pass"),
] {
let (status, compares) = counted(|| refused(&auth, &[&value], &plain()));
assert_eq!((status, compares), (StatusCode::FORBIDDEN, 0), "{value}");
}
assert_eq!(refused(&auth, &["Digest x"], &plain()), 401);
assert_eq!(item(&auth, &[], &plain()), "anonymous");
auth.cleartext = true;
assert_eq!(
item(&auth, &["Bearer a-token"], &plain()),
format!("token-sha256:{}", hex("a-token"))
);
}
#[test]
fn a_certificate_gives_the_first_item_that_matches() {
let (auth, _tmp) = auth(&format!(
"[ex-ostrya client \"by-digest\"]\nauth=tls-sha256:{}\nallow=w:**\n\
[ex-ostrya client \"by-name\"]\nauth=tls:host\nallow=w:**\n\
[ex-ostrya client \"any\"]\nauth=tls:*\nallow=w:**\n",
hex("pinned"),
));
let with =
|der: &str, cn: Option<&str>| item(&auth, &[], &peer(true, "::1", Some(cert(der, cn))));
assert_eq!(
with("pinned", Some("host")),
format!("tls-sha256:{}", hex("pinned"))
);
assert_eq!(with("other", Some("host")), "tls:host");
assert_eq!(with("other", Some("Host")), "tls:*");
assert_eq!(with("other", None), "tls:*");
assert_eq!(refused(&auth, &[], &tls()), StatusCode::FORBIDDEN);
let (auth, _tmp) = self::auth(&format!(
"[ex-ostrya client \"by-name\"]\nauth=tls:host\nallow=w:**\n\
[ex-ostrya client \"others\"]\nauth=anonymous;net:0.0.0.0/0;token-sha256:{}\n\
allow=w:**\n",
hex("a-token"),
));
let unknown = peer(true, "192.0.2.1", Some(cert("other", Some("guest"))));
let refusal = auth.identify(&HeaderMap::new(), &unknown).unwrap_err();
assert_eq!(refusal.status, StatusCode::FORBIDDEN);
assert!(
refusal.message.contains("client certificate"),
"{refusal:?}"
);
assert_eq!(item(&auth, &[], &tls()), "net:0.0.0.0/0");
}
#[test]
fn a_net_item_with_the_longest_prefix_wins() {
let (auth, _tmp) = auth(
"[ex-ostrya client \"wide\"]\nauth=net:10.0.0.0/8;net:2001:db8::/32\nallow=w:**\n\
[ex-ostrya client \"narrow\"]\nauth=net:10.0.1.0/24;net:2001:db8::/56\nallow=w:**\n\
[ex-ostrya client \"anonymous\"]\nauth=anonymous\nallow=w:**\n",
);
for (addr, expected) in [
("10.0.1.7", "net:10.0.1.0/24"),
("10.0.2.7", "net:10.0.0.0/8"),
("::ffff:10.0.1.7", "net:10.0.1.0/24"),
("::ffff:10.9.0.1", "net:10.0.0.0/8"),
("2001:db8::1", "net:2001:db8::/56"),
("2001:db8:1::1", "net:2001:db8::/32"),
("192.0.2.1", "anonymous"),
("::1", "anonymous"),
] {
assert_eq!(
item(&auth, &[], &peer(false, addr, None)),
expected,
"{addr}"
);
}
}
#[test]
fn a_request_with_nothing_is_anonymous_401_or_403() {
let (auth, _tmp) = auth(&format!(
"{}[ex-ostrya client \"anonymous\"]\nauth=anonymous\nallow=w:**\n",
credentials()
));
assert_eq!(item(&auth, &[], &tls()), "anonymous");
let (auth, _tmp) = self::auth(&credentials());
assert_eq!(refused(&auth, &[], &tls()), StatusCode::UNAUTHORIZED);
let (auth, _tmp) =
self::auth("[ex-ostrya client \"c\"]\nauth=tls:*;ssh:u;net:10.0.0.0/8\nallow=w:**\n");
assert_eq!(refused(&auth, &[], &tls()), StatusCode::FORBIDDEN);
assert_eq!(refused(&auth, &[], &plain()), StatusCode::FORBIDDEN);
}
fn parts(values: &[&str], peer: Option<Peer>) -> Parts {
let mut builder = hyper::Request::builder();
for value in values {
builder = builder.header(AUTHORIZATION, *value);
}
if let Some(peer) = peer {
builder = builder.extension(peer);
}
builder.body(()).unwrap().into_parts().0
}
fn authenticate(auth: &GroupAuth, parts: &Parts) -> Result<Client, Refusal> {
ostrya_rt::block_on(auth.authenticate(parts, RequestKind::Open))
}
#[test]
fn authenticate_reads_the_peer_and_challenges_a_401_alone() {
let (auth, _tmp) = auth(&format!(
"{}[ex-ostrya client \"cert\"]\nauth=tls:*\nallow=w:**\n",
credentials()
));
let with_cert = peer(true, "192.0.2.1", Some(cert("cert", None)));
let client = authenticate(&auth, &parts(&[], Some(with_cert))).unwrap();
assert_eq!(GroupAuth::owner(&client), "tls:*");
let token = parts(&["Bearer a-token"], Some(tls()));
for kind in [
RequestKind::Open,
RequestKind::Have,
RequestKind::Objects,
RequestKind::Commit,
RequestKind::Delete,
] {
let client = ostrya_rt::block_on(auth.authenticate(&token, kind)).unwrap();
let expected = format!("token-sha256:{}", hex("a-token"));
assert_eq!(GroupAuth::owner(&client), expected);
}
let refusal = authenticate(&auth, &parts(&[], Some(tls()))).unwrap_err();
assert_eq!(refusal.status, StatusCode::UNAUTHORIZED);
let challenges: Vec<_> = CHALLENGES
.into_iter()
.map(|c| (WWW_AUTHENTICATE, HeaderValue::from_static(c)))
.collect();
assert_eq!(refusal.headers, challenges);
let refusal = authenticate(&auth, &parts(&["Bearer a-token"], Some(plain()))).unwrap_err();
assert_eq!(refusal.status, StatusCode::FORBIDDEN);
assert!(refusal.headers.is_empty());
}
#[test]
fn a_request_with_no_peer_is_plain_http() {
let (auth, _tmp) = auth(&credentials());
let no_peer = parts(&["Bearer a-token"], None);
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
authenticate(&auth, &no_peer)
}));
if cfg!(debug_assertions) {
assert!(result.is_err());
} else {
assert_eq!(result.unwrap().unwrap_err().status, StatusCode::FORBIDDEN);
}
}
#[test]
fn open_selects_the_group_of_the_item() {
let (auth, _tmp) = auth(
"[ex-ostrya client \"push\"]\nauth=anonymous;tls:*\nallow=w:**\n\
[ex-ostrya client \"read\"]\nauth=ssh:r;net:10.0.0.0/8\nallow=r:**\n\
[ex-ostrya client \"mirror\"]\nauth=tls:host\ncollection=*\nallow=w:**\n\
[ex-ostrya client \"os\"]\nauth=tls:host\ncollection=org.example.Os\n\
allow=w:os/**\n",
);
let hello_in = |collection: Option<&str>| Hello {
version: 1,
agent: None,
refs: vec!["main".into()],
collection: collection.map(str::to_owned),
one_way: false,
};
let hello = hello_in(None);
let open_in = |text: &str, hello: &Hello| {
let client = Client::new(AuthItem::parse(text).unwrap(), false);
ostrya_rt::block_on(auth.open(&client, hello))
};
let open = |text: &str| open_in(text, &hello);
for text in ["anonymous", "tls:*"] {
let setup = open(text).unwrap_or_else(|r| panic!("{text}: {r:?}"));
assert!(Arc::ptr_eq(
&setup.policy,
auth.policies.get("push").unwrap()
));
assert_eq!(setup.policy.group.as_deref(), Some("push"));
assert!(setup.hooks.is_none());
}
for (text, needle) in [
(
"net:10.0.0.0/8",
"client group 'read' opens no push session",
),
(
"tls:host",
"no client group of the request covers the local refs",
),
(
"tls:other",
"no client group of the request covers the local refs",
),
] {
let Err(refusal) = open(text) else {
panic!("{text} opens a session");
};
assert_eq!(refusal.status, StatusCode::FORBIDDEN, "{text}");
assert_eq!(refusal.message, needle, "{text}");
}
for (collection, group) in [("org.example.Os", "os"), ("org.example.Other", "mirror")] {
let setup = open_in("tls:host", &hello_in(Some(collection)))
.unwrap_or_else(|r| panic!("{collection}: {r:?}"));
assert!(Arc::ptr_eq(
&setup.policy,
auth.policies.get(group).unwrap()
));
}
for text in ["anonymous", "tls:other"] {
let Err(refusal) = open_in(text, &hello_in(Some("org.example.Os"))) else {
panic!("{text} opens a session in a collection");
};
assert_eq!(refusal.status, StatusCode::FORBIDDEN, "{text}");
assert_eq!(
refusal.message,
"no client group of the request covers the collection 'org.example.Os'"
);
}
}
const CA_PEM: &[u8] = include_bytes!("../../../tests/fixtures/tls/ca.pem");
const CLIENT_PEM: &str = include_str!("../../../tests/fixtures/tls/client.pem");
const SERVER_PEM: &str = include_str!("../../../tests/fixtures/tls/server.pem");
fn base64_der(pem: &str) -> String {
pem.lines()
.filter(|line| !line.starts_with("-----"))
.collect()
}
fn proxied(groups: &str) -> (GroupAuth, TmpRepo) {
let (auth, tmp) = auth(groups);
let forwarding = Forwarding::new(
vec![IpNet::parse("10.0.0.0/8").unwrap()],
ostrya::access::ForwardedHeader::XForwardedFor,
);
let verifier = ostrya_fetch::client_cert_verifier(CA_PEM).unwrap();
let certs = ForwardedCerts::new(
hyper::header::HeaderName::from_static("x-client-cert"),
verifier,
);
(auth.with_proxies(Some(forwarding), Some(certs)), tmp)
}
fn lines(lines: &[(&str, &str)]) -> HeaderMap {
let mut headers = HeaderMap::new();
for (name, value) in lines {
headers.append(
hyper::header::HeaderName::from_bytes(name.as_bytes()).unwrap(),
HeaderValue::from_str(value).unwrap(),
);
}
headers
}
fn proxy() -> Peer {
peer(false, "10.0.0.1", None)
}
fn stranger() -> Peer {
peer(false, "192.0.2.9", None)
}
#[test]
fn https_from_a_trusted_proxy_counts_as_tls() {
let (auth, _tmp) = proxied(&credentials());
let request = lines(&[
("authorization", "Bearer a-token"),
("x-forwarded-proto", "https"),
]);
let found = auth.identify(&request, &proxy()).unwrap();
assert_eq!(&*found.owner, format!("token-sha256:{}", hex("a-token")));
let refusal = auth.identify(&request, &stranger()).unwrap_err();
assert_eq!(refusal.status, StatusCode::FORBIDDEN);
assert!(refusal.message.contains("plain HTTP"), "{refusal:?}");
let http = lines(&[
("authorization", "Bearer a-token"),
("x-forwarded-proto", "http"),
]);
assert_eq!(auth.identify(&http, &proxy()).unwrap_err().status, 403);
}
#[test]
fn a_forwarded_certificate_matches_the_tls_items() {
let der = ostrya::base64::decode(&base64_der(CLIENT_PEM)).unwrap();
let by_digest = format!("tls-sha256:{}", Checksum::sha256(&der).to_hex());
for (groups, expected) in [
(
"[ex-ostrya client \"c\"]\nauth=tls:ostrya test\nallow=w:**\n\
[ex-ostrya client \"a\"]\nauth=anonymous\nallow=w:**\n"
.to_owned(),
"tls:ostrya test".to_owned(),
),
(
format!(
"[ex-ostrya client \"c\"]\nauth={by_digest};tls:*\nallow=w:**\n\
[ex-ostrya client \"a\"]\nauth=anonymous\nallow=w:**\n"
),
by_digest.clone(),
),
] {
let (auth, _tmp) = proxied(&groups);
let request = lines(&[("x-client-cert", &base64_der(CLIENT_PEM))]);
assert_eq!(&*auth.identify(&request, &proxy()).unwrap().owner, expected);
assert_eq!(
&*auth.identify(&request, &stranger()).unwrap().owner,
"anonymous"
);
}
}
#[test]
fn a_bad_forwarded_certificate_is_403() {
let (auth, _tmp) = proxied(&format!(
"{}[ex-ostrya client \"a\"]\nauth=anonymous;tls:*\nallow=w:**\n",
credentials()
));
let client = base64_der(CLIENT_PEM);
for (value, needle) in [
("!!!".to_owned(), "does not parse"),
(format!("{client},{client}"), "more than one"),
(base64_der(SERVER_PEM), "does not verify"),
] {
let request = lines(&[("x-client-cert", &value)]);
let refusal = auth.identify(&request, &proxy()).unwrap_err();
assert_eq!(refusal.status, StatusCode::FORBIDDEN, "{value}");
assert!(refusal.message.contains(needle), "{refusal:?}");
let with_token = lines(&[
("x-client-cert", &value),
("authorization", "Bearer a-token"),
("x-forwarded-proto", "https"),
]);
let found = auth.identify(&with_token, &proxy()).unwrap();
assert_eq!(&*found.owner, format!("token-sha256:{}", hex("a-token")));
assert_eq!(
&*auth.identify(&request, &stranger()).unwrap().owner,
"anonymous"
);
}
let empty = lines(&[("x-client-cert", "")]);
assert_eq!(
&*auth.identify(&empty, &proxy()).unwrap().owner,
"anonymous"
);
}
#[test]
fn a_forwarded_certificate_replaces_the_handshake() {
let groups = format!(
"[ex-ostrya client \"c\"]\nauth=tls:ostrya test\nallow=w:**\n\
[ex-ostrya client \"h\"]\nauth=tls-sha256:{}\nallow=w:**\n\
[ex-ostrya client \"a\"]\nauth=anonymous\nallow=w:**\n",
hex("handshake")
);
let (direct, _direct_tmp) = auth(&groups);
let (auth, _tmp) = proxied(&groups);
let handshake = peer(true, "10.0.0.1", Some(cert("handshake", Some("other"))));
let request = lines(&[("x-client-cert", &base64_der(CLIENT_PEM))]);
let by_handshake = format!("tls-sha256:{}", hex("handshake"));
assert_eq!(
&*auth.identify(&request, &handshake).unwrap().owner,
"tls:ostrya test"
);
for request in [HeaderMap::new(), lines(&[("x-client-cert", "")])] {
assert_eq!(
&*auth.identify(&request, &handshake).unwrap().owner,
"anonymous"
);
}
let untrusted = peer(true, "192.0.2.9", Some(cert("handshake", Some("other"))));
assert_eq!(
&*auth.identify(&request, &untrusted).unwrap().owner,
by_handshake
);
let forwarding = Forwarding::new(
vec![IpNet::parse("10.0.0.0/8").unwrap()],
ostrya::access::ForwardedHeader::XForwardedFor,
);
let auth = direct.with_proxies(Some(forwarding), None);
assert_eq!(
&*auth.identify(&request, &handshake).unwrap().owner,
by_handshake
);
}
#[test]
fn a_net_item_matches_the_forwarded_address() {
let (auth, _tmp) = proxied(
"[ex-ostrya client \"farm\"]\nauth=net:192.0.2.0/24\nallow=w:**\n\
[ex-ostrya client \"proxies\"]\nauth=net:10.0.0.0/8\nallow=w:**\n",
);
let request = |value: &str| lines(&[("x-forwarded-for", value)]);
let owner = |value: &str, peer: &Peer| {
auth.identify(&request(value), peer)
.map(|client| client.owner.to_string())
.map_err(|refusal| refusal.status)
};
assert_eq!(
owner("192.0.2.7, 10.0.0.2", &proxy()).unwrap(),
"net:192.0.2.0/24"
);
assert_eq!(owner("10.0.0.3", &proxy()).unwrap(), "net:10.0.0.0/8");
assert_eq!(owner("unknown", &proxy()), Err(StatusCode::FORBIDDEN));
assert_eq!(owner("", &proxy()).unwrap(), "net:10.0.0.0/8");
assert_eq!(owner("10.9.9.9", &stranger()).unwrap(), "net:192.0.2.0/24");
}
fn recording(auth: GroupAuth) -> (GroupAuth, Arc<std::sync::Mutex<Vec<ServerEvent>>>) {
let events = Arc::new(std::sync::Mutex::new(Vec::new()));
let sink = events.clone();
let auth = auth.with_events(Some(Arc::new(move |event: &ServerEvent| {
sink.lock().unwrap().push(event.clone());
})));
(auth, events)
}
fn hello() -> Hello {
Hello {
version: 1,
agent: None,
refs: vec!["main".into()],
collection: None,
one_way: false,
}
}
fn open_with(
auth: &GroupAuth,
headers: &HeaderMap,
peer: &Peer,
) -> Result<SessionSetup, Refusal> {
let client = auth.identify(headers, peer)?;
ostrya_rt::block_on(auth.open(&client, &hello()))
}
fn with_der(der: &[u8]) -> Peer {
peer(true, "192.0.2.1", Some(ClientCert::from_der(der)))
}
fn rule_texts(setup: &SessionSetup) -> (Vec<String>, Vec<String>) {
let rules = &setup.policy.rules;
(
rules.allow.iter().map(ToString::to_string).collect(),
rules.deny.iter().map(|p| p.as_str().to_owned()).collect(),
)
}
const CLAIMS_GROUP: &str = "[ex-ostrya client \"certs\"]\nauth=tls:*\n\
tls-accept-claim=true\nallow=r:**\nallow-privileged=true\n";
#[test]
fn the_claims_replace_the_rules_of_the_group() {
let (auth, _tmp) = auth(CLAIMS_GROUP);
let ca = TestCa::new();
let der = ca.leaf(
"c",
&[
"ostrya:ref:w:main",
"ostrya:collection:a.b:d:x",
"ostrya:ref:!main/secret",
],
);
let setup = open_with(&auth, &HeaderMap::new(), &with_der(&der)).unwrap();
assert_eq!(
rule_texts(&setup),
(vec!["w:main".to_owned()], vec!["main/secret".to_owned()])
);
let group = auth.policies.get("certs").unwrap();
assert!(!Arc::ptr_eq(&setup.policy, group));
assert_eq!(setup.policy.group.as_deref(), Some("certs"));
assert!(setup.policy.allow_privileged);
assert_eq!(group.rules.allow[0].to_string(), "r:**");
}
#[test]
fn with_no_claim_of_the_namespace_the_group_applies() {
let (auth, _tmp) = auth(&CLAIMS_GROUP.replace("allow=r:**", "allow=w:**"));
let ca = TestCa::new();
let group = auth.policies.get("certs").unwrap();
for uris in [
&[][..],
&["ostrya:collection:a.b:w:main", "https://example.com/"],
] {
let setup = open_with(&auth, &HeaderMap::new(), &with_der(&ca.leaf("c", uris)))
.unwrap_or_else(|r| panic!("{uris:?}: {r:?}"));
assert!(Arc::ptr_eq(&setup.policy, group), "{uris:?}");
}
}
#[test]
fn the_claims_of_the_collection_apply_in_its_namespace() {
let (auth, _tmp) = auth(&CLAIMS_GROUP.replace("auth=tls:*", "auth=tls:*\ncollection=*"));
let der = TestCa::new().leaf(
"c",
&[
"ostrya:ref:w:main",
"ostrya:collection:org.example.Os:w:os/**",
"ostrya:collection:org.example.Os:!os/secret",
"ostrya:collection:org.example.Other:d:x",
],
);
let client = auth.identify(&HeaderMap::new(), &with_der(&der)).unwrap();
let hello = Hello {
collection: Some("org.example.Os".into()),
..hello()
};
let setup = ostrya_rt::block_on(auth.open(&client, &hello)).unwrap();
assert_eq!(
rule_texts(&setup),
(vec!["w:os/**".to_owned()], vec!["os/secret".to_owned()])
);
assert_eq!(
setup.policy.scope,
ostrya::access::CollectionScope::AllOther
);
assert_eq!(setup.policy.group.as_deref(), Some("certs"));
}
#[test]
fn a_claim_of_the_own_collection_names_the_local_refs() {
static N: AtomicU32 = AtomicU32::new(0);
let tmp = TmpRepo::new(&format!("auth-own-{}", N.fetch_add(1, Ordering::Relaxed)));
let config = tmp.repo.path().join("config");
let text = std::fs::read_to_string(&config).unwrap();
let text = text.replacen("[core]\n", "[core]\ncollection-id=own.id\n", 1);
std::fs::write(&config, text).unwrap();
let auth = GroupAuth::new(tmp.policies(CLAIMS_GROUP), false);
let der = TestCa::new().leaf("c", &["ostrya:collection:own.id:t:release/**"]);
let setup = open_with(&auth, &HeaderMap::new(), &with_der(&der)).unwrap();
assert_eq!(
rule_texts(&setup),
(vec!["t:release/**".to_owned()], Vec::new())
);
}
#[test]
fn claims_with_no_push_mode_open_no_session() {
let (auth, _tmp) = auth(&CLAIMS_GROUP.replace("allow=r:**", "allow=w:**"));
let ca = TestCa::new();
for uris in [&["ostrya:ref:!secret"][..], &["ostrya:ref:main"]] {
let refusal = open_with(&auth, &HeaderMap::new(), &with_der(&ca.leaf("c", uris)))
.err()
.unwrap();
assert_eq!(refusal.status, StatusCode::FORBIDDEN);
assert_eq!(
refusal.message,
"the claims of the client certificate open no push session in client group \
'certs'"
);
}
}
#[test]
fn claims_that_do_not_parse_get_403_and_an_event() {
let (auth, _tmp) = auth(CLAIMS_GROUP);
let (auth, events) = recording(auth);
let ca = TestCa::new();
let many: Vec<String> = (0..=ostrya::access::MAX_CLAIMS)
.map(|i| format!("ostrya:ref:w:r{i}"))
.collect();
let many: Vec<&str> = many.iter().map(String::as_str).collect();
let san = crate::test_pki::uri_sans(&["ostrya:ref:w:main"]);
for (der, san, reason) in [
(
ca.leaf("c", &["ostrya:ref:w:main", "ostrya:ref:x:a"]),
Some("ostrya:ref:x:a"),
"the modes are one or more of r, w, c, t, and d",
),
(
ca.leaf("c", &["Ostrya:ref:w:main"]),
Some("Ostrya:ref:w:main"),
"a claim starts with ostrya:ref: or ostrya:collection:ID:",
),
(
ca.leaf("c", &["ostrya:collection:a.b:x:y"]),
Some("ostrya:collection:a.b:x:y"),
"the modes are one or more of r, w, c, t, and d",
),
(
ca.leaf("c", &many),
None,
"the certificate holds more than 256 claims",
),
(
ca.leaf_with("c", None, &[vec![0x30, 0x03, 0x86, 0x01, 0x80]]),
None,
"the subjectAltName extension of the certificate does not decode",
),
(
ca.leaf_with("c", None, &[san.clone(), san.clone()]),
None,
"the certificate holds more than one subjectAltName extension",
),
(
b"not a certificate".to_vec(),
None,
"the certificate does not decode",
),
] {
events.lock().unwrap().clear();
let peer = with_der(&der);
auth.identify(&HeaderMap::new(), &peer).unwrap();
let refusal = open_with(&auth, &HeaderMap::new(), &peer).err().unwrap();
assert_eq!(refusal.status, StatusCode::FORBIDDEN, "{san:?}");
let fault = match san {
Some(san) => format!("malformed claim '{san}': {reason}"),
None => reason.to_owned(),
};
assert_eq!(
refusal.message,
format!("the claims of the client certificate do not parse: {fault}")
);
let digest = Checksum::sha256(&der);
assert_eq!(
*events.lock().unwrap(),
[ServerEvent::BadClaims {
digest,
san: san.map(str::to_owned),
reason: reason.to_owned(),
}]
);
assert_eq!(
events.lock().unwrap()[0].to_string(),
format!("refused the client certificate tls-sha256:{digest}: {fault}")
);
}
}
#[test]
fn a_group_that_reads_no_claim_ignores_the_claims() {
let (auth, _tmp) = auth("[ex-ostrya client \"certs\"]\nauth=tls:*\nallow=w:**\n");
let (auth, events) = recording(auth);
let ca = TestCa::new();
for uris in [&["ostrya:ref:x:a"][..], &["ostrya:ref:r:main"]] {
let peer = with_der(&ca.leaf("c", uris));
let setup = open_with(&auth, &HeaderMap::new(), &peer).unwrap();
assert!(Arc::ptr_eq(
&setup.policy,
auth.policies.get("certs").unwrap()
));
let client = auth.identify(&HeaderMap::new(), &peer).unwrap();
assert_eq!(&*client.owner, "tls:*");
assert!(client.cert.is_none());
}
assert!(events.lock().unwrap().is_empty());
}
#[test]
fn an_item_other_than_a_certificate_takes_no_claims() {
let (auth, _tmp) = auth(&format!(
"[ex-ostrya client \"mixed\"]\nauth=tls-sha256:{};token-sha256:{}\n\
tls-accept-claim=true\nallow=w:**\n\
[ex-ostrya client \"anonymous\"]\nauth=anonymous\nallow=w:**\n",
hex("unused"),
hex("a-token")
));
let (auth, events) = recording(auth);
let der = TestCa::new().leaf("c", &["ostrya:ref:x:a"]);
let peer = with_der(&der);
let token = headers(&["Bearer a-token"]);
let setup = open_with(&auth, &token, &peer).unwrap();
assert!(Arc::ptr_eq(
&setup.policy,
auth.policies.get("mixed").unwrap()
));
let client = auth.identify(&token, &peer).unwrap();
assert_eq!(&*client.owner, format!("token-sha256:{}", hex("a-token")));
let setup = open_with(&auth, &HeaderMap::new(), &tls()).unwrap();
assert!(Arc::ptr_eq(
&setup.policy,
auth.policies.get("anonymous").unwrap()
));
assert!(events.lock().unwrap().is_empty());
}
#[test]
fn the_owner_key_is_the_digest_of_a_certificate_with_claims() {
let (auth, _tmp) = auth(CLAIMS_GROUP);
let ca = TestCa::new();
let a = ca.leaf("a", &["ostrya:ref:w:a"]);
let b = ca.leaf("b", &["ostrya:ref:w:b"]);
let owner = |der: &[u8]| {
let client = auth.identify(&HeaderMap::new(), &with_der(der)).unwrap();
GroupAuth::owner(&client).to_owned()
};
assert_eq!(owner(&a), format!("tls-sha256:{}", Checksum::sha256(&a)));
assert_ne!(owner(&a), owner(&b));
}
#[test]
fn a_name_with_a_space_shares_no_owner_key_with_a_digest() {
let ca = TestCa::new();
let pinned = ca.leaf("pinned", &[]);
let digest = Checksum::sha256(&pinned);
let (auth, _tmp) = auth(&format!(
"{CLAIMS_GROUP}[ex-ostrya client \"named\"]\n\
auth=tls:* tls-sha256:{digest}\nallow=r:**\n"
));
let named = ca.leaf(&format!("* tls-sha256:{digest}"), &[]);
let owner = |der: &[u8]| {
let client = auth.identify(&HeaderMap::new(), &with_der(der)).unwrap();
GroupAuth::owner(&client).to_owned()
};
assert_eq!(owner(&named), format!("tls:* tls-sha256:{digest}"));
assert_eq!(owner(&pinned), format!("tls-sha256:{digest}"));
assert_ne!(owner(&named), owner(&pinned));
}
#[test]
fn a_forwarded_certificate_takes_its_claims() {
let ca = TestCa::new();
let (auth, _tmp) = auth(CLAIMS_GROUP);
let forwarding = Forwarding::new(
vec![IpNet::parse("10.0.0.0/8").unwrap()],
ostrya::access::ForwardedHeader::XForwardedFor,
);
let verifier = ostrya_fetch::client_cert_verifier(ca.pem.as_bytes()).unwrap();
let certs = ForwardedCerts::new(
hyper::header::HeaderName::from_static("x-client-cert"),
verifier,
);
let auth = auth.with_proxies(Some(forwarding), Some(certs));
let (auth, events) = recording(auth);
let good = ca.leaf("c", &["ostrya:ref:c:main"]);
let request = lines(&[("x-client-cert", &ostrya::base64::encode(&good))]);
let setup = open_with(&auth, &request, &proxy()).unwrap();
assert_eq!(rule_texts(&setup), (vec!["c:main".to_owned()], Vec::new()));
let bad = ca.leaf("c", &["ostrya:ref:c:main/"]);
let request = lines(&[("x-client-cert", &ostrya::base64::encode(&bad))]);
let refusal = open_with(&auth, &request, &proxy()).err().unwrap();
assert_eq!(refusal.status, StatusCode::FORBIDDEN);
let events = events.lock().unwrap();
assert_eq!(events.len(), 1);
assert!(
matches!(&events[0], ServerEvent::BadClaims { digest, san: Some(san), .. }
if *digest == Checksum::sha256(&bad) && san == "ostrya:ref:c:main/"),
"{events:?}"
);
}
#[test]
fn a_read_takes_the_steps_of_the_identification() {
let (auth, _tmp) = proxied(&format!(
"{}[ex-ostrya client \"farm\"]\nauth=net:198.51.100.0/24\nallow=r:farm/**\n\
[ex-ostrya client \"anonymous\"]\nauth=anonymous\nallow=r:pub/**\n",
credentials()
));
let refusal = auth
.identify_read(&headers(&["Bearer x", "Bearer y"]), &tls())
.unwrap_err();
assert_eq!(refusal.status, StatusCode::UNAUTHORIZED);
assert_eq!(refusal.headers.len(), 2);
let refusal = auth
.identify_read(&headers(&["Bearer a-token"]), &stranger())
.unwrap_err();
assert_eq!(refusal.status, StatusCode::FORBIDDEN);
assert!(refusal.headers.is_empty());
let read = auth.identify_read(&HeaderMap::new(), &stranger()).unwrap();
assert_eq!(read.item(), &AuthItem::Anonymous);
assert!(read.can_read_path("refs/heads/pub/a"));
assert!(!read.can_read_path("refs/heads/farm/a"));
let forwarded = lines(&[("x-forwarded-for", "198.51.100.7")]);
let read = auth.identify_read(&forwarded, &proxy()).unwrap();
assert_eq!(read.item().to_string(), "net:198.51.100.0/24");
assert!(read.can_read_path("refs/heads/farm/a"));
assert!(!read.can_read_path("refs/heads/pub/a"));
let read = auth
.identify_read(&headers(&["Bearer a-token"]), &tls())
.unwrap();
assert!(read.can_read_path("refs/heads/any"));
assert!(read.summary_digest().is_none());
let (auth, _tmp) = self::auth(&credentials());
let refusal = auth.identify_read(&HeaderMap::new(), &tls()).unwrap_err();
assert_eq!(refusal.status, StatusCode::UNAUTHORIZED);
assert_eq!(refusal.headers.len(), 2);
}
#[test]
fn claims_set_the_read_rights_and_bad_claims_refuse_a_read() {
let (auth, _tmp) = auth(CLAIMS_GROUP);
let (auth, events) = recording(auth);
let ca = TestCa::new();
let good = with_der(&ca.leaf("c", &["ostrya:ref:r:main"]));
let read = auth.identify_read(&HeaderMap::new(), &good).unwrap();
assert!(read.can_read_path("refs/heads/main"));
assert!(!read.can_read_path("refs/heads/other"));
let plain = with_der(&ca.leaf("c", &[]));
let read = auth.identify_read(&HeaderMap::new(), &plain).unwrap();
assert!(read.can_read_path("refs/heads/other"));
assert!(events.lock().unwrap().is_empty());
let der = ca.leaf("c", &["ostrya:ref:x:a"]);
let bad = with_der(&der);
for _ in 0..2 {
let refusal = auth.identify_read(&HeaderMap::new(), &bad).unwrap_err();
assert_eq!(refusal.status, StatusCode::FORBIDDEN);
assert!(refusal.headers.is_empty());
}
let events = events.lock().unwrap();
assert_eq!(events.len(), 2);
assert!(
matches!(&events[0], ServerEvent::BadClaims { digest, .. }
if *digest == Checksum::sha256(&der)),
"{events:?}"
);
}
#[test]
fn a_read_of_a_group_that_reads_no_claim_ignores_the_claims() {
let (auth, _tmp) = auth("[ex-ostrya client \"certs\"]\nauth=tls:*\nallow=r:**\n");
let (auth, events) = recording(auth);
let peer = with_der(&TestCa::new().leaf("c", &["ostrya:ref:x:a"]));
let read = auth.identify_read(&HeaderMap::new(), &peer).unwrap();
assert!(read.can_read_path("refs/heads/main"));
assert!(events.lock().unwrap().is_empty());
}
#[test]
fn an_expired_forwarded_certificate_is_403() {
let (auth, _tmp) = proxied("[ex-ostrya client \"c\"]\nauth=tls:*\nallow=w:**\n");
let request = lines(&[("x-client-cert", &base64_der(CLIENT_PEM))]);
let late = UnixTime::since_unix_epoch(std::time::Duration::from_secs(4_938_667_341));
let refusal = auth.identify_at(&request, &proxy(), late).unwrap_err();
assert_eq!(refusal.status, StatusCode::FORBIDDEN);
assert_eq!(&*auth.identify(&request, &proxy()).unwrap().owner, "tls:*");
}
}