Skip to content

Commit

Permalink
Remove admin_user table
Browse files Browse the repository at this point in the history
  • Loading branch information
kderme committed Oct 15, 2021
1 parent 7117bf0 commit 8e7a99b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 61 deletions.
3 changes: 2 additions & 1 deletion cardano-db-sync/src/Cardano/DbSync.hs
Expand Up @@ -40,6 +40,7 @@ import Cardano.Sync.Database (runDbThread)

import Cardano.SMASH.Server.PoolDataLayer
import Cardano.SMASH.Server.Run
import Cardano.SMASH.Server.Types

import Cardano.Sync (Block (..), MetricSetters, SyncDataLayer (..), SyncNodePlugin (..),
configureLogging, runSyncNode)
Expand Down Expand Up @@ -86,7 +87,7 @@ runDbSyncNode metricsSetters mkPlugin knownMigrations params = do

let poolApi = postgresqlPoolDataLayer trce

race_ syncNode (runApp poolApi 3100)
race_ syncNode (runSmashServer poolApi (ApplicationUsers []) 3100)
where
-- This is only necessary because `cardano-db` and `cardano-sync` both define
-- this newtype, but the later does not depend on the former.
Expand Down
1 change: 0 additions & 1 deletion cardano-db/cardano-db.cabal
Expand Up @@ -54,7 +54,6 @@ library
, base16-bytestring
, bytestring
, cardano-crypto-class
, cardano-crypto-wrapper
, cardano-ledger-core
, cardano-ledger-shelley
, cardano-ledger-shelley-ma
Expand Down
4 changes: 0 additions & 4 deletions cardano-db/src/Cardano/Db/Insert.hs
Expand Up @@ -7,7 +7,6 @@

module Cardano.Db.Insert
( insertAdaPots
, insertAdminUser
, insertBlock
, insertCollateralTxIn
, insertDelegation
Expand Down Expand Up @@ -103,9 +102,6 @@ import Cardano.Db.Schema
insertAdaPots :: (MonadBaseControl IO m, MonadIO m) => AdaPots -> ReaderT SqlBackend m AdaPotsId
insertAdaPots = insertCheckUnique "AdaPots"

insertAdminUser :: (MonadBaseControl IO m, MonadIO m) => AdminUser -> ReaderT SqlBackend m AdminUserId
insertAdminUser = insertUnchecked "AdminUser"

insertBlock :: (MonadBaseControl IO m, MonadIO m) => Block -> ReaderT SqlBackend m BlockId
insertBlock = insertUnchecked "Block"

Expand Down
12 changes: 0 additions & 12 deletions cardano-db/src/Cardano/Db/Schema.hs
Expand Up @@ -519,13 +519,6 @@ share
poolId PoolHashId
UniqueReservedPoolTicker name

-- A table containin a list of administrator users that can be used to access the secure API endpoints.
-- Yes, we don't have any hash check mechanisms here, if they get to the database, game over anyway.
AdminUser
username Text
password Text
UniqueAdminUser username

|]

deriving instance Eq (Unique EpochSyncTime)
Expand Down Expand Up @@ -917,8 +910,3 @@ schemaDocs =
"A table containing a managed list of reserved ticker names."
ReservedPoolTickerName # "The ticker name."
ReservedPoolTickerPoolId # "The PoolHash table index for the pool that has reserved this name."

AdminUser --^ do
"A table listing all admin users (for maintaining the SMASH related data)."
AdminUserUsername # "The user name."
AdminUserPassword # "The password."
13 changes: 4 additions & 9 deletions cardano-smash-server/src/Cardano/SMASH/Server/PoolDataLayer.hs
Expand Up @@ -6,7 +6,7 @@ module Cardano.SMASH.Server.PoolDataLayer where

import Cardano.Prelude

import Cardano.BM.Trace (Trace)
import Cardano.BM.Trace (Trace, logInfo)

import qualified Data.ByteString.Base16 as Base16
import qualified Data.Map.Strict as Map
Expand Down Expand Up @@ -38,10 +38,6 @@ data PoolDataLayer =
, dlGetRetiredPools :: IO (Either DBFail [PoolId])
, dlRemoveRetiredPool :: PoolId -> IO (Either DBFail PoolId) -- Used only while inserting reg cert

, dlGetAdminUsers :: IO (Either DBFail [Db.AdminUser])
, dlAddAdminUser :: ApplicationUser -> IO (Either DBFail Db.AdminUser)
, dlRemoveAdminUser :: ApplicationUser -> IO (Either DBFail Db.AdminUser)

, dlGetFetchErrors :: PoolId -> Maybe UTCTime -> IO (Either DBFail [PoolFetchError])

, dlGetPool :: PoolId -> IO (Either DBFail PoolId)
Expand Down Expand Up @@ -69,7 +65,9 @@ postgresqlPoolDataLayer tracer = PoolDataLayer {
mticker <- Db.queryReservedTicker ph meta
pure $ map (\ticker -> (TickerName ticker, dbToServantMetaHash meta)) mticker
pure $ catMaybes tickers
, dlAddReservedTicker = undefined
, dlAddReservedTicker = \_ _ -> do
_ <- logInfo tracer $ "Add Reserved"
pure $ Left RecordDoesNotExist
, dlCheckReservedTicker = \ticker metaHash -> do
pools <- getActivePools tracer Nothing
tickers <- Db.runWithConnectionLogging tracer $ forM (Map.toList pools) $ \(ph, meta) -> do
Expand All @@ -92,9 +90,6 @@ postgresqlPoolDataLayer tracer = PoolDataLayer {
ls <- filterRetired <$> getCertActions tracer Nothing
pure $ Right $ dbToServantPoolId <$> ls
, dlRemoveRetiredPool = undefined
, dlGetAdminUsers = pure $ Right []
, dlAddAdminUser = undefined
, dlRemoveAdminUser = undefined
, dlGetFetchErrors = undefined
, dlGetPool = \poolId -> do
mmeta <- getActiveMetaHash tracer poolId
Expand Down
44 changes: 10 additions & 34 deletions cardano-smash-server/src/Cardano/SMASH/Server/Run.hs
Expand Up @@ -2,7 +2,10 @@
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE TypeOperators #-}

module Cardano.SMASH.Server.Run where
module Cardano.SMASH.Server.Run
( runSmashServer
, runAppStubbed
) where

import Cardano.Prelude

Expand All @@ -12,48 +15,32 @@ import Servant (Application, BasicAuthCheck (..), BasicAuthData (..),
import Network.Wai.Handler.Warp (defaultSettings, runSettings,
setBeforeMainLoop, setPort)

import Cardano.Db

import Cardano.SMASH.Server.Api
import Cardano.SMASH.Server.Impl
import Cardano.SMASH.Server.PoolDataLayer
import Cardano.SMASH.Server.Types


runApp :: PoolDataLayer -> Int -> IO ()
runApp dataLayer port = do
runSmashServer :: PoolDataLayer -> ApplicationUsers -> Int -> IO ()
runSmashServer dataLayer appUsers port = do
let settings =
setPort port $
setBeforeMainLoop (hPutStrLn stderr ("SMASH listening on port " ++ show port)) $
defaultSettings

runSettings settings =<< (mkApp dataLayer)
runSettings settings =<< (mkApp dataLayer appUsers)

mkApp :: PoolDataLayer -> IO Application
mkApp dataLayer = do
mkApp :: PoolDataLayer -> ApplicationUsers -> IO Application
mkApp dataLayer appUsers = do

-- Ugly hack, wait 2s for migrations to run for the admin user to be created.
-- You can always run the migrations first.
threadDelay 2_000_000

-- Fetch the admin users from the DB.
adminUsers <- dlGetAdminUsers dataLayer

-- This is pretty close to the top and we can't handle this.
let adminUsers' = case adminUsers of
Left err -> panic $ "Error with fetching application users! " <> show err
Right users -> users

let applicationUsers = ApplicationUsers $ map convertToAppUsers adminUsers'

return $ serveWithContext
fullAPI
(basicAuthServerContext applicationUsers)
(basicAuthServerContext appUsers)
(server dataLayer)
where
convertToAppUsers :: AdminUser -> ApplicationUser
convertToAppUsers (AdminUser username' password') = ApplicationUser username' password'


-- | We need to supply our handlers with the right Context.
basicAuthServerContext :: ApplicationUsers -> Context (BasicAuthCheck User ': '[])
Expand Down Expand Up @@ -84,17 +71,6 @@ checkIfUserValid (ApplicationUsers applicationUsers) applicationUser@(Applicatio
then (UserValid (User usernameText))
else UserInvalid


createAdminUser :: PoolDataLayer -> ApplicationUser -> IO (Either DBFail AdminUser)
createAdminUser dataLayer applicationUser = do
let addAdminUser = dlAddAdminUser dataLayer
addAdminUser applicationUser

deleteAdminUser :: PoolDataLayer -> ApplicationUser -> IO (Either DBFail AdminUser)
deleteAdminUser dataLayer applicationUser = do
let removeAdminUser = dlRemoveAdminUser dataLayer
removeAdminUser applicationUser

-- Stub api

runAppStubbed :: Int -> IO ()
Expand Down

0 comments on commit 8e7a99b

Please sign in to comment.