-
Notifications
You must be signed in to change notification settings - Fork 73
/
Directive.scala
263 lines (236 loc) · 9.35 KB
/
Directive.scala
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
/*
*************************************************************************************
* Copyright 2011 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************************
*/
package com.normation.rudder.domain.policies
import scala.collection.mutable.Buffer
import scala.xml._
import com.normation.utils.Utils.nonEmpty
import com.normation.cfclerk.domain.TechniqueVersion
import com.normation.utils.HashcodeCaching
import com.normation.cfclerk.domain.SectionSpec
import com.normation.cfclerk.domain.Technique
import com.normation.rudder.domain.policies.PolicyMode
case class DirectiveId(value : String) extends HashcodeCaching
/**
* Define a directive.
*
* From a business point of view, a directive is a general
* policy about your infrastructure, like "our password must be
* 10 chars, mixed symbol, case, number".
*
* In Rudder, a Directive is derived from a technique on which
* we are going to bind parameter to values matching the business
* directive. For example, in our example, it could be
* "Unix Password management with passwd"
*
* A directive also keep other information, like the priority
* of that directive compared to other directive derived from the
* the same technique.
*
*/
//TODO: why not keeping techniqueName here ? data duplication ?
case class Directive(
id : DirectiveId
/**
* They reference one and only one Technique version
*/
, techniqueVersion : TechniqueVersion
/**
* The list or parameters with their values.
* TODO: I really would like to be able to not allow to set bad parameter here,
* what mean parameter that are not in the technique.
* For now, say it's done by construction.
*/
, parameters : Map[String, Seq[String]]
/**
* A human readable name for that directive,
* typically used for CSV/grid header
* i.e: "SEC-042 Debian Etch"
* Can not be empty nor null.
*/
, name : String
/**
* Short description, typically used as field description
* Can not be empty nor null.
*/
, shortDescription : String
/**
* Policy mode defined for that Directive
* Three possibles values for now:
* None => Default (use global mode)
* Some => Verify or Enforce
*/
, policyMode : Option[PolicyMode]
/**
* A long, detailed description, typically used for
* tooltip. It allows reach content.
* Can be empty (and is by default).
*/
, longDescription : String = ""
/**
* For policies which allows only one configured instance at
* a given time for a given node, priority allows to choose
* the policy to deploy.
* Higher priority is better, default is 5
*/
, priority : Int = 5
/**
* Define if the policy is activated.
* If it is not, configuration based on that policy should not be considered
* for deployment on nodes.
*/
, _isEnabled : Boolean = false
, isSystem : Boolean = false
) extends HashcodeCaching {
//system object must ALWAYS be ENABLED.
def isEnabled = _isEnabled || isSystem
}
final case class SectionVal(
sections : Map[String, Seq[SectionVal]] = Map() //name -> values
, variables : Map[String, String] = Map() //name -> values
) extends HashcodeCaching
object SectionVal {
val ROOT_SECTION_NAME = "sections"
def toXml(sv : SectionVal, sectionName : String = ROOT_SECTION_NAME): Node = {
<section name={sectionName}>
{ //variables
sv.variables.toSeq.sortBy(_._1).map { case (variable,value) =>
<var name={variable}>{value}</var>
} ++
//section
(for {
(sectionName, sectionIterations) <- sv.sections.toSeq.sortBy(_._1)
sectionValue <- sectionIterations
} yield {
this.toXml(sectionValue,sectionName)
})
}
</section>
}
def directiveValToSectionVal(rootSection : SectionSpec, allValues : Map[String,Seq[String]]) : SectionVal = {
/*
* build variables with a parent section multivalued.
*/
def buildMonoSectionWithMultivaluedParent(spec : SectionSpec, index : Int) : SectionVal = {
if(spec.isMultivalued) throw new RuntimeException("We found a multivalued subsection of a multivalued section: " + spec)
//variable for that section: Map[String, String]
val variables = spec.getDirectVariables.map { vspec =>
(vspec.name, allValues(vspec.name)(index))
}.toMap
/*
* Get subsection. We can have several, all mono-valued
*/
val subsections = spec.getDirectSections.map { sspec =>
(sspec.name, Seq(buildMonoSectionWithMultivaluedParent(sspec,index)))
}.toMap
SectionVal(subsections, variables)
}
def buildMultiSectionWithoutMultiParent(spec : SectionSpec) : Seq[SectionVal] = {
if(!spec.isMultivalued) throw new RuntimeException("We found a monovalued section where a multivalued section was asked for: " + spec)
// find the number of iteration for that multivalued section.
// try with a direct variable, and if the section has no direct variable, with the first direct section with a variable
val cardinal = {
val name = spec.getDirectVariables.toList match {
case v :: tail => v.name
case _ => //look for the first section with a var
spec.getDirectSections.find { s => s.getDirectVariables.nonEmpty }.map { s =>
s.getDirectVariables.head.name
}.getOrElse("NO VARIABLE !!!") //used name should not be a key
}
allValues.get(name).map( _.size ).getOrElse(0)
}
//find variable of that section
val multiVariables : Seq[Map[String,String]] = {
for {
i <- 0 until cardinal
} yield {
spec.getDirectVariables.map { vspec =>
// Default value for our variable, will be use is there is no value for this variable, empty string if no default value
val defaultValue = vspec.constraint.default.getOrElse("")
// get Value for our variable in our for the current section (i), use default value if missing
val value = allValues.get(vspec.name).map(_(i)).getOrElse(defaultValue)
(vspec.name, value)
}.toMap
}
}
//build subsections:
val multiSections : Seq[Map[String, SectionVal]] = {
for {
i <- 0 until cardinal
} yield {
spec.getDirectSections.map { sspec =>
( sspec.name, buildMonoSectionWithMultivaluedParent(sspec, i) )
}.toMap
}
}
for {
i <- 0 until cardinal
} yield {
//here, children section must be with a cardinal of 1 (monovalued)
val sections = multiSections(i).map { case(k,s) => (k,Seq(s)) }.toMap
SectionVal(sections, multiVariables(i))
}
}
def buildMonoSectionWithoutMultivaluedParent(spec : SectionSpec) : SectionVal = {
val variables = spec.getDirectVariables.map { vspec =>
//we can have a empty value for a variable, for non mandatory ones
(vspec.name, allValues.getOrElse(vspec.name,Seq(""))(0))
}.toMap
val sections = spec.getDirectSections.map { vspec =>
if(vspec.isMultivalued) {
(vspec.name, buildMultiSectionWithoutMultiParent(vspec))
} else {
(vspec.name, Seq(buildMonoSectionWithoutMultivaluedParent(vspec)))
}
}.toMap
SectionVal(sections,variables)
}
buildMonoSectionWithoutMultivaluedParent(rootSection)
}
def toMapVariables(sv : SectionVal) : Map[String,Seq[String]] = {
import scala.collection.mutable.{Map, Buffer}
val res = Map[String, Buffer[String]]()
def recToMap(sec : SectionVal) : Unit = {
sec.variables.foreach { case (name,value) =>
res.getOrElseUpdate(name, Buffer()).append(value)
}
sec.sections.foreach { case (_, sections) =>
sections.foreach { recToMap( _ ) }
}
}
recToMap(sv)
res.map { case (k,buf) => (k,buf.toSeq) }.toMap
}
}