@@ -5,7 +5,7 @@ module Kubernetes.Client.Auth.GCP
55where
66
77import Control.Concurrent.STM
8- import Data.Bifunctor ( first )
8+ import Data.Attoparsec.Text
99import Data.Either.Combinators
1010import Data.Function ((&) )
1111import Data.JSONPath
@@ -47,15 +47,15 @@ gcpAuth AuthInfo{authProvider = Just(AuthProviderConfig "gcp" (Just cfg))} (tls,
4747 = Just $ do
4848 configOfErr <- parseGCPAuthInfo cfg
4949 case configOfErr of
50- Left e -> error $ Text. unpack e
50+ Left e -> error e
5151 Right gcp -> pure (tls, addAuthMethod kubecfg gcp)
5252gcpAuth _ _ = Nothing
5353
54- exceptEither :: Either Text a -> IO a
54+ exceptEither :: Either String a -> IO a
5555exceptEither (Right a) = pure a
5656exceptEither (Left t) = error (show t)
5757
58- getToken :: GCPAuth -> IO (Either Text Text )
58+ getToken :: GCPAuth -> IO (Either String Text )
5959getToken g@ (GCPAuth {.. }) = getCurrentToken g
6060 >>= maybe (fetchToken g) (return . Right )
6161
@@ -71,21 +71,20 @@ getCurrentToken (GCPAuth{..}) = do
7171 else Nothing
7272
7373-- TODO: log if parsed expiry is invalid
74- fetchToken :: GCPAuth -> IO (Either Text Text )
74+ fetchToken :: GCPAuth -> IO (Either String Text )
7575fetchToken GCPAuth {.. } = do
7676 (stdOut, _) <- readProcess_ gcpCmd
7777 let credsJSON = Aeson. eitherDecode stdOut
78- & first Text. pack
7978 token = runJSONPath gcpTokenKey =<< credsJSON
8079 expText = runJSONPath gcpExpiryKey =<< credsJSON
81- expiry :: Either Text (Maybe UTCTime )
80+ expiry :: Either String (Maybe UTCTime )
8281 expiry = Just <$> (parseExpiryTime =<< expText)
8382 atomically $ do
8483 writeTVar gcpAccessToken (rightToMaybe token)
8584 writeTVar gcpTokenExpiry (either (const Nothing ) id expiry)
8685 return token
8786
88- parseGCPAuthInfo :: Map Text Text -> IO (Either Text GCPAuth )
87+ parseGCPAuthInfo :: Map Text Text -> IO (Either String GCPAuth )
8988parseGCPAuthInfo m = do
9089 gcpAccessToken <- atomically $ newTVar $ Map. lookup " access-token" m
9190 case maybe (pure Nothing ) ((Just <$> ) . parseExpiryTime) $ Map. lookup " expiry" m of
@@ -95,15 +94,23 @@ parseGCPAuthInfo m = do
9594 return $ do
9695 cmdPath <- Text. unpack <$> lookupEither m " cmd-path"
9796 cmdArgs <- Text. splitOn " " <$> lookupEither m " cmd-args"
97+ gcpTokenKey <- readJSONPath m " token-key" [JSONPath [KeyChild " token_expiry" ]]
98+ gcpExpiryKey <- readJSONPath m " expiry-key" [JSONPath [KeyChild " access_token" ]]
9899 let gcpCmd = proc cmdPath (map Text. unpack cmdArgs)
99- gcpTokenKey = readJSONPath m " token-key" [JSONPath [KeyChild " token_expiry" ]]
100- gcpExpiryKey = readJSONPath m " expiry-key" [JSONPath [KeyChild " access_token" ]]
101100 pure $ GCPAuth {.. }
102101
103- lookupEither :: (Show key , Ord key ) => Map key val -> key -> Either Text val
102+ lookupEither :: (Show key , Ord key ) => Map key val -> key -> Either String val
104103lookupEither m k = maybeToRight e $ Map. lookup k m
105- where e = " Couldn't find key: " <> ( Text. pack $ show k) <> " in GCP auth info"
104+ where e = " Couldn't find key: " <> show k <> " in GCP auth info"
106105
107- parseExpiryTime :: Text -> Either Text UTCTime
106+ parseExpiryTime :: Text -> Either String UTCTime
108107parseExpiryTime s = zonedTimeToUTC <$> parseTimeRFC3339 s
109- & maybeToRight (" failed to parse token expiry time " <> s)
108+ & maybeToRight (" failed to parse token expiry time " <> Text. unpack s)
109+
110+ readJSONPath :: Map Text Text
111+ -> Text
112+ -> [K8sPathElement ]
113+ -> Either String [K8sPathElement ]
114+ readJSONPath m key def = case Map. lookup key m of
115+ Nothing -> pure def
116+ Just str -> parseOnly (k8sJSONPath <* endOfInput) str
0 commit comments