-
Notifications
You must be signed in to change notification settings - Fork 73
/
SystemVariableService.scala
374 lines (316 loc) · 16.7 KB
/
SystemVariableService.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
*************************************************************************************
* Copyright 2011 Normation SAS
*************************************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero GPL v3, 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 Affero GPL v3
* licence, 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.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/agpl.html>.
*
*************************************************************************************
*/
package com.normation.rudder.services.policies
import net.liftweb.common.EmptyBox
import com.normation.rudder.services.nodes.NodeInfoService
import com.normation.rudder.repository.LicenseRepository
import com.normation.cfclerk.domain._
import com.normation.rudder.domain.nodes.NodeInfo
import net.liftweb.common.Box
import com.normation.rudder.domain.nodes.NodeInfo
import com.normation.inventory.domain.NodeId
import com.normation.inventory.domain._
import net.liftweb.common._
import org.slf4j.{ Logger, LoggerFactory }
import com.normation.rudder.exceptions.LicenseException
import com.normation.cfclerk.services.SystemVariableSpecService
import com.normation.rudder.repository.FullActiveTechniqueCategory
import com.normation.rudder.repository.FullNodeGroupCategory
import com.normation.rudder.domain.policies.RuleId
import com.normation.rudder.domain.policies.Rule
import com.normation.rudder.domain.Constants
import com.normation.rudder.services.servers.PolicyServerManagementService
trait SystemVariableService {
def getGlobalSystemVariables(): Box[Map[String, Variable]]
def getSystemVariables(nodeInfo: NodeInfo, allNodeInfos: Map[NodeId, NodeInfo], globalSystemVariables: Map[String, Variable]): Box[Map[String, Variable]]
}
class SystemVariableServiceImpl(
licenseRepository: LicenseRepository
, systemVariableSpecService: SystemVariableSpecService
, policyServerManagementService: PolicyServerManagementService
// Variables definitions
, toolsFolder : String
, cmdbEndPoint : String
, communityPort : Int
, sharedFilesFolder : String
, webdavUser : String
, webdavPassword : String
, syslogPort : Int
, rudderServerRoleLdap : String
, rudderServerRoleInventoryEndpoint: String
, rudderServerRoleDb : String
, rudderServerRoleFront : String
, rudderServerRoleWebapp: String
//denybadclocks and skipIdentify are runtime properties
, getDenyBadClocks: () => Box[Boolean]
, getSkipIdentify : () => Box[Boolean]
// schedules are runtime dependencies also
, getAgentRunInterval : () => Int
, getAgentRunSplaytime : () => Box[Int]
, getAgentRunStartHour : () => Box[Int]
, getAgentRunStartMinute: () => Box[Int]
// TTLs are runtime properties too
, getModifiedFilesTtl : () => Box[Int]
, getCfengineOutputsTtl : () => Box[Int]
, getStoreAllCentralizedLogsInFile: () => Box[Boolean]
) extends SystemVariableService with Loggable {
val varToolsFolder = systemVariableSpecService.get("TOOLS_FOLDER").toVariable().copyWithSavedValue(toolsFolder)
val varCmdbEndpoint = systemVariableSpecService.get("CMDBENDPOINT").toVariable().copyWithSavedValue(cmdbEndPoint)
val varWebdavUser = systemVariableSpecService.get("DAVUSER").toVariable().copyWithSavedValue(webdavUser)
val varWebdavPassword = systemVariableSpecService.get("DAVPASSWORD").toVariable().copyWithSavedValue(webdavPassword)
val varSharedFilesFolder = systemVariableSpecService.get("SHARED_FILES_FOLDER").toVariable().copyWithSavedValue(sharedFilesFolder)
val varCommunityPort = systemVariableSpecService.get("COMMUNITYPORT").toVariable().copyWithSavedValue(communityPort.toString)
val syslogPortConfig = systemVariableSpecService.get("SYSLOGPORT").toVariable().copyWithSavedValue(syslogPort.toString)
// Compute the values for rudderServerRoleLdap, rudderServerRoleDb and rudderServerRoleFront
// if autodetect, then it is not defined, otherwise we parse it
val AUTODETECT_KEYWORD="autodetect"
def parseRoleContent(value: String) : Option[Iterable[String]] = {
value match {
case AUTODETECT_KEYWORD => None
case _ => Some(value.split(","))
}
}
lazy val definedRudderServerRoleLdap = parseRoleContent(rudderServerRoleLdap)
lazy val definedRudderServerRoleDb = parseRoleContent(rudderServerRoleDb)
lazy val definedRudderServerRoleFront = parseRoleContent(rudderServerRoleFront)
lazy val definedRudderServerRoleWebapp = parseRoleContent(rudderServerRoleWebapp)
lazy val definedRudderServerRoleInventoryEnpoint = parseRoleContent(rudderServerRoleInventoryEndpoint)
// compute all the global system variable (so that need to be computed only once in a deployment)
def getGlobalSystemVariables(): Box[Map[String, Variable]] = {
logger.trace("Preparing the global system variables")
val denyBadClocks = getProp("DENYBADCLOCKS", getDenyBadClocks)
val skipIdentify = getProp("SKIPIDENTIFY", getSkipIdentify)
val modifiedFilesTtl = getProp("MODIFIED_FILES_TTL", getModifiedFilesTtl)
val cfengineOutputsTtl = getProp("CFENGINE_OUTPUTS_TTL", getCfengineOutputsTtl)
val interval = getAgentRunInterval()
val varAgentRunInterval = systemVariableSpecService.get("AGENT_RUN_INTERVAL").toVariable().copyWithSavedValue(interval.toString)
val varAgentRunSplayTime = getProp("AGENT_RUN_SPLAYTIME", getAgentRunSplaytime)
val storeAllCentralizedLogsInFile = getProp("STORE_ALL_CENTRALIZED_LOGS_IN_FILE", getStoreAllCentralizedLogsInFile)
for {
agentRunStartHour <- getAgentRunStartHour() ?~! "Could not retrieve the configure value for the run start hour"
agentRunStartMinute <- getAgentRunStartMinute() ?~! "Could not retrieve the configure value for the run start minute"
agentRunSplaytime <- getAgentRunSplaytime() ?~! "Could not retrieve the configure value for the run splay time"
schedule <- ComputeSchedule.computeSchedule(agentRunStartHour, agentRunStartMinute, interval) ?~! "Could not compute the run schedule"
} yield {
val varAgentRunSchedule = systemVariableSpecService.get("AGENT_RUN_SCHEDULE").toVariable().copyWithSavedValue(schedule)
logger.trace("Global system variables done")
Map(
(varToolsFolder.spec.name, varToolsFolder)
, (varCmdbEndpoint.spec.name, varCmdbEndpoint)
, (varSharedFilesFolder.spec.name, varSharedFilesFolder)
, (varCommunityPort.spec.name, varCommunityPort)
, (varWebdavUser.spec.name, varWebdavUser)
, (varWebdavPassword.spec.name, varWebdavPassword)
, (syslogPortConfig.spec.name, syslogPortConfig)
, (denyBadClocks.spec.name, denyBadClocks)
, (skipIdentify.spec.name, skipIdentify)
, (varAgentRunInterval.spec.name, varAgentRunInterval)
, (varAgentRunSchedule.spec.name, varAgentRunSchedule)
, (varAgentRunSplayTime.spec.name, varAgentRunSplayTime)
, (modifiedFilesTtl.spec.name, modifiedFilesTtl)
, (cfengineOutputsTtl.spec.name, cfengineOutputsTtl)
, (storeAllCentralizedLogsInFile.spec.name, storeAllCentralizedLogsInFile)
)
}
}
// allNodeInfos has to contain ALL the node info (those of every node within Rudder)
// for this method to work properly
// The global system variables are computed before (in the method up there), and
// can be overriden by some node specific parameters (especially, the schedule for
// policy servers)
def getSystemVariables(
nodeInfo : NodeInfo
, allNodeInfos : Map[NodeId, NodeInfo]
, globalSystemVariables: Map[String, Variable]
): Box[Map[String, Variable]] = {
logger.trace("Preparing the system variables for node %s".format(nodeInfo.id.value))
// Set the roles of the nodes
val nodeConfigurationRoles = collection.mutable.Set[ServerRole]() ++ nodeInfo.serverRoles
// Define the mapping of roles/hostnames, only if the node has a role
val varRoleMappingValue = if (nodeConfigurationRoles.size > 0) {
val allNodeInfosSet = allNodeInfos.values.toSet
val nodesWithRoleLdap = definedRudderServerRoleLdap match {
case Some(seq) => seq
case None => getNodesWithRole(allNodeInfosSet, ServerRole("rudder-ldap"))
}
val nodesWithRoleInventoryEndpoint = definedRudderServerRoleInventoryEnpoint match {
case Some(seq) => seq
case None => getNodesWithRole(allNodeInfosSet, ServerRole("rudder-inventory-endpoint"))
}
val nodesWithRoleDb = definedRudderServerRoleDb match {
case Some(seq) => seq
case None => getNodesWithRole(allNodeInfosSet, ServerRole("rudder-db"))
}
val nodesWithRoleFront = definedRudderServerRoleFront match {
case Some(seq) => seq
case None => getNodesWithRole(allNodeInfosSet, ServerRole("rudder-front"))
}
val nodesWithRoleWebapp = definedRudderServerRoleWebapp match {
case Some(seq) => seq
case None => getNodesWithRole(allNodeInfosSet, ServerRole("rudder-webapp"))
}
writeNodesWithRole(nodesWithRoleLdap, "rudder-ldap") +
writeNodesWithRole(nodesWithRoleInventoryEndpoint, "rudder-inventory-endpoint") +
writeNodesWithRole(nodesWithRoleDb, "rudder-db") +
writeNodesWithRole(nodesWithRoleFront, "rudder-front") +
writeNodesWithRole(nodesWithRoleWebapp, "rudder-webapp")
} else {
""
}
val varRudderServerRole = systemVariableSpecService.get("RUDDER_SERVER_ROLES").toVariable().copyWithSavedValue(varRoleMappingValue)
if (nodeInfo.isPolicyServer) {
nodeConfigurationRoles.add(ServerRole("policy_server"))
if (nodeInfo.id == nodeInfo.policyServerId) {
nodeConfigurationRoles.add(ServerRole("root_server"))
}
}
val varNodeRoleValue = if (nodeConfigurationRoles.size > 0) {
" classes: \n" + nodeConfigurationRoles.map(x => " \"" + x.value + "\" expression => \"any\";").mkString("\n")
} else {
"# This node doesn't have any specific role"
}
val varNodeRole = systemVariableSpecService.get("NODEROLE").toVariable().copyWithSavedValue(varNodeRoleValue)
// Check that the policy server have Nova license if the host is a Nova
if (nodeInfo.agentsName.contains(NOVA_AGENT) && licenseRepository.findLicense(nodeInfo.policyServerId).isEmpty) {
logger.warn("Caution, the policy server %s does not have a registered Nova license".format(nodeInfo.policyServerId.value))
throw new LicenseException("No license found for the policy server " + nodeInfo.policyServerId.value)
}
val authorizedNetworks = policyServerManagementService.getAuthorizedNetworks(nodeInfo.id) match {
case eb:EmptyBox =>
//log ?
Seq()
case Full(nets) => nets
}
val varAllowedNetworks = systemVariableSpecService.get("AUTHORIZED_NETWORKS").toVariable(authorizedNetworks)
// If we are facing a policy server, we have to allow each child to connect, plus the policy parent,
// else it's only the policy server
val policyServerVars = if (nodeInfo.isPolicyServer) {
// Find the "policy children" of this policy server
// thanks to the allNodeInfos, this is super easy
//IT IS VERY IMPORTANT TO SORT SYSTEM VARIABLE HERE: see ticket #4859
val children = allNodeInfos.filter{ case(k,v) => v.policyServerId == nodeInfo.id }.values.toSeq.sortBy( _.id.value )
val varManagedNodes = systemVariableSpecService.get("MANAGED_NODES_NAME").toVariable(children.map(_.hostname))
val varManagedNodesId = systemVariableSpecService.get("MANAGED_NODES_ID").toVariable(children.map(_.id.value))
//IT IS VERY IMPORTANT TO SORT SYSTEM VARIABLE HERE: see ticket #4859
val varManagedNodesAdmin = systemVariableSpecService.get("MANAGED_NODES_ADMIN").toVariable(children.map(_.localAdministratorAccountName).distinct.sorted)
// the schedule must be the default one for policy server
val DEFAULT_SCHEDULE =
"""
"Min00", "Min05", "Min10", "Min15", "Min20", "Min25", "Min30", "Min35", "Min40", "Min45", "Min50", "Min55"
"""
val varAgentRunInterval = systemVariableSpecService.get("AGENT_RUN_INTERVAL").toVariable().copyWithSavedValue("5")
val varAgentRunSplayTime = systemVariableSpecService.get("AGENT_RUN_SPLAYTIME").toVariable().copyWithSavedValue("5")
val varAgentRunSchedule = systemVariableSpecService.get("AGENT_RUN_SCHEDULE").toVariable().copyWithSavedValue(DEFAULT_SCHEDULE)
Map(
varManagedNodes.spec.name -> varManagedNodes
, varManagedNodesId.spec.name -> varManagedNodesId
, varManagedNodesAdmin.spec.name -> varManagedNodesAdmin
, varAgentRunInterval.spec.name -> varAgentRunInterval
, varAgentRunSchedule.spec.name -> varAgentRunSchedule
, varAgentRunSplayTime.spec.name -> varAgentRunSplayTime
)
} else {
Map()
}
logger.trace("System variables for node %s done".format(nodeInfo.id.value))
Full(
globalSystemVariables
++ Map(
(varNodeRole.spec.name, varNodeRole)
, (varAllowedNetworks.spec.name, varAllowedNetworks)
, (varRudderServerRole.spec.name -> varRudderServerRole)
)
++ policyServerVars
)
}
// Fetch the Set of node hostnames having specific role
private[this] def getNodesWithRole(
allNodeInfos : Set[NodeInfo]
, role : ServerRole
) : Set[String] = {
allNodeInfos.filter(x => x.serverRoles.contains(role)).map(_.hostname)
}
// Formating of the roles
private[this] def writeNodesWithRole(
nodesWithRole: Iterable[String]
, roleName : String
) : String = {
nodesWithRole.size match {
case 0 => "" // no string, no role
case _ => s"${roleName}:${nodesWithRole.mkString(",")}\n"
}
}
//obtaining variable values from (failable) properties
private[this] def getProp[T](specName: String, getter: () => Box[T]): SystemVariable = {
//try to get the user configured value, else log an error and use the default value.
val variable = systemVariableSpecService.get(specName).toVariable()
getter() match {
case Full(value) =>
variable.copyWithSavedValue(value.toString)
case eb: EmptyBox =>
val e = eb ?~! s"Error when trying to get the value configured by the user for system variable '${specName}'"
logger.error(e.messageChain)
e.rootExceptionCause.foreach { ex =>
logger.error("Root exception cause was:", ex)
}
variable
}
}
}
object ComputeSchedule {
def computeSchedule(
startHour : Int
, startMinute : Int
, executionInterval: Int
): Box[String] = {
val minutesFreq = executionInterval % 60
val hoursFreq: Int = executionInterval / 60
(minutesFreq, hoursFreq) match {
case (m, h) if m > 0 && h > 0 => Failure(s"Agent execution interval can only be defined as minutes (less than 60) or complete hours, (${h} hours ${m} minutes is not supported)")
case (m, h) if h == 0 =>
// two cases, hour is 0, then only minutes
// let's modulate startMinutes by minutes
val actualStartMinute = startMinute % minutesFreq
val mins = Range(actualStartMinute, 60, minutesFreq) // range doesn't return the end range
//val mins = for ( min <- 0 to 59; if ((min%minutesFreq) == actualStartMinute) ) yield { min }
Full(mins.map("\"Min" + "%02d".format(_) + "\"").mkString(", "))
case _ =>
// hour is not 0, then we don't have minutes
val actualStartHour = startHour % hoursFreq
val hours = Range(actualStartHour, 24, hoursFreq)
val minutesFormat = "Min" + "%02d".format(startMinute)
Full(hours.map("\"Hr" + "%02d".format(_) + "." + minutesFormat + "\"").mkString(", "))
}
}
}