-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
521 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"//": "private refers to what's internal to snyk, i.e. the snyk.io server", | ||
"private": [ | ||
{ | ||
"//": "send any type of request to our connected clients", | ||
"method": "any", | ||
"path": "/*" | ||
} | ||
], | ||
"public": [ | ||
{ | ||
"//": "send any type of request to our connected clients", | ||
"method": "any", | ||
"path": "/*" | ||
} | ||
] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { getConfig } from '../../common/config/config'; | ||
import { PostFilterPreparedRequest } from '../../common/relay/prepareRequest'; | ||
import version from '../../common/utils/version'; | ||
import { | ||
HttpResponse, | ||
makeRequestToDownstream, | ||
} from '../../hybrid-sdk/http/request'; | ||
import { Role } from '../types/client'; | ||
|
||
export interface BrokerServerConnectionParams { | ||
connectionIdentifier: string; | ||
brokerClientId: string; | ||
authorization: string; | ||
role: Role; | ||
serverId: number; | ||
} | ||
export const renewBrokerServerConnection = async ( | ||
brokerServerConnectionParams: BrokerServerConnectionParams, | ||
): Promise<HttpResponse> => { | ||
const clientConfig = getConfig(); | ||
const apiHostname = clientConfig.API_BASE_URL; | ||
const body = { | ||
data: { | ||
type: 'broker_connection', | ||
attributes: { | ||
broker_client_id: brokerServerConnectionParams.brokerClientId, | ||
}, | ||
}, | ||
}; | ||
const url = new URL( | ||
`${apiHostname}/hidden/brokers/connections/${brokerServerConnectionParams.connectionIdentifier}/auth/refresh`, | ||
); | ||
url.searchParams.append('connection_role', brokerServerConnectionParams.role); | ||
if (brokerServerConnectionParams.serverId) { | ||
url.searchParams.append( | ||
'serverId', | ||
`${brokerServerConnectionParams.serverId}`, | ||
); | ||
} | ||
const req: PostFilterPreparedRequest = { | ||
url: url.toString(), | ||
headers: { | ||
authorization: brokerServerConnectionParams.authorization, | ||
'user-agent': `Snyk Broker Client ${version}`, | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
}; | ||
return await makeRequestToDownstream(req); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { getConfig } from '../../common/config/config'; | ||
import { PostFilterPreparedRequest } from '../../common/relay/prepareRequest'; | ||
import { makeSingleRawRequestToDownstream } from '../../hybrid-sdk/http/request'; | ||
import { log as logger } from '../../logs/logger'; | ||
|
||
export const validateBrokerClientCredentials = async ( | ||
authHeaderValue: string, | ||
brokerClientId: string, | ||
brokerConnectionIdentifier: string, | ||
) => { | ||
const body = { | ||
data: { | ||
type: 'broker_connection', | ||
attributes: { | ||
broker_client_id: brokerClientId, | ||
}, | ||
}, | ||
}; | ||
|
||
const req: PostFilterPreparedRequest = { | ||
url: `${ | ||
getConfig().apiHostname | ||
}/hidden/brokers/connections/${brokerConnectionIdentifier}/auth/validate?version=2024-02-08~experimental`, | ||
headers: { | ||
authorization: authHeaderValue, | ||
'Content-type': 'application/vnd.api+json', | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
}; | ||
logger.debug({ req }, `Validate Broker Client Credentials request`); | ||
const response = await makeSingleRawRequestToDownstream(req); | ||
logger.debug( | ||
{ validationResponseCode: response.statusCode }, | ||
'Validate Broker Client Credentials response', | ||
); | ||
if (response.statusCode === 201) { | ||
return true; | ||
} else { | ||
logger.debug( | ||
{ statusCode: response.statusCode, message: response.statusText }, | ||
`Broker ${brokerConnectionIdentifier} client ID ${brokerClientId} failed validation.`, | ||
); | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { getConfig } from '../../common/config/config'; | ||
import { getSocketConnections } from '../socket'; | ||
import { log as logger } from '../../logs/logger'; | ||
|
||
export const disconnectConnectionsWithStaleCreds = async () => { | ||
const connections = getSocketConnections(); | ||
const connectionsIterator = connections.entries(); | ||
for (const [identifier, connection] of connectionsIterator) { | ||
connection.forEach((client) => { | ||
if (!isDateWithinAnHourAndFiveSec(client.credsValidationTime!)) { | ||
logger.debug( | ||
{ | ||
connection: `${identifier}`, | ||
credsLastValidated: client.credsValidationTime, | ||
}, | ||
'Cutting off connection.', | ||
); | ||
client.socket!.end(); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
const isDateWithinAnHourAndFiveSec = (date: string): boolean => { | ||
const dateInMs = new Date(date); // Convert ISO string to Date | ||
const now = Date.now(); // Get current time in milliseconds | ||
const staleConnectionsCleanupInterval = | ||
getConfig().STALE_CONNECTIONS_CLEANUP_FREQUENCY ?? 65 * 60 * 1000; // 1h05 hour in milliseconds | ||
return now - dateInMs.getTime() < staleConnectionsCleanupInterval; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.