This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
RudderParsingRules.scala
239 lines (213 loc) · 8.08 KB
/
RudderParsingRules.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
/*
*************************************************************************************
* 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.inventory.provisioning.fusion
import scala.xml._
import com.normation.inventory.domain._
import net.liftweb.common._
import java.security.MessageDigest
import com.normation.utils.UuidRegex
///////////// <CONTENT> /////////////
/**
* <USERSLIST>
*/
object RudderUserListParsing extends FusionReportParsingExtension {
def processUserList(xml:Node) : Seq[String] = {
(xml \ "USER").flatMap(e => optText(e))
}
override def isDefinedAt(x:(Node,InventoryReport)) = x._1.label == "USERSLIST"
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
x._2.copy( node = x._2.node.copy( accounts = x._2.node.accounts ++ processUserList(x._1) ) )
}
}
/**
* <VMS>
*/
object RudderVMsParsing extends FusionReportParsingExtension {
override def isDefinedAt(x:(Node,InventoryReport)) = x._1.label == "VMS"
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
//TODO
x._2
}
}
///////////// ROOT+1 /////////////
/**
* <UUID>
*/
object RudderNodeIdParsing extends FusionReportParsingExtension {
override def isDefinedAt(x:(Node,InventoryReport)) = { x._1.label == "UUID" }
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
optText(x._1) match {
case None => x._2
case Some(id) => x._2.copy( node = x._2.node.copyWithMain { m => m.copy(id = NodeId(id)) } )
}
}
}
/**
* <POLICY_SERVER>
*/
object RudderPolicyServerParsing extends FusionReportParsingExtension {
override def isDefinedAt(x:(Node,InventoryReport)) = { x._1.label == "POLICY_SERVER" }
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
optText(x._1) match {
case None => x._2
case Some(ps) => x._2.copy( node = x._2.node.copyWithMain { m => m.copy(policyServerId = NodeId(ps)) } )
}
}
}
/**
* <MACHINEID>
*
* Must be executed after <UUID> parsing rule.
*
* If <MACHINEID> is not present or its content is not a valid UUID,
* define the machine ID based on the md5 of node ID.
*/
object RudderMachineIdParsing extends FusionReportParsingExtension {
private[this] def buildMachineId(report:InventoryReport) = {
val md5 = MessageDigest.getInstance("MD5").digest(report.node.main.id.value.getBytes)
val id = (md5.map(0xFF & _).map { "%02x".format(_) }.foldLeft(""){_ + _}).toLowerCase
"%s-%s-%s-%s-%s".format(
id.substring(0,8)
, id.substring(8,12)
, id.substring(12,16)
, id.substring(16,20)
, id.substring(20)
)
}
override def isDefinedAt(x:(Node,InventoryReport)) = { x._1.label == "MACHINEID" }
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
val machineId = (optText(x._1) match {
case None => buildMachineId(x._2)
case Some(id) => if(UuidRegex.isValid(id)) id else buildMachineId(x._2)
})
x._2.copy( machine = x._2.machine.copy( mbUuid = Some(MotherBoardUuid(machineId) ) ) )
}
}
/**
* <PROCESSORS>
*/
object RudderCpuParsing extends FusionReportParsingExtension with Loggable {
override def isDefinedAt(x:(Node,InventoryReport)) = { x._1.label == "PROCESSORS" }
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
x._2.copy( machine = x._2.machine.copy( processors = x._2.machine.processors ++ processProcessors(x._1) ) )
}
def processProcessors(xml:NodeSeq) : Seq[Processor] = {
val buf = scala.collection.mutable.Buffer[Processor]()
(xml \ "PROCESSOR").zipWithIndex.map { case (p,i) =>
optText(p\"NAME").map { x => x + " num " + i} match {
case None =>
logger.debug("Ignoring Processor entry because NAME tag is empty")
logger.debug(p)
case Some(name) =>
val cpu = Processor(
name = name
, speed = optText(p\"FREQUENCY").map(_.toFloat)
, model = optText(p\"MODEL")
, family = optText(p\"FAMILY")
, manufacturer = optText(p\"VENDOR") map { Manufacturer(_) }
, stepping = optText(p\"STEPPING") map { _.toInt }
)
buf += cpu
}
}
buf.toSeq
}
}
/**
* <CFKEY>
*/
class RudderPublicKeyParsing(keyNormalizer:PrintedKeyNormalizer) extends FusionReportParsingExtension {
override def isDefinedAt(x:(Node,InventoryReport)) = { x._1.label == "CFKEY" }
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
optText(x._1) match {
case None => x._2
case Some(key) => x._2.copy( node = x._2.node.copy( publicKeys = (new PublicKey(keyNormalizer(key) ) +: x._2.node.publicKeys ) ) )
}
}
}
/**
* <USER>
*/
object RudderRootUserParsing extends FusionReportParsingExtension {
override def isDefinedAt(x:(Node,InventoryReport)) = { x._1.label == "USER" }
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
optText(x._1) match {
case None => x._2
case Some(u) => x._2.copy( node = x._2.node.copyWithMain{ m => m.copy( rootUser = u ) } )
}
}
}
/**
* <AGENTSNAME>
*/
object RudderAgentNameParsing extends FusionReportParsingExtension with Loggable {
override def isDefinedAt(x:(Node,InventoryReport)) = { x._1.label == "AGENTSNAME" }
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
x._2.copy( node = x._2.node.copy( agentNames = x._2.node.agentNames ++ processAgentName(x._1) ) )
}
def processAgentName(xml:NodeSeq) : Seq[AgentType] = {
(xml \ "AGENTNAME").flatMap(e => optText(e).flatMap( a =>
AgentType.fromValue(a) match {
case Full(x) => Full(x)
case e:EmptyBox =>
logger.error("Ignore agent type '%s': unknown value. Authorized values are %s".format(a, AgentType.allValues.mkString(", ")))
Empty
}
) )
}
}
/**
* <TECHNIQUES>
*/
object RudderTechniquesParsing extends FusionReportParsingExtension {
override def isDefinedAt(x:(Node,InventoryReport)) = { x._1.label == "TECHNIQUES" }
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
x._2.copy( node = x._2.node.copy( techniques = x._2.node.techniques ++ processTechniques(x._1) ) )
}
def processTechniques(xml:NodeSeq) : Seq[String] = {
(xml \ "TECHNIQUE").flatMap(e => optText(e))
}
}
/**
* <HOSTNAME>
*/
object RudderHostnameParsing extends FusionReportParsingExtension {
override def isDefinedAt(x:(Node,InventoryReport)) = { x._1.label == "HOSTNAME" }
override def apply(x:(Node,InventoryReport)) : InventoryReport = {
optText(x._1) match {
case None => x._2
case Some(e) => x._2.copy( node = x._2.node.copyWithMain { m => m.copy( hostname = e) } )
}
}
}