@@ -5,7 +5,7 @@ module Kubernetes.Client.Auth.GCP
5
5
where
6
6
7
7
import Control.Concurrent.STM
8
- import Data.Bifunctor ( first )
8
+ import Data.Attoparsec.Text
9
9
import Data.Either.Combinators
10
10
import Data.Function ((&) )
11
11
import Data.JSONPath
@@ -47,15 +47,15 @@ gcpAuth AuthInfo{authProvider = Just(AuthProviderConfig "gcp" (Just cfg))} (tls,
47
47
= Just $ do
48
48
configOfErr <- parseGCPAuthInfo cfg
49
49
case configOfErr of
50
- Left e -> error $ Text. unpack e
50
+ Left e -> error e
51
51
Right gcp -> pure (tls, addAuthMethod kubecfg gcp)
52
52
gcpAuth _ _ = Nothing
53
53
54
- exceptEither :: Either Text a -> IO a
54
+ exceptEither :: Either String a -> IO a
55
55
exceptEither (Right a) = pure a
56
56
exceptEither (Left t) = error (show t)
57
57
58
- getToken :: GCPAuth -> IO (Either Text Text )
58
+ getToken :: GCPAuth -> IO (Either String Text )
59
59
getToken g@ (GCPAuth {.. }) = getCurrentToken g
60
60
>>= maybe (fetchToken g) (return . Right )
61
61
@@ -71,21 +71,20 @@ getCurrentToken (GCPAuth{..}) = do
71
71
else Nothing
72
72
73
73
-- TODO: log if parsed expiry is invalid
74
- fetchToken :: GCPAuth -> IO (Either Text Text )
74
+ fetchToken :: GCPAuth -> IO (Either String Text )
75
75
fetchToken GCPAuth {.. } = do
76
76
(stdOut, _) <- readProcess_ gcpCmd
77
77
let credsJSON = Aeson. eitherDecode stdOut
78
- & first Text. pack
79
78
token = runJSONPath gcpTokenKey =<< credsJSON
80
79
expText = runJSONPath gcpExpiryKey =<< credsJSON
81
- expiry :: Either Text (Maybe UTCTime )
80
+ expiry :: Either String (Maybe UTCTime )
82
81
expiry = Just <$> (parseExpiryTime =<< expText)
83
82
atomically $ do
84
83
writeTVar gcpAccessToken (rightToMaybe token)
85
84
writeTVar gcpTokenExpiry (either (const Nothing ) id expiry)
86
85
return token
87
86
88
- parseGCPAuthInfo :: Map Text Text -> IO (Either Text GCPAuth )
87
+ parseGCPAuthInfo :: Map Text Text -> IO (Either String GCPAuth )
89
88
parseGCPAuthInfo m = do
90
89
gcpAccessToken <- atomically $ newTVar $ Map. lookup " access-token" m
91
90
case maybe (pure Nothing ) ((Just <$> ) . parseExpiryTime) $ Map. lookup " expiry" m of
@@ -95,15 +94,23 @@ parseGCPAuthInfo m = do
95
94
return $ do
96
95
cmdPath <- Text. unpack <$> lookupEither m " cmd-path"
97
96
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" ]]
98
99
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" ]]
101
100
pure $ GCPAuth {.. }
102
101
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
104
103
lookupEither 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"
106
105
107
- parseExpiryTime :: Text -> Either Text UTCTime
106
+ parseExpiryTime :: Text -> Either String UTCTime
108
107
parseExpiryTime 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