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

Support nginx conf #1130

Merged
merged 23 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
add more test for parse
  • Loading branch information
suchen-sci committed Nov 7, 2023
commit 970e3aff62fe381e6f47e329473305429a0c2e10
44 changes: 37 additions & 7 deletions cmd/client/commandv2/convert/nginx/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ package nginx
import (
"fmt"
"math/rand"
"os"
"path/filepath"

"github.com/megaease/easegress/v2/cmd/client/commandv2/common"
"github.com/megaease/easegress/v2/cmd/client/general"
"github.com/megaease/easegress/v2/pkg/util/codectool"
crossplane "github.com/nginxinc/nginx-go-crossplane"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -56,13 +60,9 @@ func Cmd() *cobra.Command {
if err != nil {
general.ExitWithError(err)
}
printYaml(hs)
printYaml(pls)
// data, err := json.Marshal(payload)
// if err != nil {
// general.ExitWithError(err)
// }
// fmt.Println(string(data))
if err := writeYaml(flags.Output, hs, pls); err != nil {
general.ExitWithError(err)
}
},
}
cmd.Flags().StringVarP(&flags.NginxConf, "file", "f", "", "nginx.conf file path")
Expand Down Expand Up @@ -100,3 +100,33 @@ func (opt *Options) GetPipelineName(path string) string {
}
return opt.GetPipelineName(path)
}

func writeYaml(filename string, servers []*common.HTTPServerSpec, pipelines []*common.PipelineSpec) error {
absPath, err := filepath.Abs(filename)
if err != nil {
return err
}
file, err := os.Create(absPath)
if err != nil {
return err
}
defer file.Close()

for _, s := range servers {
data, err := codectool.MarshalYAML(s)
if err != nil {
return err
}
file.WriteString(string(data))
file.WriteString("\n---\n")
}
for _, p := range pipelines {
data, err := codectool.MarshalYAML(p)
if err != nil {
return err
}
file.WriteString(string(data))
file.WriteString("\n---\n")
}
return nil
}
29 changes: 29 additions & 0 deletions cmd/client/commandv2/convert/nginx/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nginx

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCmd(t *testing.T) {
cmd := Cmd()
assert.NotNil(t, cmd)
}
3 changes: 3 additions & 0 deletions cmd/client/commandv2/convert/nginx/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func getWebsocketFilter(info *ProxyInfo) *httpproxy.WebSocketProxySpec {

func getProxyFilter(info *ProxyInfo) *httpproxy.Spec {
spec := common.NewProxyFilterSpec("proxy")
spec.Compression = &httpproxy.CompressionSpec{
MinLength: uint32(info.GzipMinLength),
}
spec.Pools = []*httpproxy.ServerPoolSpec{{
BaseServerPoolSpec: getBaseServerPool(info),
}}
Expand Down
41 changes: 39 additions & 2 deletions cmd/client/commandv2/convert/nginx/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ type ServerEnv struct {
type ProxyEnv struct {
Pass *Directive `json:"pass"`
ProxySetHeader []*Directive `json:"proxy_set_header"`
Gzip *GzipEnv `json:"gzip"`
}

type GzipEnv struct {
Gzip *Directive `json:"gzip"`
GzipMinLength *Directive `json:"gzip_min_length"`
}

func (env *Env) init() {
Expand All @@ -80,6 +86,8 @@ func (env *Env) init() {
"ssl_certificate_key": func(d *Directive) { env.Server.SSLCertificateKey = append(env.Server.SSLCertificateKey, d) },
"proxy_set_header": func(d *Directive) { env.Proxy.ProxySetHeader = append(env.Proxy.ProxySetHeader, d) },
"upstream": func(d *Directive) { env.Upstream = append(env.Upstream, d) },
"gzip": func(d *Directive) { env.Proxy.Gzip.Gzip = d },
"gzip_min_length": func(d *Directive) { env.Proxy.Gzip.GzipMinLength = d },
}
}

Expand All @@ -91,6 +99,7 @@ func newEnv() *Env {
},
Proxy: &ProxyEnv{
ProxySetHeader: make([]*Directive, 0),
Gzip: &GzipEnv{},
},
Upstream: make([]*Directive, 0),
}
Expand Down Expand Up @@ -195,12 +204,40 @@ func (env *Env) GetProxyInfo() (*ProxyInfo, error) {
}
}

gzipMinLength := processGzip(p.Gzip)

return &ProxyInfo{
Servers: servers,
SetHeaders: setHeaders,
Servers: servers,
SetHeaders: setHeaders,
GzipMinLength: gzipMinLength,
}, nil
}

func processGzip(gzip *GzipEnv) int {
if gzip.Gzip == nil {
return 0
}
mustContainArgs(gzip.Gzip, 1)
if gzip.Gzip.Args[0] != "on" {
return 0
}
if gzip.GzipMinLength == nil {
// nginx default value
return 20
}
mustContainArgs(gzip.GzipMinLength, 1)
minLength, err := strconv.Atoi(gzip.GzipMinLength.Args[0])
if err != nil {
general.Warnf("%s: invalid number %v, use default value of 20 instead", directiveInfo(gzip.GzipMinLength), err)
return 20
}
if minLength < 0 {
general.Warnf("%s: negative number, use default value of 20 instead", directiveInfo(gzip.GzipMinLength))
return 20
}
return minLength
}

func processProxySetHeader(ds []*Directive) (map[string]string, error) {
res := make(map[string]string)
for _, d := range ds {
Expand Down
142 changes: 127 additions & 15 deletions cmd/client/commandv2/convert/nginx/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
package nginx

import (
"bytes"
"fmt"
"html/template"
"testing"

"github.com/megaease/easegress/v2/pkg/util/codectool"
crossplane "github.com/nginxinc/nginx-go-crossplane"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -154,10 +157,7 @@ func TestParsePayload(t *testing.T) {
server {
listen 80;

location / {
proxy_set_header X-Path "prefix";
proxy_pass http:https://localhost:8080;
}
server_name www.example.com *.example.com;

location /apis {
proxy_set_header X-Path "apis";
Expand All @@ -169,11 +169,9 @@ func TestParsePayload(t *testing.T) {
}
}

location = /user {
proxy_pass http:https://localhost:8890;
}

location /upstream {
gzip on;
gzip_min_length 1000;
proxy_pass http:https://backend;
}

Expand All @@ -184,20 +182,134 @@ func TestParsePayload(t *testing.T) {
proxy_pass http:https://localhost:9090;
}
}

server {
listen 127.0.0.1:443 ssl;
ssl_client_certificate {{ .CaFile }};
ssl_certificate {{ .CertFile }};
ssl_certificate_key {{ .KeyFile }};

location = /user {
proxy_pass http:https://localhost:9999;
}

location ^~ /user/admin {
proxy_pass http:https://localhost:9991;
}

location ~* /user/.* {
proxy_pass http:https://localhost:9992;
}

location ~ /user/.* {
proxy_pass http:https://localhost:9993;
}
}
}
`
file := tempDir.Create("nginx.conf", []byte(nginxConf))
tmplValue := map[string]string{
"CaFile": tempDir.Create("ca.crt", []byte("ca")),
"CertFile": tempDir.Create("cert.crt", []byte("cert")),
"KeyFile": tempDir.Create("key.crt", []byte("key")),
}

tmpl, err := template.New("nginx").Parse(nginxConf)
assert.Nil(t, err)
var buffer bytes.Buffer
tmpl.Execute(&buffer, tmplValue)

file := tempDir.Create("nginx.conf", buffer.Bytes())
payload, err := crossplane.Parse(file, &crossplane.ParseOptions{})
assert.Nil(t, err)
config, err := parsePayload(payload)
assert.Nil(t, err)
// printJson(config)

opt := &Options{Prefix: "test"}
opt.init()
hs, pls, err := convertConfig(opt, config)
proxyInfo := `
servers:
- port: 80
rules:
- hosts:
- value: www.example.com
isRegexp: false
- value: '*.example.com'
isRegexp: false
paths:
- path: /apis
type: prefix
backend:
servers:
- server: http:https://localhost:8880
weight: 1
setHeaders:
X-Path: apis
- path: /apis/v1
type: prefix
backend:
servers:
- server: http:https://localhost:8888
weight: 1
setHeaders:
X-Path: apis/v1
- path: /upstream
type: prefix
backend:
servers:
- server: http:https://localhost:1234
weight: 1
- server: http:https://localhost:2345
weight: 10
gzipMinLength: 1000
- path: /websocket
type: prefix
backend:
servers:
- server: http:https://localhost:9090
weight: 1
setHeaders:
Connection: $connection_upgrade
Upgrade: $http_upgrade
- port: 443
address: 127.0.0.1
https: true
caCert: Y2E=
certs:
{{ .CertFile }}: Y2VydA==
keys:
{{ .CertFile }}: a2V5
rules:
- paths:
- path: /user
type: exact
backend:
servers:
- server: http:https://localhost:9999
weight: 1
- path: /user/admin
type: prefix
backend:
servers:
- server: http:https://localhost:9991
weight: 1
- path: /user/.*
type: caseInsensitiveRegexp
backend:
servers:
- server: http:https://localhost:9992
weight: 1
- path: /user/.*
type: regexp
backend:
servers:
- server: http:https://localhost:9993
weight: 1
`
tmp, err := template.New("proxyInfo").Parse(proxyInfo)
assert.Nil(t, err)
var proxyBuffer bytes.Buffer
tmp.Execute(&proxyBuffer, tmplValue)
expected := &Config{}
err = codectool.UnmarshalYAML(proxyBuffer.Bytes(), expected)
assert.Nil(t, err)
printJson(hs)
printYaml(pls)
assert.Equal(t, expected, config)
}
}
Loading
Loading