-
Notifications
You must be signed in to change notification settings - Fork 56
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
+102
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
-- |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)] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
kubernetes-client/test/Kubernetes/Client/Auth/BasicSpec.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofsetHeader
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akshaymankar So the
encode
function is fromData.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 toText
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
applyBasicAuth
works for rawWAI
Requests, but here we are havingKubernetesRequest
which come from the OpenAPI generated code. I am not sure how I can use theapplyBasicAuth
here.There was a problem hiding this comment.
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 👍