Skip to content

Commit ce5f34e

Browse files
pashabitzConvex, Inc.
authored and
Convex, Inc.
committed
[CX-6356] Accept Access Token in all Backend Endpoints for convex deploy (#25759)
GitOrigin-RevId: 325724c9f2dc0e851752243e6a710455686b81af
1 parent 69ebba0 commit ce5f34e

File tree

5 files changed

+40
-25
lines changed

5 files changed

+40
-25
lines changed

crates/application/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,8 @@ impl<RT: Runtime> Application<RT> {
728728
self.database.latest_snapshot()
729729
}
730730

731-
pub fn app_auth(&self) -> Arc<ApplicationAuth> {
732-
self.app_auth.clone()
731+
pub fn app_auth(&self) -> &Arc<ApplicationAuth> {
732+
&self.app_auth
733733
}
734734

735735
pub async fn search_with_compiled_query(
@@ -2097,12 +2097,14 @@ impl<RT: Runtime> Application<RT> {
20972097
) -> anyhow::Result<Identity> {
20982098
let identity = match token {
20992099
AuthenticationToken::Admin(token, acting_as) => {
2100-
let admin_identity = self.key_broker().check_admin_key(&token).context(
2101-
ErrorMetadata::unauthenticated(
2100+
let admin_identity = self
2101+
.app_auth()
2102+
.check_key(token.to_string(), self.instance_name())
2103+
.await
2104+
.context(ErrorMetadata::unauthenticated(
21022105
"BadAdminKey",
21032106
"The provided admin key was invalid for this instance",
2104-
),
2105-
)?;
2107+
))?;
21062108

21072109
match acting_as {
21082110
Some(acting_user) => {

crates/authentication/src/application_auth.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,15 @@ impl ApplicationAuth {
2727
admin_key_or_access_token: String,
2828
instance_name: String,
2929
) -> anyhow::Result<Identity> {
30-
if admin_key_or_access_token.contains('|')
31-
|| self
32-
.key_broker
33-
.is_encrypted_admin_key(&admin_key_or_access_token)
30+
if self
31+
.key_broker
32+
.is_encrypted_admin_key(&admin_key_or_access_token)
3433
{
3534
// assume this is a legacy Deploy Key
36-
// This is either a pipe-delimited deployment specific key
37-
// or an encrypted admin key.
38-
// The latter is used by smoke tests.
3935
self.key_broker.check_admin_key(&admin_key_or_access_token)
4036
} else {
4137
// assume this is an Access Token
42-
// Access Tokens are base64 encoded strings and do not have pipes
43-
// in them
38+
// Access Tokens are base64 encoded strings
4439
self.access_token_auth
4540
.is_authorized(&instance_name, &admin_key_or_access_token)
4641
.await

crates/local_backend/src/admin.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::Context;
2+
use authentication::application_auth::ApplicationAuth;
23
use common::types::MemberId;
34
use errors::ErrorMetadata;
45
use keybroker::{
@@ -17,6 +18,18 @@ pub fn must_be_admin_from_keybroker(
1718
Ok(identity)
1819
}
1920

21+
pub async fn must_be_admin_from_key(
22+
app_auth: &ApplicationAuth,
23+
instance_name: String,
24+
admin_key_or_access_token: String,
25+
) -> anyhow::Result<Identity> {
26+
let identity = app_auth
27+
.check_key(admin_key_or_access_token, instance_name.clone())
28+
.await
29+
.context(bad_admin_key_error(Some(instance_name)))?;
30+
Ok(identity)
31+
}
32+
2033
pub fn must_be_admin(identity: &Identity) -> anyhow::Result<MemberId> {
2134
let member_id = identity
2235
.member_id()

crates/local_backend/src/deploy_config.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ use serde_json::Value as JsonValue;
7373
use value::ConvexObject;
7474

7575
use crate::{
76-
admin::must_be_admin_from_keybroker,
76+
admin::{
77+
must_be_admin_from_key,
78+
must_be_admin_from_keybroker,
79+
},
7780
parse::parse_module_path,
7881
EmptyResponse,
7982
LocalAppState,
@@ -350,11 +353,12 @@ pub async fn get_config_hashes(
350353
State(st): State<LocalAppState>,
351354
Json(req): Json<GetConfigRequest>,
352355
) -> Result<impl IntoResponse, HttpResponseError> {
353-
let identity = must_be_admin_from_keybroker(
354-
st.application.key_broker(),
355-
Some(st.instance_name.clone()),
356+
let identity = must_be_admin_from_key(
357+
st.application.app_auth(),
358+
st.instance_name.clone(),
356359
req.admin_key,
357-
)?;
360+
)
361+
.await?;
358362

359363
let mut tx = st.application.begin(identity).await?;
360364
let (config, modules, udf_config) = ConfigModel::new(&mut tx)

crates/local_backend/src/schema.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use value::{
6060
use crate::{
6161
admin::{
6262
must_be_admin,
63-
must_be_admin_from_keybroker,
63+
must_be_admin_from_key,
6464
},
6565
authentication::ExtractIdentity,
6666
deploy_config::ModuleJson,
@@ -255,11 +255,12 @@ pub async fn prepare_schema_handler(
255255
req: PrepareSchemaArgs,
256256
) -> Result<(Json<PrepareSchemaResponse>, bool), HttpResponseError> {
257257
let bundle = req.bundle.try_into()?;
258-
let identity = must_be_admin_from_keybroker(
259-
st.application.key_broker(),
260-
Some(st.instance_name.clone()),
258+
let identity = must_be_admin_from_key(
259+
st.application.app_auth(),
260+
st.instance_name.clone(),
261261
req.admin_key,
262-
)?;
262+
)
263+
.await?;
263264
let schema = match st.application.evaluate_schema(bundle).await {
264265
Ok(m) => m,
265266
Err(e) => return Err(e.into()),

0 commit comments

Comments
 (0)