Support interactive customer transfers for enrolled devices
Rust development slice / linux (push) Failing after 8s
Rust development slice / linux (push) Failing after 8s
This commit is contained in:
@@ -64,6 +64,8 @@ struct Agent<C: reconcile::SyncClock = reconcile::NativeClock> {
|
||||
rotation_policy: certificate_rotation::RotationPolicy,
|
||||
catalog: agent_update_catalog::Checker,
|
||||
update_observer: agent_update_apply::Observer,
|
||||
transfer_state: Option<PathBuf>,
|
||||
transfer_gate: Option<fs::File>,
|
||||
}
|
||||
#[derive(Default)]
|
||||
struct EffectFence {
|
||||
@@ -465,6 +467,9 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
"heartbeat/1".into(),
|
||||
"agent.update/1".into(),
|
||||
];
|
||||
if self.transfer_state.is_some() {
|
||||
capabilities.push(rmm_domain::customer_transfer::CAPABILITY.into());
|
||||
}
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
capabilities.push(rmm_domain::agent_update::LINUX_ARM64_RELEASE_CAPABILITY.into());
|
||||
if self.binding.is_some() {
|
||||
@@ -476,12 +481,39 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
if rmm_agent_linux::actions::Interpreter::PosixSh.available() {
|
||||
capabilities.push("run_script".into());
|
||||
}
|
||||
capabilities.extend(helper_sync::script_capabilities(binding));
|
||||
if self.helpers.is_some() {
|
||||
capabilities.extend(helper_sync::script_capabilities(binding));
|
||||
}
|
||||
}
|
||||
capabilities.sort();
|
||||
capabilities.dedup();
|
||||
capabilities
|
||||
}
|
||||
async fn stage_customer_transfer(&mut self) -> Result<bool> {
|
||||
let (Some(dir), Some(binding)) = (&self.transfer_state, &self.binding) else {
|
||||
return Ok(false);
|
||||
};
|
||||
if !self.running_cancels.is_empty() || self.prepared.is_some()
|
||||
|| !rmm_agent_core::customer_transfer::idle(&self.db, &self.ledger)? {
|
||||
return Ok(false);
|
||||
}
|
||||
let identity = self.certificates.current(Utc::now().timestamp())?;
|
||||
let client = https_channel::mtls_client(
|
||||
self.address, &self.ca, &identity.certificate_pem, &identity.private_key_pem,
|
||||
)?;
|
||||
let base = format!("https://{}:{}", https_channel::server_name()?, self.address.port());
|
||||
let guard = fs::File::open(env::var_os("RMM_RELEASE_LOCK").context("RELEASE_LOCK_REQUIRED")?)?;
|
||||
match fs2::FileExt::try_lock_shared(&guard) {
|
||||
Ok(()) => (),
|
||||
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => return Ok(false),
|
||||
Err(error) => return Err(error.into()),
|
||||
}
|
||||
if rmm_agent_core::customer_transfer::stage(&client, &base, dir, binding, transfer_csr).await? {
|
||||
self.transfer_gate = Some(guard);
|
||||
return Ok(true);
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
fn queue_inventory(&mut self) -> bool {
|
||||
if self.inventory_inflight || !storage::passive_collection_ready(&self.db).unwrap_or(false)
|
||||
{
|
||||
@@ -849,6 +881,11 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
}
|
||||
if rotation_at.elapsed() >= Duration::from_secs(30) {
|
||||
rotation_at = Instant::now();
|
||||
match self.stage_customer_transfer().await {
|
||||
Ok(true) => return Ok(()),
|
||||
Ok(false) => (),
|
||||
Err(_) => eprintln!("customer transfer waiting for a valid handoff"),
|
||||
}
|
||||
if self
|
||||
.certificates
|
||||
.due(self.rotation_policy, Utc::now().timestamp())?
|
||||
@@ -873,6 +910,27 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
}
|
||||
}
|
||||
|
||||
fn transfer_csr(previous: Option<&[u8]>) -> Result<(Vec<u8>, String)> {
|
||||
let key = match previous {
|
||||
Some(raw) => PKey::private_key_from_pem(raw)?,
|
||||
None => {
|
||||
let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1)?;
|
||||
PKey::from_ec_key(EcKey::generate(&group)?)?
|
||||
}
|
||||
};
|
||||
let mut csr = X509Req::builder()?;
|
||||
let mut name = X509NameBuilder::new()?;
|
||||
name.append_entry_by_text("CN", "customer-transfer")?;
|
||||
csr.set_subject_name(&name.build())?;
|
||||
csr.set_pubkey(&key)?;
|
||||
csr.sign(&key, MessageDigest::sha256())?;
|
||||
Ok((key.private_key_to_pem_pkcs8()?, String::from_utf8(csr.build().to_pem()?)?))
|
||||
}
|
||||
|
||||
fn customer_transfer_signer(dir: &Path) -> Result<rmm_privileged_helper::SigningKey> {
|
||||
rmm_privileged_helper::SigningKey::read(&dir.join("job-signing-key.json"), unsafe { libc::geteuid() })
|
||||
}
|
||||
|
||||
async fn enrollment(dir: &Path, address: std::net::SocketAddr, ca: &[u8]) -> Result<String> {
|
||||
let identity = dir.join("identity.json");
|
||||
if identity.exists() {
|
||||
@@ -950,6 +1008,18 @@ async fn main() -> Result<()> {
|
||||
PathBuf::from(env::var("RMM_AGENT_STATE").unwrap_or_else(|_| "devstate/agent".into()));
|
||||
storage::prepare_dir(&dir)?;
|
||||
let _lock = storage::lock(&dir)?;
|
||||
let mut release_guard = None;
|
||||
loop {
|
||||
if rmm_agent_core::customer_transfer::pending(&dir)? {
|
||||
let signer = customer_transfer_signer(&dir)?;
|
||||
rmm_agent_core::customer_transfer::activate(&dir, &signer.public_key_hex)?;
|
||||
}
|
||||
drop(release_guard.take());
|
||||
release_guard = Some(run_agent(&dir).await?);
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_agent(dir: &Path) -> Result<fs::File> {
|
||||
let address = env::var("RMM_CONNECT_ADDR")
|
||||
.unwrap_or_else(|_| "127.0.0.1:18443".into())
|
||||
.parse()?;
|
||||
@@ -978,6 +1048,20 @@ async fn main() -> Result<()> {
|
||||
enrollment_generation: 1, assignment_version: 1, public_key_hex: String::new(),
|
||||
});
|
||||
}
|
||||
let transfer_state = (env::var_os("RMM_JOB_BINDING_FILE").is_none()
|
||||
&& env::var_os("RMM_ENROLLMENT_TENANT").is_some()
|
||||
&& env::var("RMM_JOB_SIGNING_KEY_BOOTSTRAP").as_deref() == Ok("1")
|
||||
&& env::var_os("RMM_RELEASE_LOCK").is_some()).then(|| dir.to_owned());
|
||||
let mut moved = false;
|
||||
if transfer_state.is_some() && let Some(binding) = binding.as_mut()
|
||||
&& dir.join("job-signing-key.json").try_exists()? {
|
||||
let identity: EnrollmentResponse = serde_json::from_slice(&fs::read(dir.join("identity.json"))?)?;
|
||||
let signer = customer_transfer_signer(dir)?;
|
||||
if let Some(tenant) = rmm_agent_core::customer_transfer::assigned_tenant(dir, &identity, &signer.public_key_hex)? {
|
||||
binding.tenant_id = tenant;
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
let certificate_dir = dir.join("certificates");
|
||||
let certificates = certificate_rotation::RotationStore::open(
|
||||
&certificate_dir,
|
||||
@@ -1009,10 +1093,9 @@ async fn main() -> Result<()> {
|
||||
let sync = binding
|
||||
.as_ref()
|
||||
.map(|b| reconcile::ClientSync::new(b.clone()));
|
||||
let helpers = binding
|
||||
.as_ref()
|
||||
.map(helper_sync::Helpers::configured)
|
||||
.transpose()?;
|
||||
let helpers = if moved { None } else {
|
||||
binding.as_ref().map(helper_sync::Helpers::configured).transpose()?
|
||||
};
|
||||
let mut rotation_policy = certificate_rotation::RotationPolicy::default();
|
||||
if let Ok(after) = env::var("RMM_ROTATION_TEST_AFTER_SECONDS") {
|
||||
ensure!(
|
||||
@@ -1054,6 +1137,8 @@ async fn main() -> Result<()> {
|
||||
rotation_policy,
|
||||
catalog: agent_update_catalog::Checker::default(),
|
||||
update_observer: agent_update_apply::Observer::default(),
|
||||
transfer_state,
|
||||
transfer_gate: None,
|
||||
};
|
||||
agent.recover_interrupted()?;
|
||||
let mut backoff = 1_u64;
|
||||
@@ -1063,6 +1148,9 @@ async fn main() -> Result<()> {
|
||||
report_closed_gate(&error);
|
||||
}
|
||||
agent.close_gate();
|
||||
if let Some(guard) = agent.transfer_gate.take() {
|
||||
return Ok(guard);
|
||||
}
|
||||
eprintln!("channel unavailable; retrying with jitter (no jobs can execute)");
|
||||
if started.elapsed() > Duration::from_secs(60) {
|
||||
backoff = 1;
|
||||
|
||||
@@ -152,6 +152,8 @@ fn new_process_persists_interruption_before_helper_io_without_unlock_or_repeat()
|
||||
rotation_policy: certificate_rotation::RotationPolicy::default(),
|
||||
catalog: agent_update_catalog::Checker::default(),
|
||||
update_observer: agent_update_apply::Observer::default(),
|
||||
transfer_state: None,
|
||||
transfer_gate: None,
|
||||
};
|
||||
// A forged local consumption/start cannot become a purported grant result.
|
||||
agent
|
||||
@@ -541,6 +543,8 @@ async fn run(mode: wire_channel::Mode, trusted: bool, forged: bool, rounds: u64)
|
||||
rotation_policy: certificate_rotation::RotationPolicy::default(),
|
||||
catalog: agent_update_catalog::Checker::default(),
|
||||
update_observer: agent_update_apply::Observer::default(),
|
||||
transfer_state: None,
|
||||
transfer_gate: None,
|
||||
};
|
||||
for _ in 0..rounds {
|
||||
assert!(
|
||||
|
||||
Reference in New Issue
Block a user