Skip to content

Commit da645ad

Browse files
committed
chore: align
1 parent 87882e8 commit da645ad

File tree

7 files changed

+63
-21
lines changed

7 files changed

+63
-21
lines changed

packages/cubejs-api-gateway/src/SubscriptionServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class SubscriptionServer {
5353
}
5454

5555
if (message.authorization) {
56-
authContext = { isSubscription: true };
56+
authContext = { isSubscription: true, protocol: 'ws' };
5757
await this.apiGateway.checkAuthFn(authContext, message.authorization);
5858
const acceptanceResult = await this.contextAcceptor(authContext);
5959
if (!acceptanceResult.accepted) {

packages/cubejs-api-gateway/test/auth.test.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ class ApiGatewayOpenAPI extends ApiGateway {
3030
public async shutdownSQLServer(): Promise<void> {
3131
try {
3232
await this.sqlServer.shutdown('fast');
33-
} catch (error) {
34-
console.log(`Error while shutting down server: ${error}`);
33+
} finally {
34+
this.isRunning = null;
3535
}
36-
37-
this.isRunning = null;
3836
}
3937
}
4038

@@ -110,10 +108,15 @@ describe('test authorization with native gateway', () => {
110108
});
111109

112110
afterAll(async () => {
113-
await apiGateway.shutdownSQLServer();
111+
try {
112+
await apiGateway.shutdownSQLServer();
113+
} catch (error) {
114+
// TODO: Figure out, why ApiGatewayServer cannot shutdown!?
115+
console.log(`Error while shutting down server: ${error}`);
116+
}
114117
});
115118

116-
it('default authorization', async () => {
119+
it('default authorization - success', async () => {
117120
const token = generateAuthToken({ uid: 5, });
118121

119122
await request(app)
@@ -128,6 +131,35 @@ describe('test authorization with native gateway', () => {
128131

129132
await apiGateway.shutdownSQLServer();
130133
});
134+
135+
it('default authorization - wrong secret', async () => {
136+
const badToken = generateAuthToken({ uid: 5, }, {}, 'bad');
137+
138+
await request(app)
139+
.get('/cubejs-api/v2/stream')
140+
.set('Authorization', `${badToken}`)
141+
.expect(403);
142+
143+
// No bad logs
144+
expect(loggerMock.mock.calls.length).toEqual(0);
145+
// We should not call js handler, request should go into rust code
146+
expect(handlerMock.mock.calls.length).toEqual(0);
147+
148+
await apiGateway.shutdownSQLServer();
149+
});
150+
151+
it('default authorization - missing auth header', async () => {
152+
await request(app)
153+
.get('/cubejs-api/v2/stream')
154+
.expect(403);
155+
156+
// No bad logs
157+
expect(loggerMock.mock.calls.length).toEqual(0);
158+
// We should not call js handler, request should go into rust code
159+
expect(handlerMock.mock.calls.length).toEqual(0);
160+
161+
await apiGateway.shutdownSQLServer();
162+
});
131163
});
132164

133165
describe('test authorization', () => {

packages/cubejs-backend-native/js/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ export interface ContextToApiScopesPayload {
4343

4444
export type ContextToApiScopesResponse = string[];
4545

46+
export interface CheckAuthPayloadRequestMeta extends BaseMeta {}
47+
4648
export interface CheckAuthPayload {
47-
request: Request<undefined>,
49+
request: Request<CheckAuthPayloadRequestMeta>,
4850
token: string,
4951
}
5052

packages/cubejs-backend-native/src/auth.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ impl SqlAuthService for NodeBridgeAuthService {
121121

122122
#[derive(Debug, Serialize)]
123123
struct CheckAuthTransportRequest {
124-
request: TransportRequest,
125-
req: GatewayCheckAuthRequest,
124+
request: GatewayCheckAuthRequest,
126125
token: String,
127126
}
128127

@@ -154,19 +153,13 @@ type ContextToApiScopesTransportResponse = Vec<String>;
154153
impl GatewayAuthService for NodeBridgeAuthService {
155154
async fn authenticate(
156155
&self,
157-
req: GatewayCheckAuthRequest,
156+
request: GatewayCheckAuthRequest,
158157
token: String,
159158
) -> Result<GatewayAuthenticateResponse, CubeError> {
160159
trace!("[auth] Request ->");
161160

162-
let request_id = Uuid::new_v4().to_string();
163-
164161
let extra = serde_json::to_string(&CheckAuthTransportRequest {
165-
request: TransportRequest {
166-
id: format!("{}-span-1", request_id),
167-
meta: None,
168-
},
169-
req,
162+
request,
170163
token: token.clone(),
171164
})?;
172165
let response: CheckAuthTransportResponse = call_js_with_channel_as_callback(

packages/cubejs-backend-native/src/gateway/auth_middleware.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::gateway::http_error::HttpError;
22
use crate::gateway::state::ApiGatewayStateRef;
33
use crate::gateway::{GatewayAuthContextRef, GatewayAuthService, GatewayCheckAuthRequest};
44
use axum::extract::State;
5+
use axum::http::HeaderValue;
56
use axum::response::IntoResponse;
67

78
#[derive(Debug, Clone)]
@@ -15,6 +16,18 @@ impl AuthExtension {
1516
}
1617
}
1718

19+
fn parse_token(header_value: &HeaderValue) -> Result<&str, HttpError> {
20+
let trimmed = header_value.to_str()?.trim();
21+
22+
if let Some(stripped) = trimmed.strip_prefix("Bearer ") {
23+
Ok(stripped)
24+
} else if let Some(stripped) = trimmed.strip_prefix("bearer ") {
25+
Ok(stripped)
26+
} else {
27+
Ok(trimmed)
28+
}
29+
}
30+
1831
pub async fn gateway_auth_middleware(
1932
State(state): State<ApiGatewayStateRef>,
2033
mut req: axum::extract::Request,
@@ -26,6 +39,8 @@ pub async fn gateway_auth_middleware(
2639
));
2740
};
2841

42+
let bearer_token = parse_token(token_header_value)?;
43+
2944
let auth = state
3045
.injector_ref()
3146
.get_service_typed::<dyn GatewayAuthService>()
@@ -35,7 +50,7 @@ pub async fn gateway_auth_middleware(
3550
GatewayCheckAuthRequest {
3651
protocol: "http".to_string(),
3752
},
38-
token_header_value.to_str()?.to_string(),
53+
bearer_token.to_string(),
3954
);
4055

4156
let auth_response = auth_fut

packages/cubejs-backend-native/src/gateway/auth_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct GatewayContextToApiScopesResponse {
3131
pub trait GatewayAuthService: Send + Sync + Debug {
3232
async fn authenticate(
3333
&self,
34-
request: GatewayCheckAuthRequest,
34+
req: GatewayCheckAuthRequest,
3535
token: String,
3636
) -> Result<GatewayAuthenticateResponse, CubeError>;
3737

packages/cubejs-backend-native/src/gateway/handlers/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub async fn stream_handler_v2(
2323
Ok((
2424
StatusCode::NOT_IMPLEMENTED,
2525
Json(HandlerResponse {
26-
message: "Not implemented".to_string(),
26+
message: "/v2/stream is not implemented".to_string(),
2727
}),
2828
))
2929
}

0 commit comments

Comments
 (0)