Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce /.well-known endpoint for service discovery information #3632

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion services/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ app.use(json());
app.use(
morgan('combined', {
skip: (req, res) => {
return req.originalUrl.startsWith('/status');
if (req.originalUrl.startsWith('/status')) {
return req.originalUrl.startsWith('/status')
}
if (req.originalUrl.startsWith('/favicon.ico')) {
return req.originalUrl.startsWith('/favicon.ico')
}
if (req.originalUrl.startsWith('/.well-known')) {
return req.originalUrl.startsWith('/.well-known')
}
},
stream: {
write: message => logger.info(message.trim())
Expand Down
4 changes: 4 additions & 0 deletions services/api/src/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const authenticateJWT = async (
if (req.url === '/status') {
return next();
}
// Allow access to status without auth.
if (req.url === '/.well-known/appspecific/sh.lagoon.discovery.json') {
return next();
}

// @ts-ignore
const token = getBearerTokenFromHeader(req.get('Authorization'));
Expand Down
4 changes: 4 additions & 0 deletions services/api/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express, { Request, Response } from 'express';
import statusRoute from './status';
import keysRoute from './keys';
import wellKnown from './well-known';

export function createRouter() {
const router = express.Router();
Expand All @@ -13,6 +14,9 @@ export function createRouter() {
// Fetch the current api status.
router.get('/status', ...statusRoute);

// Fetch the well-known.
router.get('/.well-known/appspecific/sh.lagoon.discovery.json', ...wellKnown);

// Return keys of all customers
router.post('/keys', ...keysRoute);

Expand Down
18 changes: 18 additions & 0 deletions services/api/src/routes/well-known.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Request, Response } from 'express';
import { envHasConfig, getConfigFromEnv } from '../util/config';

const wellKnown = (req: Request, res: Response) => {
let discoverData = {
lagoon_version: getConfigFromEnv('LAGOON_VERSION',''),
authorization_endpoint: getConfigFromEnv('KEYCLOAK_URL', ''),
ssh_token_exchange: {
token_endpoint_host: getConfigFromEnv('SSH_TOKEN_ENDPOINT', ''),
token_endpoint_port: parseInt(getConfigFromEnv('SSH_TOKEN_ENDPOINT_PORT', '22'), 10)
},
webhook_endpoint: getConfigFromEnv('WEBHOOK_URL', ''),
ui_url: getConfigFromEnv('UI_URL',''),
}
res.json(discoverData);
};

export default [wellKnown];
2 changes: 1 addition & 1 deletion tests/checks/check-api-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
headers:
Authorization: "Bearer {{ bearer_token }}"
register: result
until: result.content is match(expected_content)
until: result.content is search(expected_content)
retries: 1
delay: 0
2 changes: 1 addition & 1 deletion tests/tests/features/api-token.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
vars:
url: "{{ lookup('env','API_PROTOCOL') }}://{{ lookup('env','API_HOST') }}:{{ lookup('env','API_PORT') }}/"
bearer_token: "{{ token }}"
expected_content: '{"status":"success","data":{}}'
expected_content: 'success'
tasks:
- ansible.builtin.include_tasks: ../../checks/check-api-request.yaml