Skip to content

Commit 314149f

Browse files
committed
Implement basic authentication
1 parent f89f1ae commit 314149f

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
module Kubernetes.Client.Auth.Basic where
4+
5+
import Data.ByteString.Base64 ( encode )
6+
import Data.Function ( (&) )
7+
import Data.Monoid ( (<>) )
8+
import Data.Text ( Text )
9+
import Kubernetes.Client.Auth.Internal.Types
10+
import Kubernetes.OpenAPI.Core
11+
import Kubernetes.Client.KubeConfig
12+
13+
import qualified Data.Text.Encoding as T
14+
import qualified Lens.Micro as L
15+
16+
17+
data BasicAuth = BasicAuth { basicAuthUsername :: Text
18+
, basicAuthPassword :: Text
19+
}
20+
21+
instance AuthMethod BasicAuth where
22+
applyAuthMethod _ BasicAuth{..} req =
23+
pure
24+
$ req
25+
`setHeader` toHeader ("authorization", "Basic " <> encodeBasicAuth)
26+
& L.set rAuthTypesL []
27+
where
28+
encodeBasicAuth = T.decodeUtf8 $ encode $ T.encodeUtf8 $ basicAuthUsername <> ":" <> basicAuthPassword
29+
30+
-- |Detects if username and password is specified in AuthConfig, if it is configures 'KubernetesClientConfig' with 'BasicAuth'
31+
basicAuth :: DetectAuth
32+
basicAuth auth (tlsParams, cfg) = do
33+
u <- username auth
34+
p <- password auth
35+
return $ return (tlsParams, setBasicAuth u p cfg)
36+
37+
-- |Configures the 'KubernetesClientConfig' to use basic authentication.
38+
setBasicAuth
39+
:: Text -- ^Username
40+
-> Text -- ^Password
41+
-> KubernetesClientConfig
42+
-> KubernetesClientConfig
43+
setBasicAuth u p kcfg = kcfg
44+
{ configAuthMethods = [AnyAuthMethod (BasicAuth u p)]
45+
}

kubernetes-client/src/Kubernetes/Client/Config.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Data.Maybe
4444
import qualified Data.Text as T
4545
import qualified Data.Text.Encoding as T
4646
import Data.Yaml
47+
import Kubernetes.Client.Auth.Basic
4748
import Kubernetes.Client.Auth.ClientCert
4849
import Kubernetes.Client.Auth.GCP
4950
import Kubernetes.Client.Auth.OIDC
@@ -172,3 +173,4 @@ applyAuthSettings oidcCache auth input =
172173
<|> tokenFileAuth auth input
173174
<|> gcpAuth auth input
174175
<|> cachedOIDCAuth oidcCache auth input
176+
<|> basicAuth auth input

0 commit comments

Comments
 (0)