Skip to content

Commit

Permalink
add policy module
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdiramen committed Jan 18, 2024
1 parent 852b642 commit eddf0b5
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/ingress/v1alpha1/httpsedge_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type HTTPSEdgeRouteSpec struct {

// WebhookVerification is webhook verification configuration to apply to this route
WebhookVerification *EndpointWebhookVerification `json:"webhookVerification,omitempty"`

Policies *EndpointPolicies `json:"policies,omitempty"`
}

// HTTPSEdgeSpec defines the desired state of HTTPSEdge
Expand Down
66 changes: 66 additions & 0 deletions api/ingress/v1alpha1/ngrok_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,69 @@ func (amazon *EndpointOAuthAmazon) ToNgrok(clientSecret *string) *ngrok.Endpoint
}
return mod
}

type EndpointPolicies struct {
Enabled *bool `json:"enabled,omitempty"`
Inbound []EndpointPolicy `json:"inbound,omitempty"`
Outbound []EndpointPolicy `json:"outbound,omitempty"`
}

type EndpointPolicy struct {
Expressions []string `json:"expressions,omitempty"`
Actions []EndpointAction `json:"actions,omitempty"`
Name string `json:"name,omitempty"`
}

type EndpointAction struct {
Type string `json:"type,omitempty"`
Config any `json:"config,omitempty"`
}

func (policies *EndpointPolicies) ToNgrok() *ngrok.EndpointPolicies {
if policies == nil {
return nil
}

inbound := make([]ngrok.EndpointPolicy, len(policies.Inbound))
for _, policy := range policies.Inbound {
inbound = append(inbound, *policy.ToNgrok())
}
outbound := make([]ngrok.EndpointPolicy, len(policies.Outbound))
for _, policy := range policies.Outbound {
outbound = append(outbound, *policy.ToNgrok())
}

return &ngrok.EndpointPolicies{
Enabled: policies.Enabled,
Inbound: inbound,
Outbound: outbound,
}
}

func (policy *EndpointPolicy) ToNgrok() *ngrok.EndpointPolicy {
if policy == nil {
return nil
}

actions := make([]ngrok.EndpointAction, len(policy.Actions))
for _, action := range policy.Actions {
actions = append(actions, *action.ToNgrok())
}

return &ngrok.EndpointPolicy{
Expressions: policy.Expressions,
Actions: actions,
Name: policy.Name,
}
}

func (action *EndpointAction) ToNgrok() *ngrok.EndpointAction {
if action == nil {
return nil
}

return &ngrok.EndpointAction{
Type: action.Type,
Config: action.Config,
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-logr/logr v1.2.4
github.com/golang/mock v1.4.4
github.com/imdario/mergo v0.3.16
github.com/ngrok/ngrok-api-go/v5 v5.0.0
github.com/ngrok/ngrok-api-go/v5 v5.2.1-0.20240117170843-c468056fd303
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
github.com/spf13/cobra v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU=
github.com/ngrok/ngrok-api-go/v5 v5.0.0 h1:eksowVztKNQU0JBaYS2hXGiC/xtGXj8LAx8lAuzYlsw=
github.com/ngrok/ngrok-api-go/v5 v5.0.0/go.mod h1:cxMRsWuE0EwK/JB/5prvHK0LEWB3KP16iwvIMqvDVP0=
github.com/ngrok/ngrok-api-go/v5 v5.2.1-0.20240117170843-c468056fd303 h1:td6hx8jy4X+U/Ed/zl6gTrCVmyldZ7tMNJQHa2YvcXc=
github.com/ngrok/ngrok-api-go/v5 v5.2.1-0.20240117170843-c468056fd303/go.mod h1:UVTaHI5B4gEsfHCOZTlRg8WkT6+KBijIkVtjpDqCyIU=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
Expand Down
44 changes: 41 additions & 3 deletions internal/controller/ingress/httpsedge_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -342,7 +342,7 @@ func (r *HTTPSEdgeReconciler) setEdgeTLSTermination(ctx context.Context, edge *n
_, err := client.Replace(ctx, &ngrok.EdgeTLSTerminationAtEdgeReplace{
ID: edge.ID,
Module: ngrok.EndpointTLSTerminationAtEdge{
MinVersion: pointer.String(tlsTermination.MinVersion),
MinVersion: ptr.To(tlsTermination.MinVersion),
},
})
return err
Expand Down Expand Up @@ -523,6 +523,7 @@ func (u *edgeRouteModuleUpdater) updateModulesForRoute(ctx context.Context, rout
u.setEdgeRouteOIDC,
u.setEdgeRouteSAML,
u.setEdgeRouteWebhookVerification,
u.setEdgeRoutePolicies,
}

for _, f := range funcs {
Expand Down Expand Up @@ -605,7 +606,7 @@ func (u *edgeRouteModuleUpdater) setEdgeRouteCompression(ctx context.Context, ro
EdgeID: route.EdgeID,
ID: route.ID,
Module: ngrok.EndpointCompression{
Enabled: pointer.Bool(routeSpec.Compression.Enabled),
Enabled: ptr.To(routeSpec.Compression.Enabled),
},
})
return err
Expand Down Expand Up @@ -1028,3 +1029,40 @@ func (r *HTTPSEdgeReconciler) takeOfflineWithoutAuth(ctx context.Context, route

return nil
}

func (u *edgeRouteModuleUpdater) setEdgeRoutePolicies(ctx context.Context, route *ngrok.HTTPSEdgeRoute, routeSpec *ingressv1alpha1.HTTPSEdgeRouteSpec) error {
log := ctrl.LoggerFrom(ctx)
policies := routeSpec.Policies
policiesClient := u.clientset.Policies()

if policies == nil {
if route.Policies == nil {
u.logMatches(log, "Policies", routeModuleComparisonBothNil)
return nil
}

log.Info("Deleting Policies module")
return policiesClient.Delete(ctx, edgeRouteItem(route))
}

module := policies.ToNgrok()
var err error

if module == nil {
// should this be an error?
return ierr.NewErrInvalidConfiguration(fmt.Errorf("no policies configured"))
}

if reflect.DeepEqual(module, route.Policies) {
u.logMatches(log, "Policies", routeModuleComparisonDeepEqual)
return nil
}

log.Info("Updating Policies module")
_, err = policiesClient.Replace(ctx, &ngrok.EdgeRoutePoliciesReplace{
EdgeID: route.EdgeID,
ID: route.ID,
Module: *module,
})
return err
}
8 changes: 8 additions & 0 deletions internal/ngrokapi/edge_modules_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ngrok/ngrok-api-go/v5/edge_modules/https_edge_route_ip_restriction"
"github.com/ngrok/ngrok-api-go/v5/edge_modules/https_edge_route_oauth"
"github.com/ngrok/ngrok-api-go/v5/edge_modules/https_edge_route_oidc"
"github.com/ngrok/ngrok-api-go/v5/edge_modules/https_edge_route_policies"
"github.com/ngrok/ngrok-api-go/v5/edge_modules/https_edge_route_request_headers"
"github.com/ngrok/ngrok-api-go/v5/edge_modules/https_edge_route_response_headers"
"github.com/ngrok/ngrok-api-go/v5/edge_modules/https_edge_route_saml"
Expand Down Expand Up @@ -55,6 +56,7 @@ type HTTPSEdgeRouteModulesClientset interface {
Compression() *https_edge_route_compression.Client
IPRestriction() *https_edge_route_ip_restriction.Client
OAuth() *https_edge_route_oauth.Client
Policies() *https_edge_route_policies.Client
OIDC() *https_edge_route_oidc.Client
RequestHeaders() *https_edge_route_request_headers.Client
ResponseHeaders() *https_edge_route_response_headers.Client
Expand All @@ -69,6 +71,7 @@ type defaultHTTPSEdgeRouteModulesClientset struct {
compression *https_edge_route_compression.Client
ipRestriction *https_edge_route_ip_restriction.Client
oauth *https_edge_route_oauth.Client
policies *https_edge_route_policies.Client
oidc *https_edge_route_oidc.Client
requestHeaders *https_edge_route_request_headers.Client
responseHeaders *https_edge_route_response_headers.Client
Expand All @@ -84,6 +87,7 @@ func newHTTPSEdgeRouteModulesClient(config *ngrok.ClientConfig) *defaultHTTPSEdg
compression: https_edge_route_compression.NewClient(config),
ipRestriction: https_edge_route_ip_restriction.NewClient(config),
oauth: https_edge_route_oauth.NewClient(config),
policies: https_edge_route_policies.NewClient(config),
oidc: https_edge_route_oidc.NewClient(config),
requestHeaders: https_edge_route_request_headers.NewClient(config),
responseHeaders: https_edge_route_response_headers.NewClient(config),
Expand Down Expand Up @@ -113,6 +117,10 @@ func (c *defaultHTTPSEdgeRouteModulesClientset) OAuth() *https_edge_route_oauth.
return c.oauth
}

func (c *defaultHTTPSEdgeRouteModulesClientset) Policies() *https_edge_route_policies.Client {
return c.policies
}

func (c *defaultHTTPSEdgeRouteModulesClientset) OIDC() *https_edge_route_oidc.Client {
return c.oidc
}
Expand Down

0 comments on commit eddf0b5

Please sign in to comment.