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 {
|
type reserveCommand struct {
|
||||||
basicAuth []string
|
basicAuth []string
|
||||||
frontendSelection []string
|
frontendSelection []string
|
||||||
|
backendMode string
|
||||||
cmd *cobra.Command
|
cmd *cobra.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
func newReserveCommand() *reserveCommand {
|
func newReserveCommand() *reserveCommand {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "reserve <public|private> <targetEndpoint>",
|
Use: "reserve <public|private> <target>",
|
||||||
Short: "Create a reserved share",
|
Short: "Create a reserved share",
|
||||||
Args: cobra.ExactArgs(2),
|
Args: cobra.ExactArgs(2),
|
||||||
}
|
}
|
||||||
command := &reserveCommand{cmd: cmd}
|
command := &reserveCommand{cmd: cmd}
|
||||||
cmd.Flags().StringArrayVar(&command.basicAuth, "basic-auth", []string{}, "Basic authentication users (<username:password>,...)")
|
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().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
|
cmd.Run = command.run
|
||||||
return command
|
return command
|
||||||
}
|
}
|
||||||
@ -43,6 +45,9 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) {
|
|||||||
tui.Error("invalid sharing mode; expecting 'public' or 'private'", nil)
|
tui.Error("invalid sharing mode; expecting 'public' or 'private'", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var target string
|
||||||
|
switch cmd.backendMode {
|
||||||
|
case "proxy":
|
||||||
targetEndpoint, err := url.Parse(args[1])
|
targetEndpoint, err := url.Parse(args[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
@ -53,6 +58,11 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) {
|
|||||||
if targetEndpoint.Scheme == "" {
|
if targetEndpoint.Scheme == "" {
|
||||||
targetEndpoint.Scheme = "https"
|
targetEndpoint.Scheme = "https"
|
||||||
}
|
}
|
||||||
|
target = targetEndpoint.String()
|
||||||
|
|
||||||
|
case "web":
|
||||||
|
target = args[1]
|
||||||
|
}
|
||||||
|
|
||||||
zrd, err := zrokdir.Load()
|
zrd, err := zrokdir.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -78,8 +88,8 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) {
|
|||||||
req.Body = &rest_model_zrok.ShareRequest{
|
req.Body = &rest_model_zrok.ShareRequest{
|
||||||
EnvZID: zrd.Env.ZId,
|
EnvZID: zrd.Env.ZId,
|
||||||
ShareMode: shareMode,
|
ShareMode: shareMode,
|
||||||
BackendMode: "proxy",
|
BackendMode: cmd.backendMode,
|
||||||
BackendProxyEndpoint: targetEndpoint.String(),
|
BackendProxyEndpoint: target,
|
||||||
AuthScheme: string(model.None),
|
AuthScheme: string(model.None),
|
||||||
Reserved: true,
|
Reserved: true,
|
||||||
}
|
}
|
||||||
|
@ -2,15 +2,17 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
httptransport "github.com/go-openapi/runtime/client"
|
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/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/metadata"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/share"
|
"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/rest_model_zrok"
|
||||||
"github.com/openziti-test-kitchen/zrok/tui"
|
"github.com/openziti-test-kitchen/zrok/tui"
|
||||||
"github.com/openziti-test-kitchen/zrok/zrokdir"
|
"github.com/openziti-test-kitchen/zrok/zrokdir"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"net/url"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,20 +38,7 @@ func newShareReservedCommand() *shareReservedCommand {
|
|||||||
|
|
||||||
func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
|
func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
|
||||||
shrToken := args[0]
|
shrToken := args[0]
|
||||||
targetEndpoint := ""
|
var target string
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
zrd, err := zrokdir.Load()
|
zrd, err := zrokdir.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -80,8 +69,8 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if targetEndpoint == "" {
|
if target == "" {
|
||||||
targetEndpoint = resp.Payload.BackendProxyEndpoint
|
target = resp.Payload.BackendProxyEndpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
zif, err := zrokdir.ZitiIdentityFile("backend")
|
zif, err := zrokdir.ZitiIdentityFile("backend")
|
||||||
@ -91,18 +80,14 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
panic(err)
|
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 := share.NewUpdateShareParams()
|
||||||
upReq.Body = &rest_model_zrok.UpdateShareRequest{
|
upReq.Body = &rest_model_zrok.UpdateShareRequest{
|
||||||
ShrToken: shrToken,
|
ShrToken: shrToken,
|
||||||
BackendProxyEndpoint: targetEndpoint,
|
BackendProxyEndpoint: target,
|
||||||
}
|
}
|
||||||
if _, err := zrok.Share.UpdateShare(upReq, auth); err != nil {
|
if _, err := zrok.Share.UpdateShare(upReq, auth); err != nil {
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
@ -110,27 +95,43 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
logrus.Infof("updated backend proxy endpoint to: %v", targetEndpoint)
|
logrus.Infof("updated backend proxy endpoint to: %v", target)
|
||||||
} else {
|
} 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 err != nil {
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
tui.Error("unable to create http backend", err)
|
tui.Error("unable to create proxy backend handler", err)
|
||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
case "web":
|
||||||
if err := httpProxy.Run(); err != nil {
|
cfg := &webBackend.Config{
|
||||||
|
IdentityPath: zif,
|
||||||
|
WebRoot: target,
|
||||||
|
ShrToken: shrToken,
|
||||||
|
}
|
||||||
|
_, err := cmd.webBackendMode(cfg)
|
||||||
|
if err != nil {
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
tui.Error("unable to run http proxy", err)
|
tui.Error("unable to create web backend handler", err)
|
||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
default:
|
||||||
|
tui.Error("invalid backend mode", nil)
|
||||||
|
}
|
||||||
|
|
||||||
switch resp.Payload.ShareMode {
|
switch resp.Payload.ShareMode {
|
||||||
case "public":
|
case "public":
|
||||||
@ -144,3 +145,33 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
|
|||||||
time.Sleep(30 * time.Second)
|
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