mirror of
https://github.com/openziti/zrok.git
synced 2025-06-20 09:48:07 +02:00
zrok reserve; zrok share reserved updated to work with --backend-mode (#151)
This commit is contained in:
parent
575a3f7030
commit
0516f28b72
@ -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,15 +45,23 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) {
|
||||
tui.Error("invalid sharing mode; expecting 'public' or 'private'", nil)
|
||||
}
|
||||
|
||||
targetEndpoint, err := url.Parse(args[1])
|
||||
if err != nil {
|
||||
if !panicInstead {
|
||||
tui.Error("invalid target endpoint URL", err)
|
||||
var target string
|
||||
switch cmd.backendMode {
|
||||
case "proxy":
|
||||
targetEndpoint, err := url.Parse(args[1])
|
||||
if err != nil {
|
||||
if !panicInstead {
|
||||
tui.Error("invalid target endpoint URL", err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
if targetEndpoint.Scheme == "" {
|
||||
targetEndpoint.Scheme = "https"
|
||||
if targetEndpoint.Scheme == "" {
|
||||
targetEndpoint.Scheme = "https"
|
||||
}
|
||||
target = targetEndpoint.String()
|
||||
|
||||
case "web":
|
||||
target = args[1]
|
||||
}
|
||||
|
||||
zrd, err := zrokdir.Load()
|
||||
@ -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,
|
||||
}
|
||||
|
@ -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)
|
||||
if err != nil {
|
||||
if !panicInstead {
|
||||
tui.Error("unable to create http backend", err)
|
||||
switch resp.Payload.BackendMode {
|
||||
case "proxy":
|
||||
cfg := &proxyBackend.Config{
|
||||
IdentityPath: zif,
|
||||
EndpointAddress: target,
|
||||
ShrToken: shrToken,
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := httpProxy.Run(); err != nil {
|
||||
_, err := cmd.proxyBackendMode(cfg)
|
||||
if err != nil {
|
||||
if !panicInstead {
|
||||
tui.Error("unable to run http proxy", err)
|
||||
tui.Error("unable to create proxy backend handler", err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
case "web":
|
||||
cfg := &webBackend.Config{
|
||||
IdentityPath: zif,
|
||||
WebRoot: target,
|
||||
ShrToken: shrToken,
|
||||
}
|
||||
_, err := cmd.webBackendMode(cfg)
|
||||
if err != nil {
|
||||
if !panicInstead {
|
||||
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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user