-
Notifications
You must be signed in to change notification settings - Fork 3
feat(client): generate local machine id and send x-machine-id on all connections #1729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
clients/openframe-client/src/services/machine_id_service.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| use anyhow::{Context, Result}; | ||
| use std::fs; | ||
| use std::path::PathBuf; | ||
| use std::sync::Arc; | ||
| use tracing::{debug, info}; | ||
| use uuid::Uuid; | ||
|
|
||
| use crate::platform::DirectoryManager; | ||
|
|
||
| pub const MACHINE_ID_HEADER: &str = "x-machine-id"; | ||
|
|
||
| // Locally generated machine identity, distinct from the server-assigned machine_id in agent config. | ||
| // Persisted in the shared app-support dir so integrated tool agents (mesh, fleet) can read it. | ||
| #[derive(Clone)] | ||
| pub struct MachineIdService { | ||
| file_path: PathBuf, | ||
| cached_id: Arc<std::sync::RwLock<Option<String>>>, | ||
| } | ||
|
|
||
| impl MachineIdService { | ||
| pub fn new(directory_manager: &DirectoryManager) -> Self { | ||
| Self { | ||
| file_path: directory_manager.app_support_dir().join("machine_id"), | ||
| cached_id: Arc::new(std::sync::RwLock::new(None)), | ||
| } | ||
| } | ||
|
|
||
| pub fn get_or_create(&self) -> Result<String> { | ||
| if let Some(id) = self.cached_id.read().unwrap().clone() { | ||
| return Ok(id); | ||
| } | ||
|
|
||
| if let Ok(id) = self.read() { | ||
| debug!("Using existing machine ID: {}", id); | ||
| *self.cached_id.write().unwrap() = Some(id.clone()); | ||
| return Ok(id); | ||
| } | ||
|
|
||
| let id = Uuid::new_v4().to_string(); | ||
| self.write(&id)?; | ||
| info!("Generated new machine ID: {}", id); | ||
| *self.cached_id.write().unwrap() = Some(id.clone()); | ||
| Ok(id) | ||
| } | ||
|
|
||
| // Cached id only; empty string until get_or_create has run. | ||
| pub fn get(&self) -> String { | ||
| self.cached_id.read().unwrap().clone().unwrap_or_default() | ||
| } | ||
|
|
||
| fn read(&self) -> Result<String> { | ||
| let content = fs::read_to_string(&self.file_path) | ||
| .with_context(|| format!("Failed to read {}", self.file_path.display()))?; | ||
|
|
||
| let id = content.trim(); | ||
| if id.is_empty() { | ||
| anyhow::bail!("Machine ID file is empty"); | ||
| } | ||
| Ok(id.to_string()) | ||
| } | ||
|
|
||
| fn write(&self, id: &str) -> Result<()> { | ||
| if let Some(parent) = self.file_path.parent() { | ||
| fs::create_dir_all(parent) | ||
| .with_context(|| format!("Failed to create {}", parent.display()))?; | ||
| } | ||
|
|
||
| fs::write(&self.file_path, id) | ||
| .with_context(|| format!("Failed to write {}", self.file_path.display()))?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Create the persisted machine ID atomically.
Line 33 ignores every read error. An empty, unreadable, or transiently unavailable file then causes Lines 39-42 to generate a new ID.
Concurrent callers can also both observe a missing file and write different IDs. The
RwLockdoes not protect the full read-create-write sequence across service instances or processes. HTTP, NATS control, and NATS logs can then use differentx-machine-idvalues.Generate only when the file is
NotFound. Serialize initialization with an interprocess lock or atomic create-and-re-read. Write through a temporary file and rename it atomically.Based on learnings, implement this behavior in the upstream tenant repository first, then retain a 1:1 port here.
🤖 Prompt for AI Agents
Source: Learnings