-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #515 from hmrc/APB-8276
APB-8276 Setting up field encryption and support for unencrypted old data
- Loading branch information
Showing
11 changed files
with
398 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
52 changes: 52 additions & 0 deletions
52
app/uk/gov/hmrc/agentservicesaccount/modules/CryptoProviderModule.scala
This file contains 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,52 @@ | ||
/* | ||
* Copyright 2024 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.agentservicesaccount.modules | ||
|
||
import play.api.inject.{Binding, Module} | ||
import play.api.{Configuration, Environment} | ||
import uk.gov.hmrc.crypto.{Crypted, Decrypter, Encrypter, PlainBytes, PlainContent, PlainText, SymmetricCryptoFactory} | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.util.Base64 | ||
|
||
class CryptoProviderModule extends Module { | ||
|
||
def aesCryptoInstance(configuration: Configuration): Encrypter with Decrypter = if ( | ||
configuration.underlying.getBoolean("fieldLevelEncryption.enable") | ||
) | ||
SymmetricCryptoFactory.aesCryptoFromConfig("fieldLevelEncryption", configuration.underlying) | ||
else | ||
NoCrypto | ||
|
||
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = | ||
Seq( | ||
bind[Encrypter with Decrypter].qualifiedWith("aes").toInstance(aesCryptoInstance(configuration)) | ||
) | ||
} | ||
|
||
/** Encrypter/decrypter that does nothing (i.e. leaves content in plaintext). Only to be used for debugging. | ||
*/ | ||
trait NoCrypto extends Encrypter with Decrypter { | ||
def encrypt(plain: PlainContent): Crypted = plain match { | ||
case PlainText(text) => Crypted(text) | ||
case PlainBytes(bytes) => Crypted(new String(Base64.getEncoder.encode(bytes), StandardCharsets.UTF_8)) | ||
} | ||
def decrypt(notEncrypted: Crypted): PlainText = PlainText(notEncrypted.value) | ||
def decryptAsBytes(nullEncrypted: Crypted): PlainBytes = PlainBytes(Base64.getDecoder.decode(nullEncrypted.value)) | ||
} | ||
|
||
object NoCrypto extends NoCrypto |
This file contains 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
37 changes: 37 additions & 0 deletions
37
app/uk/gov/hmrc/agentservicesaccount/utils/EncryptedStringUtil.scala
This file contains 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 2024 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.agentservicesaccount.utils | ||
|
||
import play.api.Logging | ||
import play.api.libs.json.{Format, Json} | ||
import uk.gov.hmrc.crypto.json.JsonEncryption.{stringDecrypter, stringEncrypter} | ||
import uk.gov.hmrc.crypto.{Decrypter, Encrypter} | ||
|
||
object EncryptedStringUtil extends Logging { | ||
//TODO: replace all usages of this with stringDecrypterEncrypter once all old sessions have timed out | ||
def fallbackStringFormat(implicit crypto: Encrypter with Decrypter): Format[String] = | ||
Format( | ||
json => try { | ||
stringDecrypter.reads(json) | ||
} catch { | ||
case _: Throwable => | ||
logger.warn("[EncryptedStringUtil] found unencrypted string") | ||
Json.fromJson[String](json) | ||
}, | ||
stringEncrypter | ||
) | ||
} |
This file contains 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 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.