Skip to content

Commit a96b6b6

Browse files
heathsAlex Kinnane
andauthored
Fix broken code behind client_certificate feature (#1706)
* Fix ErrorKind::http_response_from_body() was deleted and this code was never built. This uses the new method * Fix: unwrap authority_host() as we are trying to store a `Url` * Fix: `log` was not inclueded in the package dependencies. Use tracing to be consistent with the rest of the SDK * Add check / tests to build all features in CI to prevent dead code * Fix warnings hidden behind features * Formatting --------- Co-authored-by: Alex Kinnane <[email protected]>
1 parent 8c4caa2 commit a96b6b6

File tree

6 files changed

+20
-24
lines changed

6 files changed

+20
-24
lines changed

sdk/core/src/error/macros.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ mod tests {
7676
}
7777
}
7878

79-
#[derive(thiserror::Error, Debug)]
80-
enum IntermediateError {
81-
#[error("second error")]
82-
Io(#[from] std::io::Error),
83-
}
84-
8579
#[test]
8680
fn ensure_works() {
8781
fn test_ensure(predicate: bool) -> crate::Result<()> {

sdk/identity/examples/azureauth_cli_credential.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use azure_core::auth::TokenCredential;
22
use azure_identity::AzureauthCliCredential;
33
use clap::Parser;
44
use std::error::Error;
5-
use url::Url;
65

76
#[derive(Debug, Parser)]
87
struct Args {

sdk/identity/examples/client_certificate_credentials.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
/// please make sure to set the `send_certificate_chain` option to true otherwise
55
/// the authentication will fail.
66
use azure_core::auth::{Secret, TokenCredential};
7-
use azure_identity::{
8-
ClientCertificateCredential, ClientCertificateCredentialOptions, DefaultAzureCredential,
9-
};
7+
use azure_identity::{ClientCertificateCredential, ClientCertificateCredentialOptions};
108
use azure_security_keyvault::KeyvaultClient;
119
use std::env::var;
1210
use url::Url;
@@ -44,7 +42,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4442
let creds =
4543
ClientCertificateCredential::new(tenant_id, client_id, cert, String::new(), options);
4644

47-
let res = creds
45+
let res = creds?
4846
.get_token(&["https://management.azure.com/.default"])
4947
.await?;
5048
// Let's enumerate the Azure SQL Databases instances

sdk/identity/src/token_credentials/azureauth_cli_credentials.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use crate::token_credentials::cache::TokenCache;
22
use async_process::Command;
33
use azure_core::{
44
auth::{AccessToken, Secret, TokenCredential},
5-
error::{Error, ErrorKind, ResultExt},
5+
error::{Error, ErrorKind},
66
from_json,
77
};
8-
use oauth2::ClientId;
98
use serde::Deserialize;
109
use std::str;
1110
use time::OffsetDateTime;
@@ -40,7 +39,9 @@ mod unix_date_string {
4039

4140
#[derive(Debug, Clone, Deserialize)]
4241
struct CliTokenResponse {
42+
#[allow(dead_code)]
4343
pub user: String,
44+
#[allow(dead_code)]
4445
pub display_name: String,
4546
#[serde(rename = "token")]
4647
pub access_token: Secret,
@@ -211,6 +212,8 @@ mod tests {
211212

212213
let response: CliTokenResponse = from_json(src)?;
213214
assert_eq!(response.access_token.secret(), "security token here");
215+
assert_eq!(response.user, "[email protected]");
216+
assert_eq!(response.display_name, "Example User");
214217
assert_eq!(
215218
response.expires_on,
216219
OffsetDateTime::from_unix_timestamp(1700166595).expect("known valid date")

sdk/identity/src/token_credentials/client_certificate_credentials.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,22 @@ impl ClientCertificateCredential {
116116
client_certificate: C,
117117
client_certificate_pass: P,
118118
options: impl Into<ClientCertificateCredentialOptions>,
119-
) -> ClientCertificateCredential
119+
) -> azure_core::Result<ClientCertificateCredential>
120120
where
121121
C: Into<Secret>,
122122
P: Into<Secret>,
123123
{
124124
let options = options.into();
125-
ClientCertificateCredential {
125+
Ok(ClientCertificateCredential {
126126
tenant_id,
127127
client_id,
128128
client_certificate: client_certificate.into(),
129129
client_certificate_pass: client_certificate_pass.into(),
130130
http_client: options.options().http_client().clone(),
131-
authority_host: options.options().authority_host().clone(),
131+
authority_host: options.options().authority_host()?.clone(),
132132
send_certificate_chain: options.send_certificate_chain(),
133133
cache: TokenCache::new(),
134-
}
134+
})
135135
}
136136

137137
fn sign(jwt: &str, pkey: &PKey<Private>) -> Result<Vec<u8>, ErrorStack> {
@@ -258,8 +258,12 @@ impl ClientCertificateCredential {
258258
let rsp_status = rsp.status();
259259

260260
if !rsp_status.is_success() {
261-
let rsp_body = rsp.into_body().collect().await?;
262-
return Err(ErrorKind::http_response_from_body(rsp_status, &rsp_body).into_error());
261+
let (rsp_status, rsp_headers, rsp_body) = rsp.deconstruct();
262+
let rsp_body = rsp_body.collect().await?;
263+
return Err(
264+
ErrorKind::http_response_from_parts(rsp_status, &rsp_headers, &rsp_body)
265+
.into_error(),
266+
);
263267
}
264268

265269
let response: AadTokenResponse = rsp.json().await?;
@@ -315,13 +319,13 @@ impl ClientCertificateCredential {
315319
)
316320
})?;
317321

318-
Ok(ClientCertificateCredential::new(
322+
ClientCertificateCredential::new(
319323
tenant_id,
320324
client_id,
321325
client_certificate,
322326
client_certificate_password,
323327
options,
324-
))
328+
)
325329
}
326330
}
327331

sdk/storage_blobs/tests/blob.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#![cfg(all(test, feature = "test_e2e", feature = "md5"))]
2-
#[macro_use]
3-
extern crate log;
4-
52
use azure_core::{date, Url};
63
use azure_storage::prelude::*;
74
use azure_storage_blobs::container::operations::ListBlobsResponse;
@@ -11,6 +8,7 @@ use futures::StreamExt;
118
use std::ops::{Add, Deref};
129
use std::time::Duration;
1310
use time::OffsetDateTime;
11+
use tracing::trace;
1412
use uuid::Uuid;
1513

1614
#[tokio::test]

0 commit comments

Comments
 (0)