Skip to content
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

add timeout to IMDS when used in DefaultAzureCredential #1016

Merged
merged 5 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ base64 = "0.13.0"
uuid = { version = "1.0", features = ["v4"] }
# work around https://github.com/rust-lang/rust/issues/63033
fix-hidden-lifetime-bug = "0.2"
pin-project = "1.0"

[dev-dependencies]
reqwest = { version = "0.11", features = ["json"], default-features = false }
Expand Down
1 change: 1 addition & 0 deletions sdk/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub mod development;
pub mod device_code_flow;
mod oauth2_http_client;
pub mod refresh_token;
mod timeout;
mod token_credentials;

pub use crate::token_credentials::*;
70 changes: 70 additions & 0 deletions sdk/identity/src/timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2020 Yoshua Wuyts
//
// based on https://crates.io/crates/futures-time
// Licensed under either of Apache License, Version 2.0 or MIT license at your option.

use azure_core::sleep::{sleep, Sleep};
use futures::Future;
use std::time::Duration;
use std::{
pin::Pin,
task::{Context, Poll},
};

#[pin_project::pin_project]
#[derive(Debug)]
pub(crate) struct Timeout<F, D> {
#[pin]
future: F,
#[pin]
deadline: D,
completed: bool,
}

impl<F, D> Timeout<F, D> {
pub(crate) fn new(future: F, deadline: D) -> Self {
Self {
future,
deadline,
completed: false,
}
}
}

impl<F: Future, D: Future> Future for Timeout<F, D> {
type Output = azure_core::Result<F::Output>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();

assert!(!*this.completed, "future polled after completing");

match this.future.poll(cx) {
Poll::Ready(v) => {
*this.completed = true;
Poll::Ready(Ok(v))
}
Poll::Pending => match this.deadline.poll(cx) {
Poll::Ready(_) => {
*this.completed = true;
Poll::Ready(Err(azure_core::error::Error::with_message(
azure_core::error::ErrorKind::Other,
|| String::from("operation timed out"),
)))
}
Poll::Pending => Poll::Pending,
},
}
}
}

pub(crate) trait TimeoutExt: Future {
fn timeout(self, duration: Duration) -> Timeout<Self, Sleep>
where
Self: Sized,
{
Timeout::new(self, sleep(duration))
}
}

impl<T> TimeoutExt for T where T: Future {}
39 changes: 23 additions & 16 deletions sdk/identity/src/token_credentials/default_credentials.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use super::{AzureCliCredential, ImdsManagedIdentityCredential};
use azure_core::auth::{TokenCredential, TokenResponse};
use azure_core::error::{Error, ErrorKind, ResultExt};
use crate::{
timeout::TimeoutExt,
{AzureCliCredential, ImdsManagedIdentityCredential},
};
use azure_core::{
auth::{TokenCredential, TokenResponse},
error::{Error, ErrorKind, ResultExt},
};
use std::time::Duration;

#[derive(Debug)]
/// Provides a mechanism of selectively disabling credentials used for a `DefaultAzureCredential` instance
Expand Down Expand Up @@ -91,10 +97,19 @@ impl TokenCredential for DefaultAzureCredentialEnum {
)
}
DefaultAzureCredentialEnum::ManagedIdentity(credential) => {
credential.get_token(resource).await.context(
ErrorKind::Credential,
"error getting managed identity credential",
)
// IMSD timeout is only limited to 1 second when used in DefaultAzureCredential
credential
.get_token(resource)
.timeout(Duration::from_secs(1))
.await
.context(
ErrorKind::Credential,
"getting managed identity credential timed out",
)?
.context(
ErrorKind::Credential,
"error getting managed identity credential",
)
}
DefaultAzureCredentialEnum::AzureCli(credential) => {
credential.get_token(resource).await.context(
Expand Down Expand Up @@ -128,15 +143,7 @@ impl DefaultAzureCredential {

impl Default for DefaultAzureCredential {
fn default() -> Self {
DefaultAzureCredential {
sources: vec![
DefaultAzureCredentialEnum::Environment(super::EnvironmentCredential::default()),
DefaultAzureCredentialEnum::ManagedIdentity(
ImdsManagedIdentityCredential::default(),
),
DefaultAzureCredentialEnum::AzureCli(AzureCliCredential::new()),
],
}
DefaultAzureCredentialBuilder::new().build()
}
}

Expand Down