tunnel service call wiring (#3)

This commit is contained in:
Michael Quigley 2022-07-26 16:00:59 -04:00
parent 54328c488b
commit 83b141ae00
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
7 changed files with 138 additions and 39 deletions

View File

@ -1,13 +1,11 @@
package main
import (
"fmt"
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/identity"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/pkg/errors"
"github.com/openziti-test-kitchen/zrok/zrokdir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
func init() {
@ -17,33 +15,27 @@ func init() {
var enableCmd = &cobra.Command{
Use: "enable <token>",
Short: "Enable an environment for zrok",
Args: cobra.ExactArgs(1),
Run: enable,
}
func enable(_ *cobra.Command, args []string) {
if len(args) != 1 {
panic(errors.Errorf("provide a single zrok token"))
}
token := args[0]
zrok := newZrokClient()
req := identity.NewEnableParams()
req.Body = &rest_model_zrok.EnableRequest{
Token: args[0],
Token: token,
}
resp, err := zrok.Identity.Enable(req)
if err != nil {
panic(err)
}
cfgFile, err := os.Create(fmt.Sprintf("%v.json", resp.Payload.Identity))
if err != nil {
if err := zrokdir.WriteToken(token); err != nil {
panic(err)
}
defer func() { _ = cfgFile.Close() }()
_, err = cfgFile.Write([]byte(resp.Payload.Cfg))
if err != nil {
if err := zrokdir.WriteIdentity(resp.Payload.Identity); err != nil {
panic(err)
}
logrus.Infof("enabled, identity = '%v'", resp.Payload.Identity)
}

45
cmd/zrok/http.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"github.com/openziti-test-kitchen/zrok/http"
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/tunnel"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/zrokdir"
"github.com/spf13/cobra"
)
var httpCmd = &cobra.Command{
Use: "http <endpoint>",
Short: "Start an http terminator",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
idCfg, err := zrokdir.IdentityFile()
if err != nil {
panic(err)
}
cfg := &http.Config{
IdentityPath: idCfg,
EndpointAddress: args[0],
}
token, err := zrokdir.ReadToken()
if err != nil {
panic(err)
}
zrok := newZrokClient()
req := tunnel.NewTunnelParams()
req.Body = &rest_model_zrok.TunnelRequest{
Endpoint: cfg.EndpointAddress,
Token: token,
}
resp, err := zrok.Tunnel.Tunnel(req)
if err != nil {
panic(err)
}
cfg.Service = resp.Payload.Service
if err := http.Run(cfg); err != nil {
panic(err)
}
},
}

View File

@ -2,8 +2,6 @@ package main
import (
"github.com/michaelquigley/pfxlog"
"github.com/openziti-test-kitchen/zrok/http"
"github.com/openziti-test-kitchen/zrok/proxy"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
@ -16,7 +14,6 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose logging")
rootCmd.PersistentFlags().StringVarP(&endpoint, "endpoint", "e", "localhost:10888", "zrok endpoint address")
rootCmd.AddCommand(httpCmd)
rootCmd.AddCommand(proxyCmd)
}
var rootCmd = &cobra.Command{
@ -31,26 +28,6 @@ var rootCmd = &cobra.Command{
var verbose bool
var endpoint string
var httpCmd = &cobra.Command{
Use: "http <identity>",
Short: "Start an http terminator",
Run: func(_ *cobra.Command, args []string) {
if err := http.Run(&http.Config{IdentityPath: args[0]}); err != nil {
panic(err)
}
},
}
var proxyCmd = &cobra.Command{
Use: "proxy <configPath>",
Short: "Start a zrok proxy",
Run: func(_ *cobra.Command, args []string) {
if err := proxy.Run(&proxy.Config{IdentityPath: args[0], Address: "0.0.0.0:10111"}); err != nil {
panic(err)
}
},
}
func main() {
if err := rootCmd.Execute(); err != nil {
panic(err)

20
cmd/zrok/proxy.go Normal file
View File

@ -0,0 +1,20 @@
package main
import (
"github.com/openziti-test-kitchen/zrok/proxy"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(proxyCmd)
}
var proxyCmd = &cobra.Command{
Use: "proxy <configPath>",
Short: "Start a zrok proxy",
Run: func(_ *cobra.Command, args []string) {
if err := proxy.Run(&proxy.Config{IdentityPath: args[0], Address: "0.0.0.0:10111"}); err != nil {
panic(err)
}
},
}

View File

@ -3,4 +3,5 @@ package http
type Config struct {
IdentityPath string
EndpointAddress string
Service string
}

View File

@ -18,12 +18,12 @@ func Run(cfg *Config) error {
if err != nil {
return errors.Wrap(err, "error loading config")
}
listener, err := ziti.NewContextWithConfig(zcfg).ListenWithOptions("zrok", &options)
listener, err := ziti.NewContextWithConfig(zcfg).ListenWithOptions(cfg.Service, &options)
if err != nil {
return errors.Wrap(err, "error listening")
}
proxy, err := util.NewProxy("http://localhost:3000")
proxy, err := util.NewProxy(cfg.EndpointAddress)
if err != nil {
return err
}

64
zrokdir/zrokdir.go Normal file
View File

@ -0,0 +1,64 @@
package zrokdir
import (
"os"
"path/filepath"
)
func ReadToken() (string, error) {
path, err := tokenFile()
if err != nil {
return "", err
}
token, err := os.ReadFile(path)
if err != nil {
return "", err
}
return string(token), nil
}
func WriteToken(token string) error {
path, err := tokenFile()
if err != nil {
return err
}
if err := os.WriteFile(path, []byte(token), os.FileMode(400)); err != nil {
return err
}
return nil
}
func WriteIdentity(data string) error {
path, err := IdentityFile()
if err != nil {
return err
}
if err := os.WriteFile(path, []byte(data), os.FileMode(400)); err != nil {
return err
}
return nil
}
func IdentityFile() (string, error) {
zrok, err := zrokDir()
if err != nil {
return "", err
}
return filepath.Join(zrok, "identity.json"), nil
}
func tokenFile() (string, error) {
zrok, err := zrokDir()
if err != nil {
return "", err
}
return filepath.Join(zrok, "token"), nil
}
func zrokDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".zrok"), nil
}