Skip to content

Commit b6457ec

Browse files
committed
Use lenses in client-helper to make functions shorter
1 parent 38f64fc commit b6457ec

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
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: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Data.X509 (SignedCertificate,
2020
import qualified Data.X509 as X509
2121
import Data.X509.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,21 @@ 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 =
75+
clientHooksL . lens TLS.onServerCertificate (\ch osc -> ch { TLS.onServerCertificate = osc })
76+
7077
-- |Don't check whether the cert presented by the server matches the name of the server you are connecting to.
7178
-- This is necessary if you specify the server host by its IP address.
7279
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-
}
80+
disableServerNameValidation =
81+
set onServerCertificateL (X509.validate X509.HashSHA256 def (def { X509.checkFQHN = False }))
8182

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

9087
-- |Use a custom CA store.
9188
setCAStore :: [SignedCertificate] -> TLS.ClientParams -> TLS.ClientParams
@@ -95,13 +92,12 @@ setCAStore certs cp = cp
9592
}
9693
}
9794

95+
onCertificateRequestL =
96+
clientHooksL . lens TLS.onCertificateRequest (\ch ocr -> ch { TLS.onCertificateRequest = ocr })
97+
9898
-- |Use a client cert for authentication.
9999
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-
}
100+
setClientCert cred = set onCertificateRequestL (\_ -> return $ Just cred)
105101

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

0 commit comments

Comments
 (0)