Skip to content

Commit 12b071f

Browse files
Merge pull request #16 from greenrd/clienthelper-use-lenses
Use lenses in client-helper to make functions shorter
2 parents edc8c3a + c92c8da commit 12b071f

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

kubernetes-client-helper/package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies:
1515
- x509-validation
1616
- http-client >=0.5 && <0.6
1717
- http-client-tls
18+
- microlens >= 0.4.3 && <0.5
1819
- bytestring >=0.10.0 && <0.11
1920
- text >=0.11 && <1.3
2021
- safe-exceptions <0.2

kubernetes-client-helper/src/Kubernetes/ClientHelper.hs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import Data.Typeable (Typeable)
1818
import Data.X509 (SignedCertificate,
1919
decodeSignedCertificate)
2020
import qualified Data.X509 as X509
21-
import Data.X509.CertificateStore (makeCertificateStore)
21+
import Data.X509.CertificateStore (CertificateStore, makeCertificateStore)
2222
import qualified Data.X509.Validation as X509
23+
import Lens.Micro (Lens', lens, set)
2324
import Network.Connection (TLSSettings (..))
2425
import qualified Network.HTTP.Client as NH
2526
import Network.HTTP.Client.TLS (mkManagerSettings)
@@ -67,25 +68,22 @@ defaultTLSClientParams = do
6768
}
6869
}
6970

71+
clientHooksL :: Lens' TLS.ClientParams TLS.ClientHooks
72+
clientHooksL = lens TLS.clientHooks (\cp ch -> cp { TLS.clientHooks = ch })
73+
74+
onServerCertificateL :: Lens' TLS.ClientParams (CertificateStore -> TLS.ValidationCache -> X509.ServiceID -> X509.CertificateChain -> IO [X509.FailedReason])
75+
onServerCertificateL =
76+
clientHooksL . lens TLS.onServerCertificate (\ch osc -> ch { TLS.onServerCertificate = osc })
77+
7078
-- |Don't check whether the cert presented by the server matches the name of the server you are connecting to.
7179
-- This is necessary if you specify the server host by its IP address.
7280
disableServerNameValidation :: TLS.ClientParams -> TLS.ClientParams
73-
disableServerNameValidation cp = cp
74-
{ TLS.clientHooks = (TLS.clientHooks cp)
75-
{ TLS.onServerCertificate = X509.validate
76-
X509.HashSHA256
77-
def
78-
def { X509.checkFQHN = False }
79-
}
80-
}
81+
disableServerNameValidation =
82+
set onServerCertificateL (X509.validate X509.HashSHA256 def (def { X509.checkFQHN = False }))
8183

8284
-- |Insecure mode. The client will not validate the server cert at all.
8385
disableServerCertValidation :: TLS.ClientParams -> TLS.ClientParams
84-
disableServerCertValidation cp = cp
85-
{ TLS.clientHooks = (TLS.clientHooks cp)
86-
{ TLS.onServerCertificate = (\_ _ _ _ -> return [])
87-
}
88-
}
86+
disableServerCertValidation = set onServerCertificateL (\_ _ _ _ -> return [])
8987

9088
-- |Use a custom CA store.
9189
setCAStore :: [SignedCertificate] -> TLS.ClientParams -> TLS.ClientParams
@@ -95,13 +93,13 @@ setCAStore certs cp = cp
9593
}
9694
}
9795

96+
onCertificateRequestL :: Lens' TLS.ClientParams (([TLS.CertificateType], Maybe [TLS.HashAndSignatureAlgorithm], [X509.DistinguishedName]) -> IO (Maybe (X509.CertificateChain, TLS.PrivKey)))
97+
onCertificateRequestL =
98+
clientHooksL . lens TLS.onCertificateRequest (\ch ocr -> ch { TLS.onCertificateRequest = ocr })
99+
98100
-- |Use a client cert for authentication.
99101
setClientCert :: Credential -> TLS.ClientParams -> TLS.ClientParams
100-
setClientCert cred cp = cp
101-
{ TLS.clientHooks = (TLS.clientHooks cp)
102-
{ TLS.onCertificateRequest = (\_ -> return (Just cred))
103-
}
104-
}
102+
setClientCert cred = set onCertificateRequestL (\_ -> return $ Just cred)
105103

106104
-- |Parses a PEM-encoded @ByteString@ into a list of certificates.
107105
parsePEMCerts :: B.ByteString -> Either String [SignedCertificate]

0 commit comments

Comments
 (0)