Skip to content

Commit

Permalink
subordinate protocol update for 'zrok share private' (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Nov 12, 2024
1 parent f5601e3 commit ef5e654
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 127 deletions.
6 changes: 3 additions & 3 deletions cmd/zrok/accessPrivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGQUIT)
go func() {
<-c
cmd.destroy(accessResp.Payload.FrontendToken, root.Environment().ZitiIdentity, shrToken, zrok, auth)
cmd.shutdown(accessResp.Payload.FrontendToken, root.Environment().ZitiIdentity, shrToken, zrok, auth)
os.Exit(0)
}()

Expand Down Expand Up @@ -300,7 +300,7 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
}

close(requests)
cmd.destroy(accessResp.Payload.FrontendToken, root.Environment().ZitiIdentity, shrToken, zrok, auth)
cmd.shutdown(accessResp.Payload.FrontendToken, root.Environment().ZitiIdentity, shrToken, zrok, auth)
}
}

Expand All @@ -314,7 +314,7 @@ func (cmd *accessPrivateCommand) error(err error) {
panic(err)
}

func (cmd *accessPrivateCommand) destroy(frontendName, envZId, shrToken string, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
func (cmd *accessPrivateCommand) shutdown(frontendName, envZId, shrToken string, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
logrus.Infof("shutting down '%v'", shrToken)
req := share.NewUnaccessParams()
req.Body = &rest_model_zrok.UnaccessRequest{
Expand Down
124 changes: 49 additions & 75 deletions cmd/zrok/sharePrivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/openziti/zrok/environment/env_core"
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/openziti/zrok/tui"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net"
Expand Down Expand Up @@ -71,10 +72,7 @@ func newSharePrivateCommand() *sharePrivateCommand {
func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
root, err := environment.LoadRoot()
if err != nil {
if !panicInstead {
tui.Error("error loading environment", err)
}
panic(err)
cmd.error("error loading environment", err)
}

if !root.IsEnabled() {
Expand Down Expand Up @@ -105,87 +103,78 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {
switch cmd.backendMode {
case "proxy":
if len(args) != 1 {
tui.Error("the 'proxy' backend mode expects a <target>", nil)
cmd.error("unable to create share", errors.New("the 'proxy' backend mode expects a <target>"))
}
v, err := parseUrl(args[0])
if err != nil {
if !panicInstead {
tui.Error("invalid target endpoint URL", err)
}
panic(err)
cmd.error("invalid target endpoint URL", err)
}
target = v

case "web":
if len(args) != 1 {
tui.Error("the 'web' backend mode expects a <target>", nil)
cmd.error("unable to create share", errors.New("the 'web' backend mode expects a <target>"))
}
target = args[0]

case "tcpTunnel":
if len(args) != 1 {
tui.Error("the 'tcpTunnel' backend mode expects a <target>", nil)
cmd.error("unable to create share", errors.New("the 'tcpTunnel' backend mode expects a <target>"))
}
target = args[0]

case "udpTunnel":
if len(args) != 1 {
tui.Error("the 'udpTunnel' backend mode expects a <target>", nil)
cmd.error("unable to create share", errors.New("the 'udpTunnel' backend mode expects a <target>"))
}
target = args[0]

case "caddy":
if len(args) != 1 {
tui.Error("the 'caddy' backend mode expects a <target>", nil)
cmd.error("unable to create share", errors.New("the 'caddy' backend mode expects a <target>"))
}
target = args[0]
cmd.headless = true

case "drive":
if len(args) != 1 {
tui.Error("the 'drive' backend mode expects a <target>", nil)
cmd.error("unable to create share", errors.New("the 'drive' backend mode expects a <target>"))
}
target = args[0]

case "socks":
if len(args) != 0 {
tui.Error("the 'socks' backend mode does not expect <target>", nil)
cmd.error("unable to create share", errors.New("the 'socks' backend mode expects a <target>"))
}
target = "socks"

case "vpn":
if len(args) == 1 {
_, _, err := net.ParseCIDR(args[0])
if err != nil {
tui.Error("the 'vpn' backend expect valid CIDR <target>", err)
cmd.error("unable to create share", errors.New("the 'vpn' backend mode expects a valid CIDR <target>"))
}
target = args[0]
} else {
target = vpn.DefaultTarget()
}

default:
tui.Error(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web, tcpTunnel, udpTunnel, caddy, drive}", cmd.backendMode), nil)
cmd.error("unable to create share", fmt.Errorf("invalid backend mode '%v'; expected {proxy, web, tcpTunnel, udpTunnel, caddy, drive}", cmd.backendMode))
}

root, err := environment.LoadRoot()
if err != nil {
if !panicInstead {
tui.Error("unable to load environment", err)
}
panic(err)
cmd.error("unable to load environment", err)
}

if !root.IsEnabled() {
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
cmd.error("unable to create share", errors.New("unable to load environment; did you 'zrok enable'?"))
}

zif, err := root.ZitiIdentityNamed(root.EnvironmentIdentityName())
if err != nil {
if !panicInstead {
tui.Error("unable to load ziti identity configuration", err)
}
panic(err)
cmd.error("unable to load ziti identity configuration", err)
}

req := &sdk.ShareRequest{
Expand All @@ -199,21 +188,7 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {
}
shr, err := sdk.CreateShare(root, req)
if err != nil {
if !panicInstead {
tui.Error("unable to create share", err)
}
panic(err)
}

if cmd.subordinate {
data := make(map[string]interface{})
data["token"] = shr.Token
data["frontend_endpoints"] = shr.FrontendEndpoints
jsonData, err := json.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
cmd.error("unable to create share", err)
}

shareDescription := fmt.Sprintf("access your share with: %v", tui.Code.Render(fmt.Sprintf("zrok access private %v", shr.Token)))
Expand Down Expand Up @@ -244,10 +219,7 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {

be, err := proxy.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating proxy backend", err)
}
panic(err)
cmd.error("unable to create 'proxy' backend", err)
}

go func() {
Expand All @@ -266,10 +238,7 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {

be, err := proxy.NewCaddyWebBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating web backend", err)
}
panic(err)
cmd.error("unable to create 'web' backend", err)
}

go func() {
Expand All @@ -288,10 +257,7 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {

be, err := tcpTunnel.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating tcpTunnel backend", err)
}
panic(err)
cmd.error("unable to create 'tcpTunnel' backend", err)
}

go func() {
Expand All @@ -310,10 +276,7 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {

be, err := udpTunnel.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating udpTunnel backend", err)
}
panic(err)
cmd.error("unable to create 'udpTunnel' backend", err)
}

go func() {
Expand All @@ -332,10 +295,7 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {
be, err := proxy.NewCaddyfileBackend(cfg)
if err != nil {
cmd.shutdown(root, shr)
if !panicInstead {
tui.Error("error creating caddy backend", err)
}
panic(err)
cmd.error("unable to create 'caddy' backend", err)
}

go func() {
Expand All @@ -354,10 +314,7 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {

be, err := drive.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating drive backend", err)
}
panic(err)
cmd.error("unable to create 'drive' backend", err)
}

go func() {
Expand All @@ -375,10 +332,7 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {

be, err := socks.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating socks backend", err)
}
panic(err)
cmd.error("unable to create 'socks' backend", err)
}

go func() {
Expand All @@ -397,10 +351,7 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {

be, err := vpn.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating VPN backend", err)
}
panic(err)
cmd.error("unable to create 'vpn' backend", err)
}

go func() {
Expand All @@ -410,10 +361,22 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {
}()

default:
tui.Error("invalid backend mode", nil)
cmd.error("unable to create share", errors.New("invalid backend mode"))
}

if cmd.subordinate {
data := make(map[string]interface{})
data["message"] = "boot"
data["token"] = shr.Token
data["frontend_endpoints"] = shr.FrontendEndpoints
jsonData, err := json.Marshal(data)
if err != nil {
cmd.error("unable to create share", err)
}
fmt.Println(string(jsonData))
}

if cmd.headless {
if cmd.headless && !cmd.subordinate {
logrus.Infof("allow other to access your share with the following command:\nzrok access private %v", shr.Token)
for {
select {
Expand All @@ -427,6 +390,7 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {
select {
case req := <-requests:
data := make(map[string]interface{})
data["message"] = "access"
data["remote_address"] = req.RemoteAddr
data["method"] = req.Method
data["path"] = req.Path
Expand Down Expand Up @@ -461,6 +425,16 @@ func (cmd *sharePrivateCommand) shareLocal(args []string, root env_core.Root) {
}
}

func (cmd *sharePrivateCommand) error(msg string, err error) {
if cmd.subordinate {
subordinateError(errors.Wrap(err, msg))
}
if !panicInstead {
tui.Error(msg, err)
}
panic(errors.Wrap(err, msg))
}

func (cmd *sharePrivateCommand) shutdown(root env_core.Root, shr *sdk.Share) {
logrus.Debugf("shutting down '%v'", shr.Token)
if err := sdk.DeleteShare(root, shr); err != nil {
Expand Down
Loading

0 comments on commit ef5e654

Please sign in to comment.