Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion helm/core/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,16 @@ higress: {{ include "controller.name" . }}
{{- end }}

{{- define "skywalking.enabled" -}}
{{- if and (hasKey .Values "tracing") .Values.tracing.enable (hasKey .Values.tracing "skywalking") .Values.tracing.skywalking.service }}
{{- with .Values.tracing }}
{{- if .enable }}
{{- with .skywalking }}
{{- if .service }}
true
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}

{{- define "gateway.podMonitor.gvk" -}}
{{- if eq .Values.gateway.metrics.provider "monitoring.coreos.com" -}}
Expand Down
16 changes: 8 additions & 8 deletions helm/core/templates/ingressclass.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{{- if .Values.global.ingressClass }}
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: {{ .Values.global.ingressClass }}
spec:
controller: higress.io/higress-controller
{{- end }}
{{- if and .Values.global.ingressClass (ne (lower .Values.global.ingressClass) "nginx") }}
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: {{ .Values.global.ingressClass }}
spec:
controller: higress.io/higress-controller
{{- end }}
4 changes: 2 additions & 2 deletions helm/core/templates/plugin-server-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ spec:
{{- end }}
containers:
- name: {{ .Chart.Name }}
image: {{ .Values.pluginServer.hub | default .Values.global.hub }}/higress/{{ .Values.pluginServer.image | default "plugin-server" }}:{{ .Values.pluginServer.tag | default "1.0.0" }}
image: {{ .Values.pluginServer.hub | default .Values.global.hub }}/higress/{{ .Values.pluginServer.image | default "plugin-server" }}:{{ .Values.pluginServer.tag | default .Chart.AppVersion }}
{{- if .Values.global.imagePullPolicy }}
imagePullPolicy: {{ .Values.global.imagePullPolicy }}
{{- end }}
Expand All @@ -36,4 +36,4 @@ spec:
limits:
cpu: {{ .Values.pluginServer.resources.limits.cpu }}
memory: {{ .Values.pluginServer.resources.limits.memory }}
{{- end }}
{{- end }}
16 changes: 12 additions & 4 deletions hgctl/pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ import (

// StripPrefix removes the given prefix from prefix.
func StripPrefix(path, prefix string) string {
pl := len(strings.Split(prefix, "/"))
pv := strings.Split(path, "/")
return strings.Join(pv[pl:], "/")
prefix = strings.TrimSuffix(prefix, "/")
if prefix == "" {
return path
}
if path == prefix {
return ""
}
if strings.HasPrefix(path, prefix+"/") {
return strings.TrimPrefix(path, prefix+"/")
}
return path
}

func SplitSetFlag(flag string) (string, string) {
items := strings.Split(flag, "=")
items := strings.SplitN(flag, "=", 2)
if len(items) != 2 {
return flag, ""
}
Expand Down
60 changes: 60 additions & 0 deletions hgctl/pkg/util/util_split_set_flag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// 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://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 util

import "testing"

func TestSplitSetFlag(t *testing.T) {
tests := []struct {
name string
in string
key string
val string
}{
{
name: "normal pair",
in: "a=b",
key: "a",
val: "b",
},
{
name: "no separator",
in: "a",
key: "a",
val: "",
},
{
name: "value contains equals",
in: "token=abc=def==",
key: "token",
val: "abc=def==",
},
{
name: "trim spaces",
in: " key = value=1 ",
key: "key",
val: "value=1",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key, val := SplitSetFlag(tt.in)
if key != tt.key || val != tt.val {
t.Fatalf("SplitSetFlag(%q)=(%q,%q), want (%q,%q)", tt.in, key, val, tt.key, tt.val)
}
})
}
}
72 changes: 72 additions & 0 deletions hgctl/pkg/util/util_strip_prefix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// 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://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 util

import "testing"

func TestStripPrefix(t *testing.T) {
tests := []struct {
name string
path string
prefix string
want string
}{
{
name: "empty prefix keeps path",
path: "a/b/c",
prefix: "",
want: "a/b/c",
},
{
name: "full match returns empty",
path: "a/b",
prefix: "a/b",
want: "",
},
{
name: "prefix match with slash strips prefix",
path: "a/b/c.yaml",
prefix: "a/b",
want: "c.yaml",
},
{
name: "prefix ending with slash is accepted",
path: "a/b/c.yaml",
prefix: "a/b/",
want: "c.yaml",
},
{
name: "not a real prefix keeps path",
path: "a/b/c",
prefix: "x/y",
want: "a/b/c",
},
{
name: "longer prefix keeps path",
path: "a",
prefix: "a/b",
want: "a",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := StripPrefix(tt.path, tt.prefix)
if got != tt.want {
t.Fatalf("StripPrefix(%q, %q) = %q, want %q", tt.path, tt.prefix, got, tt.want)
}
})
}
}
5 changes: 3 additions & 2 deletions plugins/wasm-go/extensions/opa/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (
)

type OpaConfig struct {
policy string
timeout uint32
policy string
policyPath string
timeout uint32

client wrapper.HttpClient
}
Expand Down
18 changes: 17 additions & 1 deletion plugins/wasm-go/extensions/opa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func parseConfig(json gjson.Result, config *OpaConfig, log log.Log) error {
}
config.client = client
config.policy = policy
config.policyPath = normalizePolicyPath(policy)

return nil
}
Expand Down Expand Up @@ -107,7 +108,7 @@ func opaCall(ctx wrapper.HttpContext, config OpaConfig, body []byte, log log.Log
request["query"] = query

data, _ := json.Marshal(Metadata{Input: map[string]interface{}{"request": request}})
if err := config.client.Post(fmt.Sprintf("/v1/data/%s/allow", config.policy),
if err := config.client.Post(fmt.Sprintf("/v1/data/%s/allow", config.policyPath),
[][2]string{{"Content-Type", "application/json"}},
data, rspCall, config.timeout); err != nil {
log.Errorf("client opa fail %v", err)
Expand Down Expand Up @@ -139,3 +140,18 @@ func rspCall(statusCode int, _ http.Header, responseBody []byte) {
}
proxywasm.ResumeHttpRequest()
}

func normalizePolicyPath(policy string) string {
p := strings.TrimSpace(policy)
p = strings.TrimPrefix(p, "/")
p = strings.TrimPrefix(p, "v1/data/")
p = strings.TrimPrefix(p, "data/")
p = strings.TrimPrefix(p, "data.")
p = strings.ReplaceAll(p, ".", "/")
p = strings.TrimPrefix(p, "/")
p = strings.TrimSuffix(p, "/")
if strings.HasSuffix(p, "/allow") && p != "allow" {
p = strings.TrimSuffix(p, "/allow")
}
return p
}
58 changes: 58 additions & 0 deletions plugins/wasm-go/extensions/opa/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,30 @@ import (
"testing"

"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
"github.com/higress-group/wasm-go/pkg/log"
"github.com/higress-group/wasm-go/pkg/test"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)

type noopLog struct{}

func (noopLog) Trace(string) {}
func (noopLog) Tracef(string, ...interface{}) {}
func (noopLog) Debug(string) {}
func (noopLog) Debugf(string, ...interface{}) {}
func (noopLog) Info(string) {}
func (noopLog) Infof(string, ...interface{}) {}
func (noopLog) Warn(string) {}
func (noopLog) Warnf(string, ...interface{}) {}
func (noopLog) Error(string) {}
func (noopLog) Errorf(string, ...interface{}) {}
func (noopLog) Critical(string) {}
func (noopLog) Criticalf(string, ...interface{}) {}
func (noopLog) ResetID(string) {}

var _ log.Log = (*noopLog)(nil)

// 测试配置:基本配置
var basicConfig = func() json.RawMessage {
data, _ := json.Marshal(map[string]interface{}{
Expand Down Expand Up @@ -107,6 +127,28 @@ var invalidConfigInvalidTimeout = func() json.RawMessage {
return data
}()

func TestNormalizePolicyPath(t *testing.T) {
tests := []struct {
name string
policy string
want string
}{
{name: "plain package", policy: "authz", want: "authz"},
{name: "package with allow", policy: "authz/allow", want: "authz"},
{name: "full data dot path", policy: "data.authz.allow", want: "authz"},
{name: "full data slash path", policy: "data/authz/allow", want: "authz"},
{name: "v1 data path", policy: "v1/data/authz/allow", want: "authz"},
{name: "nested package", policy: "data.company.authz.allow", want: "company/authz"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := normalizePolicyPath(tt.policy)
require.Equal(t, tt.want, got)
})
}
}

func TestParseConfig(t *testing.T) {
test.RunGoTest(t, func(t *testing.T) {
// 测试基本配置解析
Expand All @@ -120,6 +162,22 @@ func TestParseConfig(t *testing.T) {
require.NotNil(t, config)
})

t.Run("policy path normalization for full data path", func(t *testing.T) {
raw, _ := json.Marshal(map[string]interface{}{
"policy": "data.authz.allow",
"timeout": "5s",
"serviceSource": "k8s",
"serviceName": "opa",
"servicePort": "8181",
"namespace": "higress-backend",
})

cfg := OpaConfig{}
err := parseConfig(gjson.ParseBytes(raw), &cfg, noopLog{})
require.NoError(t, err)
require.Equal(t, "authz", cfg.policyPath)
})

// 测试 IP 服务配置解析
t.Run("ip service config", func(t *testing.T) {
host, status := test.NewTestHost(ipConfig)
Expand Down