From 46773189d57b07c6d9280194a2ee4fb29208f5d5 Mon Sep 17 00:00:00 2001 From: Matthew Irish Date: Fri, 5 Oct 2018 22:04:48 -0500 Subject: [PATCH] move secret creation to always be at the root level, and simplify model creation so we're always directly manipulating a model --- .../cluster/secrets/backend/create-root.js | 46 +++++++++++++- .../vault/cluster/secrets/backend/create.js | 61 ++----------------- .../components/secret-list-header.hbs | 4 +- 3 files changed, 53 insertions(+), 58 deletions(-) diff --git a/ui/app/routes/vault/cluster/secrets/backend/create-root.js b/ui/app/routes/vault/cluster/secrets/backend/create-root.js index 28d5c8ccc..a8aa145f8 100644 --- a/ui/app/routes/vault/cluster/secrets/backend/create-root.js +++ b/ui/app/routes/vault/cluster/secrets/backend/create-root.js @@ -1 +1,45 @@ -export { default } from './create'; +import { hash } from 'rsvp'; +import { inject as service } from '@ember/service'; +import EditBase from './secret-edit'; + +let secretModel = (store, backend, key) => { + let backendModel = store.peekRecord('secret-engine', backend); + let modelType = backendModel.get('modelTypeForKV'); + if (modelType !== 'secret-v2') { + return store.createRecord(modelType, { + id: key, + }); + } + let secret = store.createRecord(modelType); + secret.set('engine', backendModel); + let version = store.createRecord('secret-v2-version', { + path: key, + }); + secret.set('selectedVersion', version); + return secret; +}; + +export default EditBase.extend({ + wizard: service(), + createModel(transition) { + const { backend } = this.paramsFor('vault.cluster.secrets.backend'); + const modelType = this.modelType(backend); + if (modelType === 'role-ssh') { + return this.store.createRecord(modelType, { keyType: 'ca' }); + } + if (modelType !== 'secret' && modelType !== 'secret-v2') { + if (this.get('wizard.featureState') === 'details' && this.get('wizard.componentState') === 'transit') { + this.get('wizard').transitionFeatureMachine('details', 'CONTINUE', 'transit'); + } + return this.store.createRecord(modelType); + } + return secretModel(this.store, backend, transition.queryParams.initialKey); + }, + + model(params, transition) { + return hash({ + secret: this.createModel(transition), + capabilities: {}, + }); + }, +}); diff --git a/ui/app/routes/vault/cluster/secrets/backend/create.js b/ui/app/routes/vault/cluster/secrets/backend/create.js index 9f9a188cf..63d6f304b 100644 --- a/ui/app/routes/vault/cluster/secrets/backend/create.js +++ b/ui/app/routes/vault/cluster/secrets/backend/create.js @@ -1,59 +1,10 @@ -import { hash } from 'rsvp'; -import { inject as service } from '@ember/service'; -import EmberObject from '@ember/object'; -import EditBase from './secret-edit'; -import KeyMixin from 'vault/models/key-mixin'; +import Route from '@ember/routing/route'; -var SecretProxy = EmberObject.extend(KeyMixin, { - store: null, - - toModel() { - return this.getProperties('id', 'secretData', 'backend'); - }, - - createRecord(backend) { - let backendModel = this.store.peekRecord('secret-engine', backend); - return this.store.createRecord(backendModel.get('modelTypeForKV'), this.toModel()); - }, - - willDestroy() { - this.store = null; - }, -}); - -export default EditBase.extend({ - wizard: service(), - createModel(transition, parentKey) { - const { backend } = this.paramsFor('vault.cluster.secrets.backend'); - const modelType = this.modelType(backend); - if (modelType === 'role-ssh') { - return this.store.createRecord(modelType, { keyType: 'ca' }); - } - if (modelType !== 'secret' && modelType !== 'secret-v2') { - if (this.get('wizard.featureState') === 'details' && this.get('wizard.componentState') === 'transit') { - this.get('wizard').transitionFeatureMachine('details', 'CONTINUE', 'transit'); - } - return this.store.createRecord(modelType); - } - const key = transition.queryParams.initialKey || ''; - const model = SecretProxy.create({ - initialParentKey: parentKey, - store: this.store, - }); - - if (key) { - // have to set this after so that it will be - // computed properly in the template (it's dependent on `initialParentKey`) - model.set('keyWithoutParent', key); - } - return model; - }, - - model(params, transition) { - const parentKey = params.secret ? params.secret : ''; - return hash({ - secret: this.createModel(transition, parentKey), - capabilities: {}, +export default Route.extend({ + beforeModel(transition) { + let { secret } = this.paramsFor(this.routeName); + return this.transitionTo('vault.cluster.secrets.backend.create-root', { + queryParams: { initialKey: secret }, }); }, }); diff --git a/ui/app/templates/components/secret-list-header.hbs b/ui/app/templates/components/secret-list-header.hbs index ffb52a961..4ae8d4d81 100644 --- a/ui/app/templates/components/secret-list-header.hbs +++ b/ui/app/templates/components/secret-list-header.hbs @@ -32,8 +32,8 @@
{{#secret-link mode="create" - secret=(or baseKey.id '') - queryParams=(query-params initialKey='') + secret='' + queryParams=(query-params initialKey=baseKey.id) class="button has-icon-right is-ghost is-compact" data-test-secret-create=true }}