Skip to content

Commit

Permalink
fix: rm fwding logic to primary
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlaud committed Jan 29, 2025
1 parent d22aeb4 commit 904809a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lib/server/auth/connectionWatchdog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const disconnectConnectionsWithStaleCreds = async () => {
},
'Cutting off connection.',
);
client.socket!.end();
client.socket?.end();
}
});
}
Expand Down
48 changes: 3 additions & 45 deletions lib/server/routesHandlers/httpRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { log as logger } from '../../logs/logger';
import { getDesensitizedToken } from '../utils/token';
import { getSocketConnections } from '../socket';
import { incrementHttpRequestsTotal } from '../../common/utils/metrics';
import { hostname } from 'node:os';
import { makeStreamingRequestToDownstream } from '../../hybrid-sdk/http/request';
import { PostFilterPreparedRequest } from '../../common/relay/prepareRequest';
import { URL, URLSearchParams } from 'node:url';
import { URLSearchParams } from 'node:url';

export const overloadHttpRequestWithConnectionDetailsMiddleware = async (
req: Request,
Expand All @@ -21,47 +18,8 @@ export const overloadHttpRequestWithConnectionDetailsMiddleware = async (
// check if we have this broker in the connections
if (!connections.has(token)) {
incrementHttpRequestsTotal(true, 'inbound-request');
const localHostname = hostname();
const regex = new RegExp(/-[0-9]{1,2}-[0-1]/);
if (
localHostname &&
localHostname.endsWith('-1') &&
localHostname.match(regex)
) {
const url = new URL(`http://${req.hostname}${req.url}`);
url.hostname = req.hostname.replace(/-[0-9]{1,2}\./, '.');
url.searchParams.append('connection_role', 'primary');

const postFilterPreparedRequest: PostFilterPreparedRequest = {
url: url.toString(),
headers: req.headers,
method: req.method,
};
if (
req.method == 'POST' ||
req.method == 'PUT' ||
req.method == 'PATCH'
) {
postFilterPreparedRequest.body = req.body;
}
logger.debug(
{ url: req.url, method: req.method },
'Making request to primary',
);
try {
const httpResponse = await makeStreamingRequestToDownstream(
postFilterPreparedRequest,
);
res.writeHead(httpResponse.statusCode ?? 500, httpResponse.headers);
return httpResponse.pipe(res);
} catch (err) {
logger.error({ err }, `Error in HTTP middleware: ${err}`);
return res.status(500).send('Error forwarding request to primary.');
}
} else {
logger.warn({ desensitizedToken }, 'no matching connection found');
return res.status(404).json({ ok: false });
}
logger.warn({ desensitizedToken }, 'no matching connection found');
return res.status(404).json({ ok: false });
}

// Grab a first (newest) client from the pool
Expand Down
10 changes: 10 additions & 0 deletions lib/server/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,23 @@ const socket = ({ server, loadedServerOpts }): SocketHandler => {
connectionIdentifier,
);
if (!credsCheckResponse) {
logger.debug(
{ maskedToken: maskToken(connectionIdentifier), brokerClientId },
`Denied auth for Connection ${connectionIdentifier} client Id ${brokerClientId}, role ${role}`,
);
done({
statusCode: 401,
authenticate: 'Bearer',
message: 'Invalid credentials.',
});
return;
}

logger.debug(
{ maskedToken: maskToken(connectionIdentifier), brokerClientId },
`Successful auth for Connection ${connectionIdentifier} client Id ${brokerClientId}, role ${role}`,
);

const decodedJwt = decode(jwt, { complete: true });
const brokerAppClientId = decodedJwt?.payload['azp'] ?? '';
const nowDate = new Date().toISOString();
Expand Down

0 comments on commit 904809a

Please sign in to comment.