/
role.js
197 lines (175 loc) · 5.56 KB
/
role.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { assign } from '@ember/polyfills';
import { assert } from '@ember/debug';
import ControlGroupError from 'vault/lib/control-group-error';
import ApplicationAdapter from '../application';
import { allSettled } from 'rsvp';
import { addToArray } from 'vault/helpers/add-to-array';
import { removeFromArray } from 'vault/helpers/remove-from-array';
export default ApplicationAdapter.extend({
namespace: 'v1',
pathForType() {
assert('Generate the url dynamically based on role type', false);
},
urlFor(backend, id, type = 'dynamic') {
let role = 'roles';
if (type === 'static') {
role = 'static-roles';
}
let url = `${this.buildURL()}/${backend}/${role}`;
if (id) {
url = `${this.buildURL()}/${backend}/${role}/${id}`;
}
return url;
},
staticRoles(backend, id) {
return this.ajax(this.urlFor(backend, id, 'static'), 'GET', this.optionsForQuery(id)).then(resp => {
if (id) {
return {
...resp,
type: 'static',
backend,
id,
};
}
return resp;
});
},
dynamicRoles(backend, id) {
return this.ajax(this.urlFor(backend, id), 'GET', this.optionsForQuery(id)).then(resp => {
if (id) {
return {
...resp,
type: 'dynamic',
backend,
id,
};
}
return resp;
});
},
optionsForQuery(id) {
let data = {};
if (!id) {
data['list'] = true;
}
return { data };
},
queryRecord(store, type, query) {
const { backend, id } = query;
if (query.type === 'static') {
return this.staticRoles(backend, id);
} else if (query?.type === 'dynamic') {
return this.dynamicRoles(backend, id);
}
// if role type is not defined, try both
return allSettled([this.staticRoles(backend, id), this.dynamicRoles(backend, id)]).then(
([staticResp, dynamicResp]) => {
if (staticResp.state === 'rejected' && dynamicResp.state === 'rejected') {
let reason = staticResp.reason;
if (dynamicResp.reason instanceof ControlGroupError) {
throw dynamicResp.reason;
}
if (reason?.httpStatus < dynamicResp.reason?.httpStatus) {
reason = dynamicResp.reason;
}
throw reason;
}
// Names are distinct across both types of role,
// so only one request should ever come back with value
let type = staticResp.value ? 'static' : 'dynamic';
let successful = staticResp.value || dynamicResp.value;
let resp = {
data: {},
backend,
id,
type,
};
resp.data = assign({}, successful.data);
return resp;
}
);
},
query(store, type, query) {
const { backend } = query;
const staticReq = this.staticRoles(backend);
const dynamicReq = this.dynamicRoles(backend);
return allSettled([staticReq, dynamicReq]).then(([staticResp, dynamicResp]) => {
let resp = {
backend,
data: { keys: [] },
};
if (staticResp.reason && dynamicResp.reason) {
// both failed, throw error
throw dynamicResp.reason;
}
// at least one request has data
let staticRoles = [];
let dynamicRoles = [];
if (staticResp.value) {
staticRoles = staticResp.value.data.keys;
}
if (dynamicResp.value) {
dynamicRoles = dynamicResp.value.data.keys;
}
resp.data = assign(
{},
resp.data,
{ keys: [...staticRoles, ...dynamicRoles] },
{ backend },
{ staticRoles, dynamicRoles }
);
return resp;
});
},
async _updateAllowedRoles(store, { role, backend, db, type = 'add' }) {
const connection = await store.queryRecord('database/connection', { backend, id: db });
let roles = [...connection.allowed_roles];
const allowedRoles = type === 'add' ? addToArray([roles, role]) : removeFromArray([roles, role]);
connection.allowed_roles = allowedRoles;
return connection.save();
},
async createRecord(store, type, snapshot) {
const serializer = store.serializerFor(type.modelName);
const data = serializer.serialize(snapshot);
const roleType = snapshot.attr('type');
const backend = snapshot.attr('backend');
const id = snapshot.attr('name');
const db = snapshot.attr('database');
try {
await this._updateAllowedRoles(store, {
role: id,
backend,
db: db[0],
});
} catch (e) {
throw new Error('Could not update allowed roles for selected database. Check Vault logs for details');
}
return this.ajax(this.urlFor(backend, id, roleType), 'POST', { data }).then(() => {
// ember data doesn't like 204s if it's not a DELETE
return {
data: assign({}, data, { id }),
};
});
},
async deleteRecord(store, type, snapshot) {
const roleType = snapshot.attr('type');
const backend = snapshot.attr('backend');
const id = snapshot.attr('name');
const db = snapshot.attr('database');
await this._updateAllowedRoles(store, {
role: id,
backend,
db: db[0],
type: 'remove',
});
return this.ajax(this.urlFor(backend, id, roleType), 'DELETE');
},
async updateRecord(store, type, snapshot) {
const serializer = store.serializerFor(type.modelName);
const data = serializer.serialize(snapshot);
const roleType = snapshot.attr('type');
const backend = snapshot.attr('backend');
const id = snapshot.attr('name');
return this.ajax(this.urlFor(backend, id, roleType), 'POST', { data }).then(() => data);
},
});