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