-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #746 from snyk/feat/retrieve-connections-from-backend
Feat/retrieve connections from backend [HYB-261]
- Loading branch information
Showing
22 changed files
with
361 additions
and
114 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
File renamed without changes.
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,60 @@ | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { makeRequestToDownstream } from '../../common/http/request'; | ||
import { PostFilterPreparedRequest } from '../../common/relay/prepareRequest'; | ||
import { ClientOpts } from '../../common/types/options'; | ||
import { BrokerConnectionApiResponse } from '../types/api'; | ||
|
||
export const retrieveConnectionsForDeployment = async ( | ||
clientOpts: ClientOpts, | ||
universalFilePath: string, | ||
) => { | ||
try { | ||
const tenantId = clientOpts.config.tenantId; | ||
const installId = clientOpts.config.installId; | ||
const deploymentId = clientOpts.config.deploymentId; | ||
const apiVersion = clientOpts.config.apiVersion; | ||
const request: PostFilterPreparedRequest = { | ||
url: `${clientOpts.config.API_BASE_URL}/rest/tenants/${tenantId}/brokers/installs/${installId}/deployments/${deploymentId}/connections?version=${apiVersion}`, | ||
headers: { | ||
'Content-Type': 'application/vnd.api+json', | ||
Authorization: `${clientOpts.accessToken?.authHeader}`, | ||
}, | ||
method: 'GET', | ||
}; | ||
const connectionsResponse = await makeRequestToDownstream(request); | ||
if (connectionsResponse.statusCode != 200) { | ||
const errorBody = JSON.parse(connectionsResponse.body); | ||
throw new Error( | ||
`${connectionsResponse.statusCode}-${errorBody.error}:${errorBody.error_description}`, | ||
); | ||
} | ||
const connections = JSON.parse(connectionsResponse.body) | ||
.data as BrokerConnectionApiResponse[]; | ||
const connectionsObjectForFile = { CONNECTIONS: {} }; | ||
for (let i = 0; i < connections.length; i++) { | ||
connectionsObjectForFile.CONNECTIONS[ | ||
`${connections[i].attributes.name}` | ||
] = { | ||
...connections[i].attributes.configuration.default, | ||
...connections[i].attributes.configuration.required, | ||
}; | ||
connectionsObjectForFile.CONNECTIONS[ | ||
`${connections[i].attributes.name}` | ||
].type = connections[i].attributes.type; | ||
connectionsObjectForFile.CONNECTIONS[ | ||
`${connections[i].attributes.name}` | ||
].identifier = connections[i].id; | ||
} | ||
const universalConfigFileBuffer = readFileSync(universalFilePath); | ||
const universalConfigFile = JSON.parse( | ||
universalConfigFileBuffer.toString() ?? { CONNECTIONS: {} }, | ||
); | ||
universalConfigFile['CONNECTIONS'] = connectionsObjectForFile.CONNECTIONS; | ||
writeFileSync(universalFilePath, JSON.stringify(universalConfigFile)); | ||
return; | ||
} catch (err) { | ||
throw new Error( | ||
`${err} - Error retrieving and loading connections from Deployment ID`, | ||
); | ||
} | ||
}; |
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,29 @@ | ||
import { ConnectionValidation } from './config'; | ||
|
||
export interface BrokerConnectionApiResponse { | ||
id: string; | ||
type: string; | ||
attributes: BrokerConnectionAttributes; | ||
} | ||
export interface BrokerConnectionAttributes { | ||
configuration: { | ||
default: {}; | ||
required: {}; | ||
validations: Array<ConnectionValidation>; | ||
}; | ||
name: string; | ||
deployment_id: string; | ||
secrets?: { | ||
primary: { | ||
encrypted: string; | ||
expires_at: string; | ||
nonce: string; | ||
}; | ||
secondary: { | ||
encrypted: string; | ||
expires_at: string; | ||
nonce: string; | ||
}; | ||
}; | ||
type: string; | ||
} |
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,10 @@ | ||
import { readFileSync, writeFileSync } from 'node:fs'; | ||
|
||
export const cleanUpUniversalFile = ( | ||
universalCfgFilePath = `${__dirname}/../../../../config.universal.json`, | ||
) => { | ||
const fileToCleanUpBuffer = readFileSync(universalCfgFilePath); | ||
const fileToCleanUp = JSON.parse(fileToCleanUpBuffer.toString()); | ||
delete fileToCleanUp['CONNECTIONS']; | ||
writeFileSync(universalCfgFilePath, JSON.stringify(fileToCleanUp)); | ||
}; |
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,11 @@ | ||
export const handleTerminationSignal = (callback: () => void) => { | ||
process.on('SIGINT', () => { | ||
callback(); | ||
process.exit(0); | ||
}); | ||
|
||
process.on('SIGTERM', () => { | ||
callback(); | ||
process.exit(0); | ||
}); | ||
}; |
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.