Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5-internal/pr-3035
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed broken stern endpoint `POST i/user/meta-info`
1 change: 1 addition & 0 deletions changelog.d/5-internal/pr-3036
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make stern fail on startup if supported backend api version needs bumping
4 changes: 4 additions & 0 deletions libs/wire-api/src/Wire/API/Routes/Version.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Control.Lens ((?~))
import Data.Aeson (FromJSON, ToJSON (..))
import qualified Data.Aeson as Aeson
import Data.Bifunctor
import Data.ByteString.Conversion (ToByteString (builder))
import qualified Data.ByteString.Lazy as LBS
import Data.Domain
import Data.Schema
Expand Down Expand Up @@ -83,6 +84,9 @@ instance ToHttpApiData Version where
toHeader = LBS.toStrict . Aeson.encode
toUrlPiece = Text.decodeUtf8 . toHeader

instance ToByteString Version where
builder = toEncodedUrlPiece

supportedVersions :: [Version]
supportedVersions = [minBound .. maxBound]

Expand Down
2 changes: 2 additions & 0 deletions tools/stern/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
, lib
, metrics-wai
, mtl
, retry
, schema-profunctor
, servant
, servant-server
Expand Down Expand Up @@ -75,6 +76,7 @@ mkDerivation {
lens
metrics-wai
mtl
retry
schema-profunctor
servant
servant-server
Expand Down
7 changes: 4 additions & 3 deletions tools/stern/src/Stern/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ default (ByteString)
start :: Opts -> IO ()
start o = do
e <- newEnv o
runAppT e $ Intra.assertBackendApiVersion
s <- Server.newSettings (server e)
Server.runSettingsWithShutdown s (servantApp e) Nothing
where
Expand Down Expand Up @@ -402,15 +403,15 @@ getUserData uid = do
convs <- Intra.getUserConversations uid
clts <- Intra.getUserClients uid
notfs <- Intra.getUserNotifications uid
consent <- Intra.getUserConsentValue uid
consentLog <- Intra.getUserConsentLog uid
consent <- (Intra.getUserConsentValue uid <&> toJSON @ConsentValue) `catchE` (pure . String . cs . show)
consentLog <- (Intra.getUserConsentLog uid <&> toJSON @ConsentLog) `catchE` (pure . String . cs . show)
cookies <- Intra.getUserCookies uid
properties <- Intra.getUserProperties uid
-- Get all info from Marketo too
let em = userEmail $ accountUser account
marketo <- do
let noEmail = MarketoResult $ KeyMap.singleton "results" emptyArray
maybe (pure noEmail) Intra.getMarketoResult em
maybe (pure $ toJSON noEmail) (\e -> (Intra.getMarketoResult e <&> toJSON) `catchE` (pure . String . cs . show)) em
pure . UserMetaInfo . KeyMap.fromList $
[ "account" .= account,
"cookies" .= cookies,
Expand Down
36 changes: 32 additions & 4 deletions tools/stern/src/Stern/Intra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Stern.Intra
( putUser,
( assertBackendApiVersion,
putUser,
putUserStatus,
getContacts,
getUserConnections,
Expand Down Expand Up @@ -61,10 +62,12 @@ module Stern.Intra
)
where

import Bilge hiding (head, options, requestId)
import Bilge hiding (head, options, path, paths, requestId)
import qualified Bilge
import Bilge.RPC
import Brig.Types.Intra
import Control.Error
import Control.Exception (ErrorCall (ErrorCall))
import Control.Lens (view, (^.))
import Control.Monad.Reader
import Data.Aeson hiding (Error)
Expand Down Expand Up @@ -94,12 +97,15 @@ import Stern.Types
import System.Logger.Class hiding (Error, name, (.=))
import qualified System.Logger.Class as Log
import UnliftIO.Exception hiding (Handler)
import UnliftIO.Retry (constantDelay, limitRetries, recoverAll)
import Wire.API.Connection
import Wire.API.Conversation
import Wire.API.Internal.Notification
import Wire.API.Properties
import Wire.API.Routes.Internal.Brig.Connection
import qualified Wire.API.Routes.Internal.Brig.EJPD as EJPD
import Wire.API.Routes.Version
import Wire.API.Routes.Versioned
import Wire.API.Team
import Wire.API.Team.Feature
import qualified Wire.API.Team.Feature as Public
Expand All @@ -112,6 +118,28 @@ import Wire.API.User.Search

-------------------------------------------------------------------------------

backendApiVersion :: Version
backendApiVersion = V2

-- | Make sure the backend supports `backendApiVersion`. Crash if it doesn't. (This is called
-- in `Stern.API` so problems make `./services/integration.sh` crash.)
assertBackendApiVersion :: App ()
assertBackendApiVersion = recoverAll (constantDelay 1000000 <> limitRetries 5) $ \_retryStatus -> do
b <- view brig
vinfo :: VersionInfo <-
responseJsonError
=<< rpc' "brig" b (method GET . Bilge.path "/api-version" . contentJson . expect2xx)
unless (maximum (vinfoSupported vinfo) == backendApiVersion) $ do
throwIO . ErrorCall $ "newest supported backend api version must be " <> show backendApiVersion

path :: ByteString -> Request -> Request
path = Bilge.path . ((toByteString' backendApiVersion <> "/") <>)

paths :: [ByteString] -> Request -> Request
paths = Bilge.paths . (toByteString' backendApiVersion :)

-------------------------------------------------------------------------------

putUser :: UserId -> UserUpdate -> Handler ()
putUser uid upd = do
info $ userMsg uid . msg "Changing user state"
Expand Down Expand Up @@ -744,12 +772,12 @@ getUserConversations uid = do
b
( method GET
. header "Z-User" (toByteString' uid)
. path "/conversations"
. path "conversations"
. queryItem "size" (toByteString' batchSize)
. maybe id (queryItem "start" . toByteString') start
. expect2xx
)
parseResponse (mkError status502 "bad-upstream") r
unVersioned @'V2 <$> parseResponse (mkError status502 "bad-upstream") r
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this wouldn't have happened if we used servant-client for calling internal api end-points... :)

batchSize = 100 :: Int

getUserClients :: UserId -> Handler [Client]
Expand Down
1 change: 1 addition & 0 deletions tools/stern/stern.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ library
, lens >=4.4
, metrics-wai >=0.3
, mtl >=2.1
, retry
, schema-profunctor
, servant
, servant-server
Expand Down