@@ -26,32 +26,36 @@ module Kubernetes.Client.Config
2626 )
2727where
2828
29- import qualified Kubernetes.OpenAPI.Core as K
29+ import qualified Kubernetes.OpenAPI.Core as K
3030
31- import Control.Applicative ((<|>) )
32- import Control.Exception.Safe (MonadThrow , throwM )
33- import Control.Monad.IO.Class (MonadIO , liftIO )
34- import qualified Data.ByteString as B
35- import qualified Data.ByteString.Base64 as B64
36- import qualified Data.ByteString.Lazy as LazyB
31+ import Control.Applicative ( (<|>) )
32+ import Control.Exception.Safe ( MonadThrow
33+ , throwM
34+ )
35+ import Control.Monad.IO.Class ( MonadIO
36+ , liftIO
37+ )
38+ import qualified Data.ByteString as B
39+ import qualified Data.ByteString.Base64 as B64
40+ import qualified Data.ByteString.Lazy as LazyB
3741import Data.Either.Combinators
38- import Data.Function ( (&) )
42+ import Data.Function ( (&) )
3943import Data.Maybe
40- import qualified Data.Text as T
41- import qualified Data.Text.Encoding as T
42- import qualified Data.Text.IO as T
44+ import qualified Data.Text as T
45+ import qualified Data.Text.Encoding as T
4346import Data.Yaml
4447import Kubernetes.Client.Auth.ClientCert
4548import Kubernetes.Client.Auth.GCP
4649import Kubernetes.Client.Auth.OIDC
4750import Kubernetes.Client.Auth.Token
51+ import Kubernetes.Client.Auth.TokenFile
4852import Kubernetes.Client.Internal.TLSUtils
4953import Kubernetes.Client.KubeConfig
50- import Network.Connection ( TLSSettings (.. ))
51- import qualified Network.HTTP.Client as NH
52- import Network.HTTP.Client.TLS ( mkManagerSettings )
53- import qualified Network.TLS as TLS
54- import System.Environment ( getEnv )
54+ import Network.Connection ( TLSSettings (.. ) )
55+ import qualified Network.HTTP.Client as NH
56+ import Network.HTTP.Client.TLS ( mkManagerSettings )
57+ import qualified Network.TLS as TLS
58+ import System.Environment ( getEnv )
5559import System.FilePath
5660
5761data KubeConfigSource = KubeConfigFile FilePath
@@ -64,42 +68,44 @@ data KubeConfigSource = KubeConfigFile FilePath
6468 token is synchronized across all the different clients being used.
6569-}
6670mkKubeClientConfig
67- :: OIDCCache
68- -> KubeConfigSource
69- -> IO (NH. Manager , K. KubernetesClientConfig )
71+ :: OIDCCache -> KubeConfigSource -> IO (NH. Manager , K. KubernetesClientConfig )
7072mkKubeClientConfig oidcCache (KubeConfigFile f) = do
7173 kubeConfig <- decodeFileThrow f
72- masterURI <- server <$> getCluster kubeConfig
73- & either (const $ pure " localhost:8080" ) return
74+ masterURI <-
75+ server
76+ <$> getCluster kubeConfig
77+ & either (const $ pure " localhost:8080" ) return
7478 tlsParams <- configureTLSParams kubeConfig (takeDirectory f)
7579 clientConfig <- K. newConfig & fmap (setMasterURI masterURI)
76- (tlsParamsWithAuth, clientConfigWithAuth) <-
77- case getAuthInfo kubeConfig of
78- Left _ -> return (tlsParams,clientConfig)
79- Right (_, auth) -> applyAuthSettings oidcCache auth (tlsParams, clientConfig)
80+ (tlsParamsWithAuth, clientConfigWithAuth) <- case getAuthInfo kubeConfig of
81+ Left _ -> return (tlsParams, clientConfig)
82+ Right (_, auth) ->
83+ applyAuthSettings oidcCache auth (tlsParams, clientConfig)
8084 mgr <- newManager tlsParamsWithAuth
8185 return (mgr, clientConfigWithAuth)
82- mkKubeClientConfig _ ( KubeConfigCluster ) = mkInClusterClientConfig
86+ mkKubeClientConfig _ KubeConfigCluster = mkInClusterClientConfig
8387
8488-- | Creates 'NH.Manager' and 'K.KubernetesClientConfig' assuming it is being executed in a pod
85- mkInClusterClientConfig :: (MonadIO m , MonadThrow m ) => m (NH. Manager , K. KubernetesClientConfig )
89+ mkInClusterClientConfig
90+ :: (MonadIO m , MonadThrow m ) => m (NH. Manager , K. KubernetesClientConfig )
8691mkInClusterClientConfig = do
8792 caStore <- loadPEMCerts $ serviceAccountDir ++ " /ca.crt"
8893 defTlsParams <- liftIO defaultTLSClientParams
89- mgr <- liftIO . newManager . setCAStore caStore $ disableServerNameValidation defTlsParams
90- tok <- liftIO . T. readFile $ serviceAccountDir ++ " /token "
94+ mgr <- liftIO . newManager . setCAStore caStore $ disableServerNameValidation
95+ defTlsParams
9196 host <- liftIO $ getEnv " KUBERNETES_SERVICE_HOST"
9297 port <- liftIO $ getEnv " KUBERNETES_SERVICE_PORT"
93- cfg <- setTokenAuth tok . setMasterURI (T. pack $ " https://" ++ host ++ " :" ++ port) <$> liftIO K. newConfig
98+ cfg <- setMasterURI (T. pack $ " https://" ++ host ++ " :" ++ port) <$> liftIO
99+ (K. newConfig >>= setTokenFileAuth (serviceAccountDir ++ " /token" ))
94100 return (mgr, cfg)
95101
96102-- | Sets the master URI in the 'K.KubernetesClientConfig'.
97103setMasterURI
98- :: T. Text -- ^ Master URI
99- -> K. KubernetesClientConfig
100- -> K. KubernetesClientConfig
104+ :: T. Text -- ^ Master URI
105+ -> K. KubernetesClientConfig
106+ -> K. KubernetesClientConfig
101107setMasterURI masterURI kcfg =
102- kcfg { K. configHost = (LazyB. fromStrict . T. encodeUtf8) masterURI }
108+ kcfg { K. configHost = (LazyB. fromStrict . T. encodeUtf8) masterURI }
103109
104110-- | Creates a 'NH.Manager' that can handle TLS.
105111newManager :: TLS. ClientParams -> IO NH. Manager
@@ -110,55 +116,59 @@ serviceAccountDir = "/var/run/secrets/kubernetes.io/serviceaccount"
110116
111117configureTLSParams :: Config -> FilePath -> IO TLS. ClientParams
112118configureTLSParams cfg dir = do
113- defaultTLS <- defaultTLSClientParams
119+ defaultTLS <- defaultTLSClientParams
114120 withCACertData <- addCACertData cfg defaultTLS
115121 withCACertFile <- addCACertFile cfg dir withCACertData
116122 return $ tlsValidation cfg withCACertFile
117123
118124tlsValidation :: Config -> TLS. ClientParams -> TLS. ClientParams
119- tlsValidation cfg tlsParams =
120- case getCluster cfg of
121- Left _ -> tlsParams
122- Right c ->
123- case insecureSkipTLSVerify c of
124- Just True -> disableServerCertValidation tlsParams
125- _ -> tlsParams
125+ tlsValidation cfg tlsParams = case getCluster cfg of
126+ Left _ -> tlsParams
127+ Right c -> case insecureSkipTLSVerify c of
128+ Just True -> disableServerCertValidation tlsParams
129+ _ -> tlsParams
126130
127- addCACertData :: (MonadThrow m ) => Config -> TLS. ClientParams -> m TLS. ClientParams
131+ addCACertData
132+ :: (MonadThrow m ) => Config -> TLS. ClientParams -> m TLS. ClientParams
128133addCACertData cfg tlsParams =
129- let eitherCertText = getCluster cfg
130- & (>>= (maybeToRight " cert data not provided" . certificateAuthorityData))
131- in case eitherCertText of
132- Left _ -> pure tlsParams
133- Right certBase64 -> do
134- certText <- B64. decode (T. encodeUtf8 certBase64)
135- & either (throwM . Base64ParsingFailed ) pure
136- updateClientParams tlsParams certText
137- & either throwM return
134+ let
135+ eitherCertText =
136+ getCluster cfg
137+ & (>>= (maybeToRight " cert data not provided" . certificateAuthorityData
138+ )
139+ )
140+ in case eitherCertText of
141+ Left _ -> pure tlsParams
142+ Right certBase64 -> do
143+ certText <-
144+ B64. decode (T. encodeUtf8 certBase64)
145+ & either (throwM . Base64ParsingFailed ) pure
146+ updateClientParams tlsParams certText & either throwM return
138147
139148addCACertFile :: Config -> FilePath -> TLS. ClientParams -> IO TLS. ClientParams
140149addCACertFile cfg dir tlsParams = do
141- let eitherCertFile = getCluster cfg
142- >>= maybeToRight " cert file not provided" . certificateAuthority
143- & fmap T. unpack
144- & fmap (dir </> )
150+ let eitherCertFile =
151+ getCluster cfg
152+ >>= maybeToRight " cert file not provided"
153+ . certificateAuthority
154+ & fmap T. unpack
155+ & fmap (dir </> )
145156 case eitherCertFile of
146- Left _ -> return tlsParams
157+ Left _ -> return tlsParams
147158 Right certFile -> do
148159 certText <- B. readFile certFile
149- return
150- $ updateClientParams tlsParams certText
151- & (fromRight tlsParams)
160+ return $ updateClientParams tlsParams certText & fromRight tlsParams
152161
153162applyAuthSettings
154163 :: OIDCCache
155164 -> AuthInfo
156165 -> (TLS. ClientParams , K. KubernetesClientConfig )
157166 -> IO (TLS. ClientParams , K. KubernetesClientConfig )
158- applyAuthSettings oidcCache auth input = fromMaybe (pure input)
159- $ clientCertFileAuth auth input
160- <|> clientCertDataAuth auth input
161- <|> tokenAuth auth input
162- <|> tokenFileAuth auth input
163- <|> gcpAuth auth input
164- <|> cachedOIDCAuth oidcCache auth input
167+ applyAuthSettings oidcCache auth input =
168+ fromMaybe (pure input)
169+ $ clientCertFileAuth auth input
170+ <|> clientCertDataAuth auth input
171+ <|> tokenAuth auth input
172+ <|> tokenFileAuth auth input
173+ <|> gcpAuth auth input
174+ <|> cachedOIDCAuth oidcCache auth input
0 commit comments