mirror of
https://github.com/openziti/zrok.git
synced 2025-01-03 12:39:07 +01:00
modular backend implementation for share private (#95)
This commit is contained in:
parent
67619df5c6
commit
ac09f2d749
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
ui "github.com/gizak/termui/v3"
|
ui "github.com/gizak/termui/v3"
|
||||||
"github.com/go-openapi/runtime"
|
"github.com/go-openapi/runtime"
|
||||||
httptransport "github.com/go-openapi/runtime/client"
|
httptransport "github.com/go-openapi/runtime/client"
|
||||||
@ -27,6 +28,7 @@ func init() {
|
|||||||
|
|
||||||
type sharePrivateCommand struct {
|
type sharePrivateCommand struct {
|
||||||
basicAuth []string
|
basicAuth []string
|
||||||
|
backendMode string
|
||||||
cmd *cobra.Command
|
cmd *cobra.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,11 +40,16 @@ func newSharePrivateCommand() *sharePrivateCommand {
|
|||||||
}
|
}
|
||||||
command := &sharePrivateCommand{cmd: cmd}
|
command := &sharePrivateCommand{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().StringVar(&command.backendMode, "backend-mode", "proxy", "The backend mode {proxy, web}")
|
||||||
cmd.Run = command.run
|
cmd.Run = command.run
|
||||||
return command
|
return command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
|
func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
|
||||||
|
var target string
|
||||||
|
|
||||||
|
switch cmd.backendMode {
|
||||||
|
case "proxy":
|
||||||
targetEndpoint, err := url.Parse(args[0])
|
targetEndpoint, err := url.Parse(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
@ -53,6 +60,11 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
|
|||||||
if targetEndpoint.Scheme == "" {
|
if targetEndpoint.Scheme == "" {
|
||||||
targetEndpoint.Scheme = "https"
|
targetEndpoint.Scheme = "https"
|
||||||
}
|
}
|
||||||
|
target = targetEndpoint.String()
|
||||||
|
|
||||||
|
default:
|
||||||
|
showError(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil)
|
||||||
|
}
|
||||||
|
|
||||||
env, err := zrokdir.LoadEnvironment()
|
env, err := zrokdir.LoadEnvironment()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -70,7 +82,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
cfg := &backend.Config{
|
cfg := &backend.Config{
|
||||||
IdentityPath: zif,
|
IdentityPath: zif,
|
||||||
EndpointAddress: targetEndpoint.String(),
|
EndpointAddress: target,
|
||||||
}
|
}
|
||||||
|
|
||||||
zrok, err := zrokdir.ZrokClient(env.ApiEndpoint)
|
zrok, err := zrokdir.ZrokClient(env.ApiEndpoint)
|
||||||
@ -120,23 +132,21 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
httpProxy, err := backend.NewHTTP(cfg)
|
switch cmd.backendMode {
|
||||||
|
case "proxy":
|
||||||
|
_, err = cmd.proxyBackendMode(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Close()
|
ui.Close()
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
showError("unable to create http backend", err)
|
showError("unable to create proxy backend handler", err)
|
||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
default:
|
||||||
if err := httpProxy.Run(); err != nil {
|
ui.Close()
|
||||||
if !panicInstead {
|
showError("invalid backend mode", nil)
|
||||||
showError("unable to run http proxy", err)
|
|
||||||
}
|
}
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
logrus.Infof("share your zrok service; use this command for access: 'zrok access private %v'", resp.Payload.SvcToken)
|
logrus.Infof("share your zrok service; use this command for access: 'zrok access private %v'", resp.Payload.SvcToken)
|
||||||
|
|
||||||
@ -145,7 +155,22 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *sharePrivateCommand) destroy(id string, cfg *backend.Config, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
func (cmd *sharePrivateCommand) proxyBackendMode(cfg *backend.Config) (backendHandler, error) {
|
||||||
|
httpProxy, err := backend.NewHTTP(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error creating http proxy backend")
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := httpProxy.Run(); err != nil {
|
||||||
|
logrus.Errorf("error running http proxy backend: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return httpProxy, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cmd *sharePrivateCommand) destroy(id string, cfg *backend.Config, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
||||||
logrus.Debugf("shutting down '%v'", cfg.Service)
|
logrus.Debugf("shutting down '%v'", cfg.Service)
|
||||||
req := service.NewUnshareParams()
|
req := service.NewUnshareParams()
|
||||||
req.Body = &rest_model_zrok.UnshareRequest{
|
req.Body = &rest_model_zrok.UnshareRequest{
|
||||||
|
@ -51,10 +51,10 @@ func newSharePublicCommand() *sharePublicCommand {
|
|||||||
return command
|
return command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
||||||
var target string
|
var target string
|
||||||
|
|
||||||
switch self.backendMode {
|
switch cmd.backendMode {
|
||||||
case "proxy":
|
case "proxy":
|
||||||
targetEndpoint, err := url.Parse(args[0])
|
targetEndpoint, err := url.Parse(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -69,10 +69,10 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
target = targetEndpoint.String()
|
target = targetEndpoint.String()
|
||||||
|
|
||||||
default:
|
default:
|
||||||
showError(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", self.backendMode), nil)
|
showError(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.quiet {
|
if !cmd.quiet {
|
||||||
if err := ui.Init(); err != nil {
|
if err := ui.Init(); err != nil {
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
showError("unable to initialize user interface", err)
|
showError("unable to initialize user interface", err)
|
||||||
@ -117,15 +117,15 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
req.Body = &rest_model_zrok.ShareRequest{
|
req.Body = &rest_model_zrok.ShareRequest{
|
||||||
EnvZID: env.ZId,
|
EnvZID: env.ZId,
|
||||||
ShareMode: "public",
|
ShareMode: "public",
|
||||||
FrontendSelection: self.frontendSelection,
|
FrontendSelection: cmd.frontendSelection,
|
||||||
BackendMode: "proxy",
|
BackendMode: "proxy",
|
||||||
BackendProxyEndpoint: cfg.EndpointAddress,
|
BackendProxyEndpoint: cfg.EndpointAddress,
|
||||||
AuthScheme: string(model.None),
|
AuthScheme: string(model.None),
|
||||||
}
|
}
|
||||||
if len(self.basicAuth) > 0 {
|
if len(cmd.basicAuth) > 0 {
|
||||||
logrus.Infof("configuring basic auth")
|
logrus.Infof("configuring basic auth")
|
||||||
req.Body.AuthScheme = string(model.Basic)
|
req.Body.AuthScheme = string(model.Basic)
|
||||||
for _, pair := range self.basicAuth {
|
for _, pair := range cmd.basicAuth {
|
||||||
tokens := strings.Split(pair, ":")
|
tokens := strings.Split(pair, ":")
|
||||||
if len(tokens) == 2 {
|
if len(tokens) == 2 {
|
||||||
req.Body.AuthUsers = append(req.Body.AuthUsers, &rest_model_zrok.AuthUser{Username: strings.TrimSpace(tokens[0]), Password: strings.TrimSpace(tokens[1])})
|
req.Body.AuthUsers = append(req.Body.AuthUsers, &rest_model_zrok.AuthUser{Username: strings.TrimSpace(tokens[0]), Password: strings.TrimSpace(tokens[1])})
|
||||||
@ -148,14 +148,14 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
go func() {
|
go func() {
|
||||||
<-c
|
<-c
|
||||||
self.destroy(env.ZId, cfg, zrok, auth)
|
cmd.destroy(env.ZId, cfg, zrok, auth)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var bh backendHandler
|
var bh backendHandler
|
||||||
switch self.backendMode {
|
switch cmd.backendMode {
|
||||||
case "proxy":
|
case "proxy":
|
||||||
bh, err = self.proxyBackendMode(cfg)
|
bh, err = cmd.proxyBackendMode(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Close()
|
ui.Close()
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
@ -169,7 +169,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
showError("invalid backend mode", nil)
|
showError("invalid backend mode", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.quiet {
|
if !cmd.quiet {
|
||||||
ui.Clear()
|
ui.Clear()
|
||||||
w, h := ui.TerminalDimensions()
|
w, h := ui.TerminalDimensions()
|
||||||
|
|
||||||
@ -210,7 +210,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
switch e.ID {
|
switch e.ID {
|
||||||
case "q", "<C-c>":
|
case "q", "<C-c>":
|
||||||
ui.Close()
|
ui.Close()
|
||||||
self.destroy(env.ZId, cfg, zrok, auth)
|
cmd.destroy(env.ZId, cfg, zrok, auth)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -237,7 +237,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *sharePublicCommand) proxyBackendMode(cfg *backend.Config) (backendHandler, error) {
|
func (cmd *sharePublicCommand) proxyBackendMode(cfg *backend.Config) (backendHandler, error) {
|
||||||
httpProxy, err := backend.NewHTTP(cfg)
|
httpProxy, err := backend.NewHTTP(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "error creating http proxy backend")
|
return nil, errors.Wrap(err, "error creating http proxy backend")
|
||||||
@ -252,7 +252,7 @@ func (self *sharePublicCommand) proxyBackendMode(cfg *backend.Config) (backendHa
|
|||||||
return httpProxy, nil
|
return httpProxy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *sharePublicCommand) destroy(id string, cfg *backend.Config, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
func (cmd *sharePublicCommand) destroy(id string, cfg *backend.Config, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
||||||
logrus.Debugf("shutting down '%v'", cfg.Service)
|
logrus.Debugf("shutting down '%v'", cfg.Service)
|
||||||
req := service.NewUnshareParams()
|
req := service.NewUnshareParams()
|
||||||
req.Body = &rest_model_zrok.UnshareRequest{
|
req.Body = &rest_model_zrok.UnshareRequest{
|
||||||
|
@ -100,15 +100,21 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_
|
|||||||
logrus.Debugf("allocated service '%v'", svcToken)
|
logrus.Debugf("allocated service '%v'", svcToken)
|
||||||
|
|
||||||
reserved := params.Body.Reserved
|
reserved := params.Body.Reserved
|
||||||
sid, err := str.CreateService(envId, &store.Service{
|
ssvc := &store.Service{
|
||||||
ZId: svcZId,
|
ZId: svcZId,
|
||||||
Token: svcToken,
|
Token: svcToken,
|
||||||
ShareMode: params.Body.ShareMode,
|
ShareMode: params.Body.ShareMode,
|
||||||
BackendMode: params.Body.BackendMode,
|
BackendMode: params.Body.BackendMode,
|
||||||
FrontendEndpoint: &frontendEndpoints[0],
|
|
||||||
BackendProxyEndpoint: ¶ms.Body.BackendProxyEndpoint,
|
BackendProxyEndpoint: ¶ms.Body.BackendProxyEndpoint,
|
||||||
Reserved: reserved,
|
Reserved: reserved,
|
||||||
}, tx)
|
}
|
||||||
|
if len(frontendEndpoints) > 0 {
|
||||||
|
ssvc.FrontendEndpoint = &frontendEndpoints[0]
|
||||||
|
} else if ssvc.ShareMode == "private" {
|
||||||
|
ssvc.FrontendEndpoint = &ssvc.ShareMode
|
||||||
|
}
|
||||||
|
|
||||||
|
sid, err := str.CreateService(envId, ssvc, tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("error creating service record: %v", err)
|
logrus.Errorf("error creating service record: %v", err)
|
||||||
return service.NewShareInternalServerError()
|
return service.NewShareInternalServerError()
|
||||||
|
Loading…
Reference in New Issue
Block a user