-
-
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 14 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,31 @@ | ||
| --- | ||
| id: security | ||
| title: Security & Certificates | ||
| --- | ||
|
|
||
| ## Security: certificates, trust stores, and passwords | ||
|
|
||
| The `KafkaCredentialStore` can be used to create the necessary trust stores and passwords to access kafka. | ||
|
|
||
| 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)`. | ||
|
|
||
| ```scala mdoc | ||
| import cats.effect._ | ||
| import cats.syntax.all._ | ||
| import fs2.kafka._ | ||
| import fs2.kafka.security._ | ||
|
|
||
| def createKafkaProducer[F[_]: Sync: ContextShift, K, V]( | ||
| clientPrivateKey: String, | ||
| clientCertificate: String, | ||
| serviceCertificate: String, | ||
| )(implicit keySer: Serializer[F, K], valSer: Serializer[F, V]): F[ProducerSettings[F, K, V]] = | ||
| Blocker[F].use { blocker => | ||
| KafkaCredentialStore.createFromStrings[F](clientPrivateKey, clientCertificate, serviceCertificate, blocker) | ||
| }.map { credentialStore => | ||
| ProducerSettings(keySer, valSer) | ||
| .withCredentials(credentialStore) | ||
| } | ||
| ``` | ||
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
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})" | ||
| } | ||
| } |
162 changes: 162 additions & 0 deletions
162
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,162 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| final def createFromStrings[F[_]: Sync: ContextShift]( | ||
| clientPrivateKey: String, | ||
| clientCertificate: String, | ||
| serviceCertificate: String, | ||
| blocker: Blocker | ||
| ): F[KafkaCredentialStore] = | ||
| ( | ||
| ClientPrivateKey.fromString(clientPrivateKey).liftTo[F], | ||
| ClientCertificate.fromString(clientCertificate).liftTo[F], | ||
| ServiceCertificate.fromString(serviceCertificate).liftTo[F] | ||
| ).tupled.flatMap { | ||
| case (clientPrivateKey, clientCertificate, serviceCertificate) => | ||
| apply[F](clientPrivateKey, clientCertificate, serviceCertificate, blocker) | ||
| } | ||
|
|
||
| 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.path.toString, | ||
| "ssl.truststore.password" -> trustStorePassword.value, | ||
| "ssl.keystore.type" -> "PKCS12", | ||
| "ssl.keystore.location" -> keyStoreFile.path.toString, | ||
| "ssl.keystore.password" -> keyStorePassword.value, | ||
| "ssl.key.password" -> keyStorePassword.value | ||
| ) | ||
|
|
||
| override final def toString: String = | ||
| s"KafkaCredentialStore($keyStoreFile, $keyStorePassword, $trustStoreFile, $trustStorePassword)" | ||
| } | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
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,28 @@ | ||
| /* | ||
| * 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 | ||
| } | ||
|
|
||
| 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 toString: String = | ||
| s"KeyStoreFile(${path.toString})" | ||
| } | ||
| } | ||
| } |
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.