-
-
Notifications
You must be signed in to change notification settings - Fork 109
Create KafkaCredentialStore #530
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
Merged
bplommer
merged 19 commits into
typelevel:series/1.x
from
agustafson:certificate-loading
Apr 6, 2021
Merged
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2f7f4f0
Create KafkaCredentialStore
agustafson e206794
Add certificates.md
agustafson 39e0330
certificates.md: add title and id
agustafson 8894492
certificates.md: fix docs using fromString methods
agustafson ffe9c4b
Add headers
agustafson 349bf03
Merge branch 'master' into certificate-loading
agustafson ee2a052
scalafmt
agustafson bcaca0a
Update docs/src/main/mdoc/certificates.md
agustafson 88cfa0b
Update certificates.md
agustafson 7af23c0
Code review comments
agustafson 7e8c6f7
Add KafkaCredentialStore.createFromStrings
agustafson 6789be1
Merge branch 'master' into certificate-loading
agustafson fc79475
Add withCredentials(credentialsStore: KafkaCredentialStore) method to…
agustafson 83aaa67
Fix certificates.md
agustafson 5815a7f
Merge branch 'master' into certificate-loading
agustafson eef3208
Revert withCredentials method on AvroSettings and SchemaRegistryClien…
agustafson 9d36955
Create PemKafkaCredentialStore
agustafson 266409c
Simplify + add tests
bplommer 53cf695
Update certificates.md
bplommer 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| id: security | ||
| title: Security & Certificates | ||
| --- | ||
|
|
||
| ## Security: certificates, trust stores, and passwords | ||
|
|
||
| The `KafkaCredentialStore` can be used to create the necessary. | ||
|
|
||
| The parameters passed in are string representations of the client private key, client certificate | ||
| and service certificate. the `properties` field in `KafkaCredentialStore` can then be applied to | ||
| any of the `*Settings` classes by using the `withProperties(kafkaCredentialStore.properties)`. | ||
|
agustafson marked this conversation as resolved.
|
||
|
|
||
| ```scala mdoc | ||
| import cats.effect._ | ||
| import cats.syntax.all._ | ||
| import fs2.kafka.security._ | ||
|
|
||
| def loadKafkaSetup[F[_]: Async: ContextShift]( | ||
|
agustafson marked this conversation as resolved.
Outdated
|
||
| clientPrivateKey: String, | ||
| clientCertificate: String, | ||
| serviceCertificate: String, | ||
| ): Resource[F, KafkaCredentialStore] = | ||
| Blocker[F].evalMap { blocker => | ||
|
agustafson marked this conversation as resolved.
Outdated
|
||
| ( | ||
| ClientPrivateKey.fromString(clientPrivateKey).liftTo[F], | ||
| ClientCertificate.fromString(clientCertificate).liftTo[F], | ||
| ServiceCertificate.fromString(serviceCertificate).liftTo[F], | ||
| ).tupled.flatMap { | ||
| case (clientPrivateKey, clientCertificate, serviceCertificate) => | ||
| KafkaCredentialStore[F](clientPrivateKey, clientCertificate, serviceCertificate, blocker) | ||
| } | ||
| } | ||
|
agustafson marked this conversation as resolved.
Outdated
|
||
| ``` | ||
31 changes: 31 additions & 0 deletions
31
modules/core/src/main/scala/fs2/kafka/security/ClientCertificate.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,31 @@ | ||
| /* | ||
| * Copyright 2018-2021 OVO Energy Limited | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package fs2.kafka.security | ||
|
|
||
| import java.security.cert.{Certificate, CertificateException} | ||
|
|
||
| sealed abstract class ClientCertificate { | ||
| def value: Certificate | ||
| } | ||
|
|
||
| object ClientCertificate { | ||
| def fromString(clientCertificate: String): Either[CertificateException, ClientCertificate] = | ||
| internal.CertificateOps.loadFromString(clientCertificate).map { certificate => | ||
| new ClientCertificate { | ||
| override final val value: Certificate = | ||
| certificate | ||
|
|
||
| override final def toString: String = | ||
| s"ClientCertificate(${clientCertificate.valueShortHash})" | ||
| } | ||
| } | ||
|
|
||
| def fromCertificate(certificate: Certificate): ClientCertificate = | ||
| new ClientCertificate { | ||
| override def value: Certificate = certificate | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
modules/core/src/main/scala/fs2/kafka/security/ClientPrivateKey.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,51 @@ | ||
| /* | ||
| * Copyright 2018-2021 OVO Energy Limited | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package fs2.kafka.security | ||
|
|
||
| import cats.syntax.all._ | ||
|
|
||
| import java.nio.charset.StandardCharsets | ||
| import java.security.{GeneralSecurityException, KeyFactory, PrivateKey} | ||
| import java.security.spec.PKCS8EncodedKeySpec | ||
| import java.util.Base64 | ||
|
|
||
| sealed abstract class ClientPrivateKey { | ||
| def value: PrivateKey | ||
| } | ||
|
|
||
| object ClientPrivateKey { | ||
| def fromString(clientPrivateKey: String): Either[GeneralSecurityException, ClientPrivateKey] = | ||
| Either.catchOnly[GeneralSecurityException] { | ||
| new ClientPrivateKey { | ||
| override final val value: PrivateKey = | ||
| KeyFactory | ||
| .getInstance("RSA") | ||
| .generatePrivate { | ||
| new PKCS8EncodedKeySpec( | ||
| Base64.getDecoder.decode { | ||
| clientPrivateKey | ||
| .replace("-----BEGIN PRIVATE KEY-----", "") | ||
| .replace("-----END PRIVATE KEY-----", "") | ||
| .filterNot(_.isWhitespace) | ||
| .getBytes(StandardCharsets.UTF_8) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| override final def toString: String = | ||
| s"ClientPrivateKey(${clientPrivateKey.valueShortHash})" | ||
| } | ||
| } | ||
|
|
||
| def fromPrivateKey(privateKey: PrivateKey): ClientPrivateKey = | ||
| new ClientPrivateKey { | ||
| override def value: PrivateKey = privateKey | ||
|
|
||
| override final def toString: String = | ||
| s"ClientPrivateKey(${privateKey.toString.valueShortHash})" | ||
| } | ||
| } |
147 changes: 147 additions & 0 deletions
147
modules/core/src/main/scala/fs2/kafka/security/KafkaCredentialStore.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,147 @@ | ||
| /* | ||
| * Copyright 2018-2021 OVO Energy Limited | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package fs2.kafka.security | ||
|
|
||
| import cats.effect._ | ||
| import cats.syntax.all._ | ||
|
|
||
| import java.nio.file.{Files, Path} | ||
| import java.security.KeyStore | ||
|
|
||
| sealed abstract class KafkaCredentialStore { | ||
| def keyStoreFile: KeyStoreFile | ||
|
|
||
| def keyStorePassword: KeyStorePassword | ||
|
|
||
| def trustStoreFile: TrustStoreFile | ||
|
|
||
| def trustStorePassword: TrustStorePassword | ||
|
|
||
| def properties: Map[String, String] | ||
| } | ||
|
|
||
| object KafkaCredentialStore { | ||
| final def apply[F[_]: ContextShift]( | ||
| clientPrivateKey: ClientPrivateKey, | ||
| clientCertificate: ClientCertificate, | ||
| serviceCertificate: ServiceCertificate, | ||
| blocker: Blocker | ||
| )(implicit F: Sync[F]): F[KafkaCredentialStore] = | ||
| for { | ||
| setupDetails <- KafkaCredentialStore.createTemporary[F] | ||
| _ <- setupKeyStore( | ||
| clientPrivateKey = clientPrivateKey, | ||
| clientCertificate = clientCertificate, | ||
| keyStoreFile = setupDetails.keyStoreFile, | ||
| keyStorePassword = setupDetails.keyStorePassword, | ||
| blocker = blocker | ||
| ) | ||
| _ <- setupTrustStore( | ||
| serviceCertificate = serviceCertificate, | ||
| trustStoreFile = setupDetails.trustStoreFile, | ||
| trustStorePassword = setupDetails.trustStorePassword, | ||
| blocker = blocker | ||
| ) | ||
| } yield setupDetails | ||
|
|
||
| private final def setupStore[F[_]: ContextShift]( | ||
| storeType: String, | ||
| storePath: Path, | ||
| storePasswordChars: Array[Char], | ||
| setupStore: KeyStore => Unit, | ||
| blocker: Blocker | ||
| )(implicit F: Sync[F]): F[Unit] = | ||
| blocker.delay { | ||
| val keyStore = KeyStore.getInstance(storeType) | ||
| keyStore.load(null, storePasswordChars) | ||
| setupStore(keyStore) | ||
|
|
||
| val outputStream = Files.newOutputStream(storePath) | ||
|
|
||
| try { | ||
| keyStore.store(outputStream, storePasswordChars) | ||
| } finally { | ||
| outputStream.close() | ||
| } | ||
| } | ||
|
|
||
| private final def setupKeyStore[F[_]: ContextShift]( | ||
| clientPrivateKey: ClientPrivateKey, | ||
| clientCertificate: ClientCertificate, | ||
| keyStoreFile: KeyStoreFile, | ||
| keyStorePassword: KeyStorePassword, | ||
| blocker: Blocker | ||
| )(implicit F: Sync[F]): F[Unit] = { | ||
| val keyStorePasswordChars = | ||
| keyStorePassword.value.toCharArray | ||
|
|
||
| setupStore( | ||
| storeType = "PKCS12", | ||
| storePath = keyStoreFile.path, | ||
| storePasswordChars = keyStorePasswordChars, | ||
| setupStore = _.setEntry( | ||
| "service_key", | ||
| new KeyStore.PrivateKeyEntry( | ||
| clientPrivateKey.value, | ||
| Array(clientCertificate.value) | ||
| ), | ||
| new KeyStore.PasswordProtection(keyStorePasswordChars) | ||
| ), | ||
| blocker = blocker | ||
| ) | ||
| } | ||
|
|
||
| private final def setupTrustStore[F[_]: ContextShift]( | ||
| serviceCertificate: ServiceCertificate, | ||
| trustStoreFile: TrustStoreFile, | ||
| trustStorePassword: TrustStorePassword, | ||
| blocker: Blocker | ||
| )(implicit F: Sync[F]): F[Unit] = | ||
| setupStore( | ||
| storeType = "JKS", | ||
| storePath = trustStoreFile.path, | ||
| storePasswordChars = trustStorePassword.value.toCharArray, | ||
| setupStore = _.setCertificateEntry("CA", serviceCertificate.value), | ||
| blocker = blocker | ||
| ) | ||
|
|
||
| private final def createTemporary[F[_]](implicit F: Sync[F]): F[KafkaCredentialStore] = | ||
| for { | ||
| _keyStoreFile <- KeyStoreFile.createTemporary[F] | ||
| _keyStorePassword <- KeyStorePassword.createTemporary[F] | ||
| _trustStoreFile <- TrustStoreFile.createTemporary[F] | ||
| _trustStorePassword <- TrustStorePassword.createTemporary[F] | ||
| } yield { | ||
| new KafkaCredentialStore { | ||
| override final val keyStoreFile: KeyStoreFile = | ||
| _keyStoreFile | ||
|
|
||
| override final val keyStorePassword: KeyStorePassword = | ||
| _keyStorePassword | ||
|
|
||
| override final val trustStoreFile: TrustStoreFile = | ||
| _trustStoreFile | ||
|
|
||
| override final val trustStorePassword: TrustStorePassword = | ||
| _trustStorePassword | ||
|
|
||
| override final val properties: Map[String, String] = | ||
| Map( | ||
| "security.protocol" -> "SSL", | ||
| "ssl.truststore.location" -> trustStoreFile.pathAsString, | ||
| "ssl.truststore.password" -> trustStorePassword.value, | ||
| "ssl.keystore.type" -> "PKCS12", | ||
| "ssl.keystore.location" -> keyStoreFile.pathAsString, | ||
| "ssl.keystore.password" -> keyStorePassword.value, | ||
| "ssl.key.password" -> keyStorePassword.value | ||
| ) | ||
|
|
||
| override final def toString: String = | ||
| s"KafkaCredentialStore($keyStoreFile, $keyStorePassword, $trustStoreFile, $trustStorePassword)" | ||
| } | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
modules/core/src/main/scala/fs2/kafka/security/KeyStoreFile.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,34 @@ | ||
| /* | ||
| * Copyright 2018-2021 OVO Energy Limited | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package fs2.kafka.security | ||
|
|
||
| import cats.effect.Sync | ||
| import cats.syntax.all._ | ||
|
|
||
| import java.nio.file.Path | ||
|
|
||
| sealed abstract class KeyStoreFile { | ||
| def path: Path | ||
|
|
||
| def pathAsString: String | ||
| } | ||
|
|
||
| private[security] object KeyStoreFile { | ||
| final def createTemporary[F[_]](implicit F: Sync[F]): F[KeyStoreFile] = | ||
| internal.FileOps.createTemp("client.keystore-", ".p12").map { _path => | ||
| new KeyStoreFile { | ||
| override final val path: Path = | ||
| _path | ||
|
|
||
| override final def pathAsString: String = | ||
| path.toString | ||
|
|
||
| override final def toString: String = | ||
| s"KeyStoreFile($pathAsString)" | ||
| } | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
modules/core/src/main/scala/fs2/kafka/security/KeyStorePassword.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,30 @@ | ||
| /* | ||
| * Copyright 2018-2021 OVO Energy Limited | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package fs2.kafka.security | ||
|
|
||
| import cats.effect.Sync | ||
|
|
||
| import java.util.UUID | ||
|
|
||
| sealed abstract class KeyStorePassword { | ||
| def value: String | ||
| } | ||
|
|
||
| private[security] object KeyStorePassword { | ||
| def createTemporary[F[_]](implicit F: Sync[F]): F[KeyStorePassword] = | ||
| F.delay { | ||
| val _value = UUID.randomUUID().toString | ||
|
|
||
| new KeyStorePassword { | ||
| override final val value: String = | ||
| _value | ||
|
|
||
| override final def toString: String = | ||
| s"KeyStorePassword(${value.valueShortHash})" | ||
| } | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
modules/core/src/main/scala/fs2/kafka/security/ServiceCertificate.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,31 @@ | ||
| /* | ||
| * Copyright 2018-2021 OVO Energy Limited | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package fs2.kafka.security | ||
|
|
||
| import java.security.cert.{Certificate, CertificateException} | ||
|
|
||
| sealed abstract class ServiceCertificate { | ||
| def value: Certificate | ||
| } | ||
|
|
||
| object ServiceCertificate { | ||
| def fromString(serviceCertificate: String): Either[CertificateException, ServiceCertificate] = | ||
| internal.CertificateOps.loadFromString(serviceCertificate).map { certificate => | ||
| new ServiceCertificate { | ||
| override final val value: Certificate = | ||
| certificate | ||
|
|
||
| override final def toString: String = | ||
| s"ServiceCertificate(${serviceCertificate.valueShortHash})" | ||
| } | ||
| } | ||
|
|
||
| def fromCertificate(certificate: Certificate): ServiceCertificate = | ||
| new ServiceCertificate { | ||
| override def value: Certificate = certificate | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.