-
Notifications
You must be signed in to change notification settings - Fork 101
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 #530 from agustafson/certificate-loading
- Loading branch information
Showing
6 changed files
with
134 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
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 fs2.kafka._ | ||
import fs2.kafka.security._ | ||
|
||
def createKafkaProducerUsingPem[F[_]: Sync, K, V]( | ||
caCertificate: String, | ||
accessKey: String, | ||
accessCertificate: String | ||
)(implicit keySer: Serializer[F, K], valSer: Serializer[F, V]): ProducerSettings[F, K, V] = | ||
ProducerSettings[F, K, V] | ||
.withCredentials( | ||
KafkaCredentialStore.fromPemStrings( | ||
caCertificate, | ||
accessKey, | ||
accessCertificate | ||
) | ||
) | ||
``` |
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
30 changes: 30 additions & 0 deletions
30
modules/core/src/main/scala/fs2/kafka/security/KafkaCredentialStore.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,30 @@ | ||
/* | ||
* Copyright 2018-2021 OVO Energy Limited | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package fs2.kafka.security | ||
|
||
sealed trait KafkaCredentialStore { | ||
def properties: Map[String, String] | ||
} | ||
|
||
object KafkaCredentialStore { | ||
final def fromPemStrings( | ||
caCertificate: String, | ||
clientPrivateKey: String, | ||
clientCertificate: String | ||
): KafkaCredentialStore = | ||
new KafkaCredentialStore { | ||
override val properties: Map[String, String] = | ||
Map( | ||
"security.protocol" -> "SSL", | ||
"ssl.truststore.type" -> "PEM", | ||
"ssl.truststore.certificates" -> caCertificate.replace("\n", ""), | ||
"ssl.keystore.type" -> "PEM", | ||
"ssl.keystore.key" -> clientPrivateKey.replace("\n", ""), | ||
"ssl.keystore.certificate.chain" -> clientCertificate.replace("\n", "") | ||
) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
modules/core/src/test/scala/fs2/kafka/security/KafkaCredentialStoreSpec.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,48 @@ | ||
package fs2.kafka.security | ||
|
||
import fs2.kafka.BaseSpec | ||
|
||
final class KafkaCredentialStoreSpec extends BaseSpec { | ||
describe("KafkaCredentialStore") { | ||
describe("fromPemStrigs") { | ||
it("should create a KafkaCredentialStore with the expected properties") { | ||
val caCert = | ||
""" | ||
|-----BEGIN CERTIFICATE----- | ||
|RmFrZSBDQSBjZXJ0aWZpY2F0ZSBGYWtlIENBIGNlcnRpZmljYXRlIEZha2UgQ0EgY2VydGlmaWNh | ||
|dGUgRmFrZSBDQSBjZXJ0aWZpY2F0ZQ== | ||
|-----END CERTIFICATE----- | ||
|""".stripMargin | ||
|
||
val privateKey = | ||
""" | ||
|-----BEGIN PRIVATE KEY----- | ||
|RmFrZSBwcml2YXRlIGtleSBGYWtlIHByaXZhdGUga2V5IEZha2UgcHJpdmF0ZSBrZXkgRmFrZSBw | ||
|cml2YXRlIGtleSBGYWtlIHByaXZhdGUga2V5IA== | ||
|-----END PRIVATE KEY----- | ||
|""".stripMargin | ||
|
||
val clientCert = | ||
""" | ||
|-----BEGIN CERTIFICATE----- | ||
|RmFrZSBjbGllbnQgY2VydCBGYWtlIGNsaWVudCBjZXJ0IEZha2UgY2xpZW50IGNlcnQgRmFrZSBj | ||
|bGllbnQgY2VydCBGYWtlIGNsaWVudCBjZXJ0IA== | ||
|-----END CERTIFICATE----- | ||
|""".stripMargin | ||
|
||
val store = KafkaCredentialStore.fromPemStrings(caCert, privateKey, clientCert) | ||
|
||
assert( | ||
store.properties === Map( | ||
"security.protocol" -> "SSL", | ||
"ssl.truststore.type" -> "PEM", | ||
"ssl.truststore.certificates" -> "-----BEGIN CERTIFICATE-----RmFrZSBDQSBjZXJ0aWZpY2F0ZSBGYWtlIENBIGNlcnRpZmljYXRlIEZha2UgQ0EgY2VydGlmaWNhdGUgRmFrZSBDQSBjZXJ0aWZpY2F0ZQ==-----END CERTIFICATE-----", | ||
"ssl.keystore.type" -> "PEM", | ||
"ssl.keystore.key" -> "-----BEGIN PRIVATE KEY-----RmFrZSBwcml2YXRlIGtleSBGYWtlIHByaXZhdGUga2V5IEZha2UgcHJpdmF0ZSBrZXkgRmFrZSBwcml2YXRlIGtleSBGYWtlIHByaXZhdGUga2V5IA==-----END PRIVATE KEY-----", | ||
"ssl.keystore.certificate.chain" -> "-----BEGIN CERTIFICATE-----RmFrZSBjbGllbnQgY2VydCBGYWtlIGNsaWVudCBjZXJ0IEZha2UgY2xpZW50IGNlcnQgRmFrZSBjbGllbnQgY2VydCBGYWtlIGNsaWVudCBjZXJ0IA==-----END CERTIFICATE-----" | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |