Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

feat(enterprise/sync): server-driven upload mode — no env vars on customer machines

Louis Beaumont566ae6cc457e
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/enterprise_sync.rs b/apps/screenpipe-app-tauri/src-tauri/src/enterprise_sync.rs
index 6e80df38b..5cc7f4a64 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/enterprise_sync.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/enterprise_sync.rs
@@ -410,6 +410,19 @@ mod imp {
// Small startup delay so the local screenpipe server is up before
// we hammer it. Mirrors calendar publisher's `sleep(10)`.
tokio::time::sleep(std::time::Duration::from_secs(15)).await;
+
+ // Ask the control plane what upload mode this license should run
+ // in. Replaces the old "set SCREENPIPE_ENTERPRISE_UPLOAD_MODE on
+ // every customer machine" UX — the dashboard binding is now the
+ // single source of truth, so a fresh enterprise install just
+ // needs the license key and uploads start automatically.
+ let mut cfg = cfg;
+ cfg.resolve_upload_mode().await;
+ info!(
+ "enterprise sync: resolved upload mode = {:?}",
+ std::mem::discriminant(&cfg.upload_mode)
+ );
+
ee_sync::run(cfg, local, rx).await;
});
diff --git a/ee/desktop-rust/enterprise_sync.rs b/ee/desktop-rust/enterprise_sync.rs
index 697d737b5..a153893a2 100644
--- a/ee/desktop-rust/enterprise_sync.rs
+++ b/ee/desktop-rust/enterprise_sync.rs
@@ -93,6 +93,13 @@ impl EnterpriseSyncConfig {
/// Build config from env vars + the OS device id. Returns `None` when
/// required env (`SCREENPIPE_ENTERPRISE_LICENSE_KEY`) is missing — caller
/// should silently skip sync in that case.
+ ///
+ /// `upload_mode` is initialized to `HostedIngest` as a safe default. The
+ /// caller should run [`Self::resolve_upload_mode`] once the async runtime
+ /// is up to upgrade to `DirectReadable` / `DirectEncrypted` based on the
+ /// customer's storage binding in the control plane. This replaces the
+ /// old "set `SCREENPIPE_ENTERPRISE_UPLOAD_MODE` on every device" UX —
+ /// the dashboard binding is now the single source of truth.
pub fn from_env(
app_data_dir: PathBuf,
device_id: String,
@@ -105,7 +112,21 @@ impl EnterpriseSyncConfig {
.ok()
.filter(|s| !s.trim().is_empty())
.unwrap_or_else(|| DEFAULT_INGEST_URL.to_string());
- let upload_mode = EnterpriseUploadMode::from_env(&ingest_url)?;
+ // Honor an explicit env override at boot for MDM / dev / test flows.
+ // Fail-closed semantics: if the operator explicitly set a mode and
+ // it can't be honored (invalid keys etc.), refuse to start sync — a
+ // silent fallback to plaintext could leak data. When no override is
+ // set we start in HostedIngest and let `resolve_upload_mode` ask
+ // the control plane what this license is actually configured for.
+ let explicit_mode = std::env::var("SCREENPIPE_ENTERPRISE_UPLOAD_MODE")
+ .ok()
+ .map(|s| s.trim().to_string())
+ .filter(|s| !s.is_empty() && s != "auto");
+ let upload_mode = if explicit_mode.is_some() {
+ EnterpriseUploadMode::from_env(&ingest_url)?
+ } else {
+ EnterpriseUploadMode::HostedIngest
+ };
let cursor_path = app_data_dir.join(CURSOR_FILENAME);
Some(Self {
license_key,
@@ -116,6 +137,19 @@ impl EnterpriseSyncConfig {
upload_mode,
})
}
+
+ /// Ask the control plane which upload mode this license should run in,
+ /// and update `self.upload_mode` accordingly. Safe to call before every
+ /// sync run — if the lookup fails, the existing mode is preserved.
+ ///
+ /// This is what makes the "install enterprise build → enter license key
+ /// → uploads start" flow possible without any env-var setup on the
+ /// customer's machine.
+ pub async fn resolve_upload_mode(&mut self) {
+ let resolved =
+ EnterpriseUploadMode::resolve(&self.license_key, &self.ingest_url).await;
+ self.upload_mode = resolved;
+ }
}
// ─── Cursor (last-synced timestamp) ─────────────────────────────────────────
@@ -334,6 +368,8 @@ pub enum EnterpriseSyncError {
IngestAuthRejected,
#[error("ingest server error: status {0}")]
IngestServerError(u16),
+ #[error("control-plane network error: {0}")]
+ Network(String),
#[error("io: {0}")]
Io(#[from] std::io::Error),
}
diff --git a/ee/desktop-rust/enterprise_upload.rs b/ee/desktop-rust/enterprise_upload.rs
index d29151692..5e6e9460d 100644
--- a/ee/desktop-rust/enterprise_upload.rs
+++ b/ee/desktop-rust/enterprise_upload.rs
@@ -54,6 +54,76 @@ pub struct DirectUploadKeyRecipientConfig {
}
impl EnterpriseUploadMode {
+ /// Resolve the upload mode by asking the control plane what this
+ /// license is configured for. Replaces the old env-var bootstrap so
+ /// customers don't have to set `SCREENPIPE_ENTERPRISE_UPLOAD_MODE`
+ /// on every device — the storage binding in the dashboard is the
+ /// single source of truth.
+ ///
+ /// Fails open to `HostedIngest` on any error (network down, license
+ /// invalid, server flake). The legacy env-var override is still
+ /// honored for advanced/test scenarios when explicitly set to
+ /// anything other than the default `screenpipe_write`.
+ pub async fn resolve(license_key: &str, ingest_url: &str) -> Self {
+ // Explicit env override — for MDM rollouts and local testing.
+ // Only takes effect when set to a non-default value; the empty /
+ // default case falls through to server resolution.
+ if let Ok(raw) = std::env::var("SCREENPIPE_ENTERPRISE_UPLOAD_MODE") {
+ let normalized = raw.trim().to_ascii_lowercase();
+ if !normalized.is_empty()
+ && normalized != "screenpipe_write"
+ && normalized != "hosted_ingest"
+ && normalized != "auto"
+ {
+ if let Some(mode) = Self::from_env(ingest_url) {
+ tracing::info!(
+ "enterprise sync: upload mode taken from \
+ SCREENPIPE_ENTERPRISE_UPLOAD_MODE env override ({})",
+ normalized
+ );
+ return mode;
+ }
+ }
+ }
+
+ match fetch_desired_mode_from_server(license_key, ingest_url).await {
+ Ok(ServerModeHint::DirectUpload) => {
+ // Encrypted if MDM root keys are present, readable
+ // otherwise. Same logic the env path uses; just gated by
+ // server intent instead of env var.
+ if std::env::var("SCREENPIPE_ENTERPRISE_DIRECT_UPLOAD_ROOT_KEY_B64").is_ok() {
+ if let Some(mode) = Self::build_direct_encrypted(ingest_url) {
+ tracing::info!(
+ "enterprise sync: server requested direct upload + MDM keys \
+ present → direct_upload_encrypted"
+ );
+ return mode;
+ }
+ tracing::warn!(
+ "enterprise sync: server requested direct upload + MDM keys present \
+ but key material was invalid; falling back to direct_upload_readable"
+ );
+ }
+ tracing::info!(
+ "enterprise sync: server requested direct upload → direct_upload_readable"
+ );
+ Self::DirectReadable(DirectUploadConfig::without_recipients(ingest_url))
+ }
+ Ok(ServerModeHint::HostedIngest) => {
+ tracing::info!("enterprise sync: server requested hosted_ingest");
+ Self::HostedIngest
+ }
+ Err(e) => {
+ tracing::warn!(
+ error = %e,
+ "enterprise sync: control-plane mode lookup failed; \
+ defaulting to hosted_ingest (will retry next batch)"
+ );
+ Self::HostedIngest
+ }
+ }
+ }
+
pub fn from_env(ingest_url: &str) -> Option<Self> {
let mode = std::env::var("SCREENPIPE_ENTERPRISE_UPLOAD_MODE")
.unwrap_or_else(|_| "screenpipe_write".to_string())
@@ -65,101 +135,159 @@ impl EnterpriseUploadMode {
"direct_upload_readable" => Some(Self::DirectReadable(
DirectUploadConfig::without_recipients(ingest_url),
)),
- "direct_upload" | "direct_upload_encrypted" => {
- let primary_key_b64 = match required_env(
- "SCREENPIPE_ENTERPRISE_DIRECT_UPLOAD_ROOT_KEY_B64",
- ) {
- Some(v) => v,
- None => {
- warn!(
- "enterprise sync: direct upload requested but primary root key env is missing"
- );
- return None;
- }
- };
- let recovery_key_b64 = match required_env(
- "SCREENPIPE_ENTERPRISE_DIRECT_UPLOAD_RECOVERY_ROOT_KEY_B64",
- ) {
- Some(v) => v,
- None => {
- warn!(
- "enterprise sync: direct upload requested but recovery root key env is missing"
- );
- return None;
- }
- };
- let primary_root_key = match decode_root_key(&primary_key_b64) {
- Ok(k) => k,
- Err(e) => {
- warn!(
- "enterprise sync: invalid direct upload primary root key: {}",
- e
- );
- return None;
- }
- };
- let recovery_root_key = match decode_root_key(&recovery_key_b64) {
- Ok(k) => k,
- Err(e) => {
- warn!(
- "enterprise sync: invalid direct upload recovery root key: {}",
- e
- );
- return None;
- }
- };
- let primary_key_id = std::env::var("SCREENPIPE_ENTERPRISE_DIRECT_UPLOAD_KEY_ID")
- .ok()
- .map(|s| s.trim().to_string())
- .filter(|s| !s.is_empty())
- .unwrap_or_else(|| "mdm-primary-v1".to_string());
- let recovery_key_id =
- std::env::var("SCREENPIPE_ENTERPRISE_DIRECT_UPLOAD_RECOVERY_KEY_ID")
- .ok()
- .map(|s| s.trim().to_string())
- .filter(|s| !s.is_empty())
- .unwrap_or_else(|| "mdm-recovery-v1".to_string());
- if primary_key_id == recovery_key_id {
+ "direct_upload" | "direct_upload_encrypted" => Self::build_direct_encrypted(ingest_url),
+ other => {
+ warn!(
+ "enterprise sync: unknown upload mode '{}'; refusing to start sync",
+ other
+ );
+ None
+ }
+ }
+ }
+
+ /// Build a `DirectEncrypted` mode from MDM-deployed root key env vars.
+ /// Shared by the legacy `from_env` path and the new server-driven
+ /// `resolve` path so the encrypted-mode contract stays in one place.
+ fn build_direct_encrypted(ingest_url: &str) -> Option<Self> {
+ let primary_key_b64 =
+ match required_env("SCREENPIPE_ENTERPRISE_DIRECT_UPLOAD_ROOT_KEY_B64") {
+ Some(v) => v,
+ None => {
warn!(
- "enterprise sync: direct upload primary and recovery key ids must differ"
+ "enterprise sync: direct upload requested but primary root key env is missing"
);
return None;
}
- if primary_root_key == recovery_root_key {
+ };
+ let recovery_key_b64 =
+ match required_env("SCREENPIPE_ENTERPRISE_DIRECT_UPLOAD_RECOVERY_ROOT_KEY_B64") {
+ Some(v) => v,
+ None => {
warn!(
- "enterprise sync: direct upload primary and recovery root keys must differ"
+ "enterprise sync: direct upload requested but recovery root key env is missing"
);
return None;
}
- let control_plane = DirectUploadConfig::without_recipients(ingest_url);
-
- Some(Self::DirectEncrypted(DirectUploadConfig {
- ticket_url: control_plane.ticket_url,
- complete_url: control_plane.complete_url,
- recipients: vec![
- DirectUploadKeyRecipientConfig {
- purpose: "primary".to_string(),
- key_provider: "mdm_symmetric_v1".to_string(),
- key_id: primary_key_id,
- root_key: primary_root_key,
- },
- DirectUploadKeyRecipientConfig {
- purpose: "recovery".to_string(),
- key_provider: "mdm_symmetric_v1".to_string(),
- key_id: recovery_key_id,
- root_key: recovery_root_key,
- },
- ],
- }))
+ };
+ let primary_root_key = match decode_root_key(&primary_key_b64) {
+ Ok(k) => k,
+ Err(e) => {
+ warn!(
+ "enterprise sync: invalid direct upload primary root key: {}",
+ e
+ );
+ return None;
}
- other => {
+ };
+ let recovery_root_key = match decode_root_key(&recovery_key_b64) {
+ Ok(k) => k,
+ Err(e) => {
warn!(
- "enterprise sync: unknown upload mode '{}'; refusing to start sync",
- other
+ "enterprise sync: invalid direct upload recovery root key: {}",
+ e
);
- None
+ return None;
}
+ };
+ let primary_key_id = std::env::var("SCREENPIPE_ENTERPRISE_DIRECT_UPLOAD_KEY_ID")
+ .ok()
+ .map(|s| s.trim().to_string())
+ .filter(|s| !s.is_empty())
+ .unwrap_or_else(|| "mdm-primary-v1".to_string());
+ let recovery_key_id =
+ std::env::var("SCREENPIPE_ENTERPRISE_DIRECT_UPLOAD_RECOVERY_KEY_ID")
+ .ok()
+ .map(|s| s.trim().to_string())
+ .filter(|s| !s.is_empty())
+ .unwrap_or_else(|| "mdm-recovery-v1".to_string());
+ if primary_key_id == recovery_key_id {
+ warn!(
+ "enterprise sync: direct upload primary and recovery key ids must differ"
+ );
+ return None;
}
+ if primary_root_key == recovery_root_key {
+ warn!(
+ "enterprise sync: direct upload primary and recovery root keys must differ"
+ );
+ return None;
+ }
+ let control_plane = DirectUploadConfig::without_recipients(ingest_url);
+
+ Some(Self::DirectEncrypted(DirectUploadConfig {
+ ticket_url: control_plane.ticket_url,
+ complete_url: control_plane.complete_url,
+ recipients: vec![
+ DirectUploadKeyRecipientConfig {
+ purpose: "primary".to_string(),
+ key_provider: "mdm_symmetric_v1".to_string(),
+ key_id: primary_key_id,
+ root_key: primary_root_key,
+ },
+ DirectUploadKeyRecipientConfig {
+ purpose: "recovery".to_string(),
+ key_provider: "mdm_symmetric_v1".to_string(),
+ key_id: recovery_key_id,
+ root_key: recovery_root_key,
+ },
+ ],
+ }))
+ }
+}
+
+// ─── Control-plane mode hint ─────────────────────────────────────────────────
+
+/// What the server tells the desktop to do for this license. The desktop
+/// translates this into a concrete `EnterpriseUploadMode` based on locally
+/// available material (e.g. MDM-deployed root keys).
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum ServerModeHint {
+ DirectUpload,
+ HostedIngest,
+}
+
+#[derive(Deserialize)]
+struct ModeResponse {
+ desired_mode: String,
+}
+
+const MODE_RESOLVE_TIMEOUT: Duration = Duration::from_secs(10);
+
+/// HTTP GET `<ingest sibling>/storage-binding/mode` with the license-key
+/// header. Returns the parsed hint; any non-2xx, parse failure, or network
+/// error bubbles up so the caller can fall back to `HostedIngest`.
+async fn fetch_desired_mode_from_server(
+ license_key: &str,
+ ingest_url: &str,
+) -> Result<ServerModeHint, EnterpriseSyncError> {
+ let endpoint = sibling_enterprise_endpoint(ingest_url, "storage-binding/mode");
+ let client = reqwest::Client::builder()
+ .timeout(MODE_RESOLVE_TIMEOUT)
+ .build()
+ .map_err(|e| EnterpriseSyncError::Network(e.to_string()))?;
+ let resp = client
+ .get(&endpoint)
+ .header("x-license-key", license_key)
+ .send()
+ .await
+ .map_err(|e| EnterpriseSyncError::Network(e.to_string()))?;
+ if !resp.status().is_success() {
+ return Err(EnterpriseSyncError::Network(format!(
+ "mode endpoint returned status {}",
+ resp.status()
+ )));
+ }
+ let parsed: ModeResponse = resp
+ .json()
+ .await
+ .map_err(|e| EnterpriseSyncError::Network(format!("mode response parse failed: {e}")))?;
+ match parsed.desired_mode.trim().to_ascii_lowercase().as_str() {
+ "direct_upload" => Ok(ServerModeHint::DirectUpload),
+ "hosted_ingest" | "screenpipe_write" | "" => Ok(ServerModeHint::HostedIngest),
+ other => Err(EnterpriseSyncError::Network(format!(
+ "unknown desired_mode '{other}' from control plane"
+ ))),
}
}