-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathPolicyCrudService.java
854 lines (761 loc) · 36 KB
/
PolicyCrudService.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/*
* Copyright 2019 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
package com.redhat.cloud.policies.app.rest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redhat.cloud.policies.app.lightweight.OrgIdLatestUpdateRepository;
import com.redhat.cloud.policies.app.lightweight.LightweightEngine;
import com.redhat.cloud.policies.app.auth.RhIdPrincipal;
import com.redhat.cloud.policies.app.model.ColumnGetter;
import com.redhat.cloud.policies.app.model.Msg;
import com.redhat.cloud.policies.app.model.Policy;
import com.redhat.cloud.policies.app.model.UUIDHelperBean;
import com.redhat.cloud.policies.app.model.engine.HistoryItem;
import com.redhat.cloud.policies.app.model.history.PoliciesHistoryEntry;
import com.redhat.cloud.policies.app.model.history.PoliciesHistoryRepository;
import com.redhat.cloud.policies.app.model.pager.Page;
import com.redhat.cloud.policies.app.model.pager.Pager;
import com.redhat.cloud.policies.app.rest.utils.PagingUtils;
import io.micrometer.core.annotation.Timed;
import io.quarkus.logging.Log;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.ParameterIn;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.headers.Header;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameters;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.hibernate.exception.ConstraintViolationException;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceException;
import jakarta.transaction.SystemException;
import jakarta.transaction.TransactionManager;
import jakarta.transaction.Transactional;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Valid;
import jakarta.validation.Validator;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.EntityTag;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.ResponseBuilder;
import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.core.UriInfo;
import java.net.ConnectException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
@Path("/api/policies/v1.0/policies")
@Produces("application/json")
@Consumes("application/json")
@Timed("PolicySvc")
@RequestScoped
public class PolicyCrudService {
public static final String MISSING_PERMISSIONS_TO_RETRIEVE_POLICIES = "Missing permissions to retrieve policies";
public static final String MISSING_PERMISSIONS_TO_VERIFY_POLICY = "Missing permissions to verify policy";
public static final String MISSING_PERMISSIONS_TO_UPDATE_POLICY = "Missing permissions to update policy";
public static final String ERROR_STRING = "error";
public static final String CTIME_STRING = "ctime";
@Inject
@RestClient
LightweightEngine lightweightEngine;
@Inject
OrgIdLatestUpdateRepository orgIdLatestUpdateRepository;
@Context
UriInfo uriInfo;
@SuppressWarnings("CdiInjectionPointsInspection")
@Inject
RhIdPrincipal user;
@Inject
EntityManager entityManager;
@Inject
TransactionManager transactionManager;
@Inject
UUIDHelperBean uuidHelper;
@Inject
Validator validator;
@Inject
PoliciesHistoryRepository policiesHistoryRepository;
@Inject
ObjectMapper objectMapper;
// workaround for returning generic types: https://github.com/swagger-api/swagger-core/issues/498#issuecomment-74510379
// This class is used only for swagger return type
private static class PagedResponseOfPolicy extends PagingUtils.PagedResponse<Policy> {
private PagedResponseOfPolicy(Page<Policy> page) {
super(page);
}
}
@Operation(summary = "Return all policies for a given account")
@GET
@Path("/")
@Parameters({
@Parameter(
name = "offset",
in = ParameterIn.QUERY,
description = "Page number, starts 0, if not specified uses 0.",
schema = @Schema(type = SchemaType.INTEGER)
),
@Parameter(
name = "limit",
in = ParameterIn.QUERY,
description = "Number of items per page, if not specified uses 50. " + Pager.NO_LIMIT + " can be used to specify an unlimited page, when specified it ignores the offset",
schema = @Schema(type = SchemaType.INTEGER)
),
@Parameter(
name = "sortColumn",
in = ParameterIn.QUERY,
description = "Column to sort the results by",
schema = @Schema(
type = SchemaType.STRING,
enumeration = {
"name",
"description",
"is_enabled",
"mtime",
"last_triggered"
}
)
),
@Parameter(
name = "sortDirection",
in = ParameterIn.QUERY,
description = "Sort direction used",
schema = @Schema(
type = SchemaType.STRING,
enumeration = {
"asc",
"desc"
}
)
),
@Parameter(
name = "filter[name]",
in = ParameterIn.QUERY,
description = "Filtering policies by the name depending on the Filter operator used.",
schema = @Schema(type = SchemaType.STRING)
),
@Parameter(
name = "filter:op[name]",
in = ParameterIn.QUERY,
description = "Operations used with the filter",
schema = @Schema(
type = SchemaType.STRING,
enumeration = {
"equal",
"like",
"ilike",
"not_equal"
},
defaultValue = "equal"
)
),
@Parameter(
name = "filter[description]",
in = ParameterIn.QUERY,
description = "Filtering policies by the description depending on the Filter operator used.",
schema = @Schema(type = SchemaType.STRING)
),
@Parameter(
name = "filter:op[description]",
in = ParameterIn.QUERY,
description = "Operations used with the filter",
schema = @Schema(
type = SchemaType.STRING,
enumeration = {
"equal",
"like",
"ilike",
"not_equal"
},
defaultValue = "equal"
)
),
@Parameter(
name = "filter[is_enabled]",
in = ParameterIn.QUERY,
description = "Filtering policies by the is_enabled field." +
"Defaults to true if no operand is given.",
schema = @Schema(type = SchemaType.STRING, defaultValue = "true", enumeration = {"true", "false"})
),
})
@APIResponse(responseCode = "400", description = "Bad parameter for sorting was passed")
@APIResponse(responseCode = "404", description = "No policies found for customer")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "200", description = "Policies found", content =
@Content(schema = @Schema(implementation = PagedResponseOfPolicy.class)),
headers = @Header(name = "TotalCount", description = "Total number of items found",
schema = @Schema(type = SchemaType.INTEGER)))
public Response getPoliciesForCustomer() {
if (!user.canReadPolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_RETRIEVE_POLICIES)).build();
}
Page<Policy> page;
try {
Pager pager = PagingUtils.extractPager(uriInfo, new ColumnGetter(Policy.class));
page = Policy.pagePoliciesForCustomer(entityManager, user.getOrgId(), pager);
} catch (IllegalArgumentException iae) {
return Response.status(400, iae.getLocalizedMessage()).build();
}
return PagingUtils.responseBuilder(page).build();
}
@Operation(summary = "Return all policy ids for a given account after applying the filters")
@GET
@Path("/ids")
@Parameters({
@Parameter(
name = "filter[name]",
in = ParameterIn.QUERY,
description = "Filtering policies by the name depending on the Filter operator used.",
schema = @Schema(type = SchemaType.STRING)
),
@Parameter(
name = "filter:op[name]",
in = ParameterIn.QUERY,
description = "Operations used with the filter",
schema = @Schema(
type = SchemaType.STRING,
enumeration = {
"equal",
"like",
"ilike",
"not_equal"
},
defaultValue = "equal"
)
),
@Parameter(
name = "filter[description]",
in = ParameterIn.QUERY,
description = "Filtering policies by the description depending on the Filter operator used.",
schema = @Schema(type = SchemaType.STRING)
),
@Parameter(
name = "filter:op[description]",
in = ParameterIn.QUERY,
description = "Operations used with the filter",
schema = @Schema(
type = SchemaType.STRING,
enumeration = {
"equal",
"like",
"ilike",
"not_equal"
},
defaultValue = "equal"
)
),
@Parameter(
name = "filter[is_enabled]",
in = ParameterIn.QUERY,
description = "Filtering policies by the is_enabled field." +
"Defaults to true if no operand is given.",
schema = @Schema(type = SchemaType.STRING, defaultValue = "true", enumeration = {"true", "false"})
),
})
@APIResponse(responseCode = "400", description = "Bad parameter for sorting was passed")
@APIResponse(responseCode = "404", description = "No policies found for customer")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "200", description = "PolicyIds found", content =
@Content(schema = @Schema(type = SchemaType.ARRAY, implementation = UUID.class)))
public Response getPolicyIdsForCustomer() {
if (!user.canReadPolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_RETRIEVE_POLICIES)).build();
}
List<UUID> uuids;
try {
Pager pager = PagingUtils.extractPager(uriInfo, new ColumnGetter(Policy.class));
uuids = Policy.getPolicyIdsForCustomer(entityManager, user.getOrgId(), pager);
} catch (IllegalArgumentException iae) {
return Response.status(400, iae.getLocalizedMessage()).build();
}
return Response.ok(uuids).build();
}
@Operation(summary = "Validate (and possibly persist) a passed policy for the given account")
@Parameter(name = "alsoStore",
description = "If passed and set to true, the passed policy is also persisted (if it is valid)")
@APIResponses({
@APIResponse(responseCode = "500", description = "Internal error"),
@APIResponse(responseCode = "400", description = "No policy provided or policy validation failed",
content = @Content(schema = @Schema(implementation = Msg.class))),
@APIResponse(responseCode = "409", description = "Persisting failed",
content = @Content(schema = @Schema(implementation = Msg.class))),
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action"),
@APIResponse(responseCode = "201", description = "Policy persisted",
content = @Content(schema = @Schema(implementation = Policy.class))),
@APIResponse(responseCode = "200", description = "Policy validated")
})
@POST
@Path("/")
@Transactional
public Response storePolicy(@QueryParam("alsoStore") boolean alsoStore, @NotNull @Valid Policy policy) {
if (!user.canReadPolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_VERIFY_POLICY)).build();
}
// We use the indirection, so that for testing we can produce known UUIDs
policy.id = uuidHelper.getUUID();
policy.customerid = user.getAccount();
policy.orgId = user.getOrgId();
Response invalidNameResponse = isNameUnique(policy);
if (invalidNameResponse != null) {
return invalidNameResponse;
}
try {
lightweightEngine.validateCondition(policy.conditions);
} catch (Exception e) {
return Response.status(400, e.getMessage()).entity(getEngineExceptionMsg(e)).build();
}
if (!alsoStore) {
return Response.status(200).entity(new Msg("Policy validated")).build();
}
if (!user.canWritePolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg("Missing permissions to store policy")).build();
}
policy.persist();
setLatestToNow();
// Policy is persisted. Return its location.
URI location =
UriBuilder.fromResource(PolicyCrudService.class).path(PolicyCrudService.class, "getPolicy").build(policy.id);
return Response.created(location).entity(policy).build();
}
private Response getResponseSavingPolicyThrowable(Throwable t) {
if (t instanceof PersistenceException && t.getCause() instanceof ConstraintViolationException) {
return Response.status(409, t.getMessage()).entity(new Msg("Constraint violation")).build();
} else {
Log.warn("Getting response failed", t);
return Response.status(500, t.getMessage()).build();
}
}
private Msg getEngineExceptionMsg(Exception e) {
Msg msg;
if (e instanceof RuntimeException && e.getCause() instanceof ConnectException
|| e instanceof ProcessingException) {
msg = new Msg("Connection to backend-engine failed. Please retry later");
} else {
msg = new Msg(e.getMessage());
}
return msg;
}
@Operation(summary = "Delete a single policy for a customer by its id")
@DELETE
@Path("/{id}")
@APIResponse(responseCode = "200", description = "Policy deleted")
@APIResponse(responseCode = "404", description = "Policy not found")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@Parameter(name = "id", description = "UUID of the policy")
@Transactional
public Response deletePolicy(@PathParam("id") UUID policyId) {
if (!user.canWritePolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg("Missing permissions to delete policy")).build();
}
Policy policy = findPolicy(policyId);
if (policy == null) {
return Response.status(Response.Status.NOT_FOUND).build();
} else {
policy.delete(policy);
setLatestToNow();
return Response.ok(policy).build();
}
}
@Operation(summary = "Delete policies for a customer by the ids passed in the body. Result will be a list of deleted UUIDs")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "200", description = "Policies deleted",
content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = UUID.class)))
@DELETE
@Path("/ids")
@Transactional
public Response deletePolicies(List<UUID> uuids) {
if (!user.canWritePolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg("Missing permissions to delete policy")).build();
}
boolean dbUpdated = false;
for (UUID uuid : uuids) {
Policy policy = findPolicy(uuid);
if (policy != null) {
policy.delete();
dbUpdated = true;
}
}
if (dbUpdated) {
setLatestToNow();
}
return Response.ok(uuids).build();
}
@Operation(summary = "Enable/disable a policy")
@Parameter(name = "id", description = "ID of the Policy")
@Parameter(name = "enabled",
schema = @Schema(type = SchemaType.BOOLEAN, defaultValue = "false"),
description = "Should the policy be enabled (true) or disabled (false, default)")
@APIResponse(responseCode = "200", description = "Policy updated")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "404", description = "Policy not found")
@APIResponse(responseCode = "500", description = "Updating failed")
@POST
@Path("/{id:[0-9a-fA-F-]+}/enabled")
@Transactional
public Response setEnabledStateForPolicy(@PathParam("id") UUID policyId, @QueryParam("enabled") boolean shouldBeEnabled) {
if (!user.canWritePolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_UPDATE_POLICY)).build();
}
Policy storedPolicy = findPolicy(policyId);
if (storedPolicy == null) {
return Response.status(404, "Original policy not found").build();
} else {
storedPolicy.isEnabled = shouldBeEnabled;
storedPolicy.setMtimeToNow();
storedPolicy.persist();
setLatestToNow();
return Response.ok().build();
}
}
@Operation(summary = "Enable/disable policies identified by list of uuid in body")
@Parameter(name = "uuids", schema = @Schema(type = SchemaType.ARRAY, implementation = UUID.class))
@APIResponse(responseCode = "200", description = "Policy updated", content = @Content(
schema = @Schema(
type = SchemaType.ARRAY,
implementation = UUID.class
)
))
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@POST
@Path("/ids/enabled")
@Transactional
public Response setEnabledStateForPolicies(@QueryParam("enabled") boolean shouldBeEnabled, @NotEmpty List<UUID> uuids) {
if (!user.canWritePolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_UPDATE_POLICY)).build();
}
List<UUID> changed = new ArrayList<>(uuids.size());
for (UUID uuid : uuids) {
Policy storedPolicy = findPolicy(uuid);
if (storedPolicy != null) {
storedPolicy.isEnabled = shouldBeEnabled;
storedPolicy.setMtimeToNow();
storedPolicy.persist();
changed.add(uuid);
}
}
if (!changed.isEmpty()) {
setLatestToNow();
}
return Response.ok(changed).build();
}
@Operation(summary = "Update a single policy for a customer by its id")
@PUT
@Path("/{policyId}")
@APIResponse(responseCode = "200", description = "Policy updated or policy validated",
content = @Content(schema = @Schema(implementation = Policy.class))
)
@APIResponse(responseCode = "400", description = "Invalid or no policy provided")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "404", description = "Policy did not exist - did you store it before?")
@APIResponse(responseCode = "409", description = "Persisting failed",
content = @Content(schema = @Schema(implementation = Msg.class))
)
@Transactional
public Response updatePolicy(@QueryParam("dry") boolean dryRun, @PathParam("policyId") UUID policyId,
@NotNull @Valid Policy policy) {
if (!user.canWritePolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_UPDATE_POLICY)).build();
}
Policy storedPolicy = findPolicy(policyId);
ResponseBuilder builder;
if (storedPolicy == null) {
builder = Response.status(404, "Original policy not found");
} else {
if (!policy.id.equals(policyId)) {
builder = Response.status(400, "Invalid policy");
} else {
Response invalidNameResponse = isNameUnique(policy);
if (invalidNameResponse != null) {
return invalidNameResponse;
}
try {
lightweightEngine.validateCondition(policy.conditions);
} catch (Exception e) {
return Response.status(400, e.getMessage()).entity(getEngineExceptionMsg(e)).build();
}
if (dryRun) {
return Response.status(200).entity(new Msg("Policy validated")).build();
}
// All is good, we can now do the real work
try {
storedPolicy.populateFrom(policy);
storedPolicy.setMtimeToNow();
setLatestToNow();
return Response.ok(storedPolicy).build();
} catch (Throwable t) {
try {
transactionManager.setRollbackOnly();
} catch (SystemException ex) {
throw new RuntimeException(ex);
}
return getResponseSavingPolicyThrowable(t);
}
}
}
return builder.build();
}
@Operation(summary = "Validates a Policy condition")
@POST
@Path("/validate")
@APIResponses({
@APIResponse(responseCode = "200", description = "Condition validated", content = @Content(schema = @Schema(implementation = Msg.class))),
@APIResponse(responseCode = "400", description = "No policy provided or condition not valid", content = @Content(schema = @Schema(implementation = Msg.class))),
@APIResponse(responseCode = "500", description = "Internal error")
})
public Response validateCondition(@Valid @NotNull Policy policy) {
if (!user.canReadPolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_VERIFY_POLICY)).build();
}
try {
lightweightEngine.validateCondition(policy.conditions);
} catch (Exception e) {
return Response.status(400, e.getMessage()).entity(getEngineExceptionMsg(e)).build();
}
return Response.status(200).entity(new Msg("Policy.condition validated")).build();
}
@Operation(summary = "Validates the Policy.name and verifies if it is unique.")
@POST
@Path("/validate-name")
@RequestBody(content = {@Content(schema = @Schema(type = SchemaType.STRING))})
@APIResponses({
@APIResponse(responseCode = "200", description = "Name validated", content = @Content(schema = @Schema(implementation = Msg.class))),
@APIResponse(responseCode = "400", description = "Policy validation failed", content = @Content(schema = @Schema(implementation = Msg.class))),
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action", content = @Content(schema = @Schema(implementation = Msg.class))),
@APIResponse(responseCode = "409", description = "Name not unique"),
@APIResponse(responseCode = "500", description = "Internal error")
})
@Parameter(name = "id", description = "UUID of the policy")
public Response validateName(@NotNull String jsonPolicyName, @QueryParam("id") UUID id) {
if (!user.canReadPolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_VERIFY_POLICY)).build();
}
String policyName;
try {
policyName = objectMapper.readValue(jsonPolicyName, String.class);
} catch (JsonProcessingException jpe) {
return Response.status(400, "Invalid policy name received in body").build();
}
Policy policy = new Policy();
policy.id = id;
policy.name = policyName;
Set<ConstraintViolation<Policy>> result = validator.validateProperty(policy, "name");
if (result.size() > 0) {
String error = String.join(
";",
result.stream().map(ConstraintViolation::getMessage).collect(Collectors.toSet())
);
return Response.status(400).entity(new Msg(error)).build();
}
Response isNameValid = isNameUnique(policy);
if (isNameValid != null) {
return isNameValid;
}
return Response.status(200).entity(new Msg("Policy.name validated")).build();
}
@Operation(summary = "Retrieve a single policy for a customer by its id")
@GET
@Path("/{id}")
@APIResponse(responseCode = "200", description = "Policy found", content =
@Content(schema = @Schema(implementation = Policy.class)))
@APIResponse(responseCode = "404", description = "Policy not found")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action", content = @Content(schema = @Schema(implementation = Msg.class)))
@Parameter(name = "id", description = "UUID of the policy")
public Response getPolicy(@PathParam("id") UUID policyId) {
if (!user.canReadPolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_RETRIEVE_POLICIES)).build();
}
Policy policy = findPolicy(policyId);
ResponseBuilder builder;
if (policy == null) {
builder = Response.status(Response.Status.NOT_FOUND);
} else {
builder = Response.ok(policy);
EntityTag etag = new EntityTag(String.valueOf(policy.hashCode()));
builder.header("ETag", etag);
}
return builder.build();
}
// workaround for returning generic types: https://github.com/swagger-api/swagger-core/issues/498#issuecomment-74510379
// This class is used only for swagger return type
private static class PagedResponseOfHistoryItem extends PagingUtils.PagedResponse<HistoryItem> {
private PagedResponseOfHistoryItem(Page<HistoryItem> page) {
super(page);
}
}
@Operation(summary = "Retrieve the trigger history of a single policy")
@APIResponse(responseCode = "200", description = "History could be retrieved",
content = @Content(schema = @Schema(implementation = PagedResponseOfHistoryItem.class)),
headers = @Header(name = "TotalCount", description = "Total number of items found",
schema = @Schema(type = SchemaType.INTEGER)))
@APIResponse(responseCode = "400", description = "Bad parameters passed")
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "404", description = "Policy not found")
@APIResponse(responseCode = "500", description = "Retrieval of History failed")
@Parameters({
@Parameter(
name = "offset",
in = ParameterIn.QUERY,
description = "Page number, starts 0, if not specified uses 0.",
schema = @Schema(type = SchemaType.INTEGER)
),
@Parameter(
name = "limit",
in = ParameterIn.QUERY,
description = "Number of items per page, if not specified uses 50. Maximum value is 200.",
schema = @Schema(type = SchemaType.INTEGER)
),
@Parameter(
name = "filter[name]",
in = ParameterIn.QUERY,
description = "Filtering history entries by the host name depending on the Filter operator used.",
schema = @Schema(type = SchemaType.STRING)
),
@Parameter(
name = "filter:op[name]",
in = ParameterIn.QUERY,
description = "Operations used with the name filter",
schema = @Schema(
type = SchemaType.STRING,
enumeration = {
"equal",
"like",
"not_equal"
},
defaultValue = "equal"
)
),
@Parameter(
name = "filter[id]",
in = ParameterIn.QUERY,
description = "Filtering history entries by the id depending on the Filter operator used.",
schema = @Schema(type = SchemaType.STRING)
),
@Parameter(
name = "filter:op[id]",
in = ParameterIn.QUERY,
description = "Operations used with the name filter",
schema = @Schema(
type = SchemaType.STRING,
enumeration = {
"equal",
"not_equal",
"like"
},
defaultValue = "equal"
)
),
@Parameter(
name = "sortColumn",
in = ParameterIn.QUERY,
description = "Column to sort the results by",
schema = @Schema(
type = SchemaType.STRING,
enumeration = {
"hostName",
CTIME_STRING
},
defaultValue = CTIME_STRING
)
),
@Parameter(
name = "sortDirection",
in = ParameterIn.QUERY,
description = "Sort direction used",
schema = @Schema(
type = SchemaType.STRING,
enumeration = {
"asc",
"desc"
}
)
),
@Parameter(name = "id", description = "UUID of the policy")
})
@GET
@Path("/{id}/history/trigger")
public Response getTriggerHistoryForPolicy(@PathParam("id") UUID policyId) {
if (!user.canReadPolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg("Missing permissions to retrieve the policy history")).build();
}
ResponseBuilder builder;
Policy policy = findPolicy(policyId);
if (policy == null) {
builder = Response.status(Response.Status.NOT_FOUND);
} else {
try {
Pager pager = PagingUtils.extractPager(uriInfo, new ColumnGetter(PoliciesHistoryEntry.class));
builder = buildHistoryResponse(policyId, pager);
} catch (IllegalArgumentException iae) {
builder = Response.status(400, iae.getMessage());
} catch (Exception e) {
String msg = "Retrieval of history failed with: " + e.getMessage();
Log.warn(msg);
builder = Response.serverError().entity(msg);
}
}
return builder.build();
}
private ResponseBuilder buildHistoryResponse(UUID policyId, Pager pager) {
List<HistoryItem> items;
long totalCount = policiesHistoryRepository.count(user.getOrgId(), user.getHostGroupIds(), policyId, pager);
if (totalCount > 0) {
items = policiesHistoryRepository.find(user.getOrgId(), user.getHostGroupIds(), policyId, pager)
.stream().map(historyEntry ->
new HistoryItem(historyEntry.getCtime(), historyEntry.getHostId(), historyEntry.getHostName())
).collect(Collectors.toList());
} else {
items = Collections.emptyList();
}
Page<HistoryItem> itemsPage = new Page<>(items, pager, totalCount);
return PagingUtils.responseBuilder(itemsPage);
}
private Response isNameUnique(Policy policy) {
Policy tmp = Policy.findByName(user.getOrgId(), policy.name);
if (tmp != null && !tmp.id.equals(policy.id)) {
return Response.status(409).entity(new Msg("Policy name is not unique")).build();
}
return null;
}
private Policy findPolicy(UUID policyId) {
return Policy.findById(user.getOrgId(), policyId);
}
private void setLatestToNow() {
orgIdLatestUpdateRepository.setLatestToNow(user.getOrgId());
}
}