diff --git a/jwt/src/main/scala/it/pagopa/interop/commons/jwt/package.scala b/jwt/src/main/scala/it/pagopa/interop/commons/jwt/package.scala index 79749860..48fc1410 100644 --- a/jwt/src/main/scala/it/pagopa/interop/commons/jwt/package.scala +++ b/jwt/src/main/scala/it/pagopa/interop/commons/jwt/package.scala @@ -1,8 +1,5 @@ package it.pagopa.interop.commons -import akka.http.scaladsl.marshalling.ToEntityMarshaller -import akka.http.scaladsl.model.StatusCodes -import akka.http.scaladsl.server.Directives.complete import akka.http.scaladsl.server.Route import cats.syntax.all._ import com.nimbusds.jose.crypto.{ECDSAVerifier, RSASSAVerifier} @@ -159,26 +156,4 @@ package object jwt { AkkaResponses.forbidden(OperationForbidden, logMessage) } - - // TODO Kept for backward compatibility. - // To be removed as soon as all services have been migrated to the new authorize function - def authorizeInterop[T](isAuthorized: => Boolean, errorMessage: => T)( - route: => Route - )(implicit contexts: Seq[(String, String)], errorMarshaller: ToEntityMarshaller[T]): Route = if (isAuthorized) route - else { - val values: Map[String, String] = contexts.toMap - val ipAddress: String = values.getOrElse(IP_ADDRESS, "") - val uid: String = values.get(UID).filterNot(_.isBlank).orElse(values.get(SUB)).getOrElse("") - val organizationId: String = values.getOrElse(ORGANIZATION_ID_CLAIM, "") - val correlationId: String = values.getOrElse(CORRELATION_ID_HEADER, "") - val header: String = s"[IP=$ipAddress] [UID=$uid] [OID=$organizationId] [CID=$correlationId]" - val body: String = values - .get(USER_ROLES) - .fold(s"No user roles found to execute this request")(roles => - s"Invalid user roles ($roles) to execute this request" - ) - - logger.error(s"$header $body") - complete(StatusCodes.Forbidden, errorMessage) - } } diff --git a/utils/src/main/scala/it/pagopa/interop/commons/logging/LoggerLayout.scala b/utils/src/main/scala/it/pagopa/interop/commons/logging/LoggerLayout.scala index ef172f88..c211610b 100644 --- a/utils/src/main/scala/it/pagopa/interop/commons/logging/LoggerLayout.scala +++ b/utils/src/main/scala/it/pagopa/interop/commons/logging/LoggerLayout.scala @@ -5,7 +5,7 @@ import ch.qos.logback.core.CoreConstants.LINE_SEPARATOR import ch.qos.logback.core.LayoutBase import ch.qos.logback.core.util.CachingDateFormatter import scala.jdk.CollectionConverters.MapHasAsScala -import it.pagopa.interop.commons.utils.{CORRELATION_ID_HEADER, IP_ADDRESS, ORGANIZATION_ID_CLAIM, SUB, UID} +import it.pagopa.interop.commons.utils.{CORRELATION_ID_HEADER, ORGANIZATION_ID_CLAIM, SUB, UID} final class LoggerLayout extends LayoutBase[ILoggingEvent] { @@ -22,7 +22,6 @@ final class LoggerLayout extends LayoutBase[ILoggingEvent] { val mdc: Map[String, String] = event.getMdc().asScala.toMap val cid: String = mdc.get(CORRELATION_ID_HEADER).fold("[CID=]")(s => s"[CID=$s]") val header: String = List( - mdc.get(IP_ADDRESS).fold("[IP=]")(s => s"[IP=$s]"), mdc.get(UID).orElse(mdc.get(SUB)).fold("[UID=]")(s => s"[UID=$s]"), mdc.get(ORGANIZATION_ID_CLAIM).fold("[OID=]")(s => s"[OID=$s]"), cid diff --git a/utils/src/main/scala/it/pagopa/interop/commons/logging/logging.scala b/utils/src/main/scala/it/pagopa/interop/commons/logging/logging.scala index 8472ef0a..947ad152 100644 --- a/utils/src/main/scala/it/pagopa/interop/commons/logging/logging.scala +++ b/utils/src/main/scala/it/pagopa/interop/commons/logging/logging.scala @@ -7,7 +7,6 @@ import com.typesafe.config.{Config, ConfigFactory} import com.typesafe.scalalogging.CanLog import it.pagopa.interop.commons.utils.{ CORRELATION_ID_HEADER, - IP_ADDRESS, ORGANIZATION_ID_CLAIM, SUB, UID, @@ -29,7 +28,6 @@ package object logging { implicit case object CanLogContextFields extends CanLog[ContextFieldsToLog] { override def logMessage(originalMsg: String, fields: ContextFieldsToLog): String = { - MDC.put(IP_ADDRESS, contextOrBlank(fields, IP_ADDRESS)) MDC.put(UID, contextOrBlank(fields, UID)) MDC.put(SUB, contextOrBlank(fields, SUB)) MDC.put(ORGANIZATION_ID_CLAIM, contextOrBlank(fields, ORGANIZATION_ID_CLAIM)) @@ -39,7 +37,6 @@ package object logging { } override def afterLog(context: ContextFieldsToLog): Unit = { - MDC.remove(IP_ADDRESS) MDC.remove(UID) MDC.remove(SUB) MDC.remove(ORGANIZATION_ID_CLAIM) @@ -59,19 +56,15 @@ package object logging { changeUUID: Boolean )(wrappingDirective: Directive1[Seq[(String, String)]]): Directive1[Seq[(String, String)]] = for { - ip <- extractClientIP correlationId <- optionalHeaderValueByName(CORRELATION_ID_HEADER) language <- selectPreferredLanguage(DEFAULT_LANGUAGE, OTHER_LANGUAGES: _*) contexts <- wrappingDirective } yield { - val ipAddress: String = ip.toOption.map(_.getHostAddress).getOrElse("unknown") def uuid: String = UUID.randomUUID().toString val actualCorrelationId: String = if (changeUUID) uuid else correlationId.getOrElse(uuid) val acceptLanguage: String = language.toString - contexts.prependedAll( - List(CORRELATION_ID_HEADER -> actualCorrelationId, IP_ADDRESS -> ipAddress, ACCEPT_LANGUAGE -> acceptLanguage) - ) + contexts.prependedAll(List(CORRELATION_ID_HEADER -> actualCorrelationId, ACCEPT_LANGUAGE -> acceptLanguage)) } def logHttp( diff --git a/utils/src/main/scala/it/pagopa/interop/commons/utils/package.scala b/utils/src/main/scala/it/pagopa/interop/commons/utils/package.scala index e9a95dbe..e149f03c 100644 --- a/utils/src/main/scala/it/pagopa/interop/commons/utils/package.scala +++ b/utils/src/main/scala/it/pagopa/interop/commons/utils/package.scala @@ -20,7 +20,6 @@ package object utils { val ORGANIZATION: String = "organization" val USER_ROLES: String = "user-roles" val CORRELATION_ID_HEADER: String = "X-Correlation-Id" - val IP_ADDRESS: String = "X-Forwarded-For" val ACCEPT_LANGUAGE: String = "Accept-Language" val CONTENT_LANGUAGE: String = "Content-Language" val INTEROP_PRODUCT_NAME: String = "prod-interop" @@ -39,21 +38,17 @@ package object utils { type BearerToken = String type CorrelationId = String - type IpAddress = String - def extractHeaders( - contexts: Seq[(String, String)] - ): Either[ComponentError, (BearerToken, CorrelationId, Option[IpAddress])] = { + def extractHeaders(contexts: Seq[(String, String)]): Either[ComponentError, (BearerToken, CorrelationId)] = { val contextsMap = contexts.toMap for { bearerToken <- contextsMap.get(BEARER).toRight(MissingBearer) correlationId <- contextsMap.get(CORRELATION_ID_HEADER).toRight(MissingHeader(CORRELATION_ID_HEADER)) - ip = contextsMap.get(IP_ADDRESS) - } yield (bearerToken, correlationId, ip) + } yield (bearerToken, correlationId) } def withHeaders[T]( - f: (BearerToken, CorrelationId, Option[IpAddress]) => Future[T] + f: (BearerToken, CorrelationId) => Future[T] )(implicit contexts: Seq[(String, String)]): Future[T] = extractHeaders(contexts) match { case Left(ex) => Future.failed(ex) case Right(x) => f.tupled(x)