Skip to content

Commit

Permalink
UI: Allow repeat data wrapping for wrap tool (#27289)
Browse files Browse the repository at this point in the history
* update selectors

* add tests

* add tests

* add explanations to true only args

* allow token wrap to wrap again

* update test wording

* add wrap specific modules to tools acceptance test

* add changelog

* remove selectedAction

* trim args and update tests
  • Loading branch information
hellobontempo committed Jun 4, 2024
1 parent 5acc433 commit 1f64e6e
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 127 deletions.
3 changes: 3 additions & 0 deletions changelog/27289.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
ui: Allow users to wrap inputted data again instead of resetting form
```
23 changes: 13 additions & 10 deletions ui/app/components/tool-actions-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export default Component.extend(DEFAULTS, {
flashMessages: service(),
store: service(),
// putting these attrs here so they don't get reset when you click back
//random
// random
bytes: 32,
//hash
// hash
format: 'base64',
algorithm: 'sha2-256',

data: '{\n}',
tagName: '',

didReceiveAttrs() {
Expand Down Expand Up @@ -139,15 +139,18 @@ export default Component.extend(DEFAULTS, {
this.reset();
},

updateTtl(ttl) {
set(this, 'wrapTTL', ttl);
onBack(properties) {
// only reset specific properties so user can reuse input data and repeat the action
if (this.isDestroyed || this.isDestroying) {
return;
}
properties.forEach((prop) => {
set(this, prop, DEFAULTS[prop]);
});
},

codemirrorUpdated(val, hasErrors) {
setProperties(this, {
buttonDisabled: hasErrors,
data: val,
});
onChange(param, value) {
set(this, param, value);
},
},
});
37 changes: 14 additions & 23 deletions ui/app/components/tool-wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,36 @@ import { tracked } from '@glimmer/tracking';
* ToolWrap components are components that sys/wrapping/wrap functionality. Most of the functionality is passed through as actions from the tool-actions-form and then called back with properties.
*
* @example
* ```js
* <ToolWrap
* @errors={{@errors}}
* @onBack={{action "onBack" (array "token")}}
* @onChange={{action "onChange"}}
* @onClear={{action "onClear"}}
* @token={{token}}
* @selectedAction="wrap"
* @codemirrorUpdated={{action "codemirrorUpdated"}}
* @updateTtl={{action "updateTtl"}}
* @buttonDisabled={{buttonDisabled}}
* @errors={{errors}}/>
* ```
* @param onClear {Function} - parent action that is passed through. Must be passed as {{action "onClear"}}
* @param token=null {String} - property passed from parent to child and then passed back up to parent
* @param selectedAction="wrap" - passed in from parent. This is the wrap action, others include hash, etc.
* @param codemirrorUpdated {Function} - parent action that is passed through. Must be passed as {{action "codemirrorUpdated"}}.
* @param updateTtl {Function} - parent action that is passed through. Must be passed as {{action "updateTtl"}}
* @param buttonDisabled=false {Boolean} - false default and if there is an error on codemirror it turns to true.
* @param error=null {Object} - errors passed from parent as default then from child back to parent.
* @token={{@token}}
* />
*
* @param {object} errors=null - errors returned if wrap fails
* @param {function} onBack - callback that only clears specific values so the action can be repeated. Must be passed as `{{action "onBack"}}`
* @param {function} onChange - callback that fires when inputs change and passes value and param name back to the parent
* @param {function} onClear - callback that resets all of values to defaults. Must be passed as `{{action "onClear"}}`
* @param {string} token=null - returned after user clicks "Wrap data", if there is a token value it displays instead of the JsonEditor
*/

export default class ToolWrap extends Component {
@tracked data = '{\n}';
@tracked buttonDisabled = false;

@action
onClear() {
this.args.onClear();
}
@action
updateTtl(evt) {
if (!evt) return;
const ttl = evt.enabled ? `${evt.seconds}s` : '30m';
this.args.updateTtl(ttl);
this.args.onChange('wrapTTL', ttl);
}

@action
codemirrorUpdated(val, codemirror) {
codemirror.performLint();
const hasErrors = codemirror?.state.lint.marked?.length > 0;
this.data = val;
this.buttonDisabled = hasErrors;
this.args.codemirrorUpdated(val, hasErrors);
this.args.onChange('data', val);
}
}
7 changes: 3 additions & 4 deletions ui/app/templates/components/tool-actions-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@
{{else if (eq this.selectedAction "wrap")}}
<ToolWrap
@token={{this.token}}
@selectedAction={{this.selectedAction}}
@onBack={{action "onBack" (array "token")}}
@onClear={{action "onClear"}}
@codemirrorUpdated={{action "codemirrorUpdated"}}
@updateTtl={{action "updateTtl"}}
@buttonDisabled={{this.buttonDisabled}}
@onChange={{action "onChange"}}
@errors={{this.errors}}
@data={{this.data}}
/>
{{else}}
<EmptyState @title="Tool not available" />
Expand Down
4 changes: 2 additions & 2 deletions ui/app/templates/components/tool-hash.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/>
</div>
<div class="control">
<Hds::Button @text="Back" @color="secondary" {{on "click" this.onClear}} data-test-tools-back={{true}} />
<Hds::Button @text="Back" @color="secondary" {{on "click" this.onClear}} data-test-button="Back" />
</div>
</div>
{{else}}
Expand Down Expand Up @@ -80,7 +80,7 @@
</div>
<div class="field is-grouped box is-fullwidth is-bottomless">
<div class="control">
<Hds::Button @text="Hash" type="submit" data-test-tools-submit="true" />
<Hds::Button @text="Hash" type="submit" data-test-tools-submit />
</div>
</div>
{{/if}}
12 changes: 6 additions & 6 deletions ui/app/templates/components/tool-lookup.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

{{#if (or @creation_time @creation_ttl)}}
<div class="box is-fullwidth is-sideless is-paddingless is-marginless">
<InfoTableRow @label="Creation path" @value={{@creation_path}} data-test-tools="token-lookup-row" />
<InfoTableRow @label="Creation time" @value={{@creation_time}} data-test-tools="token-lookup-row" />
<InfoTableRow @label="Creation TTL" @value={{@creation_ttl}} data-test-tools="token-lookup-row" />
<InfoTableRow @label="Creation path" @value={{@creation_path}} />
<InfoTableRow @label="Creation time" @value={{@creation_time}} />
<InfoTableRow @label="Creation TTL" @value={{@creation_ttl}} />
{{#if @expirationDate}}
<InfoTableRow @label="Expiration date" @value={{@expirationDate}} data-test-tools="token-lookup-row" />
<InfoTableRow @label="Expires in" @value={{date-from-now @expirationDate}} data-test-tools="token-lookup-row" />
<InfoTableRow @label="Expiration date" @value={{@expirationDate}} />
<InfoTableRow @label="Expires in" @value={{date-from-now @expirationDate}} />
{{/if}}
</div>
<div class="field is-grouped box is-fullwidth is-bottomless">
Expand All @@ -42,7 +42,7 @@
</div>
<div class="field is-grouped box is-fullwidth is-bottomless">
<div class="control">
<Hds::Button @text="Lookup token" type="submit" data-test-tools-submit="true" />
<Hds::Button @text="Lookup token" type="submit" data-test-tools-submit />
</div>
</div>
{{/if}}
2 changes: 1 addition & 1 deletion ui/app/templates/components/tool-random.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
</div>
<div class="field is-grouped box is-fullwidth is-bottomless">
<div class="control">
<Hds::Button @text="Generate" type="submit" data-test-tools-submit="true" />
<Hds::Button @text="Generate" type="submit" data-test-tools-submit />
</div>
</div>
{{/if}}
2 changes: 1 addition & 1 deletion ui/app/templates/components/tool-rewrap.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</div>
<div class="field is-grouped box is-fullwidth is-bottomless">
<div class="control">
<Hds::Button @text="Rewrap token" type="submit" data-test-tools-submit="true" />
<Hds::Button @text="Rewrap token" type="submit" data-test-tools-submit />
</div>
</div>
{{/if}}
6 changes: 3 additions & 3 deletions ui/app/templates/components/tool-unwrap.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

{{#if @unwrap_data}}
<Hds::Tabs as |T|>
<T.Tab data-test-button-data>Data</T.Tab>
<T.Tab data-test-button-details>Wrap Details</T.Tab>
<T.Tab data-test-tab="data">Data</T.Tab>
<T.Tab data-test-tab="details">Wrap Details</T.Tab>
<T.Panel>
<JsonEditor
class="has-top-padding-m"
Expand Down Expand Up @@ -75,7 +75,7 @@
</div>
<div class="field is-grouped box is-fullwidth is-bottomless">
<div class="control">
<Hds::Button @text="Unwrap data" type="submit" data-test-tools-submit="true" />
<Hds::Button @text="Unwrap data" type="submit" data-test-tools-submit />
</div>
</div>
{{/if}}
37 changes: 15 additions & 22 deletions ui/app/templates/components/tool-wrap.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,49 @@
<div class="field">
<label for="wrap-info" class="is-label">Wrapped token</label>
<div class="control">
<Textarea
@value={{@token}}
readonly={{true}}
class="textarea"
id="wrap-info"
name="wrap-info"
<Hds::Copy::Snippet
@textToCopy={{@token}}
@color="secondary"
data-test-tools-input="wrapping-token"
@onError={{(fn
(set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger")
)}}
/>
</div>
</div>
</div>
<div class="field is-grouped box is-fullwidth is-bottomless">
<div class="control">
<Hds::Copy::Button
@text="Copy"
@textToCopy={{@token}}
@onError={{(fn (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger"))}}
class="primary"
/>
</div>
<div class="control">
<Hds::Button @text="Back" @color="secondary" {{on "click" this.onClear}} />
</div>
<Hds::ButtonSet>
<Hds::Button @icon="arrow-left" @text="Back" @color="tertiary" {{on "click" @onBack}} data-test-button="Back" />
<Hds::Button @text="Done" @color="secondary" {{on "click" @onClear}} data-test-button="Done" />
</Hds::ButtonSet>
</div>
{{else}}
<div class="box is-sideless is-fullwidth is-marginless">
<NamespaceReminder @mode="perform" @noun={{@selectedAction}} />
<NamespaceReminder @mode="perform" @noun="wrap" />
<MessageError @errors={{@errors}} />
<div class="field">
<div class="control">
<JsonEditor
@title="Data to wrap"
@subTitle="json-formatted"
@value={{this.data}}
@valueUpdated={{action this.codemirrorUpdated}}
@value={{@data}}
@valueUpdated={{this.codemirrorUpdated}}
/>
</div>
</div>
<TtlPicker
@label="Wrap TTL"
@initialValue="30m"
@onChange={{action "updateTtl"}}
@onChange={{this.updateTtl}}
@helperTextDisabled="Vault will use the default (30m)"
@helperTextEnabled="Wrap will expire after"
@changeOnInit={{true}}
/>
</div>
<div class="field is-grouped box is-fullwidth is-bottomless">
<div class="control">
<Hds::Button @text="Wrap data" type="submit" data-test-tools-submit="true" disabled={{@buttonDisabled}} />
<Hds::Button @text="Wrap data" type="submit" disabled={{this.buttonDisabled}} data-test-tools-submit />
</div>
</div>
{{/if}}
Loading

0 comments on commit 1f64e6e

Please sign in to comment.