Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth user check - EDAPI #423

Merged
merged 32 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f3c827d
Add query for claim sub
sajidanower23 Apr 16, 2019
d740d45
Remove redundant imports
sajidanower23 Apr 17, 2019
b84d597
Add abstraction for running db operations
sajidanower23 Apr 17, 2019
42dab2f
Add error type, introduce error hierarchy
sajidanower23 Apr 17, 2019
8a52255
Style tweaks
sajidanower23 Apr 17, 2019
ae3dbbf
Implement FromField StringOrURI
sajidanower23 Apr 17, 2019
9887a50
Add function to add users
sajidanower23 Apr 17, 2019
0398210
Remove unused debug function
sajidanower23 Apr 18, 2019
2e35982
Run stylish-haskell through some BR modules
sajidanower23 Apr 18, 2019
d0b1b3d
Style tweaks
sajidanower23 Apr 18, 2019
299ce39
Add different app modes; add way to bootstrap users into the database
sajidanower23 Apr 18, 2019
eed3dcc
Bugfix bootstrap to allow bootstrapping without an auth user
sajidanower23 Apr 18, 2019
fc8f0c3
Add comment
sajidanower23 Apr 18, 2019
ed86c56
Actually use the bootstrap function
sajidanower23 Apr 18, 2019
cbea350
Complete Var instance for AppMode
sajidanower23 Apr 23, 2019
0fd2de5
Use putStrLn instead of print
sajidanower23 Apr 23, 2019
ce7a4e8
Merge branch 'master' of github.com:data61/Mirza into auth-user-check…
sajidanower23 Apr 30, 2019
f95c93e
Tweak dockerfile
sajidanower23 Apr 30, 2019
6197dc1
List workdir
sajidanower23 May 1, 2019
8737ae3
Use text instead of varchar
sajidanower23 May 1, 2019
4c0c3b8
Make errors a new module
sajidanower23 May 1, 2019
dfb8406
Remove redundant wildcard
sajidanower23 May 1, 2019
b295e1b
Add further error types
sajidanower23 May 1, 2019
669d1b6
Throw proper errors instead of returning Bool
sajidanower23 May 1, 2019
4fd0b3e
Store adding user's creds in case of db insertion fails for unauth ac…
sajidanower23 May 1, 2019
5f4c184
Make dir /edapi
sajidanower23 May 2, 2019
3dff9b3
Use LambdaCase
sajidanower23 May 2, 2019
51ddfd4
Add more log in Dockerfile
sajidanower23 May 2, 2019
1baaee3
Run mkdir before setting it as workdir
sajidanower23 May 2, 2019
d7b77db
Put /edapi in /src
sajidanower23 May 2, 2019
4453d01
Remove FROM flag
sajidanower23 May 2, 2019
bcc80dc
Add further log
sajidanower23 May 2, 2019
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ stages:
- "Build"
- name: "Deploy"
if: branch = master AND type = push

jobs:
include:
- name: "Web"
Expand Down
47 changes: 18 additions & 29 deletions projects/br_scs/src/Mirza/BusinessRegistry/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import Mirza.Common.Types as CT
import Data.GS1.EPC (GS1CompanyPrefix (..))

import Servant
import Servant.Swagger.UI
import Servant.Auth.Server
import Servant.Swagger.UI

import Crypto.JWT (Audience (..), string)

import qualified Data.Pool as Pool
import Database.PostgreSQL.Simple

import Network.URI (nullURI)
import Network.Wai (Middleware)
import qualified Network.Wai.Handler.Warp as Warp
import Network.URI (nullURI)

import Data.Aeson (eitherDecodeFileStrict)

Expand All @@ -39,13 +39,15 @@ import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.Semigroup ((<>))
import Data.Text (Text, pack)
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
import Data.Text.Encoding (decodeUtf8,
encodeUtf8)
import Options.Applicative hiding (action)
import Text.Email.Parser (addrSpec)
import Text.Email.Validate (validate, toByteString)
import Text.Email.Validate (toByteString,
validate)

import Control.Lens (review)
import Control.Exception (finally)
import Control.Lens (review)
import Data.Maybe (fromMaybe)
import Katip as K
import System.IO (IOMode (AppendMode),
Expand Down Expand Up @@ -82,14 +84,14 @@ data ExecMode
| Bootstrap EmailAddress GS1CompanyPrefix

data ServerOptionsBR = ServerOptionsBR
{ sobDbConnStr :: ByteString
, sobLoggingLevel :: K.Severity
, sobLogLocation :: Maybe FilePath
, sobEnvType :: CT.EnvType
{ sobDbConnStr :: ByteString
, sobLoggingLevel :: K.Severity
, sobLogLocation :: Maybe FilePath
, sobEnvType :: CT.EnvType
}

data RunServerOptions = RunServerOptions
{ rsoPortNumber :: Int
{ rsoPortNumber :: Int
, sobOAuthAudience :: Text
}

Expand Down Expand Up @@ -118,12 +120,12 @@ main = multiplexInitOptions =<< execParser opts where
-- where the single binary could be split into multiple binaries.
multiplexInitOptions :: InitOptionsBR -> IO ()
multiplexInitOptions (InitOptionsBR opts mode) = case mode of
RunServer rsOpts -> launchServer opts rsOpts
InitDb -> runMigration opts
UserAction uc -> runUserCommand opts uc
BusinessAction bc -> runBusinessCommand opts bc
PopulateDatabase -> runPopulateDatabase opts
Bootstrap email companyPrefix -> runBootstrap opts email companyPrefix
RunServer rsOpts -> launchServer opts rsOpts
InitDb -> runMigration opts
UserAction uc -> runUserCommand opts uc
BusinessAction bc -> runBusinessCommand opts bc
PopulateDatabase -> runPopulateDatabase opts
Bootstrap email companyPrefix -> runBootstrap opts email companyPrefix


--------------------------------------------------------------------------------
Expand Down Expand Up @@ -354,19 +356,6 @@ runBootstrap opts email companyPrefix = do
let newUserPhoneNumber = ""
NewUser{..}


--------------------------------------------------------------------------------
-- Debug Command
--------------------------------------------------------------------------------

-- This is a debug function for activating development test stub functions.
-- TODO: Remove this stub before release.
debugFunc :: IO()
debugFunc = do
putStrLn "Running Debug Option"
-- Debug test code goes here...


--------------------------------------------------------------------------------
-- Command Line Options Argument Parsers
--------------------------------------------------------------------------------
Expand Down
5 changes: 2 additions & 3 deletions projects/br_scs/src/Mirza/BusinessRegistry/Service.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
-- | Endpoint definitions go here. Most of the endpoint definitions are
-- light wrappers around functions in BeamQueries
module Mirza.BusinessRegistry.Service
(
appHandlers
( appHandlers
, publicServer
, privateServer
, appMToHandler
, serveSwaggerAPI
, module Handlers
) where
) where

import Mirza.Common.Utils

Expand Down
63 changes: 32 additions & 31 deletions projects/br_scs/src/Mirza/BusinessRegistry/Types.hs
Original file line number Diff line number Diff line change
@@ -1,59 +1,60 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Mirza.BusinessRegistry.Types (
module Mirza.BusinessRegistry.Types
, module CT
) where

import Mirza.Common.Time (CreationTime,
ExpirationTime,
RevocationTime)
import Mirza.Common.Types as CT
import Mirza.Common.Time (CreationTime,
ExpirationTime,
RevocationTime)
import Mirza.Common.Types as CT

import Data.GS1.EPC as EPC
import Data.GS1.EPC as EPC

import Data.Pool as Pool
import Data.Pool as Pool

import Database.Beam
import Database.Beam.Backend.SQL
import Database.Beam.Postgres.Syntax (PgDataTypeSyntax)
import Database.PostgreSQL.Simple (Connection, SqlError)
import qualified Database.Beam.Migrate as BMigrate
import qualified Database.Beam.Postgres as BPostgres
import Database.Beam.Postgres.Syntax (PgDataTypeSyntax)
import Database.PostgreSQL.Simple (Connection, SqlError)
import Database.PostgreSQL.Simple.FromField (FromField, fromField)
import Database.PostgreSQL.Simple.ToField (ToField, toField)
import qualified Database.Beam.Migrate as BMigrate
import qualified Database.Beam.Postgres as BPostgres

import Crypto.JOSE (JWK)
import Crypto.JWT (Audience, ClaimsSet, claimSub, string)
import Crypto.JOSE (JWK)
import Crypto.JWT (Audience, ClaimsSet,
claimSub, string)

import qualified Servant.Auth.Server as SAS
import qualified Servant.Auth.Server as SAS

import Katip as K
import Katip as K

import Network.URI (URI)

import Control.Lens
import Network.URI (URI)

import Data.Aeson
import Data.Aeson.Types
import Data.Aeson.TH
import Data.Aeson.Types

import Data.Swagger
import Data.Text (Text)
import Data.Time (LocalTime)
import Data.Text (Text)
import Data.Time (LocalTime)

import Data.Proxy (Proxy (..))
import Control.Lens
import Data.Proxy (Proxy (..))

import GHC.Generics (Generic)
import GHC.Stack (CallStack)
import GHC.Generics (Generic)
import GHC.Stack (CallStack)

-- *****************************************************************************
-- Context Types
Expand Down Expand Up @@ -258,12 +259,12 @@ longitudeType = BMigrate.DataType doubleType


data LocationResponse = LocationResponse
{ locationId :: PrimaryKeyType
, locationGLN :: EPC.LocationEPC
, locationBiz :: GS1CompanyPrefix
, geoLocId :: PrimaryKeyType
, geoLocCoord :: Maybe (Latitude, Longitude)
, geoLocAddress :: Maybe Text
{ locationId :: PrimaryKeyType
, locationGLN :: EPC.LocationEPC
, locationBiz :: GS1CompanyPrefix
, geoLocId :: PrimaryKeyType
, geoLocCoord :: Maybe (Latitude, Longitude)
, geoLocAddress :: Maybe Text
} deriving (Show, Generic, Eq)
instance ToSchema LocationResponse
instance ToJSON LocationResponse
Expand Down
49 changes: 24 additions & 25 deletions projects/br_scs/src/Mirza/SupplyChain/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,44 @@ import Mirza.SupplyChain.API
import Mirza.SupplyChain.Auth
import Mirza.SupplyChain.Database.Migrate
import Mirza.SupplyChain.Service
import Mirza.SupplyChain.Types (AppError, EnvType (..),
SCSContext (..), User)
import Mirza.SupplyChain.Types (AppError, EnvType (..),
SCSContext (..), User)

import Mirza.SupplyChain.PopulateUtils (insertCitrusData)
import qualified Mirza.SupplyChain.Types as ST
import Mirza.SupplyChain.PopulateUtils (insertCitrusData)
import qualified Mirza.SupplyChain.Types as ST

import Mirza.BusinessRegistry.Client.Servant

import Servant
import Servant.Client
import Servant.Swagger.UI

import qualified Data.Pool as Pool
import qualified Data.Pool as Pool
import Database.PostgreSQL.Simple

import Network.HTTP.Client (defaultManagerSettings,
newManager)
import Network.Wai (Middleware)
import qualified Network.Wai.Handler.Warp as Warp
import Network.HTTP.Client (defaultManagerSettings,
newManager)
import Network.Wai (Middleware)
import qualified Network.Wai.Handler.Warp as Warp

import Data.ByteString (ByteString)
import Data.Text (pack)
import Data.ByteString (ByteString)
import Data.Text (pack)

import Data.Semigroup ((<>))
import Data.Semigroup ((<>))
import Options.Applicative

import Control.Lens

import qualified Crypto.Scrypt as Scrypt
import qualified Crypto.Scrypt as Scrypt

import Control.Exception (finally)
import Data.Maybe (fromMaybe)
import Katip as K
import Control.Exception (finally)
import Data.Maybe (fromMaybe)
import Katip as K

import System.Exit (exitFailure)
import System.IO (IOMode (AppendMode),
hPutStrLn, openFile,
stderr, stdout)
import System.Exit (exitFailure)
import System.IO (IOMode (AppendMode),
hPutStrLn, openFile,
stderr, stdout)

data ServerOptionsSCS = ServerOptionsSCS
{ env :: EnvType
Expand Down Expand Up @@ -125,13 +125,12 @@ main = runProgram =<< execParser opts
<> header "SupplyChainServer - A server for capturing GS1 events and recording them on a blockchain")

runProgram :: ServerOptionsSCS -> IO ()
runProgram so@ServerOptionsSCS{initDB = True, dbPopulateInfo =Just _, brServiceInfo =Just __} = do
runProgram so@ServerOptionsSCS{initDB = True, dbPopulateInfo =Just _, brServiceInfo =Just _} = do
ctx <- initSCSContext so
migrate ctx $ connectionStr so
runDbPopulate so
runProgram so@ServerOptionsSCS{initDB =False, dbPopulateInfo =Just _, brServiceInfo =Just __} = do
runDbPopulate so
runProgram so@ServerOptionsSCS{initDB = False, scsServiceInfo=(scsHst, scsPort), brServiceInfo =Just __} = do
runProgram so@ServerOptionsSCS{initDB =False, dbPopulateInfo =Just _, brServiceInfo =Just _} = runDbPopulate so
runProgram so@ServerOptionsSCS{initDB = False, scsServiceInfo=(scsHst, scsPort), brServiceInfo =Just _} = do
ctx <- initSCSContext so
app <- initApplication so ctx
mids <- initMiddleware so
Expand Down Expand Up @@ -188,7 +187,7 @@ initSCSContext (ServerOptionsSCS envT _ _ dbConnStr _ n p r lev (Just (brHost, b
mempty
mempty
(mkClientEnv manager baseUrl)
initSCSContext so@(ServerOptionsSCS{ brServiceInfo = Nothing}) = initSCSContext so{brServiceInfo = Just ("localhost", 8200)}
initSCSContext so@ServerOptionsSCS{brServiceInfo = Nothing} = initSCSContext so{brServiceInfo = Just ("localhost", 8200)}

initApplication :: ServerOptionsSCS -> ST.SCSContext -> IO Application
initApplication _so ev =
Expand Down
15 changes: 9 additions & 6 deletions projects/entity-data-api/EntityDataAPI.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
ARG HS_BUILDER_IMAGE=hsbuilder:latest

FROM $HS_BUILDER_IMAGE as BUILD
WORKDIR /edapi
RUN mkdir -p /src/edapi
WORKDIR /src/edapi

COPY stack.yaml entity-data-api.cabal LICENSE README.md /edapi/
COPY src/ /edapi/src/
COPY app/ /edapi/app/
COPY stack.yaml entity-data-api.cabal LICENSE README.md /src/edapi/
COPY src/ /src/edapi/src/
COPY app/ /src/edapi/app/

RUN /usr/local/bin/stack install --test --dependencies-only --ghc-options='-O2 -j -fPIC' 2>&1

RUN mkdir /edapi/dist/ && \
RUN mkdir /src/edapi/dist/ && \
/usr/local/bin/stack install --ghc-options='-O2 -j -fPIC' 2>&1


Expand All @@ -18,6 +19,8 @@ FROM ubuntu:18.04 as PKG-EDAPI
RUN apt update && \
apt install -y libpq-dev libffi-dev ca-certificates

COPY --from=0 /edapi/dist/entity-data-api /opt/Mirza/entity-data-api
RUN pwd; ls /; ls /src/edapi; ls /src

COPY /src/edapi/dist/entity-data-api /opt/Mirza/entity-data-api

ENTRYPOINT [ "/opt/Mirza/entity-data-api" ]
3 changes: 3 additions & 0 deletions projects/entity-data-api/database/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE users (
user_sub text NOT NULL PRIMARY KEY
);
13 changes: 8 additions & 5 deletions projects/entity-data-api/entity-data-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ library
, Mirza.EntityDataAPI.AuthProxy
, Mirza.EntityDataAPI.Utils
, Mirza.EntityDataAPI.Main
, Mirza.EntityDataAPI.Database.Utils
, Mirza.EntityDataAPI.Errors

-- Other library packages from which modules are imported.
build-depends: base
Expand All @@ -80,7 +82,9 @@ library
, monad-time
, optparse-applicative
, jose
, postgresql-simple
, req
, resource-pool
, wai
, warp
, http-types
Expand All @@ -94,13 +98,12 @@ library
default-language: Haskell2010


executable entity-data-api
default-extensions: OverloadedStrings
, FlexibleContexts
, FlexibleInstances
executable entity-data-api-proxy
default-extensions: OverloadedStrings
, FlexibleContexts
, FlexibleInstances
-- .hs or .lhs file containing the Main module.
main-is: EntityDataAPI.hs

-- Modules included in this executable, other than Main.
-- other-modules:

Expand Down
Loading