aboutsummaryrefslogtreecommitdiffstats
path: root/ui/imports/api/scans/scans.js
blob: 857c2ea9601a55dc7debe72abdc4a49fe6348acf (plain)
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
/////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) and others /
//                                                                                      /
// All rights reserved. This program and the accompanying materials                     /
// are made available under the terms of the Apache License, Version 2.0                /
// which accompanies this distribution, and is available at                             /
// http://www.apache.org/licenses/LICENSE-2.0                                           /
/////////////////////////////////////////////////////////////////////////////////////////
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import * as R from 'ramda';

import { Constants } from '/imports/api/constants/constants';
import { StatusesInOperation } from '/imports/api/constants/data/scans-statuses';

export const Scans = new Mongo.Collection('scans', { idGeneration: 'MONGO' });

Scans.schemaRelated = {
  environment: {
    label: 'Environment',
    description: 'Name of environment to scan',
    disabled: true,
  },
  status: {
    label: 'Status',
    description: 'Scan lifecycle status',
    subtype: 'select',
    options: 'scans_statuses',
    disabled: true,
  },
  object_id: {
    label: 'Scan specific object',
    description: 'Object ID',
  },
  log_level: {
    label: 'Log level',
    description: 'logging level',
    subtype: 'select',
    options: 'log_levels',
  },
  clear: {
    label: 'Clear data',
    description: 'clear all data prior to scanning',
  },
  scan_only_inventory: {
    label: 'Scan only inventory',
    description: 'do only scan to inventory',
  },
  scan_only_links: {
    label: 'Scan only links',
    description: 'do only links creation',
  },
  scan_only_cliques: {
    label: 'Scan only cliques',
    description: 'do only cliques creation',
  },
};

Scans.scansOnlyFields = ['scan_only_inventory', 'scan_only_links', 'scan_only_cliques'];

let schema = {
  _id: { type: { _str: { type: String, regEx: SimpleSchema.RegEx.Id } } },
  environment: { 
    type: String 
  }, 
  status: {
    type: String, 
    defaultValue: 'draft', 
    custom: function () {
      let that = this;
      let statuses = Constants.findOne({ name: 'scans_statuses' }).data;

      if (R.isNil(R.find(R.propEq('value', that.value), statuses))) {
        return 'notAllowed';
      }
    },
  },
  object_id: {
    type: String,
    optional: true,
  },
  log_level: {
    type: String,
    defaultValue: 'warning', 
    custom: function () {
      let that = this;
      let logLevels = Constants.findOne({ name: 'log_levels' }).data;

      if (R.isNil(R.find(R.propEq('value', that.value), logLevels))) {
        return 'notAllowed';
      }
    },
  },
  clear: {
    type: Boolean,
    defaultValue: true, 
  },
  scan_only_inventory: {
    type: Boolean,
    defaultValue: false,
  },
  scan_only_links: {
    type: Boolean,
    defaultValue: false,
  },
  scan_only_cliques: {
    type: Boolean,
    defaultValue: false,
  },
  submit_timestamp: {
    type: Date,
    defaultValue: null
  },

};

Scans.schema = new SimpleSchema(schema);
Scans.schema.addValidator(function () {
  let that = this;
  let env = that.field('environment').value;

  let currentScansCount = Scans.find({
    environment: env,
    status: { $in: StatusesInOperation }
  }).count();

  if (currentScansCount > 0) {
    throw {
      isError: true,
      type: 'notUinque',
      data: [],
      message: 'There is already a scan in progress.'
    };
  }

  let scanOnlyFields = R.filter( f => that.field(f).value, Scans.scansOnlyFields);

  if(scanOnlyFields.length > 1) {
    throw {
      isError: true,
      type: 'conflict',
      data: scanOnlyFields,
      message: 'Only one of the scan only fields can be selected'
    };
  }

});

Scans.attachSchema(Scans.schema);

Scans.schemaRelated = R.mapObjIndexed((relatedItem, key) => {
  return R.merge(relatedItem, {
    type: schema[key].type 
  });

}, Scans.schemaRelated);

export const subsScansEnvPageAmountSorted = 'scans?env*&page&amount&sortField&sortDirection';
export const subsScansEnvPageAmountSortedCounter = `${subsScansEnvPageAmountSorted}!counter`;