Skip to content

Commit dab4ad4

Browse files
committed
keylimectl: Use detected API version for verifier in push-mode
Also, correct the URL to add the agent to the verifier. Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 0691eb6 commit dab4ad4

File tree

3 files changed

+11
-112
lines changed

3 files changed

+11
-112
lines changed

keylimectl/src/client/factory.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use crate::error::KeylimectlError;
1818
use std::sync::OnceLock;
1919

2020
static VERIFIER_CLIENT: OnceLock<VerifierClient> = OnceLock::new();
21-
static VERIFIER_CLIENT_OVERRIDE: OnceLock<VerifierClient> = OnceLock::new();
2221
static REGISTRAR_CLIENT: OnceLock<RegistrarClient> = OnceLock::new();
2322

2423
/// Get or create the verifier client
@@ -62,52 +61,6 @@ pub async fn get_verifier() -> Result<&'static VerifierClient, KeylimectlError>
6261
}
6362
}
6463

65-
/// Get or create the verifier client with API version override
66-
///
67-
/// This function is used for push-model where we need to force API v3.0
68-
/// for verifier requests while still benefiting from version detection.
69-
///
70-
/// # Arguments
71-
///
72-
/// * `api_version` - The API version to use (e.g., "3.0")
73-
///
74-
/// # Errors
75-
///
76-
/// Returns an error if the client cannot be created.
77-
///
78-
/// # Examples
79-
///
80-
/// ```rust,ignore
81-
/// use keylimectl::client::factory;
82-
///
83-
/// // For push-model operations
84-
/// let verifier = factory::get_verifier_with_override("3.0").await?;
85-
/// ```
86-
pub async fn get_verifier_with_override(
87-
api_version: &str,
88-
) -> Result<&'static VerifierClient, KeylimectlError> {
89-
if let Some(client) = VERIFIER_CLIENT_OVERRIDE.get() {
90-
return Ok(client);
91-
}
92-
93-
// Create and initialize the client with override
94-
let config = get_config();
95-
let client = VerifierClient::builder()
96-
.config(config)
97-
.override_api_version(api_version)
98-
.build()
99-
.await?;
100-
101-
// Try to set it
102-
match VERIFIER_CLIENT_OVERRIDE.set(client) {
103-
Ok(()) => Ok(VERIFIER_CLIENT_OVERRIDE.get().unwrap()),
104-
Err(client) => {
105-
drop(client);
106-
Ok(VERIFIER_CLIENT_OVERRIDE.get().unwrap())
107-
}
108-
}
109-
}
110-
11164
/// Get or create the registrar client
11265
///
11366
/// This function returns a cached registrar client if one exists, or creates

keylimectl/src/client/verifier.rs

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,12 @@ pub struct VerifierClient {
159159
#[derive(Debug)]
160160
pub struct VerifierClientBuilder<'a> {
161161
config: Option<&'a Config>,
162-
override_api_version: Option<String>,
163162
}
164163

165164
impl<'a> VerifierClientBuilder<'a> {
166165
/// Create a new builder instance
167166
pub fn new() -> Self {
168-
Self {
169-
config: None,
170-
override_api_version: None,
171-
}
167+
Self { config: None }
172168
}
173169

174170
/// Set the configuration for the client
@@ -177,40 +173,19 @@ impl<'a> VerifierClientBuilder<'a> {
177173
self
178174
}
179175

180-
/// Override the API version after detection
181-
///
182-
/// This allows you to override the detected API version for specific
183-
/// operations while still benefiting from detection for component
184-
/// discovery. Useful for push-model where verifier needs v3.0 but
185-
/// other components may use different versions.
186-
pub fn override_api_version(mut self, version: &str) -> Self {
187-
self.override_api_version = Some(version.to_string());
188-
self
189-
}
190-
191176
/// Build the VerifierClient with automatic API version detection
192177
///
193178
/// This is the recommended way to create a client for production use,
194179
/// as it will automatically detect the optimal API version supported
195180
/// by the verifier service.
196-
///
197-
/// If `override_api_version()` was called, the detected version will
198-
/// be overridden after detection completes.
199181
pub async fn build(self) -> Result<VerifierClient, KeylimectlError> {
200182
let config = self.config.ok_or_else(|| {
201183
KeylimectlError::validation(
202184
"Configuration is required for VerifierClient",
203185
)
204186
})?;
205187

206-
let mut client = VerifierClient::new(config).await?;
207-
208-
// Override API version if specified
209-
if let Some(version) = self.override_api_version {
210-
client.api_version = version;
211-
}
212-
213-
Ok(client)
188+
VerifierClient::new(config).await
214189
}
215190
}
216191

@@ -580,16 +555,11 @@ impl VerifierClient {
580555
) -> Result<Value, KeylimectlError> {
581556
debug!("Adding agent {agent_uuid} to verifier");
582557

583-
// API v3.0+ uses POST /v3.0/agents/ (without agent ID)
584-
// API v2.x uses POST /v2.x/agents/{agent_id}
585-
let url = if self.api_version.parse::<f32>().unwrap_or(2.1) >= 3.0 {
586-
format!("{}/v{}/agents/", self.base.base_url, self.api_version)
587-
} else {
588-
format!(
589-
"{}/v{}/agents/{}",
590-
self.base.base_url, self.api_version, agent_uuid
591-
)
592-
};
558+
// POST to /agents/:agent_uuid for all API versions
559+
let url = format!(
560+
"{}/v{}/agents/{}",
561+
self.base.base_url, self.api_version, agent_uuid
562+
);
593563

594564
debug!(
595565
"POST {url} with data: {}",
@@ -1991,22 +1961,7 @@ mod tests {
19911961
}
19921962

19931963
#[tokio::test]
1994-
async fn test_builder_override_api_version() {
1995-
let config = create_test_config();
1996-
let client = VerifierClient::builder()
1997-
.config(&config)
1998-
.override_api_version("3.0")
1999-
.build()
2000-
.await;
2001-
2002-
assert!(client.is_ok());
2003-
let client = client.unwrap();
2004-
// Should use the overridden version
2005-
assert_eq!(client.api_version, "3.0");
2006-
}
2007-
2008-
#[tokio::test]
2009-
async fn test_builder_without_override() {
1964+
async fn test_builder() {
20101965
let config = create_test_config();
20111966
let client =
20121967
VerifierClient::builder().config(&config).build().await;

keylimectl/src/commands/agent.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -758,18 +758,9 @@ async fn add_agent(
758758
// Step 2: Determine API version and enrollment approach
759759
output.step(2, 4, "Detecting verifier API version");
760760

761-
// For push model, we need to use API v3.0 for verifier requests
762-
let verifier_client = if params.push_model {
763-
factory::get_verifier_with_override("3.0")
764-
.await
765-
.map_err(|e| {
766-
CommandError::resource_error("verifier", e.to_string())
767-
})?
768-
} else {
769-
factory::get_verifier().await.map_err(|e| {
770-
CommandError::resource_error("verifier", e.to_string())
771-
})?
772-
};
761+
let verifier_client = factory::get_verifier().await.map_err(|e| {
762+
CommandError::resource_error("verifier", e.to_string())
763+
})?;
773764

774765
let api_version =
775766
verifier_client.api_version().parse::<f32>().unwrap_or(2.1);

0 commit comments

Comments
 (0)