Skip to content

Commit

Permalink
Pass all requests to OpenStack through proxy server
Browse files Browse the repository at this point in the history
  • Loading branch information
c-mart committed May 31, 2019
1 parent c1d2895 commit 617083c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
53 changes: 50 additions & 3 deletions src/Rest/Helpers.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Rest.Helpers exposing (openstackCredentialedRequest)
module Rest.Helpers exposing (openstackCredentialedRequest, proxyifyRequest)

import Helpers.Helpers as Helpers
import Http
import OpenStack.Types as OSTypes
import Task
import Time
import Types.Types as TT
import Url


httpRequestMethodStr : TT.HttpRequestMethod -> String
Expand All @@ -31,12 +32,16 @@ openstackCredentialedRequest project method url requestBody expect =
-}
let
( proxyUrl, headers ) =
-- TODO don't hard-code proxy server URL, specify it in global defaults or something
proxyifyRequest "https://dogfood.exosphere.app/proxy" url

tokenToRequestCmd : OSTypes.AuthTokenString -> Cmd TT.Msg
tokenToRequestCmd token =
Http.request
{ method = httpRequestMethodStr method
, headers = [ Http.header "X-Auth-Token" token ]
, url = url
, headers = [ Http.header "X-Auth-Token" token ] ++ headers
, url = proxyUrl
, body = requestBody
, expect = expect
, timeout = Nothing
Expand All @@ -46,3 +51,45 @@ openstackCredentialedRequest project method url requestBody expect =
Task.perform
(\posixTime -> TT.ProjectMsg (Helpers.getProjectId project) (TT.ValidateTokenForCredentialedRequest tokenToRequestCmd posixTime))
Time.now


proxyifyRequest : String -> String -> ( String, List Http.Header )
proxyifyRequest proxyServerUrl requestUrlStr =
{- Returns URL to pass to proxy server, and a list of HTTP headers -}
let
{- Todo should we pass Url.Url around the app instead of URLs as strings? The following string-to-URL conversion is ugly -}
defaultUrl =
Url.Url
Url.Https
"thisisbroken.pizza"
Nothing
""
Nothing
Nothing

requestUrl =
Url.fromString requestUrlStr
|> Maybe.withDefault defaultUrl

origHost =
requestUrl.host

origPort =
requestUrl.port_ |> Maybe.withDefault 443

pathQuery =
case requestUrl.query of
Just query ->
requestUrl.path ++ "?" ++ query

Nothing ->
requestUrl.path

proxyRequestUrl =
proxyServerUrl ++ pathQuery
in
( proxyRequestUrl
, [ Http.header "exo-proxy-orig-host" origHost
, Http.header "exo-proxy-orig-port" <| String.fromInt origPort
]
)
18 changes: 12 additions & 6 deletions src/Rest/Rest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,24 @@ requestAuthToken creds =
]
)
]
in
{- https://stackoverflow.com/questions/44368340/get-request-headers-from-http-request -}
Http.request
{ method = "POST"
, headers = []
, url =

authUrl =
if String.contains "/auth/tokens" creds.authUrl then
-- We previously expected users to provide "/auth/tokens" to be in the Keystone Auth URL; this case statement avoids breaking the app for users who still have that
creds.authUrl

else
creds.authUrl ++ "/auth/tokens"

( proxyUrl, headers ) =
-- TODO don't hard-code proxy server URL, specify it in global defaults or something
proxyifyRequest "https://dogfood.exosphere.app/proxy" authUrl
in
{- https://stackoverflow.com/questions/44368340/get-request-headers-from-http-request -}
Http.request
{ method = "POST"
, headers = headers
, url = proxyUrl
, body = Http.jsonBody requestBody

{- Todo handle no response? -}
Expand Down

0 comments on commit 617083c

Please sign in to comment.