Skip to content

Commit

Permalink
Merge branch 'arch_21995/auto_format_scala_code_7_2_pr' into branches…
Browse files Browse the repository at this point in the history
…/rudder/7.2
  • Loading branch information
fanf committed Oct 20, 2022
2 parents 73425a1 + 6347fd6 commit 7b4eae3
Show file tree
Hide file tree
Showing 650 changed files with 97,381 additions and 81,052 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
/*
*************************************************************************************
* 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/>.
*
*************************************************************************************
*/
*************************************************************************************
* 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.inventory.domain
*
*************************************************************************************
*/

import org.bouncycastle.openssl.PEMParser
import java.io.StringReader
package com.normation.inventory.domain

import com.normation.NamedZioLogger
import com.normation.errors._
import com.normation.inventory.domain.InventoryError.CryptoEx
import com.normation.inventory.services.provisioning.ParsedSecurityToken
import java.io.StringReader
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter
import org.bouncycastle.cert.X509CertificateHolder
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter
import org.bouncycastle.openssl.PEMParser
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter
import zio._
import zio.syntax._

Expand All @@ -60,19 +59,19 @@ import zio.syntax._
* A simple class to denote a manufacturer
* TODO : Should be merge with SoftwareEditor
*/
final case class Manufacturer(name:String) extends AnyVal
final case class Manufacturer(name: String) extends AnyVal

/**
* A simple class to denote a software editor
*/
final case class SoftwareEditor(val name:String) extends AnyVal
final case class SoftwareEditor(val name: String) extends AnyVal

sealed trait SecurityToken {
def key : String
def key: String
}

case object SecurityToken {
def kind(token : SecurityToken) = {
def kind(token: SecurityToken) = {
token match {
case _: PublicKey => PublicKey.kind
case _: Certificate => Certificate.kind
Expand All @@ -82,27 +81,34 @@ case object SecurityToken {
def parseCertificate(cert: Certificate): IO[InventoryError, (java.security.PublicKey, List[(String, String)])] = {
cert.cert.flatMap { ch =>
IO.effect {
val c = new JcaX509CertificateConverter().getCertificate( ch )
val dn = ch.getSubject.getRDNs.flatMap(_.getTypesAndValues.flatMap(tv => (tv.getType.toString, tv.getValue.toString) :: Nil)).toList
val c = new JcaX509CertificateConverter().getCertificate(ch)
val dn = ch.getSubject.getRDNs
.flatMap(_.getTypesAndValues.flatMap(tv => (tv.getType.toString, tv.getValue.toString) :: Nil))
.toList
(c.getPublicKey, dn)
}.mapError { ex =>
CryptoEx(s"Error when trying to parse agent certificate information", ex)
}
}.mapError(ex => CryptoEx(s"Error when trying to parse agent certificate information", ex))
}
}

def checkCertificateSubject(nodeId: NodeId, subject: List[(String, String)]): IO[InventoryError, Unit] = {
//format subject
// format subject
def formatSubject(list: List[(String, String)]) = list.map(kv => s"${kv._1}=${kv._2}").mkString(",")

// in rudder, we ensure that at list one (k,v) pair is "UID = the node id". If missing, it's an error
subject.find { case (k,v) => k == ParsedSecurityToken.nodeidOID } match {
case None => InventoryError.SecurityToken(s"Certificate subject doesn't contain node ID in 'UID' attribute: ${formatSubject(subject)}").fail
case Some((k,v)) =>
if(v.trim.equalsIgnoreCase(nodeId.value)) {
subject.find { case (k, v) => k == ParsedSecurityToken.nodeidOID } match {
case None =>
InventoryError
.SecurityToken(s"Certificate subject doesn't contain node ID in 'UID' attribute: ${formatSubject(subject)}")
.fail
case Some((k, v)) =>
if (v.trim.equalsIgnoreCase(nodeId.value)) {
UIO.unit
} else {
InventoryError.SecurityToken(s"Certificate subject doesn't contain same node ID in 'UID' attribute as inventory node ID: ${formatSubject(subject)}").fail
InventoryError
.SecurityToken(
s"Certificate subject doesn't contain same node ID in 'UID' attribute as inventory node ID: ${formatSubject(subject)}"
)
.fail
}
}
}
Expand All @@ -115,66 +121,63 @@ case object SecurityToken {
}
}

object PublicKey {
object PublicKey {
val kind = "publicKey"
}
object Certificate {
val kind = "certificate"
}

/**
* A simple class to denote a software cryptographic public key
*/
final case class PublicKey(value : String) extends SecurityToken {
final case class PublicKey(value: String) extends SecurityToken {

// Value of the key may be stored (with old fusion inventory version) as one line and without rsa header and footer, we should add them if missing and format the key
val key = {
if (value.startsWith("-----BEGIN RSA PUBLIC KEY-----")) {
value
} else {
s"""-----BEGIN RSA PUBLIC KEY-----
|${value.grouped(80).mkString("\n")}
|-----END RSA PUBLIC KEY-----""".stripMargin
|${value.grouped(80).mkString("\n")}
|-----END RSA PUBLIC KEY-----""".stripMargin
}
}
def publicKey : IOResult[java.security.PublicKey] = {
def publicKey: IOResult[java.security.PublicKey] = {
IO.effect {
(new PEMParser(new StringReader(key))).readObject()
}.mapError { ex =>
InventoryError.CryptoEx(s"Key '${key}' cannot be parsed as a public key", ex)
}.flatMap { obj =>
}.mapError(ex => InventoryError.CryptoEx(s"Key '${key}' cannot be parsed as a public key", ex)).flatMap { obj =>
obj match {
case a : SubjectPublicKeyInfo =>
case a: SubjectPublicKeyInfo =>
(new JcaPEMKeyConverter().getPublicKey(a)).succeed
case _ => InventoryError.Crypto(s"Key '${key}' cannot be parsed as a public key").fail
}
}
}
}

final case class Certificate(value : String) extends SecurityToken {
final case class Certificate(value: String) extends SecurityToken {

// Value of the key may be stored (with old fusion inventory version) as one line and without rsa header and footer, we should add them if missing and format the key
val key = {
if (value.startsWith("-----BEGIN CERTIFICATE-----")) {
value
} else {
s"""-----BEGIN CERTIFICATE-----
|${value.grouped(80).mkString("\n")}
|-----END CERTIFICATE-----""".stripMargin
|${value.grouped(80).mkString("\n")}
|-----END CERTIFICATE-----""".stripMargin
}
}
def cert : IO[InventoryError, X509CertificateHolder] = {
def cert: IO[InventoryError, X509CertificateHolder] = {
for {
reader <- IO.effect {
new PEMParser(new StringReader(key))
} mapError { e =>
InventoryError.CryptoEx(s"Key '${key}' cannot be parsed as a valid certificate", e)
}
} mapError { e => InventoryError.CryptoEx(s"Key '${key}' cannot be parsed as a valid certificate", e) }
obj <- IO.effect(reader.readObject()).mapError { e =>
InventoryError.CryptoEx(s"Key '${key}' cannot be parsed as a valid certificate", e)
}
res <- obj match {
case a : X509CertificateHolder =>
case a: X509CertificateHolder =>
a.succeed
case _ => InventoryError.Crypto(s"Key '${key}' cannot be parsed as a valid certificate").fail
}
Expand All @@ -192,35 +195,31 @@ final case class Certificate(value : String) extends SecurityToken {
*
* Comparison are really important in Version
*/
final class Version(val value:String) extends AnyVal with Comparable[Version] {
override def compareTo(other:Version) = this.value.compareTo(other.value)
override def toString() = "[%s]".format(value)
final class Version(val value: String) extends AnyVal with Comparable[Version] {
override def compareTo(other: Version) = this.value.compareTo(other.value)
override def toString() = "[%s]".format(value)
}


object InventoryProcessingLogger extends NamedZioLogger {
override def loggerName: String = "inventory-processing"
object timing extends NamedZioLogger(){ def loggerName = "inventory-processing.timing"}
object timing extends NamedZioLogger() { def loggerName = "inventory-processing.timing" }
}

object InventoryDataLogger extends NamedZioLogger {
override def loggerName: String = "inventory-data"
}


sealed trait InventoryError extends RudderError

object InventoryError {

final case class Crypto(msg: String) extends InventoryError
final case class CryptoEx(hint: String, ex: Throwable) extends InventoryError {
final case class Crypto(msg: String) extends InventoryError
final case class CryptoEx(hint: String, ex: Throwable) extends InventoryError {
def msg = hint + "; root exception was: " + ex.getMessage()
}
final case class AgentType(msg: String) extends InventoryError
final case class SecurityToken(msg: String) extends InventoryError
final case class AgentType(msg: String) extends InventoryError
final case class SecurityToken(msg: String) extends InventoryError
final case class Deserialisation(msg: String, ex: Throwable) extends InventoryError
final case class Inconsistency(msg: String) extends InventoryError
final case class System(msg: String) extends InventoryError
final case class Inconsistency(msg: String) extends InventoryError
final case class System(msg: String) extends InventoryError
}


Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
/*
*************************************************************************************
* 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/>.
*************************************************************************************
* 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.inventory.domain

final case class FullInventory(
node:NodeInventory,
machine:Option[MachineInventory]
node: NodeInventory,
machine: Option[MachineInventory]
)

0 comments on commit 7b4eae3

Please sign in to comment.