Skip to content

Commit

Permalink
Sync Mirage Setup (#23683)
Browse files Browse the repository at this point in the history
* adds mirage setup for sync endpoints

* updates secret_name default in sync-association mirage factory
  • Loading branch information
zofskeez authored Oct 16, 2023
1 parent dc27b26 commit 8338397
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 2 deletions.
16 changes: 16 additions & 0 deletions ui/mirage/factories/sync-association.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

import { Factory } from 'ember-cli-mirage';

export default Factory.extend({
accessor: 'kv_eb4acbae',
secret_name: 'my-path/my-secret-1',
sync_status: 'SYNCED',
updated_at: '2023-09-20T10:51:53.961861096-04:00',
// set on create for lookup by destination
type: null,
name: null,
});
44 changes: 44 additions & 0 deletions ui/mirage/factories/sync-destination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

import { Factory, trait } from 'ember-cli-mirage';

export default Factory.extend({
['aws-sm']: trait({
access_key_id: 'foo',
secret_access_key: 'bar',
region: 'us-west-1',
type: 'aws-sm',
name: 'destination-aws',
}),
['azure-kv']: trait({
key_vault_uri: 'https://keyvault-1234abcd.vault.azure.net',
subscription_id: 'subscription-id',
tenant_id: 'tenant-id',
client_id: 'client-id',
client_secret: 'my-secret',
type: 'azure-kv',
name: 'destination-azure',
}),
['gcp-sm']: trait({
credentials: '{"username":"foo","password":"bar"}',
type: 'gcp-sm',
name: 'destination-gcp',
}),
gh: trait({
access_token: 'github_pat_12345',
repository_owner: 'my-organization-or-username',
repository_name: 'my-repository',
type: 'gh',
name: 'destination-gh',
}),
['vercel-project']: trait({
access_token: 'my-access-token',
project_id: 'prj_12345',
deployment_environments: ['development', 'preview', 'production'],
type: 'vercel-project',
name: 'destination-vercel',
}),
});
3 changes: 2 additions & 1 deletion ui/mirage/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ import oidcConfig from './oidc-config';
import hcpLink from './hcp-link';
import kubernetes from './kubernetes';
import ldap from './ldap';
import sync from './sync';

export { base, clients, db, kms, mfaConfig, mfaLogin, oidcConfig, hcpLink, kubernetes, ldap };
export { base, clients, db, kms, mfaConfig, mfaLogin, oidcConfig, hcpLink, kubernetes, ldap, sync };
92 changes: 92 additions & 0 deletions ui/mirage/handlers/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

import { Response } from 'miragejs';

export default function (server) {
const base = '/sys/sync/destinations';
const uri = `${base}/:type/:name`;
// destinations
server.get(base, (schema) => {
const records = schema.db.syncDestinations.where({});
if (!records.length) {
return new Response(404, {}, { errors: [] });
}
return {
data: {
key_info: records.reduce((keyInfo, record) => {
if (!keyInfo[record.type]) {
keyInfo[record.type] = [record.name];
} else {
keyInfo[record.type].push(record.name);
}
return keyInfo;
}, {}),
},
};
});
server.get(uri, (schema, req) => {
const { type, name } = req.params;
const record = schema.db.syncDestinations.findBy({ type, name });
if (record) {
delete record.type;
delete record.name;
return {
data: {
connection_details: record,
name,
type,
},
};
}
return new Response(404, {}, { errors: [] });
});
server.post(uri, (schema, req) => {
const { type, name } = req.params;
const data = { ...JSON.parse(req.requestBody), type, name };
schema.db.syncDestinations.firstOrCreate({ type, name }, data);
schema.db.syncDestinations.update({ type, name }, data);
return new Response(204);
});
server.delete(uri, (schema, req) => {
const { type, name } = req.params;
schema.db.syncDestinations.remove({ type, name });
return new Response(204);
});
// associations
server.get(`${uri}/associations`, (schema, req) => {
const { type, name } = req.params;
const records = schema.db.syncAssociations.where({ type, name });
if (!records.length) {
return new Response(404, {}, { errors: [] });
}
return {
data: {
associated_secrets: records.reduce((associations, association) => {
const key = `${association.accessor}/${association.secret_name}`;
delete association.type;
delete association.name;
associations[key] = association;
return associations;
}, {}),
store_name: name,
store_type: type,
},
};
});
server.post(`${uri}/associations/set`, (schema, req) => {
const { type, name } = req.params;
const { secret_name, mount: accessor } = JSON.parse(req.requestBody);
const data = { type, name, accessor, secret_name };
schema.db.syncAssociations.firstOrCreate({ type, name }, data);
schema.db.syncAssociations.update({ type, name }, data);
return new Response(204);
});
server.post(`${uri}/associations/remove`, (schema, req) => {
const { type, name } = req.params;
schema.db.syncAssociations.remove({ type, name });
return new Response(204);
});
}
3 changes: 2 additions & 1 deletion ui/mirage/scenarios/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import kubernetes from './kubernetes';
import ldap from './ldap';
import sync from './sync';

export { kubernetes, ldap };
export { kubernetes, ldap, sync };
12 changes: 12 additions & 0 deletions ui/mirage/scenarios/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

export default function (server) {
const types = ['aws-sm', 'azure-kv', 'gcp-sm', 'gh', 'vercel-project'];
types.forEach((type) => {
server.create('sync-destination', type);
server.create('sync-association', { type, name: `destination-${type.split('-')[0]}` });
});
}

0 comments on commit 8338397

Please sign in to comment.