|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights reserved |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +use super::RecoverableConnection; |
| 5 | +use crate::{common::retry_azure_operation, RetryOptions}; |
| 6 | +use azure_core::{credentials::Secret, error::ErrorKind as AzureErrorKind, error::Result}; |
| 7 | +use azure_core_amqp::{ |
| 8 | + AmqpClaimsBasedSecurity, AmqpClaimsBasedSecurityApis, AmqpConnection, AmqpError, AmqpSession, |
| 9 | + AmqpSessionApis, |
| 10 | +}; |
| 11 | +use std::error::Error; |
| 12 | +use std::sync::Arc; |
| 13 | +use tracing::{debug, warn}; |
| 14 | + |
| 15 | +/// Thin wrapper around the [`AmqpClaimsBasedSecurityApis`] trait that implements the retry functionality. |
| 16 | +/// |
| 17 | +/// A RecoverableClaimsBasedSecurity is a thin wrapper around the [`AmqpClaimsBasedSecurityApis`] trait which implements |
| 18 | +/// the retry functionality. That allows implementations which call into the authorize_path API to not have |
| 19 | +/// to worry about retrying the operation themselves. |
| 20 | +pub(crate) struct RecoverableClaimsBasedSecurity { |
| 21 | + recoverable_connection: Arc<RecoverableConnection>, |
| 22 | +} |
| 23 | + |
| 24 | +impl RecoverableClaimsBasedSecurity { |
| 25 | + /// Creates a new RecoverableClaimsBasedSecurity. |
| 26 | + /// |
| 27 | + /// # Arguments |
| 28 | + /// |
| 29 | + /// * `recoverable_connection` - The recoverable connection to use for authorization. |
| 30 | + pub(super) fn new(recoverable_connection: Arc<RecoverableConnection>) -> Self { |
| 31 | + Self { |
| 32 | + recoverable_connection, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + pub(super) async fn create_claims_based_security( |
| 37 | + connection: Arc<AmqpConnection>, |
| 38 | + retry_options: &RetryOptions, |
| 39 | + ) -> Result<Arc<AmqpClaimsBasedSecurity>> { |
| 40 | + retry_azure_operation( |
| 41 | + || async { |
| 42 | + let session = AmqpSession::new(); |
| 43 | + session.begin(connection.as_ref(), None).await?; |
| 44 | + |
| 45 | + let claims_based_security = Arc::new(AmqpClaimsBasedSecurity::new(session)?); |
| 46 | + |
| 47 | + // Attach the claims_based_security client to the session. |
| 48 | + claims_based_security.attach().await?; |
| 49 | + Ok(claims_based_security) |
| 50 | + }, |
| 51 | + retry_options, |
| 52 | + Some(Self::should_retry_claims_based_security_response), |
| 53 | + ) |
| 54 | + .await |
| 55 | + } |
| 56 | + |
| 57 | + fn should_retry_claims_based_security_response(e: &azure_core::Error) -> bool { |
| 58 | + match e.kind() { |
| 59 | + AzureErrorKind::Amqp => { |
| 60 | + warn!(err=?e, "Amqp operation failed: {:?}", e.source()); |
| 61 | + if let Some(e) = e.source() { |
| 62 | + debug!(err=?e, "Error: {e}"); |
| 63 | + |
| 64 | + if let Some(amqp_error) = e.downcast_ref::<Box<AmqpError>>() { |
| 65 | + RecoverableConnection::should_retry_amqp_error(amqp_error) |
| 66 | + } else if let Some(amqp_error) = e.downcast_ref::<AmqpError>() { |
| 67 | + RecoverableConnection::should_retry_amqp_error(amqp_error) |
| 68 | + } else { |
| 69 | + debug!(err=?e, "Non AMQP error: {e}"); |
| 70 | + false |
| 71 | + } |
| 72 | + } else { |
| 73 | + debug!("No source error found"); |
| 74 | + false |
| 75 | + } |
| 76 | + } |
| 77 | + _ => { |
| 78 | + debug!(err=?e, "Non AMQP error: {e}"); |
| 79 | + false |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] |
| 86 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] |
| 87 | +impl AmqpClaimsBasedSecurityApis for RecoverableClaimsBasedSecurity { |
| 88 | + async fn authorize_path( |
| 89 | + &self, |
| 90 | + path: String, |
| 91 | + token_type: Option<String>, |
| 92 | + secret: &Secret, |
| 93 | + expires_on: time::OffsetDateTime, |
| 94 | + ) -> Result<()> { |
| 95 | + let result = retry_azure_operation( |
| 96 | + || { |
| 97 | + let path = path.clone(); |
| 98 | + let token_type = token_type.clone(); |
| 99 | + let secret = secret.clone(); |
| 100 | + |
| 101 | + async move { |
| 102 | + let claims_based_security_client = |
| 103 | + self.recoverable_connection.ensure_amqp_cbs().await?; |
| 104 | + claims_based_security_client |
| 105 | + .authorize_path(path, token_type, &secret, expires_on) |
| 106 | + .await |
| 107 | + } |
| 108 | + }, |
| 109 | + &self.recoverable_connection.retry_options, |
| 110 | + Some(Self::should_retry_claims_based_security_response), |
| 111 | + ) |
| 112 | + .await?; |
| 113 | + Ok(result) |
| 114 | + } |
| 115 | + |
| 116 | + async fn attach(&self) -> azure_core::Result<()> { |
| 117 | + unimplemented!("AmqpClaimsBasedSecurityClient does not support attach operation"); |
| 118 | + } |
| 119 | + |
| 120 | + async fn detach(self) -> azure_core::Result<()> { |
| 121 | + unimplemented!("AmqpClaimsBasedSecurityClient does not support detach operation"); |
| 122 | + } |
| 123 | +} |
0 commit comments