Skip to content

Commit

Permalink
Merge pull request #55 from elasticio/secrets
Browse files Browse the repository at this point in the history
Secrets
  • Loading branch information
if0s authored Nov 10, 2021
2 parents d1abd04 + af1a742 commit b684730
Show file tree
Hide file tree
Showing 7 changed files with 4,547 additions and 272 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.2.0 (November 12, 2021)
* Added `fetchAllSecretsForWorkspace` method to `PlatformApiLogicClient`

## 1.1.7 (August 30, 2021)
* Added `PlatformApiRestClient` and `PlatformApiLogicClient` classes from `IPaaS-core-component`

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const Client = new PlatformApiLogicClient(emitter, cfg);
#### List of methods
- fetchAllFlowsForWorkspace(options) - Fetch all flows for a given workspace
- fetchAllCredentialsForWorkspace(options) - Fetch all credentials for a given workspace
- fetchAllSecretsForWorkspace - Fetch all secrets for a given workspace
- fetchComponentsAccessibleFromContract(options) - Fetch All Components Accessible From a Given Workspace
- splitParallelization(maxParallelization, splitFactor) - Helping method to calculate right number of parallel calls
- fetchFlowList(options) - Fetches a list of flows
Expand Down
57 changes: 51 additions & 6 deletions lib/platformApi/PlatformApiLogicClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
}));
}

/**
* Fetch all credentials for a given workspace
* @param {string} options.workspaceId
* @returns {Promise<[{{
* secretId: string,
* secretName: string,
* componentIds: string[],
* }}]>}
*/
async fetchAllSecretsForWorkspace(options: any = {}) {
const { workspaceId } = options;
if (!workspaceId) throw new Error(`workspaceId not provided, can't fetch secrets`)
const secrets = await this.makeRequest({ method: 'GET', url: `/workspaces/${workspaceId}/secrets` });
const resp: any = [];

for (const secret of secrets.data) {
const secretId = secret.id;
const secretName = secret.attributes.name.trim();
let componentIds: any = [];
try {
if (secret.relationships.component) componentIds.push(secret.relationships.component.data.id);
if (secret.relationships.auth_client) {
const clientId = secret.relationships.auth_client.data.id;
const clientResponse = await this.makeRequest({ method: 'GET', url: `/auth-clients/${clientId}` });
componentIds = clientResponse.data.relationships.components.data.map(x => x.id);
}
} catch (e: any) {
this.emitter.logger.info(`Can't find related to secret component - ${e.message}`)
}
resp.push({ secretId, secretName, componentIds });
}
return resp;
}

/**
* Fetch All Components Accessible From a Given Workspace
* @param {string} options.contractId Contract ID
Expand Down Expand Up @@ -147,10 +181,7 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
const workspaces = await this.fetchWorkspaceList({});
if (!workspaceId) {
const nonFlatFlows = await mapLimit(workspaces, realSplitFactor,
async workspace => this.fetchAllFlowsForWorkspace({
parallelCalls: parallelizationPerTask,
workspaceId: workspace.workspaceId,
}));
async workspace => this.fetchAllFlowsForWorkspace({ parallelCalls: parallelizationPerTask, workspaceId: workspace.workspaceId }));
flows = nonFlatFlows.flat();
} else {
flows = await this.fetchAllFlowsForWorkspace({
Expand Down Expand Up @@ -244,7 +275,7 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
/* eslint-disable-next-line no-param-reassign */
soFar[contract.id] = contract;
return soFar;
}, {});
}, {});

const nonFlatWorkspaces = await mapLimit(
contracts,
Expand Down Expand Up @@ -521,7 +552,7 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
/* eslint-disable-next-line no-param-reassign */
soFar[sample.sampleId] = sample.sample;
return soFar;
}, {});
}, {});
flow.attributes.graph.nodes
.filter(node => node.selected_data_samples)
.forEach((node) => {
Expand Down Expand Up @@ -562,6 +593,20 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
}
});

const secretsList = await this.fetchAllSecretsForWorkspace({
workspaceId: flow.relationships.workspace.data.id,
});
flow.attributes.graph.nodes.forEach((node) => {
if (node.secret_id) {
const matchingSecrets = secretsList.filter(secret => secret.secretId === node.secret_id);
if (matchingSecrets.length !== 1) throw new Error('Expected a single matching secret');
/* eslint-disable-next-line no-param-reassign */
node.secret_id = {
secretId: matchingSecrets[0].secretId,
secretName: matchingSecrets[0].secretName,
};
}
});
// Enrich command and component Id fields
flow.attributes.graph.nodes.forEach((node) => {
const commandParts = node.command.split(/[/:@]/);
Expand Down
Loading

0 comments on commit b684730

Please sign in to comment.