-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
UI: Add capabilities service #28168
Merged
Merged
UI: Add capabilities service #28168
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c377156
add capabilities service
hellobontempo 5edaf11
remove from kv engine for now
hellobontempo df967e1
add canRead
hellobontempo 7dcaff4
move await helper to addon
hellobontempo f38a323
add test
hellobontempo f0fe02f
update capabilities service to accommodate multiple paths
hellobontempo e8211c0
address comments, make methods more explicit
hellobontempo 709261d
remove namespace key
hellobontempo 1ce2db4
fix typo in test
hellobontempo e7302fb
add namespace back!
hellobontempo cac8388
round out tests for other methods
hellobontempo a3f095d
add test
hellobontempo 7a019ab
add comment
hellobontempo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: BUSL-1.1 | ||
*/ | ||
|
||
import Service, { service } from '@ember/service'; | ||
|
||
import type StoreService from 'vault/services/store'; | ||
|
||
export default class CapabilitiesService extends Service { | ||
@service declare readonly store: StoreService; | ||
|
||
request = (apiPath: string) => { | ||
return this.store.findRecord('capabilities', apiPath); | ||
}; | ||
|
||
async fetchAll(apiPath: string) { | ||
try { | ||
return await this.request(apiPath); | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
|
||
async fetchSpecific(apiPath: string, capability: string) { | ||
try { | ||
const capabilities = await this.request(apiPath); | ||
return capabilities[capability]; | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
hellobontempo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
async canRead(apiPath: string) { | ||
try { | ||
return await this.fetchSpecific(apiPath, 'canRead'); | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
|
||
async canUpdate(apiPath: string) { | ||
try { | ||
return await this.fetchSpecific(apiPath, 'canUpdate'); | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move this to addon so in templates we can do things like {{#if (await (this.capabilities.canRead "secret/data/my-secret"))}}
Show something...
{{/if}} |
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,6 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: BUSL-1.1 | ||
*/ | ||
|
||
export { default } from 'core/helpers/await'; |
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,79 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: BUSL-1.1 | ||
*/ | ||
|
||
import { module, test } from 'qunit'; | ||
import { setupTest } from 'ember-qunit'; | ||
import { setupMirage } from 'ember-cli-mirage/test-support'; | ||
|
||
module('Unit | Service | capabilities', function (hooks) { | ||
setupTest(hooks); | ||
setupMirage(hooks); | ||
|
||
hooks.beforeEach(function () { | ||
this.capabilities = this.owner.lookup('service:capabilities'); | ||
this.store = this.owner.lookup('service:store'); | ||
this.generateResponse = (apiPath, perms) => { | ||
return { | ||
[apiPath]: perms, | ||
capabilities: perms, | ||
request_id: '6cc7a484-921a-a730-179c-eaf6c6fbe97e', | ||
data: { | ||
capabilities: perms, | ||
[apiPath]: perms, | ||
}, | ||
}; | ||
}; | ||
}); | ||
|
||
test('it makes request to capabilities-self', function (assert) { | ||
const apiPath = '/my/api/path'; | ||
const expectedPayload = { | ||
paths: [apiPath], | ||
}; | ||
this.server.post('/sys/capabilities-self', (schema, req) => { | ||
const actual = JSON.parse(req.requestBody); | ||
assert.true(true, 'request made to capabilities-self'); | ||
assert.propEqual(actual, expectedPayload, `request made with path: ${JSON.stringify(actual)}`); | ||
return this.generateResponse(apiPath, ['read']); | ||
}); | ||
this.capabilities.request(apiPath); | ||
}); | ||
|
||
const TEST_CASES = [ | ||
{ | ||
capabilities: ['read'], | ||
canRead: true, | ||
canUpdate: false, | ||
}, | ||
{ | ||
capabilities: ['update'], | ||
canRead: false, | ||
canUpdate: true, | ||
}, | ||
{ | ||
capabilities: ['deny'], | ||
canRead: false, | ||
canUpdate: false, | ||
}, | ||
{ | ||
capabilities: ['read', 'update'], | ||
canRead: true, | ||
canUpdate: true, | ||
}, | ||
]; | ||
TEST_CASES.forEach(({ capabilities, canRead, canUpdate }) => { | ||
test(`it returns expected boolean for "${capabilities.join(', ')}"`, async function (assert) { | ||
const apiPath = '/my/api/path'; | ||
this.server.post('/sys/capabilities-self', () => { | ||
return this.generateResponse(apiPath, capabilities); | ||
}); | ||
|
||
const canReadResponse = await this.capabilities.canRead(apiPath); | ||
const canUpdateResponse = await this.capabilities.canUpdate(apiPath); | ||
assert[canRead](canReadResponse, `canRead returns ${canRead}`); | ||
assert[canUpdate](canUpdateResponse, `canUpdate returns ${canRead}`); | ||
}); | ||
}); | ||
}); |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could also call this
@permissions
if we're worried about confusing it with the@lazyCapabilities
method