|
| 1 | +;; Copyright © Manetu, Inc. All rights reserved |
| 2 | + |
| 3 | +(ns temporal.tls |
| 4 | + "Utilities for connecting to TLS enabled Temporal clusters" |
| 5 | + (:require [clojure.java.io :as io]) |
| 6 | + (:import [java.security KeyStore] |
| 7 | + [java.security.cert CertificateFactory X509Certificate] |
| 8 | + [javax.net.ssl TrustManagerFactory] |
| 9 | + [io.grpc.netty.shaded.io.grpc.netty GrpcSslContexts] |
| 10 | + [io.grpc.netty.shaded.io.netty.handler.ssl SslContext])) |
| 11 | + |
| 12 | +(defn- new-ca |
| 13 | + ^X509Certificate [certpath] |
| 14 | + (let [cf (CertificateFactory/getInstance "X.509")] |
| 15 | + (with-open [is (io/input-stream certpath)] |
| 16 | + (.generateCertificate cf is)))) |
| 17 | + |
| 18 | +(defn- new-keystore |
| 19 | + ^KeyStore [certpath] |
| 20 | + (let [ca (new-ca certpath) |
| 21 | + ks-type (KeyStore/getDefaultType)] |
| 22 | + (doto (KeyStore/getInstance ks-type) |
| 23 | + (.load nil nil) |
| 24 | + (.setCertificateEntry "ca" ca)))) |
| 25 | + |
| 26 | +(defn- new-trustmanagerfactory |
| 27 | + ^TrustManagerFactory [certpath] |
| 28 | + (let [alg (TrustManagerFactory/getDefaultAlgorithm) |
| 29 | + ks (new-keystore certpath)] |
| 30 | + (doto (TrustManagerFactory/getInstance alg) |
| 31 | + (.init ks)))) |
| 32 | + |
| 33 | +(defn new-ssl-context |
| 34 | + " |
| 35 | +Creates a new gRPC [SslContext](https://netty.io/4.0/api/io/netty/handler/ssl/SslContext.html) suitable for passing to the :ssl-context option of [[temporal.client.core/create-client]] |
| 36 | +
|
| 37 | +Arguments: |
| 38 | +
|
| 39 | +- `ca-path`: The path to a PEM encoded x509 Certificate Authority root certificate for validating the Temporal server. |
| 40 | +- `cert-path`: The path to a PEM encoded x509 Certificate representing this client's identity, used for mutual TLS authentication. |
| 41 | +- 'key-path': The path to a PEM encoded private key representing this client's identity, used for mutual TLS authentication. |
| 42 | +
|
| 43 | +" |
| 44 | + ^SslContext [{:keys [ca-path cert-path key-path] :as args}] |
| 45 | + (-> (GrpcSslContexts/forClient) |
| 46 | + (cond-> |
| 47 | + (some? ca-path) (.trustManager (new-trustmanagerfactory ca-path)) |
| 48 | + (and (some? cert-path) (some? key-path)) (.keyManager (io/file cert-path) (io/file key-path))) |
| 49 | + (.build))) |
0 commit comments