2018-09-25 16:28:26 +00:00
|
|
|
import { set } from '@ember/object';
|
|
|
|
import Service from '@ember/service';
|
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupTest } from 'ember-qunit';
|
2018-07-19 01:59:04 +00:00
|
|
|
import sinon from 'sinon';
|
|
|
|
|
|
|
|
import { storageKey, CONTROL_GROUP_PREFIX, TOKEN_SEPARATOR } from 'vault/services/control-group';
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
let versionStub = Service.extend();
|
|
|
|
let routerStub = Service.extend({
|
2018-07-19 01:59:04 +00:00
|
|
|
transitionTo: sinon.stub(),
|
|
|
|
urlFor: sinon.stub().returns('/ui/vault/foo'),
|
|
|
|
});
|
|
|
|
|
|
|
|
function storage() {
|
|
|
|
return {
|
|
|
|
items: {},
|
|
|
|
getItem(key) {
|
|
|
|
var item = this.items[key];
|
|
|
|
return item && JSON.parse(item);
|
|
|
|
},
|
|
|
|
|
|
|
|
setItem(key, val) {
|
|
|
|
return (this.items[key] = JSON.stringify(val));
|
|
|
|
},
|
|
|
|
|
|
|
|
removeItem(key) {
|
|
|
|
delete this.items[key];
|
|
|
|
},
|
|
|
|
|
|
|
|
keys() {
|
|
|
|
return Object.keys(this.items);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
module('Unit | Service | control group', function (hooks) {
|
2018-09-25 16:28:26 +00:00
|
|
|
setupTest(hooks);
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
hooks.beforeEach(function () {
|
2018-09-25 16:28:26 +00:00
|
|
|
this.owner.register('service:version', versionStub);
|
|
|
|
this.version = this.owner.lookup('service:version');
|
|
|
|
this.owner.register('service:router', routerStub);
|
|
|
|
this.router = this.owner.lookup('service:router');
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
hooks.afterEach(function () {});
|
2018-09-25 16:28:26 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
let isOSS = (context) => set(context, 'version.isOSS', true);
|
|
|
|
let isEnt = (context) => set(context, 'version.isOSS', false);
|
2018-09-25 16:28:26 +00:00
|
|
|
let resolvesArgs = (assert, result, expectedArgs) => {
|
|
|
|
return result.then((...args) => {
|
|
|
|
return assert.deepEqual(args, expectedArgs, 'resolves with the passed args');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-07-19 01:59:04 +00:00
|
|
|
[
|
|
|
|
[
|
2018-09-25 16:28:26 +00:00
|
|
|
'it resolves isOSS:true, wrapTTL: true, response: has wrap_info',
|
|
|
|
isOSS,
|
|
|
|
[[{ one: 'two', three: 'four' }], { wrap_info: { token: 'foo', accessor: 'bar' } }, true],
|
|
|
|
(assert, result) => resolvesArgs(assert, result, [{ one: 'two', three: 'four' }]),
|
2018-07-19 01:59:04 +00:00
|
|
|
],
|
2018-09-25 16:28:26 +00:00
|
|
|
[
|
|
|
|
'it resolves isOSS:true, wrapTTL: false, response: has no wrap_info',
|
|
|
|
isOSS,
|
|
|
|
[[{ one: 'two', three: 'four' }], { wrap_info: null }, false],
|
|
|
|
(assert, result) => resolvesArgs(assert, result, [{ one: 'two', three: 'four' }]),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'it resolves isOSS: false and wrapTTL:true response: has wrap_info',
|
|
|
|
isEnt,
|
|
|
|
[[{ one: 'two', three: 'four' }], { wrap_info: { token: 'foo', accessor: 'bar' } }, true],
|
|
|
|
(assert, result) => resolvesArgs(assert, result, [{ one: 'two', three: 'four' }]),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'it resolves isOSS: false and wrapTTL:false response: has no wrap_info',
|
|
|
|
isEnt,
|
|
|
|
[[{ one: 'two', three: 'four' }], { wrap_info: null }, false],
|
|
|
|
(assert, result) => resolvesArgs(assert, result, [{ one: 'two', three: 'four' }]),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'it rejects isOSS: false, wrapTTL:false, response: has wrap_info',
|
|
|
|
isEnt,
|
|
|
|
[
|
|
|
|
[{ one: 'two', three: 'four' }],
|
|
|
|
{ foo: 'bar', wrap_info: { token: 'secret', accessor: 'lookup' } },
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
(assert, result) => {
|
|
|
|
// ensure failure if we ever don't reject
|
|
|
|
assert.expect(2);
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
return result.then(
|
|
|
|
() => {},
|
2021-12-17 03:44:29 +00:00
|
|
|
(err) => {
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.equal(err.token, 'secret');
|
|
|
|
assert.equal(err.accessor, 'lookup');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
],
|
2021-12-17 03:44:29 +00:00
|
|
|
].forEach(function ([name, setup, args, expectation]) {
|
|
|
|
test(`checkForControlGroup: ${name}`, function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
if (setup) {
|
|
|
|
setup(this);
|
|
|
|
}
|
|
|
|
let service = this.owner.lookup('service:control-group');
|
|
|
|
let result = service.checkForControlGroup(...args);
|
|
|
|
return expectation(assert, result);
|
|
|
|
});
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test(`handleError: transitions to accessor when there is no transition passed in`, function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let error = {
|
|
|
|
accessor: '12345',
|
|
|
|
token: 'token',
|
|
|
|
creation_path: 'kv/',
|
|
|
|
creation_time: new Date().toISOString(),
|
|
|
|
ttl: 400,
|
|
|
|
};
|
|
|
|
let url;
|
|
|
|
let expected = { ...error, uiParams: { url } };
|
|
|
|
let transition = {
|
Update ui dependencies (#7244)
* be more specific about node version, and specify a yarn version
* update ember, ember-cli, ember-data, ember-data-model-fragments
* use router handlers to access transition information
* fix shadowing of component helper
* update ivy-codemirror, ember-cli-inject-live-reload
* remove custom router service
* don't use transition.queryParams
* update ember-cli-deprecation-workflow
* refactor kv v1 to use 'path' instead of 'id' on creation
* fix auth-jwt-test and toolbar-link-test
* update ember composable helpers
* remove Ember.copy from test file
* no more deprecations in the workflow
* fix more secret tests
* fix remaining failed tests
* move select component to core because it's used by ttl-picker
* generate new model class for each test instead of reusing an existing one
* fix selectors on kmip tests
* refactor how control groups construct urls from the new transition objects
* add router service override back in, and have it be evented so that we can trigger router events on it
* move stories and markdown files to core if the component lives in core
* update ember-cli, ember-cli-babel, ember-auto-import
* update base64js, date-fns, deepmerge, codemirror, broccoli-asset-rev
* update linting rules
* fix test selectors
* update ember-api-actions, ember-concurrency, ember-load-initializers, escape-string-regexp, normalize.css, prettier-eslint-cli, jsdoc-to-markdown
* remove test-results dir
* update base64js, ember-cli-clipboard, ember-cli-sass, ember-cli-string-helpers, ember-cli-template-lint, ember-cli-uglify, ember-link-action
* fix linting
* run yarn install without restoring from cache
* refactor how tests are run and handle the vault server subprocess
* update makefile for new test task names
* update circle config to use the new yarn task
* fix writing the seal keys when starting the dev server
* remove optional deps from the lockfile
* don't ignore-optional on yarn install
* remove errant console.log
* update ember-basic-dropdown-hover, jsonlint, yargs-parser
* update ember-cli-flash
* add back optionalDeps
* update @babel/core@7.5.5, ember-basic-dropdown@1.1.3, eslint-plugin-ember@6.8.2
* update storybook to the latest release
* add a babel config with targets so that the ember babel plugin works properly
* update ember-resolver, move ember-cli-storybook to devDependencies
* revert normalize.css upgrade
* silence fetchadapter warning for now
* exclude 3rd party array helper now that ember includes one
* fix switch and entity lookup styling
* only add -root suffix if it's not in versions mode
* make sure drop always has an array on the aws role form
* fix labels like we did with the backport
* update eslintignore
* update the yarn version in the docker build file
* update eslint ignore
2019-08-19 20:45:39 +00:00
|
|
|
to: {
|
|
|
|
name: 'vault.cluster.foo',
|
|
|
|
},
|
2018-09-25 16:28:26 +00:00
|
|
|
};
|
|
|
|
let service = this.owner.factoryFor('service:control-group').create({
|
|
|
|
urlFromTransition: sinon.spy(),
|
|
|
|
storeControlGroupToken: sinon.spy(),
|
|
|
|
});
|
|
|
|
service.handleError(error, transition);
|
|
|
|
assert.ok(service.urlFromTransition.calledWith(transition), 'calls urlFromTransition');
|
|
|
|
assert.ok(service.storeControlGroupToken.calledWith(expected), 'calls storeControlGroupToken');
|
|
|
|
assert.ok(
|
|
|
|
this.router.transitionTo.calledWith('vault.cluster.access.control-group-accessor', '12345'),
|
|
|
|
'calls router transitionTo'
|
|
|
|
);
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test(`logFromError: returns correct content string`, function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let error = {
|
|
|
|
accessor: '12345',
|
|
|
|
token: 'token',
|
|
|
|
creation_path: 'kv/',
|
|
|
|
creation_time: new Date().toISOString(),
|
|
|
|
ttl: 400,
|
|
|
|
};
|
|
|
|
let service = this.owner.factoryFor('service:control-group').create({
|
|
|
|
storeControlGroupToken: sinon.spy(),
|
|
|
|
});
|
|
|
|
let contentString = service.logFromError(error);
|
|
|
|
assert.ok(
|
|
|
|
this.router.urlFor.calledWith('vault.cluster.access.control-group-accessor', '12345'),
|
|
|
|
'calls urlFor with accessor'
|
|
|
|
);
|
|
|
|
assert.ok(service.storeControlGroupToken.calledWith(error), 'calls storeControlGroupToken');
|
|
|
|
assert.ok(contentString.content.includes('12345'), 'contains accessor');
|
|
|
|
assert.ok(contentString.content.includes('kv/'), 'contains creation path');
|
|
|
|
assert.ok(contentString.content.includes('token'), 'contains token');
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('urlFromTransition', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let transition = {
|
Update ui dependencies (#7244)
* be more specific about node version, and specify a yarn version
* update ember, ember-cli, ember-data, ember-data-model-fragments
* use router handlers to access transition information
* fix shadowing of component helper
* update ivy-codemirror, ember-cli-inject-live-reload
* remove custom router service
* don't use transition.queryParams
* update ember-cli-deprecation-workflow
* refactor kv v1 to use 'path' instead of 'id' on creation
* fix auth-jwt-test and toolbar-link-test
* update ember composable helpers
* remove Ember.copy from test file
* no more deprecations in the workflow
* fix more secret tests
* fix remaining failed tests
* move select component to core because it's used by ttl-picker
* generate new model class for each test instead of reusing an existing one
* fix selectors on kmip tests
* refactor how control groups construct urls from the new transition objects
* add router service override back in, and have it be evented so that we can trigger router events on it
* move stories and markdown files to core if the component lives in core
* update ember-cli, ember-cli-babel, ember-auto-import
* update base64js, date-fns, deepmerge, codemirror, broccoli-asset-rev
* update linting rules
* fix test selectors
* update ember-api-actions, ember-concurrency, ember-load-initializers, escape-string-regexp, normalize.css, prettier-eslint-cli, jsdoc-to-markdown
* remove test-results dir
* update base64js, ember-cli-clipboard, ember-cli-sass, ember-cli-string-helpers, ember-cli-template-lint, ember-cli-uglify, ember-link-action
* fix linting
* run yarn install without restoring from cache
* refactor how tests are run and handle the vault server subprocess
* update makefile for new test task names
* update circle config to use the new yarn task
* fix writing the seal keys when starting the dev server
* remove optional deps from the lockfile
* don't ignore-optional on yarn install
* remove errant console.log
* update ember-basic-dropdown-hover, jsonlint, yargs-parser
* update ember-cli-flash
* add back optionalDeps
* update @babel/core@7.5.5, ember-basic-dropdown@1.1.3, eslint-plugin-ember@6.8.2
* update storybook to the latest release
* add a babel config with targets so that the ember babel plugin works properly
* update ember-resolver, move ember-cli-storybook to devDependencies
* revert normalize.css upgrade
* silence fetchadapter warning for now
* exclude 3rd party array helper now that ember includes one
* fix switch and entity lookup styling
* only add -root suffix if it's not in versions mode
* make sure drop always has an array on the aws role form
* fix labels like we did with the backport
* update eslintignore
* update the yarn version in the docker build file
* update eslint ignore
2019-08-19 20:45:39 +00:00
|
|
|
to: {
|
|
|
|
name: 'vault.cluster.foo',
|
|
|
|
params: { bar: '1' },
|
|
|
|
paramNames: ['bar'],
|
|
|
|
queryParams: {},
|
|
|
|
parent: {
|
|
|
|
name: 'vault.cluster',
|
|
|
|
params: { cluster_name: 'vault' },
|
|
|
|
paramNames: ['cluster_name'],
|
|
|
|
parent: {
|
|
|
|
name: 'vault',
|
|
|
|
params: {},
|
|
|
|
paramNames: [],
|
|
|
|
},
|
|
|
|
},
|
2018-09-25 16:28:26 +00:00
|
|
|
},
|
|
|
|
};
|
Update ui dependencies (#7244)
* be more specific about node version, and specify a yarn version
* update ember, ember-cli, ember-data, ember-data-model-fragments
* use router handlers to access transition information
* fix shadowing of component helper
* update ivy-codemirror, ember-cli-inject-live-reload
* remove custom router service
* don't use transition.queryParams
* update ember-cli-deprecation-workflow
* refactor kv v1 to use 'path' instead of 'id' on creation
* fix auth-jwt-test and toolbar-link-test
* update ember composable helpers
* remove Ember.copy from test file
* no more deprecations in the workflow
* fix more secret tests
* fix remaining failed tests
* move select component to core because it's used by ttl-picker
* generate new model class for each test instead of reusing an existing one
* fix selectors on kmip tests
* refactor how control groups construct urls from the new transition objects
* add router service override back in, and have it be evented so that we can trigger router events on it
* move stories and markdown files to core if the component lives in core
* update ember-cli, ember-cli-babel, ember-auto-import
* update base64js, date-fns, deepmerge, codemirror, broccoli-asset-rev
* update linting rules
* fix test selectors
* update ember-api-actions, ember-concurrency, ember-load-initializers, escape-string-regexp, normalize.css, prettier-eslint-cli, jsdoc-to-markdown
* remove test-results dir
* update base64js, ember-cli-clipboard, ember-cli-sass, ember-cli-string-helpers, ember-cli-template-lint, ember-cli-uglify, ember-link-action
* fix linting
* run yarn install without restoring from cache
* refactor how tests are run and handle the vault server subprocess
* update makefile for new test task names
* update circle config to use the new yarn task
* fix writing the seal keys when starting the dev server
* remove optional deps from the lockfile
* don't ignore-optional on yarn install
* remove errant console.log
* update ember-basic-dropdown-hover, jsonlint, yargs-parser
* update ember-cli-flash
* add back optionalDeps
* update @babel/core@7.5.5, ember-basic-dropdown@1.1.3, eslint-plugin-ember@6.8.2
* update storybook to the latest release
* add a babel config with targets so that the ember babel plugin works properly
* update ember-resolver, move ember-cli-storybook to devDependencies
* revert normalize.css upgrade
* silence fetchadapter warning for now
* exclude 3rd party array helper now that ember includes one
* fix switch and entity lookup styling
* only add -root suffix if it's not in versions mode
* make sure drop always has an array on the aws role form
* fix labels like we did with the backport
* update eslintignore
* update the yarn version in the docker build file
* update eslint ignore
2019-08-19 20:45:39 +00:00
|
|
|
let expected = [transition.to.name, 'vault', '1', { queryParams: {} }];
|
2018-09-25 16:28:26 +00:00
|
|
|
let service = this.owner.lookup('service:control-group');
|
|
|
|
service.urlFromTransition(transition);
|
|
|
|
assert.ok(this.router.urlFor.calledWith(...expected), 'calls urlFor with expected args');
|
|
|
|
});
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('storageKey', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let accessor = '12345';
|
|
|
|
let path = 'kv/foo/bar';
|
|
|
|
let expectedKey = `${CONTROL_GROUP_PREFIX}${accessor}${TOKEN_SEPARATOR}${path}`;
|
|
|
|
assert.equal(storageKey(accessor, path), expectedKey, 'uses expected key');
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('keyFromAccessor', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let store = storage();
|
|
|
|
let accessor = '12345';
|
|
|
|
let path = 'kv/foo/bar';
|
|
|
|
let data = { foo: 'bar' };
|
|
|
|
let expectedKey = `${CONTROL_GROUP_PREFIX}${accessor}${TOKEN_SEPARATOR}${path}`;
|
|
|
|
let subject = this.owner.factoryFor('service:control-group').create({
|
|
|
|
storage() {
|
|
|
|
return store;
|
|
|
|
},
|
|
|
|
});
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
store.setItem(expectedKey, data);
|
|
|
|
store.setItem(`${CONTROL_GROUP_PREFIX}2345${TOKEN_SEPARATOR}${path}`, 'ok');
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.equal(subject.keyFromAccessor(accessor), expectedKey, 'finds key given the accessor');
|
|
|
|
assert.equal(subject.keyFromAccessor('foo'), null, 'returns null if no key was found');
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('storeControlGroupToken', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let store = storage();
|
|
|
|
let subject = this.owner.factoryFor('service:control-group').create({
|
|
|
|
storage() {
|
|
|
|
return store;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
let info = {
|
|
|
|
accessor: '12345',
|
|
|
|
creation_path: 'foo/',
|
|
|
|
creation_time: new Date().toISOString(),
|
|
|
|
ttl: 300,
|
|
|
|
};
|
|
|
|
let key = `${CONTROL_GROUP_PREFIX}${info.accessor}${TOKEN_SEPARATOR}${info.creation_path}`;
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject.storeControlGroupToken(info);
|
|
|
|
assert.deepEqual(store.items[key], JSON.stringify(info), 'stores the whole info object');
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('deleteControlGroupToken', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let store = storage();
|
|
|
|
let subject = this.owner.factoryFor('service:control-group').create({
|
|
|
|
storage() {
|
|
|
|
return store;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
let accessor = 'foo';
|
|
|
|
let path = 'kv/one';
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
let expectedKey = `${CONTROL_GROUP_PREFIX}${accessor}${TOKEN_SEPARATOR}${path}`;
|
|
|
|
store.setItem(expectedKey, { one: '2' });
|
|
|
|
subject.deleteControlGroupToken(accessor);
|
|
|
|
assert.equal(Object.keys(store.items).length, 0, 'there are no keys stored in storage');
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('deleteTokens', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let store = storage();
|
|
|
|
let subject = this.owner.factoryFor('service:control-group').create({
|
|
|
|
storage() {
|
|
|
|
return store;
|
|
|
|
},
|
|
|
|
});
|
2018-07-19 01:59:04 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
let keyOne = `${CONTROL_GROUP_PREFIX}foo`;
|
|
|
|
let keyTwo = `${CONTROL_GROUP_PREFIX}bar`;
|
|
|
|
store.setItem(keyOne, { one: '2' });
|
|
|
|
store.setItem(keyTwo, { two: '2' });
|
|
|
|
store.setItem('value', 'one');
|
|
|
|
assert.equal(Object.keys(store.items).length, 3, 'stores 3 values');
|
|
|
|
subject.deleteTokens();
|
|
|
|
assert.equal(Object.keys(store.items).length, 1, 'removes tokens with control group prefix');
|
|
|
|
assert.equal(store.getItem('value'), 'one', 'keeps the non-prefixed value');
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('wrapInfoForAccessor', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let store = storage();
|
|
|
|
let subject = this.owner.factoryFor('service:control-group').create({
|
|
|
|
storage() {
|
|
|
|
return store;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let keyOne = `${CONTROL_GROUP_PREFIX}foo`;
|
|
|
|
store.setItem(keyOne, { one: '2' });
|
|
|
|
assert.deepEqual(subject.wrapInfoForAccessor('foo'), { one: '2' });
|
|
|
|
});
|
2018-07-19 01:59:04 +00:00
|
|
|
});
|