1 | import { DeviceCheck, DeviceCheckResult } from "./deviceTrust";
|
2 | import { CheckResultMetrics, SecurityCheckResultMetric } from "./metrics";
|
3 | import { MetricsService } from "@aerogear/core";
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export class SecurityService {
|
13 | private static readonly METRICS_KEY = "security";
|
14 |
|
15 | constructor(private metrics?: MetricsService) {
|
16 | }
|
17 |
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 | public check(check: DeviceCheck): Promise<DeviceCheckResult> {
|
24 | return check.check();
|
25 | }
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 | public checkAndPublishMetric(check: DeviceCheck): Promise<SecurityCheckResultMetric> {
|
33 | return this.check(check)
|
34 | .then(checkResult => this.publishCheckResultMetrics(checkResult))
|
35 | .then(checkMetrics => checkMetrics[0]);
|
36 | }
|
37 |
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 | public checkMany(...checks: DeviceCheck[]): Promise<DeviceCheckResult[]> {
|
44 | return Promise.all(checks.map(check => check.check()));
|
45 | }
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 | public checkManyAndPublishMetric(...checks: DeviceCheck[]): Promise<SecurityCheckResultMetric[]> {
|
53 | return this.checkMany(...checks)
|
54 | .then(checkResults => this.publishCheckResultMetrics(...checkResults));
|
55 | }
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | private publishCheckResultMetrics(...results: DeviceCheckResult[]): Promise<SecurityCheckResultMetric[]> {
|
65 | if (!results || results.length === 0) {
|
66 | return Promise.resolve([]);
|
67 | }
|
68 |
|
69 | const checkResultMetrics = new CheckResultMetrics(results);
|
70 | if (!this.metrics) {
|
71 | return Promise.reject(new Error("Metrics configuration is not available."));
|
72 | }
|
73 |
|
74 | return this.metrics.publish(SecurityService.METRICS_KEY, [checkResultMetrics])
|
75 | .then(() => checkResultMetrics.collect());
|
76 | }
|
77 | }
|