Skip to content

Commit

Permalink
Merge pull request #888 from snyk/chore/add-auth-headers-to-all-flows
Browse files Browse the repository at this point in the history
fix: add auth headers on all flows [HYB-724]
  • Loading branch information
aarlaud authored Dec 6, 2024
2 parents 17d84c5 + e76e820 commit 9398c25
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
7 changes: 7 additions & 0 deletions lib/client/config/configHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { getConfig, loadBrokerConfig } from '../../common/config/config';
import version from '../../common/utils/version';
import { CONFIGURATION } from '../../common/types/options';

let globalClientOpts: Record<string, any> = {};

export const getClientOpts = () => {
return globalClientOpts;
};

export const reloadConfig = async (clientOpts) => {
// Reload config with connection
await loadBrokerConfig();
Expand All @@ -14,6 +20,7 @@ export const reloadConfig = async (clientOpts) => {
clientOpts.config,
globalConfig.config,
) as Record<string, any> as CONFIGURATION;
globalClientOpts = clientOpts;
};

export const getClientConfigMetadata = (
Expand Down
9 changes: 6 additions & 3 deletions lib/client/dispatcher/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DispatcherServiceClient,
ServerId,
} from '../dispatcher-service';
import { getClientOpts } from '../../config/configHelpers';

export class HttpDispatcherServiceClient implements DispatcherServiceClient {
private readonly version = '2022-12-01~experimental';
Expand All @@ -24,12 +25,14 @@ export class HttpDispatcherServiceClient implements DispatcherServiceClient {
const path = `/hidden/broker/${params.hashedBrokerToken}/connections/${params.brokerClientId}`;
const url = new URL(path, this.baseUrl);
url.searchParams.append('version', this.version);
const headers = { 'Content-type': 'application/vnd.api+json' };
if (getClientOpts().accessToken) {
headers['Authorization'] = getClientOpts().accessToken?.authHeader;
}
const req: PostFilterPreparedRequest = {
url: url.toString(),
method: 'POST',
headers: {
'Content-type': 'application/vnd.api+json',
},
headers,
body: JSON.stringify({
data: {
attributes: {
Expand Down
6 changes: 4 additions & 2 deletions lib/client/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export const createWebSocket = (
socketSettings['transport'] = {
extraHeaders: {
Authorization: clientOpts.accessToken?.authHeader,
'x-snyk-broker-client-id': identifyingMetadata.clientId,
'x-snyk-broker-client-role': identifyingMetadata.role,
},
};
}
Expand Down Expand Up @@ -171,8 +173,8 @@ export const createWebSocket = (
clientOpts.config.brokerClientConfiguration.common.oauth!.clientSecret,
);

// websocket.transport.extraHeaders['Authorization'] =
// clientOpts.accessToken!.authHeader;
websocket.transport.extraHeaders['Authorization'] =
clientOpts.accessToken!.authHeader;
// websocket.end();
// websocket.open();
timeoutHandlerId = setTimeout(
Expand Down
6 changes: 6 additions & 0 deletions lib/hybrid-sdk/http/downstream-post-stream-to-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { bootstrap } from 'global-agent';
import https from 'https';
import http from 'http';
import { getConfig } from '../../common/config/config';
import { getClientOpts } from '../../client/config/configHelpers';

const BROKER_CONTENT_TYPE = 'application/vnd.broker.stream+octet-stream';

Expand Down Expand Up @@ -68,6 +69,7 @@ class BrokerServerPostResponseHandler {
this.#streamingId
}`,
);

if (this.#serverId) {
url.searchParams.append('server_id', this.#serverId);
}
Expand All @@ -94,6 +96,10 @@ class BrokerServerPostResponseHandler {
? parseInt(this.#config.brokerClientPostTimeout)
: 1200000,
};
if (getClientOpts().accessToken) {
options.headers['authorization'] =
getClientOpts().accessToken.authHeader;
}

this.#brokerSrvPostRequestHandler = client.request(
brokerServerPostRequestUrl,
Expand Down

0 comments on commit 9398c25

Please sign in to comment.