zrok reserve; zrok share reserved updated to work with --backend-mode (#151)

This commit is contained in:
Michael Quigley 2023-01-10 17:13:34 -05:00
parent 575a3f7030
commit 0516f28b72
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 90 additions and 49 deletions

View File

@ -21,18 +21,20 @@ func init() {
type reserveCommand struct {
basicAuth []string
frontendSelection []string
backendMode string
cmd *cobra.Command
}
func newReserveCommand() *reserveCommand {
cmd := &cobra.Command{
Use: "reserve <public|private> <targetEndpoint>",
Use: "reserve <public|private> <target>",
Short: "Create a reserved share",
Args: cobra.ExactArgs(2),
}
command := &reserveCommand{cmd: cmd}
cmd.Flags().StringArrayVar(&command.basicAuth, "basic-auth", []string{}, "Basic authentication users (<username:password>,...)")
cmd.Flags().StringArrayVar(&command.frontendSelection, "frontends", []string{"public"}, "Selected frontends to use for the share")
cmd.Flags().StringVar(&command.backendMode, "backend-mode", "proxy", "The backend mode {proxy, web}")
cmd.Run = command.run
return command
}
@ -43,6 +45,9 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) {
tui.Error("invalid sharing mode; expecting 'public' or 'private'", nil)
}
var target string
switch cmd.backendMode {
case "proxy":
targetEndpoint, err := url.Parse(args[1])
if err != nil {
if !panicInstead {
@ -53,6 +58,11 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) {
if targetEndpoint.Scheme == "" {
targetEndpoint.Scheme = "https"
}
target = targetEndpoint.String()
case "web":
target = args[1]
}
zrd, err := zrokdir.Load()
if err != nil {
@ -78,8 +88,8 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) {
req.Body = &rest_model_zrok.ShareRequest{
EnvZID: zrd.Env.ZId,
ShareMode: shareMode,
BackendMode: "proxy",
BackendProxyEndpoint: targetEndpoint.String(),
BackendMode: cmd.backendMode,
BackendProxyEndpoint: target,
AuthScheme: string(model.None),
Reserved: true,
}

View File

@ -2,15 +2,17 @@ package main
import (
httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti-test-kitchen/zrok/endpoints"
"github.com/openziti-test-kitchen/zrok/endpoints/proxyBackend"
"github.com/openziti-test-kitchen/zrok/endpoints/webBackend"
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/metadata"
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/share"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/tui"
"github.com/openziti-test-kitchen/zrok/zrokdir"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/url"
"time"
)
@ -36,20 +38,7 @@ func newShareReservedCommand() *shareReservedCommand {
func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
shrToken := args[0]
targetEndpoint := ""
if cmd.overrideEndpoint != "" {
e, err := url.Parse(cmd.overrideEndpoint)
if err != nil {
if !panicInstead {
tui.Error("invalid override endpoint URL", err)
}
panic(err)
}
if e.Scheme == "" {
e.Scheme = "https"
}
targetEndpoint = e.String()
}
var target string
zrd, err := zrokdir.Load()
if err != nil {
@ -80,8 +69,8 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
}
panic(err)
}
if targetEndpoint == "" {
targetEndpoint = resp.Payload.BackendProxyEndpoint
if target == "" {
target = resp.Payload.BackendProxyEndpoint
}
zif, err := zrokdir.ZitiIdentityFile("backend")
@ -91,18 +80,14 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
}
panic(err)
}
cfg := &proxyBackend.Config{
IdentityPath: zif,
EndpointAddress: targetEndpoint,
ShrToken: shrToken,
}
logrus.Infof("sharing target endpoint: '%v'", cfg.EndpointAddress)
if resp.Payload.BackendProxyEndpoint != targetEndpoint {
logrus.Infof("sharing target: '%v'", target)
if resp.Payload.BackendProxyEndpoint != target {
upReq := share.NewUpdateShareParams()
upReq.Body = &rest_model_zrok.UpdateShareRequest{
ShrToken: shrToken,
BackendProxyEndpoint: targetEndpoint,
BackendProxyEndpoint: target,
}
if _, err := zrok.Share.UpdateShare(upReq, auth); err != nil {
if !panicInstead {
@ -110,27 +95,43 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
}
panic(err)
}
logrus.Infof("updated backend proxy endpoint to: %v", targetEndpoint)
logrus.Infof("updated backend proxy endpoint to: %v", target)
} else {
logrus.Infof("using existing backend proxy endpoint: %v", targetEndpoint)
logrus.Infof("using existing backend proxy endpoint: %v", target)
}
httpProxy, err := proxyBackend.NewBackend(cfg)
switch resp.Payload.BackendMode {
case "proxy":
cfg := &proxyBackend.Config{
IdentityPath: zif,
EndpointAddress: target,
ShrToken: shrToken,
}
_, err := cmd.proxyBackendMode(cfg)
if err != nil {
if !panicInstead {
tui.Error("unable to create http backend", err)
tui.Error("unable to create proxy backend handler", err)
}
panic(err)
}
go func() {
if err := httpProxy.Run(); err != nil {
case "web":
cfg := &webBackend.Config{
IdentityPath: zif,
WebRoot: target,
ShrToken: shrToken,
}
_, err := cmd.webBackendMode(cfg)
if err != nil {
if !panicInstead {
tui.Error("unable to run http proxy", err)
tui.Error("unable to create web backend handler", err)
}
panic(err)
}
}()
default:
tui.Error("invalid backend mode", nil)
}
switch resp.Payload.ShareMode {
case "public":
@ -144,3 +145,33 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
time.Sleep(30 * time.Second)
}
}
func (cmd *shareReservedCommand) proxyBackendMode(cfg *proxyBackend.Config) (endpoints.BackendHandler, error) {
be, err := proxyBackend.NewBackend(cfg)
if err != nil {
return nil, errors.Wrap(err, "error creating http proxy backend")
}
go func() {
if err := be.Run(); err != nil {
logrus.Errorf("error running http proxy backend: %v", err)
}
}()
return be, nil
}
func (cmd *shareReservedCommand) webBackendMode(cfg *webBackend.Config) (endpoints.BackendHandler, error) {
be, err := webBackend.NewBackend(cfg)
if err != nil {
return nil, errors.Wrap(err, "error creating http web backend")
}
go func() {
if err := be.Run(); err != nil {
logrus.Errorf("error running http web backend: %v", err)
}
}()
return be, nil
}