Skip to content
This repository was archived by the owner on Nov 16, 2020. It is now read-only.

Commit 70daecb

Browse files
seanhuxyberndtj
authored andcommitted
fix an api-gateway bug (#177)
* fix a api-gateway bug related to cors #174 - enable query params to http body tranform for OPTIONS method, previous only for GET - automatically add OPTIONS method for cors-enabled api - will not add OPTIONS method while register the corresponding kong cors plugin, this is a hard requirement from kong, as listed in the issue too. Also: for convenience of local dev: print Kong https and http port in dispatch install output which looks like this: ``` $ dispatch install --file config.yaml --charts-dir charts ... Installing charts/kong helm chart Installing charts/openfaas helm chart Installing charts/dispatch helm chart dispatch api-gateway is running at http port 32519 and https port 31841 Config file written to: /Users/xueyangh/.dispatch/config.json ... note: the line for api-gateway port is only printed only when kong is deployed at nodePort mode ``` * import api-gateway ports into ~/.dispatch/config.json * re-trigger e2e * revert changes in e2e
1 parent 31042d7 commit 70daecb

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

charts/kong/dispatch-transformer/handler.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ local function transform_method(conf)
6060

6161
-- for GET to POST transform:
6262
-- tranform query strings into the req body
63-
if org_http_method == "GET" and rpl_http_method == "POST" then
63+
-- hack: cors sends OPTIONS as a preflight query
64+
-- hence, we also need to support OPTIONS
65+
if (org_http_method == "GET" or org_http_method == "OPTIONS") and rpl_http_method == "POST" then
6466
local body = {}
6567
local args = ngx.req.get_uri_args()
6668
for key, val in pairs(args) do

e2e/tests/variables.bash

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/bash
22

33
### COMMON VARIABLES ###
4-
54
: ${DOCKER_REGISTRY:="vmware"}
65
: ${BASE_IMAGE_PYTHON3:="dispatch-openfaas-python-base:0.0.5-dev1"}
76
: ${BASE_IMAGE_NODEJS6:="dispatch-openfaas-nodejs6-base:0.0.3-dev1"}

pkg/api-manager/gateway/kong/kong.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ func (k *Client) apiEntityToKong(entity *gateway.API) *API {
116116
} else {
117117
a.HTTPSOnly = false
118118
}
119+
if entity.CORS == true {
120+
// note: OPTIONS is a CORS preflight request
121+
// it is added by dispatch automatically
122+
// users should not add them mannually
123+
a.Methods = append(a.Methods, "OPTIONS")
124+
}
119125
return &a
120126
}
121127

@@ -237,7 +243,9 @@ func (k *Client) AddAPI(entity *gateway.API) (*gateway.API, error) {
237243
Config: map[string]interface{}{
238244
// TODO: '*' for now, should be able to configure the origin later
239245
"config.origins": "*",
240-
"config.methods": strings.Join(a.Methods, ","),
246+
// Workaround: fix https://github.com/vmware/dispatch/issues/174
247+
// OPTIIONS is not an allowed method in kong cors plugin
248+
"config.methods": strings.Join(entity.Methods, ","),
241249
},
242250
}
243251
err := k.updatePluginByName(a.Name, corsPlugin.Name, &corsPlugin)
@@ -290,7 +298,9 @@ func (k *Client) UpdateAPI(name string, entity *gateway.API) (*gateway.API, erro
290298
Config: map[string]interface{}{
291299
// TODO: '*' for now, should be able to configure the origin later
292300
"config.origins": "*",
293-
"config.methods": strings.Join(a.Methods, ","),
301+
// Workaround: fix https://github.com/vmware/dispatch/issues/174
302+
// OPTIIONS is not an allowed method in kong cors plugin
303+
"config.methods": strings.Join(entity.Methods, ","),
294304
},
295305
}
296306
err := k.updatePluginByName(name, "cors", &corsPlugin)

pkg/dispatchcli/cmd/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ var dispatchConfig struct {
2626
SkipAuth bool `json:"skipauth"`
2727
Insecure bool `json:"insecure"`
2828
Json bool `json:"-"`
29+
APIHTTPSPort int `json:"api-https-port"`
30+
APIHTTPPort int `json:"api-http-port"`
2931
}
3032

3133
var validResources = i18n.T(`Valid resource types include:

pkg/dispatchcli/cmd/install.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func installCert(out, errOut io.Writer, configDir, namespace, domain string, tls
160160
var key, cert string
161161
if tls.CertFile != "" {
162162
if tls.PrivateKey == "" {
163-
return errors.New("Error installing certificate: missing private key for the tls cert.")
163+
return errors.New("error installing certificate: missing private key for the tls cert")
164164
}
165165
key = tls.PrivateKey
166166
cert = tls.CertFile
@@ -306,6 +306,10 @@ func writeConfig(out, errOut io.Writer, configDir string, config *installConfig)
306306
if err != nil {
307307
return err
308308
}
309+
if config.APIGateway.ServiceType == "NodePort" {
310+
fmt.Fprintf(out, "dispatch api-gateway is running at http port: %d and https port: %d\n",
311+
dispatchConfig.APIHTTPPort, dispatchConfig.APIHTTPSPort)
312+
}
309313
if installDryRun {
310314
fmt.Fprintf(out, "Copy the following to your %s/config.json\n", configDir)
311315
fmt.Fprintln(out, string(b))
@@ -486,17 +490,15 @@ func runInstall(out, errOut io.Writer, cmd *cobra.Command, args []string) error
486490
if config.APIGateway.ServiceType == "NodePort" {
487491

488492
service := fmt.Sprintf("%s-kongproxy", config.APIGateway.Chart.Release)
489-
httpsPort, err := getK8sServiceNodePort(service, config.APIGateway.Chart.Namespace, true)
493+
dispatchConfig.APIHTTPSPort, err = getK8sServiceNodePort(service, config.APIGateway.Chart.Namespace, true)
490494
if err != nil {
491495
return err
492496
}
493-
httpPort, err := getK8sServiceNodePort(service, config.APIGateway.Chart.Namespace, false)
497+
dispatchConfig.APIHTTPPort, err = getK8sServiceNodePort(service, config.APIGateway.Chart.Namespace, false)
494498
if err != nil {
495499
return err
496500
}
497501

498-
os.Setenv("API_GATEWAY_HTTPS_PORT", strconv.Itoa(httpsPort))
499-
os.Setenv("API_GATEWAY_HTTP_PORT", strconv.Itoa(httpPort))
500502
}
501503
}
502504

0 commit comments

Comments
 (0)