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

Split hello package into syncing and pairing packages #83

Merged
merged 2 commits into from
Nov 5, 2024
Merged
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: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Now you can expose a service from one infrastructure to another. Services expose
kubectl annotate --overwrite svc --namespace <namespace> <service> wormhole.glothriel.github.com/exposed=yes
```

After up to 30 seconds the service will be available on the other side.
After up to 30 seconds the service will be available on the other side.

### Customize the exposed services

Expand All @@ -106,7 +106,7 @@ You can secure the services exposed on another end by configuring network polici

You can enable network policies by setting `--set networkPolicies.enabled=true` helm chart value. Network policies of course in order to work require the cluster that supports them.

When wormhole is deployed with network policies support, each time it exposes a remote service it also creates a matching network policy. The network policy is created in the same namespace as the service and allows filtering of the traffic from other workloads in the cluster to the remote service.
When wormhole is deployed with network policies support, each time it exposes a remote service it also creates a matching network policy. The network policy is created in the same namespace as the service and allows filtering of the traffic from other workloads in the cluster to the remote service.

```
apiVersion: networking.k8s.io/v1
Expand Down Expand Up @@ -136,11 +136,11 @@ Effectively this means, that the permission to communicate is granted per applic

## HTTP API

Wormhole exposes API, that allows querying apps exposed by remote peers. The API does not require authentication. The API by default listens on port 8082.
Wormhole exposes API, that allows querying apps exposed by remote apps. The API does not require authentication. The API by default listens on port 8082.

### GET /api/apps/v1

This endpoint returns the list of apps exposed locally by the remote peers.
This endpoint returns the list of apps exposed locally by the remote apps.

#### Request

Expand Down
4 changes: 2 additions & 2 deletions kubernetes/helm/templates/server-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spec:
checksum/config: {{ include (print $.Template.BasePath "/server-config.yaml") . | sha256sum }}
labels:
application: {{ template "name-server" . }}
spec:
spec:
shareProcessNamespace: true
{{- if .Values.server.priorityClassName }}
priorityClassName: {{ .Values.server.priorityClassName }}
Expand Down Expand Up @@ -91,7 +91,7 @@ spec:
capabilities:
add:
- NET_ADMIN

- image: {{ $.Values.docker.registry }}{{ if $.Values.docker.registry }}/{{ end }}{{ $.Values.docker.image }}:{{ $.Values.docker.version }}
name: wormhole
envFrom:
Expand Down
16 changes: 8 additions & 8 deletions pkg/api/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ package api

import (
"github.com/gin-gonic/gin"
"github.com/glothriel/wormhole/pkg/hello"
"github.com/glothriel/wormhole/pkg/peers"
"github.com/glothriel/wormhole/pkg/apps"
"github.com/glothriel/wormhole/pkg/syncing"
)

type appsController struct {
appSource hello.AppSource
appSource syncing.AppSource
}

func (ac *appsController) registerRoutes(r *gin.Engine) {
r.GET("/api/apps/v1", func(c *gin.Context) {
apps, err := ac.appSource.List()
theApps, err := ac.appSource.List()
if err != nil {
c.JSON(500, gin.H{
"error": err.Error(),
})
return
}
if apps == nil {
apps = []peers.App{}
if theApps == nil {
theApps = []apps.App{}
}
c.JSON(200, apps)
c.JSON(200, theApps)
})
}

// NewAppsController bootstraps creation of the API that allows displaying currently exposed apps
func NewAppsController(appSource hello.AppSource) Controller {
func NewAppsController(appSource syncing.AppSource) Controller {
return &appsController{appSource: appSource}
}
6 changes: 3 additions & 3 deletions pkg/api/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package api

import (
"github.com/gin-gonic/gin"
"github.com/glothriel/wormhole/pkg/hello"
"github.com/glothriel/wormhole/pkg/pairing"
"github.com/glothriel/wormhole/pkg/wg"
)

type peerController struct {
peers hello.PeerStorage
peers pairing.PeerStorage
wgConfig *wg.Config
watcher *wg.Watcher
enablePeerDeletion bool
Expand Down Expand Up @@ -66,7 +66,7 @@ func (p *peerController) registerRoutes(r *gin.Engine) {
}

// NewPeersController allows querying and manipulation of the connected peers
func NewPeersController(peers hello.PeerStorage, wgConfig *wg.Config, watcher *wg.Watcher) Controller {
func NewPeersController(peers pairing.PeerStorage, wgConfig *wg.Config, watcher *wg.Watcher) Controller {
return &peerController{
peers: peers,
wgConfig: wgConfig,
Expand Down
4 changes: 2 additions & 2 deletions pkg/peers/apps.go → pkg/apps/apps.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package peers defines basic structures for apps and peers
package peers
// Package apps defines basic structures for apps
package apps

// App represents an application that can be peered
type App struct {
Expand Down
31 changes: 16 additions & 15 deletions pkg/cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"time"

"github.com/glothriel/wormhole/pkg/api"
"github.com/glothriel/wormhole/pkg/hello"
"github.com/glothriel/wormhole/pkg/k8s"
"github.com/glothriel/wormhole/pkg/listeners"
"github.com/glothriel/wormhole/pkg/nginx"
"github.com/glothriel/wormhole/pkg/pairing"
"github.com/glothriel/wormhole/pkg/syncing"
"github.com/glothriel/wormhole/pkg/wg"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -76,20 +77,20 @@ var clientCommand *cli.Command = &cli.Command{
}
remoteListenerRegistry := listeners.NewApps(effectiveExposer)

appStateChangeGenerator := hello.NewAppStateChangeGenerator()
appStateChangeGenerator := syncing.NewAppStateChangeGenerator()

transport := hello.NewHTTPClientPairingTransport(c.String(pairingServerURL.Name))
transport := pairing.NewHTTPClientPairingTransport(c.String(pairingServerURL.Name))
if c.String(inviteTokenFlag.Name) != "" {
transport = hello.NewPSKClientPairingTransport(
transport = pairing.NewPSKClientPairingTransport(
c.String(inviteTokenFlag.Name),
transport,
)
}

pairingKeyCache := hello.NewInMemoryKeyCachingPairingClientStorage()
pairingKeyCache := pairing.NewInMemoryKeyCachingPairingClientStorage()
if c.String(pairingClientCacheDBPath.Name) != "" {
var err error
pairingKeyCache, err = hello.NewBoltKeyCachingPairingClientStorage(c.String(pairingClientCacheDBPath.Name))
pairingKeyCache, err = pairing.NewBoltKeyCachingPairingClientStorage(c.String(pairingClientCacheDBPath.Name))
if err != nil {
logrus.Fatalf("Failed to create pairing key cache: %v", err)
}
Expand All @@ -99,25 +100,25 @@ var clientCommand *cli.Command = &cli.Command{
PrivateKey: privateKey,
Subnet: "32",
}
keyPair := hello.KeyPair{
keyPair := pairing.KeyPair{
PublicKey: publicKey,
PrivateKey: privateKey,
}
client := hello.NewKeyCachingPairingClient(
client := pairing.NewKeyCachingPairingClient(
pairingKeyCache,
wgConfig,
wgReloader,
hello.NewDefaultPairingClient(
pairing.NewDefaultPairingClient(
c.String(peerNameFlag.Name),
wgConfig,
keyPair,
wgReloader,
hello.NewJSONPairingEncoder(),
pairing.NewJSONPairingEncoder(),
transport,
),
)

var pairingResponse hello.PairingResponse
var pairingResponse pairing.Response
for {
var err error
if pairingResponse, err = client.Pair(); err != nil {
Expand All @@ -139,14 +140,14 @@ var clientCommand *cli.Command = &cli.Command{
go localListenerRegistry.Watch(getAppStateChangeGenerator(c).Changes(), make(chan bool))
go remoteListenerRegistry.Watch(appStateChangeGenerator.Changes(), make(chan bool))

sc, scErr := hello.NewHTTPSyncingClient(
sc, scErr := syncing.NewHTTPClient(
c.String(peerNameFlag.Name),
appStateChangeGenerator,
hello.NewJSONSyncingEncoder(),
syncing.NewJSONSyncingEncoder(),
time.Second*5,
hello.NewAddressEnrichingAppSource(
syncing.NewAddressEnrichingAppSource(
pairingResponse.AssignedIP,
hello.NewPeerEnrichingAppSource(
syncing.NewPeerEnrichingAppSource(
c.String(peerNameFlag.Name),
localListenerRegistry,
),
Expand Down
8 changes: 0 additions & 8 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log"
"os"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -48,13 +47,6 @@ func Run() {
},

Before: setLogLevel,
ExitErrHandler: func(_ *cli.Context, _ error) {
if logrus.GetLevel() != logrus.DebugLevel {
logrus.Error(
"Wormhole command failed. For verbose output, please use `wormhole --debug <your-command>`",
)
}
},
}

if runErr := app.Run(os.Args); runErr != nil {
Expand Down
29 changes: 15 additions & 14 deletions pkg/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"time"

"github.com/glothriel/wormhole/pkg/api"
"github.com/glothriel/wormhole/pkg/pairing"
"github.com/glothriel/wormhole/pkg/syncing"

"github.com/glothriel/wormhole/pkg/hello"
"github.com/glothriel/wormhole/pkg/k8s"
"github.com/glothriel/wormhole/pkg/listeners"
"github.com/glothriel/wormhole/pkg/nginx"
Expand Down Expand Up @@ -115,7 +116,7 @@ var serverCommand *cli.Command = &cli.Command{

go appsExposedHere.Watch(getAppStateChangeGenerator(c).Changes(), make(chan bool))

remoteNginxAdapter := hello.NewAppStateChangeGenerator()
remoteNginxAdapter := syncing.NewAppStateChangeGenerator()
go appsExposedFromRemote.Watch(remoteNginxAdapter.Changes(), make(chan bool))

wgConfig := &wg.Config{
Expand All @@ -136,21 +137,21 @@ var serverCommand *cli.Command = &cli.Command{
AllowedIPs: fmt.Sprintf("%s/32,%s/32", savedPeer.IP, wgConfig.Address),
})
}
syncTransport := hello.NewHTTPServerSyncingTransport(&http.Server{
syncTransport := syncing.NewHTTPServerSyncingTransport(&http.Server{
Addr: fmt.Sprintf("%s:%d", c.String(wgAddressFlag.Name), c.Int(intServerListenPort.Name)),
ReadHeaderTimeout: time.Second * 5,
})

appSource := hello.NewAddressEnrichingAppSource(
appSource := syncing.NewAddressEnrichingAppSource(
wgConfig.Address,
hello.NewPeerEnrichingAppSource("server", appsExposedHere),
syncing.NewPeerEnrichingAppSource("server", appsExposedHere),
)

ss := hello.NewSyncingServer(
ss := syncing.NewServer(
c.String(peerNameFlag.Name),
remoteNginxAdapter,
appSource,
hello.NewJSONSyncingEncoder(),
syncing.NewJSONSyncingEncoder(),
syncTransport,
peerStorage,
)
Expand All @@ -159,32 +160,32 @@ var serverCommand *cli.Command = &cli.Command{
if updateErr != nil {
return fmt.Errorf("failed to bootstrap wireguard config: %w", updateErr)
}
peerTransport := hello.NewHTTPServerPairingTransport(&http.Server{
peerTransport := pairing.NewHTTPServerPairingTransport(&http.Server{
Addr: c.String(extServerListenAddress.Name),
ReadHeaderTimeout: time.Second * 5,
})
if c.String(inviteTokenFlag.Name) != "" {
peerTransport = hello.NewPSKPairingServerTransport(
peerTransport = pairing.NewPSKPairingServerTransport(
c.String(inviteTokenFlag.Name),
peerTransport,
)
}
ps := hello.NewPairingServer(
ps := pairing.NewServer(
"server",
fmt.Sprintf("%s:%d", c.String(wgPublicHostFlag.Name), c.Int(wgPortFlag.Name)),
wgConfig,
hello.KeyPair{
pairing.KeyPair{
PublicKey: publicKey,
PrivateKey: privateKey,
},
watcher,
hello.NewJSONPairingEncoder(),
pairing.NewJSONPairingEncoder(),
peerTransport,
hello.NewIPPool(c.String(wgAddressFlag.Name), hello.NewReservedAddressLister(
pairing.NewIPPool(c.String(wgAddressFlag.Name), pairing.NewReservedAddressLister(
peerStorage,
)),
peerStorage,
[]hello.MetadataEnricher{syncTransport},
[]pairing.MetadataEnricher{syncTransport},
)
go ss.Start()
go func() {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/storage.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package cmd

import (
"github.com/glothriel/wormhole/pkg/hello"
"github.com/glothriel/wormhole/pkg/pairing"
"github.com/glothriel/wormhole/pkg/wg"
"github.com/urfave/cli/v2"
)

func getPeerStorage(c *cli.Context) hello.PeerStorage {
func getPeerStorage(c *cli.Context) pairing.PeerStorage {
if c.String(peerStorageDBFlag.Name) == "" {
return hello.NewInMemoryPeerStorage()
return pairing.NewInMemoryPeerStorage()
}
return hello.NewBoltPeerStorage(c.String(peerStorageDBFlag.Name))
return pairing.NewBoltPeerStorage(c.String(peerStorageDBFlag.Name))
}

func getKeyStorage(c *cli.Context) wg.KeyStorage {
Expand Down
Loading
Loading