LCOV - code coverage report
Current view: top level - proxy/src/auth/backend - classic.rs (source / functions) Coverage Total Hit
Test: 32f4a56327bc9da697706839ed4836b2a00a408f.info Lines: 84.9 % 53 45
Test Date: 2024-02-07 07:37:29 Functions: 13.9 % 36 5

            Line data    Source code
       1              : use super::{ComputeCredentials, ComputeUserInfo};
       2              : use crate::{
       3              :     auth::{self, backend::ComputeCredentialKeys, AuthFlow},
       4              :     compute,
       5              :     config::AuthenticationConfig,
       6              :     console::AuthSecret,
       7              :     metrics::LatencyTimer,
       8              :     sasl,
       9              :     stream::{PqStream, Stream},
      10              : };
      11              : use tokio::io::{AsyncRead, AsyncWrite};
      12              : use tracing::{info, warn};
      13              : 
      14           37 : pub(super) async fn authenticate(
      15           37 :     creds: ComputeUserInfo,
      16           37 :     client: &mut PqStream<Stream<impl AsyncRead + AsyncWrite + Unpin>>,
      17           37 :     config: &'static AuthenticationConfig,
      18           37 :     latency_timer: &mut LatencyTimer,
      19           37 :     secret: AuthSecret,
      20           37 : ) -> auth::Result<ComputeCredentials<ComputeCredentialKeys>> {
      21           37 :     let flow = AuthFlow::new(client);
      22           37 :     let scram_keys = match secret {
      23              :         #[cfg(any(test, feature = "testing"))]
      24              :         AuthSecret::Md5(_) => {
      25            0 :             info!("auth endpoint chooses MD5");
      26            0 :             return Err(auth::AuthError::bad_auth_method("MD5"));
      27              :         }
      28           37 :         AuthSecret::Scram(secret) => {
      29           37 :             info!("auth endpoint chooses SCRAM");
      30           37 :             let scram = auth::Scram(&secret);
      31              : 
      32           37 :             let auth_outcome = tokio::time::timeout(
      33           37 :                 config.scram_protocol_timeout,
      34           37 :                 async {
      35           37 :                     // pause the timer while we communicate with the client
      36           37 :                     let _paused = latency_timer.pause();
      37           37 : 
      38           37 :                     flow.begin(scram).await.map_err(|error| {
      39            0 :                         warn!(?error, "error sending scram acknowledgement");
      40            0 :                         error
      41           74 :                     })?.authenticate().await.map_err(|error| {
      42            0 :                         warn!(?error, "error processing scram messages");
      43            0 :                         error
      44           37 :                     })
      45           37 :                 }
      46           37 :             )
      47           74 :             .await
      48           37 :             .map_err(|error| {
      49            0 :                 warn!("error processing scram messages error = authentication timed out, execution time exeeded {} seconds", config.scram_protocol_timeout.as_secs());
      50            0 :                 auth::io::Error::new(auth::io::ErrorKind::TimedOut, error)
      51           37 :             })??;
      52              : 
      53           37 :             let client_key = match auth_outcome {
      54           34 :                 sasl::Outcome::Success(key) => key,
      55            3 :                 sasl::Outcome::Failure(reason) => {
      56            3 :                     info!("auth backend failed with an error: {reason}");
      57            3 :                     return Err(auth::AuthError::auth_failed(&*creds.user));
      58              :                 }
      59              :             };
      60              : 
      61           34 :             compute::ScramKeys {
      62           34 :                 client_key: client_key.as_bytes(),
      63           34 :                 server_key: secret.server_key.as_bytes(),
      64           34 :             }
      65           34 :         }
      66           34 :     };
      67           34 : 
      68           34 :     Ok(ComputeCredentials {
      69           34 :         info: creds,
      70           34 :         keys: ComputeCredentialKeys::AuthKeys(tokio_postgres::config::AuthKeys::ScramSha256(
      71           34 :             scram_keys,
      72           34 :         )),
      73           34 :     })
      74           37 : }
        

Generated by: LCOV version 2.1-beta