-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
mount-backend-form.js
180 lines (164 loc) · 5.74 KB
/
mount-backend-form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { task } from 'ember-concurrency';
import { waitFor } from '@ember/test-waiters';
import { supportedSecretBackends } from 'vault/helpers/supported-secret-backends';
import { methods } from 'vault/helpers/mountable-auth-methods';
/**
* @module MountBackendForm
* The `MountBackendForm` is used to mount either a secret or auth backend.
*
* @example ```js
* <MountBackendForm @mountType="secret" @onMountSuccess={{this.onMountSuccess}} />```
*
* @param {function} onMountSuccess - A function that transitions once the Mount has been successfully posted.
* @param {string} [mountType=auth] - The type of backend we want to mount.
*
*/
export default class MountBackendForm extends Component {
@service store;
@service flashMessages;
// validation related properties
@tracked modelValidations = null;
@tracked invalidFormAlert = null;
@tracked errorMessage = '';
willDestroy() {
if (this.args?.mountModel) {
try {
// if unsaved, we want to unload so it doesn't show up in the auth mount list
const method = this.args.mountModel.isNew ? 'unloadRecord' : 'rollbackAttributes';
this.args.mountModel[method]();
} catch (e) {
// In component test the store is torn down before willDestroy is called, causing error
console.debug('mountModel rollback failed with error:', e.message); // eslint-disable-line
}
}
super.willDestroy(...arguments);
}
checkPathChange(type) {
if (!type) return;
const mount = this.args.mountModel;
const currentPath = mount.path;
const mountTypes =
this.args.mountType === 'secret' ? supportedSecretBackends() : methods().map((auth) => auth.type);
// if the current path has not been altered by user,
// change it here to match the new type
if (!currentPath || mountTypes.includes(currentPath)) {
mount.path = type;
}
}
typeChangeSideEffect(type) {
if (!this.args.mountType === 'secret') return;
if (type === 'pki') {
// If type PKI, set max lease to ~10years
this.args.mountModel.config.maxLeaseTtl = '3650d';
} else {
// otherwise reset
this.args.mountModel.config.maxLeaseTtl = 0;
}
}
checkModelValidity(model) {
const { isValid, state, invalidFormMessage } = model.validate();
this.modelValidations = state;
this.invalidFormAlert = invalidFormMessage;
return isValid;
}
checkModelWarnings() {
// check for warnings on change
// since we only show errors on submit we need to clear those out and only send warning state
const { state } = this.args.mountModel.validate();
for (const key in state) {
state[key].errors = [];
}
this.modelValidations = state;
this.invalidFormAlert = null;
}
async showWarningsForKvv2() {
try {
const capabilities = await this.store.findRecord('capabilities', `${this.args.mountModel.path}/config`);
if (!capabilities?.canUpdate) {
// config error is not thrown from secret-engine adapter, so handling here
this.flashMessages.warning(
'You do not have access to the config endpoint. The secret engine was mounted, but the configuration settings were not saved.'
);
// remove the config data from the model otherwise it will persist in the store even though network request failed.
[
this.args.mountModel.maxVersions,
this.args.mountModel.casRequired,
this.args.mountModel.deleteVersionAfter,
] = [0, false, 0];
}
} catch (e) {
// Show different warning if we're not sure the config saved
this.flashMessages.warning(
'You may not have access to the config endpoint. The secret engine was mounted, but the configuration settings may not be saved.'
);
}
return;
}
@task
@waitFor
*mountBackend(event) {
event.preventDefault();
const mountModel = this.args.mountModel;
const { type, path } = mountModel;
// only submit form if validations pass
if (!this.checkModelValidity(mountModel)) {
return;
}
const changedAttrKeys = Object.keys(mountModel.changedAttributes());
const updatesConfig =
changedAttrKeys.includes('casRequired') ||
changedAttrKeys.includes('deleteVersionAfter') ||
changedAttrKeys.includes('maxVersions');
try {
yield mountModel.save();
} catch (err) {
if (err.httpStatus === 403) {
this.flashMessages.danger(
'You do not have access to the sys/mounts endpoint. The secret engine was not mounted.'
);
return;
}
if (err.errors) {
const errors = err.errors.map((e) => {
if (typeof e === 'object') return e.title || e.message || JSON.stringify(e);
return e;
});
this.errorMessage = errors;
} else if (err.message) {
this.errorMessage = err.message;
} else {
this.errorMessage = 'An error occurred, check the vault logs.';
}
return;
}
if (mountModel.isV2KV && updatesConfig) {
yield this.showWarningsForKvv2();
}
this.flashMessages.success(
`Successfully mounted the ${type} ${
this.args.mountType === 'secret' ? 'secrets engine' : 'auth method'
} at ${path}.`
);
yield this.args.onMountSuccess(type, path);
return;
}
@action
onKeyUp(name, value) {
this.args.mountModel[name] = value;
this.checkModelWarnings();
}
@action
setMountType(value) {
this.args.mountModel.type = value;
this.typeChangeSideEffect(value);
this.checkPathChange(value);
}
}