use std::collections::BTreeMap;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::{CodingError, CodingErrorCode};
const TRUST_SCHEMA_VERSION: u32 = 1;
const MAX_TRUST_FILE_BYTES: usize = 256 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PersistedTrustDecision {
Trusted,
Rejected,
Ignored,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrustRequest {
Default,
TrustOnce,
TrustPersisted,
Reject,
Ignore,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InteractionMode {
Interactive,
NonInteractive,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProjectAccess {
Trusted,
Ignored,
Ask,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct TrustFile {
schema_version: u32,
decisions: BTreeMap<String, PersistedTrustDecision>,
}
#[derive(Debug, Clone)]
pub struct ProjectTrustStore {
path: PathBuf,
}
impl ProjectTrustStore {
#[must_use]
pub fn new(path: impl Into<PathBuf>) -> Self {
Self { path: path.into() }
}
pub fn get(&self, workspace: &Path) -> Result<Option<PersistedTrustDecision>, CodingError> {
let workspace = canonical_workspace(workspace)?;
Ok(self.load()?.decisions.get(&workspace).copied())
}
pub fn set(
&self,
workspace: &Path,
decision: PersistedTrustDecision,
) -> Result<(), CodingError> {
let workspace = canonical_workspace(workspace)?;
let mut file = self.load()?;
file.decisions.insert(workspace, decision);
let bytes = serde_json::to_vec_pretty(&file).map_err(|_| persistence())?;
if bytes.len() > MAX_TRUST_FILE_BYTES {
return Err(persistence());
}
let parent = self.path.parent().ok_or_else(persistence)?;
fs::create_dir_all(parent).map_err(|_| persistence())?;
let temporary = self.path.with_extension("json.tmp");
let mut options = OpenOptions::new();
options.write(true).create(true).truncate(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
let mut output = options.open(&temporary).map_err(|_| persistence())?;
output.write_all(&bytes).map_err(|_| persistence())?;
output.sync_all().map_err(|_| persistence())?;
fs::rename(&temporary, &self.path).map_err(|_| persistence())?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(&self.path, fs::Permissions::from_mode(0o600))
.map_err(|_| persistence())?;
}
Ok(())
}
pub fn resolve(
&self,
workspace: &Path,
request: TrustRequest,
mode: InteractionMode,
) -> Result<ProjectAccess, CodingError> {
match request {
TrustRequest::TrustOnce => return Ok(ProjectAccess::Trusted),
TrustRequest::TrustPersisted => {
self.set(workspace, PersistedTrustDecision::Trusted)?;
return Ok(ProjectAccess::Trusted);
}
TrustRequest::Reject => return Err(not_trusted()),
TrustRequest::Ignore => return Ok(ProjectAccess::Ignored),
TrustRequest::Default => {}
}
match self.get(workspace)? {
Some(PersistedTrustDecision::Trusted) => Ok(ProjectAccess::Trusted),
Some(PersistedTrustDecision::Ignored) => Ok(ProjectAccess::Ignored),
None if mode == InteractionMode::Interactive => Ok(ProjectAccess::Ask),
None | Some(PersistedTrustDecision::Rejected) => Err(not_trusted()),
}
}
fn load(&self) -> Result<TrustFile, CodingError> {
let bytes = match fs::read(&self.path) {
Ok(bytes) => bytes,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok(TrustFile {
schema_version: TRUST_SCHEMA_VERSION,
decisions: BTreeMap::new(),
});
}
Err(_) => return Err(persistence()),
};
if bytes.len() > MAX_TRUST_FILE_BYTES {
return Err(persistence());
}
let file = serde_json::from_slice::<TrustFile>(&bytes).map_err(|_| persistence())?;
if file.schema_version != TRUST_SCHEMA_VERSION || file.decisions.len() > 4096 {
return Err(persistence());
}
Ok(file)
}
}
fn canonical_workspace(workspace: &Path) -> Result<String, CodingError> {
let canonical = fs::canonicalize(workspace).map_err(|_| {
CodingError::new(
CodingErrorCode::NotFound,
"workspace directory does not exist",
)
})?;
if !canonical.is_dir() {
return Err(CodingError::new(
CodingErrorCode::InvalidInput,
"workspace is not a directory",
));
}
canonical
.to_str()
.filter(|value| value.len() <= 4096 && !value.chars().any(char::is_control))
.map(str::to_owned)
.ok_or_else(|| {
CodingError::new(
CodingErrorCode::InvalidInput,
"workspace identity is invalid",
)
})
}
fn persistence() -> CodingError {
CodingError::new(CodingErrorCode::Persistence, "project trust state failed")
}
fn not_trusted() -> CodingError {
CodingError::new(
CodingErrorCode::ProjectNotTrusted,
"project-local configuration is not trusted",
)
}