pub mod cert;
mod forget;
mod guard;
pub mod origin;
pub mod store;
#[cfg(feature = "network")]
pub mod tls;
#[cfg(all(test, any(feature = "hosting", feature = "network")))]
mod fixtures;
#[cfg(feature = "hosting")]
mod verify;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Mutex;
use gitpoulpi_config::{ConfigPaths, Integration};
pub use self::forget::{forget, ForgetReport};
pub use self::guard::GuardedTransport;
pub use self::origin::{api_origins, integration_origin, is_public_saas_host, normalize};
pub use self::store::{
AiRecord, CertInfo, IntegrationRecord, TlsMode, TlsRecord, TrustStore, TRUST_FILE,
};
#[cfg(feature = "hosting")]
pub use self::verify::VerifyReason;
use crate::{Error, Result};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TrustSubject {
Integration(String),
Ai(String),
Host(String),
}
impl TrustSubject {
pub fn name(&self) -> &str {
match self {
Self::Integration(s) | Self::Ai(s) | Self::Host(s) => s,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Account {
pub login: String,
pub id: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TrustProblem {
OriginChanged {
subject: TrustSubject,
old: String,
new: String,
},
AccountChanged {
subject: TrustSubject,
old: Box<Account>,
new: Box<Account>,
},
UntrustedCertificate {
subject: TrustSubject,
new: Box<CertInfo>,
},
CertificateChanged {
subject: TrustSubject,
old: Box<CertInfo>,
new: Box<CertInfo>,
},
PlainHttp {
subject: TrustSubject,
origin: String,
},
}
impl TrustProblem {
pub fn subject(&self) -> &TrustSubject {
match self {
Self::OriginChanged { subject, .. }
| Self::AccountChanged { subject, .. }
| Self::UntrustedCertificate { subject, .. }
| Self::CertificateChanged { subject, .. }
| Self::PlainHttp { subject, .. } => subject,
}
}
#[must_use]
pub fn with_subject(mut self, new_subject: TrustSubject) -> Self {
match &mut self {
Self::OriginChanged { subject, .. }
| Self::AccountChanged { subject, .. }
| Self::UntrustedCertificate { subject, .. }
| Self::CertificateChanged { subject, .. }
| Self::PlainHttp { subject, .. } => *subject = new_subject,
}
self
}
pub fn change(&self) -> &'static str {
match self {
Self::OriginChanged { .. } => "origin",
Self::AccountChanged { .. } => "account",
Self::UntrustedCertificate { .. } => "untrusted_certificate",
Self::CertificateChanged { .. } => "certificate",
Self::PlainHttp { .. } => "plain_http",
}
}
pub fn old_text(&self) -> String {
match self {
Self::OriginChanged { old, .. } => old.clone(),
Self::AccountChanged { old, .. } => format!("{} ({})", old.login, old.id),
Self::CertificateChanged { old, .. } => old.spki_sha256.clone(),
Self::UntrustedCertificate { .. } | Self::PlainHttp { .. } => String::new(),
}
}
pub fn new_text(&self) -> String {
match self {
Self::OriginChanged { new, .. } => new.clone(),
Self::AccountChanged { new, .. } => new.id.clone(),
Self::UntrustedCertificate { new, .. } | Self::CertificateChanged { new, .. } => {
new.spki_sha256.clone()
}
Self::PlainHttp { origin, .. } => origin.clone(),
}
}
pub fn fix(&self) -> Vec<String> {
let (cmd, name) = match self.subject() {
TrustSubject::Ai(p) => ("gitpoulpi ai trust".to_string(), p.clone()),
other => (
"gitpoulpi hosting integrations trust".to_string(),
other.name().to_string(),
),
};
let accept = match self {
Self::OriginChanged { new, .. } => format!("--accept-origin {new}"),
Self::AccountChanged { new, .. } => format!("--accept-account {}", new.id),
Self::UntrustedCertificate { new, .. } | Self::CertificateChanged { new, .. } => {
format!("--accept-certificate {}", new.spki_sha256)
}
Self::PlainHttp { .. } => "--allow-insecure-http on".to_string(),
};
vec![format!("{cmd} {name} {accept}")]
}
}
impl std::fmt::Display for TrustProblem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let who = self.subject().name();
match self {
Self::OriginChanged { old, new, .. } => write!(
f,
"`{who}` now points to {new}, it was {old}: nothing was sent; \
review it with `gitpoulpi hosting integrations trust`"
),
Self::AccountChanged { old, new, .. } => write!(
f,
"the token of `{who}` now belongs to {} (id {}), it was {} (id {})",
new.login, new.id, old.login, old.id
),
Self::UntrustedCertificate { new, .. } => write!(
f,
"`{who}` presents a certificate that is not signed by a known authority \
(key {}, {})",
new.spki_sha256, new.subject
),
Self::CertificateChanged { old, new, .. } => write!(
f,
"the certificate key of `{who}` changed: it was {}, it is {}",
old.spki_sha256, new.spki_sha256
),
Self::PlainHttp { origin, .. } => write!(
f,
"`{who}` uses {origin}, which is not encrypted: allow it explicitly to continue"
),
}
}
}
static PENDING: Mutex<Vec<TrustProblem>> = Mutex::new(Vec::new());
fn pending() -> std::sync::MutexGuard<'static, Vec<TrustProblem>> {
PENDING.lock().unwrap_or_else(|p| p.into_inner())
}
pub fn report(problem: &TrustProblem) {
let mut queue = pending();
let same =
|p: &TrustProblem| p.subject() == problem.subject() && p.change() == problem.change();
queue.retain(|p| !same(p));
queue.push(problem.clone());
}
pub fn take_pending() -> Vec<TrustProblem> {
std::mem::take(&mut *pending())
}
pub fn peek_pending() -> Vec<TrustProblem> {
pending().clone()
}
pub fn clear_pending(subject: &TrustSubject) {
pending().retain(|p| p.subject() != subject);
}
fn unix_now() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs())
}
#[derive(Debug, Clone)]
pub struct Admitted {
pub subject: TrustSubject,
pub record: IntegrationRecord,
pub origins: Vec<String>,
}
pub struct TrustGate {
file: Option<PathBuf>,
mem: Mutex<TrustStore>,
provisional: Mutex<HashMap<String, IntegrationRecord>>,
provisional_ai: Mutex<HashMap<String, AiRecord>>,
lenient: bool,
}
fn lock<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
m.lock().unwrap_or_else(|p| p.into_inner())
}
impl TrustGate {
fn new(file: Option<PathBuf>, lenient: bool) -> Self {
Self {
file,
mem: Mutex::new(TrustStore::default()),
provisional: Mutex::default(),
provisional_ai: Mutex::default(),
lenient,
}
}
pub fn load(paths: &ConfigPaths) -> Self {
Self::new(Some(TrustStore::file_in(paths)), false)
}
pub fn ambient() -> Self {
match ConfigPaths::discover() {
Some(paths) => Self::load(&paths),
None => Self::new(None, false),
}
}
#[cfg(any(test, feature = "test-support"))]
pub fn for_tests() -> Self {
Self::new(None, true)
}
#[cfg(any(test, feature = "test-support"))]
pub fn for_tests_in(paths: &ConfigPaths) -> Self {
Self::new(Some(TrustStore::file_in(paths)), true)
}
fn store(&self) -> Result<TrustStore> {
match &self.file {
Some(path) => TrustStore::load(path).map_err(Error::TrustStore),
None => Ok(lock(&self.mem).clone()),
}
}
fn save(&self, store: &TrustStore) -> Result<()> {
match &self.file {
Some(path) => store.save(path).map_err(|e| {
Error::TrustStore(format!("{} could not be written: {e}", path.display()))
}),
None => {
*lock(&self.mem) = store.clone();
Ok(())
}
}
}
fn fresh_record(&self, integration: &Integration, origin: String) -> IntegrationRecord {
let user = integration.user.as_ref();
IntegrationRecord {
id: integration.id.clone(),
origin,
account_id: user.map(|u| u.id.clone()).unwrap_or_default(),
account_login: user.map(|u| u.login.clone()).unwrap_or_default(),
recorded_at: unix_now(),
account_checked_at: 0,
allow_plain_http: self.lenient,
tls: None,
}
}
fn refuse<T>(&self, problem: TrustProblem) -> Result<T> {
report(&problem);
Err(Error::Trust(problem))
}
pub fn admit(&self, integration: &Integration) -> Result<Admitted> {
let origin = integration_origin(integration)?;
let subject = TrustSubject::Integration(integration.id.clone());
let mut store = self.store()?;
let record = match store.integration(&integration.id) {
Some(r) => r.clone(),
None => {
let record = lock(&self.provisional)
.get(&integration.id)
.cloned()
.unwrap_or_else(|| self.fresh_record(integration, origin.clone()));
if integration.user.is_some() {
store.put_integration(record.clone());
self.save(&store)?;
} else {
lock(&self.provisional).insert(integration.id.clone(), record.clone());
}
record
}
};
if origin::is_plain_http_remote(&origin) && !record.allow_plain_http {
return self.refuse(TrustProblem::PlainHttp {
subject,
origin: origin.clone(),
});
}
if record.origin != origin {
return self.refuse(TrustProblem::OriginChanged {
subject,
old: record.origin.clone(),
new: origin,
});
}
let origins = api_origins(integration.kind, &record.origin);
apply_tls_policy(&record.origin, record.tls.as_ref());
Ok(Admitted {
subject,
record,
origins,
})
}
pub fn adopt(&self, integration: &Integration) -> Result<()> {
if self.record_of(&integration.id).is_some() {
return Ok(());
}
self.update_record(integration, |_| ())
}
pub fn preflight(&self, integration: &Integration) -> Result<()> {
self.admit(integration).map(|_| ())
}
pub fn record_of(&self, id: &str) -> Option<IntegrationRecord> {
self.store().ok()?.integration(id).cloned()
}
pub fn update_record<R>(
&self,
integration: &Integration,
change: impl FnOnce(&mut IntegrationRecord) -> R,
) -> Result<R> {
let origin = integration_origin(integration)?;
let mut store = self.store()?;
let mut record = match store.integration(&integration.id) {
Some(r) => r.clone(),
None => lock(&self.provisional)
.remove(&integration.id)
.unwrap_or_else(|| self.fresh_record(integration, origin)),
};
let out = change(&mut record);
apply_tls_policy(&record.origin, record.tls.as_ref());
store.put_integration(record);
self.save(&store)?;
lock(&self.provisional).remove(&integration.id);
clear_pending(&TrustSubject::Integration(integration.id.clone()));
Ok(out)
}
pub fn record_after_check(&self, integration: &Integration, account: &Account) -> Result<()> {
let origin = integration_origin(integration)?;
self.update_record(integration, |r| {
if r.origin != origin {
r.origin = origin;
}
r.account_id = account.id.clone();
r.account_login = account.login.clone();
r.account_checked_at = unix_now();
})
}
pub fn trust_origin(&self, integration: &Integration) -> Result<()> {
let origin = integration_origin(integration)?;
self.update_record(integration, |r| {
if r.origin != origin {
r.origin = origin;
r.account_id.clear();
r.account_login.clear();
r.account_checked_at = 0;
r.tls = None;
}
})
}
pub fn trust_account(&self, integration: &Integration, account: &Account) -> Result<()> {
self.update_record(integration, |r| {
r.account_id = account.id.clone();
r.account_login = account.login.clone();
r.account_checked_at = unix_now();
})
}
pub fn trust_certificate(
&self,
integration: &Integration,
cert: &CertInfo,
mode: TlsMode,
) -> Result<()> {
self.update_record(integration, |r| {
r.tls = Some(TlsRecord {
mode,
cert: cert.clone(),
});
})
}
pub fn set_strict(&self, integration: &Integration, strict: bool) -> Result<bool> {
self.update_record(integration, |r| match &mut r.tls {
Some(tls) => {
tls.mode = if strict {
TlsMode::Strict
} else {
TlsMode::Tofu
};
true
}
None => false,
})
}
pub fn set_allow_plain_http(&self, integration: &Integration, on: bool) -> Result<()> {
self.update_record(integration, |r| r.allow_plain_http = on)
}
fn fresh_ai(&self, profile: &str, origin: String) -> AiRecord {
AiRecord {
profile: profile.to_string(),
origin,
recorded_at: unix_now(),
allow_plain_http: self.lenient,
tls: None,
}
}
pub fn admit_ai(&self, profile: &str, base_url: &str, has_key: bool) -> Result<Vec<String>> {
let origin = normalize(base_url).ok_or_else(|| Error::BadUrl(base_url.to_string()))?;
let subject = TrustSubject::Ai(profile.to_string());
let mut store = self.store()?;
let record = match store.ai_record(profile) {
Some(r) => r.clone(),
None => {
let record = lock(&self.provisional_ai)
.get(profile)
.cloned()
.unwrap_or_else(|| self.fresh_ai(profile, origin.clone()));
if has_key {
store.put_ai(record.clone());
self.save(&store)?;
} else {
lock(&self.provisional_ai).insert(profile.to_string(), record.clone());
}
record
}
};
if origin::is_plain_http_remote(&origin) && !record.allow_plain_http {
return self.refuse(TrustProblem::PlainHttp { subject, origin });
}
if record.origin != origin {
return self.refuse(TrustProblem::OriginChanged {
subject,
old: record.origin,
new: origin,
});
}
apply_tls_policy(&record.origin, record.tls.as_ref());
Ok(origin::origin_of(&record.origin).into_iter().collect())
}
pub fn ai_record_of(&self, profile: &str) -> Option<AiRecord> {
self.store().ok()?.ai_record(profile).cloned()
}
pub fn update_ai_record<R>(
&self,
profile: &str,
base_url: &str,
change: impl FnOnce(&mut AiRecord) -> R,
) -> Result<R> {
let origin = normalize(base_url).ok_or_else(|| Error::BadUrl(base_url.to_string()))?;
let mut store = self.store()?;
let mut record = match store.ai_record(profile) {
Some(r) => r.clone(),
None => lock(&self.provisional_ai)
.remove(profile)
.unwrap_or_else(|| self.fresh_ai(profile, origin)),
};
let out = change(&mut record);
apply_tls_policy(&record.origin, record.tls.as_ref());
store.put_ai(record);
self.save(&store)?;
lock(&self.provisional_ai).remove(profile);
clear_pending(&TrustSubject::Ai(profile.to_string()));
Ok(out)
}
pub fn record_ai(&self, profile: &str, base_url: &str) -> Result<()> {
let origin = normalize(base_url).ok_or_else(|| Error::BadUrl(base_url.to_string()))?;
self.update_ai_record(profile, base_url, |r| r.origin = origin)
}
pub fn trust_ai_origin(&self, profile: &str, base_url: &str) -> Result<()> {
self.record_ai(profile, base_url)
}
}
#[cfg(feature = "network")]
fn apply_tls_policy(origin: &str, tls: Option<&TlsRecord>) {
let Some(host) = origin::host_of(origin) else {
return;
};
tls::set_policy(
&host,
tls.map(|t| match t.mode {
TlsMode::Tofu => tls::TlsPolicy::Tofu {
spki: t.cert.spki_sha256.clone(),
},
TlsMode::Strict => tls::TlsPolicy::Strict {
spki: t.cert.spki_sha256.clone(),
},
}),
);
}
#[cfg(not(feature = "network"))]
fn apply_tls_policy(_origin: &str, _tls: Option<&TlsRecord>) {}
#[cfg(all(test, feature = "hosting"))]
mod tests;