Skip to content

Commit fb827ae

Browse files
authored
WPB-21785 [fix] BrigAPIAccess interpreter sometimes throws 5 xx when it shouldn't (#4865)
1 parent 5c8736f commit fb827ae

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

changelog.d/5-internal/WPB-21785

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix: Inappropriate 500 errors removed from BrigAPIAccess interpreter

libs/bilge/src/Bilge/Request.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ module Bilge.Request
7070
Rq.applyBasicAuth,
7171
Rq.urlEncodedBody,
7272
Rq.getUri,
73+
expect2xxOr404,
7374
)
7475
where
7576

@@ -153,6 +154,9 @@ expect3xx = expectStatus ((== 3) . (`div` 100))
153154
expect4xx :: Request -> Request
154155
expect4xx = expectStatus ((== 4) . (`div` 100))
155156

157+
expect2xxOr404 :: Request -> Request
158+
expect2xxOr404 = expectStatus (\code -> code == 404 || (code >= 200 && code < 300))
159+
156160
expectStatus :: (Int -> Bool) -> Request -> Request
157161
expectStatus property r = r {Rq.checkResponse = check}
158162
where

libs/wire-subsystems/src/Wire/BrigAPIAccess.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ data BrigAPIAccess m a where
162162
CreateGroupInternal :: ManagedBy -> TeamId -> Maybe UserId -> NewUserGroup -> BrigAPIAccess m (Either Wai.Error UserGroup)
163163
GetGroupInternal :: TeamId -> UserGroupId -> Bool -> BrigAPIAccess m (Maybe UserGroup)
164164
GetGroupsInternal :: TeamId -> Maybe Scim.Filter -> BrigAPIAccess m UserGroupPage
165-
UpdateGroup :: UpdateGroupInternalRequest -> BrigAPIAccess m ()
165+
UpdateGroup :: UpdateGroupInternalRequest -> BrigAPIAccess m (Either Wai.Error ())
166166
DeleteGroupInternal :: ManagedBy -> TeamId -> UserGroupId -> BrigAPIAccess m (Either DeleteGroupManagedError ())
167167

168168
makeSem ''BrigAPIAccess

libs/wire-subsystems/src/Wire/BrigAPIAccess/Rpc.hs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ deleteBot cid bot = do
212212
. header "Z-Type" "bot"
213213
. header "Z-Bot" (toByteString' bot)
214214
. header "Z-Conversation" (toByteString' cid)
215-
. expect2xx
215+
. expect2xxOr404
216216

217217
-- | Calls 'Brig.User.API.Auth.reAuthUserH'.
218218
reAuthUser ::
@@ -296,7 +296,7 @@ deleteUser uid = do
296296
brigRequest $
297297
method DELETE
298298
. paths ["/i/users", toByteString' uid]
299-
. expect2xx
299+
. expect2xxOr404
300300

301301
-- | Calls 'Brig.API.getContactListH'.
302302
getContactList ::
@@ -333,10 +333,12 @@ getUserExportData ::
333333
getUserExportData uid = do
334334
resp <-
335335
brigRequest $
336-
method GET
336+
check [status200, status404]
337+
. method GET
337338
. paths ["i/users", toByteString' uid, "export-data"]
338-
. expect2xx
339-
decodeBodyOrThrow "brig" resp
339+
if statusCode resp == 404
340+
then pure Nothing
341+
else decodeBodyOrThrow "brig" resp
340342

341343
getAccountConferenceCallingConfigClient ::
342344
(Member Rpc r, Member (Input Endpoint) r, Member (Error ParseException) r) =>
@@ -415,7 +417,7 @@ notifyClientsAboutLegalHoldRequest requesterUid targetUid lastPrekey' = do
415417
method POST
416418
. paths ["i", "clients", "legalhold", toByteString' targetUid, "request"]
417419
. json (LegalHoldClientRequest requesterUid lastPrekey')
418-
. expect2xx
420+
. expect2xxOr404
419421

420422
-- | Calls 'Brig.User.API.Auth.legalHoldLoginH'.
421423
getLegalHoldAuthToken ::
@@ -476,7 +478,7 @@ removeLegalHoldClientFromUser targetUid = do
476478
method DELETE
477479
. paths ["i", "clients", "legalhold", toByteString' targetUid]
478480
. contentJson
479-
. expect2xx
481+
. expect2xxOr404
480482

481483
-- | Calls 'Brig.API.addClientInternalH'.
482484
brigAddClient ::
@@ -579,7 +581,7 @@ createGroupInternal managedBy teamId creatorUserId newGroup = do
579581
method POST
580582
. path "/i/user-groups/full"
581583
. json req
582-
if statusCode r >= 200 && statusCode r < 300
584+
if is2xx r
583585
then Right <$> decodeBodyOrThrow @UserGroup "brig" r
584586
else Left <$> decodeBodyOrThrow @Wai.Error "brig" r
585587

@@ -592,10 +594,12 @@ getGroupInternal ::
592594
getGroupInternal tid gid includeChannels = do
593595
r <-
594596
brigRequest $
595-
method GET
597+
check [status200, status404]
598+
. method GET
596599
. paths ["i", "user-groups", toByteString' tid, toByteString' gid, toByteString' includeChannels]
597-
. expect2xx
598-
decodeBodyOrThrow "brig" r
600+
if statusCode r == 404
601+
then pure Nothing
602+
else decodeBodyOrThrow "brig" r
599603

600604
getGroupsInternal ::
601605
(Member Rpc r, Member (Input Endpoint) r, Member (Error ParseException) r) =>
@@ -617,16 +621,18 @@ getGroupsInternal tid mbFilter = do
617621
decodeBodyOrThrow "brig" r
618622

619623
updateGroup ::
620-
(Member Rpc r, Member (Input Endpoint) r) =>
624+
(Member Rpc r, Member (Input Endpoint) r, Member (Error ParseException) r) =>
621625
UpdateGroupInternalRequest ->
622-
Sem r ()
623-
updateGroup reqBody =
624-
void $
626+
Sem r (Either Wai.Error ())
627+
updateGroup reqBody = do
628+
resp <-
625629
brigRequest $
626630
method PUT
627631
. paths ["i", "user-groups"]
628632
. json reqBody
629-
. expect2xx
633+
if is2xx resp
634+
then pure (Right ())
635+
else Left <$> decodeBodyOrThrow @Wai.Error "brig" resp
630636

631637
deleteGroupInternal ::
632638
( Member Rpc r,
@@ -643,7 +649,8 @@ deleteGroupInternal managedBy teamId groupId = do
643649
method DELETE
644650
. paths ["i", "user-groups", toByteString' teamId, toByteString' groupId, "managed", toByteString' managedBy]
645651
case (statusCode resp, errorLabel resp) of
646-
(status, _) | status >= 200 && status < 300 -> pure $ Right ()
652+
(status, _) | statusIs2xx status -> pure $ Right ()
653+
(404, _) -> pure $ Right ()
647654
(403, Just "user-group-managed-by-mismatch") -> pure $ Left DeleteGroupManagedManagedByMismatch
648655
(status, label) ->
649656
throw $
@@ -654,3 +661,9 @@ deleteGroupInternal managedBy teamId groupId = do
654661
where
655662
errorLabel :: ResponseLBS -> Maybe LText
656663
errorLabel = fmap Wai.label . responseJsonMaybe
664+
665+
is2xx :: ResponseLBS -> Bool
666+
is2xx = statusIs2xx . statusCode
667+
668+
statusIs2xx :: Int -> Bool
669+
statusIs2xx s = s >= 200 && s < 300

libs/wire-subsystems/src/Wire/ScimSubsystem/Interpreter.hs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Data.Text qualified as Text
2727
import Data.UUID qualified as UUID
2828
import Data.Vector qualified as V
2929
import Imports
30+
import Network.HTTP.Types.Status (notFound404)
3031
import Network.URI (parseURI)
3132
import Network.Wai.Utilities.Error qualified as Wai
3233
import Polysemy
@@ -185,8 +186,13 @@ scimUpdateUserGroupImpl teamId gid grp = do
185186
[] -> pure ()
186187
(u : _) -> scimThrow $ notFound "User" (idToText u)
187188

188-
-- replace the members of the user group
189-
BrigAPI.updateGroup (UpdateGroupInternalRequest teamId gid (Just ugName) (Just reqMemberIds))
189+
-- replace the members of the user group; propagate Brig errors
190+
BrigAPI.updateGroup (UpdateGroupInternalRequest teamId gid (Just ugName) (Just reqMemberIds)) >>= \case
191+
Right () -> pure ()
192+
Left err ->
193+
if err.code == notFound404
194+
then scimThrow $ notFound "Group" (UUID.toText $ gid.toUUID)
195+
else throw (ScimSubsystemInternal err)
190196

191197
ScimSubsystemConfig scimBaseUri <- input
192198
maybe (scimThrow $ notFound "Group" (UUID.toText $ gid.toUUID)) (pure . toStoredGroup scimBaseUri)

0 commit comments

Comments
 (0)