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

PIN-3516 Bulk update Client and Keys (One shot) #229

Merged
merged 9 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -11,6 +11,7 @@ import com.nimbusds.jwt.proc.DefaultJWTClaimsVerifier
import it.pagopa.interop.authorizationmanagement.client.api.{
ClientApi => AuthorizationClientApi,
KeyApi => AuthorizationKeyApi,
MigrateApi,
PurposeApi => AuthorizationPurposeApi
}
import it.pagopa.interop.authorizationprocess.api.impl.{
Expand Down Expand Up @@ -79,6 +80,7 @@ trait Dependencies {
AuthorizationManagementInvoker(blockingEc)(actorSystem.classicSystem),
AuthorizationClientApi(ApplicationConfiguration.getAuthorizationManagementURL),
AuthorizationKeyApi(ApplicationConfiguration.getAuthorizationManagementURL),
MigrateApi(ApplicationConfiguration.getAuthorizationManagementURL),
AuthorizationPurposeApi(ApplicationConfiguration.getAuthorizationManagementURL)
)(blockingEc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ object Main extends App with Dependencies {
for {

relationship <- client.relationships.toList.traverse(partyManagementService.getRelationshipById)
_ <- relationship.traverse(rel => authorizationManagementService.addUser(client.id, rel.from))
_ <- relationship.traverse(rel => {
if (client.users.exists(_ == rel.from)) Future.unit
else authorizationManagementService.addUser(client.id, rel.from)
})
} yield ()
}

Expand All @@ -88,8 +91,12 @@ object Main extends App with Dependencies {
def updateKeys(key: Parameter): Future[Unit] = {
logger.info(s"Update keys for client ${key.clientId}")
for {
relationship <- partyManagementService.getRelationshipById(key.relationShipId)
_ <- authorizationManagementService.updateKey(key.clientId, key.kid, relationship.from)
relationship <- partyManagementService.getRelationshipById(key.relationShipId)
persistentKey <- authorizationManagementService.getClientKey(key.clientId, key.kid)
_ <- persistentKey.userId match {
case Some(userId) if (userId == key.relationShipId) => Future.unit
case _ => authorizationManagementService.migrateKeyRelationshipToUser(key.clientId, key.kid, relationship.from)
}
nttdata-rtorsoli marked this conversation as resolved.
Show resolved Hide resolved
} yield ()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ trait AuthorizationManagementService {

def addUser(clientId: UUID, userId: UUID)(implicit contexts: Seq[(String, String)]): Future[ManagementClient]

def updateKey(clientId: UUID, keyId: String, userId: UUID)(implicit contexts: Seq[(String, String)]): Future[Unit]
def migrateKeyRelationshipToUser(clientId: UUID, keyId: String, userId: UUID)(implicit
contexts: Seq[(String, String)]
): Future[Unit]

def addRelationship(clientId: UUID, relationshipId: UUID)(implicit
contexts: Seq[(String, String)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.pagopa.interop.authorizationprocess.service.impl

import com.typesafe.scalalogging.{Logger, LoggerTakingImplicit}
import it.pagopa.interop.authorizationmanagement.client.api.{ClientApi, KeyApi, PurposeApi}
import it.pagopa.interop.authorizationmanagement.client.api.{ClientApi, KeyApi, PurposeApi, MigrateApi}
import it.pagopa.interop.authorizationmanagement.client.invoker.{ApiError, ApiRequest, BearerToken}
import it.pagopa.interop.authorizationmanagement.client.model._
import it.pagopa.interop.authorizationmanagement.model.client.{PersistentClient, PersistentClientKind}
Expand All @@ -25,6 +25,7 @@ final case class AuthorizationManagementServiceImpl(
invoker: AuthorizationManagementInvoker,
clientApi: ClientApi,
keyApi: KeyApi,
migrateApi: MigrateApi,
purposeApi: PurposeApi
)(implicit ec: ExecutionContext)
extends AuthorizationManagementService {
Expand Down Expand Up @@ -71,14 +72,18 @@ final case class AuthorizationManagementServiceImpl(
}
}

override def updateKey(clientId: UUID, keyId: String, userId: UUID)(implicit
override def migrateKeyRelationshipToUser(clientId: UUID, keyId: String, userId: UUID)(implicit
contexts: Seq[(String, String)]
): Future[Unit] = withHeaders[Unit] { (bearerToken, correlationId, ip) =>
val request: ApiRequest[Unit] =
keyApi.updateKey(xCorrelationId = correlationId, clientId, keyId, UserSeed(userId), xForwardedFor = ip)(
BearerToken(bearerToken)
)
invoker.invoke(request, "Operator addition to client")
migrateApi.migrateKeyRelationshipToUser(
xCorrelationId = correlationId,
clientId,
keyId,
UserSeed(userId),
xForwardedFor = ip
)(BearerToken(bearerToken))
invoker.invoke(request, "Key user migration")
}

override def addUser(clientId: UUID, userId: UUID)(implicit contexts: Seq[(String, String)]): Future[Client] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package it.pagopa.interop.authorizationprocess
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.testkit.ScalatestRouteTest
import it.pagopa.interop.authorizationmanagement
import it.pagopa.interop.authorizationmanagement.client.api.{ClientApi, KeyApi, PurposeApi}
import it.pagopa.interop.authorizationmanagement.client.api.{ClientApi, KeyApi, PurposeApi, MigrateApi}
import it.pagopa.interop.authorizationmanagement.model.client.{Api, PersistentClient, PersistentClientKind}
import it.pagopa.interop.authorizationprocess.api.impl.ClientApiServiceImpl
import it.pagopa.interop.authorizationprocess.common.readmodel.PaginatedResult
Expand Down Expand Up @@ -88,6 +88,7 @@ class ClientOperationSpec extends AnyWordSpecLike with MockFactory with SpecUtil
AuthorizationManagementInvoker(ExecutionContext.global),
ClientApi(),
KeyApi(),
MigrateApi(),
PurposeApi()
),
mockAgreementManagementService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package it.pagopa.interop.authorizationprocess

import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.testkit.ScalatestRouteTest
import it.pagopa.interop.authorizationmanagement.client.api.{ClientApi, KeyApi, PurposeApi}
import it.pagopa.interop.authorizationmanagement.client.api.{ClientApi, KeyApi, PurposeApi, MigrateApi}
import it.pagopa.interop.authorizationmanagement.client.{model => AuthorizationManagementDependency}
import it.pagopa.interop.authorizationprocess.api.impl.ClientApiMarshallerImpl._
import it.pagopa.interop.authorizationprocess.api.impl.{ClientApiServiceImpl, keyFormat, keysFormat}
Expand Down Expand Up @@ -66,6 +66,7 @@ class KeyOperationSpec
AuthorizationManagementInvoker(ExecutionContext.global),
ClientApi(),
KeyApi(),
MigrateApi(),
PurposeApi()
),
mockAgreementManagementService,
Expand Down Expand Up @@ -132,6 +133,7 @@ class KeyOperationSpec
AuthorizationManagementInvoker(ExecutionContext.global),
ClientApi(),
KeyApi(),
MigrateApi(),
PurposeApi()
),
mockAgreementManagementService,
Expand Down Expand Up @@ -213,6 +215,7 @@ class KeyOperationSpec
AuthorizationManagementInvoker(ExecutionContext.global),
ClientApi(),
KeyApi(),
MigrateApi(),
PurposeApi()
),
mockAgreementManagementService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package it.pagopa.interop.authorizationprocess

import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.testkit.ScalatestRouteTest
import it.pagopa.interop.authorizationmanagement.client.api.{ClientApi, KeyApi, PurposeApi}
import it.pagopa.interop.authorizationmanagement.client.api.{ClientApi, KeyApi, PurposeApi, MigrateApi}
import it.pagopa.interop.authorizationprocess.api.impl.{ClientApiServiceImpl, OperatorApiServiceImpl}
import it.pagopa.interop.authorizationprocess.api.impl.ClientApiMarshallerImpl._
import it.pagopa.interop.authorizationprocess.error.AuthorizationProcessErrors.ClientNotFound
Expand Down Expand Up @@ -101,6 +101,7 @@ class OperatorOperationSpec
AuthorizationManagementInvoker(ExecutionContext.global),
ClientApi(),
KeyApi(),
MigrateApi(),
PurposeApi()
),
mockAgreementManagementService,
Expand Down Expand Up @@ -351,6 +352,7 @@ class OperatorOperationSpec
AuthorizationManagementInvoker(ExecutionContext.global),
ClientApi(),
KeyApi(),
MigrateApi(),
PurposeApi()
),
mockAgreementManagementService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ object FakeDependencies {
limit: Int
)(implicit ec: ExecutionContext, readModel: ReadModelService): Future[PaginatedResult[ReadModelClientWithKeys]] =
Future.successful(PaginatedResult(results = Seq.empty, totalCount = 0))

override def migrateKeyRelationshipToUser(clientId: UUID, keyId: String, userId: UUID)(implicit
contexts: Seq[(String, String)]
): Future[Unit] = Future.unit

override def addUser(clientId: UUID, userId: UUID)(implicit
contexts: Seq[(String, String)]
): Future[ManagementClient] = Future.successful(
Client(
id = UUID.randomUUID(),
consumerId = UUID.randomUUID(),
name = "fake",
purposes = Seq.empty,
users = Set(userId),
kind = ClientKind.API,
createdAt = OffsetDateTimeSupplier.get()
)
)
}
class FakePartyManagementService extends PartyManagementService {

Expand Down
Loading