LCOV - code coverage report
Current view: top level - proxy/src/console - messages.rs (source / functions) Coverage Total Hit
Test: 90b23405d17e36048d3bb64e314067f397803f1b.info Lines: 60.0 % 180 108
Test Date: 2024-09-20 13:14:58 Functions: 19.0 % 200 38

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

Generated by: LCOV version 2.1-beta