From 83b141ae005a6e0f17ad091530220fa2bb6643bb Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Tue, 26 Jul 2022 16:00:59 -0400 Subject: [PATCH] tunnel service call wiring (#3) --- cmd/zrok/enable.go | 20 +++++---------- cmd/zrok/http.go | 45 ++++++++++++++++++++++++++++++++ cmd/zrok/main.go | 23 ----------------- cmd/zrok/proxy.go | 20 +++++++++++++++ http/config.go | 1 + http/http.go | 4 +-- zrokdir/zrokdir.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 138 insertions(+), 39 deletions(-) create mode 100644 cmd/zrok/http.go create mode 100644 cmd/zrok/proxy.go create mode 100644 zrokdir/zrokdir.go diff --git a/cmd/zrok/enable.go b/cmd/zrok/enable.go index a27e7997..9d84406c 100644 --- a/cmd/zrok/enable.go +++ b/cmd/zrok/enable.go @@ -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 ", 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) } diff --git a/cmd/zrok/http.go b/cmd/zrok/http.go new file mode 100644 index 00000000..191b634f --- /dev/null +++ b/cmd/zrok/http.go @@ -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 ", + 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) + } + }, +} diff --git a/cmd/zrok/main.go b/cmd/zrok/main.go index 5690ff53..0c216470 100644 --- a/cmd/zrok/main.go +++ b/cmd/zrok/main.go @@ -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 ", - 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 ", - 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) diff --git a/cmd/zrok/proxy.go b/cmd/zrok/proxy.go new file mode 100644 index 00000000..029f4608 --- /dev/null +++ b/cmd/zrok/proxy.go @@ -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 ", + 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) + } + }, +} diff --git a/http/config.go b/http/config.go index 1dc1c40c..4c9b2a51 100644 --- a/http/config.go +++ b/http/config.go @@ -3,4 +3,5 @@ package http type Config struct { IdentityPath string EndpointAddress string + Service string } diff --git a/http/http.go b/http/http.go index aca1a9fd..329c4ec6 100644 --- a/http/http.go +++ b/http/http.go @@ -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 } diff --git a/zrokdir/zrokdir.go b/zrokdir/zrokdir.go new file mode 100644 index 00000000..4251262c --- /dev/null +++ b/zrokdir/zrokdir.go @@ -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 +}