1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2018 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 #pragma once
18 
19 #include <gsl.h>
20 #include <nlohmann/json.hpp>
21 #include <platform/dirutils.h>
22 #include <vector>
23 
24 class Event;
25 
26 /**
27  * The Module class represents the configuration for a single module.
28  * See ../README.md for more information.
29  */
30 class Module {
31 public:
32     Module() = delete;
33     Module(const nlohmann::json& json,
34            const std::string& srcRoot,
35            const std::string& objRoot);
36 
37     void createHeaderFile();
38 
39     /**
40      * The name of the module
41      */
42     std::string name;
43     /**
44      * The lowest identifier for the audit events in this module. All
45      * audit descriptor defined for this module MUST be within the range
46      * [start, start + max_events_per_module]
47      */
48     int64_t start;
49     /**
50      * The name of the file containing the audit descriptors for this
51      * module.
52      */
53     std::string file;
54     /**
55      * The JSON data describing the audit descriptors for this module
56      */
57     nlohmann::json json;
58     /**
59      * Is this module enterprise only?
60      */
61     bool enterprise = false;
62 
63     /**
64      * If present this is the name of a C headerfile to generate with
65      * #defines for all audit identifiers for the module.
66      */
67     std::string header;
68 
69     /**
70      * A list of all of the events defined for this module
71      */
72     std::vector<std::unique_ptr<Event>> events;
73 
74 protected:
75     /**
76      * Add the event to the list of events for the module
77      *
78      * @param event the event to add
79      * @throws std::invalid_argument if the event is outside the legal range
80      *                               for the module
81      */
82     void addEvent(std::unique_ptr<Event> event);
83 
84     /// Parse the event descriptor file and add all of the events into
85     /// the list of events
86     void parseEventDescriptorFile();
87 };
88