modular backend implementation for share private (#95)

This commit is contained in:
Michael Quigley 2022-12-13 12:24:28 -05:00
parent 67619df5c6
commit ac09f2d749
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 73 additions and 42 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
ui "github.com/gizak/termui/v3"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
@ -26,8 +27,9 @@ func init() {
}
type sharePrivateCommand struct {
basicAuth []string
cmd *cobra.Command
basicAuth []string
backendMode string
cmd *cobra.Command
}
func newSharePrivateCommand() *sharePrivateCommand {
@ -38,20 +40,30 @@ func newSharePrivateCommand() *sharePrivateCommand {
}
command := &sharePrivateCommand{cmd: cmd}
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
return command
}
func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
targetEndpoint, err := url.Parse(args[0])
if err != nil {
if !panicInstead {
showError("invalid target endpoint URL", err)
var target string
switch cmd.backendMode {
case "proxy":
targetEndpoint, err := url.Parse(args[0])
if err != nil {
if !panicInstead {
showError("invalid target endpoint URL", err)
}
panic(err)
}
panic(err)
}
if targetEndpoint.Scheme == "" {
targetEndpoint.Scheme = "https"
if targetEndpoint.Scheme == "" {
targetEndpoint.Scheme = "https"
}
target = targetEndpoint.String()
default:
showError(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil)
}
env, err := zrokdir.LoadEnvironment()
@ -70,7 +82,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
}
cfg := &backend.Config{
IdentityPath: zif,
EndpointAddress: targetEndpoint.String(),
EndpointAddress: target,
}
zrok, err := zrokdir.ZrokClient(env.ApiEndpoint)
@ -120,23 +132,21 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
os.Exit(0)
}()
httpProxy, err := backend.NewHTTP(cfg)
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to create http backend", err)
}
panic(err)
}
go func() {
if err := httpProxy.Run(); err != nil {
switch cmd.backendMode {
case "proxy":
_, err = cmd.proxyBackendMode(cfg)
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to run http proxy", err)
showError("unable to create proxy backend handler", err)
}
panic(err)
}
}()
default:
ui.Close()
showError("invalid backend mode", nil)
}
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)
req := service.NewUnshareParams()
req.Body = &rest_model_zrok.UnshareRequest{

View File

@ -51,10 +51,10 @@ func newSharePublicCommand() *sharePublicCommand {
return command
}
func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
var target string
switch self.backendMode {
switch cmd.backendMode {
case "proxy":
targetEndpoint, err := url.Parse(args[0])
if err != nil {
@ -69,10 +69,10 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
target = targetEndpoint.String()
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 !panicInstead {
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{
EnvZID: env.ZId,
ShareMode: "public",
FrontendSelection: self.frontendSelection,
FrontendSelection: cmd.frontendSelection,
BackendMode: "proxy",
BackendProxyEndpoint: cfg.EndpointAddress,
AuthScheme: string(model.None),
}
if len(self.basicAuth) > 0 {
if len(cmd.basicAuth) > 0 {
logrus.Infof("configuring basic auth")
req.Body.AuthScheme = string(model.Basic)
for _, pair := range self.basicAuth {
for _, pair := range cmd.basicAuth {
tokens := strings.Split(pair, ":")
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])})
@ -148,14 +148,14 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
self.destroy(env.ZId, cfg, zrok, auth)
cmd.destroy(env.ZId, cfg, zrok, auth)
os.Exit(0)
}()
var bh backendHandler
switch self.backendMode {
switch cmd.backendMode {
case "proxy":
bh, err = self.proxyBackendMode(cfg)
bh, err = cmd.proxyBackendMode(cfg)
if err != nil {
ui.Close()
if !panicInstead {
@ -169,7 +169,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
showError("invalid backend mode", nil)
}
if !self.quiet {
if !cmd.quiet {
ui.Clear()
w, h := ui.TerminalDimensions()
@ -210,7 +210,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
switch e.ID {
case "q", "<C-c>":
ui.Close()
self.destroy(env.ZId, cfg, zrok, auth)
cmd.destroy(env.ZId, cfg, zrok, auth)
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)
if err != nil {
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
}
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)
req := service.NewUnshareParams()
req.Body = &rest_model_zrok.UnshareRequest{

View File

@ -100,15 +100,21 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_
logrus.Debugf("allocated service '%v'", svcToken)
reserved := params.Body.Reserved
sid, err := str.CreateService(envId, &store.Service{
ssvc := &store.Service{
ZId: svcZId,
Token: svcToken,
ShareMode: params.Body.ShareMode,
BackendMode: params.Body.BackendMode,
FrontendEndpoint: &frontendEndpoints[0],
BackendProxyEndpoint: &params.Body.BackendProxyEndpoint,
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 {
logrus.Errorf("error creating service record: %v", err)
return service.NewShareInternalServerError()