Skip to content

Commit d03c5e3

Browse files
committed
keylimectl: Use API version 3.0 when --push-model is passed
Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 6571e13 commit d03c5e3

File tree

2 files changed

+116
-13
lines changed

2 files changed

+116
-13
lines changed

keylimectl/src/client/verifier.rs

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,18 @@ pub struct VerifierClient {
159159
#[derive(Debug)]
160160
pub struct VerifierClientBuilder<'a> {
161161
config: Option<&'a Config>,
162+
api_version: Option<String>,
163+
skip_version_detection: bool,
162164
}
163165

164166
impl<'a> VerifierClientBuilder<'a> {
165167
/// Create a new builder instance
166168
pub fn new() -> Self {
167-
Self { config: None }
169+
Self {
170+
config: None,
171+
api_version: None,
172+
skip_version_detection: false,
173+
}
168174
}
169175

170176
/// Set the configuration for the client
@@ -173,18 +179,55 @@ impl<'a> VerifierClientBuilder<'a> {
173179
self
174180
}
175181

182+
/// Set a specific API version to use
183+
///
184+
/// When set, this version will be used instead of automatic detection.
185+
/// Typically used with `skip_version_detection()` for cases where the
186+
/// API version is known in advance (e.g., push-model requires v3.0).
187+
pub fn api_version(mut self, version: &str) -> Self {
188+
self.api_version = Some(version.to_string());
189+
self
190+
}
191+
192+
/// Skip automatic API version detection
193+
///
194+
/// When set, the client will not attempt to detect the API version
195+
/// from the server. Use with `api_version()` to specify the version
196+
/// explicitly, or the default version (2.1) will be used.
197+
pub fn skip_version_detection(mut self) -> Self {
198+
self.skip_version_detection = true;
199+
self
200+
}
201+
176202
/// Build the VerifierClient with automatic API version detection
177203
///
178204
/// This is the recommended way to create a client for production use,
179205
/// as it will automatically detect the optimal API version supported
180206
/// by the verifier service.
207+
///
208+
/// If `skip_version_detection()` was called, version detection is skipped.
209+
/// If `api_version()` was called, that version is used instead of detection.
181210
pub async fn build(self) -> Result<VerifierClient, KeylimectlError> {
182211
let config = self.config.ok_or_else(|| {
183212
KeylimectlError::validation(
184213
"Configuration is required for VerifierClient",
185214
)
186215
})?;
187216

217+
// If version detection is skipped, create client without detection
218+
if self.skip_version_detection {
219+
let mut client =
220+
VerifierClient::new_without_version_detection(config)?;
221+
222+
// If specific API version was requested, set it
223+
if let Some(version) = self.api_version {
224+
client.api_version = version;
225+
}
226+
227+
return Ok(client);
228+
}
229+
230+
// Otherwise, use normal version detection
188231
VerifierClient::new(config).await
189232
}
190233
}
@@ -274,7 +317,8 @@ impl VerifierClient {
274317
///
275318
/// Initializes a new `VerifierClient` with the provided configuration
276319
/// using the default API version without attempting to detect the
277-
/// server's supported version. This is mainly useful for testing.
320+
/// server's supported version. This is mainly useful for testing and
321+
/// when the API version is known in advance (e.g., push-model).
278322
///
279323
/// # Arguments
280324
///
@@ -290,7 +334,7 @@ impl VerifierClient {
290334
/// - TLS certificate files cannot be read
291335
/// - Certificate/key files are invalid
292336
/// - HTTP client initialization fails
293-
pub(crate) fn new_without_version_detection(
337+
pub fn new_without_version_detection(
294338
config: &Config,
295339
) -> Result<Self, KeylimectlError> {
296340
let base_url = config.verifier_base_url();
@@ -1948,6 +1992,52 @@ mod tests {
19481992
}
19491993
}
19501994

1995+
#[tokio::test]
1996+
async fn test_builder_with_explicit_api_version() {
1997+
let config = create_test_config();
1998+
let client = VerifierClient::builder()
1999+
.config(&config)
2000+
.api_version("3.0")
2001+
.skip_version_detection()
2002+
.build()
2003+
.await;
2004+
2005+
assert!(client.is_ok());
2006+
let client = client.unwrap();
2007+
assert_eq!(client.api_version, "3.0");
2008+
}
2009+
2010+
#[tokio::test]
2011+
async fn test_builder_skip_version_detection() {
2012+
let config = create_test_config();
2013+
let client = VerifierClient::builder()
2014+
.config(&config)
2015+
.skip_version_detection()
2016+
.build()
2017+
.await;
2018+
2019+
assert!(client.is_ok());
2020+
let client = client.unwrap();
2021+
// Should use default version when skipping detection
2022+
assert_eq!(client.api_version, "2.1");
2023+
}
2024+
2025+
#[tokio::test]
2026+
async fn test_builder_with_custom_version() {
2027+
let config = create_test_config();
2028+
let client = VerifierClient::builder()
2029+
.config(&config)
2030+
.api_version("3.0")
2031+
.skip_version_detection()
2032+
.build()
2033+
.await;
2034+
2035+
assert!(client.is_ok());
2036+
let client = client.unwrap();
2037+
// Should use the specified version
2038+
assert_eq!(client.api_version, "3.0");
2039+
}
2040+
19512041
#[test]
19522042
fn test_client_struct_fields() {
19532043
let config = create_test_config();

keylimectl/src/commands/agent.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -772,19 +772,32 @@ async fn add_agent(
772772
let agent_data = agent_data.unwrap();
773773

774774
// Step 2: Determine API version and enrollment approach
775-
output.step(2, 4, "Detecting verifier API version");
776-
777-
let verifier_client = VerifierClient::builder()
778-
.config(config)
779-
.build()
780-
.await
781-
.map_err(|e| {
782-
CommandError::resource_error("verifier", e.to_string())
783-
})?;
775+
// If push_model is explicitly requested, use API v3.0 without detection
776+
let verifier_client = if params.push_model {
777+
output.step(2, 4, "Using API v3.0 for push-model");
778+
VerifierClient::builder()
779+
.config(config)
780+
.api_version("3.0")
781+
.skip_version_detection()
782+
.build()
783+
.await
784+
.map_err(|e| {
785+
CommandError::resource_error("verifier", e.to_string())
786+
})?
787+
} else {
788+
output.step(2, 4, "Detecting verifier API version");
789+
VerifierClient::builder()
790+
.config(config)
791+
.build()
792+
.await
793+
.map_err(|e| {
794+
CommandError::resource_error("verifier", e.to_string())
795+
})?
796+
};
784797

785798
let api_version =
786799
verifier_client.api_version().parse::<f32>().unwrap_or(2.1);
787-
let is_push_model = api_version >= 3.0;
800+
let is_push_model = params.push_model || api_version >= 3.0;
788801

789802
debug!("Detected API version: {api_version}, using push model: {is_push_model}");
790803

0 commit comments

Comments
 (0)