-
Notifications
You must be signed in to change notification settings - Fork 8
DDCNL-12161: TAI - Add in auth caching #425
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
Open
askar-makkunnath
wants to merge
5
commits into
main
Choose a base branch
from
DDCNL-12161
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0308433
DDCNL-12161: Add in auth cache
askar-makkunnath 5ea29c8
DDCNL-12161: Adding tests for auth cache
askar-makkunnath 74e49e9
DDCNL-12161: Cleanup
askar-makkunnath e51f07d
Merge branch 'main' into DDCNL-12161
askar-makkunnath 1ba6f22
DDCNL-12161: pr comment fix
askar-makkunnath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright 2026 HM Revenue & Customs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.gov.hmrc.tai.model | ||
|
|
||
| import play.api.libs.json.{Json, OFormat} | ||
|
|
||
| case class CachedAuthRetrievals( | ||
| nino: String | ||
| ) | ||
|
|
||
| object CachedAuthRetrievals { | ||
| implicit val format: OFormat[CachedAuthRetrievals] = Json.format[CachedAuthRetrievals] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright 2026 HM Revenue & Customs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.gov.hmrc.tai.model | ||
|
|
||
| import play.api.libs.json.* | ||
| import uk.gov.hmrc.crypto.json.JsonEncryption.{sensitiveDecrypter, sensitiveEncrypter} | ||
| import uk.gov.hmrc.crypto.{Decrypter, Encrypter, Sensitive} | ||
|
|
||
| case class SensitiveWrapper[T](override val decryptedValue: T) extends Sensitive[T] | ||
|
|
||
| object SensitiveWrapper { | ||
|
|
||
| implicit def reads[T](implicit | ||
| reads: Reads[T], | ||
| crypto: Encrypter with Decrypter | ||
| ): Reads[SensitiveWrapper[T]] = sensitiveDecrypter(SensitiveWrapper[T]) | ||
|
|
||
| implicit def writes[T](implicit | ||
| writes: Writes[T], | ||
| crypto: Encrypter with Decrypter | ||
| ): Writes[SensitiveWrapper[T]] = sensitiveEncrypter | ||
|
|
||
| } |
58 changes: 58 additions & 0 deletions
58
app/uk/gov/hmrc/tai/repositories/cache/AuthCacheRepository.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright 2026 HM Revenue & Customs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.gov.hmrc.tai.repositories.cache | ||
|
|
||
| import play.api.libs.json.{Reads, Writes} | ||
| import uk.gov.hmrc.tai.config.{CryptoProvider, MongoConfig} | ||
| import uk.gov.hmrc.http.HeaderCarrier | ||
| import uk.gov.hmrc.mongo.cache.DataKey | ||
| import uk.gov.hmrc.mongo.{CurrentTimestampSupport, MongoComponent} | ||
|
|
||
| import java.util.concurrent.TimeUnit | ||
| import javax.inject.{Inject, Singleton} | ||
| import scala.concurrent.ExecutionContext | ||
| import scala.concurrent.duration.Duration | ||
|
|
||
| @Singleton | ||
| class AuthCacheRepository @Inject() ( | ||
| mongoConfig: MongoConfig, | ||
| cryptoProvider: CryptoProvider, | ||
| mongoComponent: MongoComponent | ||
| )(implicit ec: ExecutionContext) | ||
| extends GenericCacheRepository[HeaderCarrier]( | ||
| mongoComponent = mongoComponent, | ||
| crypto = cryptoProvider.get, | ||
| collectionName = "taiAuthCache", | ||
| ttl = Duration(mongoConfig.mongoAuthTTL, TimeUnit.SECONDS), | ||
| timestampSupport = new CurrentTimestampSupport(), | ||
| cacheIdType = RequestCacheId | ||
| ) { | ||
|
|
||
| def putSession[T: Writes](dataKey: DataKey[T], data: T)(implicit | ||
| hc: HeaderCarrier | ||
| ): scala.concurrent.Future[(String, String)] = | ||
| put(hc, dataKey, data) | ||
|
|
||
| def getFromSession[T: Reads](dataKey: DataKey[T])(implicit hc: HeaderCarrier): scala.concurrent.Future[Option[T]] = | ||
| get(hc, dataKey) | ||
|
|
||
| def deleteFromSession[T](dataKey: DataKey[T])(implicit hc: HeaderCarrier): scala.concurrent.Future[Unit] = | ||
| delete(hc, dataKey) | ||
|
|
||
| def deleteAllFromSession(implicit hc: HeaderCarrier): scala.concurrent.Future[Unit] = | ||
| deleteAll(hc) | ||
| } |
84 changes: 84 additions & 0 deletions
84
app/uk/gov/hmrc/tai/repositories/cache/GenericCacheRepository.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Copyright 2026 HM Revenue & Customs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.gov.hmrc.tai.repositories.cache | ||
|
|
||
| import org.mongodb.scala.model.IndexModel | ||
| import play.api.Logging | ||
| import play.api.libs.json.{Reads, Writes} | ||
| import uk.gov.hmrc.crypto.{Decrypter, Encrypter} | ||
| import uk.gov.hmrc.tai.model.SensitiveWrapper | ||
| import uk.gov.hmrc.mdc.Mdc | ||
| import uk.gov.hmrc.mongo.cache.{CacheIdType, DataKey, MongoCacheRepository} | ||
| import uk.gov.hmrc.mongo.{MongoComponent, MongoDatabaseCollection, TimestampSupport} | ||
|
|
||
| import javax.inject.{Inject, Singleton} | ||
| import scala.concurrent.duration.Duration | ||
| import scala.concurrent.{ExecutionContext, Future} | ||
| import scala.util.control.NonFatal | ||
|
|
||
| @Singleton | ||
| abstract class GenericCacheRepository[CacheId] @Inject() ( | ||
| mongoComponent: MongoComponent, | ||
| override val collectionName: String, | ||
| val crypto: Encrypter with Decrypter, | ||
| replaceIndexes: Boolean = true, | ||
| ttl: Duration, | ||
| timestampSupport: TimestampSupport, | ||
| cacheIdType: CacheIdType[CacheId] | ||
| )(implicit ec: ExecutionContext) | ||
| extends MongoDatabaseCollection with Logging { | ||
|
|
||
| private val cacheRepo: MongoCacheRepository[CacheId] = new MongoCacheRepository[CacheId]( | ||
| mongoComponent = mongoComponent, | ||
| collectionName = collectionName, | ||
| replaceIndexes = replaceIndexes, | ||
| ttl = ttl, | ||
| timestampSupport = timestampSupport, | ||
| cacheIdType = cacheIdType | ||
| ) | ||
|
|
||
| override val indexes: Seq[IndexModel] = cacheRepo.indexes | ||
|
|
||
| given (Encrypter with Decrypter) = crypto | ||
|
|
||
| def put[T: Writes](cacheId: CacheId, dataKey: DataKey[T], data: T): Future[(String, String)] = | ||
| Mdc.preservingMdc { | ||
| cacheRepo | ||
| .put[SensitiveWrapper[T]](cacheId)(DataKey[SensitiveWrapper[T]](dataKey.unwrap), SensitiveWrapper(data)) | ||
| .map(res => "id" -> res.id) | ||
| } | ||
|
|
||
| def get[T: Reads](cacheId: CacheId, dataKey: DataKey[T]): Future[Option[T]] = | ||
| Mdc.preservingMdc { | ||
| cacheRepo | ||
| .get[SensitiveWrapper[T]](cacheId)(DataKey[SensitiveWrapper[T]](dataKey.unwrap)) | ||
| .map(_.map(_.decryptedValue)) recoverWith { case NonFatal(error) => | ||
| logger.error(s"Failed to read data from cache", error) | ||
| Future.successful(None) | ||
| } | ||
| } | ||
|
|
||
| def delete[T](cacheId: CacheId, dataKey: DataKey[T]): Future[Unit] = | ||
| Mdc.preservingMdc { | ||
| cacheRepo.delete(cacheId)(DataKey[SensitiveWrapper[T]](dataKey.unwrap)) | ||
| } | ||
|
|
||
| def deleteAll(cacheId: CacheId): Future[Unit] = | ||
| Mdc.preservingMdc { | ||
| cacheRepo.deleteEntity(cacheId) | ||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
app/uk/gov/hmrc/tai/repositories/cache/RequestCacheId.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright 2026 HM Revenue & Customs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.gov.hmrc.tai.repositories.cache | ||
|
|
||
| import play.api.Logger | ||
| import uk.gov.hmrc.http.HeaderCarrier | ||
| import uk.gov.hmrc.mongo.cache.CacheIdType | ||
|
|
||
| import java.util.UUID.randomUUID | ||
|
|
||
| case object RequestCacheId extends CacheIdType[HeaderCarrier] { | ||
| override def run: HeaderCarrier => String = { | ||
| val logger: Logger = Logger(this.getClass) | ||
|
|
||
| _.requestId | ||
| .map(_.value) | ||
| .getOrElse { | ||
| logger.error(NoRequestIdException.getMessage, NoRequestIdException) | ||
| randomUUID.toString | ||
| } | ||
| } | ||
| } | ||
| case object NoRequestIdException extends Exception("Could not find requestId") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think put should also have recoverWith like get wher ewe log an error and move on, now it would fail bcos of mongo blip
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept this aligned with Pertax and FANDF, where put() doesn't recover. If we want to make cache writes best-effort, I'd prefer handling it in AuthAction around the putSession call rather than changing the generic repository.