Skip to content

Commit

Permalink
working 'zrok agent access private' (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Sep 17, 2024
1 parent e6a74ad commit fb23d23
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
18 changes: 18 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ func (a *Agent) manager() {
} else {
logrus.Debug("skipping unidentified (orphaned) share removal")
}

case inAccess := <-a.inAccesses:
logrus.Infof("adding new access '%v'", inAccess.frontendToken)
a.accesses[inAccess.frontendToken] = inAccess

case outAccess := <-a.outAccesses:
if outAccess.frontendToken != "" {
logrus.Infof("removing access '%v'", outAccess.frontendToken)
if err := proctree.StopChild(outAccess.process); err != nil {
logrus.Errorf("error stopping access '%v': %v", outAccess.frontendToken, err)
}
if err := proctree.WaitChild(outAccess.process); err != nil {
logrus.Errorf("error joining access '%v': %v", outAccess.frontendToken, err)
}
delete(a.accesses, outAccess.frontendToken)
} else {
logrus.Debug("skipping unidentified (orphaned) access removal")
}
}
}
}
Expand Down
65 changes: 65 additions & 0 deletions cmd/zrok/agentAccessPrivate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"context"
"fmt"
"github.com/openziti/zrok/agent/agentClient"
"github.com/openziti/zrok/agent/agentGrpc"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/tui"
"github.com/spf13/cobra"
)

func init() {
agentAccessCmd.AddCommand(newAgentAccessPrivateCommand().cmd)
}

type agentAccessPrivateCommand struct {
bindAddress string
responseHeaders []string
cmd *cobra.Command
}

func newAgentAccessPrivateCommand() *agentAccessPrivateCommand {
cmd := &cobra.Command{
Use: "private <token>",
Short: "Bind a private access in the zrok Agent",
Args: cobra.ExactArgs(1),
}
command := &agentAccessPrivateCommand{cmd: cmd}
cmd.Flags().StringVarP(&command.bindAddress, "bind", "b", "127.0.0.1:9191", "The address to bind the private frontend")
cmd.Flags().StringArrayVar(&command.responseHeaders, "response-header", []string{}, "Add a response header ('key:value')")
cmd.Run = command.run
return command
}

func (cmd *agentAccessPrivateCommand) run(_ *cobra.Command, args []string) {
root, err := environment.LoadRoot()
if err != nil {
if !panicInstead {
tui.Error("unable to load environment", err)
}
panic(err)
}

if !root.IsEnabled() {
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
}

client, conn, err := agentClient.NewClient(root)
if err != nil {
tui.Error("error connecting to agent", err)
}
defer conn.Close()

acc, err := client.PrivateAccess(context.Background(), &agentGrpc.PrivateAccessRequest{
Token: args[0],
BindAddress: cmd.bindAddress,
ResponseHeaders: cmd.responseHeaders,
})
if err != nil {
tui.Error("error creating access", err)
}

fmt.Println(acc)
}
2 changes: 0 additions & 2 deletions cmd/zrok/agentSharePrivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func init() {

type agentSharePrivateCommand struct {
backendMode string
headless bool
insecure bool
closed bool
accessGrants []string
Expand All @@ -34,7 +33,6 @@ func newAgentSharePrivateCommand() *agentSharePrivateCommand {
}
command := &agentSharePrivateCommand{cmd: cmd}
cmd.Flags().StringVarP(&command.backendMode, "backend-mode", "b", "proxy", "The backend mode {proxy, web, tcpTunnel, udpTunnel, caddy, drive, socks, vpn}")
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
cmd.Flags().BoolVar(&command.insecure, "insecure", false, "Enable insecure TLS certificate validation for <target>")
cmd.Flags().BoolVar(&command.closed, "closed", false, "Enable closed permission mode (see --access-grant)")
cmd.Flags().StringArrayVar(&command.accessGrants, "access-grant", []string{}, "zrok accounts that are allowed to access this share (see --closed)")
Expand Down
6 changes: 6 additions & 0 deletions cmd/zrok/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func init() {
adminCmd.AddCommand(adminListCmd)
adminCmd.AddCommand(adminUpdateCmd)
rootCmd.AddCommand(agentCmd)
agentCmd.AddCommand(agentAccessCmd)
agentCmd.AddCommand(agentShareCmd)
agentCmd.AddCommand(agentReleaseCmd)
testCmd.AddCommand(loopCmd)
Expand Down Expand Up @@ -80,6 +81,11 @@ var adminUpdateCmd = &cobra.Command{
Short: "Update global resources",
}

var agentAccessCmd = &cobra.Command{
Use: "access",
Short: "zrok Agent access commands",
}

var agentCmd = &cobra.Command{
Use: "agent",
Short: "zrok Agent commands",
Expand Down

0 comments on commit fb23d23

Please sign in to comment.