Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latest changes from trx #588

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.advancedtelematic.libtuf_server.data

import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model.MediaTypes
import akka.http.scaladsl.server.PathMatchers
import akka.http.scaladsl.unmarshalling.Unmarshaller
import com.advancedtelematic.libtuf.data.TufDataType.{RoleType, TargetFormat, ValidKeyId}
import com.advancedtelematic.libtuf.data.TufDataType.TargetFormat.TargetFormat
import com.advancedtelematic.libats.data.RefinedUtils._
import com.advancedtelematic.libats.data.RefinedUtils.*
import com.advancedtelematic.libtuf.crypt.CanonicalJson.*
import com.advancedtelematic.libtuf.data.ClientDataType.DelegatedRoleName
import com.advancedtelematic.libtuf.data.TufCodecs
import com.advancedtelematic.libtuf.data.TufDataType.TargetFormat.TargetFormat
import com.advancedtelematic.libtuf.data.TufDataType.{JsonSignedPayload, RoleType, TargetFormat, ValidKeyId}

import scala.util.Try

Expand All @@ -32,4 +36,8 @@ object Marshalling {
case _ => None
}
}

implicit val jsonSignedPayloadMarshaller: ToEntityMarshaller[JsonSignedPayload] = Marshaller.stringMarshaller(MediaTypes.`application/json`).compose[JsonSignedPayload](jsonSignedPayload =>
TufCodecs.jsonSignedPayloadEncoder.apply(jsonSignedPayload).canonical
)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.advancedtelematic.libtuf_server.data

import com.advancedtelematic.libats.codecs.CirceAts._
import com.advancedtelematic.libats.codecs.CirceAts.*
import com.advancedtelematic.libats.data.DataType.Checksum
import com.advancedtelematic.libats.slick.codecs.SlickEnumMapper
import com.advancedtelematic.libats.slick.db.SlickEncryptedColumn
import com.advancedtelematic.libtuf.data.ClientCodecs._
import com.advancedtelematic.libtuf.data.ClientCodecs.*
import com.advancedtelematic.libtuf.data.ClientDataType.TargetCustom
import com.advancedtelematic.libtuf.data.TufCodecs._
import com.advancedtelematic.libtuf.data.TufCodecs.*
import com.advancedtelematic.libtuf.data.TufDataType.{EcPrime256KeyType, Ed25519KeyType, JsonSignedPayload, KeyType, RoleType, RsaKeyType, TufKey, TufPrivateKey}
import com.advancedtelematic.libats.slick.db.SlickCirceMapper
import slick.jdbc.MySQLProfile.api._
import com.advancedtelematic.libtuf.data.TufCodecs
import slick.jdbc.MySQLProfile.api.*


object TufSlickMappings {
Expand All @@ -33,7 +34,10 @@ object TufSlickMappings {

implicit val targetCustomMapper = SlickCirceMapper.circeMapper[TargetCustom]

implicit val jsonSignedPayloadMapper = SlickCirceMapper.circeMapper[JsonSignedPayload]
implicit val jsonSignedPayloadMapper = {
implicit val encoder = TufCodecs.jsonSignedPayloadEncoder
SlickCirceMapper.circeMapper[JsonSignedPayload]
}

implicit val tufKeyMapper = SlickCirceMapper.circeMapper[TufKey]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
package com.advancedtelematic.libtuf_server.repo.client

import java.util.UUID
import akka.actor.ActorSystem
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model.*
import akka.http.scaladsl.model.Uri.Path.Slash
import akka.http.scaladsl.model.Uri.{Path, Query}
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.FromEntityUnmarshaller
import akka.http.scaladsl.util.FastFuture
import akka.stream.Materializer
import akka.stream.scaladsl.Source
import akka.util.ByteString
import com.advancedtelematic.libats.codecs.CirceCodecs.*
import com.advancedtelematic.libats.data.DataType.{Checksum, Namespace}
import com.advancedtelematic.libats.data.{ErrorCode, PaginationResult}
import com.advancedtelematic.libats.http.Errors.{RawError, RemoteServiceError}
import com.advancedtelematic.libats.http.HttpCodecs._
import com.advancedtelematic.libats.http.ServiceHttpClientSupport
import com.advancedtelematic.libats.http.tracing.Tracing.ServerRequestTracing
import com.advancedtelematic.libats.http.tracing.TracingHttpClient
import com.advancedtelematic.libats.http.ServiceHttpClientSupport
import com.advancedtelematic.libtuf.data.TufCodecs._
import com.advancedtelematic.libtuf.data.ClientCodecs.*
import com.advancedtelematic.libtuf.data.ClientDataType.{ClientTargetItem, DelegatedRoleName, Delegation, DelegationClientTargetItem, DelegationFriendlyName, RootRole, TargetsRole}
import com.advancedtelematic.libtuf.data.TufCodecs.*
import com.advancedtelematic.libtuf.data.TufDataType.TargetFormat.TargetFormat
import com.advancedtelematic.libtuf.data.TufDataType.{HardwareIdentifier, JsonSignedPayload, KeyType, RepoId, SignedPayload, TargetFilename, TargetName, TargetVersion}
import com.advancedtelematic.libtuf_server.data.Requests.{CommentRequest, CreateRepositoryRequest, FilenameComment, TargetComment}
import com.advancedtelematic.libtuf_server.repo.client.ReposerverClient.{DelegationInfo, KeysNotReady, NotFound, RootNotInKeyserver}
import io.circe.{Decoder, Encoder, Json}
import com.advancedtelematic.libats.codecs.CirceCodecs._
import com.advancedtelematic.libats.codecs.CirceValidatedGeneric.validatedGenericKeyDecoder
import com.advancedtelematic.libtuf.data.ClientCodecs._
import com.advancedtelematic.libtuf.data.ClientDataType.{ClientTargetItem, DelegatedRoleName, Delegation, DelegationClientTargetItem, DelegationFriendlyName, RootRole, TargetCustom, TargetsRole}

import scala.concurrent.{ExecutionContext, Future}
import scala.reflect.ClassTag
import scala.util.{Failure, Success}
import io.circe.generic.semiauto._
import io.circe.generic.semiauto.*
import io.circe.{Codec, Decoder, Encoder, Json}
import org.slf4j.LoggerFactory

import java.net.URI
import java.time.Instant
import java.util.UUID
import scala.concurrent.{ExecutionContext, Future}
import scala.reflect.ClassTag
import scala.util.{Failure, Success}
import com.advancedtelematic.libats.http.HttpCodecs.*

object ReposerverClient {

object RequestTargetItem {
implicit val encoder: Encoder[RequestTargetItem] = deriveEncoder
implicit val decoder: Decoder[RequestTargetItem] = deriveDecoder
implicit val requestTargetItemCode: Codec[RequestTargetItem] = deriveCodec
}

case class RequestTargetItem(uri: Uri, checksum: Checksum,
Expand Down Expand Up @@ -142,11 +138,11 @@ class ReposerverHttpClient(reposerverUri: Uri, httpClient: HttpRequest => Future
(implicit ec: ExecutionContext, system: ActorSystem, tracing: ServerRequestTracing)
extends TracingHttpClient(httpClient, "reposerver") with ReposerverClient {

import ReposerverClient._
import ReposerverClient.*
import com.advancedtelematic.libats.http.ServiceHttpClient
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import io.circe.syntax._
import ServiceHttpClient._
import ServiceHttpClient.*
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport.*
import io.circe.syntax.*

val log = LoggerFactory.getLogger(this.getClass)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import java.time.Instant
import akka.http.scaladsl.model.Uri
import akka.http.scaladsl.util.FastFuture
import com.advancedtelematic.libats.data.DataType.Checksum
import com.advancedtelematic.libtuf.data.ClientDataType
import com.advancedtelematic.libtuf.data.{ClientDataType, TufCodecs}
import com.advancedtelematic.libtuf.data.ClientDataType.TufRole
import com.advancedtelematic.libtuf.data.TufDataType.JsonSignedPayload
import com.advancedtelematic.libtuf.crypt.CanonicalJson._
import com.advancedtelematic.libtuf.crypt.CanonicalJson.*
import com.advancedtelematic.libtuf.data.ClientDataType.{MetaItem, MetaPath}
import com.advancedtelematic.libtuf_server.crypto.Sha256Digest
import io.circe.Decoder
import io.circe.syntax._
import io.circe.syntax.*

import scala.concurrent.Future
import scala.util.Try
Expand Down Expand Up @@ -48,7 +48,7 @@ object DataType {

def withChecksum[T : TufRole : Decoder](content: JsonSignedPayload, version: Int, expireAt: Instant): Future[SignedRole[T]] = FastFuture {
Try {
val canonicalJson = content.asJson.canonical
val canonicalJson = TufCodecs.jsonSignedPayloadEncoder(content).canonical
val checksum = Sha256Digest.digest(canonicalJson.getBytes)
val signedRole = SignedRole[T](content, checksum, canonicalJson.length, version, expireAt)
signedRole.role // Decode the role to make sure it's valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ object TufCodecs {

implicit def signedPayloadCodec[T : Encoder : Decoder]: Codec[SignedPayload[T]] = io.circe.Codec.from(signedPayloadDecoder, signedPayloadEncoder)

implicit val jsonSignedPayloadEncoder: Encoder[JsonSignedPayload] = deriveEncoder
// This encoder is not implicit to force custom http marshallers to be used instead, to marshall responses as canonical json
val jsonSignedPayloadEncoder: Encoder[JsonSignedPayload] = deriveEncoder
implicit val jsonSignedPayloadDecoder: Decoder[JsonSignedPayload] = deriveDecoder

implicit val rsaKeyTypeEncoder: Encoder[RsaKeyType.type] = Encoder[String].contramap(_ => "RSA")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package com.advancedtelematic.tuf.reposerver.delegations

import akka.http.scaladsl.model.Uri
import cats.implicits._
import cats.implicits.*
import cats.data.Validated.{Invalid, Valid}
import cats.data.{NonEmptyList, Validated, ValidatedNel}
import com.advancedtelematic.libats.data.RefinedUtils._
import com.advancedtelematic.libats.data.RefinedUtils.*
import com.advancedtelematic.libtuf.crypt.TufCrypto
import com.advancedtelematic.libtuf.data.ClientCodecs._
import com.advancedtelematic.libtuf.data.ClientCodecs.*
import com.advancedtelematic.libtuf.data.ClientDataType.{ClientTargetItem, DelegatedRoleName, Delegation, DelegationClientTargetItem, DelegationFriendlyName, MetaItem, MetaPath, TargetCustom, TargetsRole, ValidMetaPath}
import com.advancedtelematic.libtuf.data.TufDataType.{JsonSignedPayload, RepoId, SignedPayload, TargetFilename}
import com.advancedtelematic.libtuf_server.crypto.Sha256Digest
import com.advancedtelematic.libtuf_server.repo.server.DataType.SignedRole
import com.advancedtelematic.libtuf_server.repo.server.SignedRoleGeneration
import com.advancedtelematic.tuf.reposerver.db.{DelegationRepositorySupport, SignedRoleRepositorySupport}
import com.advancedtelematic.tuf.reposerver.http._
import slick.jdbc.MySQLProfile.api._
import com.advancedtelematic.tuf.reposerver.http.*
import slick.jdbc.MySQLProfile.api.*

import scala.async.Async._
import scala.async.Async.*
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import akka.http.scaladsl.unmarshalling._
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport.*
import akka.http.scaladsl.unmarshalling.*
import akka.http.scaladsl.util.FastFuture
import com.advancedtelematic.libtuf.data.TufCodecs._
import com.advancedtelematic.libtuf.data.ClientCodecs._
import com.advancedtelematic.libtuf.data.TufCodecs.*
import com.advancedtelematic.libtuf.data.ClientCodecs.*
import com.advancedtelematic.libtuf.data.TufCodecs
import com.advancedtelematic.tuf.reposerver.data.RepoDataType.{DelegationInfo, TargetItem}

import java.nio.file.{FileSystems, Paths}
Expand Down Expand Up @@ -53,7 +54,7 @@ class SignedRoleDelegationsFind()(implicit val db: Database, val ec: ExecutionCo
}

private def asMetaItem(content: JsonSignedPayload): Try[MetaItem] = {
val canonicalJson = content.asJson.canonical
val canonicalJson = TufCodecs.jsonSignedPayloadEncoder(content).canonical
val checksum = Sha256Digest.digest(canonicalJson.getBytes)
val hashes = Map(checksum.method -> checksum.hash)
val versionT = content.signed.hcursor.downField("version").as[Int].toTry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import java.time.Instant
import io.circe.syntax.*
import com.advancedtelematic.libats.data.ErrorRepresentation.*
import akka.http.scaladsl.model.headers.{RawHeader, `Content-Length`}
import akka.http.scaladsl.model.{EntityStreamException, HttpEntity, HttpHeader, HttpRequest, HttpResponse, ParsingException, StatusCode, StatusCodes, Uri}
import akka.http.scaladsl.model.{EntityStreamException, HttpEntity, HttpHeader, HttpRequest, HttpResponse, MediaTypes, ParsingException, StatusCode, StatusCodes, Uri}
import akka.http.scaladsl.server.*
import akka.http.scaladsl.unmarshalling.*
import akka.http.scaladsl.util.FastFuture
import akka.stream.scaladsl.Source
import akka.util.ByteString
import cats.data.Validated.{Invalid, Valid}
import com.advancedtelematic.libats.codecs.CirceRefined._
import com.advancedtelematic.libats.codecs.CirceRefined.*
import com.advancedtelematic.libats.codecs.CirceValidatedGeneric.validatedGenericDecoder
import com.advancedtelematic.libats.data.DataType.HashMethod.HashMethod
import com.advancedtelematic.libats.data.RefinedUtils.*
Expand All @@ -25,8 +25,8 @@ import com.advancedtelematic.libtuf.data.TufDataType.RoleType.RoleType
import com.advancedtelematic.libats.http.AnyvalMarshallingSupport.*
import com.advancedtelematic.libats.data.DataType.{Namespace, ValidChecksum}
import com.advancedtelematic.libats.data.{ErrorRepresentation, PaginationResult}
import com.advancedtelematic.libtuf.data.{ClientCodecs, TufCodecs}
import com.advancedtelematic.libtuf.data.TufDataType.*
import com.advancedtelematic.libtuf.data.TufDataType.TargetFilename
import com.advancedtelematic.libtuf_server.data.Marshalling.*
import com.advancedtelematic.libtuf_server.data.Requests.{CommentRequest, CreateRepositoryRequest, *}
import com.advancedtelematic.libtuf_server.keyserver.KeyserverClient
Expand All @@ -53,8 +53,10 @@ import scala.util.{Failure, Success}
import com.advancedtelematic.tuf.reposerver.data.RepoCodecs.*
import com.advancedtelematic.tuf.reposerver.http.CustomParameterUnmarshallers.nonNegativeLong
import com.advancedtelematic.tuf.reposerver.http.PaginationParams.PaginationResultOps
import com.advancedtelematic.tuf.reposerver.data.RepoCodecs.*
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
import eu.timepit.refined.api.Refined

import com.advancedtelematic.libtuf_server.data.Marshalling.jsonSignedPayloadMarshaller

class RepoResource(keyserverClient: KeyserverClient, namespaceValidation: NamespaceValidation,
targetStore: TargetStore, tufTargetsPublisher: TufTargetsPublisher,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import scala.concurrent.Future
import org.scalatest.OptionValues._

import java.net.URI
import com.advancedtelematic.libtuf_server.data.Marshalling.*

class RepoResourceSpec extends TufReposerverSpec with RepoResourceSpecUtil
with ResourceSpec with BeforeAndAfterAll with Inspectors with Whenever with PatienceConfiguration with SignedRoleRepositorySupport {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import scala.concurrent.Future
import com.advancedtelematic.tuf.reposerver.data.RepoCodecs._
import com.advancedtelematic.tuf.reposerver.http.{TufRepoSignedRoleProvider, TufRepoTargetItemsProvider}
import eu.timepit.refined.api.Refined
import com.advancedtelematic.libtuf_server.data.Marshalling.*

trait RepoResourceDelegationsSpecUtil extends RepoResourceSpecUtil {
lazy val keyPair = Ed25519KeyType.crypto.generateKeyPair()
Expand Down
Loading