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

sotw: auth #952

Merged
merged 25 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
43008f1
Prepare AuthPolicy type for the merge strategy
guicassolato Oct 22, 2024
7c5afed
sotw: auth
guicassolato Oct 23, 2024
6d28a9b
activate auth service in the wasm config
guicassolato Oct 23, 2024
4989285
check status of the authconfigs for the authpolicy enforced status co…
guicassolato Oct 23, 2024
b9545b9
tests: fix unit tests pkg/wasm
guicassolato Oct 24, 2024
cae90a2
bump policy-machinery to v0.6.2
guicassolato Oct 25, 2024
a07e99c
bump policy-machinery to v0.6.3
guicassolato Oct 25, 2024
bafd5a6
add effective authpolicy count to debug log messages when building ga…
guicassolato Oct 25, 2024
64e025e
fix: equality between envoy gateway extension resources
guicassolato Oct 25, 2024
07c99a6
De/restructure all objects via JSON
guicassolato Oct 25, 2024
31cb095
Remove unused funcs from the reconciliation of AuthConfigs
guicassolato Oct 25, 2024
2d09f47
fix: equality between envoy gateway cluster patch resources
guicassolato Oct 25, 2024
e1b8074
bump policy-machinery to v0.6.4
guicassolato Oct 25, 2024
3f45d6a
remove unnecessary custom json unmarshallers from poliyc types
guicassolato Oct 25, 2024
3b558e9
tests: activate auth service in the wasm config
guicassolato Oct 25, 2024
fbe96e1
fix: build envoy auth cluster patch with correct name
guicassolato Oct 26, 2024
bda0743
fix: cel validations of the authpolicy
guicassolato Oct 28, 2024
14d3b8e
tests: fix authpolicy integration tests
guicassolato Oct 28, 2024
feb5448
fix: mark empty authpolicies as enforced
guicassolato Oct 28, 2024
bcd093a
disable prealloc linter
guicassolato Oct 28, 2024
e47be3c
refactor: improved tracking of the origin of a policy rule throughout…
guicassolato Oct 28, 2024
dafdbab
fix log message
guicassolato Oct 30, 2024
1a709ef
fix nil custom response unauthenticated/unauthorized configs
guicassolato Oct 30, 2024
2082bc5
preallocate the modifiedAuthConfigs slice
guicassolato Nov 4, 2024
6138e34
docs: updated user guide Enforcing authentication & authorization wit…
guicassolato Nov 4, 2024
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
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ linters:
- errorlint
- revive
- gosec
- prealloc
- stylecheck
- prealloc
- tparallel
- unconvert
- unparam
Expand Down
56 changes: 41 additions & 15 deletions api/v1/merge_strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,20 @@
PolicyRuleMergeStrategy = "merge"
)

type MergeableRule struct {
Spec any
Source string
// NewMergeableRule creates a new MergeableRule with a default source if the rule does not have one.
func NewMergeableRule(rule MergeableRule, defaultSource string) MergeableRule {
if rule.GetSource() == "" {
return rule.WithSource(defaultSource)
}
return rule
}

// MergeableRule is a policy rule that contains a spec which can be traced back to its source,
// i.e. to the policy where the rule spec was defined.
type MergeableRule interface {
GetSpec() any
GetSource() string
WithSource(string) MergeableRule
}

// +kubebuilder:object:generate=false
Expand All @@ -58,13 +69,11 @@
return source
}

mergeableTargetPolicy := target.(MergeablePolicy)

if !mergeableTargetPolicy.Empty() {
return mergeableTargetPolicy.DeepCopyObject().(machinery.Policy)
if mergeableTarget := target.(MergeablePolicy); !mergeableTarget.Empty() {
return copyMergeablePolicy(mergeableTarget)
}

return source.(MergeablePolicy).DeepCopyObject().(machinery.Policy)
return copyMergeablePolicy(source.(MergeablePolicy))
}

var _ machinery.MergeStrategy = AtomicDefaultsMergeStrategy
Expand All @@ -75,7 +84,7 @@
if source == nil {
return nil
}
return source.(MergeablePolicy).DeepCopyObject().(machinery.Policy)
return copyMergeablePolicy(source.(MergeablePolicy))
}

var _ machinery.MergeStrategy = AtomicOverridesMergeStrategy
Expand All @@ -94,15 +103,16 @@
targetMergeablePolicy := target.(MergeablePolicy)

// copy rules from the target
rules := targetMergeablePolicy.Rules()
rules := lo.MapValues(targetMergeablePolicy.Rules(), mapRuleWithSourceFunc(target))

Check warning on line 106 in api/v1/merge_strategies.go

View check run for this annotation

Codecov / codecov/patch

api/v1/merge_strategies.go#L106

Added line #L106 was not covered by tests

// add extra rules from the source
for ruleID, rule := range sourceMergeablePolicy.Rules() {
if _, ok := targetMergeablePolicy.Rules()[ruleID]; !ok {
rules[ruleID] = MergeableRule{
Spec: rule.Spec,
Source: source.GetLocator(),
origin := rule.GetSource()
if origin == "" {
origin = source.GetLocator()

Check warning on line 113 in api/v1/merge_strategies.go

View check run for this annotation

Codecov / codecov/patch

api/v1/merge_strategies.go#L111-L113

Added lines #L111 - L113 were not covered by tests
}
rules[ruleID] = rule.WithSource(origin)

Check warning on line 115 in api/v1/merge_strategies.go

View check run for this annotation

Codecov / codecov/patch

api/v1/merge_strategies.go#L115

Added line #L115 was not covered by tests
}
}

Expand All @@ -121,12 +131,16 @@
targetMergeablePolicy := target.(MergeablePolicy)

// copy rules from the source
rules := sourceMergeablePolicy.Rules()
rules := lo.MapValues(sourceMergeablePolicy.Rules(), mapRuleWithSourceFunc(source))

Check warning on line 134 in api/v1/merge_strategies.go

View check run for this annotation

Codecov / codecov/patch

api/v1/merge_strategies.go#L134

Added line #L134 was not covered by tests

// add extra rules from the target
for ruleID, rule := range targetMergeablePolicy.Rules() {
if _, ok := sourceMergeablePolicy.Rules()[ruleID]; !ok {
rules[ruleID] = rule
origin := rule.GetSource()
if origin == "" {
origin = target.GetLocator()
}
rules[ruleID] = rule.WithSource(origin)

Check warning on line 143 in api/v1/merge_strategies.go

View check run for this annotation

Codecov / codecov/patch

api/v1/merge_strategies.go#L139-L143

Added lines #L139 - L143 were not covered by tests
}
}

Expand Down Expand Up @@ -198,3 +212,15 @@
return strings.TrimPrefix(k8stypes.NamespacedName{Namespace: t.GetNamespace(), Name: t.GetName()}.String(), string(k8stypes.Separator))
}), "|")
}

func mapRuleWithSourceFunc(source machinery.Policy) func(MergeableRule, string) MergeableRule {
return func(rule MergeableRule, _ string) MergeableRule {
return rule.WithSource(source.GetLocator())
}
}

func copyMergeablePolicy(policy MergeablePolicy) MergeablePolicy {
dup := policy.DeepCopyObject().(MergeablePolicy)
dup.SetRules(lo.MapValues(dup.Rules(), mapRuleWithSourceFunc(policy)))
return dup
}
42 changes: 33 additions & 9 deletions api/v1beta1/topology.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package v1beta1

import (
authorinov1beta1 "github.com/kuadrant/authorino-operator/api/v1beta1"
authorinooperatorv1beta1 "github.com/kuadrant/authorino-operator/api/v1beta1"
authorinov1beta2 "github.com/kuadrant/authorino/api/v1beta2"
limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1"
"github.com/kuadrant/policy-machinery/controller"
"github.com/kuadrant/policy-machinery/machinery"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/runtime/schema"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

var (
AuthorinoGroupKind = schema.GroupKind{Group: authorinov1beta1.GroupVersion.Group, Kind: "Authorino"}
KuadrantGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "Kuadrant"}
LimitadorGroupKind = schema.GroupKind{Group: limitadorv1alpha1.GroupVersion.Group, Kind: "Limitador"}
KuadrantGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "Kuadrant"}
LimitadorGroupKind = schema.GroupKind{Group: limitadorv1alpha1.GroupVersion.Group, Kind: "Limitador"}
AuthorinoGroupKind = schema.GroupKind{Group: authorinooperatorv1beta1.GroupVersion.Group, Kind: "Authorino"}
AuthConfigGroupKind = schema.GroupKind{Group: authorinov1beta2.GroupVersion.Group, Kind: "AuthConfig"}

AuthorinosResource = authorinov1beta1.GroupVersion.WithResource("authorinos")
KuadrantsResource = GroupVersion.WithResource("kuadrants")
LimitadorsResource = limitadorv1alpha1.GroupVersion.WithResource("limitadors")
KuadrantsResource = GroupVersion.WithResource("kuadrants")
LimitadorsResource = limitadorv1alpha1.GroupVersion.WithResource("limitadors")
AuthorinosResource = authorinooperatorv1beta1.GroupVersion.WithResource("authorinos")
AuthConfigsResource = authorinov1beta2.GroupVersion.WithResource("authconfigs")

AuthConfigHTTPRouteRuleAnnotation = machinery.HTTPRouteRuleGroupKind.String()
)

var _ machinery.Object = &Kuadrant{}
Expand All @@ -31,7 +36,7 @@ func LinkKuadrantToGatewayClasses(objs controller.Store) machinery.LinkFunc {

return machinery.LinkFunc{
From: KuadrantGroupKind,
To: schema.GroupKind{Group: gwapiv1.GroupVersion.Group, Kind: "GatewayClass"},
To: schema.GroupKind{Group: gatewayapiv1.GroupVersion.Group, Kind: "GatewayClass"},
Func: func(_ machinery.Object) []machinery.Object {
parents := make([]machinery.Object, len(kuadrants))
for _, parent := range kuadrants {
Expand Down Expand Up @@ -69,3 +74,22 @@ func LinkKuadrantToAuthorino(objs controller.Store) machinery.LinkFunc {
},
}
}

func LinkHTTPRouteRuleToAuthConfig(objs controller.Store) machinery.LinkFunc {
httpRoutes := lo.Map(objs.FilterByGroupKind(machinery.HTTPRouteGroupKind), controller.ObjectAs[*gatewayapiv1.HTTPRoute])
httpRouteRules := lo.FlatMap(lo.Map(httpRoutes, func(r *gatewayapiv1.HTTPRoute, _ int) *machinery.HTTPRoute {
return &machinery.HTTPRoute{HTTPRoute: r}
}), machinery.HTTPRouteRulesFromHTTPRouteFunc)

return machinery.LinkFunc{
From: machinery.HTTPRouteRuleGroupKind,
To: AuthConfigGroupKind,
Func: func(child machinery.Object) []machinery.Object {
return lo.FilterMap(httpRouteRules, func(httpRouteRule *machinery.HTTPRouteRule, _ int) (machinery.Object, bool) {
authConfig := child.(*controller.RuntimeObject).Object.(*authorinov1beta2.AuthConfig)
annotations := authConfig.GetAnnotations()
return httpRouteRule, annotations != nil && annotations[AuthConfigHTTPRouteRuleAnnotation] == httpRouteRule.GetLocator()
})
},
}
}
Loading
Loading