Skip to content

Commit

Permalink
Merge pull request #167 from ngrok/mo/bindings
Browse files Browse the repository at this point in the history
Add WithBindings Mixin
  • Loading branch information
Megalonia committed Jun 25, 2024
2 parents d4bcbfd + 48f9275 commit 46e6a32
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.10.0
Enhancements:

- Adds `WithBindings` option

## 1.9.1

Bug fixes:
Expand All @@ -10,7 +15,7 @@ Enhancements:
- Adds `WithAdditionalServers` and `WithMultiLeg` options

## 1.8.1
Enhancements:
Enhancements:

- Provides access to structs for building a Traffic Policy configuration

Expand Down
30 changes: 30 additions & 0 deletions config/bindings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

type bindings []string

// WithBinding configures ingress for an endpoint
//
// The requestedBindings argument specifies the type of ingress for the endpoint.
func WithBindings(requestedBindings ...string) interface {
HTTPEndpointOption
TLSEndpointOption
TCPEndpointOption
} {
ret := bindings{}
for _, binding := range requestedBindings {
ret = append(ret, binding)
}
return ret
}

func (b bindings) ApplyTLS(cfg *tlsOptions) {
cfg.Bindings = []string(b)
}

func (b bindings) ApplyTCP(cfg *tcpOptions) {
cfg.Bindings = []string(b)
}

func (b bindings) ApplyHTTP(cfg *httpOptions) {
cfg.Bindings = []string(b)
}
45 changes: 45 additions & 0 deletions config/bindings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package config

import (
"testing"
)

func testBindings[T tunnelConfigPrivate, OT any](t *testing.T,
makeOpts func(...OT) Tunnel,
) {
optsFunc := func(opts ...any) Tunnel {
return makeOpts(assertSlice[OT](opts)...)
}

cases := testCases[T, any]{
{
name: "absent",
opts: optsFunc(),
expectExtra: &matchBindExtra{
Bindings: ptr([]string{}),
},
},
{
name: "with bindings",
opts: optsFunc(WithBindings("public")),
expectExtra: &matchBindExtra{
Bindings: ptr([]string{"public"}),
},
},
{
name: "with bindings with spread op",
opts: optsFunc(WithBindings([]string{"public"}...)),
expectExtra: &matchBindExtra{
Bindings: ptr([]string{"public"}),
},
},
}

cases.runAll(t)
}

func TestBindings(t *testing.T) {
testBindings[*httpOptions](t, HTTPEndpoint)
testBindings[*tlsOptions](t, TLSEndpoint)
testBindings[*tcpOptions](t, TCPEndpoint)
}
2 changes: 2 additions & 0 deletions config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type commonOpts struct {
// Policy that define rules that should be applied to incoming or outgoing
// connections to the edge.
Policy *policy
// Enables ingress for ngrok endpoints.
Bindings []string
}

type CommonOptionsFunc func(cfg *commonOpts)
Expand Down
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type matchBindExtra struct {
Token *string
IPPolicyRef *string
Metadata *string
Bindings *[]string
}

func (m matchBindExtra) RequireMatches(t *testing.T, actual proto.BindExtra) {
Expand Down
1 change: 1 addition & 0 deletions config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func (cfg *httpOptions) WithForwardsTo(url *url.URL) {
func (cfg httpOptions) Extra() proto.BindExtra {
return proto.BindExtra{
Metadata: cfg.Metadata,
Bindings: cfg.Bindings,
}
}

Expand Down
1 change: 1 addition & 0 deletions config/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (cfg *tcpOptions) WithForwardsTo(url *url.URL) {
func (cfg tcpOptions) Extra() proto.BindExtra {
return proto.BindExtra{
Metadata: cfg.Metadata,
Bindings: cfg.Bindings,
}
}

Expand Down
1 change: 1 addition & 0 deletions config/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (cfg *tlsOptions) WithForwardsTo(url *url.URL) {
func (cfg tlsOptions) Extra() proto.BindExtra {
return proto.BindExtra{
Metadata: cfg.Metadata,
Bindings: cfg.Bindings,
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/tunnel/proto/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ type BindExtra struct {
Token string
IPPolicyRef string
Metadata string
Bindings []string
}

// The server responds with a BindResp message to notify the client of the
Expand Down

0 comments on commit 46e6a32

Please sign in to comment.