Skip to content

Commit

Permalink
Add admin user option
Browse files Browse the repository at this point in the history
  • Loading branch information
kderme committed Oct 15, 2021
1 parent 67b810c commit 636e896
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 8 deletions.
10 changes: 10 additions & 0 deletions cardano-db-sync-extended/app/cardano-db-sync-extended.hs
Expand Up @@ -65,6 +65,7 @@ pRunDbSyncNode =
<*> pLedgerStateDir
<*> pMigrationDir
<*> pRunSmash
<*> optional pSmashUserFile
<*> optional pSlotNo

pConfigFile :: Parser ConfigFile
Expand Down Expand Up @@ -100,6 +101,15 @@ pRunSmash = Opt.switch
<> Opt.help "Enable smash web server"
)

pSmashUserFile :: Parser FilePath
pSmashUserFile =
Opt.strOption
( Opt.long "admins"
<> Opt.help "Path to the file containing the credentials of smash server admin users"
<> Opt.completer (Opt.bashCompleter "file")
<> Opt.metavar "FILEPATH"
)

pSocketPath :: Parser SocketPath
pSocketPath =
SocketPath <$> Opt.strOption
Expand Down
10 changes: 10 additions & 0 deletions cardano-db-sync/app/cardano-db-sync.hs
Expand Up @@ -64,6 +64,7 @@ pRunDbSyncNode =
<*> pLedgerStateDir
<*> pMigrationDir
<*> pRunSmash
<*> optional pSmashUserFile
<*> optional pSlotNo

pConfigFile :: Parser ConfigFile
Expand Down Expand Up @@ -99,6 +100,15 @@ pRunSmash = Opt.switch
<> Opt.help "Enable smash web server"
)

pSmashUserFile :: Parser FilePath
pSmashUserFile =
Opt.strOption
( Opt.long "admins"
<> Opt.help "Path to the file containing the credentials of smash server admin users"
<> Opt.completer (Opt.bashCompleter "file")
<> Opt.metavar "FILEPATH"
)

pSocketPath :: Parser SocketPath
pSocketPath =
SocketPath <$> Opt.strOption
Expand Down
3 changes: 1 addition & 2 deletions cardano-db-sync/src/Cardano/DbSync.hs
Expand Up @@ -40,7 +40,6 @@ 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 @@ -88,7 +87,7 @@ runDbSyncNode metricsSetters mkPlugin knownMigrations params = do
let poolApi = postgresqlPoolDataLayer trce

if enpRunSmash params
then race_ syncNode (runSmashServer trce poolApi (ApplicationUsers []) 3100)
then race_ syncNode (runSmashServer trce poolApi (enpSmashUserFile params) 3100)
else syncNode
where
-- This is only necessary because `cardano-db` and `cardano-sync` both define
Expand Down
1 change: 0 additions & 1 deletion cardano-db/src/Cardano/Db/Error.hs
Expand Up @@ -8,7 +8,6 @@ module Cardano.Db.Error
) where


-- import Data.Aeson (ToJSON (..))
import qualified Data.ByteString.Base16 as Base16
import Data.ByteString.Char8 (ByteString)
import GHC.Generics
Expand Down
2 changes: 0 additions & 2 deletions cardano-db/src/Cardano/Db/Query.hs
Expand Up @@ -332,8 +332,6 @@ queryCurrentEpochNo = do
pure $ max_ (blk ^. BlockEpochNo)
pure $ join (unValue =<< listToMaybe res)



-- | Get the fees paid in all block from genesis up to and including the specified block.
queryFeesUpToBlockNo :: MonadIO m => Word64 -> ReaderT SqlBackend m Ada
queryFeesUpToBlockNo blkNo = do
Expand Down
2 changes: 1 addition & 1 deletion cardano-db/src/Cardano/Db/Schema.hs
Expand Up @@ -520,7 +520,7 @@ share
poolId PoolHashId
UniqueReservedPoolTicker name

-- A table containing a list of delisted pools.
-- A table containing delisted pools.
DelistedPool
hashRaw ByteString sqltype=hash28type
UniqueDelistedPool hashRaw
Expand Down
30 changes: 28 additions & 2 deletions cardano-smash-server/src/Cardano/SMASH/Server/Run.hs
Expand Up @@ -15,6 +15,9 @@ import Servant (Application, BasicAuthCheck (..), BasicAuthData (..),
import Network.Wai.Handler.Warp (defaultSettings, runSettings,
setBeforeMainLoop, setPort)

import qualified Data.Text as Text
import qualified Data.Text.IO as Text

import Cardano.BM.Trace (Trace, logInfo)

import Cardano.Db (textShow)
Expand All @@ -24,9 +27,13 @@ import Cardano.SMASH.Server.Impl
import Cardano.SMASH.Server.PoolDataLayer
import Cardano.SMASH.Server.Types

import System.IO.Error

runSmashServer :: Trace IO Text -> PoolDataLayer -> Maybe FilePath -> Int -> IO ()
runSmashServer tracer dataLayer mUsersFile port = do

appUsers <- readAppUsers mUsersFile

runSmashServer :: Trace IO Text -> PoolDataLayer -> ApplicationUsers -> Int -> IO ()
runSmashServer tracer dataLayer appUsers port = do
let settings =
setPort port $
setBeforeMainLoop
Expand All @@ -35,6 +42,25 @@ runSmashServer tracer dataLayer appUsers port = do

runSettings settings =<< mkApp dataLayer appUsers

readAppUsers :: Maybe FilePath -> IO ApplicationUsers
readAppUsers mPath = case mPath of
Nothing -> pure $ ApplicationUsers []
Just path -> do
userLines <- Text.lines <$> Text.readFile path
case mapM parseAppUser userLines of
Right users -> pure $ ApplicationUsers users
Left err -> throwIO $ userError $ Text.unpack err

parseAppUser :: Text -> Either Text ApplicationUser
parseAppUser line = case Text.breakOn "," line of
(user, commaPswd)
| not (Text.null commaPswd)
, passwd <- Text.tail commaPswd -- strip the comma
-> Right $ ApplicationUser (prepareCred user) (prepareCred passwd)
_ -> Left "Credentials need to be supplied in the form: username,password"
where
prepareCred name = Text.strip name

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

Expand Down
1 change: 1 addition & 0 deletions cardano-sync/src/Cardano/Sync/Config/Types.hs
Expand Up @@ -109,6 +109,7 @@ data SyncNodeParams = SyncNodeParams
, enpLedgerStateDir :: !LedgerStateDir
, enpMigrationDir :: !MigrationDir
, enpRunSmash :: !Bool
, enpSmashUserFile :: !(Maybe FilePath)
, enpMaybeRollback :: !(Maybe SlotNo)
}

Expand Down

0 comments on commit 636e896

Please sign in to comment.