-
Notifications
You must be signed in to change notification settings - Fork 989
/
EntityResource.java
343 lines (305 loc) · 15 KB
/
EntityResource.java
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package org.openmetadata.service.resources;
import static org.openmetadata.common.utils.CommonUtil.listOrEmpty;
import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty;
import static org.openmetadata.schema.type.MetadataOperation.CREATE;
import static org.openmetadata.schema.type.MetadataOperation.VIEW_BASIC;
import static org.openmetadata.service.util.EntityUtil.createOrUpdateOperation;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import javax.json.JsonPatch;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.schema.EntityInterface;
import org.openmetadata.schema.type.EntityHistory;
import org.openmetadata.schema.type.EntityReference;
import org.openmetadata.schema.type.Include;
import org.openmetadata.schema.type.MetadataOperation;
import org.openmetadata.schema.type.csv.CsvImportResult;
import org.openmetadata.service.Entity;
import org.openmetadata.service.OpenMetadataApplicationConfig;
import org.openmetadata.service.exception.CatalogExceptionMessage;
import org.openmetadata.service.jdbi3.EntityRepository;
import org.openmetadata.service.jdbi3.ListFilter;
import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.security.policyevaluator.CreateResourceContext;
import org.openmetadata.service.security.policyevaluator.OperationContext;
import org.openmetadata.service.security.policyevaluator.ResourceContext;
import org.openmetadata.service.security.policyevaluator.ResourceContextInterface;
import org.openmetadata.service.util.EntityUtil;
import org.openmetadata.service.util.EntityUtil.Fields;
import org.openmetadata.service.util.RestUtil;
import org.openmetadata.service.util.RestUtil.DeleteResponse;
import org.openmetadata.service.util.RestUtil.PatchResponse;
import org.openmetadata.service.util.RestUtil.PutResponse;
import org.openmetadata.service.util.ResultList;
@Slf4j
public abstract class EntityResource<T extends EntityInterface, K extends EntityRepository<T>> {
protected final Class<T> entityClass;
protected final String entityType;
protected final Set<String> allowedFields;
@Getter protected final K repository;
protected final Authorizer authorizer;
protected final Map<String, MetadataOperation> fieldsToViewOperations = new HashMap<>();
protected EntityResource(String entityType, Authorizer authorizer) {
this.entityType = entityType;
this.repository = (K) Entity.getEntityRepository(entityType);
this.entityClass = (Class<T>) Entity.getEntityClassFromType(entityType);
allowedFields = repository.getAllowedFields();
this.authorizer = authorizer;
addViewOperation("owner,followers,votes,tags,extension,domain,dataProducts,experts", VIEW_BASIC);
Entity.registerResourcePermissions(entityType, getEntitySpecificOperations());
}
/** Method used for initializing a resource, such as creating default policies, roles, etc. */
public void initialize(OpenMetadataApplicationConfig config) throws IOException {}
/**
* Method used for upgrading a resource such as adding new fields to entities, etc. that can't be done in bootstrap
* migrate
*/
public void upgrade() {
// Nothing to do in the default implementation
}
public final Fields getFields(String fields) {
return repository.getFields(fields);
}
protected T addHref(UriInfo uriInfo, T entity) {
Entity.withHref(uriInfo, entity.getOwner());
Entity.withHref(uriInfo, entity.getFollowers());
Entity.withHref(uriInfo, entity.getExperts());
Entity.withHref(uriInfo, entity.getReviewers());
Entity.withHref(uriInfo, entity.getChildren());
Entity.withHref(uriInfo, entity.getDomain());
Entity.withHref(uriInfo, entity.getDataProducts());
return entity;
}
protected List<MetadataOperation> getEntitySpecificOperations() {
return null;
}
public final ResultList<T> addHref(UriInfo uriInfo, ResultList<T> list) {
listOrEmpty(list.getData()).forEach(i -> addHref(uriInfo, i));
return list;
}
public ResultList<T> listInternal(
UriInfo uriInfo,
SecurityContext securityContext,
String fieldsParam,
ListFilter filter,
int limitParam,
String before,
String after) {
Fields fields = getFields(fieldsParam);
OperationContext listOperationContext = new OperationContext(entityType, getViewOperations(fields));
return listInternal(
uriInfo,
securityContext,
fields,
filter,
limitParam,
before,
after,
listOperationContext,
getResourceContext());
}
public ResultList<T> listInternal(
UriInfo uriInfo,
SecurityContext securityContext,
Fields fields,
ListFilter filter,
int limitParam,
String before,
String after,
OperationContext operationContext,
ResourceContextInterface resourceContext) {
RestUtil.validateCursors(before, after);
authorizer.authorize(securityContext, operationContext, resourceContext);
ResultList<T> resultList;
if (before != null) { // Reverse paging
resultList = repository.listBefore(uriInfo, fields, filter, limitParam, before);
} else { // Forward paging or first page
resultList = repository.listAfter(uriInfo, fields, filter, limitParam, after);
}
return addHref(uriInfo, resultList);
}
public T getInternal(UriInfo uriInfo, SecurityContext securityContext, UUID id, String fieldsParam, Include include) {
Fields fields = getFields(fieldsParam);
OperationContext operationContext = new OperationContext(entityType, getViewOperations(fields));
return getInternal(uriInfo, securityContext, id, fields, include, operationContext, getResourceContextById(id));
}
public T getInternal(
UriInfo uriInfo,
SecurityContext securityContext,
UUID id,
Fields fields,
Include include,
OperationContext operationContext,
ResourceContextInterface resourceContext) {
authorizer.authorize(securityContext, operationContext, resourceContext);
return addHref(uriInfo, repository.get(uriInfo, id, fields, include, false));
}
public T getVersionInternal(SecurityContext securityContext, UUID id, String version) {
OperationContext operationContext = new OperationContext(entityType, MetadataOperation.VIEW_BASIC);
return getVersionInternal(securityContext, id, version, operationContext, getResourceContextById(id));
}
public T getVersionInternal(
SecurityContext securityContext,
UUID id,
String version,
OperationContext operationContext,
ResourceContextInterface resourceContext) {
authorizer.authorize(securityContext, operationContext, resourceContext);
return repository.getVersion(id, version);
}
protected EntityHistory listVersionsInternal(SecurityContext securityContext, UUID id) {
OperationContext operationContext = new OperationContext(entityType, MetadataOperation.VIEW_BASIC);
return listVersionsInternal(securityContext, id, operationContext, getResourceContextById(id));
}
protected EntityHistory listVersionsInternal(
SecurityContext securityContext,
UUID id,
OperationContext operationContext,
ResourceContextInterface resourceContext) {
authorizer.authorize(securityContext, operationContext, resourceContext);
return repository.listVersions(id);
}
public T getByNameInternal(
UriInfo uriInfo, SecurityContext securityContext, String name, String fieldsParam, Include include) {
Fields fields = getFields(fieldsParam);
OperationContext operationContext = new OperationContext(entityType, getViewOperations(fields));
return getByNameInternal(
uriInfo, securityContext, name, fields, include, operationContext, getResourceContextByName(name));
}
public T getByNameInternal(
UriInfo uriInfo,
SecurityContext securityContext,
String name,
Fields fields,
Include include,
OperationContext operationContext,
ResourceContextInterface resourceContext) {
authorizer.authorize(securityContext, operationContext, resourceContext);
return addHref(uriInfo, repository.getByName(uriInfo, name, fields, include, false));
}
public Response create(UriInfo uriInfo, SecurityContext securityContext, T entity) {
OperationContext operationContext = new OperationContext(entityType, CREATE);
CreateResourceContext<T> createResourceContext = new CreateResourceContext<>(entityType, entity);
authorizer.authorize(securityContext, operationContext, createResourceContext);
entity = addHref(uriInfo, repository.create(uriInfo, entity));
return Response.created(entity.getHref()).entity(entity).build();
}
public Response createOrUpdate(UriInfo uriInfo, SecurityContext securityContext, T entity) {
repository.prepareInternal(entity, true);
// If entity does not exist, this is a create operation, else update operation
ResourceContext<T> resourceContext = getResourceContextByName(entity.getFullyQualifiedName());
MetadataOperation operation = createOrUpdateOperation(resourceContext);
OperationContext operationContext = new OperationContext(entityType, operation);
if (operation == CREATE) {
CreateResourceContext<T> createResourceContext = new CreateResourceContext<>(entityType, entity);
authorizer.authorize(securityContext, operationContext, createResourceContext);
entity = addHref(uriInfo, repository.create(uriInfo, entity));
return new PutResponse<>(Response.Status.CREATED, entity, RestUtil.ENTITY_CREATED).toResponse();
}
authorizer.authorize(securityContext, operationContext, resourceContext);
PutResponse<T> response = repository.createOrUpdate(uriInfo, entity);
addHref(uriInfo, response.getEntity());
return response.toResponse();
}
public Response patchInternal(UriInfo uriInfo, SecurityContext securityContext, UUID id, JsonPatch patch) {
OperationContext operationContext = new OperationContext(entityType, patch);
authorizer.authorize(securityContext, operationContext, getResourceContextById(id));
PatchResponse<T> response = repository.patch(uriInfo, id, securityContext.getUserPrincipal().getName(), patch);
addHref(uriInfo, response.getEntity());
return response.toResponse();
}
public Response delete(
UriInfo uriInfo, SecurityContext securityContext, UUID id, boolean recursive, boolean hardDelete) {
OperationContext operationContext = new OperationContext(entityType, MetadataOperation.DELETE);
authorizer.authorize(securityContext, operationContext, getResourceContextById(id));
DeleteResponse<T> response =
repository.delete(securityContext.getUserPrincipal().getName(), id, recursive, hardDelete);
repository.deleteFromSearch(response.getEntity(), response.getChangeType());
addHref(uriInfo, response.getEntity());
return response.toResponse();
}
public Response deleteByName(
UriInfo uriInfo, SecurityContext securityContext, String name, boolean recursive, boolean hardDelete) {
OperationContext operationContext = new OperationContext(entityType, MetadataOperation.DELETE);
authorizer.authorize(securityContext, operationContext, getResourceContextByName(name));
DeleteResponse<T> response =
repository.deleteByName(securityContext.getUserPrincipal().getName(), name, recursive, hardDelete);
repository.deleteFromSearch(response.getEntity(), response.getChangeType());
addHref(uriInfo, response.getEntity());
return response.toResponse();
}
public Response restoreEntity(UriInfo uriInfo, SecurityContext securityContext, UUID id) {
OperationContext operationContext = new OperationContext(entityType, MetadataOperation.EDIT_ALL);
authorizer.authorize(securityContext, operationContext, getResourceContextById(id));
PutResponse<T> response = repository.restoreEntity(securityContext.getUserPrincipal().getName(), entityType, id);
repository.restoreFromSearch(response.getEntity());
addHref(uriInfo, response.getEntity());
LOG.info("Restored {}:{}", Entity.getEntityTypeFromObject(response.getEntity()), response.getEntity().getId());
return response.toResponse();
}
public String exportCsvInternal(SecurityContext securityContext, String name) throws IOException {
OperationContext operationContext = new OperationContext(entityType, MetadataOperation.VIEW_ALL);
authorizer.authorize(securityContext, operationContext, getResourceContextByName(name));
return repository.exportToCsv(name, securityContext.getUserPrincipal().getName());
}
protected CsvImportResult importCsvInternal(SecurityContext securityContext, String name, String csv, boolean dryRun)
throws IOException {
OperationContext operationContext = new OperationContext(entityType, MetadataOperation.EDIT_ALL);
authorizer.authorize(securityContext, operationContext, getResourceContextByName(name));
return repository.importFromCsv(name, csv, dryRun, securityContext.getUserPrincipal().getName());
}
protected ResourceContext<T> getResourceContext() {
return new ResourceContext<>(entityType);
}
protected ResourceContext<T> getResourceContextById(UUID id) {
return new ResourceContext<>(entityType, id, null);
}
protected ResourceContext<T> getResourceContextByName(String name) {
return new ResourceContext<>(entityType, null, name);
}
protected static final MetadataOperation[] VIEW_ALL_OPERATIONS = {MetadataOperation.VIEW_ALL};
protected static final MetadataOperation[] VIEW_BASIC_OPERATIONS = {MetadataOperation.VIEW_BASIC};
private MetadataOperation[] getViewOperations(Fields fields) {
if (fields.getFieldList().isEmpty()) {
return VIEW_BASIC_OPERATIONS;
}
Set<MetadataOperation> viewOperations = new TreeSet<>();
for (String field : fields.getFieldList()) {
MetadataOperation operation = fieldsToViewOperations.get(field);
if (operation == null) {
return VIEW_ALL_OPERATIONS;
}
viewOperations.add(operation);
}
return viewOperations.toArray(new MetadataOperation[0]);
}
protected EntityReference getEntityReference(String entityType, String fqn) {
return EntityUtil.getEntityReference(entityType, fqn);
}
protected static List<EntityReference> getEntityReferences(String entityType, List<String> fqns) {
if (nullOrEmpty(fqns)) {
return null;
}
return EntityUtil.getEntityReferences(entityType, fqns);
}
protected void addViewOperation(String fieldsParam, MetadataOperation operation) {
String[] fields = fieldsParam.replace(" ", "").split(",");
for (String field : fields) {
if (allowedFields.contains(field)) {
fieldsToViewOperations.put(field, operation);
} else if (!"owner,followers,votes,tags,extension,domain,dataProducts,experts".contains(field)) {
// Some common fields for all the entities might be missing. Ignore it.
throw new IllegalArgumentException(CatalogExceptionMessage.invalidField(field));
}
}
}
}