@@ -26,32 +26,36 @@ module Kubernetes.Client.Config
26
26
)
27
27
where
28
28
29
- import qualified Kubernetes.OpenAPI.Core as K
29
+ import qualified Kubernetes.OpenAPI.Core as K
30
30
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
37
41
import Data.Either.Combinators
38
- import Data.Function ( (&) )
42
+ import Data.Function ( (&) )
39
43
import 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
43
46
import Data.Yaml
44
47
import Kubernetes.Client.Auth.ClientCert
45
48
import Kubernetes.Client.Auth.GCP
46
49
import Kubernetes.Client.Auth.OIDC
47
50
import Kubernetes.Client.Auth.Token
51
+ import Kubernetes.Client.Auth.TokenFile
48
52
import Kubernetes.Client.Internal.TLSUtils
49
53
import 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 )
55
59
import System.FilePath
56
60
57
61
data KubeConfigSource = KubeConfigFile FilePath
@@ -64,42 +68,44 @@ data KubeConfigSource = KubeConfigFile FilePath
64
68
token is synchronized across all the different clients being used.
65
69
-}
66
70
mkKubeClientConfig
67
- :: OIDCCache
68
- -> KubeConfigSource
69
- -> IO (NH. Manager , K. KubernetesClientConfig )
71
+ :: OIDCCache -> KubeConfigSource -> IO (NH. Manager , K. KubernetesClientConfig )
70
72
mkKubeClientConfig oidcCache (KubeConfigFile f) = do
71
73
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
74
78
tlsParams <- configureTLSParams kubeConfig (takeDirectory f)
75
79
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)
80
84
mgr <- newManager tlsParamsWithAuth
81
85
return (mgr, clientConfigWithAuth)
82
- mkKubeClientConfig _ ( KubeConfigCluster ) = mkInClusterClientConfig
86
+ mkKubeClientConfig _ KubeConfigCluster = mkInClusterClientConfig
83
87
84
88
-- | 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 )
86
91
mkInClusterClientConfig = do
87
92
caStore <- loadPEMCerts $ serviceAccountDir ++ " /ca.crt"
88
93
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
91
96
host <- liftIO $ getEnv " KUBERNETES_SERVICE_HOST"
92
97
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" ))
94
100
return (mgr, cfg)
95
101
96
102
-- | Sets the master URI in the 'K.KubernetesClientConfig'.
97
103
setMasterURI
98
- :: T. Text -- ^ Master URI
99
- -> K. KubernetesClientConfig
100
- -> K. KubernetesClientConfig
104
+ :: T. Text -- ^ Master URI
105
+ -> K. KubernetesClientConfig
106
+ -> K. KubernetesClientConfig
101
107
setMasterURI masterURI kcfg =
102
- kcfg { K. configHost = (LazyB. fromStrict . T. encodeUtf8) masterURI }
108
+ kcfg { K. configHost = (LazyB. fromStrict . T. encodeUtf8) masterURI }
103
109
104
110
-- | Creates a 'NH.Manager' that can handle TLS.
105
111
newManager :: TLS. ClientParams -> IO NH. Manager
@@ -110,55 +116,59 @@ serviceAccountDir = "/var/run/secrets/kubernetes.io/serviceaccount"
110
116
111
117
configureTLSParams :: Config -> FilePath -> IO TLS. ClientParams
112
118
configureTLSParams cfg dir = do
113
- defaultTLS <- defaultTLSClientParams
119
+ defaultTLS <- defaultTLSClientParams
114
120
withCACertData <- addCACertData cfg defaultTLS
115
121
withCACertFile <- addCACertFile cfg dir withCACertData
116
122
return $ tlsValidation cfg withCACertFile
117
123
118
124
tlsValidation :: 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
126
130
127
- addCACertData :: (MonadThrow m ) => Config -> TLS. ClientParams -> m TLS. ClientParams
131
+ addCACertData
132
+ :: (MonadThrow m ) => Config -> TLS. ClientParams -> m TLS. ClientParams
128
133
addCACertData 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
138
147
139
148
addCACertFile :: Config -> FilePath -> TLS. ClientParams -> IO TLS. ClientParams
140
149
addCACertFile 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 </> )
145
156
case eitherCertFile of
146
- Left _ -> return tlsParams
157
+ Left _ -> return tlsParams
147
158
Right certFile -> do
148
159
certText <- B. readFile certFile
149
- return
150
- $ updateClientParams tlsParams certText
151
- & (fromRight tlsParams)
160
+ return $ updateClientParams tlsParams certText & fromRight tlsParams
152
161
153
162
applyAuthSettings
154
163
:: OIDCCache
155
164
-> AuthInfo
156
165
-> (TLS. ClientParams , K. KubernetesClientConfig )
157
166
-> 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