Skip to content

Commit 3ca39ce

Browse files
committed
♻️(yprovider) support multiple API keys to separate responsibilities
Support for two API keys has been added to the YProvider microservice to decouple responsibilities between the collaboration server and other endpoints. This improves security by scoping keys to specific purposes and ensures a clearer separation of concerns for easier management and debugging.
1 parent 8a93122 commit 3ca39ce

File tree

5 files changed

+10
-4
lines changed

5 files changed

+10
-4
lines changed

src/backend/impress/settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,6 @@ class Base(Configuration):
506506
}
507507

508508
# Y provider microservice
509-
# Note: Be careful, this value is currently the same as in the collaboration service.
510509
Y_PROVIDER_API_KEY = values.Value(
511510
environ_name="Y_PROVIDER_API_KEY",
512511
environ_prefix=None,

src/frontend/servers/y-provider/__tests__/server.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jest.mock('../src/env', () => {
1414
PORT: port,
1515
COLLABORATION_SERVER_ORIGIN: origin,
1616
COLLABORATION_SERVER_SECRET: 'test-secret-api-key',
17+
Y_PROVIDER_API_KEY: 'yprovider-api-key',
1718
};
1819
});
1920

@@ -115,7 +116,7 @@ describe('Server Tests', () => {
115116
const response = await request(app as any)
116117
.post('/api/convert-markdown')
117118
.set('Origin', origin)
118-
.set('Authorization', 'test-secret-api-key');
119+
.set('Authorization', 'yprovider-api-key');
119120

120121
expect(response.status).toBe(400);
121122
expect(response.body.error).toBe('Invalid request: missing content');
@@ -125,7 +126,7 @@ describe('Server Tests', () => {
125126
const response = await request(app as any)
126127
.post('/api/convert-markdown')
127128
.set('Origin', origin)
128-
.set('Authorization', 'test-secret-api-key')
129+
.set('Authorization', 'yprovider-api-key')
129130
.send({
130131
content: '',
131132
});

src/frontend/servers/y-provider/src/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ export const COLLABORATION_SERVER_ORIGIN =
44
process.env.COLLABORATION_SERVER_ORIGIN || 'http://localhost:3000';
55
export const COLLABORATION_SERVER_SECRET =
66
process.env.COLLABORATION_SERVER_SECRET || 'secret-api-key';
7+
export const Y_PROVIDER_API_KEY =
8+
process.env.Y_PROVIDER_API_KEY || 'yprovider-api-key';
79
export const PORT = Number(process.env.PORT || 4444);
810
export const SENTRY_DSN = process.env.SENTRY_DSN || '';

src/frontend/servers/y-provider/src/middlewares.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import * as ws from 'ws';
44
import {
55
COLLABORATION_SERVER_ORIGIN,
66
COLLABORATION_SERVER_SECRET,
7+
Y_PROVIDER_API_KEY,
78
} from '@/env';
89

910
import { logger } from './utils';
1011

12+
const VALID_API_KEYS = [COLLABORATION_SERVER_SECRET, Y_PROVIDER_API_KEY];
13+
1114
export const httpSecurity = (
1215
req: Request,
1316
res: Response,
@@ -27,7 +30,7 @@ export const httpSecurity = (
2730
// Secret API Key check
2831
// Note: Changing this header to Bearer token format will break backend compatibility with this microservice.
2932
const apiKey = req.headers['authorization'];
30-
if (apiKey !== COLLABORATION_SERVER_SECRET) {
33+
if (!apiKey || !VALID_API_KEYS.includes(apiKey)) {
3134
res.status(403).json({ error: 'Forbidden: Invalid API Key' });
3235
return;
3336
}

src/helm/env.d/dev/values.impress.yaml.gotmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ yProvider:
105105
COLLABORATION_LOGGING: true
106106
COLLABORATION_SERVER_ORIGIN: https://impress.127.0.0.1.nip.io
107107
COLLABORATION_SERVER_SECRET: my-secret
108+
Y_PROVIDER_API_KEY: my-secret
108109

109110
ingress:
110111
enabled: true

0 commit comments

Comments
 (0)