Skip to content

Implement basic authentication #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions kubernetes-client/src/Kubernetes/Client/Auth/Basic.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Kubernetes.Client.Auth.Basic where

import Data.ByteString.Base64 ( encode )
import Data.Function ( (&) )
import Data.Monoid ( (<>) )
import Data.Text ( Text )
import Kubernetes.Client.Auth.Internal.Types
import Kubernetes.OpenAPI.Core
import Kubernetes.Client.KubeConfig

import qualified Data.Text.Encoding as T
import qualified Lens.Micro as L


data BasicAuth = BasicAuth { basicAuthUsername :: Text
, basicAuthPassword :: Text
}

instance AuthMethod BasicAuth where
applyAuthMethod _ BasicAuth{..} req =
pure
$ req
`setHeader` toHeader ("authorization", "Basic " <> encodeBasicAuth)
& L.set rAuthTypesL []
where
encodeBasicAuth = T.decodeUtf8 $ encode $ T.encodeUtf8 $ basicAuthUsername <> ":" <> basicAuthPassword
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be base64 encoded (at least according to RFC 7617. It is better to ues applyBasicAuth function from http-client instead of setHeader.

Copy link
Contributor Author

@frincon frincon Jun 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be base64 encoded (at least according to RFC 7617. It is better to ues applyBasicAuth function from http-client instead of setHeader.

@akshaymankar So the encode function is from Data.ByteString.Base64 so this encoding-decoding stuff what it does is Text -> ByteString -> Encode to base 64 -> Text. I understand it is quite ugly but it is encoding the username and password as base64.

I opted for converting to ByteString then encode, and then convert to Text again because the library base64-bytestring is already imported and did not want to introduce another library for just doing the base64 encoding.

Will do the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The applyBasicAuth works for raw WAI Requests, but here we are having KubernetesRequest which come from the OpenAPI generated code. I am not sure how I can use the applyBasicAuth here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I totally missed encode in all of this. I think this ok then 👍


-- |Detects if username and password is specified in AuthConfig, if it is configures 'KubernetesClientConfig' with 'BasicAuth'
basicAuth :: DetectAuth
basicAuth auth (tlsParams, cfg) = do
u <- username auth
p <- password auth
return $ return (tlsParams, setBasicAuth u p cfg)

-- |Configures the 'KubernetesClientConfig' to use basic authentication.
setBasicAuth
:: Text -- ^Username
-> Text -- ^Password
-> KubernetesClientConfig
-> KubernetesClientConfig
setBasicAuth u p kcfg = kcfg
{ configAuthMethods = [AnyAuthMethod (BasicAuth u p)]
}
2 changes: 2 additions & 0 deletions kubernetes-client/src/Kubernetes/Client/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import Data.Maybe
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Yaml
import Kubernetes.Client.Auth.Basic
import Kubernetes.Client.Auth.ClientCert
import Kubernetes.Client.Auth.GCP
import Kubernetes.Client.Auth.OIDC
Expand Down Expand Up @@ -172,3 +173,4 @@ applyAuthSettings oidcCache auth input =
<|> tokenFileAuth auth input
<|> gcpAuth auth input
<|> cachedOIDCAuth oidcCache auth input
<|> basicAuth auth input
55 changes: 55 additions & 0 deletions kubernetes-client/test/Kubernetes/Client/Auth/BasicSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{-# LANGUAGE OverloadedStrings #-}
module Kubernetes.Client.Auth.BasicSpec where

import Test.Hspec
import Data.Typeable
import Data.Maybe ( isJust
, isNothing
, fromJust
)
import Kubernetes.Client.Auth.Basic
import Kubernetes.Client.KubeConfig
import Kubernetes.OpenAPI
import Network.TLS ( defaultParamsClient )

emptyAuthInfo :: AuthInfo
emptyAuthInfo = AuthInfo Nothing
Nothing
Nothing
Nothing
Nothing
Nothing
Nothing
Nothing
Nothing
Nothing
Nothing
Nothing

spec :: Spec
spec = do
let testTLSParams = defaultParamsClient "" ""
testUsername = Just "testuser"
testPassword = Just "testpassword"
basicAuthInfo = emptyAuthInfo { username = testUsername, password = testPassword}
describe "Basic Authentication" $ do
it "should return Nothing if the username an d/or password is not provided" $ do
testConfig <- newConfig
isNothing (basicAuth emptyAuthInfo (testTLSParams, testConfig))
`shouldBe` True
isNothing (basicAuth emptyAuthInfo { username = testUsername} (testTLSParams, testConfig))
`shouldBe` True
isNothing (basicAuth emptyAuthInfo { password = testUsername} (testTLSParams, testConfig))
`shouldBe` True

context "when username and password are provided" $ do
it "should return a configuration provider io" $ do
testConfig <- newConfig
isJust (basicAuth basicAuthInfo (testTLSParams, testConfig)) `shouldBe` True

it "should configure basic auth" $ do
testConfig <- newConfig
(_, (KubernetesClientConfig { configAuthMethods = AnyAuthMethod (a) : as })) <-
fromJust $ basicAuth basicAuthInfo (testTLSParams, testConfig)
null as `shouldBe` True
isJust (cast a :: Maybe BasicAuth) `shouldBe` True