mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-03 09:35:41 +00:00
feat(credentials): manage declarative credentials through TOML (#2515)
* feat(credentials): manage declarative credentials through TOML Make managed credentials part of the canonical TOML configuration and load them before peers can authenticate. Reuse ConfigRpc hot patches to durably replace the configured credential set without restarting the instance. Serialize credential mutations so base, managed, and ephemeral keys cannot race into conflicts. Remove the managed overlay file format, digest protocol, capability negotiation, force reconciliation, and database CAS machinery. Redact credential secrets from debug output and management events. Write credential-bearing files atomically with private permissions. * fix(core): release JoinSet reapers with their owners Pass weak task-set references into background reapers so they cannot retain the JoinSet they are meant to collect. This lets stale smoltcp bridge tasks terminate when an IPv4 generation is replaced. Add ownership and TCP generation-replacement regressions covering the production port-forward failure.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use std::{io::Write, path::PathBuf, sync::Arc};
|
||||
|
||||
use atomic_write_file::AtomicWriteFile;
|
||||
use atomic_write_file::{AtomicWriteFile, OpenOptions};
|
||||
use easytier_core::peers::credential_manager::CredentialStorage;
|
||||
|
||||
struct FileCredentialStorage {
|
||||
@@ -9,21 +9,33 @@ struct FileCredentialStorage {
|
||||
|
||||
impl CredentialStorage for FileCredentialStorage {
|
||||
fn load(&self) -> anyhow::Result<Option<String>> {
|
||||
let Ok(serialized) = std::fs::read_to_string(&self.path) else {
|
||||
return Ok(None);
|
||||
let serialized = match std::fs::read_to_string(&self.path) {
|
||||
Ok(serialized) => serialized,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(error) => return Err(error.into()),
|
||||
};
|
||||
tracing::info!(path = %self.path.display(), "loaded credentials");
|
||||
Ok(Some(serialized))
|
||||
}
|
||||
|
||||
fn store(&self, serialized_credentials: &str) -> anyhow::Result<()> {
|
||||
let mut file = AtomicWriteFile::open(&self.path)?;
|
||||
let mut file = restricted_atomic_file(&self.path)?;
|
||||
file.write_all(serialized_credentials.as_bytes())?;
|
||||
file.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn restricted_atomic_file(path: &std::path::Path) -> std::io::Result<AtomicWriteFile> {
|
||||
let mut options = OpenOptions::new();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
atomic_write_file::unix::OpenOptionsExt::preserve_mode(&mut options, false);
|
||||
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
|
||||
}
|
||||
options.open(path)
|
||||
}
|
||||
|
||||
pub(crate) fn runtime_credential_storage(
|
||||
path: Option<PathBuf>,
|
||||
) -> Option<Arc<dyn CredentialStorage>> {
|
||||
@@ -48,5 +60,28 @@ mod tests {
|
||||
storage.load().unwrap().as_deref(),
|
||||
Some("{\"credential\":false}")
|
||||
);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt as _;
|
||||
|
||||
assert_eq!(
|
||||
std::fs::metadata(&storage.path)
|
||||
.unwrap()
|
||||
.permissions()
|
||||
.mode()
|
||||
& 0o777,
|
||||
0o600
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_storage_reports_read_errors() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let storage = FileCredentialStorage {
|
||||
path: directory.path().to_path_buf(),
|
||||
};
|
||||
|
||||
assert!(storage.load().is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user