Skip to content

Commit

Permalink
Have only auth accessing actions happen after auth
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Nov 14, 2024
1 parent fe39f7f commit e6ef741
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
8 changes: 7 additions & 1 deletion controllers/envoy_gateway_extension_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ func (r *EnvoyGatewayExtensionReconciler) buildWasmConfigs(ctx context.Context,

// rate limit
if effectivePolicy, ok := effectiveRateLimitPoliciesMap[pathID]; ok {
actions = append(actions, buildWasmActionsForRateLimit(effectivePolicy, state)...)
rlAction := buildWasmActionsForRateLimit(effectivePolicy, state)
if hasAuthAccess(rlAction) {
actions = append(actions, rlAction...)
} else {
// pre auth rate limiting
actions = append(rlAction, actions...)
}
}

if len(actions) == 0 {
Expand Down
17 changes: 16 additions & 1 deletion controllers/istio_extension_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,13 @@ func (r *IstioExtensionReconciler) buildWasmConfigs(ctx context.Context, state *

// rate limit
if effectivePolicy, ok := effectiveRateLimitPoliciesMap[pathID]; ok {
actions = append(actions, buildWasmActionsForRateLimit(effectivePolicy, state)...)
rlAction := buildWasmActionsForRateLimit(effectivePolicy, state)
if hasAuthAccess(rlAction) {
actions = append(actions, rlAction...)
} else {
// pre auth rate limiting
actions = append(rlAction, actions...)
}
}

if len(actions) == 0 {
Expand All @@ -212,6 +218,15 @@ func (r *IstioExtensionReconciler) buildWasmConfigs(ctx context.Context, state *
return wasmConfigs, nil
}

func hasAuthAccess(actionSet []wasm.Action) bool {
for _, action := range actionSet {
if action.HasAuthAccess() {
return true
}
}
return false
}

// buildIstioWasmPluginForGateway builds a desired WasmPlugin custom resource for a given gateway and corresponding wasm config
func buildIstioWasmPluginForGateway(gateway *machinery.Gateway, wasmConfig wasm.Config) *istioclientgoextensionv1alpha1.WasmPlugin {
wasmPlugin := &istioclientgoextensionv1alpha1.WasmPlugin{
Expand Down
21 changes: 21 additions & 0 deletions pkg/wasm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"strings"

_struct "google.golang.org/protobuf/types/known/structpb"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -144,6 +145,26 @@ type Action struct {
Data []DataType `json:"data,omitempty"`
}

func (a *Action) HasAuthAccess() bool {
for _, predicate := range a.Predicates {
if strings.Contains(predicate, "auth.") {
return true
}
}
for _, data := range a.Data {
switch val := data.Value.(type) {
case *Static:

continue
case *Expression:
if strings.Contains(val.ExpressionItem.Value, "auth.") {
return true
}
}
}
return false
}

func (a *Action) EqualTo(other Action) bool {
if a.Scope != other.Scope ||
a.ServiceName != other.ServiceName ||
Expand Down
90 changes: 90 additions & 0 deletions pkg/wasm/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,93 @@ data:
})
}
}

func TestAuthAccesses(t *testing.T) {
action := Action{
ServiceName: "ratelimit-service",
Scope: "default/other",
Predicates: []string{
"source.address != '127.0.0.1'",
},
Data: []DataType{
{
Value: &Static{
Static: StaticSpec{
Key: "limit.global__f63bec56",
Value: "1",
},
},
},
},
}

if action.HasAuthAccess() {
t.Fatal("must not have auth access")
}

action = Action{
ServiceName: "ratelimit-service",
Scope: "default/other",
Predicates: []string{
"auth.something != '127.0.0.1'",
},
Data: []DataType{
{
Value: &Static{
Static: StaticSpec{
Key: "limit.global__f63bec56",
Value: "1",
},
},
},
},
}

if !action.HasAuthAccess() {
t.Fatal("must have auth access")
}

action = Action{
ServiceName: "ratelimit-service",
Scope: "default/other",
Predicates: []string{
"source.address != '127.0.0.1'",
},
Data: []DataType{
{
Value: &Expression{
ExpressionItem: ExpressionItem{
Key: "limit.global__f63bec56",
Value: "auth.identity.anonymous",
},
},
},
},
}

if !action.HasAuthAccess() {
t.Fatal("must have auth access")
}

action = Action{
ServiceName: "ratelimit-service",
Scope: "default/other",
Predicates: []string{
"source.address != '127.0.0.1'",
},
Data: []DataType{
{
Value: &Static{
Static: StaticSpec{
Key: "auth.global__f63bec56",
Value: "auth",
},
},
},
},
}

if action.HasAuthAccess() {
t.Fatal("must not have auth access")
}
}

0 comments on commit e6ef741

Please sign in to comment.