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

#648 add Conditional primitive supports params in path #651

Closed
wants to merge 12 commits into from
4 changes: 4 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
| Hao Dong | anotherwriter |
| Haobin zhang | zhanghaobin |
| Hui Yu | dblate |
| Gen Wang | gracewang510 |
| Jie Liu | freeHackOfJeff |
| Jie Wan | wanjiecs |
| Jin Tong | cumirror |
| Jiyang Zhang | scriptkids |
| Kaiyu Zheng | kaiyuzheng |
| Lidong Chang | changlidong68 |
| Lihua Chen | clh651188968 |
| Liujia Wei | weiliujia |
| Limei Xiao | limeix |
| Lei Zhang | deancn |
| Lu Guo | guolu60 |
Expand All @@ -41,6 +43,7 @@
| Shuai Yan | yanshuai615270 |
| Sijie Yang | iyangsj |
| Tianqi Zhang | NKztq |
| Weijie Zhao | zwj13513118235 |
| Weiqiang Zheng | wrayzheng |
| Wenjie Tian | WJTian |
| Wenlong Chen | LeroChen |
Expand All @@ -50,6 +53,7 @@
| Xiaonan chen | two |
| Xiaoye Jiang | kidleaf-jiang |
| Xin Li | lx-or-xxxl |
| Yang Guo | Marswin |
| Yang Liu | dut-yangliu |
| Yusheng Sun | wodedipanr |
| Yuan Liu | lewisay |
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=`ca

FROM alpine:3.10 AS run
RUN apk update && apk add --no-cache ca-certificates
COPY --from=build /bfe/bfe /bfe/output/bin/
COPY conf /bfe/output/conf/
COPY --from=build /bfe/bfe /bfe/bin/
COPY conf /bfe/conf/
EXPOSE 8080 8443 8421

WORKDIR /bfe/output/bin
WORKDIR /bfe/bin
ENTRYPOINT ["./bfe"]
CMD ["-c", "../conf/", "-l", "../log"]
4 changes: 4 additions & 0 deletions bfe_basic/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const (
HeaderRealPort = "X-Real-Port"
)

const (
BfeVarsKey = "bfe-vars-key"
)

type OperationStage int

const (
Expand Down
29 changes: 29 additions & 0 deletions bfe_basic/condition/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,35 @@ func buildPrimitive(node *parser.CallExpr) (Condition, error) {
fetcher: &ClientCANameFetcher{},
matcher: NewInMatcher(node.Args[0].Value, false),
}, nil
case "req_path_with_vars_in":
matcher, err := NewPathVariableMatcher(node.Args[0].Value, false)
if err != nil {
return nil, err
}
return &VariableCond{
name: node.Fun.Name,
node: node,
fetcher: &PathFetcher{},
matcher: matcher,
}, nil
case "req_path_with_vars_prefix_in":
matcher, err := NewPathVariableMatcher(node.Args[0].Value, true)
if err != nil {
return nil, err
}
return &VariableCond{
name: node.Fun.Name,
node: node,
fetcher: &PathFetcher{},
matcher: matcher,
}, nil
case "req_context_value_in":
return &PrimitiveCond{
name: node.Fun.Name,
node: node,
fetcher: &ContextValueFetcher{node.Args[0].Value},
matcher: NewInMatcher(node.Args[1].Value, false),
}, nil
default:
return nil, fmt.Errorf("unsupported primitive %s", node.Fun.Name)
}
Expand Down
105 changes: 54 additions & 51 deletions bfe_basic/condition/parser/semant.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,60 @@ import (

// funcProtos holds a mapping from func name to args types.
var funcProtos = map[string][]Token{
"default_t": nil,
"req_cip_trusted": nil,
"req_vip_in": {STRING},
"req_proto_match": {STRING},
"req_proto_secure": nil,
"req_host_in": {STRING},
"req_host_regmatch": {STRING},
"req_host_tag_in": {STRING},
"req_host_suffix_in": {STRING},
"req_path_in": {STRING, BOOL},
"req_path_prefix_in": {STRING, BOOL},
"req_path_suffix_in": {STRING, BOOL},
"req_path_regmatch": {STRING},
"req_query_key_prefix_in": {STRING},
"req_query_key_in": {STRING},
"req_query_exist": nil,
"req_query_value_in": {STRING, STRING, BOOL},
"req_query_value_prefix_in": {STRING, STRING, BOOL},
"req_query_value_suffix_in": {STRING, STRING, BOOL},
"req_query_value_regmatch": {STRING, STRING},
"req_query_value_contain": {STRING, STRING, BOOL},
"req_query_value_hash_in": {STRING, STRING, BOOL},
"req_url_regmatch": {STRING},
"req_cookie_key_in": {STRING},
"req_cookie_value_in": {STRING, STRING, BOOL},
"req_cookie_value_prefix_in": {STRING, STRING, BOOL},
"req_cookie_value_suffix_in": {STRING, STRING, BOOL},
"req_cookie_value_contain": {STRING, STRING, BOOL},
"req_cookie_value_hash_in": {STRING, STRING, BOOL},
"req_port_in": {STRING},
"req_tag_match": {STRING, STRING},
"req_ua_regmatch": {STRING},
"req_header_key_in": {STRING},
"req_header_value_in": {STRING, STRING, BOOL},
"req_header_value_prefix_in": {STRING, STRING, BOOL},
"req_header_value_suffix_in": {STRING, STRING, BOOL},
"req_header_value_regmatch": {STRING, STRING},
"req_header_value_contain": {STRING, STRING, BOOL},
"req_header_value_hash_in": {STRING, STRING, BOOL},
"req_method_in": {STRING},
"req_cip_range": {STRING, STRING},
"req_vip_range": {STRING, STRING},
"req_cip_hash_in": {STRING},
"res_code_in": {STRING},
"res_header_key_in": {STRING},
"res_header_value_in": {STRING, STRING, BOOL},
"ses_vip_range": {STRING, STRING},
"ses_sip_range": {STRING, STRING},
"ses_tls_sni_in": {STRING},
"ses_tls_client_auth": nil,
"ses_tls_client_ca_in": {STRING},
"default_t": nil,
"req_cip_trusted": nil,
"req_vip_in": {STRING},
"req_proto_match": {STRING},
"req_proto_secure": nil,
"req_host_in": {STRING},
"req_host_regmatch": {STRING},
"req_host_tag_in": {STRING},
"req_host_suffix_in": {STRING},
"req_path_in": {STRING, BOOL},
"req_path_prefix_in": {STRING, BOOL},
"req_path_suffix_in": {STRING, BOOL},
"req_path_regmatch": {STRING},
"req_query_key_prefix_in": {STRING},
"req_query_key_in": {STRING},
"req_query_exist": nil,
"req_query_value_in": {STRING, STRING, BOOL},
"req_query_value_prefix_in": {STRING, STRING, BOOL},
"req_query_value_suffix_in": {STRING, STRING, BOOL},
"req_query_value_regmatch": {STRING, STRING},
"req_query_value_contain": {STRING, STRING, BOOL},
"req_query_value_hash_in": {STRING, STRING, BOOL},
"req_url_regmatch": {STRING},
"req_cookie_key_in": {STRING},
"req_cookie_value_in": {STRING, STRING, BOOL},
"req_cookie_value_prefix_in": {STRING, STRING, BOOL},
"req_cookie_value_suffix_in": {STRING, STRING, BOOL},
"req_cookie_value_contain": {STRING, STRING, BOOL},
"req_cookie_value_hash_in": {STRING, STRING, BOOL},
"req_port_in": {STRING},
"req_tag_match": {STRING, STRING},
"req_ua_regmatch": {STRING},
"req_header_key_in": {STRING},
"req_header_value_in": {STRING, STRING, BOOL},
"req_header_value_prefix_in": {STRING, STRING, BOOL},
"req_header_value_suffix_in": {STRING, STRING, BOOL},
"req_header_value_regmatch": {STRING, STRING},
"req_header_value_contain": {STRING, STRING, BOOL},
"req_header_value_hash_in": {STRING, STRING, BOOL},
"req_method_in": {STRING},
"req_cip_range": {STRING, STRING},
"req_vip_range": {STRING, STRING},
"req_cip_hash_in": {STRING},
"res_code_in": {STRING},
"res_header_key_in": {STRING},
"res_header_value_in": {STRING, STRING, BOOL},
"ses_vip_range": {STRING, STRING},
"ses_sip_range": {STRING, STRING},
"ses_tls_sni_in": {STRING},
"ses_tls_client_auth": nil,
"ses_tls_client_ca_in": {STRING},
"req_path_with_vars_in": {STRING},
"req_path_with_vars_prefix_in": {STRING},
"req_context_value_in": {STRING, STRING},
}

func prototypeCheck(expr *CallExpr) error {
Expand Down
12 changes: 12 additions & 0 deletions bfe_basic/condition/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,3 +926,15 @@ func (fetcher *ClientCANameFetcher) Fetch(req *bfe_basic.Request) (interface{},

return req.Session.TlsState.ClientCAName, nil
}

type ContextValueFetcher struct {
key string
}

func (f *ContextValueFetcher) Fetch(req *bfe_basic.Request) (interface{}, error) {
if req == nil || req.HttpRequest == nil || req.Context == nil || f.key == "" {
return nil, fmt.Errorf("fetcher: nil pointer")
}

return req.GetContext(f.key), nil
}
20 changes: 20 additions & 0 deletions bfe_basic/condition/primitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,23 @@ func TestContainMatcher_2(t *testing.T) {
t.Fatalf("should match Yingwen")
}
}

// test ContextFetcher
func TestContextValueFetcher(t *testing.T) {
// prepare input data
hf := ContextValueFetcher{"hello"}
req := bfe_basic.NewRequest(nil, nil, nil, nil, nil)
req.HttpRequest = &bfe_http.Request{}
req.SetContext("hello", "world")
// Fetch
contextVal, err := hf.Fetch(req)
if err != nil {
t.Fatalf("Fetch(): %v", err)
t.FailNow()
}

// check
if contextVal.(string) != "world" {
t.Errorf("Fetch contextVal error, want=%v, got=%v", "world", contextVal)
}
}
Loading