Skip to content

UI Secrets Sync: Destinations adapter add LIST #23716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a07e039
add models
hellobontempo Oct 16, 2023
e2cd3b2
adapters
hellobontempo Oct 16, 2023
17959ec
base model adapter
hellobontempo Oct 16, 2023
96c8a05
update test response
hellobontempo Oct 16, 2023
00b3f2b
Merge branch 'ui/VAULT-17968/secrets-sync' into ui/VAULT-21049/ember-…
hellobontempo Oct 16, 2023
76ac9ba
add sync destinations helper
hellobontempo Oct 16, 2023
2772cd6
finish renaming base destination model/adapter
hellobontempo Oct 16, 2023
3fd98f0
add comment
hellobontempo Oct 16, 2023
3e22e15
add serializer
hellobontempo Oct 16, 2023
705f420
doc-link helper
hellobontempo Oct 17, 2023
377807c
add version service
hellobontempo Oct 17, 2023
e238920
landing and overview component
hellobontempo Oct 17, 2023
6b71489
Merge branch 'ui/VAULT-17968/secrets-sync' into ui/VAULT-21036/overvi…
hellobontempo Oct 17, 2023
2a45393
overview page
hellobontempo Oct 17, 2023
1fc759b
build out serializer and adapters
hellobontempo Oct 17, 2023
59294ce
update mirage
hellobontempo Oct 17, 2023
26e5890
Merge branch 'ui/VAULT-17968/secrets-sync' into ui/VAULT-21208/query-…
hellobontempo Oct 18, 2023
f31c2a1
fix merge conflicts
hellobontempo Oct 18, 2023
4784436
one more conflict!
hellobontempo Oct 18, 2023
59a0a86
pull transformQueryResponse to separate method in adapter
hellobontempo Oct 18, 2023
8d186c1
move data transforming all to serializer and tests
hellobontempo Oct 18, 2023
0799124
add note to paginationd ocs
hellobontempo Oct 18, 2023
9d380de
conditionally render CTA
hellobontempo Oct 18, 2023
264554e
add lazyPaginatedQuery method to destinations route
hellobontempo Oct 18, 2023
c90f169
remove partial error
hellobontempo Oct 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ui/app/adapters/sync/destination.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ export default class SyncDestinationAdapter extends ApplicationAdapter {
}

// id is the destination name
// modelName is sync/destination/<destination type>
// modelName is sync/destinations/:type
urlForFindRecord(id, modelName) {
return `${this._baseUrl()}/${modelName}/${id}`;
}

query() {
const url = `${this._baseUrl()}/sync/destinations`;
return this.ajax(url, 'GET', { data: { list: true } });
}
}
37 changes: 30 additions & 7 deletions ui/app/serializers/sync/destination.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@
import ApplicationSerializer from 'vault/serializers/application';

export default class SyncDestinationSerializer extends ApplicationSerializer {
primaryKey = 'name';
// overwrite application serializer's normalizeResponse method
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
let transformedPayload = payload;

normalizeItems(payload) {
if (payload.data?.connection_details) {
const data = { ...payload.data, ...payload.data.connection_details };
delete data.connection_details;
return data;
if (requestType === 'findRecord') {
transformedPayload = this._normalizeFindRecord(payload);
}
return super.normalizeItems(...arguments);
return super.normalizeResponse(store, primaryModelClass, transformedPayload, id, requestType);
}
Comment on lines +10 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to figure out how to still extend the ApplicationSerializer while being able to use normalizeFindRecordResponse rather than conditionally targeting the requestType but this does the trick.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah...I couldn't find a way around this. I tried a few things like just interrupt normalizeItems and return payload but I got a Maximum call stack exceeded error 🙃


extractLazyPaginatedData(payload) {
const transformedPayload = [];
// loop through each destination type (keys in key_info)
for (const type in payload.data.key_info) {
// iterate through each type's destination names
payload.data.key_info[type].forEach((name) => {
const id = `${type}/${name}`;
// create object with destination's id and attributes, add to payload
transformedPayload.pushObject({ id, name, type });
});
}
return transformedPayload;
}

// generates id and spreads connection_details object into data
_normalizeFindRecord(payload) {
if (payload?.data?.connection_details) {
const { type, name, connection_details } = payload.data;
const id = `${type}/${name}`;
return { data: { id, type, name, ...connection_details } };
}
return payload;
}
}
2 changes: 1 addition & 1 deletion ui/docs/client-pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The `size` param defaults to the default page size set in [the app config](../co

### Serializing

In order to interrupt the regular serialization when using `lazyPaginatedData`, define `extractLazyPaginatedData` on the modelType's serializer. This will be called with the raw response before being cached on the store.
In order to interrupt the regular serialization when using `lazyPaginatedData`, define `extractLazyPaginatedData` on the modelType's serializer. This will be called with the raw response before being cached on the store. `extractLazyPaginatedData` should return an array of objects.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉


## Gotchas

Expand Down
8 changes: 6 additions & 2 deletions ui/lib/sync/addon/components/secrets/page/overview.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
~}}

<SyncHeader @title="Secrets sync" />
{{! TODO if no destinations CTA otherwise LIST view }}
<Secrets::LandingCta />

{{#if @shouldRenderOverview}}
{{! overview cards here }}
{{else}}
<Secrets::LandingCta />
{{/if}}
17 changes: 17 additions & 0 deletions ui/lib/sync/addon/routes/secrets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

import type StoreService from 'vault/services/store';

export default class SyncSecretsRoute extends Route {
@service declare readonly store: StoreService;

async model() {
return this.store.query('sync/destination', {}).catch(() => []);
}
}
26 changes: 26 additions & 0 deletions ui/lib/sync/addon/routes/secrets/destinations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

import type StoreService from 'vault/services/store';

interface SyncSecretsDestinationsRouteParams {
page: string;
pageFilter: string;
}

export default class SyncSecretsDestinationsRoute extends Route {
@service declare readonly store: StoreService;

async model(params: SyncSecretsDestinationsRouteParams) {
return this.store.lazyPaginatedQuery('sync/destination', {
page: Number(params.page) || 1,
pageFilter: params.pageFilter,
responsePath: 'data.keys',
});
}
}
7 changes: 6 additions & 1 deletion ui/lib/sync/addon/templates/secrets/overview.hbs
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<Secrets::Page::Overview />
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: BUSL-1.1
~}}

<Secrets::Page::Overview @shouldRenderOverview={{this.model.length}} />
2 changes: 2 additions & 0 deletions ui/mirage/handlers/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function (server) {
}
return keyInfo;
}, {}),
keys: records.map((r) => r.type),
},
};
});
Expand All @@ -33,6 +34,7 @@ export default function (server) {
if (record) {
delete record.type;
delete record.name;
delete record.id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

return {
data: {
connection_details: record,
Expand Down
21 changes: 20 additions & 1 deletion ui/tests/unit/adapters/sync/destinations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { module, test } from 'qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { destinationTypes } from 'vault/helpers/sync-destinations';

module('Unit | Adapter | sync/destinations', function (hooks) {
module('Unit | Adapter | sync | destination', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

Expand All @@ -34,4 +34,23 @@ module('Unit | Adapter | sync/destinations', function (hooks) {
this.store.findRecord(`sync/destinations/${type}`, 'my-dest');
}
});

test('it calls the correct endpoint for query', async function (assert) {
assert.expect(2);

this.server.get('sys/sync/destinations', (schema, req) => {
assert.propEqual(req.queryParams, { list: 'true' }, 'it passes { list: true } as query params');
assert.ok(true, `request is made to LIST sys/sync/destinations endpoint on query`);
return {
data: {
key_info: {
'aws-sm': ['my-dest-1'],
gh: ['my-dest-1'],
},
keys: ['aws-sm', 'gh'],
},
};
});
this.store.query('sync/destination', {});
});
});
42 changes: 37 additions & 5 deletions ui/tests/unit/serializers/sync/destinations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { setupTest } from 'vault/tests/helpers';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { destinationTypes } from 'vault/helpers/sync-destinations';

module('Unit | Serializer | sync/destination', function (hooks) {
module('Unit | Serializer | sync | destination', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

hooks.beforeEach(function () {
this.serializer = this.owner.lookup('serializer:sync/destination');
});

test('it normalizes the payload from the server', async function (assert) {
test('it normalizes findRecord payload from the server', async function (assert) {
const types = destinationTypes();
assert.expect(types.length);

Expand All @@ -26,13 +26,45 @@ module('Unit | Serializer | sync/destination', function (hooks) {
destinationType
);
const serverData = { request_id: id, data: { name, type, connection_details } };
const normalized = this.serializer.normalizeItems(serverData);
const expected = { type, name, ...connection_details };

const normalized = this.serializer._normalizeFindRecord(serverData);
const expected = { data: { id: `${type}/${name}`, type, name, ...connection_details } };
assert.propEqual(
normalized,
expected,
`connection details key is removed and params are added to ${destinationType} data object`
`generates id and adds connection details to ${destinationType} data object`
);
}
});

test('it normalizes query payload from the server', async function (assert) {
assert.expect(1);
// hardcoded from docs https://developer.hashicorp.com/vault/api-docs/system/secrets-sync#sample-response
// destinations intentionally named the same to test no id naming collision happens
const serverData = {
data: {
key_info: {
'aws-sm': ['my-dest-1'],
gh: ['my-dest-1'],
},
keys: ['aws-sm', 'gh'],
},
};

const normalized = this.serializer.extractLazyPaginatedData(serverData);
const expected = [
{
id: 'aws-sm/my-dest-1',
name: 'my-dest-1',
type: 'aws-sm',
},
{
id: 'gh/my-dest-1',
name: 'my-dest-1',
type: 'gh',
},
];

assert.propEqual(normalized, expected, 'payload is array of objects with concatenated type/name as id');
});
});