Skip to content

Commit b684730

Browse files
authored
Merge pull request #55 from elasticio/secrets
Secrets
2 parents d1abd04 + af1a742 commit b684730

File tree

7 files changed

+4547
-272
lines changed

7 files changed

+4547
-272
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.2.0 (November 12, 2021)
2+
* Added `fetchAllSecretsForWorkspace` method to `PlatformApiLogicClient`
3+
14
## 1.1.7 (August 30, 2021)
25
* Added `PlatformApiRestClient` and `PlatformApiLogicClient` classes from `IPaaS-core-component`
36

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ const Client = new PlatformApiLogicClient(emitter, cfg);
170170
#### List of methods
171171
- fetchAllFlowsForWorkspace(options) - Fetch all flows for a given workspace
172172
- fetchAllCredentialsForWorkspace(options) - Fetch all credentials for a given workspace
173+
- fetchAllSecretsForWorkspace - Fetch all secrets for a given workspace
173174
- fetchComponentsAccessibleFromContract(options) - Fetch All Components Accessible From a Given Workspace
174175
- splitParallelization(maxParallelization, splitFactor) - Helping method to calculate right number of parallel calls
175176
- fetchFlowList(options) - Fetches a list of flows

lib/platformApi/PlatformApiLogicClient.ts

+51-6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,40 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
8080
}));
8181
}
8282

83+
/**
84+
* Fetch all credentials for a given workspace
85+
* @param {string} options.workspaceId
86+
* @returns {Promise<[{{
87+
* secretId: string,
88+
* secretName: string,
89+
* componentIds: string[],
90+
* }}]>}
91+
*/
92+
async fetchAllSecretsForWorkspace(options: any = {}) {
93+
const { workspaceId } = options;
94+
if (!workspaceId) throw new Error(`workspaceId not provided, can't fetch secrets`)
95+
const secrets = await this.makeRequest({ method: 'GET', url: `/workspaces/${workspaceId}/secrets` });
96+
const resp: any = [];
97+
98+
for (const secret of secrets.data) {
99+
const secretId = secret.id;
100+
const secretName = secret.attributes.name.trim();
101+
let componentIds: any = [];
102+
try {
103+
if (secret.relationships.component) componentIds.push(secret.relationships.component.data.id);
104+
if (secret.relationships.auth_client) {
105+
const clientId = secret.relationships.auth_client.data.id;
106+
const clientResponse = await this.makeRequest({ method: 'GET', url: `/auth-clients/${clientId}` });
107+
componentIds = clientResponse.data.relationships.components.data.map(x => x.id);
108+
}
109+
} catch (e: any) {
110+
this.emitter.logger.info(`Can't find related to secret component - ${e.message}`)
111+
}
112+
resp.push({ secretId, secretName, componentIds });
113+
}
114+
return resp;
115+
}
116+
83117
/**
84118
* Fetch All Components Accessible From a Given Workspace
85119
* @param {string} options.contractId Contract ID
@@ -147,10 +181,7 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
147181
const workspaces = await this.fetchWorkspaceList({});
148182
if (!workspaceId) {
149183
const nonFlatFlows = await mapLimit(workspaces, realSplitFactor,
150-
async workspace => this.fetchAllFlowsForWorkspace({
151-
parallelCalls: parallelizationPerTask,
152-
workspaceId: workspace.workspaceId,
153-
}));
184+
async workspace => this.fetchAllFlowsForWorkspace({ parallelCalls: parallelizationPerTask, workspaceId: workspace.workspaceId }));
154185
flows = nonFlatFlows.flat();
155186
} else {
156187
flows = await this.fetchAllFlowsForWorkspace({
@@ -244,7 +275,7 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
244275
/* eslint-disable-next-line no-param-reassign */
245276
soFar[contract.id] = contract;
246277
return soFar;
247-
}, {});
278+
}, {});
248279

249280
const nonFlatWorkspaces = await mapLimit(
250281
contracts,
@@ -521,7 +552,7 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
521552
/* eslint-disable-next-line no-param-reassign */
522553
soFar[sample.sampleId] = sample.sample;
523554
return soFar;
524-
}, {});
555+
}, {});
525556
flow.attributes.graph.nodes
526557
.filter(node => node.selected_data_samples)
527558
.forEach((node) => {
@@ -562,6 +593,20 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
562593
}
563594
});
564595

596+
const secretsList = await this.fetchAllSecretsForWorkspace({
597+
workspaceId: flow.relationships.workspace.data.id,
598+
});
599+
flow.attributes.graph.nodes.forEach((node) => {
600+
if (node.secret_id) {
601+
const matchingSecrets = secretsList.filter(secret => secret.secretId === node.secret_id);
602+
if (matchingSecrets.length !== 1) throw new Error('Expected a single matching secret');
603+
/* eslint-disable-next-line no-param-reassign */
604+
node.secret_id = {
605+
secretId: matchingSecrets[0].secretId,
606+
secretName: matchingSecrets[0].secretName,
607+
};
608+
}
609+
});
565610
// Enrich command and component Id fields
566611
flow.attributes.graph.nodes.forEach((node) => {
567612
const commandParts = node.command.split(/[/:@]/);

0 commit comments

Comments
 (0)