MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / resolve_identity

Method resolve_identity

nodedb/src/control/server/pgwire/handler/core.rs:96–152  ·  view source on GitHub ↗

Resolve the authenticated identity from pgwire client metadata, then overlay the session's superuser tenant override (if any) onto the resolved `tenant_id`. The override is installed via `SET TENANT = ' ' | | DEFAULT` or `SET nodedb.tenant_id = `; the SET handler enforces that only superuser sessions may install one and that no active transaction is in flight. Honoring it here — at

(
        &self,
        client: &C,
        addr: &std::net::SocketAddr,
    )

Source from the content-addressed store, hash-verified

94 /// downstream `identity.tenant_id` read correct without threading the
95 /// session into 13 unrelated dispatchers.
96 pub(crate) fn resolve_identity<C: ClientInfo>(
97 &self,
98 client: &C,
99 addr: &std::net::SocketAddr,
100 ) -> PgWireResult<AuthenticatedIdentity> {
101 let username = client
102 .metadata()
103 .get("user")
104 .cloned()
105 .unwrap_or_else(|| "unknown".to_string());
106
107 let mut identity = match self.auth_mode {
108 AuthMode::Trust => {
109 // Strict resolution: `post_startup` has already ensured the
110 // user exists (either because it was already in the store
111 // or via the bootstrap auto-create path on an empty store),
112 // so any miss here is a genuine unknown user.
113 self.state
114 .credentials
115 .to_identity(&username, AuthMethod::Trust)
116 .ok_or_else(|| {
117 PgWireError::UserError(Box::new(ErrorInfo::new(
118 "FATAL".to_owned(),
119 "28000".to_owned(),
120 format!("trust auth: user '{username}' does not exist"),
121 )))
122 })?
123 }
124 AuthMode::Password | AuthMode::Certificate => self
125 .state
126 .credentials
127 .to_identity(&username, AuthMethod::ScramSha256)
128 .ok_or_else(|| {
129 PgWireError::UserError(Box::new(ErrorInfo::new(
130 "FATAL".to_owned(),
131 "28000".to_owned(),
132 format!("authenticated user '{username}' not found in credential store"),
133 )))
134 })?,
135 };
136
137 if let Some(effective) = self.sessions.get_effective_tenant_id(addr) {
138 // The SET handler guarantees the override only gets installed for
139 // superuser sessions. If somehow a non-superuser carries one
140 // (e.g. an ALTER USER demotion landed after the override was
141 // installed), treat the override as cleared — the identity-bound
142 // tenant is the safe fallback. Drop the override so future
143 // requests on this session don't keep paying the check.
144 if identity.is_superuser {
145 identity.tenant_id = effective;
146 } else {
147 self.sessions.set_effective_tenant_id(addr, None);
148 }
149 }
150
151 Ok(identity)
152 }
153

Callers 2

do_queryMethod · 0.80
execute_preparedMethod · 0.80

Calls 5

to_stringMethod · 0.80
getMethod · 0.45
to_identityMethod · 0.45

Tested by

no test coverage detected