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