error handing for '--subordinate' in 'zrok access private' (#789)

This commit is contained in:
Michael Quigley 2024-11-08 14:53:05 -05:00
parent f00835d68b
commit 1face3bb86
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 65 additions and 16 deletions

View File

@ -44,6 +44,7 @@ func (i *agentGrpcImpl) AccessPrivate(_ context.Context, req *agentGrpc.AccessPr
acc.process, err = proctree.StartChild(acc.tail, accCmd...)
if err != nil {
logrus.Errorf("child start '%v': %v", accCmd, err)
return nil, err
}

View File

@ -79,6 +79,9 @@ func newAccessPrivateCommand() *accessPrivateCommand {
func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
root, err := environment.LoadRoot()
if err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("error loading environment", err)
}
@ -126,6 +129,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
}
accessResp, err := zrok.Share.Access(req, auth)
if err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("unable to access", err)
}
@ -136,6 +142,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
if cmd.autoMode {
if accessResp.Payload.BackendMode == "udpTunnel" {
cmd.destroy(accessResp.Payload.FrontendToken, root.Environment().ZitiIdentity, shrToken, zrok, auth)
if cmd.subordinate {
subordinateError(errors.New("auto-addressing is not compatible with the 'udpTunnel' backend mode"))
}
if !panicInstead {
tui.Error("auto-addressing is not compatible with the 'udpTunnel' backend mode", nil)
}
@ -143,24 +152,15 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
}
autoAddress, err := util.AutoListenerAddress("tcp", cmd.autoAddress, cmd.autoStartPort, cmd.autoEndPort)
if err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("unable to automatically find a listener address: %v", err)
}
}
bindAddress = autoAddress
}
if cmd.subordinate {
data := make(map[string]interface{})
data["frontend_token"] = accessResp.Payload.FrontendToken
data["bind_address"] = bindAddress
jsonData, err := json.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
} else {
logrus.Infof("allocated frontend '%v'", accessResp.Payload.FrontendToken)
bindAddress = autoAddress
}
protocol := "http://"
@ -173,6 +173,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
endpointUrl, err := url.Parse(protocol + bindAddress)
if err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("invalid endpoint address", err)
}
@ -189,6 +192,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
RequestsChan: requests,
})
if err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("unable to create private access", err)
}
@ -196,6 +202,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
}
go func() {
if err := fe.Run(); err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("error starting access", err)
}
@ -212,6 +221,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
IdleTime: time.Minute,
})
if err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("unable to create private frontend", err)
}
@ -219,6 +231,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
}
go func() {
if err := fe.Run(); err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("error starting frontend", err)
}
@ -234,6 +249,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
RequestsChan: requests,
})
if err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("unable to create private access", err)
}
@ -241,6 +259,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
}
go func() {
if err := fe.Run(); err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("error starting access", err)
}
@ -258,6 +279,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
RequestsChan: requests,
})
if err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("unable to create private access", err)
}
@ -265,6 +289,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
}
go func() {
if err := fe.Run(); err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("error starting access", err)
}
@ -280,6 +307,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
cfg.RequestsChan = requests
fe, err := proxy.NewFrontend(cfg)
if err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("unable to create private frontend", err)
}
@ -287,6 +317,9 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
}
go func() {
if err := fe.Run(); err != nil {
if cmd.subordinate {
subordinateError(err)
}
if !panicInstead {
tui.Error("unable to run frontend", err)
}
@ -302,6 +335,17 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
os.Exit(0)
}()
if cmd.subordinate {
data := make(map[string]interface{})
data["frontend_token"] = accessResp.Payload.FrontendToken
data["bind_address"] = bindAddress
jsonData, err := json.Marshal(data)
if err != nil {
subordinateError(err)
}
fmt.Println(string(jsonData))
}
if cmd.headless {
logrus.Infof("access the zrok share at the following endpoint: %v", endpointUrl.String())
for {
@ -310,7 +354,6 @@ func (cmd *accessPrivateCommand) accessLocal(args []string, root env_core.Root)
logrus.Infof("%v -> %v %v", req.RemoteAddr, req.Method, req.Path)
}
}
} else if cmd.subordinate {
for {
select {

View File

@ -30,8 +30,8 @@ func newAgentStartCommand() *agentStartCommand {
command := &agentStartCommand{cmd: cmd}
cmd.Run = command.run
cmd.Flags().StringVar(&command.consoleAddress, "console-address", "127.0.0.1", "gRPC gateway address")
cmd.Flags().Uint16Var(&command.consoleStartPort, "console-start-port", 8080, "gRPC gateway starting port")
cmd.Flags().Uint16Var(&command.consoleEndPort, "console-end-port", 8181, "gRPC gateway ending port")
cmd.Flags().Uint16Var(&command.consoleStartPort, "console-start-port", 8888, "gRPC gateway starting port")
cmd.Flags().Uint16Var(&command.consoleEndPort, "console-end-port", 8988, "gRPC gateway ending port")
return command
}

View File

@ -45,3 +45,8 @@ func parseUrl(in string) (string, error) {
return targetEndpoint.String(), nil
}
func subordinateError(err error) {
fmt.Printf("{ \"error\": \"%v\" }\n", err.Error())
os.Exit(1)
}