-
Notifications
You must be signed in to change notification settings - Fork 159
/
saved_objects_wrapper.ts
226 lines (206 loc) · 8.43 KB
/
saved_objects_wrapper.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright OpenSearch Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import _ from 'lodash';
import {
HttpServiceStart,
SavedObject,
SavedObjectsBaseOptions,
SavedObjectsBulkCreateObject,
SavedObjectsBulkGetObject,
SavedObjectsBulkResponse,
SavedObjectsBulkUpdateObject,
SavedObjectsBulkUpdateOptions,
SavedObjectsBulkUpdateResponse,
SavedObjectsCheckConflictsObject,
SavedObjectsCheckConflictsResponse,
SavedObjectsClientWrapperFactory,
SavedObjectsCreateOptions,
SavedObjectsDeleteOptions,
SavedObjectsFindOptions,
SavedObjectsFindResponse,
SavedObjectsUpdateOptions,
SavedObjectsUpdateResponse,
} from 'opensearch-dashboards/server';
import { SecurityPluginConfigType } from '..';
import { OpenSearchDashboardsAuthState } from '../auth/types/authentication_type';
import {
DEFAULT_TENANT,
GLOBAL_TENANT_SYMBOL,
globalTenantName,
isPrivateTenant,
PRIVATE_TENANT_SYMBOL,
} from '../../common';
export class SecuritySavedObjectsClientWrapper {
public httpStart?: HttpServiceStart;
public config?: SecurityPluginConfigType;
constructor() {}
public wrapperFactory: SavedObjectsClientWrapperFactory = (wrapperOptions) => {
const state: OpenSearchDashboardsAuthState =
(this.httpStart!.auth.get(wrapperOptions.request).state as OpenSearchDashboardsAuthState) ||
{};
const selectedTenant = state.selectedTenant;
const username = state.authInfo?.user_name;
const isGlobalEnabled = this.config!.multitenancy.tenants.enable_global;
const isPrivateEnabled = this.config!.multitenancy.tenants.enable_private;
let namespaceValue = selectedTenant;
const createWithNamespace = async <T = unknown>(
type: string,
attributes: T,
options?: SavedObjectsCreateOptions
) => {
namespaceValue = this.getNamespaceValue(selectedTenant, isPrivateEnabled, username);
_.assign(options, { namespace: [namespaceValue] });
return await wrapperOptions.client.create(type, attributes, options);
};
const bulkGetWithNamespace = async <T = unknown>(
objects: SavedObjectsBulkGetObject[] = [],
options: SavedObjectsBaseOptions = {}
): Promise<SavedObjectsBulkResponse<T>> => {
namespaceValue = this.getNamespaceValue(selectedTenant, isPrivateEnabled, username);
_.assign(options, { namespace: [namespaceValue] });
return await wrapperOptions.client.bulkGet(objects, options);
};
const findWithNamespace = async <T = unknown>(
options: SavedObjectsFindOptions
): Promise<SavedObjectsFindResponse<T>> => {
const tenants = state.authInfo?.tenants;
const availableTenantNames = Object.keys(tenants!);
availableTenantNames.push(DEFAULT_TENANT); // The value of namespace is "default" if saved objects are created when opensearch_security.multitenancy.enable_aggregation_view is set to false. So adding it to find.
if (isGlobalEnabled) {
availableTenantNames.push(GLOBAL_TENANT_SYMBOL);
}
if (isPrivateEnabled) {
availableTenantNames.push(PRIVATE_TENANT_SYMBOL + username);
}
if (availableTenantNames.includes(globalTenantName)) {
let index = availableTenantNames.indexOf(globalTenantName);
if (index > -1) {
availableTenantNames.splice(index, 1);
}
index = availableTenantNames.indexOf(username!);
if (index > -1) {
availableTenantNames.splice(index, 1);
}
}
if (isPrivateTenant(selectedTenant!)) {
namespaceValue = selectedTenant! + username;
}
if (!!options.namespaces) {
const namespacesToInclude = Array.isArray(options.namespaces)
? options.namespaces
: [options.namespaces];
const typeToNamespacesMap: any = {};
const searchTypes = Array.isArray(options.type) ? options.type : [options.type];
searchTypes.forEach((t) => {
typeToNamespacesMap[t] = namespacesToInclude;
});
if (searchTypes.includes('config')) {
if (namespacesToInclude.includes(namespaceValue)) {
typeToNamespacesMap.config = [namespaceValue];
} else {
delete typeToNamespacesMap.config;
}
}
options.typeToNamespacesMap = new Map(Object.entries(typeToNamespacesMap));
options.type = '';
options.namespaces = [];
} else {
options.namespaces = [namespaceValue];
}
return await wrapperOptions.client.find(options);
};
const getWithNamespace = async <T = unknown>(
type: string,
id: string,
options: SavedObjectsBaseOptions = {}
): Promise<SavedObject<T>> => {
namespaceValue = this.getNamespaceValue(selectedTenant, isPrivateEnabled, username);
_.assign(options, { namespace: [namespaceValue] });
return await wrapperOptions.client.get(type, id, options);
};
const updateWithNamespace = async <T = unknown>(
type: string,
id: string,
attributes: Partial<T>,
options: SavedObjectsUpdateOptions = {}
): Promise<SavedObjectsUpdateResponse<T>> => {
namespaceValue = this.getNamespaceValue(selectedTenant, isPrivateEnabled, username);
_.assign(options, { namespace: [namespaceValue] });
return await wrapperOptions.client.update(type, id, attributes, options);
};
const bulkCreateWithNamespace = async <T = unknown>(
objects: Array<SavedObjectsBulkCreateObject<T>>,
options?: SavedObjectsCreateOptions
): Promise<SavedObjectsBulkResponse<T>> => {
namespaceValue = this.getNamespaceValue(selectedTenant, isPrivateEnabled, username);
_.assign(options, { namespace: [namespaceValue] });
return await wrapperOptions.client.bulkCreate(objects, options);
};
const bulkUpdateWithNamespace = async <T = unknown>(
objects: Array<SavedObjectsBulkUpdateObject<T>>,
options?: SavedObjectsBulkUpdateOptions
): Promise<SavedObjectsBulkUpdateResponse<T>> => {
namespaceValue = this.getNamespaceValue(selectedTenant, isPrivateEnabled, username);
_.assign(options, { namespace: [namespaceValue] });
return await wrapperOptions.client.bulkUpdate(objects, options);
};
const deleteWithNamespace = async (
type: string,
id: string,
options: SavedObjectsDeleteOptions = {}
) => {
namespaceValue = this.getNamespaceValue(selectedTenant, isPrivateEnabled, username);
_.assign(options, { namespace: [namespaceValue] });
return await wrapperOptions.client.delete(type, id, options);
};
const checkConflictsWithNamespace = async (
objects: SavedObjectsCheckConflictsObject[] = [],
options: SavedObjectsBaseOptions = {}
): Promise<SavedObjectsCheckConflictsResponse> => {
namespaceValue = this.getNamespaceValue(selectedTenant, isPrivateEnabled, username);
_.assign(options, { namespace: [namespaceValue] });
return await wrapperOptions.client.checkConflicts(objects, options);
};
return {
...wrapperOptions.client,
get: getWithNamespace,
update: updateWithNamespace,
bulkCreate: bulkCreateWithNamespace,
bulkGet: bulkGetWithNamespace,
bulkUpdate: bulkUpdateWithNamespace,
create: createWithNamespace,
delete: deleteWithNamespace,
errors: wrapperOptions.client.errors,
checkConflicts: checkConflictsWithNamespace,
addToNamespaces: wrapperOptions.client.addToNamespaces,
find: findWithNamespace,
deleteFromNamespaces: wrapperOptions.client.deleteFromNamespaces,
};
};
private isAPrivateTenant(selectedTenant: string | undefined, isPrivateEnabled: boolean) {
return selectedTenant !== undefined && isPrivateEnabled && isPrivateTenant(selectedTenant);
}
private getNamespaceValue(
selectedTenant: string | undefined,
isPrivateEnabled: boolean,
username: string | undefined
) {
let namespaceValue = selectedTenant;
if (this.isAPrivateTenant(selectedTenant, isPrivateEnabled)) {
namespaceValue = selectedTenant! + username;
}
return namespaceValue;
}
}