Merge pull request #69 from begriffs/https-redirect
Remove internal TLS, add option for HTTPS redirection
This commit is contained in:
+5
-5
@@ -1,5 +1,5 @@
|
|||||||
name: dbapi
|
name: dbapi
|
||||||
version: 0.1.0.0
|
version: 0.2.0.0
|
||||||
synopsis: The database is your api
|
synopsis: The database is your api
|
||||||
license: MIT
|
license: MIT
|
||||||
license-file: LICENSE
|
license-file: LICENSE
|
||||||
@@ -20,7 +20,7 @@ executable dbapi
|
|||||||
, HTTP, convertible
|
, HTTP, convertible
|
||||||
, case-insensitive
|
, case-insensitive
|
||||||
, http-types, scientific, time
|
, http-types, scientific, time
|
||||||
, bytestring, aeson, network
|
, bytestring, aeson, network >= 2.6
|
||||||
, text , containers
|
, text , containers
|
||||||
, optparse-applicative >= 0.9.1 && < 0.10
|
, optparse-applicative >= 0.9.1 && < 0.10
|
||||||
, unordered-containers
|
, unordered-containers
|
||||||
@@ -29,10 +29,10 @@ executable dbapi
|
|||||||
, http-media, regex-tdfa
|
, http-media, regex-tdfa
|
||||||
, Ranged-sets
|
, Ranged-sets
|
||||||
, transformers
|
, transformers
|
||||||
, warp-tls
|
|
||||||
, bcrypt
|
, bcrypt
|
||||||
, base64-string
|
, base64-string
|
||||||
, split
|
, split
|
||||||
|
, network-uri >= 2.6
|
||||||
Other-Modules: Dbapi
|
Other-Modules: Dbapi
|
||||||
, PgStructure
|
, PgStructure
|
||||||
, PgQuery
|
, PgQuery
|
||||||
@@ -55,7 +55,7 @@ Test-Suite spec
|
|||||||
, case-insensitive
|
, case-insensitive
|
||||||
, wai-extra, wai-cors, containers
|
, wai-extra, wai-cors, containers
|
||||||
, http-types, scientific, time
|
, http-types, scientific, time
|
||||||
, bytestring, aeson, network
|
, bytestring, aeson, network >= 2.6
|
||||||
, text, optparse-applicative
|
, text, optparse-applicative
|
||||||
, unordered-containers
|
, unordered-containers
|
||||||
, regex-base
|
, regex-base
|
||||||
@@ -63,7 +63,7 @@ Test-Suite spec
|
|||||||
, http-media, regex-tdfa
|
, http-media, regex-tdfa
|
||||||
, Ranged-sets
|
, Ranged-sets
|
||||||
, transformers
|
, transformers
|
||||||
, warp-tls
|
|
||||||
, bcrypt
|
, bcrypt
|
||||||
, base64-string
|
, base64-string
|
||||||
, split
|
, split
|
||||||
|
, network-uri >= 2.6
|
||||||
|
|||||||
+1
-2
@@ -49,9 +49,8 @@ import Codec.Binary.Base64.String (decode)
|
|||||||
data AppConfig = AppConfig {
|
data AppConfig = AppConfig {
|
||||||
configDbUri :: String
|
configDbUri :: String
|
||||||
, configPort :: Int
|
, configPort :: Int
|
||||||
, configSslCert :: FilePath
|
|
||||||
, configSslKey :: FilePath
|
|
||||||
, configAnonRole :: String
|
, configAnonRole :: String
|
||||||
|
, configSecure :: Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonContentType :: (HeaderName, BS.ByteString)
|
jsonContentType :: (HeaderName, BS.ByteString)
|
||||||
|
|||||||
+11
-9
@@ -4,14 +4,14 @@
|
|||||||
|
|
||||||
module Main where
|
module Main where
|
||||||
import Dbapi
|
import Dbapi
|
||||||
import Middleware (reportPgErrors)
|
import Middleware (reportPgErrors, redirectInsecure)
|
||||||
import Network.Wai.Handler.Warp hiding (Connection)
|
import Network.Wai.Handler.Warp hiding (Connection)
|
||||||
import Database.HDBC.PostgreSQL (connectPostgreSQL')
|
import Database.HDBC.PostgreSQL (connectPostgreSQL')
|
||||||
import Data.String.Conversions (cs)
|
import Data.String.Conversions (cs)
|
||||||
|
|
||||||
|
import Control.Monad (unless)
|
||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
import Options.Applicative hiding (columns)
|
import Options.Applicative hiding (columns)
|
||||||
import Network.Wai.Handler.WarpTLS (tlsSettings, runTLS)
|
|
||||||
import Network.Wai.Middleware.Gzip (gzip, def)
|
import Network.Wai.Middleware.Gzip (gzip, def)
|
||||||
import Network.Wai.Middleware.Cors (cors)
|
import Network.Wai.Middleware.Cors (cors)
|
||||||
|
|
||||||
@@ -23,12 +23,10 @@ argParser = AppConfig
|
|||||||
<> help "database uri to expose, e.g. postgres://user:pass@host:port/database")
|
<> help "database uri to expose, e.g. postgres://user:pass@host:port/database")
|
||||||
<*> option (long "port" <> short 'p' <> metavar "NUMBER" <> value 3000
|
<*> option (long "port" <> short 'p' <> metavar "NUMBER" <> value 3000
|
||||||
<> help "port number on which to run HTTP server")
|
<> help "port number on which to run HTTP server")
|
||||||
<*> strOption (long "sslcert" <> short 'c' <> metavar "PATH" <> value "test/test.crt"
|
|
||||||
<> help "path to SSL cert file")
|
|
||||||
<*> strOption (long "sslkey" <> short 'k' <> metavar "PATH" <> value "test/test.key"
|
|
||||||
<> help "path to SSL key file")
|
|
||||||
<*> strOption (long "anonymous" <> short 'a' <> metavar "ROLE"
|
<*> strOption (long "anonymous" <> short 'a' <> metavar "ROLE"
|
||||||
<> help "postgres role to use for non-authenticated requests")
|
<> help "postgres role to use for non-authenticated requests")
|
||||||
|
<*> switch (long "secure" <> short 's'
|
||||||
|
<> help "Redirect all requests to HTTPS" )
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
@@ -36,12 +34,16 @@ main = do
|
|||||||
let port = configPort conf
|
let port = configPort conf
|
||||||
let dburi = configDbUri conf
|
let dburi = configDbUri conf
|
||||||
|
|
||||||
let tls = tlsSettings (configSslCert conf) (configSslKey conf)
|
unless (configSecure conf) $
|
||||||
let settings = setPort port defaultSettings
|
putStrLn "WARNING, running in insecure mode, auth will be in plaintext"
|
||||||
|
|
||||||
Prelude.putStrLn $ "Listening on port " ++ (show $ configPort conf :: String)
|
Prelude.putStrLn $ "Listening on port " ++ (show $ configPort conf :: String)
|
||||||
conn <- connectPostgreSQL' dburi
|
conn <- connectPostgreSQL' dburi
|
||||||
runTLS tls settings $ gzip def $ cors corsPolicy $ reportPgErrors $ app conn (cs $ configAnonRole conf)
|
run port $ (if configSecure conf then redirectInsecure else id)
|
||||||
|
$ gzip def
|
||||||
|
$ cors corsPolicy
|
||||||
|
$ reportPgErrors
|
||||||
|
$ app conn (cs $ configAnonRole conf)
|
||||||
|
|
||||||
where
|
where
|
||||||
describe = progDesc "create a REST API to an existing Postgres database"
|
describe = progDesc "create a REST API to an existing Postgres database"
|
||||||
|
|||||||
+27
-3
@@ -5,12 +5,14 @@ module Middleware where
|
|||||||
|
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
|
|
||||||
import Network.HTTP.Types.Header (hContentType)
|
import Network.HTTP.Types.Header (hContentType, hLocation)
|
||||||
import Network.HTTP.Types.Status (status400)
|
import Network.HTTP.Types.Status (status400, status301)
|
||||||
import Database.HDBC.Types (SqlError(..))
|
import Database.HDBC.Types (SqlError(..))
|
||||||
import Control.Exception (catchJust)
|
import Control.Exception (catchJust)
|
||||||
|
import Data.String.Conversions (cs)
|
||||||
import Network.Wai
|
import Network.Wai
|
||||||
|
import Network.URI (URI(..), parseURI)
|
||||||
|
import Data.Monoid (mconcat)
|
||||||
|
|
||||||
instance ToJSON SqlError where
|
instance ToJSON SqlError where
|
||||||
toJSON t = object [
|
toJSON t = object [
|
||||||
@@ -31,3 +33,25 @@ reportPgErrors app req respond =
|
|||||||
where
|
where
|
||||||
isPgException :: SqlError -> Maybe SqlError
|
isPgException :: SqlError -> Maybe SqlError
|
||||||
isPgException = Just
|
isPgException = Just
|
||||||
|
|
||||||
|
|
||||||
|
redirectInsecure :: Middleware
|
||||||
|
redirectInsecure app req respond = do
|
||||||
|
let hdrs = requestHeaders req
|
||||||
|
host = lookup "host" hdrs
|
||||||
|
uriM = parseURI . cs =<< mconcat [
|
||||||
|
Just "https://",
|
||||||
|
host,
|
||||||
|
Just $ rawPathInfo req,
|
||||||
|
Just $ rawQueryString req]
|
||||||
|
isHerokuSecure = lookup "x-forwarded-proto" hdrs == Just "https"
|
||||||
|
|
||||||
|
if not (isSecure req || isHerokuSecure)
|
||||||
|
then case uriM of
|
||||||
|
Just uri ->
|
||||||
|
respond $ responseLBS status301 [
|
||||||
|
(hLocation, cs . show $ uri { uriScheme = "https:" })
|
||||||
|
] ""
|
||||||
|
Nothing ->
|
||||||
|
respond $ responseLBS status400 [] "SSL is required"
|
||||||
|
else app req respond
|
||||||
|
|||||||
+1
-1
@@ -29,7 +29,7 @@ isLeft (Left _ ) = True
|
|||||||
isLeft _ = False
|
isLeft _ = False
|
||||||
|
|
||||||
cfg :: AppConfig
|
cfg :: AppConfig
|
||||||
cfg = AppConfig "postgres://dbapi_test:@localhost:5432/dbapi_test" 9000 "test/test.crt" "test/test.key" "dbapi_anonymous"
|
cfg = AppConfig "postgres://dbapi_test:@localhost:5432/dbapi_test" 9000 "dbapi_anonymous" False
|
||||||
|
|
||||||
openConnection :: IO Connection
|
openConnection :: IO Connection
|
||||||
openConnection = connectPostgreSQL' $ configDbUri cfg
|
openConnection = connectPostgreSQL' $ configDbUri cfg
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
-----BEGIN CERTIFICATE-----
|
|
||||||
MIICLTCCAZYCCQCj6GtISfdwNjANBgkqhkiG9w0BAQUFADBbMQswCQYDVQQGEwJV
|
|
||||||
UzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDVNhbiBGcmFuY2lzY28xEzARBgNVBAoT
|
|
||||||
Ckxvb3AgUmVjdXIxEjAQBgNVBAMTCWxvY2FsaG9zdDAeFw0xNDEwMDMyMDQyNDNa
|
|
||||||
Fw0xNTEwMDMyMDQyNDNaMFsxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQG
|
|
||||||
A1UEBxMNU2FuIEZyYW5jaXNjbzETMBEGA1UEChMKTG9vcCBSZWN1cjESMBAGA1UE
|
|
||||||
AxMJbG9jYWxob3N0MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC5gFnTRBge
|
|
||||||
mXdkCMD+OycujZrCWOOLIDBqRr7kDbxVXqz/TKHRVx6bz88g9egzvR2HLyA418kd
|
|
||||||
dAu+lMmjrRv/k0Lnk/UvC1aj0huoHpOVUgOwy3qS4cE663uU5qs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pLxZu2MSBqBenHZ5DIhBMOrkVMR0PrKeWsCQCHh
|
|
||||||
gsSkIysgpP7oSALFmSCh8a2c+ZUtP7EH8NzjTLsH/iVNVOvy2FPygBQcvZ2RcF95
|
|
||||||
naMeQCq9nrkQy/qTMqECQQDa1WjWS2ngwqGzHovqiKRg3lQ8MFNlvlStAQl8+9h0
|
|
||||||
KJj8Rl6h+gO8eretBIe+y5j/hCC90xlJotzwO3jJF6Hc
|
|
||||||
-----END RSA PRIVATE KEY-----
|
|
||||||
Reference in New Issue
Block a user