Line data Source code
1 : use super::{
2 : ComputeCredentialKeys, ComputeCredentials, ComputeUserInfo, ComputeUserInfoNoEndpoint,
3 : };
4 : use crate::{
5 : auth::{self, AuthFlow},
6 : config::AuthenticationConfig,
7 : console::AuthSecret,
8 : context::RequestMonitoring,
9 : intern::EndpointIdInt,
10 : sasl,
11 : stream::{self, Stream},
12 : };
13 : use tokio::io::{AsyncRead, AsyncWrite};
14 : use tracing::{info, warn};
15 :
16 : /// Compared to [SCRAM](crate::scram), cleartext password auth saves
17 : /// one round trip and *expensive* computations (>= 4096 HMAC iterations).
18 : /// These properties are benefical for serverless JS workers, so we
19 : /// use this mechanism for websocket connections.
20 1 : pub(crate) async fn authenticate_cleartext(
21 1 : ctx: &RequestMonitoring,
22 1 : info: ComputeUserInfo,
23 1 : client: &mut stream::PqStream<Stream<impl AsyncRead + AsyncWrite + Unpin>>,
24 1 : secret: AuthSecret,
25 1 : config: &'static AuthenticationConfig,
26 1 : ) -> auth::Result<ComputeCredentials> {
27 1 : warn!("cleartext auth flow override is enabled, proceeding");
28 1 : ctx.set_auth_method(crate::context::AuthMethod::Cleartext);
29 1 :
30 1 : // pause the timer while we communicate with the client
31 1 : let paused = ctx.latency_timer_pause(crate::metrics::Waiting::Client);
32 1 :
33 1 : let ep = EndpointIdInt::from(&info.endpoint);
34 :
35 1 : let auth_flow = AuthFlow::new(client)
36 1 : .begin(auth::CleartextPassword {
37 1 : secret,
38 1 : endpoint: ep,
39 1 : pool: config.thread_pool.clone(),
40 1 : })
41 0 : .await?;
42 1 : drop(paused);
43 : // cleartext auth is only allowed to the ws/http protocol.
44 : // If we're here, we already received the password in the first message.
45 : // Scram protocol will be executed on the proxy side.
46 2 : let auth_outcome = auth_flow.authenticate().await?;
47 :
48 1 : let keys = match auth_outcome {
49 1 : sasl::Outcome::Success(key) => key,
50 0 : sasl::Outcome::Failure(reason) => {
51 0 : info!("auth backend failed with an error: {reason}");
52 0 : return Err(auth::AuthError::auth_failed(&*info.user));
53 : }
54 : };
55 :
56 1 : Ok(ComputeCredentials { info, keys })
57 1 : }
58 :
59 : /// Workaround for clients which don't provide an endpoint (project) name.
60 : /// Similar to [`authenticate_cleartext`], but there's a specific password format,
61 : /// and passwords are not yet validated (we don't know how to validate them!)
62 1 : pub(crate) async fn password_hack_no_authentication(
63 1 : ctx: &RequestMonitoring,
64 1 : info: ComputeUserInfoNoEndpoint,
65 1 : client: &mut stream::PqStream<Stream<impl AsyncRead + AsyncWrite + Unpin>>,
66 1 : ) -> auth::Result<ComputeCredentials> {
67 1 : warn!("project not specified, resorting to the password hack auth flow");
68 1 : ctx.set_auth_method(crate::context::AuthMethod::Cleartext);
69 1 :
70 1 : // pause the timer while we communicate with the client
71 1 : let _paused = ctx.latency_timer_pause(crate::metrics::Waiting::Client);
72 :
73 1 : let payload = AuthFlow::new(client)
74 1 : .begin(auth::PasswordHack)
75 0 : .await?
76 1 : .get_password()
77 1 : .await?;
78 :
79 1 : info!(project = &*payload.endpoint, "received missing parameter");
80 :
81 : // Report tentative success; compute node will check the password anyway.
82 1 : Ok(ComputeCredentials {
83 1 : info: ComputeUserInfo {
84 1 : user: info.user,
85 1 : options: info.options,
86 1 : endpoint: payload.endpoint,
87 1 : },
88 1 : keys: ComputeCredentialKeys::Password(payload.password),
89 1 : })
90 1 : }
|