Line data Source code
1 : use std::fmt::{self, Display};
2 :
3 : use measured::FixedCardinalityLabel;
4 : use serde::{Deserialize, Serialize};
5 : use smol_str::SmolStr;
6 :
7 : use crate::auth::IpPattern;
8 : use crate::intern::{AccountIdInt, BranchIdInt, EndpointIdInt, ProjectIdInt, RoleNameInt};
9 : use crate::proxy::retry::CouldRetry;
10 :
11 : /// Generic error response with human-readable description.
12 : /// Note that we can't always present it to user as is.
13 0 : #[derive(Debug, Deserialize, Clone)]
14 : pub(crate) struct ControlPlaneErrorMessage {
15 : pub(crate) error: Box<str>,
16 : #[serde(skip)]
17 : pub(crate) http_status_code: http::StatusCode,
18 : pub(crate) status: Option<Status>,
19 : }
20 :
21 : impl ControlPlaneErrorMessage {
22 3 : pub(crate) fn get_reason(&self) -> Reason {
23 3 : self.status
24 3 : .as_ref()
25 3 : .and_then(|s| s.details.error_info.as_ref())
26 3 : .map_or(Reason::Unknown, |e| e.reason)
27 3 : }
28 :
29 0 : pub(crate) fn get_user_facing_message(&self) -> String {
30 : use super::errors::REQUEST_FAILED;
31 0 : self.status
32 0 : .as_ref()
33 0 : .and_then(|s| s.details.user_facing_message.as_ref())
34 0 : .map_or_else(|| {
35 : // Ask @neondatabase/control-plane for review before adding more.
36 0 : match self.http_status_code {
37 : http::StatusCode::NOT_FOUND => {
38 : // Status 404: failed to get a project-related resource.
39 0 : format!("{REQUEST_FAILED}: endpoint cannot be found")
40 : }
41 : http::StatusCode::NOT_ACCEPTABLE => {
42 : // Status 406: endpoint is disabled (we don't allow connections).
43 0 : format!("{REQUEST_FAILED}: endpoint is disabled")
44 : }
45 : http::StatusCode::LOCKED | http::StatusCode::UNPROCESSABLE_ENTITY => {
46 : // Status 423: project might be in maintenance mode (or bad state), or quotas exceeded.
47 0 : format!("{REQUEST_FAILED}: endpoint is temporarily unavailable. Check your quotas and/or contact our support.")
48 : }
49 0 : _ => REQUEST_FAILED.to_owned(),
50 : }
51 0 : }, |m| m.message.clone().into())
52 0 : }
53 : }
54 :
55 : impl Display for ControlPlaneErrorMessage {
56 0 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 0 : let msg: &str = self
58 0 : .status
59 0 : .as_ref()
60 0 : .and_then(|s| s.details.user_facing_message.as_ref())
61 0 : .map_or_else(|| self.error.as_ref(), |m| m.message.as_ref());
62 0 : write!(f, "{msg}")
63 0 : }
64 : }
65 :
66 : impl CouldRetry for ControlPlaneErrorMessage {
67 6 : fn could_retry(&self) -> bool {
68 : // If the error message does not have a status,
69 : // the error is unknown and probably should not retry automatically
70 6 : let Some(status) = &self.status else {
71 2 : return false;
72 : };
73 :
74 : // retry if the retry info is set.
75 4 : if status.details.retry_info.is_some() {
76 4 : return true;
77 0 : }
78 :
79 : // if no retry info set, attempt to use the error code to guess the retry state.
80 0 : let reason = status
81 0 : .details
82 0 : .error_info
83 0 : .map_or(Reason::Unknown, |e| e.reason);
84 :
85 0 : reason.can_retry()
86 6 : }
87 : }
88 :
89 0 : #[derive(Debug, Deserialize, Clone)]
90 : #[allow(dead_code)]
91 : pub(crate) struct Status {
92 : pub(crate) code: Box<str>,
93 : pub(crate) message: Box<str>,
94 : pub(crate) details: Details,
95 : }
96 :
97 0 : #[derive(Debug, Deserialize, Clone)]
98 : pub(crate) struct Details {
99 : pub(crate) error_info: Option<ErrorInfo>,
100 : pub(crate) retry_info: Option<RetryInfo>,
101 : pub(crate) user_facing_message: Option<UserFacingMessage>,
102 : }
103 :
104 0 : #[derive(Copy, Clone, Debug, Deserialize)]
105 : pub(crate) struct ErrorInfo {
106 : pub(crate) reason: Reason,
107 : // Schema could also have `metadata` field, but it's not structured. Skip it for now.
108 : }
109 :
110 0 : #[derive(Clone, Copy, Debug, Deserialize, Default)]
111 : pub(crate) enum Reason {
112 : /// RoleProtected indicates that the role is protected and the attempted operation is not permitted on protected roles.
113 : #[serde(rename = "ROLE_PROTECTED")]
114 : RoleProtected,
115 : /// ResourceNotFound indicates that a resource (project, endpoint, branch, etc.) wasn't found,
116 : /// usually due to the provided ID not being correct or because the subject doesn't have enough permissions to
117 : /// access the requested resource.
118 : /// Prefer a more specific reason if possible, e.g., ProjectNotFound, EndpointNotFound, etc.
119 : #[serde(rename = "RESOURCE_NOT_FOUND")]
120 : ResourceNotFound,
121 : /// ProjectNotFound indicates that the project wasn't found, usually due to the provided ID not being correct,
122 : /// or that the subject doesn't have enough permissions to access the requested project.
123 : #[serde(rename = "PROJECT_NOT_FOUND")]
124 : ProjectNotFound,
125 : /// EndpointNotFound indicates that the endpoint wasn't found, usually due to the provided ID not being correct,
126 : /// or that the subject doesn't have enough permissions to access the requested endpoint.
127 : #[serde(rename = "ENDPOINT_NOT_FOUND")]
128 : EndpointNotFound,
129 : /// BranchNotFound indicates that the branch wasn't found, usually due to the provided ID not being correct,
130 : /// or that the subject doesn't have enough permissions to access the requested branch.
131 : #[serde(rename = "BRANCH_NOT_FOUND")]
132 : BranchNotFound,
133 : /// RateLimitExceeded indicates that the rate limit for the operation has been exceeded.
134 : #[serde(rename = "RATE_LIMIT_EXCEEDED")]
135 : RateLimitExceeded,
136 : /// NonDefaultBranchComputeTimeExceeded indicates that the compute time quota of non-default branches has been
137 : /// exceeded.
138 : #[serde(rename = "NON_PRIMARY_BRANCH_COMPUTE_TIME_EXCEEDED")]
139 : NonDefaultBranchComputeTimeExceeded,
140 : /// ActiveTimeQuotaExceeded indicates that the active time quota was exceeded.
141 : #[serde(rename = "ACTIVE_TIME_QUOTA_EXCEEDED")]
142 : ActiveTimeQuotaExceeded,
143 : /// ComputeTimeQuotaExceeded indicates that the compute time quota was exceeded.
144 : #[serde(rename = "COMPUTE_TIME_QUOTA_EXCEEDED")]
145 : ComputeTimeQuotaExceeded,
146 : /// WrittenDataQuotaExceeded indicates that the written data quota was exceeded.
147 : #[serde(rename = "WRITTEN_DATA_QUOTA_EXCEEDED")]
148 : WrittenDataQuotaExceeded,
149 : /// DataTransferQuotaExceeded indicates that the data transfer quota was exceeded.
150 : #[serde(rename = "DATA_TRANSFER_QUOTA_EXCEEDED")]
151 : DataTransferQuotaExceeded,
152 : /// LogicalSizeQuotaExceeded indicates that the logical size quota was exceeded.
153 : #[serde(rename = "LOGICAL_SIZE_QUOTA_EXCEEDED")]
154 : LogicalSizeQuotaExceeded,
155 : /// RunningOperations indicates that the project already has some running operations
156 : /// and scheduling of new ones is prohibited.
157 : #[serde(rename = "RUNNING_OPERATIONS")]
158 : RunningOperations,
159 : /// ConcurrencyLimitReached indicates that the concurrency limit for an action was reached.
160 : #[serde(rename = "CONCURRENCY_LIMIT_REACHED")]
161 : ConcurrencyLimitReached,
162 : /// LockAlreadyTaken indicates that the we attempted to take a lock that was already taken.
163 : #[serde(rename = "LOCK_ALREADY_TAKEN")]
164 : LockAlreadyTaken,
165 : /// ActiveEndpointsLimitExceeded indicates that the limit of concurrently active endpoints was exceeded.
166 : #[serde(rename = "ACTIVE_ENDPOINTS_LIMIT_EXCEEDED")]
167 : ActiveEndpointsLimitExceeded,
168 : #[default]
169 : #[serde(other)]
170 : Unknown,
171 : }
172 :
173 : impl Reason {
174 0 : pub(crate) fn is_not_found(self) -> bool {
175 0 : matches!(
176 0 : self,
177 : Reason::ResourceNotFound
178 : | Reason::ProjectNotFound
179 : | Reason::EndpointNotFound
180 : | Reason::BranchNotFound
181 : )
182 0 : }
183 :
184 0 : pub(crate) fn can_retry(self) -> bool {
185 0 : match self {
186 : // do not retry role protected errors
187 : // not a transitive error
188 0 : Reason::RoleProtected => false,
189 : // on retry, it will still not be found
190 : Reason::ResourceNotFound
191 : | Reason::ProjectNotFound
192 : | Reason::EndpointNotFound
193 0 : | Reason::BranchNotFound => false,
194 : // we were asked to go away
195 : Reason::RateLimitExceeded
196 : | Reason::NonDefaultBranchComputeTimeExceeded
197 : | Reason::ActiveTimeQuotaExceeded
198 : | Reason::ComputeTimeQuotaExceeded
199 : | Reason::WrittenDataQuotaExceeded
200 : | Reason::DataTransferQuotaExceeded
201 : | Reason::LogicalSizeQuotaExceeded
202 0 : | Reason::ActiveEndpointsLimitExceeded => false,
203 : // transitive error. control plane is currently busy
204 : // but might be ready soon
205 : Reason::RunningOperations
206 : | Reason::ConcurrencyLimitReached
207 0 : | Reason::LockAlreadyTaken => true,
208 : // unknown error. better not retry it.
209 0 : Reason::Unknown => false,
210 : }
211 0 : }
212 : }
213 :
214 0 : #[derive(Copy, Clone, Debug, Deserialize)]
215 : #[allow(dead_code)]
216 : pub(crate) struct RetryInfo {
217 : pub(crate) retry_delay_ms: u64,
218 : }
219 :
220 0 : #[derive(Debug, Deserialize, Clone)]
221 : pub(crate) struct UserFacingMessage {
222 : pub(crate) message: Box<str>,
223 : }
224 :
225 : /// Response which holds client's auth secret, e.g. [`crate::scram::ServerSecret`].
226 : /// Returned by the `/get_endpoint_access_control` API method.
227 0 : #[derive(Deserialize)]
228 : pub(crate) struct GetEndpointAccessControl {
229 : pub(crate) role_secret: Box<str>,
230 :
231 : pub(crate) project_id: Option<ProjectIdInt>,
232 : pub(crate) account_id: Option<AccountIdInt>,
233 :
234 : pub(crate) allowed_ips: Option<Vec<IpPattern>>,
235 : pub(crate) allowed_vpc_endpoint_ids: Option<Vec<String>>,
236 : pub(crate) block_public_connections: Option<bool>,
237 : pub(crate) block_vpc_connections: Option<bool>,
238 :
239 : #[serde(default)]
240 : pub(crate) rate_limits: EndpointRateLimitConfig,
241 : }
242 :
243 0 : #[derive(Copy, Clone, Deserialize, Default)]
244 : pub struct EndpointRateLimitConfig {
245 : pub connection_attempts: ConnectionAttemptsLimit,
246 : }
247 :
248 0 : #[derive(Copy, Clone, Deserialize, Default)]
249 : pub struct ConnectionAttemptsLimit {
250 : pub tcp: Option<LeakyBucketSetting>,
251 : pub ws: Option<LeakyBucketSetting>,
252 : pub http: Option<LeakyBucketSetting>,
253 : }
254 :
255 0 : #[derive(Copy, Clone, Deserialize)]
256 : pub struct LeakyBucketSetting {
257 : pub rps: f64,
258 : pub burst: f64,
259 : }
260 :
261 : /// Response which holds compute node's `host:port` pair.
262 : /// Returned by the `/proxy_wake_compute` API method.
263 0 : #[derive(Debug, Deserialize)]
264 : pub(crate) struct WakeCompute {
265 : pub(crate) address: Box<str>,
266 : pub(crate) server_name: Option<String>,
267 : pub(crate) aux: MetricsAuxInfo,
268 : }
269 :
270 : /// Async response which concludes the console redirect auth flow.
271 : /// Also known as `kickResponse` in the console.
272 : #[derive(Debug, Deserialize)]
273 : pub(crate) struct KickSession<'a> {
274 : /// Session ID is assigned by the proxy.
275 : pub(crate) session_id: &'a str,
276 :
277 : /// Compute node connection params.
278 : #[serde(deserialize_with = "KickSession::parse_db_info")]
279 : pub(crate) result: DatabaseInfo,
280 : }
281 :
282 : impl KickSession<'_> {
283 1 : fn parse_db_info<'de, D>(des: D) -> Result<DatabaseInfo, D::Error>
284 1 : where
285 1 : D: serde::Deserializer<'de>,
286 : {
287 0 : #[derive(Deserialize)]
288 : enum Wrapper {
289 : // Currently, console only reports `Success`.
290 : // `Failure(String)` used to be here... RIP.
291 : Success(DatabaseInfo),
292 : }
293 :
294 1 : Wrapper::deserialize(des).map(|x| match x {
295 1 : Wrapper::Success(info) => info,
296 1 : })
297 1 : }
298 : }
299 :
300 : /// Compute node connection params.
301 0 : #[derive(Deserialize)]
302 : pub(crate) struct DatabaseInfo {
303 : pub(crate) host: Box<str>,
304 : pub(crate) port: u16,
305 : pub(crate) dbname: Box<str>,
306 : pub(crate) user: Box<str>,
307 : /// Console always provides a password, but it might
308 : /// be inconvenient for debug with local PG instance.
309 : pub(crate) password: Option<Box<str>>,
310 : pub(crate) aux: MetricsAuxInfo,
311 : #[serde(default)]
312 : pub(crate) allowed_ips: Option<Vec<IpPattern>>,
313 : #[serde(default)]
314 : pub(crate) allowed_vpc_endpoint_ids: Option<Vec<String>>,
315 : #[serde(default)]
316 : pub(crate) public_access_allowed: Option<bool>,
317 : }
318 :
319 : // Manually implement debug to omit sensitive info.
320 : impl fmt::Debug for DatabaseInfo {
321 0 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
322 0 : f.debug_struct("DatabaseInfo")
323 0 : .field("host", &self.host)
324 0 : .field("port", &self.port)
325 0 : .field("dbname", &self.dbname)
326 0 : .field("user", &self.user)
327 0 : .field("allowed_ips", &self.allowed_ips)
328 0 : .field("allowed_vpc_endpoint_ids", &self.allowed_vpc_endpoint_ids)
329 0 : .finish_non_exhaustive()
330 0 : }
331 : }
332 :
333 : /// Various labels for prometheus metrics.
334 : /// Also known as `ProxyMetricsAuxInfo` in the console.
335 0 : #[derive(Debug, Deserialize, Clone)]
336 : pub(crate) struct MetricsAuxInfo {
337 : pub(crate) endpoint_id: EndpointIdInt,
338 : pub(crate) project_id: ProjectIdInt,
339 : pub(crate) branch_id: BranchIdInt,
340 : // note: we don't use interned strings for compute IDs.
341 : // they churn too quickly and we have no way to clean up interned strings.
342 : pub(crate) compute_id: SmolStr,
343 : #[serde(default)]
344 : pub(crate) cold_start_info: ColdStartInfo,
345 : }
346 :
347 0 : #[derive(Debug, Default, Serialize, Deserialize, Clone, Copy, FixedCardinalityLabel)]
348 : #[serde(rename_all = "snake_case")]
349 : pub enum ColdStartInfo {
350 : #[default]
351 : Unknown,
352 : /// Compute was already running
353 : Warm,
354 : #[serde(rename = "pool_hit")]
355 : #[label(rename = "pool_hit")]
356 : /// Compute was not running but there was an available VM
357 : VmPoolHit,
358 : #[serde(rename = "pool_miss")]
359 : #[label(rename = "pool_miss")]
360 : /// Compute was not running and there were no VMs available
361 : VmPoolMiss,
362 :
363 : // not provided by control plane
364 : /// Connection available from HTTP pool
365 : HttpPoolHit,
366 : /// Cached connection info
367 : WarmCached,
368 : }
369 :
370 : impl ColdStartInfo {
371 0 : pub(crate) fn as_str(self) -> &'static str {
372 0 : match self {
373 0 : ColdStartInfo::Unknown => "unknown",
374 0 : ColdStartInfo::Warm => "warm",
375 0 : ColdStartInfo::VmPoolHit => "pool_hit",
376 0 : ColdStartInfo::VmPoolMiss => "pool_miss",
377 0 : ColdStartInfo::HttpPoolHit => "http_pool_hit",
378 0 : ColdStartInfo::WarmCached => "warm_cached",
379 : }
380 0 : }
381 : }
382 :
383 0 : #[derive(Debug, Deserialize, Clone)]
384 : pub struct EndpointJwksResponse {
385 : pub jwks: Vec<JwksSettings>,
386 : }
387 :
388 0 : #[derive(Debug, Deserialize, Clone)]
389 : pub struct JwksSettings {
390 : pub id: String,
391 : pub jwks_url: url::Url,
392 : #[serde(rename = "provider_name")]
393 : pub _provider_name: String,
394 : pub jwt_audience: Option<String>,
395 : pub role_names: Vec<RoleNameInt>,
396 : }
397 :
398 : #[cfg(test)]
399 : mod tests {
400 : use serde_json::json;
401 :
402 : use super::*;
403 :
404 6 : fn dummy_aux() -> serde_json::Value {
405 6 : json!({
406 6 : "endpoint_id": "endpoint",
407 6 : "project_id": "project",
408 6 : "branch_id": "branch",
409 6 : "compute_id": "compute",
410 6 : "cold_start_info": "unknown",
411 : })
412 6 : }
413 :
414 : #[test]
415 1 : fn parse_kick_session() -> anyhow::Result<()> {
416 : // This is what the console's kickResponse looks like.
417 1 : let json = json!({
418 1 : "session_id": "deadbeef",
419 1 : "result": {
420 1 : "Success": {
421 1 : "host": "localhost",
422 1 : "port": 5432,
423 1 : "dbname": "postgres",
424 1 : "user": "john_doe",
425 1 : "password": "password",
426 1 : "aux": dummy_aux(),
427 : }
428 : }
429 : });
430 1 : serde_json::from_str::<KickSession<'_>>(&json.to_string())?;
431 :
432 1 : Ok(())
433 1 : }
434 :
435 : #[test]
436 1 : fn parse_db_info() -> anyhow::Result<()> {
437 : // with password
438 1 : serde_json::from_value::<DatabaseInfo>(json!({
439 1 : "host": "localhost",
440 1 : "port": 5432,
441 1 : "dbname": "postgres",
442 1 : "user": "john_doe",
443 1 : "password": "password",
444 1 : "aux": dummy_aux(),
445 0 : }))?;
446 :
447 : // without password
448 1 : serde_json::from_value::<DatabaseInfo>(json!({
449 1 : "host": "localhost",
450 1 : "port": 5432,
451 1 : "dbname": "postgres",
452 1 : "user": "john_doe",
453 1 : "aux": dummy_aux(),
454 0 : }))?;
455 :
456 : // new field (forward compatibility)
457 1 : serde_json::from_value::<DatabaseInfo>(json!({
458 1 : "host": "localhost",
459 1 : "port": 5432,
460 1 : "dbname": "postgres",
461 1 : "user": "john_doe",
462 1 : "project": "hello_world",
463 1 : "N.E.W": "forward compatibility check",
464 1 : "aux": dummy_aux(),
465 0 : }))?;
466 :
467 : // with allowed_ips
468 1 : let dbinfo = serde_json::from_value::<DatabaseInfo>(json!({
469 1 : "host": "localhost",
470 1 : "port": 5432,
471 1 : "dbname": "postgres",
472 1 : "user": "john_doe",
473 1 : "password": "password",
474 1 : "aux": dummy_aux(),
475 1 : "allowed_ips": ["127.0.0.1"],
476 0 : }))?;
477 :
478 1 : assert_eq!(
479 : dbinfo.allowed_ips,
480 1 : Some(vec![IpPattern::Single("127.0.0.1".parse()?)])
481 : );
482 :
483 1 : Ok(())
484 1 : }
485 :
486 : #[test]
487 1 : fn parse_wake_compute() -> anyhow::Result<()> {
488 1 : let json = json!({
489 1 : "address": "0.0.0.0",
490 1 : "aux": dummy_aux(),
491 : });
492 1 : serde_json::from_str::<WakeCompute>(&json.to_string())?;
493 1 : Ok(())
494 1 : }
495 :
496 : #[test]
497 1 : fn parse_get_role_secret() -> anyhow::Result<()> {
498 : // Empty `allowed_ips` and `allowed_vpc_endpoint_ids` field.
499 1 : let json = json!({
500 1 : "role_secret": "secret",
501 : });
502 1 : serde_json::from_str::<GetEndpointAccessControl>(&json.to_string())?;
503 1 : let json = json!({
504 1 : "role_secret": "secret",
505 1 : "allowed_ips": ["8.8.8.8"],
506 : });
507 1 : serde_json::from_str::<GetEndpointAccessControl>(&json.to_string())?;
508 1 : let json = json!({
509 1 : "role_secret": "secret",
510 1 : "allowed_vpc_endpoint_ids": ["vpce-0abcd1234567890ef"],
511 : });
512 1 : serde_json::from_str::<GetEndpointAccessControl>(&json.to_string())?;
513 1 : let json = json!({
514 1 : "role_secret": "secret",
515 1 : "allowed_ips": ["8.8.8.8"],
516 1 : "allowed_vpc_endpoint_ids": ["vpce-0abcd1234567890ef"],
517 : });
518 1 : serde_json::from_str::<GetEndpointAccessControl>(&json.to_string())?;
519 1 : let json = json!({
520 1 : "role_secret": "secret",
521 1 : "allowed_ips": ["8.8.8.8"],
522 1 : "allowed_vpc_endpoint_ids": ["vpce-0abcd1234567890ef"],
523 1 : "project_id": "project",
524 : });
525 1 : serde_json::from_str::<GetEndpointAccessControl>(&json.to_string())?;
526 :
527 1 : Ok(())
528 1 : }
529 : }
|