Skip to content

Commit

Permalink
Remove cassandra queries to the user_keys_hashed table, as they are n…
Browse files Browse the repository at this point in the history
…ever read anymore since 'onboarding' / auto-connect was removed in #1005
  • Loading branch information
jschaul committed Dec 5, 2022
1 parent 1518191 commit 918ca7d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 66 deletions.
1 change: 1 addition & 0 deletions changelog.d/5-internal/remove-hashed-key-queries
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove cassandra queries to the user_keys_hashed table, as they are never read anymore since 'onboarding' / auto-connect was removed in https://github.com/wireapp/wire-server/pull/1005
2 changes: 2 additions & 0 deletions services/brig/schema/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,7 @@ main = do

-- FUTUREWORK: undo V41 (searchable flag); we stopped using it in
-- https://github.com/wireapp/wire-server/pull/964
--
-- FUTUREWORK after July 2023: integrate V_FUTUREWORK here.
]
`finally` Log.close l
47 changes: 47 additions & 0 deletions services/brig/schema/src/V_FUTUREWORK.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{-# LANGUAGE QuasiQuotes #-}

-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module V_FUTUREWORK
( migration,
)
where

import Cassandra.Schema
import Imports
import Text.RawString.QQ

-- user_keys_hashed usage was removed in https://github.com/wireapp/wire-server/pull/2902
--
-- However, it's dangerous to remove a cassandra table together with the usage, as
-- during deployment, there is a time window where the schema migration has run, but the
-- old code still serves traffic, which then leads to 5xxs and user-observable errors.
-- Therefore the policy is to wait a reasonable amount of time (6 months) to allow all
-- installations to upgrade before removing the database tables. See also
-- backwards-incompatbile schema migration docs in
-- https://docs.wire.com/developer/developer/cassandra-interaction.html?highlight=backwards+incompatbile#backwards-incompatible-schema-changes
--
-- FUTUREWORK: uncomment the code below after July 2023, rename this module with a version number, and
-- integrate it inside Main.hs and App.hs
migration :: Migration
migration = undefined
-- Migration FUTUREWORK_NUMBER "Drop deprecated user_keys_hashed table" $ do
-- schema'
-- [r|
-- DROP TABLE IF EXISTS user_keys_hashed
-- |]
67 changes: 1 addition & 66 deletions services/brig/src/Brig/Data/UserKey.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,17 @@ module Brig.Data.UserKey
lookupKey,
deleteKey,
deleteKeyForUser,
lookupPhoneHashes,
)
where

import Brig.App (Env, digestSHA256)
import Brig.App (Env)
import Brig.Data.Instances ()
import qualified Brig.Data.User as User
import Brig.Email
import Brig.Phone
import Cassandra
import Control.Lens (view)
import qualified Data.ByteString as B
import Data.ByteString.Lazy (toStrict)
import Data.Id
import qualified Data.Multihash.Digest as MH
import qualified Data.Text.Encoding as T
import Imports
import OpenSSL.EVP.Digest (digestBS)
import Wire.API.User (fromEmail)

-- | A natural identifier (i.e. unique key) of a user.
Expand All @@ -60,35 +53,6 @@ instance Eq UserKey where
(UserPhoneKey k) == (UserPhoneKey k') = k == k'
_ == _ = False

data UKHashType
= UKHashPhone
| UKHashEmail
deriving (Eq)

instance Cql UKHashType where
ctype = Tagged IntColumn

fromCql (CqlInt i) = case i of
0 -> pure UKHashPhone
1 -> pure UKHashEmail
n -> Left $ "unexpected hashtype: " ++ show n
fromCql _ = Left "userkeyhashtype: int expected"

toCql UKHashPhone = CqlInt 0
toCql UKHashEmail = CqlInt 1

newtype UserKeyHash = UserKeyHash MH.MultihashDigest

instance Cql UserKeyHash where
ctype = Tagged BlobColumn

fromCql (CqlBlob lbs) = case MH.decode (toStrict lbs) of
Left e -> Left ("userkeyhash: " ++ e)
Right h -> pure $ UserKeyHash h
fromCql _ = Left "userkeyhash: expected blob"

toCql (UserKeyHash d) = CqlBlob $ MH.encode (MH.algorithm d) (MH.digest d)

userEmailKey :: Email -> UserKey
userEmailKey = UserEmailKey . mkEmailKey

Expand Down Expand Up @@ -154,15 +118,10 @@ lookupKey k =

insertKey :: (MonadClient m, MonadReader Env m) => UserId -> UserKey -> m ()
insertKey u k = do
hk <- hashKey k
let kt = foldKey (\(_ :: Email) -> UKHashEmail) (\(_ :: Phone) -> UKHashPhone) k
retry x5 $ write insertHashed (params LocalQuorum (hk, kt, u))
retry x5 $ write keyInsert (params LocalQuorum (keyText k, u))

deleteKey :: (MonadClient m, MonadReader Env m) => UserKey -> m ()
deleteKey k = do
hk <- hashKey k
retry x5 $ write deleteHashed (params LocalQuorum (Identity hk))
retry x5 $ write keyDelete (params LocalQuorum (Identity $ keyText k))

-- | Delete `UserKey` for `UserId`
Expand All @@ -180,21 +139,6 @@ deleteKeyForUser uid k = do
Just keyUid | keyUid == uid -> deleteKey k
_ -> pure ()

hashKey :: MonadReader Env m => UserKey -> m UserKeyHash
hashKey uk = do
d <- view digestSHA256
let d' = digestBS d $ T.encodeUtf8 (keyText uk)
pure . UserKeyHash $
MH.MultihashDigest MH.SHA256 (B.length d') d'

lookupPhoneHashes :: MonadClient m => [ByteString] -> m [(ByteString, UserId)]
lookupPhoneHashes hp =
mapMaybe mk <$> retry x1 (query selectHashed (params One (Identity hashed)))
where
hashed = fmap (\h -> UserKeyHash $ MH.MultihashDigest MH.SHA256 (B.length h) h) hp
mk (UserKeyHash d, UKHashPhone, u) = Just (MH.digest d, u)
mk (_, _, _) = Nothing

--------------------------------------------------------------------------------
-- Queries

Expand All @@ -206,12 +150,3 @@ keySelect = "SELECT user FROM user_keys WHERE key = ?"

keyDelete :: PrepQuery W (Identity Text) ()
keyDelete = "DELETE FROM user_keys WHERE key = ?"

insertHashed :: PrepQuery W (UserKeyHash, UKHashType, UserId) ()
insertHashed = "INSERT INTO user_keys_hash(key, key_type, user) VALUES (?, ?, ?)"

deleteHashed :: PrepQuery W (Identity UserKeyHash) ()
deleteHashed = "DELETE FROM user_keys_hash WHERE key = ?"

selectHashed :: PrepQuery R (Identity [UserKeyHash]) (UserKeyHash, UKHashType, UserId)
selectHashed = "SELECT key, key_type, user FROM user_keys_hash WHERE key IN ?"

0 comments on commit 918ca7d

Please sign in to comment.