From 6379223be2033b58eeb3374f9712175099f8196d Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 30 Nov 2022 11:12:14 -0500 Subject: [PATCH] rough 'share again' (#41) --- cmd/zrok/share_again.go | 126 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 cmd/zrok/share_again.go diff --git a/cmd/zrok/share_again.go b/cmd/zrok/share_again.go new file mode 100644 index 00000000..8e69bc4a --- /dev/null +++ b/cmd/zrok/share_again.go @@ -0,0 +1,126 @@ +package main + +import ( + ui "github.com/gizak/termui/v3" + "github.com/go-openapi/runtime" + httptransport "github.com/go-openapi/runtime/client" + "github.com/openziti-test-kitchen/zrok/endpoints/backend" + "github.com/openziti-test-kitchen/zrok/rest_client_zrok" + "github.com/openziti-test-kitchen/zrok/rest_client_zrok/service" + "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/zrokdir" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "net/url" + "os" + "os/signal" + "syscall" + "time" +) + +func init() { + shareCmd.AddCommand(newShareAgainCommand().cmd) +} + +type shareAgainCommand struct { + cmd *cobra.Command +} + +func newShareAgainCommand() *shareAgainCommand { + cmd := &cobra.Command{ + Use: "again ", + Short: "Share a previously reserved service again", + Args: cobra.ExactArgs(2), + } + command := &shareAgainCommand{cmd: cmd} + cmd.Run = command.run + return command +} + +func (cmd *shareAgainCommand) run(_ *cobra.Command, args []string) { + targetEndpoint, err := url.Parse(args[1]) + if err != nil { + if !panicInstead { + showError("invalid target endpoint URL", err) + } + panic(err) + } + if targetEndpoint.Scheme == "" { + targetEndpoint.Scheme = "https" + } + + env, err := zrokdir.LoadEnvironment() + if err != nil { + if !panicInstead { + showError("unable to load environment; did you 'zrok enable'?", err) + } + panic(err) + } + zif, err := zrokdir.ZitiIdentityFile("backend") + if err != nil { + if !panicInstead { + showError("unable to load ziti identity configuration", err) + } + panic(err) + } + svcToken := args[0] + cfg := &backend.Config{ + IdentityPath: zif, + EndpointAddress: targetEndpoint.String(), + Service: svcToken, + } + + zrok, err := zrokdir.ZrokClient(env.ApiEndpoint) + if err != nil { + if !panicInstead { + showError("unable to create zrok client", err) + } + panic(err) + } + auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.Token) + + c := make(chan os.Signal) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + cmd.destroy(env.ZId, cfg, zrok, auth) + os.Exit(0) + }() + + httpProxy, err := backend.NewHTTP(cfg) + if err != nil { + ui.Close() + if !panicInstead { + showError("unable to create http backend", err) + } + panic(err) + } + + go func() { + if err := httpProxy.Run(); err != nil { + if !panicInstead { + showError("unable to run http proxy", err) + } + panic(err) + } + }() + + logrus.Infof("share your zrok service; use this command for access: 'zrok access private %v'", svcToken) + for { + time.Sleep(30 * time.Second) + } +} + +func (self *shareAgainCommand) destroy(id string, cfg *backend.Config, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) { + logrus.Debugf("shutting down '%v'", cfg.Service) + req := service.NewUnshareParams() + req.Body = &rest_model_zrok.UnshareRequest{ + ZID: id, + SvcName: cfg.Service, + } + if _, err := zrok.Service.Unshare(req, auth); err == nil { + logrus.Debugf("shutdown complete") + } else { + logrus.Errorf("error shutting down: %v", err) + } +}