@@ -59,7 +59,8 @@ data OIDCGetTokenException = OIDCOAuthException (OAuth2Error OAuth2TokenRequest.
59
59
deriving Show
60
60
instance Exception OIDCGetTokenException
61
61
62
- data OIDCAuthParsingException = OIDCAuthParsingException String
62
+ data OIDCAuthParsingException = OIDCAuthCAParsingFailed ParseCertException
63
+ | OIDCAuthMissingInformation String
63
64
deriving Show
64
65
instance Exception OIDCAuthParsingException
65
66
@@ -119,7 +120,7 @@ oidcAuth :: DetectAuth
119
120
oidcAuth AuthInfo {authProvider = Just (AuthProviderConfig " oidc" (Just cfg))} (tls, kubecfg)
120
121
= Just
121
122
$ parseOIDCAuthInfo cfg
122
- >>= either ( throwM . OIDCAuthParsingException ) (\ oidc -> pure (tls, addAuthMethod kubecfg oidc))
123
+ >>= either throwM (\ oidc -> pure (tls, addAuthMethod kubecfg oidc))
123
124
oidcAuth _ _ = Nothing
124
125
125
126
-- TODO: Consider doing this whole function atomically, as two threads may miss the cache simultaneously
@@ -129,54 +130,53 @@ oidcAuth _ _ = Nothing
129
130
-}
130
131
cachedOIDCAuth :: OIDCCache -> DetectAuth
131
132
cachedOIDCAuth cache AuthInfo {authProvider = Just (AuthProviderConfig " oidc" (Just cfg))} (tls, kubecfg) = Just $ do
132
- m <- readTVarIO cache
133
- o <- case findInCache m cfg of
134
- Left e -> throwM $ OIDCAuthParsingException e
135
- Right (Just o) -> return o
136
- Right Nothing -> do
137
- o@ (OIDCAuth {.. }) <- parseOIDCAuthInfo cfg
138
- >>= either (throwM . OIDCAuthParsingException ) pure
139
- let newCache = Map. insert (issuerURL, clientID) o m
133
+ latestCache <- readTVarIO cache
134
+ issuerURL <- lookupOrThrow " idp-issuer-url"
135
+ clientID <- lookupOrThrow " client-id"
136
+ case Map. lookup (issuerURL, clientID) latestCache of
137
+ Just cacheHit -> return $ newTLSAndAuth cacheHit
138
+ Nothing -> do
139
+ parsedAuth <- parseOIDCAuthInfo cfg
140
+ >>= either throwM pure
141
+ let newCache = Map. insert (issuerURL, clientID) parsedAuth latestCache
140
142
_ <- atomically $ swapTVar cache newCache
141
- return o
142
- pure (tls, addAuthMethod kubecfg o)
143
+ return $ newTLSAndAuth parsedAuth
144
+ where lookupOrThrow k = Map. lookup k cfg
145
+ & maybe (throwM $ OIDCAuthMissingInformation $ Text. unpack k) pure
146
+ newTLSAndAuth auth = (tls, addAuthMethod kubecfg auth)
143
147
cachedOIDCAuth _ _ _ = Nothing
144
148
145
- findInCache :: Map (Text , Text ) a -> Map Text Text -> Either String (Maybe a )
146
- findInCache cache cfg = do
147
- issuerURL <- lookupEither cfg " idp-issuer-url"
148
- clientID <- lookupEither cfg " client-id"
149
- return $ Map. lookup (issuerURL, clientID) cache
150
-
151
- parseOIDCAuthInfo :: Map Text Text -> IO (Either String OIDCAuth )
149
+ parseOIDCAuthInfo :: Map Text Text -> IO (Either OIDCAuthParsingException OIDCAuth )
152
150
parseOIDCAuthInfo m = do
153
151
eitherTLSParams <- parseCA m
154
152
idTokenTVar <- atomically $ newTVar $ Map. lookup " id-token" m
155
153
refreshTokenTVar <- atomically $ newTVar $ Map. lookup " refresh-token" m
156
154
return $ do
157
- tlsParams <- eitherTLSParams
158
- issuerURL <- lookupEither m " idp-issuer-url"
159
- clientID <- lookupEither m " client-id"
160
- clientSecret <- lookupEither m " client-secret"
155
+ tlsParams <- mapLeft OIDCAuthCAParsingFailed eitherTLSParams
156
+ issuerURL <- lookupEither " idp-issuer-url"
157
+ clientID <- lookupEither " client-id"
158
+ clientSecret <- lookupEither " client-secret"
161
159
return OIDCAuth {.. }
160
+ where lookupEither k = Map. lookup k m
161
+ & maybeToRight (OIDCAuthMissingInformation $ Text. unpack k)
162
162
163
- parseCA :: Map Text Text -> IO (Either String TLS. ClientParams )
163
+ parseCA :: Map Text Text -> IO (Either ParseCertException TLS. ClientParams )
164
164
parseCA m = do
165
165
t <- defaultTLSClientParams
166
166
fromMaybe (pure $ pure t) (parseCAFile t m <|> parseCAData t m)
167
167
168
- parseCAFile :: TLS. ClientParams -> Map Text Text -> Maybe (IO (Either String TLS. ClientParams ))
168
+ parseCAFile :: TLS. ClientParams -> Map Text Text -> Maybe (IO (Either ParseCertException TLS. ClientParams ))
169
169
parseCAFile t m = do
170
170
caFile <- Text. unpack <$> Map. lookup " idp-certificate-authority" m
171
- return $ updateClientParams t <$> BS. readFile caFile
171
+ Just $ do
172
+ caText <- BS. readFile caFile
173
+ return $ updateClientParams t caText
172
174
173
- parseCAData :: TLS. ClientParams -> Map Text Text -> Maybe (IO (Either String TLS. ClientParams ))
175
+ parseCAData :: TLS. ClientParams -> Map Text Text -> Maybe (IO (Either ParseCertException TLS. ClientParams ))
174
176
parseCAData t m = do
175
- caText <- Map. lookup " idp-certificate-authority-data" m
176
- pure . pure
177
- $ (B64. decode $ Text. encodeUtf8 caText)
178
- >>= updateClientParams t
179
-
180
- lookupEither :: (Show key , Ord key ) => Map key val -> key -> Either String val
181
- lookupEither m k = maybeToRight e $ Map. lookup k m
182
- where e = " Couldn't find key: " <> show k <> " in OIDC auth info"
177
+ caBase64 <- Map. lookup " idp-certificate-authority-data" m
178
+ Just $ pure $ do
179
+ caText <- Text. encodeUtf8 caBase64
180
+ & B64. decode
181
+ & mapLeft Base64ParsingFailed
182
+ updateClientParams t caText
0 commit comments